1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
|
# -*- coding: utf-8 -*-
# Copyright Martin Manns
# Distributed under the terms of the GNU General Public License
# --------------------------------------------------------------------
# pyspread is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyspread is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyspread. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------
"""
Pyspread's main grid
**Provides**
* :class:`Grid`: QTableView of the main grid
* :class:`GridHeaderView`: QHeaderView for the main grids headers
* :class:`GridTableModel`: QAbstractTableModel linking the view to code_array
backend
* :class:`GridCellDelegate`: QStyledItemDelegate handling custom painting and
editors
* :class:`TableChoice`: The TabBar below the main grid
"""
from ast import literal_eval
from contextlib import contextmanager
from datetime import datetime, date, time
from io import BytesIO
from typing import Any, Iterable, List, Tuple, Union
import numpy
from PyQt6.QtWidgets \
import (QTableView, QStyledItemDelegate, QTabBar, QWidget, QMainWindow,
QStyleOptionViewItem, QApplication, QStyle, QAbstractItemDelegate,
QHeaderView, QFontDialog, QInputDialog, QLineEdit,
QAbstractItemView)
from PyQt6.QtGui \
import (QColor, QBrush, QFont, QPainter, QPalette, QImage, QKeyEvent,
QTextOption, QAbstractTextDocumentLayout, QTextDocument,
QWheelEvent, QContextMenuEvent, QTextCursor)
from PyQt6.QtCore \
import (Qt, QAbstractTableModel, QModelIndex, QVariant, QEvent, QSize,
QRect, QRectF, QItemSelectionModel, QObject, QAbstractItemModel,
QByteArray, pyqtSignal)
from PyQt6.QtSvg import QSvgRenderer
try:
import matplotlib
import matplotlib.figure
except ImportError:
matplotlib = None
try:
from moneyed import Money
except ImportError:
Money = None
try:
from pyspread import commands
from pyspread.dialogs import DiscardDataDialog
from pyspread.grid_renderer import (painter_save, CellRenderer,
QColorCache, BorderWidthBottomCache,
BorderWidthRightCache,
EdgeBordersCache,
BorderColorRightCache,
BorderColorBottomCache)
from pyspread.model.model import (CodeArray, CellAttribute,
DefaultCellAttributeDict,
class_format_functions)
from pyspread.lib.attrdict import AttrDict
from pyspread.interfaces.pys import (qt52qt6_fontweights,
qt62qt5_fontweights)
from pyspread.lib.selection import Selection
from pyspread.lib.string_helpers import quote, wrap_text
from pyspread.lib.qimage2ndarray import array2qimage
from pyspread.lib.typechecks import is_svg, check_shape_validity
from pyspread.menus import (GridContextMenu, TableChoiceContextMenu,
HorizontalHeaderContextMenu,
VerticalHeaderContextMenu)
from pyspread.themes import ColorRole
from pyspread.widgets import CellButton
except ImportError:
import commands
from dialogs import DiscardDataDialog
from grid_renderer import (painter_save, CellRenderer, QColorCache,
BorderWidthBottomCache, BorderWidthRightCache,
EdgeBordersCache, BorderColorRightCache,
BorderColorBottomCache)
from model.model import (CodeArray, CellAttribute,
DefaultCellAttributeDict, class_format_functions)
from lib.attrdict import AttrDict
from interfaces.pys import qt52qt6_fontweights, qt62qt5_fontweights
from lib.selection import Selection
from lib.string_helpers import quote, wrap_text
from lib.qimage2ndarray import array2qimage
from lib.typechecks import is_svg, check_shape_validity
from menus import (GridContextMenu, TableChoiceContextMenu,
HorizontalHeaderContextMenu, VerticalHeaderContextMenu)
from themes import ColorRole
from widgets import CellButton
FONTSTYLES = (QFont.Style.StyleNormal,
QFont.Style.StyleItalic,
QFont.Style.StyleOblique)
class Grid(QTableView):
"""The main grid of pyspread"""
def __init__(self, main_window: QMainWindow, model=None):
"""
:param main_window: Application main window
:param model: GridTableModel for grid
"""
super().__init__()
self.main_window = main_window
shape = main_window.settings.shape
if model is None:
self.model = GridTableModel(main_window, shape)
else:
self.model = model
self.setModel(self.model)
self.qcolor_cache = QColorCache(self)
self.borderwidth_bottom_cache = BorderWidthBottomCache(self)
self.borderwidth_right_cache = BorderWidthRightCache(self)
self.edge_borders_cache = EdgeBordersCache()
self.border_color_bottom_cache = BorderColorBottomCache(self)
self.border_color_right_cache = BorderColorRightCache(self)
self.table_choice = main_window.table_choice
self.widget_indices = [] # Store each index with an indexWidget here
# Signals
self.model.dataChanged.connect(self.on_data_changed)
self.selectionModel().currentChanged.connect(self.on_current_changed)
self.selectionModel().selectionChanged.connect(
self.on_selection_changed)
self.setHorizontalHeader(GridHeaderView(Qt.Orientation.Horizontal,
self))
self.setVerticalHeader(GridHeaderView(Qt.Orientation.Vertical, self))
self.verticalHeader().setDefaultSectionSize(
self.main_window.settings.default_row_height)
self.horizontalHeader().setDefaultSectionSize(
self.main_window.settings.default_column_width)
self.verticalHeader().setMinimumSectionSize(0)
self.horizontalHeader().setMinimumSectionSize(0)
self.setCornerButtonEnabled(False)
self._zoom = 1.0 # Initial zoom level for the grid
self.current_selection_mode_start = None
self.selection_mode_exiting = False # True only during exit
self.verticalHeader().sectionResized.connect(self.on_row_resized)
self.horizontalHeader().sectionResized.connect(self.on_column_resized)
self.setShowGrid(False)
self.delegate = GridCellDelegate(main_window, self,
self.model.code_array)
self.setItemDelegate(self.delegate)
# Select upper left cell because initial selection behaves strange
self.reset_selection()
# Locking states for operations by undo and redo operations
self.__undo_resizing_row = False
self.__undo_resizing_column = False
# Initially, select top left cell on table 0
self.current = 0, 0, 0
# Store initial viewport
self.table_scrolls = {0: (self.verticalScrollBar().value(),
self.horizontalScrollBar().value())}
@contextmanager
def undo_resizing_row(self):
"""Sets self.__undo_resizing_row to True for context"""
self.__undo_resizing_row = True
yield
self.__undo_resizing_row = False
@contextmanager
def undo_resizing_column(self):
"""Sets self.__undo_resizing_column to True for context"""
self.__undo_resizing_column = True
yield
self.__undo_resizing_column = False
@property
def row(self) -> int:
"""Current row"""
return self.currentIndex().row()
@row.setter
def row(self, value: int):
"""Sets current row to value
:param value: Row to be made current
"""
self.current = value, self.column
@property
def column(self) -> int:
"""Current column"""
return self.currentIndex().column()
@column.setter
def column(self, value: int):
"""Sets current column to value
:param value: Column to be made current
"""
self.current = self.row, value
@property
def table(self) -> int:
"""Current table"""
return self.table_choice.table
@table.setter
def table(self, value: int):
"""Sets current table
:param value: Table to be made current
"""
if 0 <= value < self.model.shape[2]:
self.table_choice.table = value
@property
def current(self) -> Tuple[int, int, int]:
"""Tuple of row, column, table of the current index"""
return self.row, self.column, self.table
@current.setter
def current(self, value: Union[Tuple[int, int, int], Tuple[int, int]]):
"""Sets the current index to row, column and if given table
:param value: Key of cell to be made current
"""
if len(value) not in (2, 3):
msg = "Current cell must be defined with a tuple " + \
"(row, column) or (rol, column, table)."
raise ValueError(msg)
row, column, *table_list = value
if not 0 <= row < self.model.shape[0]:
row = self.row
if not 0 <= column < self.model.shape[1]:
column = self.column
if table_list:
self.table = table_list[0]
index = self.model.index(row, column, QModelIndex())
self.setCurrentIndex(index)
@property
def row_heights(self) -> List[Tuple[int, float]]:
"""Returns list of tuples (row_index, row height) for current table"""
row_heights = self.model.code_array.row_heights
return [(row, row_heights[row, tab]) for row, tab in row_heights
if tab == self.table]
@property
def column_widths(self) -> List[Tuple[int, float]]:
"""Returns list of tuples (col_index, col_width) for current table"""
col_widths = self.model.code_array.col_widths
return [(col, col_widths[col, tab]) for col, tab in col_widths
if tab == self.table]
@property
def selection(self) -> Selection:
"""Pyspread selection based on self's QSelectionModel"""
if len(self.selected_idx) == 1:
# Return current cell selection to get accurate results
current = tuple(self.main_window.focused_grid.current[:2])
return Selection([], [], [], [], [current])
selection = self.main_window.focused_grid.selectionModel().selection()
block_top_left = []
block_bottom_right = []
rows = []
columns = []
cells = []
# Selection are made of selection ranges that we call span
for span in selection:
top, bottom = span.top(), span.bottom()
left, right = span.left(), span.right()
if top == bottom and left == right:
# The span is a single cell
cells.append((top, right))
elif left == 0 and right == self.model.shape[1] - 1:
# The span consists of selected rows
rows += list(range(top, bottom + 1))
elif top == 0 and bottom == self.model.shape[0] - 1:
# The span consists of selected columns
columns += list(range(left, right + 1))
else:
# Otherwise append a block
block_top_left.append((top, left))
block_bottom_right.append((bottom, right))
return Selection(block_top_left, block_bottom_right,
rows, columns, cells)
@property
def selected_idx(self) -> List[QModelIndex]:
"""Currently selected indices"""
return self.main_window.focused_grid.selectionModel().selectedIndexes()
@property
def zoom(self) -> float:
"""Returns zoom level"""
return self._zoom
@zoom.setter
def zoom(self, zoom: float):
"""Updates _zoom property and zoom visualization of the grid
Does nothing if not between minimum and maximum of settings.zoom_levels
:param zoom: Zoom level to be set
"""
zoom_levels = self.main_window.settings.zoom_levels
if min(zoom_levels) <= zoom <= max(zoom_levels):
self._zoom = zoom
self.update_zoom()
@property
def selection_mode(self) -> bool:
"""In selection mode, cells cannot be edited"""
return self.editTriggers() \
== QAbstractItemView.EditTrigger.NoEditTriggers
@selection_mode.setter
def selection_mode(self, on: bool):
"""Sets or unsets selection mode for this grid
In selection mode, cells cannot be edited.
This triggers the selection_mode icon in the statusbar.
:param on: If True, selection mode is set, if False unset
"""
grid = self.main_window.focused_grid
if on:
self.current_selection_mode_start = tuple(grid.current)
self.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
self.main_window.selection_mode_widget.show()
else:
self.selection_mode_exiting = True
if self.current_selection_mode_start is not None:
grid.current = self.current_selection_mode_start
self.current_selection_mode_start = None
self.setEditTriggers(QAbstractItemView.EditTrigger.DoubleClicked
| QAbstractItemView.EditTrigger.EditKeyPressed
| QAbstractItemView.EditTrigger.AnyKeyPressed)
self.selection_mode_exiting = False
self.main_window.selection_mode_widget.hide()
self.main_window.entry_line.setFocus()
def set_selection_mode(self, value=True):
"""Setter for selection mode for all grids
:param value: If True, selection mode is set, if False unset
"""
# All grids must simultaneously got into or out of selection mode
for grid in self.main_window.grids:
grid.selection_mode = value
# Adjust the menu
main_window_actions = self.main_window.main_window_actions
toggle_selection_mode = main_window_actions.toggle_selection_mode
toggle_selection_mode.setChecked(value)
def toggle_selection_mode(self):
"""Toggle selection mode for all grids
This method is required for accessing selection mode from QActions.
"""
main_window_actions = self.main_window.main_window_actions
toggle_selection_mode = main_window_actions.toggle_selection_mode
value = toggle_selection_mode.toggled
for grid in self.main_window.grids:
grid.selection_mode = value
# Overrides
def focusInEvent(self, event):
"""Overrides focusInEvent storing last focused grid in main_window"""
self.main_window._last_focused_grid = self
super().focusInEvent(event)
def closeEditor(self, editor: QWidget,
hint: QAbstractItemDelegate.EndEditHint):
"""Overrides QTableView.closeEditor
Changes to overridden behavior:
* Data is submitted when a cell is changed without pressing <Enter>
e.g. by mouse click or arrow keys.
:param editor: Editor to be closed
:param hint: Hint to be overridden if == `QAbstractItemDelegate.NoHint`
"""
if hint == QAbstractItemDelegate.EndEditHint.NoHint:
hint = QAbstractItemDelegate.EndEditHint.SubmitModelCache
super().closeEditor(editor, hint)
def keyPressEvent(self, event: QKeyEvent):
"""Overrides QTableView.keyPressEvent
Changes to overridden behavior:
* If Shift is pressed, the cell in the next column is selected.
* If Shift is not pressed, the cell in the next row is selected.
:param event: Key event
"""
if event.key() in (Qt.Key.Key_Enter, Qt.Key.Key_Return):
if self.selection_mode:
# Return exits selection mode
self.selection_mode = False
self.main_window.entry_line.setFocus()
elif event.modifiers() & Qt.KeyboardModifier.ShiftModifier:
self.current = self.row, self.column + 1
else:
self.current = self.row + 1, self.column
elif event.key() in (Qt.Key.Key_Backspace, Qt.Key.Key_Delete):
self.main_window.workflows.delete()
elif (event.key() == Qt.Key.Key_Escape
and self.editTriggers()
== QAbstractItemView.EditTrigger.NoEditTriggers):
# Leave cell selection mode
self.selection_mode = False
else:
super().keyPressEvent(event)
def wheelEvent(self, event: QWheelEvent):
"""Overrides mouse wheel event handler
:param event: Mouse wheel event
"""
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.KeyboardModifier.ControlModifier:
if event.angleDelta().y() > 0:
self.on_zoom_in()
else:
self.on_zoom_out()
else:
super().wheelEvent(event)
def contextMenuEvent(self, event: QContextMenuEvent):
"""Overrides contextMenuEvent to install GridContextMenu
:param event: Context menu event
"""
menu = GridContextMenu(self.main_window.main_window_actions)
menu.exec(self.mapToGlobal(event.pos()))
# Helpers
def reset_selection(self):
"""Select upper left cell"""
self.setSelection(QRect(1, 1, 1, 1),
QItemSelectionModel.SelectionFlag.Select)
def gui_update(self):
"""Emits gui update signal"""
attributes = self.model.code_array.cell_attributes[self.current]
self.main_window.gui_update.emit(attributes)
def adjust_size(self):
"""Adjusts size to header maxima"""
horizontal_header = self.horizontalHeader()
vertical_header = self.verticalHeader()
width = horizontal_header.length() + vertical_header.width()
height = vertical_header.length() + horizontal_header.height()
self.resize(width, height)
def _selected_idx_to_str(self, selected_idx: Iterable[QModelIndex]) -> str:
"""Converts selected_idx to string with cell indices
:param selected_idx: Indices of selected cells
"""
if len(selected_idx) <= 6:
return ", ".join(str(self.model.current(idx))
for idx in selected_idx)
return ", ".join(str(self.model.current(idx))
for idx in selected_idx[:6]) + "..."
def update_zoom(self):
"""Updates the zoom level visualization to the current zoom factor"""
self.verticalHeader().update_zoom()
self.horizontalHeader().update_zoom()
def has_selection(self) -> bool:
"""Returns True if more than one cell is selected, else False
This method handles spanned/merged cells. One single cell that is
selected is considered as no cell being selected.
"""
cell_attributes = self.model.code_array.cell_attributes
merge_area = cell_attributes[self.current].merge_area
if merge_area is None:
merge_sel = Selection([], [], [], [], [])
else:
top, left, bottom, right = merge_area
merge_sel = Selection([(top, left)], [(bottom, right)], [], [], [])
return not (self.selection.single_cell_selected()
or merge_sel.get_bbox() == self.selection.get_bbox())
# Event handlers
def on_data_changed(self):
"""Event handler for data changes"""
self.qcolor_cache.clear()
self.borderwidth_bottom_cache.clear()
self.borderwidth_right_cache.clear()
self.edge_borders_cache.clear()
self.border_color_bottom_cache.clear()
self.border_color_right_cache.clear()
if not self.main_window.settings.changed_since_save:
self.main_window.settings.changed_since_save = True
main_window_title = "* " + self.main_window.windowTitle()
self.main_window.setWindowTitle(main_window_title)
def on_current_changed(self, *_: Any):
"""Event handler for change of current cell"""
if self.selection_mode_exiting:
# Do not update entry_line to preserve selection
return
if self.selection_mode:
cursor = self.main_window.entry_line.textCursor()
text_anchor = cursor.anchor()
text_position = cursor.position()
if QApplication.queryKeyboardModifiers() \
== Qt.KeyboardModifier.MetaModifier:
text = self.selection.get_absolute_access_string(
self.model.shape, self.table)
else:
text = self.selection.get_relative_access_string(
self.model.shape, self.current_selection_mode_start)
self.main_window.entry_line.insertPlainText(text)
cursor.setPosition(min(text_anchor, text_position))
cursor.setPosition(min(text_anchor, text_position) + len(text),
QTextCursor.MoveMode.KeepAnchor)
self.main_window.entry_line.setTextCursor(cursor)
else:
code = self.model.code_array(self.current)
self.main_window.entry_line.setPlainText(code)
self.gui_update()
def on_selection_changed(self):
"""Selection changed event handler"""
if not self.main_window.settings.show_statusbar_sum:
return
try:
selection = self.selection
code_array = self.model.code_array
single_cell_selected = selection.single_cell_selected()
except AttributeError:
return
if not selection or single_cell_selected:
self.main_window.statusBar().clearMessage()
return
selected_cell_list = list(selection.cell_generator(self.model.shape,
self.table))
res_gen = (code_array[key] for key in selected_cell_list
if code_array(key))
sum_list = [res for res in res_gen if res is not None]
msg_tpl = " " + " ".join(["Σ={}", "max={}", "min={}"])
msg = f"Selection: {len(selected_cell_list)} cells"
if sum_list:
try:
msg += msg_tpl.format(sum(sum_list), max(sum_list),
min(sum_list))
except Exception:
pass
self.main_window.statusBar().showMessage(msg)
def on_row_resized(self, row: int, old_height: float, new_height: float):
"""Row resized event handler
:param row: Row that is resized
:param old_height: Row height before resizing
:param new_height: Row height after resizing
"""
if self.__undo_resizing_row: # Resize from undo or redo command
return
(top, _), (bottom, _) = self.selection.get_grid_bbox(self.model.shape)
if bottom - top > 1 and top <= row <= bottom:
rows = list(range(top, bottom + 1))
else:
rows = [row]
description = f"Resize rows {rows} to {new_height}"
command = commands.SetRowsHeight(self, rows, self.table,
old_height / self.zoom,
new_height / self.zoom, description)
self.main_window.undo_stack.push(command)
def on_column_resized(self, column: int, old_width: float,
new_width: float):
"""Column resized event handler
:param row: Column that is resized
:param old_width: Column width before resizing
:param new_width: Column width after resizing
"""
if self.__undo_resizing_column: # Resize from undo or redo command
return
(_, left), (_, right) = self.selection.get_grid_bbox(self.model.shape)
if right - left > 1 and left <= column <= right:
columns = list(range(left, right + 1))
else:
columns = [column]
description = f"Resize columns {columns} to {new_width}"
command = commands.SetColumnsWidth(self, columns, self.table,
old_width / self.zoom,
new_width / self.zoom, description)
self.main_window.undo_stack.push(command)
def on_zoom_in(self):
"""Zoom in event handler"""
grid = self.main_window.focused_grid
zoom_levels = self.main_window.settings.zoom_levels
larger_zoom_levels = [zl for zl in zoom_levels if zl > grid.zoom]
if larger_zoom_levels:
grid.zoom = min(larger_zoom_levels)
def on_zoom_out(self):
"""Zoom out event handler"""
grid = self.main_window.focused_grid
zoom_levels = self.main_window.settings.zoom_levels
smaller_zoom_levels = [zl for zl in zoom_levels if zl < grid.zoom]
if smaller_zoom_levels:
grid.zoom = max(smaller_zoom_levels)
def on_zoom_1(self):
"""Sets zoom level ot 1.0"""
grid = self.main_window.focused_grid
grid.zoom = 1.0
def _refresh_frozen_cell(self, key: Tuple[int, int, int]):
"""Refreshes the frozen cell key
Does neither emit dataChanged nor clear _attr_cache or _table_cache.
:param key: Key of cell to be refreshed
"""
if self.model.code_array.cell_attributes[key].frozen:
code = self.model.code_array(key)
result = self.model.code_array._eval_cell(key, code)
self.model.code_array.frozen_cache[repr(key)] = result
def refresh_frozen_cells(self):
"""Refreshes all frozen cells"""
frozen_cache = self.model.code_array.frozen_cache
cell_attributes = self.model.code_array.cell_attributes
for repr_key in frozen_cache:
key = literal_eval(repr_key)
self._refresh_frozen_cell(key)
self.model.dataChanged.emit(QModelIndex(), QModelIndex())
def refresh_selected_frozen_cells(self):
"""Refreshes selected frozen cells"""
for idx in self.selected_idx:
self._refresh_frozen_cell((idx.row(), idx.column(), self.table))
self.model.code_array.cell_attributes._attr_cache.clear()
self.model.code_array.cell_attributes._table_cache.clear()
self.model.code_array.result_cache.clear()
self.model.dataChanged.emit(QModelIndex(), QModelIndex())
def on_show_frozen_pressed(self, toggled: bool):
"""Show frozen cells event handler
:param toggled: Toggle state
"""
self.main_window.settings.show_frozen = toggled
def on_format_default(self):
"""Reset cell format to system theme by setting values to None
The merge status of cells is unaffected
"""
attr_dict = DefaultCellAttributeDict()
attr_dict.pop("merge_area") # Merged cells are not undone
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Reset cell format for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_font_dialog(self):
"""Font dialog event handler"""
# Determine currently active font as dialog preset
font = self.model.font(self.current)
font, ok = QFontDialog().getFont(font, self.main_window)
if ok:
attr_dict = AttrDict()
attr_dict.textfont = font.family()
attr_dict.pointsize = font.pointSizeF()
attr_dict.fontweight = qt62qt5_fontweights(font.weight())
attr_dict.fontstyle = FONTSTYLES.index(font.style())
attr_dict.underline = font.underline()
attr_dict.strikethrough = font.strikeOut()
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set font {font} for indices {idx_string}"
command = commands.SetCellFormat(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_font(self):
"""Font change event handler"""
font = self.main_window.widgets.font_combo.font
attr_dict = AttrDict([("textfont", font)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set font {font} for indices {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_font_size(self):
"""Font size change event handler"""
size = self.main_window.widgets.font_size_combo.size
attr_dict = AttrDict([("pointsize", size)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set font size {size} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_bold_pressed(self, toggled: bool):
"""Bold button pressed event handler
:param toggled: Toggle state
"""
fontweight = QFont.Weight.Bold if toggled else QFont.Weight.Normal
attr_dict = AttrDict([("fontweight", qt62qt5_fontweights(fontweight))])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set font weight {fontweight} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_italics_pressed(self, toggled: bool):
"""Italics button pressed event handler
:param toggled: Toggle state
"""
fontstyle = QFont.Style.StyleItalic \
if toggled else QFont.Style.StyleNormal
attr_dict = AttrDict([("fontstyle", FONTSTYLES.index(fontstyle))])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set font style {fontstyle} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_underline_pressed(self, toggled: bool):
"""Underline button pressed event handler
:param toggled: Toggle state
"""
attr_dict = AttrDict([("underline", toggled)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set font underline {toggled} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_strikethrough_pressed(self, toggled: bool):
"""Strikethrough button pressed event handler
:param toggled: Toggle state
"""
attr_dict = AttrDict([("strikethrough", toggled)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = \
f"Set font strikethrough {toggled} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_text_renderer_pressed(self):
"""Text renderer button pressed event handler"""
attr_dict = AttrDict([("renderer", "text")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set text renderer for cells {idx_string}"
entry_line = self.main_window.entry_line
document = entry_line.document()
# Disable highlighter to speed things up
highlighter_limit = self.main_window.settings.highlighter_limit
if len(document.toRawText()) > highlighter_limit:
document = None
command = commands.SetCellRenderer(attr, self.model, entry_line,
document, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_image_renderer_pressed(self):
"""Image renderer button pressed event handler"""
attr_dict = AttrDict([("renderer", "image")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set image renderer for cells {idx_string}"
entry_line = self.main_window.entry_line
command = commands.SetCellRenderer(attr, self.model, entry_line, None,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_markup_renderer_pressed(self):
"""Markup renderer button pressed event handler"""
attr_dict = AttrDict([("renderer", "markup")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set markup renderer for cells {idx_string}"
entry_line = self.main_window.entry_line
document = entry_line.document()
# Disable highlighter to speed things up
highlighter_limit = self.main_window.settings.highlighter_limit
if len(document.toRawText()) > highlighter_limit:
document = None
command = commands.SetCellRenderer(attr, self.model, entry_line,
document, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_matplotlib_renderer_pressed(self):
"""Matplotlib renderer button pressed event handler"""
attr_dict = AttrDict([("renderer", "matplotlib")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set matplotlib renderer for cells {idx_string}"
entry_line = self.main_window.entry_line
document = entry_line.document()
# Disable highlighter to speed things up
highlighter_limit = self.main_window.settings.highlighter_limit
if len(document.toRawText()) > highlighter_limit:
document = None
command = commands.SetCellRenderer(attr, self.model, entry_line,
document, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_lock_pressed(self, toggled: bool):
"""Lock button pressed event handler
:param toggled: Toggle state
"""
attr_dict = AttrDict([("locked", toggled)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set locked state to {toggled} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_rotate_0(self):
"""Set cell rotation to 0° left button pressed event handler"""
attr_dict = AttrDict([("angle", 0.0)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set cell rotation to 0° for cells {idx_string}"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_rotate_90(self):
"""Set cell rotation to 90° left button pressed event handler"""
attr_dict = AttrDict([("angle", 90.0)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set cell rotation to 90° for cells {idx_string}"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_rotate_180(self):
"""Set cell rotation to 180° left button pressed event handler"""
attr_dict = AttrDict([("angle", 180.0)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set cell rotation to 180° for cells {idx_string}"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_rotate_270(self):
"""Set cell rotation to 270° left button pressed event handler"""
attr_dict = AttrDict([("angle", 270.0)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set cell rotation to 270° for cells {idx_string}"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_justify_left(self):
"""Justify left button pressed event handler"""
attr_dict = AttrDict([("justification", "justify_left")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Justify cells {idx_string} left"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_justify_fill(self):
"""Justify fill button pressed event handler"""
attr_dict = AttrDict([("justification", "justify_fill")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Justify cells {idx_string} filled"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_justify_center(self):
"""Justify center button pressed event handler"""
attr_dict = AttrDict([("justification", "justify_center")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Justify cells {idx_string} centered"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_justify_right(self):
"""Justify right button pressed event handler"""
attr_dict = AttrDict([("justification", "justify_right")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Justify cells {idx_string} right"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_align_top(self):
"""Align top button pressed event handler"""
attr_dict = AttrDict([("vertical_align", "align_top")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Align cells {idx_string} to top"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_align_middle(self):
"""Align centere button pressed event handler"""
attr_dict = AttrDict([("vertical_align", "align_center")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Align cells {idx_string} to center"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_align_bottom(self):
"""Align bottom button pressed event handler"""
attr_dict = AttrDict([("vertical_align", "align_bottom")])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Align cells {idx_string} to bottom"
command = commands.SetCellTextAlignment(attr, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_border_choice(self):
"""Border choice style event handler"""
self.main_window.settings.border_choice = self.sender().text()
self.gui_update()
def on_text_color(self):
"""Text color change event handler"""
text_color = self.main_window.widgets.text_color_button.color
text_color_rgb = text_color.getRgb()
attr_dict = AttrDict([("textcolor", text_color_rgb)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = \
f"Set text color to {text_color_rgb} for cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_line_color(self):
"""Line color change event handler"""
border_choice = self.main_window.settings.border_choice
bottom_selection = \
self.selection.get_bottom_borders_selection(border_choice,
self.model.shape)
right_selection = \
self.selection.get_right_borders_selection(border_choice,
self.model.shape)
line_color = self.main_window.widgets.line_color_button.color
line_color_rgb = line_color.getRgb()
attr_dict_bottom = AttrDict([("bordercolor_bottom", line_color_rgb)])
attr_bottom = CellAttribute(bottom_selection, self.table,
attr_dict_bottom)
attr_dict_right = AttrDict([("bordercolor_right", line_color_rgb)])
attr_right = CellAttribute(right_selection, self.table,
attr_dict_right)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set line color {line_color_rgb} for cells {idx_string}"
command = commands.SetCellFormat(attr_bottom, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
command = commands.SetCellFormat(attr_right, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_background_color(self):
"""Background color change event handler"""
bg_color = self.main_window.widgets.background_color_button.color
bg_color_rgb = bg_color.getRgb()
self.gui_update()
attr_dict = AttrDict([("bgcolor", bg_color_rgb)])
attr = CellAttribute(self.selection, self.table, attr_dict)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set cell background color to {bg_color_rgb} for " +\
f"cells {idx_string}"
command = commands.SetCellFormat(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def on_borderwidth(self):
"""Border width change event handler"""
width = int(self.sender().text().split()[-1])
border_choice = self.main_window.settings.border_choice
bottom_selection = \
self.selection.get_bottom_borders_selection(border_choice,
self.model.shape)
right_selection = \
self.selection.get_right_borders_selection(border_choice,
self.model.shape)
attr_dict_bottom = AttrDict([("borderwidth_bottom", width)])
attr_bottom = CellAttribute(bottom_selection, self.table,
attr_dict_bottom)
attr_dict_right = AttrDict([("borderwidth_right", width)])
attr_right = CellAttribute(right_selection, self.table,
attr_dict_right)
idx_string = self._selected_idx_to_str(self.selected_idx)
description = f"Set border width to {width} for cells {idx_string}"
command = commands.SetCellFormat(attr_bottom, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
command = commands.SetCellFormat(attr_right, self.model,
self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
def update_cell_spans(self):
"""Update cell spans from model data"""
self.clearSpans()
spans = {} # Dict of (top, left): (bottom, right)
for _, table, attrs in self.model.code_array.cell_attributes:
if table == self.table:
try:
if "merge_area" in attrs and attrs.merge_area is not None:
top, left, bottom, right = attrs["merge_area"]
spans[(top, left)] = bottom, right
except (KeyError, TypeError):
pass
for top, left in spans:
try:
bottom, right = spans[(top, left)]
self.setSpan(top, left, bottom-top+1, right-left+1)
except TypeError:
pass
def update_index_widgets(self):
"""Remove old index widgets from model data"""
# Remove old button cells
for index in self.widget_indices:
self.setIndexWidget(index, None)
self.widget_indices.clear()
# Get button cell candidates
code_array = self.model.code_array
button_cell_candidates = []
for selection, table, attr in code_array.cell_attributes:
if table == self.table and 'button_cell' in attr \
and attr['button_cell']:
row, column = selection.get_bbox()[0]
button_cell_candidates.append((row, column, table))
# Add button cells for current table
for key in set(button_cell_candidates):
text = code_array.cell_attributes[key]['button_cell']
if text is not False: # False would be deleted button cell
row, column, table = key
index = self.model.index(row, column, QModelIndex())
button = CellButton(text, self, key)
self.setIndexWidget(index, button)
self.widget_indices.append(index)
def on_freeze_pressed(self, toggled: bool):
"""Freeze cell event handler
:param toggled: Toggle state
"""
grid = self.main_window.focused_grid
current_attr = self.model.code_array.cell_attributes[grid.current]
if current_attr.frozen == toggled:
return # Something is wrong with the GUI update
cells = list(self.selection.cell_generator(shape=self.model.shape,
table=self.table))
if toggled:
# We have an non-frozen cell that has to be frozen
description = f"Freeze cells {cells}"
command = commands.FreezeCell(self.model, cells, description)
else:
# We have an frozen cell that has to be unfrozen
description = f"Thaw cells {cells}"
command = commands.ThawCell(self.model, cells, description)
self.main_window.undo_stack.push(command)
def on_button_cell_pressed(self, toggled: bool):
"""Button cell event handler
:param toggled: Toggle state
"""
grid = self.main_window.focused_grid
current_attr = grid.model.code_array.cell_attributes[grid.current]
if not toggled and current_attr.button_cell is False \
or toggled and current_attr.button_cell is not False:
# Something is not synchronized in the menu
return
if toggled:
# Get button text from user
text, accept = QInputDialog.getText(
self.main_window, "Make button cell", "Button text:",
QLineEdit.EchoMode.Normal, "")
if accept and text:
description = f"Make cell {grid.current} a button cell"
command = commands.MakeButtonCell(self, text,
grid.currentIndex(),
description)
self.main_window.undo_stack.push(command)
else:
description = f"Make cell {grid.current} a non-button cell"
command = commands.RemoveButtonCell(self, grid.currentIndex(),
description)
self.main_window.undo_stack.push(command)
def on_merge_pressed(self):
"""Merge cells button pressed event handler"""
grid = self.main_window.focused_grid
# This is not done in the model because setSpan does not work there
shape = list(self.model.shape)
shape[0] -= 1
shape[1] -= 1
bbox = self.selection.get_grid_bbox(shape)
(top, left), (bottom, right) = bbox
# Check if current cell is already merged
if self.columnSpan(top, left) > 1 or self.rowSpan(top, left) > 1:
selection = Selection([], [], [], [], [(top, left)])
attr_dict = AttrDict([("merge_area", None)])
attr = CellAttribute(selection, self.table, attr_dict)
description = f"Unmerge cells with top-left cell {(top, left)}"
elif bottom > top or right > left:
# Merge and store the current selection
merging_selection = Selection([], [], [], [], [(top, left)])
attr_dict = AttrDict([("merge_area", (top, left, bottom, right))])
attr = CellAttribute(merging_selection, self.table, attr_dict)
description = "Merge cells with top-left cell {(top, left)}"
else:
# Cells are not merged because the span is one
return
command = commands.SetCellMerge(attr, self.model, self.currentIndex(),
self.selected_idx, description)
self.main_window.undo_stack.push(command)
grid.current = top, left
def on_quote(self):
"""Quote cells event handler"""
description = f"Quote code for cell selection {id(self.selection)}"
for idx in self.selected_idx:
row = idx.row()
column = idx.column()
code = self.model.code_array((row, column, self.table))
quoted_code = quote(code)
index = self.model.index(row, column, QModelIndex())
command = commands.SetCellCode(quoted_code, self.model, index,
description)
self.main_window.undo_stack.push(command)
def on_money(self):
"""Make cell money object event handler using default currency"""
description = f"Money type for cell selection {id(self.selection)}"
for idx in self.selected_idx:
row = idx.row()
column = idx.column()
key = row, column, self.table
code = self.model.code_array(key)
res = self.model.code_array[key]
if isinstance(res, Money):
return
if isinstance(res, float):
code = quote(code)
currency_iso_code = self.main_window.settings.currency_iso_code
moneyed_code = f'Money({code}, "{currency_iso_code}")'
index = self.model.index(row, column, QModelIndex())
command = commands.SetCellCode(moneyed_code, self.model, index,
description)
self.main_window.undo_stack.push(command)
def on_datetime(self):
"""Make cell datetime object event handler"""
description = f"Datetime type for cell selection {id(self.selection)}"
for idx in self.selected_idx:
row = idx.row()
column = idx.column()
key = row, column, self.table
code = self.model.code_array(key)
res = self.model.code_array[key]
if isinstance(res, datetime):
return
if not isinstance(res, str):
code = quote(code)
datetime_code = f'dateutil.parser.parse({code})'
index = self.model.index(row, column, QModelIndex())
command = commands.SetCellCode(datetime_code, self.model, index,
description)
self.main_window.undo_stack.push(command)
def on_date(self):
"""Make cell date object event handler"""
description = f"Date type for cell selection {id(self.selection)}"
for idx in self.selected_idx:
row = idx.row()
column = idx.column()
key = row, column, self.table
code = self.model.code_array(key)
res = self.model.code_array[key]
if isinstance(res, date):
return
if not isinstance(res, str):
code = quote(code)
datetime_code = f'dateutil.parser.parse({code}).date()'
index = self.model.index(row, column, QModelIndex())
command = commands.SetCellCode(datetime_code, self.model, index,
description)
self.main_window.undo_stack.push(command)
def on_time(self):
"""Make cell time object event handler"""
description = f"Time type for cell selection {id(self.selection)}"
for idx in self.selected_idx:
row = idx.row()
column = idx.column()
key = row, column, self.table
code = self.model.code_array(key)
res = self.model.code_array[key]
if isinstance(res, time):
return
if not isinstance(res, str):
code = quote(code)
datetime_code = f'dateutil.parser.parse({code}).time()'
index = self.model.index(row, column, QModelIndex())
command = commands.SetCellCode(datetime_code, self.model, index,
description)
self.main_window.undo_stack.push(command)
def is_row_data_discarded(self, count: int) -> bool:
"""True if row data is to be discarded on row insertion
:param count: Rows to be inserted
"""
no_rows = self.model.shape[0]
rows = list(range(no_rows-count, no_rows+1))
selection = Selection([], [], rows, [], [])
sel_cell_gen = selection.cell_generator(self.model.shape, self.table)
return any(self.model.code_array(key) is not None
for key in sel_cell_gen)
def is_column_data_discarded(self, count: int) -> bool:
"""True if column data is to be discarded on column insertion
:param count: Columns to be inserted
"""
no_columns = self.model.shape[1]
columns = list(range(no_columns-count, no_columns+1))
selection = Selection([], [], [], columns, [])
sel_cell_gen = selection.cell_generator(self.model.shape, self.table)
return any(self.model.code_array(key) is not None
for key in sel_cell_gen)
def is_table_data_discarded(self, count: int) -> bool:
"""True if table data is to be discarded on table insertion
:param count: Tables to be inserted
"""
no_tables = self.model.shape[2]
tables = list(range(no_tables-count, no_tables+1))
return any(key[2] in tables and self.model.code_array(key) is not None
for key in self.model.code_array)
def on_shift_cells_down(self, *,
description_tpl: str = "Shift selection {} down"):
"""Insert shift cells down event handler
:param description_tpl: Description template for `QUndoCommand`
"""
try:
(top, left), (bottom, right) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
top = bottom = self.row
left = right = self.column
count = bottom - top + 1
if self.is_row_data_discarded(count):
text = ("Shifting cells down will discard data.\n \n"
"You may want to resize the grid before shifting.")
if DiscardDataDialog(self.main_window, text).choice is not True:
return
selection1 = Selection([(top, left)], [(None, right)], [], [], [])
self.main_window.workflows.edit_cut(description_tpl=description_tpl,
selection=selection1)
clipboard = QApplication.clipboard()
data = clipboard.text()
selection2 = Selection([(bottom+1, left)], [(None, right)],
[], [], [])
self.main_window.workflows._paste_to_selection(
selection2, data,
description_tpl=description_tpl.format(selection1))
def on_shift_cells_right(
self, *, description_tpl: str = "Shift selection {} right"):
"""Insert shift cells right event handler
:param description_tpl: Description template for `QUndoCommand`
"""
try:
(top, left), (bottom, right) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
top = bottom = self.row
left = right = self.column
count = right - left + 1
if self.is_column_data_discarded(count):
text = ("Shifting cells right will discard data.\n \n"
"You may want to resize the grid before shifting.")
if DiscardDataDialog(self.main_window, text).choice is not True:
return
selection1 = Selection([(top, left)], [(bottom, None)], [], [], [])
self.main_window.workflows.edit_cut(description_tpl=description_tpl,
selection=selection1)
clipboard = QApplication.clipboard()
data = clipboard.text()
selection2 = Selection([(top, right+1)], [(bottom, None)],
[], [], [])
self.main_window.workflows._paste_to_selection(
selection2, data,
description_tpl=description_tpl.format(selection1))
def on_delete_shift_cells_up(
self, *,
description_tpl: str = "Delete selection {} and shift cells up"):
"""Delete cells and shift below cells up event handler
:param description_tpl: Description template for `QUndoCommand`
"""
try:
(top, left), (bottom, right) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
top = bottom = self.row
left = right = self.column
selection1 = Selection([(bottom+1, left)], [(None, right)], [], [], [])
self.main_window.workflows.edit_cut(description_tpl=description_tpl,
selection=selection1)
clipboard = QApplication.clipboard()
data = clipboard.text()
selection2 = Selection([(top, left)], [(None, right)], [], [], [])
self.main_window.workflows._paste_to_selection(
selection2, data,
description_tpl=description_tpl.format(selection1))
def on_delete_shift_cells_left(
self, *,
description_tpl: str = "Delete selection {} and shift cells left"):
"""Delete cells and shift right cells left event handler
:param description_tpl: Description template for `QUndoCommand`
"""
try:
(top, left), (bottom, right) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
top = bottom = self.row
left = right = self.column
selection1 = Selection([(top, right+1)], [(bottom, None)], [], [], [])
self.main_window.workflows.edit_cut(description_tpl=description_tpl,
selection=selection1)
clipboard = QApplication.clipboard()
data = clipboard.text()
selection2 = Selection([(top, left)], [(bottom, None)],
[], [], [])
self.main_window.workflows._paste_to_selection(
selection2, data,
description_tpl=description_tpl.format(selection1))
def on_insert_rows(self):
"""Insert rows event handler"""
try:
(top, _), (bottom, _) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
top = bottom = self.row
count = bottom - top + 1
if self.is_row_data_discarded(count):
text = ("Inserting rows will discard data.\n \n"
"You may want to resize the grid before insertion.\n"
"Note that row insertion can be undone.")
if DiscardDataDialog(self.main_window, text).choice is not True:
return
index = self.currentIndex()
description = f"Insert {count} rows above row {top}"
command = commands.InsertRows(self, self.model, index, top, count,
description)
self.main_window.undo_stack.push(command)
def on_delete_rows(self):
"""Delete rows event handler"""
try:
(top, _), (bottom, _) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
top = bottom = self.row
count = bottom - top + 1
index = self.currentIndex()
description = f"Delete {count} rows starting from row {top}"
command = commands.DeleteRows(self, self.model, index, top, count,
description)
self.main_window.undo_stack.push(command)
def on_insert_columns(self):
"""Insert columns event handler"""
try:
(_, left), (_, right) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
left = right = self.column
count = right - left + 1
if self.is_column_data_discarded(count):
text = ("Inserting columns will discard data.\n \n"
"You may want to resize the grid before insertion.\n"
"Note that column insertion can be undone.")
if DiscardDataDialog(self.main_window, text).choice is not True:
return
index = self.currentIndex()
description = f"Insert {count} columns left of column {left}"
command = commands.InsertColumns(self, self.model, index, left, count,
description)
self.main_window.undo_stack.push(command)
def on_delete_columns(self):
"""Delete columns event handler"""
try:
(_, left), (_, right) = \
self.selection.get_grid_bbox(self.model.shape)
except TypeError:
left = right = self.column
count = right - left + 1
index = self.currentIndex()
description = \
f"Delete {count} columns starting from column {self.column}"
command = commands.DeleteColumns(self, self.model, index, left, count,
description)
self.main_window.undo_stack.push(command)
def on_insert_table(self):
"""Insert table event handler"""
if self.is_table_data_discarded(1):
text = ("Inserting tables will discard data.\n \n"
"You may want to resize the grid before insertion.\n"
"Note that table insertion can be undone.")
if DiscardDataDialog(self.main_window, text).choice is not True:
return
description = f"Insert table in front of table {self.table}"
command = commands.InsertTable(self, self.model, self.table,
description)
self.main_window.undo_stack.push(command)
def on_delete_table(self):
"""Delete table event handler"""
description = f"Delete table {self.table}"
command = commands.DeleteTable(self, self.model, self.table,
description)
self.main_window.undo_stack.push(command)
class GridHeaderView(QHeaderView):
"""QHeaderView with zoom support"""
def __init__(self, orientation: Qt.Orientation, grid: Grid):
"""
:param orientation: Orientation of the `QHeaderView`
:param grid: The main grid widget
"""
super().__init__(orientation, grid)
self.setSectionsClickable(True)
self.setHighlightSections(True)
self.default_section_size = self.defaultSectionSize()
self.grid = grid
# Overrides
def sizeHint(self) -> QSize:
"""Overrides sizeHint, which supports zoom"""
unzoomed_size = super().sizeHint()
return QSize(int(unzoomed_size.width() * self.grid.zoom),
int(unzoomed_size.height() * self.grid.zoom))
def sectionSizeHint(self, logicalIndex: int) -> int:
"""Overrides sectionSizeHint, which supports zoom
:param logicalIndex: Index of the section for the size hint
"""
unzoomed_size = super().sectionSizeHint(logicalIndex)
return int(unzoomed_size * self.grid.zoom)
def paintSection(self, painter: QPainter, rect: QRect, logicalIndex: int):
"""Overrides paintSection, which supports zoom
:param painter: Painter with which the section is drawn
:param rect: Outer rectangle of the section to be drawn
:param logicalIndex: Index of the section to be drawn
"""
zoom = self.grid.zoom
unzoomed_rect = QRect(0, 0,
int(round(rect.width()/zoom)),
int(round(rect.height()/zoom)))
with painter_save(painter):
painter.translate(rect.x()+1, rect.y()+1)
painter.scale(zoom, zoom)
super().paintSection(painter, unzoomed_rect, logicalIndex)
def contextMenuEvent(self, event: QContextMenuEvent):
"""Overrides contextMenuEvent
Installs HorizontalHeaderContextMenu or VerticalHeaderContextMenu
depending on self.orientation().
:param event: The triggering event
"""
actions = self.grid.main_window.main_window_actions
if self.orientation() == Qt.Orientation.Horizontal:
menu = HorizontalHeaderContextMenu(actions)
else:
menu = VerticalHeaderContextMenu(actions)
menu.exec(self.mapToGlobal(event.pos()))
# End of overrides
def update_zoom(self):
"""Updates zoom for the section sizes"""
with self.grid.undo_resizing_row():
with self.grid.undo_resizing_column():
default_size = int(self.default_section_size * self.grid.zoom)
self.setDefaultSectionSize(default_size)
# Fix for PyQt6 setDefaultSectionSize not working
for section in range(self.count()):
self.resizeSection(section, default_size)
if self.orientation() == Qt.Orientation.Horizontal:
section_sizes = self.grid.column_widths
else:
section_sizes = self.grid.row_heights
for section, size in section_sizes:
self.resizeSection(section, int(size * self.grid.zoom))
class GridTableModel(QAbstractTableModel):
"""QAbstractTableModel for Grid"""
cell_to_update = pyqtSignal(tuple)
def __init__(self, main_window: QMainWindow,
shape: Tuple[int, int, int]):
"""
:param main_window: Application main window
:param shape: Grid shape `(rows, columns, tables)`
"""
super().__init__()
self.main_window = main_window
self.code_array = CodeArray(shape, main_window.settings)
@contextmanager
def model_reset(self):
"""Context manager for handle changing/resetting model data"""
self.beginResetModel()
yield
self.endResetModel()
@contextmanager
def inserting_rows(self, index: QModelIndex, first: int, last: int):
"""Context manager for inserting rows
see `QAbstractItemModel.beginInsertRows`
:param index: Parent into which the new rows are inserted
:param first: Row number that first row will have after insertion
:param last: Row number that last row will have after insertion
"""
self.beginInsertRows(index, first, last)
yield
self.endInsertRows()
@contextmanager
def inserting_columns(self, index: QModelIndex, first: int, last: int):
"""Context manager for inserting columns
see `QAbstractItemModel.beginInsertColumns`
:param index: Parent into which the new columns are inserted
:param first: Column number that first column will have after insertion
:param last: Column number that last column will have after insertion
"""
self.beginInsertColumns(index, first, last)
yield
self.endInsertColumns()
@contextmanager
def removing_rows(self, index: QModelIndex, first: int, last: int):
"""Context manager for removing rows
see `QAbstractItemModel.beginRemoveRows`
:param index: Parent from which rows are removed
:param first: Row number of the first row to be removed
:param last: Row number of the last row to be removed
"""
self.beginRemoveRows(index, first, last)
yield
self.endRemoveRows()
@contextmanager
def removing_columns(self, index: QModelIndex, first: int, last: int):
"""Context manager for removing columns
see `QAbstractItemModel.beginRemoveColumns`
:param index: Parent from which columns are removed
:param first: Column number of the first column to be removed
:param last: Column number of the last column to be removed
"""
self.beginRemoveColumns(index, first, last)
yield
self.endRemoveColumns()
@property
def grid(self) -> Grid:
"""The main grid"""
return self.main_window.grid
@property
def shape(self) -> Tuple[int, int, int]:
"""Returns 3-tuple of rows, columns and tables"""
return self.code_array.shape
@shape.setter
def shape(self, value: Tuple[int, int, int]):
"""Sets the shape in the code array and adjusts the table_choice
:param value: Grid shape `(rows, columns, tables)`
"""
check_shape_validity(value, self.main_window.settings.maxshape)
with self.model_reset():
self.code_array.shape = value
self.grid.table_choice.no_tables = value[2]
def current(self, index: QModelIndex) -> Tuple[int, int, int]:
"""Tuple of row, column, table of given index
:param index: Index of the cell to be made the current cell
"""
return index.row(), index.column(), self.main_window.grid.table
def code(self, index: QModelIndex) -> str:
"""Code in cell index
:param index: Index of the cell for which the code is returned
"""
return self.code_array(self.current(index))
def rowCount(self, _: QModelIndex = QModelIndex()) -> int:
"""Overloaded `QAbstractItemModel.rowCount` for code_array backend"""
return self.shape[0]
def columnCount(self, _: QModelIndex = QModelIndex()) -> int:
"""Overloaded `QAbstractItemModel.columnCount` for code_array backend
"""
return self.shape[1]
def insertRows(self, row: int, count: int) -> bool:
"""Overloaded `QAbstractItemModel.insertRows` for code_array backend
:param row: Row at which rows are inserted
:param count: Number of rows to be inserted
"""
self.code_array.insert(row, count, axis=0, tab=self.grid.table)
return True
def removeRows(self, row: int, count: int) -> bool:
"""Overloaded `QAbstractItemModel.removeRows` for code_array backend
:param row: Row at which rows are removed
:param count: Number of rows to be removed
"""
try:
self.code_array.delete(row, count, axis=0, tab=self.grid.table)
except ValueError:
return False
return True
def insertColumns(self, column: int, count: int) -> bool:
"""Overloaded `QAbstractItemModel.insertColumns` for code_array backend
:param column: Column at which columns are inserted
:param count: Number of columns to be inserted
"""
self.code_array.insert(column, count, axis=1, tab=self.grid.table)
return True
def removeColumns(self, column: int, count: int) -> bool:
"""Overloaded `QAbstractItemModel.removeColumns` for code_array backend
:param column: Column at which columns are removed
:param count: Number of columns to be removed
"""
try:
self.code_array.delete(column, count, axis=1, tab=self.grid.table)
except ValueError:
return False
return True
def insertTable(self, table: int, count: int = 1):
"""Inserts tables
:param table: Table at which tables are inserted
:param count: Number of tables to be inserted
"""
self.code_array.insert(table, count, axis=2)
def removeTable(self, table: int, count: int = 1):
"""Removes tables
:param table: Table at which tables are removed
:param count: Number of tables to be removed
"""
self.code_array.delete(table, count, axis=2)
def font(self, key: Tuple[int, int, int]) -> QFont:
"""Returns font for given key
:param key: Key of cell, for which font is returned
"""
attr = self.code_array.cell_attributes[key]
font = QFont()
if attr.textfont is not None:
font.setFamily(attr.textfont)
if attr.pointsize is not None:
font.setPointSizeF(attr.pointsize)
if attr.fontweight is not None:
font.setWeight(qt52qt6_fontweights(attr.fontweight))
if attr.fontstyle is not None:
fontstyle = attr.fontstyle
if isinstance(fontstyle, int):
fontstyle = FONTSTYLES[fontstyle]
font.setStyle(fontstyle)
if attr.underline is not None:
font.setUnderline(attr.underline)
if attr.strikethrough is not None:
font.setStrikeOut(attr.strikethrough)
return font
def data(self, index: QModelIndex,
role: Qt.ItemDataRole = Qt.ItemDataRole.DisplayRole) -> Any:
"""Overloaded data for code_array backend
:param index: Index of the cell, for which data is returned
:param role: Role of data to be returned
"""
def safe_str(obj) -> str:
"""Returns str(obj), on RecursionError returns error message"""
try:
if obj.__class__ in class_format_functions:
format_function = class_format_functions[obj.__class__]
return format_function(obj)
return str(obj)
except Exception as err:
return str(err)
key = self.current(index)
if role == Qt.ItemDataRole.DisplayRole:
value = self.code_array[key]
renderer = self.code_array.cell_attributes[key].renderer
if renderer == "image" or value is None:
return ""
return safe_str(value)
if role == Qt.ItemDataRole.ToolTipRole:
value = self.code_array[key]
if value is None:
return ""
return wrap_text(safe_str(value))
if role == Qt.ItemDataRole.DecorationRole:
renderer = self.code_array.cell_attributes[key].renderer
if renderer == "image":
value = self.code_array[key]
if isinstance(value, QImage):
return value
try:
arr = numpy.array(value)
return array2qimage(arr)
except Exception:
return value
if role == Qt.ItemDataRole.BackgroundRole:
if self.main_window.settings.show_frozen \
and self.code_array.cell_attributes[key].frozen:
pattern_rgb = self.grid.palette().color(ColorRole.highlight)
bg_color = QBrush(pattern_rgb, Qt.BrushStyle.BDiagPattern)
else:
bg_color_rgb = self.code_array.cell_attributes[key].bgcolor
if bg_color_rgb is None:
bg_color = self.grid.palette().color(ColorRole.bg)
else:
bg_color = QColor(*bg_color_rgb)
return bg_color
if role == Qt.ItemDataRole.ForegroundRole:
text_color_rgb = self.code_array.cell_attributes[key].textcolor
if text_color_rgb is None:
text_color = self.grid.palette().color(ColorRole.text)
else:
text_color = QColor(*text_color_rgb)
return text_color
if role == Qt.ItemDataRole.FontRole:
return self.font(key)
if role == Qt.ItemDataRole.TextAlignmentRole:
pys2qt = {
"justify_left": Qt.AlignmentFlag.AlignLeft,
"justify_center": Qt.AlignmentFlag.AlignHCenter,
"justify_right": Qt.AlignmentFlag.AlignRight,
"justify_fill": Qt.AlignmentFlag.AlignJustify,
"align_top": Qt.AlignmentFlag.AlignTop,
"align_center": Qt.AlignmentFlag.AlignVCenter,
"align_bottom": Qt.AlignmentFlag.AlignBottom,
}
attr = self.code_array.cell_attributes[key]
alignment = pys2qt[attr.vertical_align]
justification = pys2qt[attr.justification]
alignment |= justification
return alignment
return QVariant()
def setData(self, index: QModelIndex, value: Any, role: Qt.ItemDataRole,
raw: bool = False, table: int = None) -> bool:
"""Overloaded setData for code_array backend
:param index: Index of the cell, for which data is set
:param value: Value of data to be set
:param role: Role of data to be set
:param raw: Sets raw data without string formatting in `EditRole`
:param table: Table for which data shall is set
"""
if role == Qt.ItemDataRole.EditRole:
if table is None:
key = self.current(index)
else:
key = index.row(), index.column(), table
if raw:
if value is None:
try:
self.code_array.pop(key)
except KeyError:
pass
else:
self.code_array[key] = value
else:
self.code_array[key] = f"{value}"
if not self.main_window.prevent_updates:
self.dataChanged.emit(index, index)
return True
if role in (Qt.ItemDataRole.DecorationRole,
Qt.ItemDataRole.TextAlignmentRole):
if not isinstance(value[2], AttrDict):
raise Warning(f"{value[2]} has type {type(value[2])} that "
"is not instance of AttrDict")
self.code_array.cell_attributes.append(value)
# We have a selection and no single cell
with self.main_window.workflows.busy_cursor():
with self.main_window.entry_line.disable_updates():
with self.main_window.workflows.prevent_updates():
for idx in index:
self.dataChanged.emit(idx, idx)
return True
def flags(self, index: QModelIndex) -> Qt.ItemFlag:
"""Overloaded, makes items editable
:param index: Index of cell for which flags are returned
"""
return QAbstractTableModel.flags(self,
index) | Qt.ItemFlag.ItemIsEditable
def headerData(self, idx: QModelIndex, _, role: Qt.ItemDataRole) -> str:
"""Overloaded for displaying numbers in header
:param idx: Index of header for which data is returned
:param role: Role of data to be returned
"""
if role == Qt.ItemDataRole.DisplayRole:
return str(idx)
def reset(self):
"""Deletes all grid data including undo data"""
with self.model_reset():
# Clear cells
self.code_array.dict_grid.clear()
# Clear attributes
del self.code_array.dict_grid.cell_attributes[:]
# Clear row heights and column widths
self.code_array.row_heights.clear()
self.code_array.col_widths.clear()
# Clear macros
self.code_array.macros = ""
# Clear caches
# self.main_window.undo_stack.clear()
self.code_array.result_cache.clear()
# Clear globals
self.code_array.clear_globals()
self.code_array.reload_modules()
class GridCellDelegate(QStyledItemDelegate):
"""QStyledItemDelegate for main grid QTableView"""
def __init__(self, main_window: QMainWindow, grid: Grid,
code_array: CodeArray):
"""
:param main_window: Application main window
:param grid: Grid, i.e. QTableView instance
:param code_array: Main backend model instance
"""
super().__init__()
self.main_window = main_window
self.grid = grid
self.code_array = code_array
self.cell_attributes = self.code_array.cell_attributes
def _get_render_text_document(self, rect: QRectF,
option: QStyleOptionViewItem,
index: QModelIndex) -> QTextDocument:
"""Returns styled QTextDocument that is ready for setting content
:param rect: Cell rect of the cell to be painted
:param option: Style option for rendering
:param index: Index of cell for which markup is rendered
"""
doc = QTextDocument()
font = self.grid.model.data(index, role=Qt.ItemDataRole.FontRole)
doc.setDefaultFont(font)
alignment = self.grid.model.data(
index, role=Qt.ItemDataRole.TextAlignmentRole)
doc.setDefaultTextOption(QTextOption(alignment))
bg_color = self.grid.model.data(index,
role=Qt.ItemDataRole.BackgroundRole)
css = f"background-color: {bg_color};"
doc.setDefaultStyleSheet(css)
doc.setTextWidth(rect.width())
doc.setUseDesignMetrics(True)
text_option = doc.defaultTextOption()
text_option.setWrapMode(
QTextOption.WrapMode.WrapAtWordBoundaryOrAnywhere)
doc.setDefaultTextOption(text_option)
return doc
def _render_text_document(self, doc: QTextDocument,
painter: QPainter, rect: QRectF,
option: QStyleOptionViewItem,
index: QModelIndex):
"""QTextDocument renderer
:param doc: Text document to be painted
:param painter: Painter with which markup is rendered
:param rect: Cell rect of the cell to be painted
:param option: Style option for rendering
:param index: Index of cell for which markup is rendered
"""
style = option.widget.style()
option.text = ""
style.drawControl(QStyle.ControlElement.CE_ItemViewItem, option,
painter, option.widget)
ctx = QAbstractTextDocumentLayout.PaintContext()
text_color = self.grid.model.data(index,
role=Qt.ItemDataRole.ForegroundRole)
ctx.palette.setColor(ColorRole.text, text_color)
key = index.row(), index.column(), self.grid.table
vertical_align = self.cell_attributes[key].vertical_align
y_offset = 0
if vertical_align == 'align_center':
y_offset += rect.height() / 2 - doc.size().height() / 2
elif vertical_align == 'align_bottom':
y_offset += rect.height() - doc.size().height()
with painter_save(painter):
painter.translate(rect.x(), rect.y() + y_offset)
doc.documentLayout().draw(painter, ctx)
def _render_text(self, painter: QPainter, rect: QRectF,
option: QStyleOptionViewItem, index: QModelIndex):
"""Text renderer
:param painter: Painter with which markup is rendered
:param rect: Cell rect of the cell to be painted
:param option: Style option for rendering
:param index: Index of cell for which markup is rendered
"""
self.initStyleOption(option, index)
doc = self._get_render_text_document(rect, option, index)
doc.setPlainText(option.text)
self._render_text_document(doc, painter, rect, option, index)
def _render_markup(self, painter: QPainter, rect: QRectF,
option: QStyleOptionViewItem, index: QModelIndex):
"""HTML markup renderer
:param painter: Painter with which markup is rendered
:param rect: Cell rect of the cell to be painted
:param option: Style option for rendering
:param index: Index of cell for which markup is rendered
"""
self.initStyleOption(option, index)
doc = self._get_render_text_document(rect, option, index)
doc.setHtml(option.text)
self._render_text_document(doc, painter, rect, option, index)
def _get_aligned_image_rect(
self, rect: QRectF, index: QModelIndex,
image_width: Union[int, float],
image_height: Union[int, float]) -> QRectF:
"""Returns image rect dependent on alignment and justification
:param rect: Rect to be aligned
:param image_width: Width of image [px]
:param image_height: Height of image [px]
"""
def scale_size(inner_width: Union[int, float],
inner_height: Union[int, float],
outer_width: Union[int, float],
outer_height: Union[int, float]) -> Tuple[float, float]:
"""Scales up inner_rect to fit in outer_rect
Returns width, height tuple that maintains aspect ratio.
:param inner_width: Width of inner rect (scaled to outer rect)
:param inner_height: Height of inner rect (scaled to outer rect)
:param outer_width: Width of outer rect
:param outer_height: Height of outer rect
"""
if inner_width and inner_height and outer_width and outer_height:
inner_aspect = inner_width / inner_height
outer_aspect = outer_width / outer_height
if outer_aspect < inner_aspect:
inner_width *= outer_width / inner_width
inner_height = inner_width / inner_aspect
else:
inner_height *= outer_height / inner_height
inner_width = inner_height * inner_aspect
return inner_width, inner_height
key = index.row(), index.column(), self.grid.table
justification = self.cell_attributes[key].justification
vertical_align = self.cell_attributes[key].vertical_align
if justification == "justify_fill":
return rect
try:
image_width, image_height = scale_size(image_width, image_height,
rect.width(), rect.height())
except ZeroDivisionError:
pass
image_x, image_y = rect.x(), rect.y()
if justification == "justify_center":
image_x = rect.x() + rect.width() / 2 - image_width / 2
elif justification == "justify_right":
image_x = rect.x() + rect.width() - image_width
if vertical_align == "align_center":
image_y = rect.y() + rect.height() / 2 - image_height / 2
elif vertical_align == "align_bottom":
image_y = rect.y() + rect.height() - image_height
return QRectF(image_x, image_y, image_width, image_height)
def _render_qimage(self, painter: QPainter, rect: QRectF,
index: QModelIndex, qimage: QImage = None):
"""QImage renderer
:param painter: Painter with which qimage is rendered
:param rect: Cell rect of the cell to be painted
:param index: Index of cell for which qimage is rendered
:param qimage: Image to be rendered, decoration drawn if not provided
"""
if qimage is None:
qimage = index.data(Qt.ItemDataRole.DecorationRole)
if not isinstance(qimage, QImage):
raise TypeError(f"{qimage} not of type QImage")
img_width, img_height = qimage.width(), qimage.height()
img_rect = self._get_aligned_image_rect(rect, index,
img_width, img_height)
if img_rect is None:
return
key = index.row(), index.column(), self.grid.table
justification = self.cell_attributes[key].justification
if justification == "justify_fill":
qimage = qimage.scaled(int(img_width), int(img_height),
Qt.AspectRatioMode.IgnoreAspectRatio,
Qt.TransformationMode.SmoothTransformation)
else:
try:
qimage = qimage.scaled(int(img_width), int(img_height),
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
except AttributeError:
qimage = qimage.scaled(int(img_width), int(img_height))
with painter_save(painter):
try:
scale_x = img_rect.width() / img_width
except ZeroDivisionError:
scale_x = 1
try:
scale_y = img_rect.height() / img_height
except ZeroDivisionError:
scale_y = 1
painter.translate(img_rect.x(), img_rect.y())
painter.scale(scale_x, scale_y)
painter.drawImage(0, 0, qimage)
def _render_svg(self, painter: QPainter, rect: QRectF, index: QModelIndex,
svg_str: str = None):
"""SVG renderer
:param painter: Painter with which qimage is rendered
:param rect: Cell rect of the cell to be painted
:param index: Index of cell for which qimage is rendered
:param svg_str: SVG string
"""
if svg_str is None:
svg_str = index.data(Qt.ItemDataRole.DecorationRole)
if svg_str is None:
return
try:
svg_bytes = bytes(svg_str)
except TypeError:
try:
svg_bytes = bytes(svg_str, encoding='utf-8')
except TypeError:
return
if not is_svg(svg_bytes):
return
key = index.row(), index.column(), self.grid.table
justification = self.cell_attributes[key].justification
svg = QSvgRenderer(QByteArray(svg_bytes))
if justification == "justify_fill":
svg.setAspectRatioMode(Qt.AspectRatioMode.IgnoreAspectRatio)
svg_rect = rect
svg.render(painter, svg_rect)
return
try:
svg.setAspectRatioMode(Qt.AspectRatioMode.KeepAspectRatio)
except AttributeError:
pass
svg_size = svg.defaultSize()
try:
svg_aspect = svg_size.width() / svg_size.height()
except ZeroDivisionError:
svg_aspect = 1
try:
rect_aspect = rect.width() / rect.height()
except ZeroDivisionError:
rect_aspect = 1
if svg_aspect > rect_aspect:
# svg is wider than rect
svg_width = rect.width()
svg_height = rect.width() / svg_aspect
else:
# svg is taller than rect
svg_width = rect.height() * svg_aspect
svg_height = rect.height()
svg_rect = self._get_aligned_image_rect(rect, index,
svg_width, svg_height)
if svg_rect is None:
return
svg.render(painter, svg_rect)
def _render_matplotlib(self, painter: QPainter, rect: QRectF,
index: QModelIndex):
"""Matplotlib renderer
:param painter: Painter with which the matplotlib image is rendered
:param rect: Cell rect of the cell to be painted
:param index: Index of cell for which the matplotlib image is rendered
"""
if matplotlib is None:
# matplotlib is not installed
return
key = index.row(), index.column(), self.grid.table
figure = self.code_array[key]
if isinstance(figure, bytes) or isinstance(figure, str):
# We try rendering the content as SVG
return self._render_svg(painter, rect, index, figure)
if not isinstance(figure, matplotlib.figure.Figure):
return
# Save SVG in a fake file object.
with BytesIO() as filelike:
try:
figure.savefig(filelike, format="svg", bbox_inches="tight")
except Exception:
return
svg_str = filelike.getvalue().decode()
self._render_svg(painter, rect, index, svg_str=svg_str)
def paint_(self, painter: QPainter, rect: QRectF,
option: QStyleOptionViewItem, index: QModelIndex):
"""Calls the overloaded paint function or creates html delegate
:param painter: Painter with which borders are drawn
:param rect: Cell rect of the cell to be painted
:param option: Style option for rendering
:param index: Index of cell for which borders are drawn
"""
painter.setRenderHints(QPainter.RenderHint.LosslessImageRendering
| QPainter.RenderHint.Antialiasing
| QPainter.RenderHint.TextAntialiasing
| QPainter.RenderHint.SmoothPixmapTransform)
key = index.row(), index.column(), self.grid.table
renderer = self.cell_attributes[key].renderer
old_rect = option.rect
option.rect = QRect(int(rect.x()), int(rect.y()),
int(rect.width() + 1.5),
int(rect.height() + 1.5))
if renderer == "text":
self._render_text(painter, rect, option, index)
elif renderer == "markup":
self._render_markup(painter, rect, option, index)
elif renderer == "image":
image = index.data(Qt.ItemDataRole.DecorationRole)
if isinstance(image, QImage):
self._render_qimage(painter, rect, index)
elif isinstance(image, str):
self._render_svg(painter, rect, index)
elif renderer == "matplotlib":
self._render_matplotlib(painter, rect, index)
option.rect = old_rect
def sizeHint(self, option: QStyleOptionViewItem,
index: QModelIndex) -> QSize:
"""Overloads SizeHint
:param option: Style option for rendering
:param index: Index of the cell for the size hint
"""
key = index.row(), index.column(), self.grid.table
if not self.cell_attributes[key].renderer == "markup":
return super().sizeHint(option, index)
# HTML
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
doc = QTextDocument()
doc.setHtml(options.text)
doc.setTextWidth(options.rect.width())
return QSize(doc.idealWidth(), doc.size().height())
def paint(self, painter: QPainter, option: QStyleOptionViewItem,
index: QModelIndex):
"""Overloads `QStyledItemDelegate` to add cell border painting
:param painter: Painter with which borders are drawn
:param option: Style option for rendering
:param index: Index of cell to be rendered
"""
renderer = CellRenderer(self.grid, painter, option, index)
renderer.paint()
def createEditor(self, parent: QWidget, option: QStyleOptionViewItem,
index: QModelIndex) -> QWidget:
"""Overloads `QStyledItemDelegate`
Disables editor in locked cells
Switches to chart dialog in chart cells
:param parent: Parent widget for the cell editor to be returned
:param option: Style option for the cell editor
:param index: Index of cell for which a cell editor is created
"""
key = index.row(), index.column(), self.grid.table
if self.cell_attributes[key].locked:
return
if self.cell_attributes[key].renderer == "matplotlib":
self.main_window.workflows.macro_insert_chart()
return
self.editor = super().createEditor(parent, option, index)
self.editor.setPalette(self.editor.style().standardPalette())
self.editor.installEventFilter(self)
return self.editor
def eventFilter(self, source: QObject, event: QEvent) -> bool:
"""Overloads `eventFilter`. Overrides QLineEdit default shortcut.
Quotes cell editor content for <Ctrl>+<Enter> and <Ctrl>+<Return>.
Counts as undoable action.
:param source: Source widget of event
:param event: Any QEvent
"""
if event.type() == QEvent.Type.ShortcutOverride \
and source is self.editor \
and event.modifiers() == Qt.KeyboardModifier.ControlModifier \
and event.key() in (Qt.Key.Key_Return, Qt.Key.Key_Enter):
code = quote(source.text())
index = self.grid.currentIndex()
description = f"Quote code for cell {index}"
cmd = commands.SetCellCode(code, self.grid.model, index,
description)
self.main_window.undo_stack.push(cmd)
return super().eventFilter(source, event)
def setEditorData(self, editor: QWidget, index: QModelIndex):
"""Overloads `setEditorData` to use code_array data
:param editor: Cell editor, in which data is set
:param index: Index of cell from which the cell editor data is set
"""
row = index.row()
column = index.column()
table = self.grid.table
value = self.code_array((row, column, table))
editor.setText(value)
def setModelData(self, editor: QWidget, model: QAbstractItemModel,
index: QModelIndex):
"""Overloads `setModelData` to use code_array data
:param editor: Cell editor, from which data is retrieved
:param model: `GridTableModel`
:param index: Index of cell for which data is set
"""
description = f"Set code for cell {model.current(index)}"
command = commands.SetCellCode(editor.text(), model, index,
description)
self.main_window.undo_stack.push(command)
def updateEditorGeometry(self, editor: QWidget,
option: QStyleOptionViewItem, _: QModelIndex):
"""Overloads `updateEditorGeometry` to update editor geometry to cell
:param editor: Cell editor, for which geometry is retrieved
:param option: Style option of the editor
"""
editor.setGeometry(option.rect)
class TableChoice(QTabBar):
"""The TabBar below the main grid"""
def __init__(self, main_window: QMainWindow, no_tables: int):
"""
:param main_window: Application main window
:param no_tables: Number of tables to be initially created
"""
super().__init__(shape=QTabBar.Shape.RoundedSouth)
self.setExpanding(False)
self.main_window = main_window
self.no_tables = no_tables
self.last = 0
self.currentChanged.connect(self.on_table_changed)
@property
def no_tables(self) -> int:
"""Returns the number of tables in the table_choice"""
return self._no_tables
@no_tables.setter
def no_tables(self, value: int):
"""Sets the number of tables in the table_choice
:param value: Number of tables
"""
self._no_tables = value
if value > self.count():
# Insert
for i in range(self.count(), value):
self.addTab(str(i))
elif value < self.count():
# Remove
for i in range(self.count()-1, value-1, -1):
self.removeTab(i)
@property
def table(self) -> int:
"""Returns current table from table_choice that is displayed"""
return self.currentIndex()
@table.setter
def table(self, value: int):
"""Sets a new table to be displayed
:param value: Number of the table
"""
self.setCurrentIndex(value)
# Overrides
def contextMenuEvent(self, event: QContextMenuEvent):
"""Overrides contextMenuEvent to install GridContextMenu
:param event: Triggering event
"""
actions = self.main_window.main_window_actions
menu = TableChoiceContextMenu(actions)
menu.exec(self.mapToGlobal(event.pos()))
# Event handlers
def on_table_changed(self, current: int):
"""Event handler for table changes
:param current: The current table to be displayed
"""
for grid in self.main_window.grids:
grid.table = current
grid.table_scrolls[self.last] = \
(grid.verticalScrollBar().value(),
grid.horizontalScrollBar().value())
with grid.undo_resizing_row():
with grid.undo_resizing_column():
grid.update_cell_spans()
grid.update_zoom()
grid.update_index_widgets()
grid.gui_update()
try:
v_pos, h_pos = grid.table_scrolls[current]
except KeyError:
v_pos = h_pos = 0
grid.verticalScrollBar().setValue(v_pos)
grid.horizontalScrollBar().setValue(h_pos)
self.last = current
# Update entryline
try:
self.main_window.grid.on_current_changed()
except AttributeError:
pass
|