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 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 2 of the License, or (at your option) any later
* version.
*
* This software 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
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "TextEditor.h"
#include "BaseIconNames.h"
#include "BaseContextMenu.h"
#include "BaseFindDialog.h"
#include "BaseFindWidget.h"
#include "BaseReplaceDialog.h"
#include "BaseReplaceWidget.h"
#include "InformationDialog.h"
#include "Color.h"
#include "CustomTextDocument.h"
#include "IconEngine.h"
#include "KeyModifier.h"
#include "LineNumberDisplay.h"
#include "QtUtil.h"
#include "SelectLineDialog.h"
#include "SelectLineWidget.h"
#include "Singleton.h"
#include "StandardAction.h"
#include "TextBlockData.h"
#include "TextEditorMarginWidget.h"
#include "TextSeparator.h"
#include "Util.h"
#include "XmlOptions.h"
#include <QApplication>
#include <QAbstractTextDocumentLayout>
#include <QDrag>
#include <QMenu>
#include <QMimeData>
#include <QPainter>
#include <QProgressDialog>
#include <QRegExp>
#include <QTextBlock>
#include <QTextStream>
#include <QToolTip>
//______________________________________________
TextSelection& TextEditor::lastSelection()
{
static TextSelection selection;
return selection;
}
//______________________________________________
TextEditor::TextEditor( QWidget *parent ):
BaseEditor( parent ),
Counter( "TextEditor" ),
marginWidget_( new TextEditorMarginWidget( this ) ),
boxSelection_( this ),
cursorMonitor_( viewport() ),
removeLineBuffer_( this ),
clickCounter_( this, 4 )
{
Debug::Throw( "TextEditor::TextEditor.\n" );
// set customized document
auto document( new CustomTextDocument(0) );
Base::Key::associate( this, document );
#ifdef QT_USE_PLAIN_TEXT_EDIT
// set document layout
document->setDocumentLayout( new QPlainTextDocumentLayout( document ) );
#endif
// assign
setDocument( document );
// paragraph highlight
blockHighlight_ = new BlockHighlight( this );
// actions
_installActions();
// line number
lineNumberDisplay_ = new LineNumberDisplay( this );
connect( this, SIGNAL(cursorPositionChanged()), &blockHighlight(), SLOT(highlight()) );
connect( this, SIGNAL(copyAvailable(bool)), SLOT(_updateSelectionActions(bool)) );
connect( this, SIGNAL(selectionChanged()), SLOT(_synchronizeSelection()) );
connect( this, SIGNAL(selectionChanged()), SLOT(_updateClipboard()) );
connect( this, SIGNAL(cursorPositionChanged()), SLOT(_synchronizeSelection()) );
if( Base::Singleton::get().hasApplication() )
{ connect( Base::Singleton::get().application(), SIGNAL(configurationChanged()), SLOT(_updateConfiguration()) ); }
// track changes of block counts
connect( TextEditor::document(), SIGNAL(blockCountChanged(int)), SLOT(_blockCountChanged(int)) );
connect( TextEditor::document(), SIGNAL(contentsChanged()), SLOT(_updateContentActions()) );
connect( TextEditor::document(), SIGNAL(contentsChanged()), &_marginWidget(), SLOT(setDirty()) );
// update configuration
_updateConfiguration();
_blockCountChanged(0);
_updateContentActions();
marginWidget_->show();
}
//________________________________________________
TextEditor::~TextEditor()
{
Debug::Throw() << "TextEditor::~TextEditor - key: " << key() << endl;
// cast document
auto document( qobject_cast<CustomTextDocument*>( TextEditor::document() ) );
if( document && Base::KeySet<TextEditor>( document ).size() == 1 ) document->deleteLater();
// update associates synchronization flags
Base::KeySet<TextEditor> editors( this );
// nothing to be done if no associates
if( editors.empty() ) return;
// keep position of current cursor
int position( textCursor().position() );
int anchor( textCursor().anchor() );
// need to reset Text document
// to avoid deletion while deleting this editor
setDocument( new QTextDocument );
// keep reference to first associate
TextEditor &editor( **editors.begin() );
// recreate an appropriate cursor
// this is dangerous
QTextCursor cursor( editor.document() );
cursor.setPosition( anchor );
cursor.setPosition( position, QTextCursor::KeepAnchor );
editor.setTextCursor( cursor );
// turn off synchronization
if( editors.size() == 1 ) editor.setSynchronized( false );
}
//________________________________________________
int TextEditor::blockCount() const
{
Debug::Throw( "TextEditor::blockCount.\n" );
int count = 0;
for( QTextBlock block( document()->begin() ); block.isValid(); block = block.next() )
{ count += blockCount( block ); }
return count;
}
//________________________________________________
TextPosition TextEditor::textPosition()
{
auto cursor( textCursor() );
// calculate index
TextPosition out;
#if QT_VERSION >= 0x040200
// direct access as introduced in Qt 4.2
out.index() = cursor.columnNumber();
out.paragraph() = cursor.blockNumber();
#else
// slow access for prior versions of Qt
auto block( cursor.block() );
out.index() = cursor.position() - block.position();
while( block.isValid() )
{
block = block.previous();
out.paragraph()++;
}
// need to decrement once
out.paragraph()--;
#endif
return out;
}
//________________________________________________
bool TextEditor::isCursorVisible() const
{
QRect cursor_rect( cursorRect() );
QRect rect( viewport()->rect() );
return rect.intersects( cursor_rect );
}
//______________________________________________________________________
bool TextEditor::hasSelection() const
{ return textCursor().hasSelection() || !boxSelection_.empty(); }
//______________________________________________________________________
TextSelection TextEditor::selection() const
{
Debug::Throw( "TextEditor::selection.\n" );
// copy last selection
TextSelection out( "" );
// copy attributes from last selection
out.setFlag( TextSelection::CaseSensitive, lastSelection().flag( TextSelection::CaseSensitive ) );
out.setFlag( TextSelection::EntireWord, lastSelection().flag( TextSelection::EntireWord ) );
// try set from current selection
QString text;
if( !( text = qApp->clipboard()->text( QClipboard::Selection ) ).isEmpty() ) {
Debug::Throw( "TextEditor::selection - from clipboard.\n" );
out.setText( text );
} else if( textCursor().hasSelection() ) {
Debug::Throw() << "TextEditor::selection - from cursor: " << textCursor().selectedText() << endl;
out.setText( textCursor().selectedText() );
} else {
Debug::Throw( "TextEditor::selection - from last selection.\n" );
out.setText( lastSelection().text() );
}
return out;
}
//________________________________________________
void TextEditor::setPlainText( const QString& text )
{
Debug::Throw( "TextEditor::setPlainText.\n" );
lineNumberDisplay_->clear();
bool enabled( blockHighlight_->isEnabled() );
blockHighlight_->setEnabled( false );
BaseEditor::setPlainText( text );
blockHighlight_->setEnabled( enabled );
}
//________________________________________________
void TextEditor::setHtml( const QString& text )
{
Debug::Throw( "TextEditor::setHtml.\n" );
bool enabled( blockHighlight_->isEnabled() );
blockHighlight_->setEnabled( false );
#ifdef QT_USE_PLAIN_TEXT_EDIT
BaseEditor::setPlainText( text );
#else
BaseEditor::setHtml( text );
#endif
blockHighlight_->setEnabled( enabled );
}
//___________________________________________________________________________
void TextEditor::paintMargin( QPainter& painter )
{
int height( TextEditor::height() - 2*frameWidth() );
if( horizontalScrollBar()->isVisible() ) { height -= horizontalScrollBar()->height() + 2; }
// clip
painter.setClipRect( QRect( 0, 0, _leftMargin(), height ), Qt::IntersectClip );
painter.setPen( Qt::NoPen );
painter.translate( 0, -verticalScrollBar()->value() );
// draw current block rect
if( blockHighlightAction_->isEnabled() && blockHighlightAction_->isChecked() && _currentBlockRect().isValid() )
{
painter.setBrush( palette().color( QPalette::AlternateBase ) );
painter.drawRect( _currentBlockRect() );
}
if( marginWidget_->drawVerticalLine() ) {
painter.setBrush( QBrush( marginWidget_->foregroundColor(), Qt::Dense4Pattern ) );
painter.drawRect( _leftMargin()-1, verticalScrollBar()->value(), 1, height+verticalScrollBar()->value() );
}
// set brush and pen suitable to further painting
painter.setBrush( Qt::NoBrush );
painter.setPen(marginWidget_->foregroundColor() );
// draw lines
if(
lineNumberDisplay_ &&
showLineNumberAction_ &&
showLineNumberAction_->isVisible() &&
showLineNumberAction_->isChecked() )
{ lineNumberDisplay_->paint( painter ); }
}
//________________________________________________
void TextEditor::selectWord()
{
Debug::Throw( "TextEditor::selectWord.\n" );
// retrieve text cursor, block and text
auto cursor( textCursor() );
QTextBlock block( cursor.block() );
QString text( cursor.block().text() );
// retrieve local cursor position in block
int localPosition( cursor.position() - block.position() );
int begin = localPosition;
int end = localPosition;
// parse text
if( TextSeparator::get().base().find( text[begin] ) != TextSeparator::get().base().end() )
{
// see if cursor is in base separator list
while( begin > 0 && TextSeparator::get().base().find( text[begin-1] ) != TextSeparator::get().base().end() ) begin--;
while( end < text.size() && TextSeparator::get().base().find( text[end] ) != TextSeparator::get().base().end() ) end++;
} else if( TextSeparator::get().extended().find( text[begin] ) != TextSeparator::get().extended().end() ) {
// see if cursor is in extended separator list
while( begin > 0 && TextSeparator::get().extended().find( text[begin-1] ) != TextSeparator::get().extended().end() ) begin--;
while( end < text.size() && TextSeparator::get().extended().find( text[end] ) != TextSeparator::get().extended().end() ) end++;
} else {
// cursor is in word
while( begin > 0 && TextSeparator::get().all().find( text[begin-1] ) == TextSeparator::get().all().end() ) begin--;
while( end < (int)text.size() && TextSeparator::get().all().find( text[end] ) == TextSeparator::get().all().end() ) end++;
}
// move cursor to begin of selection
for( ;begin < localPosition; localPosition-- ) { cursor.movePosition( QTextCursor::Left, QTextCursor::MoveAnchor ); }
for( ;localPosition < end; localPosition++ ) { cursor.movePosition( QTextCursor::Right, QTextCursor::KeepAnchor ); }
// assign cursor to Text editor
setTextCursor( cursor );
return;
}
//________________________________________________
void TextEditor::selectLine()
{
Debug::Throw( "TextEditor::selectLine.\n" );
auto cursor( textCursor() );
auto block( cursor.block() );
int begin( block.position() );
int end( block.position() + block.length() );
if( !block.next().isValid() ) end--;
cursor.setPosition( begin );
cursor.setPosition( end, QTextCursor::KeepAnchor );
// assign cursor to text editor and make sure it is visible
setTextCursor( cursor );
ensureCursorVisible();
// updateClipboard need to be called manually
// because somehow the selectionChanged signal is not caught.
_updateClipboard();
return;
}
//________________________________________________
void TextEditor::mergeCurrentCharFormat( const QTextCharFormat& format )
{
static QRegExp regexp( "\\s+$" );
auto cursor( textCursor() );
if( cursor.hasSelection() )
{
// get selection, look for trailing spaces
QString text( cursor.selectedText() );
if( regexp.indexIn( text ) >= 0 )
{
// create local cursor, copy current, in proper order
QTextCursor local( document() );
local.beginEditBlock();
local.setPosition( qMin( cursor.position(), cursor.anchor() ), QTextCursor::MoveAnchor );
local.setPosition( qMax( cursor.position(), cursor.anchor() ), QTextCursor::KeepAnchor );
local.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, regexp.matchedLength() );
local.mergeCharFormat( format );
local.endEditBlock();
return;
}
} else if( boxSelection_.state() == BoxSelection::State::Finished ) {
// process box selection
// boxSelection_.setCharFormat( format );
boxSelection_.mergeCharFormat( format );
return;
}
BaseEditor::mergeCurrentCharFormat( format );
return;
}
//________________________________________________
void TextEditor::synchronize( TextEditor* editor )
{
Debug::Throw( "TextEditor::synchronize.\n" );
// retrieve and cast old document
auto document( qobject_cast<CustomTextDocument*>( BaseEditor::document() ) );
// assign new document and associate
setDocument( editor->document() );
Base::Key::associate( this, qobject_cast<CustomTextDocument*>( editor->document() ) );
// delete old document, if needed
if( document && Base::KeySet<TextEditor>( document ).size() == 1 ) delete document;
// set synchronization flag
editor->setSynchronized( true );
setSynchronized( true );
// synchronize cursor position
setTextCursor( editor->textCursor() );
// restore scrollbar positions
horizontalScrollBar()->setValue( editor->horizontalScrollBar()->value() );
verticalScrollBar()->setValue( editor->verticalScrollBar()->value() );
// synchronize tab emulation
_setTabSize( editor->emulatedTabCharacter().size() );
tabEmulationAction_->setChecked( editor->tabEmulationAction_->isChecked() );
// synchronize wrap mode
wrapModeAction_->setChecked( editor->wrapModeAction_->isChecked() );
// track changes of block counts
lineNumberDisplay_->synchronize( &editor->_lineNumberDisplay() );
connect( TextEditor::document(), SIGNAL(blockCountChanged(int)), SLOT(_blockCountChanged(int)) );
connect( TextEditor::document(), SIGNAL(contentsChanged()), SLOT(_updateContentActions()) );
connect( TextEditor::document(), SIGNAL(contentsChanged()), &_marginWidget(), SLOT(setDirty()) );
// margin
_setLeftMargin( editor->_leftMargin() );
return;
}
//_____________________________________________________________________
bool TextEditor::setActive( bool active )
{
Debug::Throw( "TextEditor::setActive.\n" );
// check if value is changed
if( isActive() == active ) return false;
active_ = active;
return true;
}
//__________________________________________________________________
void TextEditor::showReplacements( int counts )
{
Debug::Throw( "TextEditor::showReplacements.\n" );
QString buffer;
if( !counts ) buffer = QString( tr( "String not found." ) );
else if( counts == 1 ) buffer = QString( tr( "1 replacement performed" ) );
else buffer = QString( tr( "%1 replacements performed" ) ).arg( counts );
InformationDialog( this, buffer ).setWindowTitle( QString( tr( "Replace in Text - %1" ) ).arg( qApp->applicationName() ) ).centerOnWidget( qApp->activeWindow() ).exec();
return;
}
//__________________________________________________________________
void TextEditor::setTrackAnchors( bool value )
{
Debug::Throw( "TextEditor::setTrackAnchors.\n" );
trackAnchors_ = value;
if( value ) setMouseTracking( true );
}
//__________________________________________________________________
void TextEditor::setReadOnly( bool readOnly )
{
Debug::Throw( "TextEditor::setReadOnly.\n" );
BaseEditor::setReadOnly( readOnly );
_updateReadOnlyActions( readOnly );
if( readOnly ) document()->setModified( false );
}
//____________________________________________________________________
void TextEditor::resetUndoRedoStack()
{
if( isReadOnly() || !document()->isUndoRedoEnabled() ) return;
Debug::Throw(" TextEditor::resetUndoRedoStack.\n");
document()->setUndoRedoEnabled( false );
document()->setUndoRedoEnabled( true );
}
//______________________________________________________________________________
void TextEditor::installContextMenuActions( BaseContextMenu* menu, bool allActions )
{
Debug::Throw( "TextEditor::installContextMenuActions.\n" );
// anchors
if( trackAnchors_ && !anchor().isEmpty() )
{
menu->addAction( copyLinkAction_ );
menu->addSeparator();
}
// wrapping
menu->addAction( showLineNumberAction_ );
menu->addAction( wrapModeAction_ );
menu->addSeparator();
if( allActions )
{
menu->addAction( undoAction_ );
menu->addAction( redoAction_ );
menu->addSeparator();
}
menu->addAction( cutAction_ );
menu->addAction( copyAction_ );
menu->addAction( pasteAction_ );
menu->addAction( clearAction_ );
menu->addSeparator();
menu->addAction( selectAllAction_ );
menu->addAction( upperCaseAction_ );
menu->addAction( lowerCaseAction_ );
menu->addSeparator();
menu->addAction( findAction_ );
if( allActions )
{
menu->addAction( findAgainAction_ );
menu->addAction( findSelectionAction_);
menu->addSeparator();
}
menu->addAction( replaceAction_ );
if( allActions )
{
menu->addAction( replaceAgainAction_ );
menu->addAction( gotoLineAction_);
}
return;
}
//______________________________________________________________________
void TextEditor::createFindWidget( bool compact )
{
Debug::Throw( "TextEditor::createFindWidget.\n" );
if( !findWidget_ )
{
findWidget_ = new BaseFindWidget( this, compact );
connect( findWidget_, SIGNAL(find(TextSelection)), SLOT(find(TextSelection)) );
connect( this, SIGNAL(matchFound()), findWidget_, SLOT(matchFound()) );
connect( this, SIGNAL(noMatchFound()), findWidget_, SLOT(noMatchFound()) );
connect( this, SIGNAL(destroyed()), findWidget_, SLOT(deleteLater()) );
}
return;
}
//______________________________________________________________________
void TextEditor::createReplaceWidget( bool compact )
{
Debug::Throw( "TextEditor::createReplaceWidget.\n" );
if( !replaceWidget_ )
{
replaceWidget_ = new BaseReplaceWidget( this, compact );
connect( replaceWidget_, SIGNAL(find(TextSelection)), SLOT(find(TextSelection)) );
connect( replaceWidget_, SIGNAL(replace(TextSelection)), SLOT(replace(TextSelection)) );
connect( replaceWidget_, SIGNAL(replaceInWindow(TextSelection)), SLOT(replaceInWindow(TextSelection)) );
connect( replaceWidget_, SIGNAL(replaceInSelection(TextSelection)), SLOT(replaceInSelection(TextSelection)) );
connect( replaceWidget_, SIGNAL(menuAboutToShow()), SLOT(_updateReplaceInSelection()) );
connect( this, SIGNAL(matchFound()), replaceWidget_, SLOT(matchFound()) );
connect( this, SIGNAL(noMatchFound()), replaceWidget_, SLOT(noMatchFound()) );
connect( this, SIGNAL(destroyed()), replaceWidget_, SLOT(deleteLater()) );
}
return;
}
//________________________________________________
void TextEditor::createSelectLineWidget( bool compact )
{
if( !selectLineWidget_ )
{
Debug::Throw( "TextEditor::createSelectLineWidget.\n" );
selectLineWidget_ = new SelectLineWidget( this, compact );
connect( selectLineWidget_, SIGNAL(lineSelected(int)), SLOT(selectLine(int)) );
connect( this, SIGNAL(lineFound()), selectLineWidget_, SLOT(matchFound()) );
connect( this, SIGNAL(lineNotFound()), selectLineWidget_, SLOT(noMatchFound()) );
connect( this, SIGNAL(destroyed()), selectLineWidget_, SLOT(deleteLater()) );
}
}
//___________________________________________________________________________
void TextEditor::setBackground( QTextBlock block, const QColor& color )
{
Debug::Throw( "TextEditor::setBackground.\n" );
// try retrieve data or create
auto data( static_cast<TextBlockData*>( block.userData() ) );
if( !data ) block.setUserData( data = new TextBlockData );
// try assign color
if( data->setBackground( color ) && updatesEnabled() )
{ document()->markContentsDirty(block.position(), block.length()-1); }
return;
}
//___________________________________________________________________________
void TextEditor::clearBackground( QTextBlock block )
{
Debug::Throw( "TextEditor::clearBackground.\n" );
auto data( static_cast<TextBlockData*>( block.userData() ) );
if( data && data->hasFlag( TextBlock::HasBackground ) && data->setBackground( QColor() ) && updatesEnabled() )
{ document()->markContentsDirty(block.position(), block.length()-1); }
return;
}
//___________________________________________________________________________
void TextEditor::clearAllBackgrounds()
{
Debug::Throw( "TextEditor::clearAllBackgrounds.\n" );
for( auto block = document()->begin(); block.isValid(); block = block.next() )
{ clearBackground( block ); }
}
//________________________________________________
void TextEditor::cut()
{
Debug::Throw( "TextEditor::cut.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
if( boxSelection_.state() == BoxSelection::State::Finished )
{
boxSelection_.toClipboard( QClipboard::Clipboard );
boxSelection_.removeSelectedText();
boxSelection_.clear();
emit copyAvailable( false );
} else BaseEditor::cut();
return;
}
//________________________________________________
void TextEditor::copy()
{
Debug::Throw( "TextEditor::copy.\n" );
if( boxSelection_.state() == BoxSelection::State::Finished ) boxSelection_.toClipboard( QClipboard::Clipboard );
else BaseEditor::copy();
}
//________________________________________________
void TextEditor::paste()
{
Debug::Throw( "TextEditor::paste.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
if( boxSelection_.state() == BoxSelection::State::Finished )
{
boxSelection_.fromClipboard( QClipboard::Clipboard );
boxSelection_.clear();
} else BaseEditor::paste();
}
//________________________________________________
void TextEditor::upperCase()
{
Debug::Throw( "TextEditor::upperCase.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
auto cursor = textCursor();
if( cursor.hasSelection() )
{
// process standard selection
cursor.insertText( cursor.selectedText().toUpper() );
} else if( boxSelection_.state() == BoxSelection::State::Finished ) {
// process box selection
boxSelection_.toUpper();
}
return;
}
//________________________________________________
void TextEditor::lowerCase()
{
Debug::Throw( "TextEditor::lowerCase.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
auto cursor = textCursor();
if( cursor.hasSelection() )
{
// process standard selection
cursor.insertText( cursor.selectedText().toLower() );
} else if( boxSelection_.state() == BoxSelection::State::Finished ) {
// process box selection
boxSelection_.toLower();
}
return;
}
//______________________________________________________________________
void TextEditor::find( TextSelection selection )
{
Debug::Throw( "TextEditor::find.\n" );
bool found( selection.flag( TextSelection::Backward ) ? _findBackward( selection, true ):_findForward( selection, true ) );
if( found ) emit matchFound();
else emit noMatchFound();
}
//______________________________________________________________________
void TextEditor::findSelectionForward()
{
Debug::Throw( "TextEditor::findSelectionForward.\n" );
_findForward( selection(), true );
}
//______________________________________________________________________
void TextEditor::findSelectionBackward()
{
Debug::Throw( "TextEditor::findSelectionBackward.\n" );
_findBackward( selection(), true );
}
//______________________________________________________________________
void TextEditor::findAgainForward()
{
Debug::Throw( "TextEditor::findAgainForward.\n" );
_findForward( lastSelection(), true );
}
//______________________________________________________________________
void TextEditor::findAgainBackward()
{
Debug::Throw( "TextEditor::findAgainBackward.\n" );
_findBackward( lastSelection(), true );
}
//______________________________________________________________________
void TextEditor::replace( TextSelection selection )
{
Debug::Throw( "TextEditor::replace.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
// see if current selection match
// perform replacement if yes
auto cursor = textCursor();
bool accepted( true );
accepted &= cursor.hasSelection();
if( selection.flag( TextSelection::RegExp ) )
{
accepted &= QRegExp( selection.text() ).exactMatch( cursor.selectedText() );
} else {
accepted &= ( !cursor.selectedText().compare(
selection.text(),
selection.flag( TextSelection::CaseSensitive ) ? Qt::CaseSensitive : Qt::CaseInsensitive ) );
}
if( accepted )
{
cursor.insertText( selection.replaceText() );
setTextCursor( cursor );
}
// try find next occurence
find( selection );
return;
}
//______________________________________________________________________
void TextEditor::replaceAgainForward()
{
Debug::Throw( "TextEditor::replaceAgainForward.\n" );
auto selection = lastSelection();
selection.setFlag( TextSelection::Backward, false );
replace( selection );
}
//______________________________________________________________________
void TextEditor::replaceAgainBackward()
{
Debug::Throw( "TextEditor::replaceAgainBackward.\n" );
auto selection = lastSelection();
selection.setFlag( TextSelection::Backward, true );
replace( selection );
}
//______________________________________________________________________
int TextEditor::replaceInSelection( TextSelection selection, bool showDialog )
{
Debug::Throw( "TextEditor::replaceInSelection.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return 0;
// progress dialog
if( showDialog ) _createProgressDialog();
int counts(0);
if( boxSelection_.state() == BoxSelection::State::Finished )
{
Debug::Throw( "TextEditor::replaceInSelection - box selection.\n" );
auto cursors( boxSelection_.cursorList() );
for( auto& cursor:cursors )
{ counts += _replaceInRange( selection, cursor, CursorMode::Move ); }
boxSelection_.clear();
} else {
Debug::Throw( "TextEditor::replaceInSelection - normal selection.\n" );
auto cursor = textCursor();
counts = _replaceInRange( selection, cursor, CursorMode::Expand );
}
Debug::Throw( "TextEditor::replaceInSelection - done.\n" );
if( showDialog ) showReplacements( counts );
return counts;
}
//______________________________________________________________________
int TextEditor::replaceInWindow( TextSelection selection, bool showDialog )
{
Debug::Throw( "TextEditor::replaceInWindow.\n" );
// progress dialog
if( showDialog ) _createProgressDialog();
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return 0;
auto cursor = textCursor();
cursor.movePosition( QTextCursor::Start );
cursor.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
auto counts = _replaceInRange( selection, cursor, CursorMode::Expand );
if( showDialog ) showReplacements( counts );
return counts;
}
//________________________________________________
void TextEditor::selectLine( int index )
{
Debug::Throw() << "TextEditor::selectLine - index: " << index << endl;
int localIndex( 0 );
auto block = document()->begin();
for( ;localIndex < index && block.isValid(); block = block.next(), localIndex++ )
{}
if( block.isValid() )
{
// create cursor at begin of block. move to end of block, keeping anchor unchanged
QTextCursor cursor( block );
cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
// assign to editor
setTextCursor( cursor );
emit lineFound();
} else {
setTextCursor( QTextCursor( document()->end() ) );
emit lineNotFound();
}
return;
}
//________________________________________________
void TextEditor::removeLine()
{
Debug::Throw( "TextEditor::removeLine.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
auto cursor = textCursor();
cursor.movePosition( QTextCursor::StartOfBlock, QTextCursor::MoveAnchor );
// create cursor selection, depending on whether next block is valid or not
if( cursor.block().next().isValid() )
{
cursor.movePosition( QTextCursor::NextBlock, QTextCursor::KeepAnchor );
removeLineBuffer_.append( cursor.selectedText() );
} else {
// move to previous character
if( cursor.block().previous().isValid() )
{
cursor.movePosition( QTextCursor::PreviousCharacter, QTextCursor::MoveAnchor );
cursor.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor );
}
// move to end of current block
cursor.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
removeLineBuffer_.append( cursor.selectedText() );
}
setUpdatesEnabled( false );
setTextCursor( cursor );
cut();
setUpdatesEnabled( true );
}
//________________________________________________
void TextEditor::clear()
{
Debug::Throw( "TextEditor::clear.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
setUpdatesEnabled( false );
selectAll();
cut();
setUpdatesEnabled( true );
}
//________________________________________________
bool TextEditor::event( QEvent* event )
{
switch( event->type() )
{
case QEvent::ToolTip:
{
auto helpEvent( static_cast<QHelpEvent*>(event) );
auto position( helpEvent->pos() );
auto globalPosition( helpEvent->globalPos() );
QString anchor;
if( trackAnchors_ && !( anchor = anchorAt( position ) ).isEmpty() )
{
QRect rect;
auto cursor( cursorForPosition( position ) );
auto block( cursor.block() );
for( auto&& it = block.begin(); !(it.atEnd()); ++it)
{
auto fragment = it.fragment();
if( !fragment.isValid() ) continue;
if( fragment.position() > cursor.position() || fragment.position() + fragment.length() <= cursor.position() )
{ continue; }
cursor.setPosition( fragment.position() );
cursor.setPosition( fragment.position() + fragment.length(), QTextCursor::KeepAnchor );
rect = cursorRect( cursor );
break;
}
QToolTip::showText( globalPosition, anchor, viewport(), rect );
}
}
break;
default: break;
}
return BaseEditor::event( event );
}
//________________________________________________
void TextEditor::enterEvent( QEvent* event )
{
#if QT_VERSION < 0x040200
Debug::Throw( "TextEditor::enterEvent.\n" );
_updateClipboardActions( QClipboard::Clipboard );
_updateClipboardActions( QClipboard::Selection );
#endif
BaseEditor::enterEvent( event );
}
//________________________________________________
void TextEditor::mousePressEvent( QMouseEvent* event )
{
Debug::Throw( "TextEditor::mousePressEvent.\n" );
// check button
if( event->button() == Qt::LeftButton )
{
// increment multiple clicks
auto cursor( textCursor() );
// increment counter
clickCounter_.increment( cursorForPosition( event->pos() ).position() );
switch( clickCounter_.counts() )
{
case 1:
{
// if single click in existing box selection, store drag position
if(
event->modifiers() == Qt::NoModifier &&
boxSelection_.state() == BoxSelection::State::Finished &&
boxSelection_.rect().contains( fromViewport( event->pos() ) )
)
{
// store position for drag
dragStart_ = event->pos();
return BaseEditor::mousePressEvent( event );
}
// if single click outside of existing box selection, clear the selection
if( event->button() == Qt::LeftButton && boxSelection_.state() == BoxSelection::State::Finished )
{
boxSelection_.clear();
_synchronizeBoxSelection();
emit copyAvailable( false );
}
// if single click and Control key pressed, start a new box selection
if( event->modifiers() == Qt::ControlModifier )
{
// try re-enable box selection in case font has changed
if( boxSelection_.checkEnabled() )
{
boxSelection_.start( event->pos() );
// synchronize with other editors
_synchronizeBoxSelection();
return;
}
}
return BaseEditor::mousePressEvent( event );
break;
}
case 2:
selectWord();
break;
case 3:
selectLine();
break;
case 4:
selectAll();
_synchronizeSelection();
_updateClipboard();
break;
default:
event->ignore();
break;
}
return;
} else {
// call base class implementation
BaseEditor::mousePressEvent( event );
}
// for mid button, locate cursor at new position
if( event->button() == Qt::MidButton )
{ setTextCursor( cursorForPosition( event->pos() ) ); }
}
//________________________________________________
void TextEditor::mouseDoubleClickEvent( QMouseEvent* event )
{
Debug::Throw( "TextEditor::mouseDoubleClickEvent.\n" );
if( event->button() != Qt::LeftButton ) BaseEditor::mouseDoubleClickEvent( event );
else {
/*
mousePressEvent is not sent in case of double-click.
we have to send one manually, in order to increment the selection mode
*/
mousePressEvent( event );
}
return;
}
//________________________________________________
void TextEditor::mouseMoveEvent( QMouseEvent* event )
{
Debug::Throw( 2, "TextEditor::mouseMoveEvent.\n" );
// do nothing if some buttons are pressed
if( trackAnchors_ )
{
QString anchor;
if( !( event->buttons() || ( anchor = anchorAt( event->pos() ) ).isEmpty() ) )
{
viewport()->setCursor( Qt::PointingHandCursor );
} else {
viewport()->setCursor( Qt::IBeamCursor );
emit linkHovered(anchor);
}
}
// see if there is a box selection in progress
if( event->buttons() == Qt::LeftButton && boxSelection_.isEnabled() && boxSelection_.state() == BoxSelection::State::Started )
{
boxSelection_.update( event->pos() );
_synchronizeBoxSelection();
emit copyAvailable( true );
if( autoScrollTimer_.isActive())
{
if( viewport()->rect().contains( event->pos() ) ) autoScrollTimer_.stop();
} else if (!viewport()->rect().contains( event->pos() )) autoScrollTimer_.start(100, this);
return;
}
// start a new box selection if requested
if( event->buttons() == Qt::LeftButton && boxSelection_.isEnabled() && event->modifiers() == Qt::ControlModifier && viewport()->rect().contains( event->pos() ) )
{
boxSelection_.start( event->pos() );
_synchronizeBoxSelection();
emit copyAvailable( true );
return;
}
// see if dragging existing box selection
if( event->buttons() == Qt::LeftButton && boxSelection_.state() == BoxSelection::State::Finished && (event->pos() - dragStart_ ).manhattanLength() > QApplication::startDragDistance() )
{
// start drag
auto drag( new QDrag(this) );
// store data
auto text( boxSelection_.toString() );
auto data = new QMimeData;
data->setText( text );
data->setData( BoxSelection::mimeType, qPrintable( text ) );
drag->setMimeData( data );
drag->start();
return;
}
return BaseEditor::mouseMoveEvent( event );
}
//________________________________________________
void TextEditor::mouseReleaseEvent( QMouseEvent* event )
{
Debug::Throw( "TextEditor::mouseReleaseEvent.\n" );
autoScrollTimer_.stop();
// no need to check for enability because there is no way for the box to start if disabled
if( event->button() == Qt::LeftButton && boxSelection_.state() == BoxSelection::State::Started )
{
boxSelection_.finish( event->pos() );
_synchronizeBoxSelection();
return BaseEditor::mouseReleaseEvent( event );
}
if( event->button() == Qt::LeftButton && boxSelection_.state() == BoxSelection::State::Finished )
{
boxSelection_.clear();
_synchronizeBoxSelection();
emit copyAvailable( false );
return BaseEditor::mouseReleaseEvent( event );
}
if( event->button() == Qt::LeftButton && clickCounter_.counts() > 1 )
{
// when multiple-click is in progress
// do nothing because it can reset the selection
event->ignore();
return;
}
QString anchor;
if( trackAnchors_ && event->button() == Qt::LeftButton &&
!event->modifiers() &&
!textCursor().hasSelection() &&
!( anchor = anchorAt( event->pos() ) ).isEmpty() )
{
emit linkActivated( anchor );
return;
}
if( event->button() == Qt::MidButton && boxSelection_.state() == BoxSelection::State::Finished )
{ boxSelection_.clear(); }
// process event
BaseEditor::mouseReleaseEvent( event );
}
//________________________________________________
void TextEditor::dropEvent( QDropEvent* event )
{
Debug::Throw( "TextEditor::dropEvent.\n" );
// static empty mimeData used to pass to base class
// so that drop events are finished properly even when actually doing nothing
static auto emptyData( new QMimeData );
QDropEvent dropEvent( event->pos(), event->possibleActions(), emptyData, Qt::NoButton, Qt::NoModifier );
// if mimeData is block selection, block selection is enabled here
// and there is no active selection (standard or box), insert new box selection
// at cursor position
if(
event->mimeData()->hasFormat( BoxSelection::mimeType ) &&
boxSelection_.isEnabled() &&
boxSelection_.state() == BoxSelection::State::Empty &&
!textCursor().hasSelection() )
{
Debug::Throw( "TextEditor::dropEvent - dropping box selection.\n" );
// retrieve text from mimeType
QString text( event->mimeData()->text() );
QStringList input_list( text.split( "\n" ) );
// create an empty boxSelection from current position with proper size
boxSelection_.start( event->pos() );
boxSelection_.finish( event->pos() );
boxSelection_.fromString( text );
boxSelection_.clear();
event->acceptProposedAction();
BaseEditor::dropEvent( &dropEvent );
return;
}
if(
event->mimeData()->hasFormat( BoxSelection::mimeType ) &&
boxSelection_.isEnabled() &&
boxSelection_.state() == BoxSelection::State::Finished &&
!toViewport( boxSelection_.rect() ).contains( event->pos() ) &&
event->source() == this
)
{
// drag is box selection and from this window. Move current block selection around.
Debug::Throw( "TextEditor::dropEvent - [box] moving current box selection.\n" );
// count rows in current selection
int rowCount( boxSelection_.cursorList().size() - 1 );
// store cursor at new insertion position
auto newCursor( cursorForPosition( event->pos() ) );
// remove current selection
boxSelection_.removeSelectedText();
boxSelection_.clear();
// prepare new selection
QRect rect( cursorRect( newCursor ) );
QPoint start( rect.center().x(), rect.top() );
QPoint end( rect.center().x(), rect.top() + rowCount*fontMetrics().height() );
boxSelection_.start( start );
boxSelection_.finish( end );
// join modifications with previous so that they appear as one entry in undo/redo list
newCursor.joinPreviousEditBlock();
// insert text in new box
boxSelection_.fromString( event->mimeData()->text() );
boxSelection_.clear();
newCursor.endEditBlock();
event->acceptProposedAction();
BaseEditor::dropEvent( &dropEvent );
return;
}
// check if there is one valid box selection that contains the drop point
if(
event->mimeData()->hasText() &&
boxSelection_.isEnabled() &&
boxSelection_.state() == BoxSelection::State::Finished &&
toViewport( boxSelection_.rect() ).contains( event->pos() ) )
{
if( event->source() == this )
{
// current selection is inserted in itself. Doing nothing
Debug::Throw( "TextEditor::dropEvent - [box] doing nothing.\n" );
event->acceptProposedAction();
BaseEditor::dropEvent( &dropEvent );
return;
} else {
// insert mine data in current box selection
Debug::Throw( "TextEditor::dropEvent - [box] inserting selection.\n" );
boxSelection_.fromString( event->mimeData()->text() );
setTextCursor( boxSelection_.cursorList().back() );
boxSelection_.clear();
event->acceptProposedAction();
BaseEditor::dropEvent( &dropEvent );
return;
}
}
// retrieve selection bounding rect
if( event->mimeData()->hasText() && textCursor().hasSelection() )
{
auto cursor( textCursor() );
auto newCursor( cursorForPosition( event->pos() ) );
bool contained(
newCursor.position() >= qMin( cursor.position(), cursor.anchor() ) &&
newCursor.position() <= qMax( cursor.position(), cursor.anchor() ) );
if( contained && event->source() != this )
{
// drag action is from another widget and ends in selection. Replace this selection
Debug::Throw( "TextEditor::dropEvent - inserting selection.\n" );
cursor.insertText( event->mimeData()->text() );
event->acceptProposedAction();
BaseEditor::dropEvent( &dropEvent );
return;
}
if( event->source() == this )
{
// drag action is from this widget
// insert selection at current location and remove old selection
Debug::Throw( "TextEditor::dropEvent - moving selection.\n" );
cursor.beginEditBlock();
cursor.removeSelectedText();
cursor.setPosition( newCursor.position() );
cursor.insertText( event->mimeData()->text() );
cursor.endEditBlock();
setTextCursor( cursor );
event->acceptProposedAction();
BaseEditor::dropEvent( &dropEvent );
return;
}
}
// for all other cases, use default
return BaseEditor::dropEvent( event );
}
//________________________________________________
void TextEditor::keyPressEvent( QKeyEvent* event )
{
// clear line buffer.
removeLineBuffer_.clear();
/*
need to grap Qt::CTRL+X, C and V event to forward them to the
daughter implementation of cut, copy and paste, otherwise
they are passed to the base class, with no way to override
*/
if( event->modifiers() == Qt::ControlModifier )
{
if( event->matches( QKeySequence::Cut ) )
{
cut();
event->ignore();
return;
}
if( event->matches( QKeySequence::Copy ) )
{
copy();
event->ignore();
return;
}
if( event->matches( QKeySequence::Paste ) )
{
paste();
event->ignore();
return;
}
}
// special key processing for box selection
if( boxSelection_.state() == BoxSelection::State::Finished )
{
if(
(event->key() >= Qt::Key_Shift && event->key() <= Qt::Key_ScrollLock) ||
(event->key() >= Qt::Key_F1 && event->key() <= Qt::Key_F25) ||
(event->key() >= Qt::Key_Super_L && event->key() <= Qt::Key_Direction_R ) ||
(event->modifiers() != Qt::NoModifier && event->modifiers() != Qt::ShiftModifier ) )
{ return BaseEditor::keyPressEvent( event ); }
// if cursor move clear selection
if( event->key() >= Qt::Key_Home && event->key() <= Qt::Key_Down )
{
boxSelection_.clear();
return BaseEditor::keyPressEvent( event );
}
// if delete or backspace remove selection
if( event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete )
{
boxSelection_.removeSelectedText();
boxSelection_.clear();
return;
}
// any other key should replace the selection
if( event->key() == Qt::Key_Tab )
{
if( !_hasTabEmulation() ) boxSelection_.fromString( tabCharacter() );
else {
// retrieve position from begin of block
int position( boxSelection_.cursorList().front().anchor() );
position -= document()->findBlock( position ).position();
int n( position % emulatedTabCharacter().size() );
boxSelection_.fromString( emulatedTabCharacter().right( emulatedTabCharacter().size()-n ) );
}
boxSelection_.clear();
} else if( !(event->text().isNull() || event->text().isEmpty() ) ) {
boxSelection_.fromString( event->text() );
boxSelection_.clear();
}
return;
}
// tab emulation
if( event->key() == Qt::Key_Tab )
{
_insertTab();
return;
}
// insertion mode
if( event->key() == Qt::Key_Insert )
{
_toggleOverwriteMode();
event->ignore();
return;
}
// default event handling
BaseEditor::keyPressEvent( event );
// check NumLock and CapsLock
/** right now this works only on X11 */
bool changed( false );
if( event->key() == Qt::Key_CapsLock ) changed = _setModifier( Modifier::CapsLock, !modifier( Modifier::CapsLock ) );
else if( event->key() == Qt::Key_NumLock ) changed = _setModifier( Modifier::NumLock, !modifier( Modifier::NumLock ) );
if( changed ) { emit modifiersChanged( modifiers() ); }
return;
}
//_______________________________________________________
void TextEditor::focusInEvent( QFocusEvent* event )
{
Debug::Throw() << "TextEditor::focusInEvent - " << key() << endl;
if(
_setModifier( Modifier::CapsLock, KeyModifier( Qt::Key_CapsLock ).state() == KeyModifier::State::On ) ||
_setModifier( Modifier::NumLock, KeyModifier( Qt::Key_NumLock ).state() == KeyModifier::State::On ) )
{ emit modifiersChanged( modifiers() );}
emit hasFocus( this );
BaseEditor::focusInEvent( event );
}
//________________________________________________
void TextEditor::contextMenuEvent( QContextMenuEvent* event )
{
Debug::Throw( "TextEditor::contextMenuEvent.\n" );
contextMenuPosition_ = event->pos();
BaseContextMenu menu( this );
menu.setHideDisabledActions( true );
installContextMenuActions( &menu );
if( !menu.isEmpty() )
{ menu.exec( event->globalPos() ); }
}
//______________________________________________________________
void TextEditor::resizeEvent( QResizeEvent* event )
{
BaseEditor::resizeEvent( event );
// update margin widget geometry
QRect rect( contentsRect() );
marginWidget_->setGeometry( QRect( rect.topLeft(), QSize( marginWidget_->width(), rect.height() ) ) );
if( lineWrapMode() == BaseEditor::NoWrap ) return;
if( event->oldSize().width() == event->size().width() ) return;
if( !lineNumberDisplay_ ) return;
// tell line number display to update at next draw
lineNumberDisplay_->needUpdate();
}
//______________________________________________________________
void TextEditor::paintEvent( QPaintEvent* event )
{
// handle block background
QTextBlock first( cursorForPosition( event->rect().topLeft() ).block() );
QTextBlock last( cursorForPosition( event->rect().bottomRight() ).block() );
// create painter and translate from widget to viewport coordinates
QPainter painter( viewport() );
painter.setClipRect( event->rect() );
painter.translate( -scrollbarPosition() );
painter.setPen( Qt::NoPen );
// loop over found blocks
for( QTextBlock block( first ); block != last.next() && block.isValid(); block = block.next() )
{
// retrieve block data and check background
// static cast is use because should be faster and safe enough here
auto data( static_cast<TextBlockData*>( block.userData() ) );
if( !(data && data->hasFlag( TextBlock::HasBackground|TextBlock::CurrentBlock ) ) ) continue;
// retrieve block rect
auto blockRect( document()->documentLayout()->blockBoundingRect( block ) );
blockRect.setLeft(0);
blockRect.setWidth( viewport()->width() + scrollbarPosition().x() );
QColor color;
if( data->hasFlag( TextBlock::CurrentBlock ) && blockHighlightAction_->isEnabled() && blockHighlightAction_->isChecked() )
{
color = palette().color( QPalette::AlternateBase );
// update current block rect
// and redraw margin if changed
if( _setCurrentBlockRect( QRect( QPoint(0, int(blockRect.topLeft().y()) ), QSize( marginWidget_->width(), int(blockRect.height()) ) ) ) )
{ marginWidget_->setDirty(); }
}
if( data->hasFlag( TextBlock::HasBackground ) )
{ color = Base::Color( color ).merge( data->background() ); }
if( color.isValid() )
{
painter.setBrush( color );
painter.drawRect( blockRect );
}
}
if( boxSelection_.state() == BoxSelection::State::Started || boxSelection_.state() == BoxSelection::State::Finished )
{
painter.setPen( boxSelection_.color() );
painter.setBrush( boxSelection_.brush() );
painter.drawRect( boxSelection_.rect().translated( 2, 2 ) );
}
painter.end();
// base class painting
BaseEditor::paintEvent( event );
return;
}
//______________________________________________________________
void TextEditor::timerEvent(QTimerEvent *event)
{
if (event->timerId() == autoScrollTimer_.timerId() )
{
auto globalPosition = QCursor::pos();
auto position = viewport()->mapFromGlobal(globalPosition);
QMouseEvent mouseEvent(QEvent::MouseMove, position, globalPosition, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
mouseMoveEvent(&mouseEvent);
} else return BaseEditor::timerEvent( event );
}
//______________________________________________________________
void TextEditor::scrollContentsBy( int dx, int dy )
{
// mark margins dirty if vertical scroll is non empty
if( dy != 0 ) marginWidget_->setDirty();
// base class call
BaseEditor::scrollContentsBy( dx, dy );
}
//______________________________________________________________
void TextEditor::_installActions()
{
Debug::Throw( "TextEditor::_installActions.\n" );
// create actions
addAction( undoAction_ = new StandardAction( StandardAction::Type::Undo, this ) );
undoAction_->setEnabled( document()->isUndoAvailable() && !isReadOnly() );
connect( undoAction_, SIGNAL(triggered()), document(), SLOT(undo()) );
connect( this, SIGNAL(undoAvailable(bool)), SLOT(_updateUndoRedoActions()) );
addAction( redoAction_ = new StandardAction( StandardAction::Type::Redo, this ) );
redoAction_->setEnabled( document()->isRedoAvailable() && !isReadOnly() );
connect( redoAction_, SIGNAL(triggered()), document(), SLOT(redo()) );
connect( this, SIGNAL(redoAvailable(bool)), SLOT(_updateUndoRedoActions()) );
addAction( cutAction_ = new StandardAction( StandardAction::Type::Cut, this ) );
cutAction_->setShortcut( QKeySequence::Cut );
connect( cutAction_, SIGNAL(triggered()), SLOT(cut()) );
addAction( copyAction_ = new StandardAction( StandardAction::Type::Copy, this ) );
connect( copyAction_, SIGNAL(triggered()), SLOT(copy()) );
addAction( pasteAction_ = new StandardAction( StandardAction::Type::Paste, this ) );
connect( pasteAction_, SIGNAL(triggered()), SLOT(paste()) );
connect( qApp->clipboard(), SIGNAL(dataChanged()), SLOT(_updatePasteAction()) );
_updatePasteAction();
addAction( clearAction_ = new QAction( tr( "Clear" ), this ) );
connect( clearAction_, SIGNAL(triggered()), SLOT(clear()) );
addAction( selectAllAction_ = new QAction( tr( "Select All" ), this ) );
selectAllAction_->setShortcut( QKeySequence::SelectAll );
selectAllAction_->setShortcutContext( Qt::WidgetShortcut );
connect( selectAllAction_, SIGNAL(triggered()), SLOT(selectAll()) );
addAction( upperCaseAction_ = new QAction( tr( "Upper Case" ), this ) );
upperCaseAction_->setShortcut( Qt::CTRL + Qt::Key_U );
upperCaseAction_->setShortcutContext( Qt::WidgetShortcut );
connect( upperCaseAction_, SIGNAL(triggered()), SLOT(upperCase()) );
addAction( lowerCaseAction_ = new QAction( tr( "Lower Case" ), this ) );
lowerCaseAction_->setShortcut( Qt::SHIFT + Qt::CTRL + Qt::Key_U );
lowerCaseAction_->setShortcutContext( Qt::WidgetShortcut );
connect( lowerCaseAction_, SIGNAL(triggered()), SLOT(lowerCase()) );
addAction( findAction_ = new QAction( IconEngine::get( IconNames::Find ), tr( "Find..." ), this ) );
findAction_->setShortcut( QKeySequence::Find );
findAction_->setShortcutContext( Qt::WidgetShortcut );
connect( findAction_, SIGNAL(triggered()), SLOT(_findFromDialog()) );
addAction( findAgainAction_ = new QAction( tr( "Find Again" ), this ) );
findAgainAction_->setShortcut( Qt::CTRL + Qt::Key_G );
findAgainAction_->setShortcutContext( Qt::WidgetShortcut );
connect( findAgainAction_, SIGNAL(triggered()), SLOT(findAgainForward()) );
addAction( findAgainBackwardAction_ = new QAction( this ) );
findAgainBackwardAction_->setShortcut( Qt::SHIFT + Qt::CTRL + Qt::Key_G );
findAgainBackwardAction_->setShortcutContext( Qt::WidgetShortcut );
connect( findAgainBackwardAction_, SIGNAL(triggered()), SLOT(findAgainBackward()) );
addAction( findSelectionAction_ = new QAction( tr( "Find Selection" ), this ) );
findSelectionAction_->setShortcut( Qt::CTRL + Qt::Key_H );
findSelectionAction_->setShortcutContext( Qt::WidgetShortcut );
connect( findSelectionAction_, SIGNAL(triggered()), SLOT(findSelectionForward()) );
addAction( findSelectionBackwardAction_ = new QAction( this ) );
findSelectionBackwardAction_->setShortcut( Qt::SHIFT + Qt::CTRL + Qt::Key_H );
findSelectionBackwardAction_->setShortcutContext( Qt::WidgetShortcut );
connect( findSelectionBackwardAction_, SIGNAL(triggered()), SLOT(findSelectionBackward()) );
addAction( replaceAction_ = new QAction( IconEngine::get( IconNames::Find ), tr( "Replace..." ), this ) );
replaceAction_->setShortcut( QKeySequence::Replace );
replaceAction_->setShortcutContext( Qt::WidgetShortcut );
connect( replaceAction_, SIGNAL(triggered()), SLOT(_replaceFromDialog()) );
addAction( replaceAgainAction_ = new QAction( tr( "Replace Again" ), this ) );
replaceAgainAction_->setShortcut( Qt::CTRL + Qt::Key_T );
replaceAgainAction_->setShortcutContext( Qt::WidgetShortcut );
connect( replaceAgainAction_, SIGNAL(triggered()), SLOT(replaceAgainForward()) );
addAction( replaceAgainBackwardAction_ = new QAction( this ) );
replaceAgainBackwardAction_->setShortcut( Qt::SHIFT + Qt::CTRL + Qt::Key_T );
replaceAgainBackwardAction_->setShortcutContext( Qt::WidgetShortcut );
connect( replaceAgainBackwardAction_, SIGNAL(triggered()), SLOT(replaceAgainBackward()) );
addAction( gotoLineAction_ = new QAction( tr( "Goto Line Number..." ), this ) );
gotoLineAction_->setShortcut( Qt::CTRL + Qt::Key_L );
gotoLineAction_->setShortcutContext( Qt::WidgetShortcut );
connect( gotoLineAction_, SIGNAL(triggered()), SLOT(_selectLineFromDialog()) );
// remove line action
QAction* remove_line_action( new QAction( tr( "Remove Current Line" ), this ) );
addAction( remove_line_action );
remove_line_action->setShortcut( Qt::CTRL + Qt::Key_K );
remove_line_action->setShortcutContext( Qt::WidgetShortcut );
connect( remove_line_action, SIGNAL(triggered()), SLOT(removeLine()) );
// current block highlight
addAction( blockHighlightAction_ = new QAction( tr( "Highlight Current Paragraph" ), this ) );
blockHighlightAction_->setCheckable( true );
blockHighlightAction_->setChecked( blockHighlight_->isEnabled() );
blockHighlightAction_->setShortcut( Qt::Key_F12 );
blockHighlightAction_->setShortcutContext( Qt::WidgetShortcut );
connect( blockHighlightAction_, SIGNAL(toggled(bool)), SLOT(_toggleBlockHighlight(bool)) );
// wrap mode
addAction( wrapModeAction_ = new QAction( tr( "Wrap Text" ), this ) );
wrapModeAction_->setCheckable( true );
wrapModeAction_->setChecked( lineWrapMode() == BaseEditor::WidgetWidth );
_setModifier( Modifier::Wrap, lineWrapMode() == BaseEditor::WidgetWidth );
wrapModeAction_->setShortcut( Qt::Key_F10 );
wrapModeAction_->setShortcutContext( Qt::WidgetShortcut );
connect( wrapModeAction_, SIGNAL(toggled(bool)), SLOT(_toggleWrapMode(bool)) );
// tab emulation action
addAction( tabEmulationAction_ = new QAction( tr( "Emulate Tabs" ), this ) );
tabEmulationAction_->setCheckable( true );
tabEmulationAction_->setChecked( hasTabEmulation_ );
connect( tabEmulationAction_, SIGNAL(toggled(bool)), SLOT(_toggleTabEmulation(bool)) );
// line number action
addAction( showLineNumberAction_ =new QAction( tr( "Show Line Numbers" ), this ) );
showLineNumberAction_->setToolTip( tr( "Show/hide line numbers" ) );
showLineNumberAction_->setCheckable( true );
showLineNumberAction_->setShortcut( Qt::Key_F11 );
showLineNumberAction_->setShortcutContext( Qt::WidgetShortcut );
connect( showLineNumberAction_, SIGNAL(toggled(bool)), SLOT(_toggleShowLineNumbers(bool)) );
// copy link
addAction( copyLinkAction_ = new QAction( IconEngine::get( IconNames::Copy ), tr( "Copy Link Location" ), this ) );
connect( copyLinkAction_, SIGNAL(triggered()), SLOT(_copyLinkLocation()) );
// update actions that depend on the presence of a selection
_updateSelectionActions( textCursor().hasSelection() );
#if QT_VERSION >= 0x040200
// update actions that depend on the content of the clipboard
// this is available only starting from Qt 4.2
connect( qApp->clipboard(), SIGNAL(changed(QClipboard::Mode)), SLOT(_updateClipboardActions(QClipboard::Mode)) );
#endif
}
//______________________________________________________________________
void TextEditor::_createFindDialog()
{
Debug::Throw( "TextEditor::_createFindDialog.\n" );
if( !findDialog_ )
{
if( !findWidget_ ) createFindWidget( false );
findDialog_ = new BaseFindDialog( this );
findDialog_->setWindowTitle( tr( "Find in Text" ) );
findDialog_->setBaseFindWidget( findWidget_ );
}
return;
}
//______________________________________________________________________
void TextEditor::_createReplaceDialog()
{
Debug::Throw( "TextEditor::_createReplaceDialog.\n" );
if( !replaceDialog_ )
{
if( !replaceWidget_ ) createReplaceWidget( false );
replaceDialog_ = new BaseReplaceDialog( this );
replaceDialog_->setWindowTitle( tr( "Replace in Text" ) );
replaceDialog_->setBaseFindWidget( replaceWidget_ );
}
return;
}
//________________________________________________
void TextEditor::_createSelectLineDialog()
{
if( !selectLineDialog_ )
{
Debug::Throw( "TextEditor::_createSelectLineDialog.\n" );
selectLineDialog_ = new SelectLineDialog( this );
if( !selectLineWidget_ ) createSelectLineWidget( false );
selectLineDialog_->setSelectLineWidget( selectLineWidget_ );
}
return;
}
//__________________________________________________
void TextEditor::_createProgressDialog()
{
Debug::Throw( "TextEditor::_createProgressDialog.\n" );
// create dialog
auto dialog = new QProgressDialog(0);
dialog->setAttribute( Qt::WA_DeleteOnClose, true );
dialog->setLabelText( tr( "Replace text in selection" ) );
dialog->setWindowTitle( tr( "Replace in Text" ) );
// connections
connect( this, SIGNAL(busy(int)), dialog, SLOT(setMaximum(int)) );
connect( this, SIGNAL(progressAvailable(int)), dialog, SLOT(setValue(int)) );
connect( this, SIGNAL(idle()), dialog, SLOT(close()) );
QtUtil::centerOnWidget( dialog, this );
dialog->show();
}
//______________________________________________________________________
bool TextEditor::_findForward( const TextSelection& selection, bool rewind )
{
Debug::Throw( "TextEditor::_findForward.\n" );
if( selection.text().isEmpty() ) return false;
// store selection
setLastSelection( selection );
// retrieve current cursor
auto cursor( textCursor() );
// if no_increment, start from the beginning of the possible current selection
if( cursor.hasSelection() && selection.flag( TextSelection::NoIncrement ) )
{ cursor.setPosition( cursor.anchor() ); }
if( selection.flag( TextSelection::RegExp ) )
{
// construct regexp and check
QRegExp regexp( selection.text() );
if( !regexp.isValid() )
{
InformationDialog( this, tr( "Invalid regular expression. Find canceled" ) ).exec();
return false;
}
// case sensitivity
regexp.setCaseSensitivity( selection.flag( TextSelection::CaseSensitive ) ? Qt::CaseSensitive : Qt::CaseInsensitive );
// make a copy of current cursor
auto found( cursor );
// if current text has selection that match, make sure pointer is located at the end of it
if( found.hasSelection() && regexp.exactMatch( found.selectedText() ) )
{ found.setPosition( qMax( found.position(), found.anchor() ) ); }
// move the found to the end of the document
// and retrieve selected text
found.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
QString text( found.selectedText() );
// parse text
int match = regexp.indexIn( text );
int length = regexp.matchedLength();
if( match < 0 )
{
// no match found
// if not rewind, stop here
if( !rewind ) return false;
// update selection to the beginning of the document
found.movePosition( QTextCursor::Start, QTextCursor::KeepAnchor );
text = found.selectedText();
match = regexp.indexIn( text );
length = regexp.matchedLength();
}
// no match found. Return
if( match < 0 ) return false;
// match found. Update selection and return
int position( match + qMin( found.anchor(), found.position() ) );
found.setPosition( position, QTextCursor::MoveAnchor );
found.setPosition( position+length, QTextCursor::KeepAnchor );
setTextCursor( found );
// copy selected text to clipboard
if( qApp->clipboard()->supportsSelection() )
{ qApp->clipboard()->setMimeData( createMimeDataFromSelection(), QClipboard::Selection ); }
return true;
} else {
// search flags
QTextDocument::FindFlags flags( 0 );
if( selection.flag( TextSelection::CaseSensitive ) ) flags |= QTextDocument::FindCaseSensitively;
if( selection.flag( TextSelection::EntireWord ) ) flags |= QTextDocument::FindWholeWords;
auto found( document()->find( selection.text(), cursor, flags ) );
// find failed.
if( found.isNull() && rewind )
{
cursor.movePosition( QTextCursor::Start );
found = document()->find( selection.text(), cursor, flags );
}
if( found.isNull() ) return false;
else {
setTextCursor( found );
// copy selected text to clipboard
if( qApp->clipboard()->supportsSelection() )
{ qApp->clipboard()->setMimeData( createMimeDataFromSelection(), QClipboard::Selection ); }
return true;
}
}
// useless
return false;
}
//______________________________________________________________________
bool TextEditor::_findBackward( const TextSelection& selection, bool rewind )
{
Debug::Throw( "TextEditor::_findBackward.\n" );
if( selection.text().isEmpty() ) return false;
setLastSelection( selection );
// retrieve current cursor
auto cursor( textCursor() );
// if no_increment, start from the beginning of the possible current selection
if( cursor.hasSelection() && selection.flag( TextSelection::NoIncrement ) )
{ cursor.setPosition( cursor.anchor()+selection.text().size()+1 ); }
if( selection.flag( TextSelection::RegExp ) )
{
// construct regexp and check
QRegExp regexp( selection.text() );
if( !regexp.isValid() )
{
InformationDialog( this, tr( "Invalid regular expression. Find canceled" ) ).exec();
return false;
}
// case sensitivity
regexp.setCaseSensitivity( selection.flag( TextSelection::CaseSensitive ) ? Qt::CaseSensitive : Qt::CaseInsensitive );
// make a copy of current cursor
auto found( cursor );
// if current text has selection that match, make sure pointer is located at the end of it
if( found.hasSelection() && regexp.exactMatch( found.selectedText() ) )
{ found.setPosition( qMin( found.position(), found.anchor() ) ); }
// move cursor to beginning of the text
found.movePosition( QTextCursor::Start, QTextCursor::KeepAnchor );
QString text( found.selectedText() );
// parse text
int match = regexp.lastIndexIn( text );
int length = regexp.matchedLength();
if( match < 0 )
{
// no match found
// if not rewind, stop here
if( !rewind ) return false;
// update selection to the beginning of the document
found.movePosition( QTextCursor::End, QTextCursor::KeepAnchor );
text = found.selectedText();
match = regexp.lastIndexIn( text );
length = regexp.matchedLength();
}
// no match found. Return
if( match < 0 ) return false;
// match found. Update selection and return
int position( match + qMin( found.anchor(), found.position() )+length );
found.setPosition( position, QTextCursor::MoveAnchor );
found.setPosition( position-length, QTextCursor::KeepAnchor );
setTextCursor( found );
// copy selected text to clipboard
if( qApp->clipboard()->supportsSelection() )
{ qApp->clipboard()->setMimeData( createMimeDataFromSelection(), QClipboard::Selection ); }
return true;
} else {
// search flags
QTextDocument::FindFlags flags( QTextDocument::FindBackward );
if( selection.flag( TextSelection::CaseSensitive ) ) flags |= QTextDocument::FindCaseSensitively;
if( selection.flag( TextSelection::EntireWord ) ) flags |= QTextDocument::FindWholeWords;
auto found( document()->find( selection.text(), cursor, flags ) );
// find failed.
if( found.isNull() && rewind )
{
cursor.movePosition( QTextCursor::End );
found = document()->find( selection.text(), cursor, flags );
}
if( found.isNull() ) return false;
else {
setTextCursor( found );
return true;
}
}
// useless
return false;
}
//______________________________________________________________________
int TextEditor::_replaceInRange( const TextSelection& selection, QTextCursor& cursor, CursorMode mode )
{
Debug::Throw()
<< "TextEditor::_replaceInRange -"
<< " anchor: " << cursor.anchor()
<< " position: " << cursor.position()
<< " selection: " << selection.text()
<< " replacement: " << selection.replaceText()
<< endl;
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return 0;
// check selection
if( selection.text().isEmpty() ) return 0;
setLastSelection( selection );
// check cursor
if( !cursor.hasSelection() ) return 0;
// store number of matches
// and make local copy of cursor
int found = 0;
int savedAnchor( qMin( cursor.position(), cursor.anchor() ) );
int savedPosition( qMax( cursor.position(), cursor.anchor() ) );
int currentPosition( savedAnchor );
// check if regexp should be used or not
if( selection.flag( TextSelection::RegExp ) )
{
Debug::Throw( "TextEditor::_replaceInRange - regexp.\n" );
// construct regexp and check
QRegExp regexp( selection.text() );
if( !regexp.isValid() )
{
InformationDialog( this, tr( "Invalid regular expression. Find canceled" ) ).exec();
return false;
}
// case sensitivity
regexp.setCaseSensitivity( selection.flag( TextSelection::CaseSensitive ) ? Qt::CaseSensitive : Qt::CaseInsensitive );
// replace everything in selected text
QString selectedText( cursor.selectedText() );
emit busy( selectedText.size() );
for( int position = 0; (position = regexp.indexIn( selectedText, position )) != -1; )
{
// replace in selected text
selectedText.replace( position, regexp.matchedLength(), selection.replaceText() );
// replace in cursor
/* this is to allow for undoing the changes one by one */
cursor.setPosition( savedAnchor + position );
cursor.setPosition( savedAnchor + position + regexp.matchedLength(), QTextCursor::KeepAnchor );
cursor.insertText( selection.replaceText() );
currentPosition = cursor.position();
// increment position
position += selection.replaceText().size();
currentPosition = savedAnchor + position;
found++;
emit progressAvailable( position );
}
emit idle();
// update cursor
if( mode == CursorMode::Expand )
{
cursor.setPosition( savedAnchor );
cursor.setPosition( savedAnchor + selectedText.length(), QTextCursor::KeepAnchor );
} else if( mode == CursorMode::Move ) cursor.setPosition( currentPosition );
} else {
Debug::Throw( "TextEditor::_replaceInRange - normal replacement.\n" );
emit busy( cursor.selectedText().size() );
// changes local cursor to beginning of the selection
cursor.setPosition( savedAnchor );
// define search flags
QTextDocument::FindFlags flags(0);
if( selection.flag( TextSelection::CaseSensitive ) ) flags |= QTextDocument::FindCaseSensitively;
if( selection.flag( TextSelection::EntireWord ) ) flags |= QTextDocument::FindWholeWords;
while( !( cursor = document()->find( selection.text(), cursor, flags ) ).isNull() && cursor.position() <= savedPosition )
{
// perform replacement
cursor.insertText( selection.replaceText() );
currentPosition = cursor.position();
savedPosition += selection.replaceText().size() - selection.text().size();
found ++;
emit busy( savedPosition );
emit progressAvailable( currentPosition );
}
emit idle();
if( mode == CursorMode::Expand )
{
cursor.setPosition( savedAnchor );
cursor.setPosition( savedPosition, QTextCursor::KeepAnchor );
} else if( mode == CursorMode::Move ) cursor.setPosition( currentPosition );
}
return found;
}
//_____________________________________________________________
void TextEditor::_synchronizeBoxSelection() const
{
if( !isSynchronized() ) return;
// Debug::Throw( "TextEditor::_synchronizeBoxSelection.\n" );
Base::KeySet<TextEditor> displays( this );
for( const auto& editor:Base::KeySet<TextEditor>(this) )
{ editor->boxSelection_.synchronize( boxSelection_ ); }
}
//_____________________________________________________________
bool TextEditor::_setLeftMargin( int margin )
{
Debug::Throw() << "TextEditor::_setLeftMargin - margin: " << margin << endl;
if( margin == _leftMargin() ) return false;
leftMargin_ = margin;
setViewportMargins( _leftMargin(), 0, 0, 0 );
marginWidget_->resize( _leftMargin(), marginWidget_->height() );
return true;
}
//_____________________________________________________________
void TextEditor::_toggleOverwriteMode()
{
Debug::Throw( "TextEditor::_toggleOverwriteMode.\n" );
setOverwriteMode( !overwriteMode() );
if( _setModifier( Modifier::Insert, overwriteMode() ) ) emit modifiersChanged( modifiers() );
return;
}
//________________________________________________
bool TextEditor::_setTabSize( int tabSize )
{
Debug::Throw() << "TextEditor::_setTabSize - " << tabSize << endl;
Q_ASSERT( tabSize > 0 );
int stopWidth( tabSize * fontMetrics().width( " " ) );
if( tabSize == emulatedTab_.size() && tabStopWidth() == stopWidth )
{ return false; }
// create strings and regular expressions
// define normal tabs
normalTab_ = "\t";
normalTabRegexp_.setPattern( "^(\\t)+" );
setTabStopWidth( stopWidth );
// define emulated tabs
emulatedTab_ = QString( tabSize, ' ' );
emulatedTabRegexp_.setPattern( QString( "^(%1)+" ).arg( emulatedTab_ ) );
// update tab string according to tab emulation state
if( _hasTabEmulation() ) tab_ = emulatedTab_;
return true;
}
//_____________________________________________________________
void TextEditor::_insertTab()
{
Debug::Throw( "TextEditor::_insertTab.\n" );
// retrieve current cursor
auto cursor( textCursor() );
if( !_hasTabEmulation() ) cursor.insertText( normalTabCharacter() );
else {
// retrieve position from begin of block
int position( qMin( cursor.position(), cursor.anchor() ) );
position -= document()->findBlock( position ).position();
int n( position % emulatedTabCharacter().size() );
cursor.insertText( emulatedTabCharacter().right( emulatedTabCharacter().size()-n ) );
}
return;
}
//___________________________________________________________________________
bool TextEditor::_updateMargin()
{
Debug::Throw( "TextEditor::_updateMargin.\n" );
int leftMargin( 0 );
if( showLineNumberAction_->isChecked() && showLineNumberAction_->isVisible() )
{ leftMargin += lineNumberDisplay_->width(); }
return _setLeftMargin( leftMargin );
if( leftMargin_ == leftMargin ) return false;
return true;
}
//________________________________________________
void TextEditor::_updateConfiguration()
{
Debug::Throw( "TextEditor::_updateConfiguration.\n" );
// wrap mode
if( wrapFromOptions() )
{ wrapModeAction_->setChecked( XmlOptions::get().get<bool>( "WRAP_TEXT" ) ); }
if( lineNumbersFromOptions() )
{ showLineNumberAction_->setChecked( XmlOptions::get().get<bool>( "SHOW_LINE_NUMBERS" ) ); }
// tab emulation
_setTabSize( XmlOptions::get().get<int>("TAB_SIZE") );
tabEmulationAction_->setChecked( XmlOptions::get().get<bool>( "TAB_EMULATION" ) );
// paragraph highlighting
if( highlightBlockFromOptions_ )
{
blockHighlight_->setEnabled( XmlOptions::get().get<bool>( "HIGHLIGHT_PARAGRAPH" ) );
blockHighlightAction_->setEnabled( true );
blockHighlightAction_->setChecked( XmlOptions::get().get<bool>( "HIGHLIGHT_PARAGRAPH" ) );
}
// update margins
lineNumberDisplay_->updateWidth( document()->blockCount() );
_updateMargin();
marginWidget_->setDirty();
// update box configuration
// clear
boxSelection_.updateConfiguration();
if( !boxSelection_.isEnabled() && boxSelection_.state() != BoxSelection::State::Empty )
{
boxSelection_.clear();
_synchronizeBoxSelection();
emit copyAvailable( false );
}
// cursor monitor
cursorMonitor_.setEnabled( XmlOptions::get().get<int>( "AUTOHIDE_CURSOR_DELAY" ) > 0 );
cursorMonitor_.setAutoHideDelay( XmlOptions::get().get<int>( "AUTOHIDE_CURSOR_DELAY" ) * 1000 );
return;
}
//________________________________________________
void TextEditor::_synchronizeSelection()
{
//Debug::Throw( "TextEditor::_synchronizeSelection.\n" );
if( !isSynchronized() ) return;
for( const auto& editor:Base::KeySet<TextEditor>(this) )
{
// check if textCursor is different
if( editor->textCursor().position() == textCursor().position() &&
editor->textCursor().anchor() == textCursor().anchor() )
continue;
// store scrollbar positions
auto scrollbars( editor->scrollbarPosition() );
editor->setSynchronized( false );
editor->setUpdatesEnabled( false );
editor->setTextCursor( textCursor() );
// restore scrollbar positions
editor->horizontalScrollBar()->setValue( scrollbars.x() );
editor->verticalScrollBar()->setValue( scrollbars.y() );
editor->setUpdatesEnabled( true );
editor->setSynchronized( true );
}
}
//________________________________________________________
void TextEditor::_updateContentActions()
{
Debug::Throw( "TextEditor::_updateContentActions.\n" );
// actions
clearAction_->setEnabled( !(document()->isEmpty() || isReadOnly() ) );
selectAllAction_->setEnabled( !document()->isEmpty() );
}
//________________________________________________
void TextEditor::_updateReadOnlyActions( bool readOnly )
{
Debug::Throw( "TextEditor::_updateReadOnlyActions.\n" );
bool hasSelection( textCursor().hasSelection() );
clearAction_->setEnabled( !(readOnly || document()->isEmpty() ) );
cutAction_->setEnabled( hasSelection && !readOnly );
upperCaseAction_->setEnabled( hasSelection && !readOnly );
lowerCaseAction_->setEnabled( hasSelection && !readOnly );
replaceAction_->setEnabled( !readOnly );
replaceAgainAction_->setEnabled( !readOnly );
replaceAgainBackwardAction_->setEnabled( !readOnly );
_updateUndoRedoActions();
_updatePasteAction();
}
//________________________________________________
void TextEditor::_updateUndoRedoActions()
{
Debug::Throw( "TextEditor::_updateUndoRedoActions.\n" );
undoAction_->setEnabled( document()->isUndoAvailable() && !isReadOnly() );
redoAction_->setEnabled( document()->isRedoAvailable() && !isReadOnly() );
}
//________________________________________________
void TextEditor::_updateSelectionActions( bool hasSelection )
{
Debug::Throw() << "TextEditor::_updateSelectionActions - hasSelection: " << hasSelection << endl;
bool editable( !isReadOnly() );
cutAction_->setEnabled( hasSelection && editable );
copyAction_->setEnabled( hasSelection );
upperCaseAction_->setEnabled( hasSelection && editable );
lowerCaseAction_->setEnabled( hasSelection && editable );
#if QT_VERSION < 0x040200
// update clipboard actions, based on the clipboard content
// this is done only for QT versions < 4.2. For higher versions
// this is called directly from a QClipboard signal
_updateClipboardActions( QClipboard::Clipboard );
_updateClipboardActions( QClipboard::Selection );
#endif
}
//________________________________________________
void TextEditor::_updateClipboardActions( QClipboard::Mode mode )
{
Debug::Throw( "TextEditor::_updateClipboardActions.\n" );
if( mode == QClipboard::Clipboard )
{
const bool hasClipboard( !qApp->clipboard()->text( QClipboard::Clipboard ).isEmpty() );
pasteAction_->setEnabled( hasClipboard && !isReadOnly() );
} else if( mode == QClipboard::Selection ) {
const bool hasSelection( !qApp->clipboard()->text( QClipboard::Selection ).isEmpty() );
findSelectionAction_->setEnabled( hasSelection );
findSelectionBackwardAction_->setEnabled( hasSelection );
}
}
//________________________________________________
void TextEditor::_updateClipboard()
{
Debug::Throw( "TextEditor::_updateClipboard.\n" );
// copy selected text to clipboard
if( qApp->clipboard()->supportsSelection() && textCursor().hasSelection() )
{ qApp->clipboard()->setMimeData( createMimeDataFromSelection(), QClipboard::Selection ); }
}
//_____________________________________________
void TextEditor::_copyLinkLocation()
{
Debug::Throw( "TextEditor::_copyLinkLocation.\n" );
if( !trackAnchors_ ) return;
auto anchor( this->anchor() );
if( anchor.isEmpty() ) return;
// copy selected text to clipboard
qApp->clipboard()->setText( anchor, QClipboard::Clipboard );
if( qApp->clipboard()->supportsSelection() ) qApp->clipboard()->setText( anchor, QClipboard::Selection );
if( qApp->clipboard()->supportsFindBuffer() ) qApp->clipboard()->setText( anchor, QClipboard::FindBuffer );
}
//________________________________________________
void TextEditor::_updatePasteAction()
{
Debug::Throw( "TextEditor::_updatePasteAction.\n" );
const bool hasClipboard( !qApp->clipboard()->text().isEmpty() );
pasteAction_->setEnabled( hasClipboard && !isReadOnly() );
}
//_________________________________________________
void TextEditor::_toggleBlockHighlight( bool state )
{
Debug::Throw( "TextEditor::_toggleBlockHighlight.\n" );
// enable
blockHighlight_->setEnabled( state );
// update options
XmlOptions::get().set<bool>( "HIGHLIGHT_PARAGRAPH", state );
// update current paragraph
if( blockHighlight_->isEnabled() ) blockHighlight_->highlight();
else blockHighlight_->clear();
// redraw
update();
// propagate to other displays
if( isSynchronized() )
{
// temporarely disable synchronization
// to avoid infinite loop
setSynchronized( false );
for( const auto& editor:Base::KeySet<TextEditor>( this ) )
{ if( editor->isSynchronized() ) editor->blockHighlightAction_->setChecked( state ); }
setSynchronized( true );
}
}
//________________________________________________
bool TextEditor::_toggleWrapMode( bool state )
{
Debug::Throw() << "TextEditor::_toggleWrapMode - " << (state ? "True":"false") << endl;
LineWrapMode mode( state ? BaseEditor::WidgetWidth : BaseEditor::NoWrap );
if( mode == lineWrapMode() ) return false;
setLineWrapMode( mode );
if( _setModifier( Modifier::Wrap, state ) ) emit modifiersChanged( modifiers() );
// propagate to associated display
if( isSynchronized() )
{
// temporarely disable synchronization
// to avoid infinite loop
setSynchronized( false );
for( const auto& editor:Base::KeySet<TextEditor>( this ) )
{ if( editor->isSynchronized() ) editor->wrapModeAction_->setChecked( state ); }
setSynchronized( true );
}
return true;
}
//________________________________________________
bool TextEditor::_toggleTabEmulation( bool state )
{
Debug::Throw() << "TextEditor::_toggleTabEmulation - " << (state ? "True":"false") << endl;
// check if changed
if( hasTabEmulation_ == state ) return false;
// set flag
hasTabEmulation_ = state;
tab_ = hasTabEmulation_ ? emulatedTab_ : normalTab_;
tabRegexp_ = hasTabEmulation_ ? emulatedTabRegexp_ : normalTabRegexp_;
// propagate to associated display
if( isSynchronized() )
{
// temporarely disable synchronization
// to avoid infinite loop
setSynchronized( false );
for( const auto& editor:Base::KeySet<TextEditor>( this ) )
{ if( editor->isSynchronized() ) editor->tabEmulationAction_->setChecked( state ); }
setSynchronized( true );
}
return true;
}
//_______________________________________________________
void TextEditor::_toggleShowLineNumbers( bool state )
{
_updateMargin();
// update options
XmlOptions::get().set<bool>( "SHOW_LINE_NUMBERS", state );
// propagate to other displays
if( isSynchronized() )
{
// temporarely disable synchronization
// to avoid infinite loop
setSynchronized( false );
for( const auto& editor:Base::KeySet<TextEditor>( this ) )
{ if( editor->isSynchronized() ) editor->showLineNumberAction_->setChecked( state ); }
setSynchronized( true );
}
return;
}
//________________________________________________________
void TextEditor::_blockCountChanged( int count )
{
Debug::Throw( "TextEditor::_blockCountChanged.\n" );
// margins
if( !( lineNumberDisplay_ && lineNumberDisplay_->updateWidth( count ) ) ) return;
if( !( showLineNumberAction_ && showLineNumberAction_->isChecked() && showLineNumberAction_->isVisible() ) ) return;
_updateMargin();
update();
}
//_____________________________________________________________________
void TextEditor::_findFromDialog()
{
Debug::Throw( "TextEditor::_findFromDialog.\n" );
// set default text
// update find text
QString text( selection().text() );
if( !text.isEmpty() )
{
const int maxLength( 1024 );
text = text.left( maxLength );
}
// create
if( useEmbeddedWidgets_ )
{
if( !findWidget_ ) createFindWidget( true );
findWidget_->show();
} else {
if( !findDialog_ ) _createFindDialog();
findDialog_->centerOnParent();
findDialog_->show();
findDialog_->activateWindow();
}
findWidget_->enableRegExp( true );
findWidget_->synchronize();
findWidget_->matchFound();
findWidget_->setText( text );
findWidget_->editor().setFocus();
return;
}
//_____________________________________________________________________
void TextEditor::_replaceFromDialog()
{
Debug::Throw( "TextEditor::_replaceFromDialog.\n" );
// need to check for editability because apparently even if calling action is disabled,
// the shortcut still can be called
if( isReadOnly() ) return;
if( useEmbeddedWidgets_ )
{
if( !replaceWidget_ ) createReplaceWidget( true );
replaceWidget_->show();
} else {
if( !replaceDialog_ ) _createReplaceDialog();
replaceDialog_->centerOnParent();
replaceDialog_->show();
replaceDialog_->activateWindow();
}
// synchronize combo-boxes
replaceWidget_->synchronize();
replaceWidget_->matchFound();
// update find text
QString text;
if( !( text = qApp->clipboard()->text( QClipboard::Selection) ).isEmpty() ) replaceWidget_->setText( text );
else if( textCursor().hasSelection() ) replaceWidget_->setText( textCursor().selectedText() );
else if( !( text = lastSelection().text() ).isEmpty() ) replaceWidget_->setText( text );
replaceWidget_->editor().setFocus();
// update replace text
if( !lastSelection().replaceText().isEmpty() ) replaceWidget_->setReplaceText( lastSelection().replaceText() );
return;
}
//_____________________________________________
void TextEditor::_updateReplaceInSelection()
{ if( replaceWidget_ ) replaceWidget_->enableReplaceInSelection( hasSelection() ); }
//________________________________________________
void TextEditor::_selectLineFromDialog()
{
Debug::Throw( "TextEditor::_selectLineFromDialog.\n" );
if( useEmbeddedWidgets_ )
{
if( !selectLineWidget_ ) createSelectLineWidget( true );
selectLineWidget_->show();
} else {
if( !selectLineDialog_ ) _createSelectLineDialog();
selectLineDialog_->centerOnParent();
selectLineDialog_->show();
selectLineDialog_->activateWindow();
}
selectLineWidget_->matchFound();
selectLineWidget_->editor().clear();
selectLineWidget_->editor().setFocus();
}
//_________________________________________________________
TextEditor::Container::Container( QWidget* parent ):
QWidget( parent ),
Counter( "TextEditor::Container" ),
editor_( new TextEditor )
{ _initialize(); }
//_________________________________________________________
TextEditor::Container::Container( QWidget* parent, TextEditor* editor ):
QWidget( parent ),
Counter( "TextEditor::Container" ),
editor_( editor )
{ _initialize(); }
//_________________________________________________________
void TextEditor::Container::_initialize()
{
Debug::Throw( "TextEditor::Container::_initialize.\n" );
editor_->setParent( this );
QVBoxLayout* vLayout = new QVBoxLayout;
vLayout->setMargin(0);
vLayout->setSpacing(2);
setLayout( vLayout );
// editor
vLayout->addWidget( editor_ );
editor_->useEmbeddedWidgets_ = true;
// find widget
editor_->createFindWidget( true );
editor_->findWidget_->setParent( this );
vLayout->addWidget( editor_->findWidget_ );
editor_->findWidget_->hide();
// replace widget
editor_->createReplaceWidget( true );
editor_->replaceWidget_->setParent( this );
vLayout->addWidget( editor_->replaceWidget_ );
editor_->replaceWidget_->hide();
// select line dialog
editor_->createSelectLineWidget( true );
editor_->selectLineWidget_->setParent( this );
vLayout->addWidget( editor_->selectLineWidget_ );
editor_->selectLineWidget_->hide();
// make connections so that focus is restored on close
connect( &editor_->findWidget_->closeButton(), SIGNAL(clicked()), editor_, SLOT(setFocus()) );
connect( &editor_->replaceWidget_->closeButton(), SIGNAL(clicked()), editor_, SLOT(setFocus()) );
connect( &editor_->selectLineWidget_->closeButton(), SIGNAL(clicked()), editor_, SLOT(setFocus()) );
}
|