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 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
|
# vim:et sts=4 sw=4
#
# ibus-table-setup - Setup UI for ibus-table
#
# Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2010 BYVoid <byvoid1@gmail.com>
# Copyright (c) 2012 Ma Xiaojun <damage3025@gmail.com>
# Copyright (c) 2012 mozbugbox <mozbugbox@yahoo.com.au>
# Copyright (c) 2014-2022 Mike FABIAN <mfabian@redhat.com>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This library 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 Lesser General Public License
# along with this library. If not, see <http://www.gnu.org/licenses/>
'''
The setup tool for ibus-table.
'''
from typing import Union
from typing import Any
from typing import Dict
from typing import List
from typing import Tuple
from typing import Optional
from types import FrameType
import sys
import os
import re
import html
import signal
import argparse
import locale
import copy
import logging
import logging.handlers
import dbus # type: ignore
import dbus.service # type: ignore
# pylint: disable=wrong-import-position
from gi import require_version # type: ignore
require_version('Gio', '2.0')
from gi.repository import Gio # type: ignore
require_version('GLib', '2.0')
from gi.repository import GLib
# set_prgname before importing other modules to show the name in warning
# messages when import modules are failed. E.g. Gtk.
GLib.set_application_name('IBus Table Preferences')
# This makes gnome-shell load the .desktop file when running under Wayland:
GLib.set_prgname('ibus-setup-table')
require_version('Gdk', '3.0')
from gi.repository import Gdk
require_version('Gtk', '3.0')
from gi.repository import Gtk
require_version('Pango', '1.0')
from gi.repository import Pango
require_version('IBus', '1.0')
from gi.repository import IBus
# pylint: enable=wrong-import-position
# pylint: disable=import-error
sys.path = [sys.path[0]+'/../engine'] + sys.path
import tabsqlitedb
import ibus_table_location
import it_util
import it_sound
# pylint: enable=import-error
from i18n import N_, _, init as i18n_init
LOGGER = logging.getLogger('ibus-table')
GLIB_MAIN_LOOP: Optional[GLib.MainLoop] = None
GTK_VERSION = (Gtk.get_major_version(),
Gtk.get_minor_version(),
Gtk.get_micro_version())
PARSER = argparse.ArgumentParser(
description='ibus-table setup tool')
PARSER.add_argument(
'-n', '--engine-name',
action='store',
type=str,
dest='engine_name',
default='',
help=('Set the name of the engine, for example “table:cangjie3” '
'or just “cangjie3”. Default: "%(default)s". '
'If this option is not used, the value of the environment '
'variable IBUS_ENGINE_NAME is tried instead. '
'If the variable IBUS_ENGINE_NAME is also not set or empty, '
'this help is printed.'))
PARSER.add_argument(
'-q', '--no-debug',
action='store_true',
default=False,
help=('Do not write log file '
'~/.cache/ibus-table/setup-debug.log, '
'default: %(default)s'))
_ARGS = PARSER.parse_args()
# Keep some translatable strings which are not used anymore here
# so that the translations which have been done already are not lost.
# I might want to use these strings again later.
UNUSED_OLD_TRANSLATIONS = [
N_('Initial state'),
N_('Direct input'),
N_('Table input'),
]
class SetupUI(Gtk.Window): # type: ignore
'''
User interface of the setup tool
'''
def __init__( # pylint: disable=too-many-statements
self, engine_name: str = '') -> None:
self._engine_name = engine_name
Gtk.Window.__init__(
self,
title='码 IBus Table '
+ self._engine_name + ' '
+ _('Preferences'))
Gtk.Window.set_default_icon_from_file(
os.path.join(
ibus_table_location.data(), 'icons', 'ibus-table.svg'))
self.set_name('IBusTablePreferences')
self.set_modal(True)
style_provider = Gtk.CssProvider()
style_provider.load_from_data(
b'''
#IBusTablePreferences {
}
row { /* This is for listbox rows */
border-style: groove;
border-width: 0.05px;
}
''')
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
database_filename = os.path.join(
os.path.join(ibus_table_location.data(), 'tables'),
self._engine_name + '.db')
if not os.path.exists(database_filename):
LOGGER.error('Cannot open database %s', database_filename)
sys.exit(1)
user_database_filename = os.path.basename(database_filename).replace(
'.db', '-user.db')
self.tabsqlitedb = tabsqlitedb.TabSqliteDb(
filename=database_filename,
user_db=user_database_filename,
create_database=False)
self.__is_db_chinese = False
self.__is_cjk = False
languages_str = self.tabsqlitedb.ime_properties.get('languages')
if languages_str:
languages = languages_str.split(',')
for language in languages:
if language.strip().startswith('zh'):
self.__is_db_chinese = True
for lang in ['zh', 'ja', 'ko']:
if language.strip().startswith(lang):
self.__is_cjk = True
self.__user_can_define_phrase = False
user_can_define_phrase = self.tabsqlitedb.ime_properties.get(
'user_can_define_phrase')
if user_can_define_phrase:
self.__user_can_define_phrase = (
user_can_define_phrase.lower() == 'true')
self.__rules = self.tabsqlitedb.ime_properties.get('rules')
self._gsettings = Gio.Settings(
schema='org.freedesktop.ibus.engine.table',
path=f'/org/freedesktop/ibus/engine/table/{self._engine_name}/')
self._fill_settings_dict()
self.set_title(
'码 IBus Table '
+ self._engine_name + ' '
+ _('Preferences'))
self.connect('destroy-event', self.__class__._on_destroy_event)
self.connect('delete-event', self.__class__._on_delete_event)
self._main_container = Gtk.Box()
self._main_container.set_orientation(Gtk.Orientation.VERTICAL)
self._main_container.set_spacing(0)
self.add(self._main_container) # pylint: disable=no-member
self._notebook = Gtk.Notebook()
self._notebook.set_visible(True)
self._notebook.set_can_focus(False)
self._notebook.set_scrollable(True)
self._notebook.set_hexpand(True)
self._notebook.set_vexpand(True)
self._main_container.pack_start(self._notebook, True, True, 0)
self._dialog_action_area = Gtk.Box()
self._dialog_action_area.set_orientation(Gtk.Orientation.HORIZONTAL)
self._dialog_action_area.set_visible(True)
self._dialog_action_area.set_can_focus(False)
self._dialog_action_area.set_hexpand(True)
self._dialog_action_area.set_vexpand(False)
self._main_container.pack_end(self._dialog_action_area, True, True, 0)
self._about_button = Gtk.Button(label=_('About'))
self._about_button.set_hexpand(True)
self._about_button.connect('clicked', self.__class__._on_about_button_clicked)
self._dialog_action_area.add(self._about_button)
self._restore_all_defaults_button = Gtk.Button()
self._restore_all_defaults_button.set_hexpand(True)
self._restore_all_defaults_button_label = Gtk.Label()
self._restore_all_defaults_button_label.set_text(
_('Restore all defaults'))
self._restore_all_defaults_button.add(
self._restore_all_defaults_button_label)
self._restore_all_defaults_button.connect(
'clicked', self._on_restore_all_defaults_button_clicked)
self._dialog_action_area.add(self._restore_all_defaults_button)
self._close_button = Gtk.Button()
self._close_button.set_hexpand(True)
self._close_button_label = Gtk.Label()
self._close_button_label.set_text_with_mnemonic(_('_Close'))
self._close_button.add(self._close_button_label)
self._close_button.connect('clicked', self.__class__._on_close_clicked)
self._dialog_action_area.add(self._close_button)
grid_border_width = 10
grid_row_spacing = 10
grid_column_spacing = 10
self._options_grid = Gtk.Grid()
self._options_grid.set_visible(True)
self._options_grid.set_can_focus(False)
self._options_grid.set_border_width(grid_border_width)
self._options_grid.set_row_spacing(grid_row_spacing)
self._options_grid.set_column_spacing(grid_column_spacing)
self._options_grid.set_row_homogeneous(False)
self._options_grid.set_column_homogeneous(True)
self._options_grid.set_hexpand(True)
self._options_grid.set_vexpand(False)
self._options_label = Gtk.Label()
# Translators: This is the label of a tab in the setup tool.
# Here the user can set up some options which influence the
# behaviour of ibus-table.
self._options_label.set_text(_('Settings'))
self._options_details_grid = Gtk.Grid()
self._options_details_grid.set_visible(True)
self._options_details_grid.set_can_focus(False)
self._options_details_grid.set_border_width(grid_border_width)
self._options_details_grid.set_row_spacing(grid_row_spacing)
self._options_details_grid.set_column_spacing(grid_column_spacing)
self._options_details_grid.set_row_homogeneous(False)
self._options_details_grid.set_column_homogeneous(True)
self._options_details_grid.set_hexpand(True)
self._options_details_grid.set_vexpand(False)
self._options_details_label = Gtk.Label()
# Translators: This is the label of a tab in the setup tool.
# Here the user can set up some more advanced options.
self._options_details_label.set_text(_('Details'))
self._keybindings_vbox = Gtk.Box()
self._keybindings_vbox.set_orientation(Gtk.Orientation.VERTICAL)
self._keybindings_vbox.set_spacing(0)
margin = 10
self._keybindings_vbox.set_margin_start(margin)
self._keybindings_vbox.set_margin_end(margin)
self._keybindings_vbox.set_margin_top(margin)
self._keybindings_vbox.set_margin_bottom(margin)
self._keybindings_label = Gtk.Label()
# Translators: This is the label of a tab in the setup tool.
# Here the user can customize the key bindings to execute
# certain commands of ibus-table. For example which key to use
# to commit, which key to use to move to the next candidate
# etc...
self._keybindings_label.set_text(_('Key bindings'))
self._input_method_menu_grid = Gtk.Grid()
self._input_method_menu_grid.set_visible(True)
self._input_method_menu_grid.set_can_focus(False)
self._input_method_menu_grid.set_border_width(grid_border_width)
self._input_method_menu_grid.set_row_spacing(grid_row_spacing)
self._input_method_menu_grid.set_column_spacing(grid_column_spacing)
self._input_method_menu_grid.set_row_homogeneous(False)
self._input_method_menu_grid.set_column_homogeneous(True)
self._input_method_menu_grid.set_hexpand(True)
self._input_method_menu_grid.set_vexpand(False)
self._input_method_menu_label = Gtk.Label()
self._input_method_menu_label.set_text(_('Menu Items'))
self._notebook.append_page(
self._options_grid,
self._options_label)
self._notebook.append_page(
self._options_details_grid,
self._options_details_label)
self._notebook.append_page(
self._keybindings_vbox,
self._keybindings_label)
self._notebook.append_page(
self._input_method_menu_grid,
self._input_method_menu_label)
_options_grid_row = -1
self._remember_input_mode_label = Gtk.Label()
self._remember_input_mode_label.set_text(
# Translators: A combobox to choose whether the input mode
# (“Direct input” or “Table input”) should be remembered
# or whether ibus-table should always use “Table input” by
# default after a restart.
_('Remember input mode'))
self._remember_input_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose whether the input mode (“Direct input” or “Table
# input”) should be remembered or whether ibus-table
# should always use “Table input” by default after a
# restart.
_('Whether the last used input mode should be remembered '
'or whether ibus-table should start in “Table mode” '
'by default after a restart. There are two input modes: '
'“Table input” means the input method is on. '
'“Direct input” is almost the same as if the '
'input method were off, i.e. not used at all, most '
'characters just get passed to the application. '
'But some conversion between fullwidth and '
'halfwidth may still happen in direct input mode.'))
self._remember_input_mode_label.set_xalign(0)
self._remember_input_mode_combobox = Gtk.ComboBox()
self._remember_input_mode_store = Gtk.ListStore(str, bool)
self._remember_input_mode_store.append(
[_('No'), False])
self._remember_input_mode_store.append(
[_('Yes'), True])
self._remember_input_mode_combobox.set_model(
self._remember_input_mode_store)
renderer_text = Gtk.CellRendererText()
self._remember_input_mode_combobox.pack_start(
renderer_text, True)
self._remember_input_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._remember_input_mode_store):
if self._settings_dict['rememberinputmode']['user'] == item[1]:
self._remember_input_mode_combobox.set_active(index)
self._remember_input_mode_combobox.connect(
"changed",
self._on_remember_input_mode_combobox_changed)
_options_grid_row += 1
self._options_grid.attach(
self._remember_input_mode_label, 0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._remember_input_mode_combobox, 1, _options_grid_row, 1, 1)
self._chinese_mode_label = Gtk.Label()
self._chinese_mode_label.set_text(
# Translators: A combobox to choose the variant of
# Chinese which should be preferred.
_('Chinese mode:'))
self._chinese_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose which variant of Chinese should be preferred.
_('“Simplified Chinese” shows only characters \n'
'used in simplified Chinese. “Traditional Chinese”\n'
'shows only characters used in traditional Chinese.\n'
'“Simplified Chinese before traditional” shows all\n'
'characters but puts the simplified characters higher\n'
'up in the candidate list. “Traditional Chinese before\n'
'simplified” puts the traditional characters higher up\n'
'in the candidate list. “All characters” just shows all\n'
'matches without any particular filtering on traditional\n'
'versus simplified Chinese.'))
self._chinese_mode_label.set_xalign(0)
self._chinese_mode_combobox = Gtk.ComboBox()
self._chinese_mode_store = Gtk.ListStore(str, int)
self._chinese_mode_store.append(
# Translators: This is the setting to use only
# simplified Chinese
[_('Simplified Chinese'), 0])
self._chinese_mode_store.append(
# Translators: This is the setting to use only
# traditional Chinese
[_('Traditional Chinese'), 1])
self._chinese_mode_store.append(
# Translators: This is the setting to use both
# simplified and traditional Chinese but prefer
# simplified Chinese
[_('Simplified Chinese before traditional'), 2])
self._chinese_mode_store.append(
# Translators: This is the setting to use both
# simplified and traditional Chinese but prefer
# traditional Chinese
[_('Traditional Chinese before simplified'), 3])
self._chinese_mode_store.append(
# Translators: This is the setting to use both
# simplified and traditional Chinese in no
# particular order
[_('All Chinese characters'), 4])
self._chinese_mode_combobox.set_model(
self._chinese_mode_store)
renderer_text = Gtk.CellRendererText()
self._chinese_mode_combobox.pack_start(
renderer_text, True)
self._chinese_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._chinese_mode_store):
if self._settings_dict['chinesemode']['user'] == item[1]:
self._chinese_mode_combobox.set_active(index)
self._chinese_mode_combobox.connect(
"changed",
self._on_chinese_mode_combobox_changed)
if self.__is_db_chinese:
_options_grid_row += 1
self._options_grid.attach(
self._chinese_mode_label, 0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._chinese_mode_combobox, 1, _options_grid_row, 1, 1)
self._table_full_width_letter_mode_label = Gtk.Label()
self._table_full_width_letter_mode_label.set_text(
# Translators: A combobox to choose the letter width
# while in “Table input” mode.
_('Table input letter width:'))
self._table_full_width_letter_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose the letter width while in “Table input” mode.
_('Whether to use fullwidth or halfwidth\n'
'letters in table input mode.'))
self._table_full_width_letter_mode_label.set_xalign(0)
self._table_full_width_letter_mode_combobox = Gtk.ComboBox()
self._table_full_width_letter_mode_store = Gtk.ListStore(str, bool)
self._table_full_width_letter_mode_store.append(
# Translators: This is the mode to use half width letters
# while in “Table input” mode.
[_('Half'), False])
self._table_full_width_letter_mode_store.append(
# Translators: This is the mode to use full width letters
# while in “Table input” mode.
[_('Full'), True])
self._table_full_width_letter_mode_combobox.set_model(
self._table_full_width_letter_mode_store)
renderer_text = Gtk.CellRendererText()
self._table_full_width_letter_mode_combobox.pack_start(
renderer_text, True)
self._table_full_width_letter_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._table_full_width_letter_mode_store):
if self._settings_dict['tabdeffullwidthletter']['user'] == item[1]:
self._table_full_width_letter_mode_combobox.set_active(index)
self._table_full_width_letter_mode_combobox.connect(
"changed",
self._on_table_full_width_letter_mode_combobox_changed)
if self.__is_cjk:
_options_grid_row += 1
self._options_grid.attach(
self._table_full_width_letter_mode_label,
0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._table_full_width_letter_mode_combobox,
1, _options_grid_row, 1, 1)
self._table_full_width_punct_mode_label = Gtk.Label()
self._table_full_width_punct_mode_label.set_text(
# Translators: A combobox to choose the punctuation width
# while in “Table input” mode.
_('Table input punctuation width:'))
self._table_full_width_punct_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose the punctuation width while in “Table input” mode.
_('Whether to use fullwidth or halfwidth\n'
'punctuation in table input mode.'))
self._table_full_width_punct_mode_label.set_xalign(0)
self._table_full_width_punct_mode_combobox = Gtk.ComboBox()
self._table_full_width_punct_mode_store = Gtk.ListStore(
str, bool)
self._table_full_width_punct_mode_store.append(
# Translators: This is the mode to use half width punctuation
# while in “Table input” mode.
[_('Half'), False])
self._table_full_width_punct_mode_store.append(
# Translators: This is the mode to use full width punctuation
# while in “Table input” mode.
[_('Full'), True])
self._table_full_width_punct_mode_combobox.set_model(
self._table_full_width_punct_mode_store)
renderer_text = Gtk.CellRendererText()
self._table_full_width_punct_mode_combobox.pack_start(
renderer_text, True)
self._table_full_width_punct_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(
self._table_full_width_punct_mode_store):
if self._settings_dict['tabdeffullwidthpunct']['user'] == item[1]:
self._table_full_width_punct_mode_combobox.set_active(
index)
self._table_full_width_punct_mode_combobox.connect(
"changed",
self._on_table_full_width_punct_mode_combobox_changed)
if self.__is_cjk:
_options_grid_row += 1
self._options_grid.attach(
self._table_full_width_punct_mode_label,
0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._table_full_width_punct_mode_combobox,
1, _options_grid_row, 1, 1)
self._direct_full_width_letter_mode_label = Gtk.Label()
self._direct_full_width_letter_mode_label.set_text(
# Translators: A combobox to choose the letter width
# while in “Direct input” mode.
_('Direct input letter width:'))
self._direct_full_width_letter_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose the letter width while in “Direct input” mode.
_('Whether to use fullwidth or halfwidth\n'
'letters in direct input mode.'))
self._direct_full_width_letter_mode_label.set_xalign(0)
self._direct_full_width_letter_mode_combobox = Gtk.ComboBox()
self._direct_full_width_letter_mode_store = Gtk.ListStore(str, bool)
self._direct_full_width_letter_mode_store.append(
# Translators: This is the mode to use half width letters
# while in “Direct input” mode.
[_('Half'), False])
self._direct_full_width_letter_mode_store.append(
# Translators: This is the mode to use full width letters
# while in “Direct input” mode.
[_('Full'), True])
self._direct_full_width_letter_mode_combobox.set_model(
self._direct_full_width_letter_mode_store)
renderer_text = Gtk.CellRendererText()
self._direct_full_width_letter_mode_combobox.pack_start(
renderer_text, True)
self._direct_full_width_letter_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(
self._direct_full_width_letter_mode_store):
if self._settings_dict['endeffullwidthletter']['user'] == item[1]:
self._direct_full_width_letter_mode_combobox.set_active(index)
self._direct_full_width_letter_mode_combobox.connect(
"changed",
self._on_direct_full_width_letter_mode_combobox_changed)
if self.__is_cjk:
_options_grid_row += 1
self._options_grid.attach(
self._direct_full_width_letter_mode_label,
0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._direct_full_width_letter_mode_combobox,
1, _options_grid_row, 1, 1)
self._direct_full_width_punct_mode_label = Gtk.Label()
self._direct_full_width_punct_mode_label.set_text(
# Translators: A combobox to choose the punctuation width
# while in “Direct input” mode.
_('Direct input punctuation width:'))
self._direct_full_width_punct_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose the punctuation width while in “Direct input” mode.
_('Whether to use fullwidth or halfwidth\n'
'punctuation in direct input mode.'))
self._direct_full_width_punct_mode_label.set_xalign(0)
self._direct_full_width_punct_mode_combobox = Gtk.ComboBox()
self._direct_full_width_punct_mode_store = Gtk.ListStore(
str, bool)
self._direct_full_width_punct_mode_store.append(
# Translators: This is the mode to use half width punctuation
# while in “Direct input” mode.
[_('Half'), False])
self._direct_full_width_punct_mode_store.append(
# Translators: This is the mode to use full width punctuation
# while in “Direct input” mode.
[_('Full'), True])
self._direct_full_width_punct_mode_combobox.set_model(
self._direct_full_width_punct_mode_store)
renderer_text = Gtk.CellRendererText()
self._direct_full_width_punct_mode_combobox.pack_start(
renderer_text, True)
self._direct_full_width_punct_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(
self._direct_full_width_punct_mode_store):
if self._settings_dict['endeffullwidthpunct']['user'] == item[1]:
self._direct_full_width_punct_mode_combobox.set_active(
index)
self._direct_full_width_punct_mode_combobox.connect(
"changed",
self._on_direct_full_width_punct_mode_combobox_changed)
if self.__is_cjk:
_options_grid_row += 1
self._options_grid.attach(
self._direct_full_width_punct_mode_label,
0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._direct_full_width_punct_mode_combobox,
1, _options_grid_row, 1, 1)
self._candidate_list_section_heading_label = Gtk.Label()
self._candidate_list_section_heading_label.set_text(
'<b>' + _('Candidate list') + '</b>')
self._candidate_list_section_heading_label.set_use_markup(True)
self._candidate_list_section_heading_label.set_xalign(0)
_options_grid_row += 1
self._options_grid.attach(
self._candidate_list_section_heading_label,
0, _options_grid_row, 2, 1)
self._always_show_lookup_checkbutton = Gtk.CheckButton(
# Translators: A combobox to choose whether
# a candidate list should be shown or hidden.
# For Chinese input methods one usually wants the
# candidate list to be shown. But for some non-Chinese
# input methods like the Russian “translit”, hiding
# the candidate lists is better.
label=_('Show candidate list'))
self._always_show_lookup_checkbutton.set_tooltip_text(
# Translit: A tooltip for the label of the combobox to
# choose whether a candidate list should be shown or
# hidden.
_('Whether candidate lists should be shown or hidden.\n'
'For Chinese input methods one usually wants the\n'
'candidate lists to be shown. But for some non-Chinese\n'
'input methods like the Russian “translit”, hiding the\n'
'candidate lists is better.'))
self._always_show_lookup_checkbutton.set_hexpand(False)
self._always_show_lookup_checkbutton.set_vexpand(False)
self._always_show_lookup_checkbutton.set_active(
self._settings_dict['alwaysshowlookup']['user'])
self._always_show_lookup_checkbutton.connect(
"clicked", self._on_always_show_lookup_checkbutton)
_options_grid_row += 1
self._options_grid.attach(
self._always_show_lookup_checkbutton, 0, _options_grid_row, 2, 1)
self._lookup_table_orientation_label = Gtk.Label()
self._lookup_table_orientation_label.set_text(
# Translators: A combobox to choose whether the candidate
# window should be drawn horizontally or vertically.
_('Orientation:'))
self._lookup_table_orientation_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose whether the candidate window should be drawn
# horizontally or vertically.
_('Whether the lookup table showing the candidates\n'
'should be vertical or horizontal.'))
self._lookup_table_orientation_label.set_xalign(0)
self._lookup_table_orientation_combobox = Gtk.ComboBox()
self._lookup_table_orientation_store = Gtk.ListStore(str, int)
self._lookup_table_orientation_store.append(
[_('Horizontal'), int(IBus.Orientation.HORIZONTAL)])
self._lookup_table_orientation_store.append(
[_('Vertical'), int(IBus.Orientation.VERTICAL)])
self._lookup_table_orientation_store.append(
[_('System default'), int(IBus.Orientation.SYSTEM)])
self._lookup_table_orientation_combobox.set_model(
self._lookup_table_orientation_store)
renderer_text = Gtk.CellRendererText()
self._lookup_table_orientation_combobox.pack_start(
renderer_text, True)
self._lookup_table_orientation_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._lookup_table_orientation_store):
if (self._settings_dict['lookuptableorientation']['user']
== item[1]):
self._lookup_table_orientation_combobox.set_active(index)
self._lookup_table_orientation_combobox.connect(
"changed",
self._on_lookup_table_orientation_combobox_changed)
_options_grid_row += 1
self._options_grid.attach(
self._lookup_table_orientation_label, 0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._lookup_table_orientation_combobox,
1, _options_grid_row, 1, 1)
self._page_size_label = Gtk.Label()
# Translators: Here one can choose how many suggestion
# candidates to show in one page of the candidate list.
self._page_size_label.set_text(_('Page size:'))
self._page_size_label.set_tooltip_text(
# Translators: A tooltip for the label of the adjustment
# for the number of candidates to show in one page of the
# candidate list.
_('The maximum number of candidates in\n'
'one page of the lookup table. You can switch\n'
'pages in the lookup table using the page up/down\n'
'keys or the arrow up/down keys.'))
self._page_size_label.set_xalign(0)
self._page_size_adjustment = Gtk.SpinButton()
self._page_size_adjustment.set_visible(True)
self._page_size_adjustment.set_can_focus(True)
self._page_size_adjustment.set_increments(1.0, 1.0)
self._page_size_adjustment.set_range(1.0, 10.0)
self._page_size_adjustment.set_value(
self._settings_dict['lookuptablepagesize']['user'])
self._page_size_adjustment.connect(
'value-changed', self._on_page_size_adjustment_value_changed)
_options_grid_row += 1
self._options_grid.attach(
self._page_size_label, 0, _options_grid_row, 1, 1)
self._options_grid.attach(
self._page_size_adjustment, 1, _options_grid_row, 1, 1)
self._keybindings_label = Gtk.Label()
self._keybindings_label.set_text(
'<b>' + _('Current key bindings:') + '</b>')
self._keybindings_label.set_use_markup(True)
self._keybindings_label.set_margin_start(margin)
self._keybindings_label.set_margin_end(margin)
self._keybindings_label.set_margin_top(margin)
self._keybindings_label.set_margin_bottom(margin)
self._keybindings_label.set_hexpand(False)
self._keybindings_label.set_vexpand(False)
self._keybindings_label.set_xalign(0)
self._keybindings_treeview_scroll = Gtk.ScrolledWindow()
self._keybindings_treeview_scroll.set_can_focus(False)
self._keybindings_treeview_scroll.set_hexpand(False)
self._keybindings_treeview_scroll.set_vexpand(True)
#self._keybindings_treeview_scroll.set_shadow_type(in)
self._keybindings_treeview = Gtk.TreeView()
self._keybindings_treeview_model = Gtk.ListStore(str, str)
self._keybindings_treeview.set_model(self._keybindings_treeview_model)
user_keybindings = self._settings_dict['keybindings']['user']
for command in sorted(user_keybindings):
self._keybindings_treeview_model.append(
(command, repr(user_keybindings[command])))
keybindings_treeview_column_0 = Gtk.TreeViewColumn(
# Translators: Column heading of the table listing the
# existing key bindings
_('Command'),
Gtk.CellRendererText(),
text=0)
keybindings_treeview_column_0.set_sort_column_id(0)
self._keybindings_treeview.append_column(keybindings_treeview_column_0)
keybindings_treeview_column_1 = Gtk.TreeViewColumn(
# Translators: Column heading of the table listing the
# existing key bindings
_('Key bindings'),
Gtk.CellRendererText(),
text=1)
keybindings_treeview_column_1.set_sort_column_id(1)
self._keybindings_treeview.append_column(keybindings_treeview_column_1)
self._keybindings_treeview.get_selection().connect(
'changed', self._on_keybindings_treeview_row_selected)
self._keybindings_treeview.connect(
'row-activated', self._on_keybindings_treeview_row_activated)
self._keybindings_treeview_scroll.add(self._keybindings_treeview)
self._keybindings_vbox.pack_start(
self._keybindings_label, False, False, 0)
self._keybindings_vbox.pack_start(
self._keybindings_treeview_scroll, True, True, 0)
self._keybindings_action_area = Gtk.Box()
self._keybindings_action_area.set_orientation(
Gtk.Orientation.HORIZONTAL)
self._keybindings_action_area.set_can_focus(False)
self._keybindings_vbox.add(
self._keybindings_action_area)
self._keybindings_edit_button = Gtk.Button()
self._keybindings_edit_button_label = Gtk.Label()
self._keybindings_edit_button_label.set_text(
_('Edit'))
self._keybindings_edit_button.add(
self._keybindings_edit_button_label)
self._keybindings_edit_button.set_tooltip_text(
# Translators: A tooltip for the button to edit
# the keybindings for the selected command.
_('Edit the key bindings for the selected command'))
self._keybindings_edit_button.set_sensitive(False)
self._keybindings_edit_button.connect(
'clicked', self._on_keybindings_edit_button_clicked)
self._keybindings_default_button = Gtk.Button()
self._keybindings_default_button_label = Gtk.Label()
self._keybindings_default_button_label.set_text(
_('Set to default'))
self._keybindings_default_button.add(
self._keybindings_default_button_label)
self._keybindings_default_button.set_tooltip_text(
# Translators: A tooltip for the button to set
# the default key bindings for the selected command.
_('Set default key bindings for the selected command'))
self._keybindings_default_button.set_sensitive(False)
self._keybindings_default_button.connect(
'clicked', self._on_keybindings_default_button_clicked)
self._keybindings_all_default_button = Gtk.Button()
self._keybindings_all_default_button_label = Gtk.Label()
self._keybindings_all_default_button_label.set_text(
_('Set all to default'))
self._keybindings_all_default_button.add(
self._keybindings_all_default_button_label)
self._keybindings_all_default_button.set_tooltip_text(
# Translators: A tooltip for the button to set the
# key bindings to default for all commands.
_('Set default key bindings for all commands'))
self._keybindings_all_default_button.set_sensitive(True)
self._keybindings_all_default_button.connect(
'clicked', self._on_keybindings_all_default_button_clicked)
self._keybindings_action_area.add(self._keybindings_edit_button)
self._keybindings_action_area.add(self._keybindings_default_button)
self._keybindings_action_area.add(self._keybindings_all_default_button)
self._keybindings_selected_command = ''
self._keybindings_edit_popover_selected_keybinding = ''
self._keybindings_edit_popover_selected_keybinding_index = -1
self._keybindings_edit_popover_listbox = None
self._keybindings_edit_popover = None
self._keybindings_edit_popover_scroll = None
self._keybindings_edit_popover_add_button = None
self._keybindings_edit_popover_remove_button = None
self._keybindings_edit_popover_default_button = None
self._keybindings_edit_popover_up_button = None
self._keybindings_edit_popover_down_button = None
_options_details_grid_row = -1
self._dynamic_adjust_checkbutton = Gtk.CheckButton(
# Translators: A checkbox where one can choose whether the
# order of the candidates is dynamically adjusted according
# to how often the candidates are used.
label=_('Dynamic adjust'))
self._dynamic_adjust_checkbutton.set_tooltip_text(
_('Here you can choose whether the order of the candidates is '
+ 'dynamically adjusted according to how often the candidates '
+ 'are used.'))
self._dynamic_adjust_checkbutton.set_hexpand(False)
self._dynamic_adjust_checkbutton.set_vexpand(False)
self._dynamic_adjust_checkbutton.set_active(
self._settings_dict['dynamicadjust']['user'])
self._dynamic_adjust_checkbutton.connect(
'clicked', self._on_dynamic_adjust_checkbutton)
self._dynamic_adjust_forget_button = Gtk.Button()
self._dynamic_adjust_forget_button_box = Gtk.HBox()
self._dynamic_adjust_forget_button_label = Gtk.Label()
self._dynamic_adjust_forget_button_label.set_text(
_('Delete all learned data'))
self._dynamic_adjust_forget_button_label.set_use_markup(True)
self._dynamic_adjust_forget_button_label.set_max_width_chars(
40)
self._dynamic_adjust_forget_button_label.set_line_wrap(False)
self._dynamic_adjust_forget_button_label.set_ellipsize(
Pango.EllipsizeMode.START)
self._dynamic_adjust_forget_button_box.pack_start(
self._dynamic_adjust_forget_button_label, False, False, 0)
self._dynamic_adjust_forget_button.add(
self._dynamic_adjust_forget_button_box)
self._dynamic_adjust_forget_button.connect(
'clicked', self._on_dynamic_adjust_forget_button)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._dynamic_adjust_checkbutton,
0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._dynamic_adjust_forget_button,
1, _options_details_grid_row, 1, 1)
self._onechar_mode_label = Gtk.Label()
self._onechar_mode_label.set_text(
# Translators: A combobox to choose whether only single
# character candidates should be shown.
_('Compose:'))
self._onechar_mode_label.set_tooltip_text(
# Translators: A tooltip for label of the combobox to
# choose whether only single character candidates should
# be shown.
_('If this is set to “single char”, only single\n'
'character candidates will be shown. If it is\n'
'set to “Phrase” candidates consisting of\n'
'several characters may be shown.'))
self._onechar_mode_label.set_xalign(0)
self._onechar_mode_combobox = Gtk.ComboBox()
self._onechar_mode_store = Gtk.ListStore(str, int)
self._onechar_mode_store.append(
[_('Phrase'), False])
self._onechar_mode_store.append(
[_('Single Char'), True])
self._onechar_mode_combobox.set_model(
self._onechar_mode_store)
renderer_text = Gtk.CellRendererText()
self._onechar_mode_combobox.pack_start(
renderer_text, True)
self._onechar_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._onechar_mode_store):
if self._settings_dict['onechar']['user'] == item[1]:
self._onechar_mode_combobox.set_active(index)
self._onechar_mode_combobox.connect(
"changed", self._on_onechar_mode_combobox_changed)
if self.__is_cjk:
_options_details_grid_row += 1
self._options_details_grid.attach(
self._onechar_mode_label,
0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._onechar_mode_combobox,
1, _options_details_grid_row, 1, 1)
self._autoselect_mode_checkbutton = Gtk.CheckButton(
# Translators: A combobox to choose whether the first
# candidate will be automatically selected during typing.
label=_('Auto select'))
self._autoselect_mode_checkbutton.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose whether the first candidate will be automatically
# select during typing.
_('If set to “Yes”, this does the following 4 things:\n'
'1) When typing “Return”, commit the \n'
' candidate + line-feed\n'
'2) When typing Tab, commit the candidate\n'
'3) When committing using a commit key, commit\n'
' the candidate + " "\n'
'4) If typing the next character matches no candidates,\n'
' commit the first candidate of the previous match.\n'
' (Mostly needed for non-Chinese input methods like\n'
' the Russian “translit”)'))
self._autoselect_mode_checkbutton.set_hexpand(False)
self._autoselect_mode_checkbutton.set_vexpand(False)
self._autoselect_mode_checkbutton.set_active(
self._settings_dict['autoselect']['user'])
self._autoselect_mode_checkbutton.connect(
'clicked', self._on_autoselect_mode_checkbutton)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._autoselect_mode_checkbutton,
0, _options_details_grid_row, 2, 1)
self._autocommit_mode_label = Gtk.Label()
self._autocommit_mode_label.set_text(
# Translators: A combobox to choose whether automatic
# commits go into the preëdit or into the application
_('Autocommit mode:'))
self._autocommit_mode_label.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose whether automatic commits go into the preëdit or
# into the application.
_('Committing with the commit keys or with the mouse\n'
'always commits to the application. This option is about\n'
'“automatic” commits which may happen when\n'
'one just continues typing input without committing\n'
'manually. From time to time, “automatic” commits will\n'
'happen then.\n'
'“Direct” means such “automatic” commits go directly\n'
'into the application, “Normal” means they get committed\n'
'to preedit.'))
self._autocommit_mode_label.set_xalign(0)
self._autocommit_mode_combobox = Gtk.ComboBox()
self._autocommit_mode_store = Gtk.ListStore(str, int)
self._autocommit_mode_store.append(
[_('Normal'), False])
self._autocommit_mode_store.append(
[_('Direct'), True])
self._autocommit_mode_combobox.set_model(
self._autocommit_mode_store)
renderer_text = Gtk.CellRendererText()
self._autocommit_mode_combobox.pack_start(
renderer_text, True)
self._autocommit_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._autocommit_mode_store):
if self._settings_dict['autocommit']['user'] == item[1]:
self._autocommit_mode_combobox.set_active(index)
self._autocommit_mode_combobox.connect(
"changed", self._on_autocommit_mode_combobox_changed)
if self.__user_can_define_phrase and self.__rules:
_options_details_grid_row += 1
self._options_details_grid.attach(
self._autocommit_mode_label,
0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._autocommit_mode_combobox,
1, _options_details_grid_row, 1, 1)
self._commit_invalid_mode_label = Gtk.Label()
self._commit_invalid_mode_label.set_text(
# Translators: A combobox to choose whether only single
# character candidates should be shown.
_('Action when typing invalid character:'))
self._commit_invalid_mode_label.set_tooltip_text(
# Translators: A tooltip for label of the combobox to
# choose whether only single character candidates should
# be shown.
_('Determines what happens when a character which is not '
'in the set of valid input characters for this table '
'is typed: With “commit current candidate”, the currently '
'selected candidate is inserted. With “commit typed keys”, '
'the raw characters typed so far during the candidate '
'selection process will be inserted instead. In all cases, '
'the candidate selection ends when an invalid character is '
'typed and the character in question is inserted immediately '
'after the text that results from the options listed above.'))
self._commit_invalid_mode_label.set_xalign(0)
self._commit_invalid_mode_combobox = Gtk.ComboBox()
self._commit_invalid_mode_store = Gtk.ListStore(str, int)
self._commit_invalid_mode_store.append(
[_('Commit current candidate'), 0])
self._commit_invalid_mode_store.append(
[_('Commit typed keys'), 1])
self._commit_invalid_mode_combobox.set_model(
self._commit_invalid_mode_store)
renderer_text = Gtk.CellRendererText()
self._commit_invalid_mode_combobox.pack_start(
renderer_text, True)
self._commit_invalid_mode_combobox.add_attribute(
renderer_text, "text", 0)
for index, item in enumerate(self._commit_invalid_mode_store):
if self._settings_dict['commitinvalidmode']['user'] == item[1]:
self._commit_invalid_mode_combobox.set_active(index)
self._commit_invalid_mode_combobox.connect(
"changed", self._on_commit_invalid_mode_combobox_changed)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._commit_invalid_mode_label,
0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._commit_invalid_mode_combobox,
1, _options_details_grid_row, 1, 1)
self._autowildcard_mode_checkbutton = Gtk.CheckButton(
# Translators: A combobox to choose whether a wildcard
# should be automatically appended to the input.
label=_('Auto wildcard'))
self._autowildcard_mode_checkbutton.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose whether a wildcard should be automatically
# appended to the input.
_('If yes, a multi wildcard will be automatically\n'
'appended to the end of the input string.'))
self._autowildcard_mode_checkbutton.set_hexpand(False)
self._autowildcard_mode_checkbutton.set_vexpand(False)
self._autowildcard_mode_checkbutton.set_active(
self._settings_dict['autowildcard']['user'])
self._autowildcard_mode_checkbutton.connect(
'clicked', self._on_autowildcard_mode_checkbutton)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._autowildcard_mode_checkbutton,
0, _options_details_grid_row, 2, 1)
self._single_wildcard_char_label = Gtk.Label()
self._single_wildcard_char_label.set_text(
# Translators: This single character is a placeholder
# to match a any single character
_('Single wildcard character:'))
self._single_wildcard_char_label.set_tooltip_text(
# Translators: This is a tooltip for the label of the
# entry where one can choose the wildcard to match a
# single character.
_('The wildcard to match any single character.\n'
'Type RETURN or ENTER to confirm after changing the wildcard.'))
self._single_wildcard_char_label.set_xalign(0)
self._single_wildcard_char_entry = Gtk.Entry()
self._single_wildcard_char_entry.set_max_length(1)
self._single_wildcard_char_entry.set_text(
self._settings_dict['singlewildcardchar']['user'])
self._single_wildcard_char_entry.connect(
'notify::text', self._on_single_wildcard_char_entry)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._single_wildcard_char_label,
0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._single_wildcard_char_entry,
1, _options_details_grid_row, 1, 1)
self._multi_wildcard_char_label = Gtk.Label()
self._multi_wildcard_char_label.set_text(
# Translators: This single character is a placeholder
# to match a any number of characters
_('Multi wildcard character:'))
self._multi_wildcard_char_label.set_tooltip_text(
# Translators: This is a tooltip for the label of the
# entry where one can choose the wildcard to match any
# number of candidates.
_('The wildcard used to match any number of characters.\n'
'Type RETURN or ENTER to confirm after changing the wildcard.'))
self._multi_wildcard_char_label.set_xalign(0)
self._multi_wildcard_char_entry = Gtk.Entry()
self._multi_wildcard_char_entry.set_max_length(1)
self._multi_wildcard_char_entry.set_text(
self._settings_dict['multiwildcardchar']['user'])
self._multi_wildcard_char_entry.connect(
'notify::text', self._on_multi_wildcard_char_entry)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._multi_wildcard_char_label,
0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._multi_wildcard_char_entry,
1, _options_details_grid_row, 1, 1)
self._use_dark_theme_checkbutton = Gtk.CheckButton(
# Translators: A combobox to choose whether
# the color scheme for a dark theme should be used.
label=_('Use dark theme'))
self._use_dark_theme_checkbutton.set_tooltip_text(
# Translators: A tooltip for the label of the combobox to
# choose whether the color scheme for a dark theme should
# be used.
_('If yes, the color scheme for a dark theme will be used.'))
self._use_dark_theme_checkbutton.set_hexpand(False)
self._use_dark_theme_checkbutton.set_vexpand(False)
self._use_dark_theme_checkbutton.set_active(
self._settings_dict['darktheme']['user'])
self._use_dark_theme_checkbutton.connect(
'clicked', self._on_use_dark_theme_checkbutton)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._use_dark_theme_checkbutton,
0, _options_details_grid_row, 2, 1)
self._error_sound_checkbutton = Gtk.CheckButton(
# Translators: A checkbox where one can choose whether a
# sound is played on error
label=_('Play sound file on error'))
self._error_sound_checkbutton.set_tooltip_text(
_('Here you can choose whether a sound file is played '
+ 'if an error occurs. '
+ 'If the simpleaudio module for Python3 is not installed, '
+ 'this option does nothing.'))
self._error_sound_checkbutton.set_hexpand(False)
self._error_sound_checkbutton.set_vexpand(False)
self._error_sound_checkbutton.set_active(
self._settings_dict['errorsound']['user'])
self._error_sound_checkbutton.connect(
'clicked', self._on_error_sound_checkbutton)
self._error_sound_file_button = Gtk.Button()
self._error_sound_file_button_box = Gtk.HBox()
self._error_sound_file_button_label = Gtk.Label()
self._error_sound_file_button_label.set_text(
self._settings_dict['errorsoundfile']['user'])
self._error_sound_file_button_label.set_use_markup(True)
self._error_sound_file_button_label.set_max_width_chars(
40)
self._error_sound_file_button_label.set_line_wrap(False)
self._error_sound_file_button_label.set_ellipsize(
Pango.EllipsizeMode.START)
self._error_sound_file_button_box.pack_start(
self._error_sound_file_button_label, False, False, 0)
self._error_sound_file_button.add(
self._error_sound_file_button_box)
self._error_sound_file_button.connect(
'clicked', self._on_error_sound_file_button)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._error_sound_checkbutton, 0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._error_sound_file_button, 1, _options_details_grid_row, 1, 1)
self._error_sound_object = it_sound.SoundObject(
os.path.expanduser(self._settings_dict['errorsoundfile']['user']),
audio_backend=self._settings_dict['soundbackend']['user'])
self._debug_level_label = Gtk.Label()
self._debug_level_label.set_text(
# Translators: When the debug level is greater than 0,
# debug information may be printed to the log file and
# debug information may also be shown graphically.
_('Debug level:'))
self._debug_level_label.set_tooltip_text(
# Translators: This is a tooltip for the label for the
# adjustment of the debug level.
_('When greater than 0, debug information may be '
'printed to the log file and debug information '
'may also be shown graphically.'))
self._debug_level_label.set_xalign(0)
self._debug_level_adjustment = Gtk.SpinButton()
self._debug_level_adjustment.set_visible(True)
self._debug_level_adjustment.set_can_focus(True)
self._debug_level_adjustment.set_increments(1.0, 1.0)
self._debug_level_adjustment.set_range(0.0, 255.0)
self._debug_level_adjustment.set_value(
self._settings_dict['debuglevel']['user'])
self._debug_level_adjustment.connect(
'value-changed',
self._on_debug_level_adjustment_value_changed)
_options_details_grid_row += 1
self._options_details_grid.attach(
self._debug_level_label, 0, _options_details_grid_row, 1, 1)
self._options_details_grid.attach(
self._debug_level_adjustment, 1, _options_details_grid_row, 1, 1)
# name, label, check button
self._input_method_menu_tuple: Tuple[
List[Union[str, Optional[Gtk.CheckButton]]], ...] = (
(["chinese_mode", _("Chinese mode"), None],
["letter_width", _("Letter width"), None],
["punctuation_width", _("Punctuation width"), None],
["pinyin_mode", _("Pinyin mode"), None],
["suggestion_mode", _("Suggestion mode"), None],
["onechar_mode", _("Onechar mode"), None],
["autocommit_mode", _("Autocommit mode"), None]))
_input_method_menu_grid_row = -1
for item in self._input_method_menu_tuple:
name, label, button = item
_input_method_menu_grid_row += 1
button = Gtk.CheckButton(label=label)
button.set_hexpand(False)
button.set_vexpand(False)
if name in self._settings_dict['inputmethodmenu']['user']:
button.set_active(True)
else:
button.set_active(False)
button.connect(
'clicked', self._on_input_method_menu_checkbutton)
self._input_method_menu_grid.attach(
button, 0, _input_method_menu_grid_row, 1, 1)
item[2] = button
self.show_all() # pylint: disable=no-member
self._notebook.set_current_page(0) # Has to be after show_all()
self._gsettings.connect('changed', self._on_gsettings_value_changed)
def _fill_settings_dict(self) -> None:
'''Fill a dictionary with the default and user settings for all
settings keys.
The default settings start with the defaults from the
gsettings schema. Some of these generic default values may be
overridden by more specific default settings coming from the
specific database of this table input method. After this
possible modification from the database we have the final default
settings for this specific table input method.
The user settings start with a copy of these final default settings,
then they are possibly modified by user gsettings.
Keeping a copy of the default settings in the settings dictionary
makes it easy to revert some or all settings to the defaults.
'''
self._settings_dict = {}
default_single_wildcard_char = it_util.variant_to_value(
self._gsettings.get_default_value('singlewildcardchar'))
if self.tabsqlitedb.ime_properties.get('single_wildcard_char'):
default_single_wildcard_char = self.tabsqlitedb.ime_properties.get(
'single_wildcard_char')
user_single_wildcard_char = it_util.variant_to_value(
self._gsettings.get_user_value('singlewildcardchar'))
if user_single_wildcard_char is None:
user_single_wildcard_char = default_single_wildcard_char
self._settings_dict['singlewildcardchar'] = {
'default': default_single_wildcard_char,
'user': user_single_wildcard_char,
'set_function': self.set_single_wildcard_char}
default_multi_wildcard_char = it_util.variant_to_value(
self._gsettings.get_default_value('multiwildcardchar'))
if self.tabsqlitedb.ime_properties.get('multi_wildcard_char'):
default_multi_wildcard_char = self.tabsqlitedb.ime_properties.get(
'multi_wildcard_char')
user_multi_wildcard_char = it_util.variant_to_value(
self._gsettings.get_user_value('multiwildcardchar'))
if user_multi_wildcard_char is None:
user_multi_wildcard_char = default_multi_wildcard_char
self._settings_dict['multiwildcardchar'] = {
'default': default_multi_wildcard_char,
'user': user_multi_wildcard_char,
'set_function': self.set_multi_wildcard_char}
default_keybindings = it_util.get_default_keybindings(
self._gsettings, self.tabsqlitedb)
# copy the updated default keybindings, i.e. the default
# keybindings for this table, into the user keybindings:
user_keybindings = copy.deepcopy(default_keybindings)
user_keybindings_gsettings = it_util.variant_to_value(
self._gsettings.get_user_value('keybindings'))
if not user_keybindings_gsettings:
user_keybindings_gsettings = {}
it_util.dict_update_existing_keys(
user_keybindings, user_keybindings_gsettings)
self._settings_dict['keybindings'] = {
'default': default_keybindings,
'user': user_keybindings,
'set_function': self.set_keybindings}
default_always_show_lookup = it_util.variant_to_value(
self._gsettings.get_default_value('alwaysshowlookup'))
if self.tabsqlitedb.ime_properties.get('always_show_lookup'):
default_always_show_lookup = (
self.tabsqlitedb.ime_properties.get(
'always_show_lookup').lower() == 'true')
user_always_show_lookup = it_util.variant_to_value(
self._gsettings.get_user_value('alwaysshowlookup'))
if user_always_show_lookup is None:
user_always_show_lookup = default_always_show_lookup
self._settings_dict['alwaysshowlookup'] = {
'default': default_always_show_lookup,
'user': user_always_show_lookup,
'set_function': self.set_always_show_lookup}
default_page_size = it_util.variant_to_value(
self._gsettings.get_default_value('lookuptablepagesize'))
for index in range(1, 10):
if not default_keybindings[f'commit_candidate_{index + 1}']:
default_page_size = min(index, default_page_size)
break
user_page_size = it_util.variant_to_value(
self._gsettings.get_user_value('lookuptablepagesize'))
if user_page_size is None:
user_page_size = default_page_size
self._settings_dict['lookuptablepagesize'] = {
'default': int(default_page_size),
'user': int(user_page_size),
'set_function': self.set_page_size}
default_lookup_table_orientation = it_util.variant_to_value(
self._gsettings.get_default_value('lookuptableorientation'))
default_lookup_table_orientation = self.tabsqlitedb.get_orientation()
user_lookup_table_orientation = it_util.variant_to_value(
self._gsettings.get_user_value('lookuptableorientation'))
if user_lookup_table_orientation is None:
user_lookup_table_orientation = default_lookup_table_orientation
self._settings_dict['lookuptableorientation'] = {
'default': default_lookup_table_orientation,
'user': user_lookup_table_orientation,
'set_function': self.set_lookup_table_orientation}
default_chinese_mode = it_util.variant_to_value(
self._gsettings.get_default_value('chinesemode'))
default_chinese_mode = it_util.get_default_chinese_mode(
self.tabsqlitedb)
user_chinese_mode = it_util.variant_to_value(
self._gsettings.get_user_value('chinesemode'))
if user_chinese_mode is None:
user_chinese_mode = default_chinese_mode
self._settings_dict['chinesemode'] = {
'default': default_chinese_mode,
'user': user_chinese_mode,
'set_function': self.set_chinese_mode}
default_input_mode = it_util.variant_to_value(
self._gsettings.get_default_value('inputmode'))
user_input_mode = it_util.variant_to_value(
self._gsettings.get_value('inputmode'))
self._settings_dict['inputmode'] = {
'default': default_input_mode,
'user': user_input_mode,
'set_function': self.set_input_mode}
default_remember_input_mode = it_util.variant_to_value(
self._gsettings.get_default_value('rememberinputmode'))
user_remember_input_mode = it_util.variant_to_value(
self._gsettings.get_value('rememberinputmode'))
self._settings_dict['rememberinputmode'] = {
'default': default_remember_input_mode,
'user': user_remember_input_mode,
'set_function': self.set_remember_input_mode}
default_dark_theme = it_util.variant_to_value(
self._gsettings.get_default_value('darktheme'))
user_dark_theme = it_util.variant_to_value(
self._gsettings.get_value('darktheme'))
self._settings_dict['darktheme'] = {
'default': default_dark_theme,
'user': user_dark_theme,
'set_function': self.set_dark_theme}
default_dynamic_adjust = it_util.variant_to_value(
self._gsettings.get_default_value('dynamicadjust'))
if self.tabsqlitedb.ime_properties.get('dynamic_adjust'):
default_dynamic_adjust = (
self.tabsqlitedb.ime_properties.get(
'dynamic_adjust').lower() == 'true')
user_dynamic_adjust = it_util.variant_to_value(
self._gsettings.get_user_value('dynamicadjust'))
if user_dynamic_adjust is None:
user_dynamic_adjust = default_dynamic_adjust
self._settings_dict['dynamicadjust'] = {
'default': default_dynamic_adjust,
'user': user_dynamic_adjust,
'set_function': self.set_dynamic_adjust}
default_error_sound = it_util.variant_to_value(
self._gsettings.get_default_value('errorsound'))
user_error_sound = it_util.variant_to_value(
self._gsettings.get_value('errorsound'))
self._settings_dict['errorsound'] = {
'default': default_error_sound,
'user': user_error_sound,
'set_function': self.set_error_sound}
default_error_sound_file = it_util.variant_to_value(
self._gsettings.get_default_value('errorsoundfile'))
user_error_sound_file = it_util.variant_to_value(
self._gsettings.get_value('errorsoundfile'))
self._settings_dict['errorsoundfile'] = {
'default': default_error_sound_file,
'user': user_error_sound_file,
'set_function': self.set_error_sound_file}
default_sound_backend = it_util.variant_to_value(
self._gsettings.get_default_value('soundbackend'))
user_sound_backend = it_util.variant_to_value(
self._gsettings.get_value('soundbackend'))
self._settings_dict['soundbackend'] = {
'default': default_sound_backend,
'user': user_sound_backend,
'set_function': self.set_sound_backend}
default_debug_level = it_util.variant_to_value(
self._gsettings.get_default_value('debuglevel'))
user_debug_level = it_util.variant_to_value(
self._gsettings.get_value('debuglevel'))
self._settings_dict['debuglevel'] = {
'default': default_debug_level,
'user': user_debug_level,
'set_function': self.set_debug_level}
default_table_full_width_letter_mode = it_util.variant_to_value(
self._gsettings.get_default_value('tabdeffullwidthletter'))
if self.tabsqlitedb.ime_properties.get('def_full_width_letter'):
default_table_full_width_letter_mode = (
self.tabsqlitedb.ime_properties.get(
'def_full_width_letter').lower() == 'true')
user_table_full_width_letter_mode = it_util.variant_to_value(
self._gsettings.get_user_value('tabdeffullwidthletter'))
if user_table_full_width_letter_mode is None:
user_table_full_width_letter_mode = (
default_table_full_width_letter_mode)
self._settings_dict['tabdeffullwidthletter'] = {
'default': default_table_full_width_letter_mode,
'user': user_table_full_width_letter_mode,
'set_function': self.set_table_full_width_letter_mode}
default_table_full_width_punct_mode = (
it_util.variant_to_value(
self._gsettings.get_default_value('tabdeffullwidthpunct')))
if self.tabsqlitedb.ime_properties.get('def_full_width_punct'):
default_table_full_width_punct_mode = (
self.tabsqlitedb.ime_properties.get(
'def_full_width_punct').lower() == 'true')
user_table_full_width_punct_mode = it_util.variant_to_value(
self._gsettings.get_user_value('tabdeffullwidthpunct'))
if user_table_full_width_punct_mode is None:
user_table_full_width_punct_mode = (
default_table_full_width_punct_mode)
self._settings_dict['tabdeffullwidthpunct'] = {
'default': default_table_full_width_punct_mode,
'user': user_table_full_width_punct_mode,
'set_function': self.set_table_full_width_punct_mode}
default_direct_full_width_letter_mode = it_util.variant_to_value(
self._gsettings.get_default_value('endeffullwidthletter'))
user_direct_full_width_letter_mode = it_util.variant_to_value(
self._gsettings.get_value('endeffullwidthletter'))
self._settings_dict['endeffullwidthletter'] = {
'default': default_direct_full_width_letter_mode,
'user': user_direct_full_width_letter_mode,
'set_function': self.set_direct_full_width_letter_mode}
default_direct_full_width_punct_mode = it_util.variant_to_value(
self._gsettings.get_default_value('endeffullwidthpunct'))
user_direct_full_width_punct_mode = it_util.variant_to_value(
self._gsettings.get_value('endeffullwidthpunct'))
self._settings_dict['endeffullwidthpunct'] = {
'default': default_direct_full_width_punct_mode,
'user': user_direct_full_width_punct_mode,
'set_function': self.set_direct_full_width_punct_mode}
default_onechar_mode = it_util.variant_to_value(
self._gsettings.get_default_value('onechar'))
user_onechar_mode = it_util.variant_to_value(
self._gsettings.get_value('onechar'))
self._settings_dict['onechar'] = {
'default': default_onechar_mode,
'user': user_onechar_mode,
'set_function': self.set_onechar_mode}
default_autoselect_mode = it_util.variant_to_value(
self._gsettings.get_default_value('autoselect'))
if self.tabsqlitedb.ime_properties.get('auto_select'):
default_autoselect_mode = (
self.tabsqlitedb.ime_properties.get(
'auto_select').lower() == 'true')
user_autoselect_mode = it_util.variant_to_value(
self._gsettings.get_user_value('autoselect'))
if user_autoselect_mode is None:
user_autoselect_mode = default_autoselect_mode
self._settings_dict['autoselect'] = {
'default': default_autoselect_mode,
'user': user_autoselect_mode,
'set_function': self.set_autoselect_mode}
default_autocommit_mode = it_util.variant_to_value(
self._gsettings.get_default_value('autocommit'))
if self.tabsqlitedb.ime_properties.get('auto_commit'):
default_autocommit_mode = (
self.tabsqlitedb.ime_properties.get(
'auto_commit').lower() == 'true')
user_autocommit_mode = it_util.variant_to_value(
self._gsettings.get_user_value('autocommit'))
if user_autocommit_mode is None:
user_autocommit_mode = default_autocommit_mode
self._settings_dict['autocommit'] = {
'default': default_autocommit_mode,
'user': user_autocommit_mode,
'set_function': self.set_autocommit_mode}
default_commit_invalid_mode = it_util.variant_to_value(
self._gsettings.get_default_value('commitinvalidmode'))
user_commit_invalid_mode = it_util.variant_to_value(
self._gsettings.get_value('commitinvalidmode'))
self._settings_dict['commitinvalidmode'] = {
'default': default_commit_invalid_mode,
'user': user_commit_invalid_mode,
'set_function': self.set_commit_invalid_mode}
default_autowildcard_mode = it_util.variant_to_value(
self._gsettings.get_default_value('autowildcard'))
if self.tabsqlitedb.ime_properties.get('auto_wildcard'):
default_autowildcard_mode = (
self.tabsqlitedb.ime_properties.get(
'auto_wildcard').lower() == 'true')
user_autowildcard_mode = it_util.variant_to_value(
self._gsettings.get_user_value('autowildcard'))
if user_autowildcard_mode is None:
user_autowildcard_mode = default_autowildcard_mode
self._settings_dict['autowildcard'] = {
'default': default_autowildcard_mode,
'user': user_autowildcard_mode,
'set_function': self.set_autowildcard_mode}
default_input_method_menu = it_util.variant_to_value(
self._gsettings.get_default_value('inputmethodmenu'))
user_input_method_menu = it_util.variant_to_value(
self._gsettings.get_value('inputmethodmenu'))
if user_input_method_menu is None:
user_input_method_menu = default_input_method_menu
self._settings_dict['inputmethodmenu'] = {
'default': default_input_method_menu,
'user': user_input_method_menu,
'set_function': self.set_input_method_menu}
@staticmethod
def run_message_dialog(
message: str,
message_type: Gtk.MessageType = Gtk.MessageType.INFO) -> None:
'''Run a dialog to show an error or warning message'''
dialog = Gtk.MessageDialog(
flags=Gtk.DialogFlags.MODAL,
message_type=message_type,
buttons=Gtk.ButtonsType.OK,
message_format=message)
dialog.run()
dialog.destroy()
def _run_are_you_sure_dialog(self, message: str) -> Gtk.ResponseType:
'''
Run a dialog to show a “Are you sure?” message.
Returns Gtk.ResponseType.OK or Gtk.ResponseType.CANCEL (an enum)
'''
confirm_question = Gtk.Dialog(
title=_('Are you sure?'),
parent=self)
confirm_question.add_button(_('_Cancel'), Gtk.ResponseType.CANCEL)
confirm_question.add_button(_('_OK'), Gtk.ResponseType.OK)
box = confirm_question.get_content_area()
label = Gtk.Label()
label.set_text(
'<span size="large" color="#ff0000"><b>'
+ html.escape(message)
+ '</b></span>')
label.set_use_markup(True)
label.set_max_width_chars(40)
label.set_line_wrap(True)
label.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR)
label.set_xalign(0)
margin = 10
label.set_margin_start(margin)
label.set_margin_end(margin)
label.set_margin_top(margin)
label.set_margin_bottom(margin)
box.add(label)
confirm_question.show_all()
response = confirm_question.run()
confirm_question.destroy()
while Gtk.events_pending():
Gtk.main_iteration()
return response
def check_instance(self) -> bool:
'''
Check whether another instance of the setup tool is running already
'''
if (dbus.SessionBus().request_name("org.ibus.table")
!= dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER):
self.__class__.run_message_dialog(
_("Another instance of this app is already running."),
Gtk.MessageType.ERROR)
sys.exit(1)
else:
return False
@staticmethod
def _on_delete_event(*_args: Any) -> None:
'''The window has been deleted, probably by the window manager.'''
LOGGER.info('Window deleted by the window manager.')
if GLIB_MAIN_LOOP is not None:
GLIB_MAIN_LOOP.quit()
else:
raise RuntimeError("GLIB_MAIN_LOOP not initialized!")
@staticmethod
def _on_destroy_event(*_args: Any) -> None:
'''The window has been destroyed.'''
LOGGER.info('Window destroyed.')
if GLIB_MAIN_LOOP is not None:
GLIB_MAIN_LOOP.quit()
else:
raise RuntimeError("GLIB_MAIN_LOOP not initialized!")
@staticmethod
def _on_close_clicked(_button: Gtk.Button) -> None:
'''The button to close the dialog has been clicked.'''
LOGGER.info('Close button clicked.')
if GLIB_MAIN_LOOP is not None:
GLIB_MAIN_LOOP.quit()
else:
raise RuntimeError("GLIB_MAIN_LOOP not initialized!")
def _on_gsettings_value_changed(
self, _settings: Gio.Settings, key: str) -> None:
'''
Called when a value in the settings has been changed.
:param settings: The settings object
:type settings: Gio.Settings object
:param key: The key of the setting which has changed
:type key: String
'''
value = it_util.variant_to_value(self._gsettings.get_value(key))
LOGGER.info('Settings changed: key=%s value=%s\n', key, value)
if key in self._settings_dict:
self._settings_dict[key]['set_function'](value,
update_gsettings=False)
return
LOGGER.error('Unknown key\n')
return
@staticmethod
def _on_about_button_clicked(_button: Gtk.Button) -> None:
'''
The “About” button has been clicked
:param _button: The “About” button
'''
it_util.ItAboutDialog()
def _on_restore_all_defaults_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Restore all default settings
'''
self._restore_all_defaults_button.set_sensitive(False)
response = self._run_are_you_sure_dialog(
# Translators: This is the text in the centre of a small
# dialog window, trying to confirm whether the user is
# really sure to restore all default settings.
_('Do you really want to restore all default settings?'))
if response == Gtk.ResponseType.OK:
LOGGER.info('Restoring all defaults.')
for _key, value in self._settings_dict.items():
value['set_function'](value['default'], update_gsettings=True)
# Call it again with update_gsettings=False to make
# sure the active state of checkbuttons etc. is
# updated immediately:
value['set_function'](value['default'], update_gsettings=False)
else:
LOGGER.info('Restore all defaults cancelled.')
self._restore_all_defaults_button.set_sensitive(True)
def _on_single_wildcard_char_entry(
self, widget: Gtk.Entry, _property_spec: Any) -> None:
'''
The character to be used as a single wildcard has been changed.
'''
self.set_single_wildcard_char(
widget.get_text(), update_gsettings=True)
def _on_multi_wildcard_char_entry(
self, widget: Gtk.Entry, _property_spec: Any) -> None:
'''
The character to be used as a multi wildcard has been changed.
'''
self.set_multi_wildcard_char(
widget.get_text(), update_gsettings=True)
def _on_page_size_adjustment_value_changed(
self, _widget: Gtk.SpinButton) -> None:
'''
The page size of the lookup table has been changed.
'''
self.set_page_size(
self._page_size_adjustment.get_value(), update_gsettings=True)
def _on_lookup_table_orientation_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the lookup table orientation has been requested
with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
orientation = model[tree_iter][1]
self.set_lookup_table_orientation(
orientation, update_gsettings=True)
def _on_remember_input_mode_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the remember input mode has been requested
with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
remember_input_mode = model[tree_iter][1]
self.set_remember_input_mode(
remember_input_mode, update_gsettings=True)
def _on_chinese_mode_combobox_changed(self, widget: Gtk.ComboBox) -> None:
'''
A change of the Chinese mode has been requested
with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
chinese_mode = model[tree_iter][1]
self.set_chinese_mode(
chinese_mode, update_gsettings=True)
def _on_onechar_mode_combobox_changed(self, widget: Gtk.ComboBox) -> None:
'''
A change of the onechar mode has been requested
with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
mode = model[tree_iter][1]
self.set_onechar_mode(
mode, update_gsettings=True)
def _on_autoselect_mode_checkbutton(self, widget: Gtk.CheckButton) -> None:
'''The checkbutton for autoselect mode has been clicked'''
self.set_autoselect_mode(widget.get_active(), update_gsettings=True)
def _on_autocommit_mode_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the autocommit mode has been requested
with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
mode = model[tree_iter][1]
self.set_autocommit_mode(
mode, update_gsettings=True)
def _on_commit_invalid_mode_combobox_changed(self, widget: Gtk.ComboBox) -> None:
'''
A change of the commit invalid mode has been requested
with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
commit_invalid_mode = model[tree_iter][1]
self.set_commit_invalid_mode(
commit_invalid_mode, update_gsettings=True)
def _on_autowildcard_mode_checkbutton(
self, widget: Gtk.CheckButton) -> None:
'''The checkbutton for autocommit mode has been clicked'''
self.set_autowildcard_mode(widget.get_active(), update_gsettings=True)
def _on_table_full_width_letter_mode_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the letter width when in “Table input” mode has been
requested with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
mode = model[tree_iter][1]
self.set_table_full_width_letter_mode(
mode, update_gsettings=True)
def _on_table_full_width_punct_mode_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the letter width when in “Table input” mode has been
requested with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
mode = model[tree_iter][1]
self.set_table_full_width_punct_mode(
mode, update_gsettings=True)
def _on_direct_full_width_letter_mode_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the letter width when in “Direct input” mode has been
requested with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
mode = model[tree_iter][1]
self.set_direct_full_width_letter_mode(
mode, update_gsettings=True)
def _on_direct_full_width_punct_mode_combobox_changed(
self, widget: Gtk.ComboBox) -> None:
'''
A change of the letter width when in “Direct input” mode has been
requested with the combobox
'''
tree_iter = widget.get_active_iter()
if tree_iter is not None:
model = widget.get_model()
mode = model[tree_iter][1]
self.set_direct_full_width_punct_mode(
mode, update_gsettings=True)
def _on_always_show_lookup_checkbutton(
self, widget: Gtk.CheckButton) -> None:
'''The checkbutton for always show lookup has been clicked'''
self.set_always_show_lookup(widget.get_active(), update_gsettings=True)
def _on_use_dark_theme_checkbutton(self, widget: Gtk.CheckButton) -> None:
'''The checkbutton for the dark theme has been clicked'''
self.set_dark_theme(widget.get_active(), update_gsettings=True)
def _on_dynamic_adjust_checkbutton(self, widget: Gtk.CheckButton) -> None:
'''
The checkbutton whether to dynamically adjust the candidates
:param widget: The check button clicked
'''
self.set_dynamic_adjust(widget.get_active(), update_gsettings=True)
def _on_dynamic_adjust_forget_button(
self, _widget: Gtk.Button) -> None:
'''
The button to select forget how often candidates were used
'''
self._dynamic_adjust_forget_button.set_sensitive(False)
response = self._run_are_you_sure_dialog(
# Translators: This is the text in the centre of a small
# dialog window, trying to confirm whether the user is
# really sure to to delete all the data
# ibus-table has learned from typing and selecting candidates.
# Deleting this learned data cannot be reversed. So
# the user should be really sure he really wants to do that.
_('Do you really want to delete all '
+ 'data learned from typing and selecting candidates?'))
if response == Gtk.ResponseType.OK:
self.tabsqlitedb.remove_all_phrases_from_user_db()
self._dynamic_adjust_forget_button.set_sensitive(True)
def _on_error_sound_checkbutton(self, widget: Gtk.CheckButton) -> None:
'''
The checkbutton whether to play a sound file on error.
:param widget: The check button clicked
'''
self.set_error_sound(widget.get_active(), update_gsettings=True)
def _on_error_sound_file_button(
self, _widget: Gtk.Button) -> None:
'''
The button to select the .wav sound file to be played on error.
'''
self._error_sound_file_button.set_sensitive(False)
filename = ''
chooser = Gtk.FileChooserDialog(
title=_('Select .wav sound file:'),
parent=self,
action=Gtk.FileChooserAction.OPEN)
chooser.add_button(_('_Cancel'), Gtk.ResponseType.CANCEL)
chooser.add_button(_('_OK'), Gtk.ResponseType.OK)
chooser.set_current_folder(os.path.dirname(
self._settings_dict['errorsoundfile']['user']))
response = chooser.run()
if response == Gtk.ResponseType.OK:
filename = chooser.get_filename()
chooser.destroy()
while Gtk.events_pending():
Gtk.main_iteration()
if filename:
self._error_sound_file_button_label.set_text(
filename)
self.set_error_sound_file(
filename, update_gsettings=True)
self._error_sound_file_button.set_sensitive(True)
def _on_debug_level_adjustment_value_changed(
self, _widget: Gtk.SpinButton) -> None:
'''The value for the debug level has been changed.'''
self.set_debug_level(
self._debug_level_adjustment.get_value(),
update_gsettings=True)
def _on_keybindings_treeview_row_activated(
self,
_treeview: Gtk.TreeView,
treepath: Gtk.TreePath,
_treeviewcolumn: Gtk.TreeViewColumn) -> None:
'''
A row in the treeview listing the key bindings has been activated.
:param treeview: The treeview listing the key bindings
:param treepath: The path to the activated row
:param treeviewcolumn: A column in the treeview listing the
key bindings
'''
model = self._keybindings_treeview_model
iterator = model.get_iter(treepath)
command = model[iterator][0]
if command != self._keybindings_selected_command:
# This should not happen, if a row is activated it should
# already be selected,
# i.e. on_keybindings_treeview_row_selected() should have
# been called already and this should have set
# self._keybindings_selected_command
LOGGER.error(
'Unexpected error, command = "%s" '
'self._keybindings_selected_command = "%s"\n',
command,
self._keybindings_selected_command)
return
self._create_and_show_keybindings_edit_popover()
def _on_keybindings_treeview_row_selected(
self, selection: Gtk.TreeSelection) -> None:
'''
A row in the treeview listing the key bindings has been selected.
'''
(model, iterator) = selection.get_selected()
if iterator:
self._keybindings_selected_command = model[iterator][0]
self._keybindings_default_button.set_sensitive(True)
self._keybindings_edit_button.set_sensitive(True)
else:
# all rows have been unselected
self._keybindings_selected_command = ''
self._keybindings_default_button.set_sensitive(False)
self._keybindings_edit_button.set_sensitive(False)
def _on_keybindings_edit_listbox_row_selected(
self, _listbox: Gtk.ListBox, listbox_row: Gtk.ListBoxRow) -> None:
'''
Signal handler for selecting one of the key bindings
for a certain command
:param _listbox: The list box used to select a key binding
:type _listbox: Gtk.ListBox object
:param listbox_row: A row containing a key binding
:type listbox_row: Gtk.ListBoxRow object
'''
if listbox_row:
keybinding = listbox_row.get_child().get_text()
index = listbox_row.get_index()
user_keybindings = self._settings_dict['keybindings']['user']
command = self._keybindings_selected_command
if (keybinding and command
and keybinding in user_keybindings[command]):
self._keybindings_edit_popover_selected_keybinding = keybinding
self._keybindings_edit_popover_selected_keybinding_index = (
index)
if self._keybindings_edit_popover_remove_button:
self._keybindings_edit_popover_remove_button.set_sensitive(
True)
if self._keybindings_edit_popover_up_button:
self._keybindings_edit_popover_up_button.set_sensitive(
index > 0)
if self._keybindings_edit_popover_down_button:
self._keybindings_edit_popover_down_button.set_sensitive(
index < len(user_keybindings[command]) - 1)
return
# all rows have been unselected
self._keybindings_edit_popover_selected_keybinding = ''
self._keybindings_edit_popover_selected_keybinding_index = -1
if self._keybindings_edit_popover_remove_button:
self._keybindings_edit_popover_remove_button.set_sensitive(False)
if self._keybindings_edit_popover_up_button:
self._keybindings_edit_popover_up_button.set_sensitive(False)
if self._keybindings_edit_popover_down_button:
self._keybindings_edit_popover_down_button.set_sensitive(False)
def _on_keybindings_edit_popover_add_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “Add” button to add
a key binding has been clicked.
'''
key_input_dialog = it_util.ItKeyInputDialog(parent=self)
response = key_input_dialog.run()
key_input_dialog.destroy()
if response == Gtk.ResponseType.OK:
keyval, state = key_input_dialog.e
key = it_util.KeyEvent(keyval, 0, state)
keybinding = it_util.keyevent_to_keybinding(key)
command = self._keybindings_selected_command
user_keybindings = self._settings_dict['keybindings']['user']
if keybinding not in user_keybindings[command]:
user_keybindings[command].append(keybinding)
self._fill_keybindings_edit_popover_listbox()
self.set_keybindings(user_keybindings)
def _on_keybindings_edit_popover_remove_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “Remove” button to remove
a key binding has been clicked.
'''
keybinding = self._keybindings_edit_popover_selected_keybinding
command = self._keybindings_selected_command
user_keybindings = self._settings_dict['keybindings']['user']
if (keybinding and command
and keybinding in user_keybindings[command]):
user_keybindings[command].remove(keybinding)
self._fill_keybindings_edit_popover_listbox()
self.set_keybindings(user_keybindings)
def _on_keybindings_edit_popover_up_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “up” button to move
a key binding up has been clicked.
'''
index = self._keybindings_edit_popover_selected_keybinding_index
command = self._keybindings_selected_command
keybinding = self._keybindings_edit_popover_selected_keybinding
user_keybindings = self._settings_dict['keybindings']['user']
if not 0 < index < len(user_keybindings[command]):
# This should not happen, one should not be able
# to click the up button in this case, just return
return
user_keybindings[command] = (
user_keybindings[command][:index - 1]
+ [user_keybindings[command][index]]
+ [user_keybindings[command][index - 1]]
+ user_keybindings[command][index + 1:])
self.set_keybindings(user_keybindings, update_gsettings=True)
self._fill_keybindings_edit_popover_listbox()
self._keybindings_edit_popover_selected_keybinding_index = index - 1
self._keybindings_edit_popover_selected_keybinding = keybinding
if self._keybindings_edit_popover_listbox:
self._keybindings_edit_popover_listbox.select_row(
self._keybindings_edit_popover_listbox.get_row_at_index(
index - 1))
def _on_keybindings_edit_popover_down_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “down” button to move
a key binding down has been clicked.
'''
index = self._keybindings_edit_popover_selected_keybinding_index
command = self._keybindings_selected_command
keybinding = self._keybindings_edit_popover_selected_keybinding
user_keybindings = self._settings_dict['keybindings']['user']
if not 0 <= index < len(user_keybindings[command]) - 1:
# This should not happen, one should not be able
# to click the up button in this case, just return
return
user_keybindings[command] = (
user_keybindings[command][:index]
+ [user_keybindings[command][index + 1]]
+ [user_keybindings[command][index]]
+ user_keybindings[command][index + 2:])
self.set_keybindings(user_keybindings, update_gsettings=True)
self._fill_keybindings_edit_popover_listbox()
self._keybindings_edit_popover_selected_keybinding_index = index + 1
self._keybindings_edit_popover_selected_keybinding = keybinding
if self._keybindings_edit_popover_listbox:
self._keybindings_edit_popover_listbox.select_row(
self._keybindings_edit_popover_listbox.get_row_at_index(
index + 1))
def _on_keybindings_edit_popover_default_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “Default” button to set
the keybindings to the default has been clicked.
'''
default_keybindings = self._settings_dict['keybindings']['default']
user_keybindings = self._settings_dict['keybindings']['user']
command = self._keybindings_selected_command
if command and command in default_keybindings:
user_keybindings[command] = default_keybindings[command].copy()
self._fill_keybindings_edit_popover_listbox()
self.set_keybindings(user_keybindings)
def _fill_keybindings_edit_popover_listbox(self) -> None:
'''
Fill the edit listbox to with the key bindings of the currently
selected command
'''
if self._keybindings_edit_popover_scroll is None:
LOGGER.debug('self._keybindings_edit_popover_scroll is None')
return
for child in self._keybindings_edit_popover_scroll.get_children():
self._keybindings_edit_popover_scroll.remove(child)
self._keybindings_edit_popover_listbox = Gtk.ListBox()
self._keybindings_edit_popover_scroll.add(
self._keybindings_edit_popover_listbox)
self._keybindings_edit_popover_selected_keybinding = ''
self._keybindings_edit_popover_selected_keybinding_index = -1
self._keybindings_edit_popover_listbox.set_visible(True)
self._keybindings_edit_popover_listbox.set_vexpand(True)
self._keybindings_edit_popover_listbox.set_selection_mode(
Gtk.SelectionMode.SINGLE)
self._keybindings_edit_popover_listbox.set_activate_on_single_click(
True)
self._keybindings_edit_popover_listbox.connect(
'row-selected', self._on_keybindings_edit_listbox_row_selected)
user_keybindings = self._settings_dict['keybindings']['user']
for keybinding in user_keybindings[self._keybindings_selected_command]:
label = Gtk.Label()
label.set_text(html.escape(keybinding))
label.set_use_markup(True)
label.set_xalign(0)
margin = 1
label.set_margin_start(margin)
label.set_margin_end(margin)
label.set_margin_top(margin)
label.set_margin_bottom(margin)
self._keybindings_edit_popover_listbox.insert(label, -1)
self._keybindings_edit_popover_remove_button.set_sensitive(False)
self._keybindings_edit_popover_up_button.set_sensitive(False)
self._keybindings_edit_popover_down_button.set_sensitive(False)
self._keybindings_edit_popover_listbox.show_all()
def _create_and_show_keybindings_edit_popover(self) -> None:
'''
Create and show the popover to edit the key bindings for a command
'''
self._keybindings_edit_popover = Gtk.Popover()
if self._keybindings_edit_popover is None:
LOGGER.debug('self._keybindings_edit_popover is None')
return
self._keybindings_edit_popover.set_relative_to(
self._keybindings_edit_button)
self._keybindings_edit_popover.set_position(Gtk.PositionType.RIGHT)
self._keybindings_edit_popover.set_vexpand(True)
self._keybindings_edit_popover.set_hexpand(True)
keybindings_edit_popover_vbox = Gtk.Box()
keybindings_edit_popover_vbox.set_orientation(
Gtk.Orientation.VERTICAL)
margin = 12
keybindings_edit_popover_vbox.set_margin_start(margin)
keybindings_edit_popover_vbox.set_margin_end(margin)
keybindings_edit_popover_vbox.set_margin_top(margin)
keybindings_edit_popover_vbox.set_margin_bottom(margin)
keybindings_edit_popover_vbox.set_spacing(margin)
keybindings_edit_popover_label = Gtk.Label()
keybindings_edit_popover_label.set_text(
_('Edit key bindings for command “%s”')
%self._keybindings_selected_command)
keybindings_edit_popover_label.set_use_markup(True)
keybindings_edit_popover_label.set_visible(True)
keybindings_edit_popover_label.set_halign(Gtk.Align.FILL)
keybindings_edit_popover_vbox.pack_start(
keybindings_edit_popover_label, False, False, 0)
self._keybindings_edit_popover_scroll = Gtk.ScrolledWindow()
self._keybindings_edit_popover_scroll.set_hexpand(True)
self._keybindings_edit_popover_scroll.set_vexpand(True)
self._keybindings_edit_popover_scroll.set_kinetic_scrolling(False)
self._keybindings_edit_popover_scroll.set_overlay_scrolling(True)
keybindings_edit_popover_vbox.pack_start(
self._keybindings_edit_popover_scroll, True, True, 0)
keybindings_edit_popover_button_box = Gtk.Box()
keybindings_edit_popover_button_box.set_orientation(
Gtk.Orientation.HORIZONTAL)
keybindings_edit_popover_button_box.set_can_focus(False)
keybindings_edit_popover_vbox.add(
keybindings_edit_popover_button_box)
self._keybindings_edit_popover_add_button = Gtk.Button()
keybindings_edit_popover_add_button_label = Gtk.Label()
keybindings_edit_popover_add_button_label.set_text(
'<span size="xx-large"><b>+</b></span>')
keybindings_edit_popover_add_button_label.set_use_markup(True)
self._keybindings_edit_popover_add_button.add(
keybindings_edit_popover_add_button_label)
self._keybindings_edit_popover_add_button.set_tooltip_text(
# Translators: This is a tooltip for the button to add a
# key binding.
_('Add a key binding'))
self._keybindings_edit_popover_add_button.connect(
'clicked', self._on_keybindings_edit_popover_add_button_clicked)
self._keybindings_edit_popover_add_button.set_sensitive(True)
self._keybindings_edit_popover_remove_button = Gtk.Button()
keybindings_edit_popover_remove_button_label = Gtk.Label()
keybindings_edit_popover_remove_button_label.set_text(
'<span size="xx-large"><b>-</b></span>')
keybindings_edit_popover_remove_button_label.set_use_markup(True)
self._keybindings_edit_popover_remove_button.add(
keybindings_edit_popover_remove_button_label)
self._keybindings_edit_popover_remove_button.set_tooltip_text(
# Translators: This is a tooltip for the button to remove
# the selected key binding.
_('Remove selected key binding'))
self._keybindings_edit_popover_remove_button.connect(
'clicked', self._on_keybindings_edit_popover_remove_button_clicked)
self._keybindings_edit_popover_remove_button.set_sensitive(False)
self._keybindings_edit_popover_default_button = Gtk.Button()
self._keybindings_edit_popover_up_button = Gtk.Button()
keybindings_edit_popover_up_button_label = Gtk.Label()
keybindings_edit_popover_up_button_label.set_text(
'<span size="xx-large"><b>↑</b></span>')
keybindings_edit_popover_up_button_label.set_use_markup(True)
self._keybindings_edit_popover_up_button.add(
keybindings_edit_popover_up_button_label)
self._keybindings_edit_popover_up_button.set_tooltip_text(
# Translators: This is a tooltip for the button to move
# the selected key binding up (higher in priority).
_('Move key binding up'))
self._keybindings_edit_popover_up_button.connect(
'clicked', self._on_keybindings_edit_popover_up_button_clicked)
self._keybindings_edit_popover_down_button = Gtk.Button()
keybindings_edit_popover_down_button_label = Gtk.Label()
keybindings_edit_popover_down_button_label.set_text(
'<span size="xx-large"><b>↓</b></span>')
keybindings_edit_popover_down_button_label.set_use_markup(True)
self._keybindings_edit_popover_down_button.add(
keybindings_edit_popover_down_button_label)
self._keybindings_edit_popover_down_button.set_tooltip_text(
# Translators: This is a tooltip for the button to move
# the selected key binding down (lower in priority).
_('Move key binding down'))
self._keybindings_edit_popover_down_button.connect(
'clicked', self._on_keybindings_edit_popover_down_button_clicked)
keybindings_edit_popover_default_button_label = Gtk.Label()
keybindings_edit_popover_default_button_label.set_text(
_('Set to default'))
keybindings_edit_popover_default_button_label.set_use_markup(True)
self._keybindings_edit_popover_default_button.add(
keybindings_edit_popover_default_button_label)
self._keybindings_edit_popover_default_button.set_tooltip_text(
# Translators: This is a tooltip for the button to set
# the key bindings for the selected command to the default.
_('Set default key bindings for the selected command'))
self._keybindings_edit_popover_default_button.connect(
'clicked',
self._on_keybindings_edit_popover_default_button_clicked)
self._keybindings_edit_popover_default_button.set_sensitive(True)
keybindings_edit_popover_button_box.add(
self._keybindings_edit_popover_add_button)
keybindings_edit_popover_button_box.add(
self._keybindings_edit_popover_remove_button)
keybindings_edit_popover_button_box.add(
self._keybindings_edit_popover_up_button)
keybindings_edit_popover_button_box.add(
self._keybindings_edit_popover_down_button)
keybindings_edit_popover_button_box.add(
self._keybindings_edit_popover_default_button)
self._keybindings_edit_popover.add(keybindings_edit_popover_vbox)
self._fill_keybindings_edit_popover_listbox()
if GTK_VERSION >= (3, 22, 0):
self._keybindings_edit_popover.popup()
self._keybindings_edit_popover.show_all()
def _on_keybindings_edit_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “edit” button to edit the
key bindings for a command has been clicked.
'''
self._create_and_show_keybindings_edit_popover()
def _on_keybindings_default_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “Set to default” button to reset the
key bindings for a command to the default has been clicked.
'''
default_keybindings = self._settings_dict['keybindings']['default']
user_keybindings = self._settings_dict['keybindings']['user']
command = self._keybindings_selected_command
if command and command in default_keybindings:
user_keybindings[command] = default_keybindings[command].copy()
self.set_keybindings(user_keybindings)
def _on_keybindings_all_default_button_clicked(
self, _button: Gtk.Button) -> None:
'''
Signal handler called when the “Set all to default” button to reset the
all key bindings top their defaults has been clicked.
'''
self._keybindings_all_default_button.set_sensitive(False)
response = self._run_are_you_sure_dialog(
# Translators: This is the text in the centre of a small
# dialog window, trying to confirm whether the user is
# really sure to reset the key bindings for *all* commands
# to their defaults. This cannot be reversed so the user
# should be really sure he wants to do that.
_('Do you really want to set the key bindings for '
+ 'all commands to their defaults?'))
if response == Gtk.ResponseType.OK:
self.set_keybindings(self._settings_dict['keybindings']['default'])
self._keybindings_all_default_button.set_sensitive(True)
def _on_input_method_menu_checkbutton(self, _widget: Gtk.CheckButton) -> None:
'''
The checkbutton to whether to display the input method menu item
:param widget: The check button clicked
'''
items = []
for item in self._input_method_menu_tuple:
name, _label, button = item
if isinstance(button, Gtk.CheckButton) and button.get_active():
items.append(str(name))
self.set_input_method_menu(items, update_gsettings=True)
def set_single_wildcard_char(self,
single_wildcard_char: str,
update_gsettings: bool = True) -> None:
'''Sets the single wildchard character.
:param single_wildcard_char: The character to use as a single wildcard
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)',
single_wildcard_char, update_gsettings)
self._settings_dict['singlewildcardchar']['user'] = (
single_wildcard_char)
if update_gsettings:
self._gsettings.set_value(
'singlewildcardchar',
GLib.Variant.new_string(single_wildcard_char))
else:
self._single_wildcard_char_entry.set_text(single_wildcard_char)
def set_multi_wildcard_char(self,
multi_wildcard_char: str,
update_gsettings: bool = True) -> None:
'''Sets the single wildchard character.
:param multi_wildcard_char: The character to use as a single wildcard
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)',
multi_wildcard_char, update_gsettings)
self._settings_dict['multiwildcardchar']['user'] = multi_wildcard_char
if update_gsettings:
self._gsettings.set_value(
'multiwildcardchar',
GLib.Variant.new_string(multi_wildcard_char))
else:
self._multi_wildcard_char_entry.set_text(multi_wildcard_char)
def set_page_size(self,
page_size: int,
update_gsettings: bool = True) -> None:
'''Sets the page size of the lookup table
:param page_size: The page size of the lookup table
(1 <= page size <= 10)
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', page_size, update_gsettings)
page_size = int(page_size)
if 1 <= page_size <= 10:
self._settings_dict['lookuptablepagesize']['user'] = page_size
if update_gsettings:
self._gsettings.set_value(
'lookuptablepagesize',
GLib.Variant.new_int32(page_size))
else:
self._page_size_adjustment.set_value(page_size)
def set_lookup_table_orientation(self,
orientation: int,
update_gsettings: bool = True) -> None:
'''Sets the page size of the lookup table
:param orientation: The orientation of the lookup table
(0 < =orientation <= 2)
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', orientation, update_gsettings)
orientation = int(orientation)
if 0 <= orientation <= 2:
self._settings_dict['lookuptableorientation']['user'] = orientation
if update_gsettings:
self._gsettings.set_value(
'lookuptableorientation',
GLib.Variant.new_int32(orientation))
else:
for index, item in enumerate(
self._lookup_table_orientation_store):
if orientation == item[1]:
self._lookup_table_orientation_combobox.set_active(
index)
def set_input_mode(self,
input_mode: int = 1,
update_gsettings: bool = True) -> None:
'''Sets whether direct input or the current table is used.
:param input_mode: Whether to use direct input.
0: Use direct input.
1: Use the current table.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', input_mode, update_gsettings)
input_mode = int(input_mode)
if 0 <= input_mode <= 1:
self._settings_dict['inputmode']['user'] = input_mode
if update_gsettings:
self._gsettings.set_value(
'inputmode',
GLib.Variant.new_int32(input_mode))
def set_remember_input_mode(self,
remember_input_mode: bool = True,
update_gsettings: bool = True) -> None:
'''Sets whether the input mode (direct or table) is remembered
:param remember_input_mode: Whether to remember the input mode.
False: Do not remember
True: Remember.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', remember_input_mode, update_gsettings)
remember_input_mode = bool(remember_input_mode)
self._settings_dict['rememberinputmode']['user'] = remember_input_mode
if update_gsettings:
self._gsettings.set_value(
'rememberinputmode',
GLib.Variant.new_boolean(remember_input_mode))
else:
for index, item in enumerate(self._remember_input_mode_store):
if remember_input_mode == item[1]:
self._remember_input_mode_combobox.set_active(index)
def set_chinese_mode(self,
chinese_mode: int = 0,
update_gsettings: bool = True) -> None:
'''Sets the candidate filter mode used for Chinese
0 means to show simplified Chinese only
1 means to show traditional Chinese only
2 means to show all characters but show simplified Chinese first
3 means to show all characters but show traditional Chinese first
4 means to show all characters
:param chinese_mode: The Chinese filter mode (0 <= mode <= 4)
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the dconf key changed
to avoid endless loops when the dconf
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', chinese_mode, update_gsettings)
chinese_mode = int(chinese_mode)
if 0 <= chinese_mode <= 4:
self._settings_dict['chinesemode']['user'] = chinese_mode
if update_gsettings:
self._gsettings.set_value(
'chinesemode',
GLib.Variant.new_int32(chinese_mode))
else:
for index, item in enumerate(self._chinese_mode_store):
if chinese_mode == item[1]:
self._chinese_mode_combobox.set_active(index)
def set_onechar_mode(self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether only single characters should be matched in
the database.
:param mode: Whether only single characters should be matched.
True: Match only single characters.
False: Possibly match multiple characters at once.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['onechar']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'onechar',
GLib.Variant.new_boolean(mode))
else:
for index, item in enumerate(self._onechar_mode_store):
if mode == item[1]:
self._onechar_mode_combobox.set_active(index)
def set_autoselect_mode(self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether the first candidate will be selected
automatically during typing.
:param mode: Whether to select the first candidate automatically.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['autoselect']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'autoselect',
GLib.Variant.new_boolean(mode))
else:
self._autoselect_mode_checkbutton.set_active(mode)
def set_autocommit_mode(self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether automatic commits go into the preëdit or into the
application.
:param mode: Whether automatic commits go into the preëdit
or into the application.
True: Into the application.
False: Into the preedit.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['autocommit']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'autocommit',
GLib.Variant.new_boolean(mode))
else:
for index, item in enumerate(self._autocommit_mode_store):
if mode == item[1]:
self._autocommit_mode_combobox.set_active(index)
def set_commit_invalid_mode(self,
mode: int = 0,
update_gsettings: bool = True) -> None:
'''Sets the commit invalid mode
This selects what is committed when a character which is not
in the set of valid input characters for the current table is
typed.
0 means to commit the current candidate
1 means to commit the raw characters typed so far
:param mode: The mode (0 <= mode <= 1)
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the dconf key changed
to avoid endless loops when the dconf
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = int(mode)
if 0 <= mode <= 1:
self._settings_dict['commitinvalidmode']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'commitinvalidmode',
GLib.Variant.new_int32(mode))
else:
for index, item in enumerate(self._commit_invalid_mode_store):
if mode == item[1]:
self._commit_invalid_mode_combobox.set_active(index)
def set_autowildcard_mode(self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether a wildcard should be automatically appended
to the input.
:param mode: Whether to append a wildcard automatically.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['autowildcard']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'autowildcard',
GLib.Variant.new_boolean(mode))
else:
self._autowildcard_mode_checkbutton.set_active(mode)
def set_table_full_width_letter_mode(
self,
mode: bool =False,
update_gsettings: bool = True) -> None:
'''Sets whether full width letters should be used
while in “Table input” mode
:param mode: Whether to use full width letters
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['tabdeffullwidthletter']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'tabdeffullwidthletter',
GLib.Variant.new_boolean(mode))
else:
for index, item in enumerate(
self._table_full_width_letter_mode_store):
if mode == item[1]:
self._table_full_width_letter_mode_combobox.set_active(
index)
def set_table_full_width_punct_mode(
self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether full width punctuation should be used
while in “Table input” mode
:param mode: Whether to use full width punctuation
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['tabdeffullwidthpunct']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'tabdeffullwidthpunct',
GLib.Variant.new_boolean(mode))
else:
for index, item in enumerate(
self._table_full_width_punct_mode_store):
if mode == item[1]:
self._table_full_width_punct_mode_combobox.set_active(
index)
def set_direct_full_width_letter_mode(
self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether full width letters should be used
while in “Direct input” mode
:param mode: Whether to use full width letters
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['endeffullwidthletter']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'endeffullwidthletter',
GLib.Variant.new_boolean(mode))
else:
for index, item in enumerate(
self._direct_full_width_letter_mode_store):
if mode == item[1]:
self._direct_full_width_letter_mode_combobox.set_active(
index)
def set_direct_full_width_punct_mode(
self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets whether full width punctuation should be used
while in “Direct input” mode
:param mode: Whether to use full width punctuation
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
mode = bool(mode)
self._settings_dict['endeffullwidthpunct']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'endeffullwidthpunct',
GLib.Variant.new_boolean(mode))
else:
for index, item in enumerate(
self._direct_full_width_punct_mode_store):
if mode == item[1]:
self._direct_full_width_punct_mode_combobox.set_active(
index)
def set_always_show_lookup(
self,
mode: bool = False,
update_gsettings: bool = True) -> None:
'''Sets the whether the lookup table is shown.
:param mode: Whether to show the lookup table
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', mode, update_gsettings)
self._settings_dict['alwaysshowlookup']['user'] = mode
if update_gsettings:
self._gsettings.set_value(
'alwaysshowlookup',
GLib.Variant.new_boolean(mode))
else:
self._always_show_lookup_checkbutton.set_active(mode)
def set_dark_theme(
self,
use_dark_theme: bool = False,
update_gsettings: bool = True) -> None:
'''Sets the whether to use the color scheme for dark theme.
:param use_dark_theme: Whether to use the color scheme for dark theme
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', use_dark_theme, update_gsettings)
self._settings_dict['darktheme']['user'] = use_dark_theme
if update_gsettings:
self._gsettings.set_value(
'darktheme',
GLib.Variant.new_boolean(use_dark_theme))
else:
self._use_dark_theme_checkbutton.set_active(use_dark_theme)
def set_dynamic_adjust(
self,
dynamic_adjust: bool,
update_gsettings: bool = True) -> None:
'''Sets the whether dynamically adjust the candidates according to
how often they are used.
:param dynamic_adjust: Whether to dynamically adjust the candidates
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', dynamic_adjust, update_gsettings)
self._settings_dict['dynamicadjust']['user'] = dynamic_adjust
if update_gsettings:
self._gsettings.set_value(
'dynamicadjust',
GLib.Variant.new_boolean(dynamic_adjust))
else:
self._dynamic_adjust_checkbutton.set_active(dynamic_adjust)
def set_error_sound(
self,
error_sound: bool,
update_gsettings: bool = True) -> None:
'''Sets the whether to play a sound on error.
:param error_sound: Whether to play a sound on error
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', error_sound, update_gsettings)
self._settings_dict['errorsound']['user'] = error_sound
if update_gsettings:
self._gsettings.set_value(
'errorsound',
GLib.Variant.new_boolean(error_sound))
else:
self._error_sound_checkbutton.set_active(error_sound)
def set_error_sound_file(
self,
path: Union[str, Any],
update_gsettings: bool = True) -> None:
'''Sets the path of file containing the sound to play on error.
:param path: Full path of the .wav file containing the error sound.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', path, update_gsettings)
if not isinstance(path, str):
return
self._settings_dict['errorsoundfile']['user'] = path
if update_gsettings:
self._gsettings.set_value(
'errorsoundfile',
GLib.Variant.new_string(path))
else:
self._error_sound_file_button_label.set_text(path)
self._error_sound_object = it_sound.SoundObject(
os.path.expanduser(self._settings_dict['errorsoundfile']['user']),
audio_backend=self._settings_dict['soundbackend']['user'])
if self._error_sound_object:
try:
self._error_sound_object.play()
except Exception as error: # pylint: disable=broad-except
LOGGER.exception('Playing error sound failed: %s: %s',
error.__class__.__name__, error)
def set_sound_backend(
self,
sound_backend: Union[str, Any],
update_gsettings: bool = True) -> None:
'''Sets the sound backend to use
:param sound_backend: The name of sound backend to use
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the dconf key changed
to avoid endless loops when the dconf
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', sound_backend, update_gsettings)
if not isinstance(sound_backend, str):
return
if sound_backend == self._settings_dict['soundbackend']['user']:
return
self._settings_dict['soundbackend']['user'] = sound_backend
if update_gsettings:
self._gsettings.set_value(
'soundbackend',
GLib.Variant.new_string(sound_backend))
self._error_sound_object = it_sound.SoundObject(
os.path.expanduser(self._settings_dict['errorsoundfile']['user']),
audio_backend=self._settings_dict['soundbackend']['user'])
def set_debug_level(self,
debug_level: int,
update_gsettings: bool = True) -> None:
'''Sets the debug level
:param debug level: The debug level (0 <= level <= 255)
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', debug_level, update_gsettings)
debug_level = int(debug_level)
if 0 <= debug_level <= 255:
self._settings_dict['debuglevel']['user'] = debug_level
if update_gsettings:
self._gsettings.set_value(
'debuglevel',
GLib.Variant.new_int32(debug_level))
else:
self._debug_level_adjustment.set_value(debug_level)
def set_keybindings(
self,
keybindings: Union[Dict[str, List[str]], Any],
update_gsettings: bool = True) -> None:
'''Set current key bindings
:param keybindings: The key bindings to use
Commands which do not already
exist in the current key bindings dictionary
will be ignored.
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', keybindings, update_gsettings)
if not isinstance(keybindings, dict):
return
keybindings = copy.deepcopy(keybindings)
user_keybindings = self._settings_dict['keybindings']['user']
# Update the default settings with the possibly changed settings:
it_util.dict_update_existing_keys(user_keybindings, keybindings)
# update the tree model
model = self._keybindings_treeview_model
iterator = model.get_iter_first()
while iterator:
for command in user_keybindings:
if model.get_value(iterator, 0) == command:
model.set_value(iterator, 1,
repr(user_keybindings[command]))
iterator = model.iter_next(iterator)
if update_gsettings:
variant_dict = GLib.VariantDict(GLib.Variant('a{sv}', {}))
for command in sorted(user_keybindings):
variant_array = GLib.Variant.new_array(
GLib.VariantType('s'),
[GLib.Variant.new_string(x)
for x in user_keybindings[command]])
variant_dict.insert_value(command, variant_array)
self._gsettings.set_value(
'keybindings',
variant_dict.end())
def set_input_method_menu(
self,
input_method_menu: List[str],
update_gsettings: bool = True) -> None:
'''Sets the visible input method menu items
:param input_method_menu: The visible input method menu items
:param update_gsettings: Whether to write the change to Gsettings.
Set this to False if this method is
called because the Gsettings key changed
to avoid endless loops when the Gsettings
key is changed twice in a short time.
'''
LOGGER.info(
'(%s, update_gsettings = %s)', input_method_menu, update_gsettings)
self._settings_dict['inputmethodmenu']['user'] = input_method_menu
if update_gsettings:
self._gsettings.set_value(
'inputmethodmenu',
GLib.Variant.new_strv(input_method_menu))
else:
for item in self._input_method_menu_tuple:
name, _label, button = item
if isinstance(button, Gtk.CheckButton):
if name in input_method_menu:
button.set_active(True)
else:
button.set_active(False)
class HelpWindow(Gtk.Window): # type: ignore
'''
A window to show help
:param parent: The parent object
:param title: Title of the help window
:param contents: Contents of the help window
'''
def __init__(self,
parent: Gtk.Window = None,
title: str = '',
contents: str = '') -> None:
Gtk.Window.__init__(self, title=title)
if parent:
self.set_parent(parent)
self.set_transient_for(parent)
# to receive mouse events for scrolling and for the close
# button
self.set_modal(True)
self.set_destroy_with_parent(False)
self.set_default_size(600, 500)
self.vbox = Gtk.Box()
self.vbox.set_orientation(Gtk.Orientation.VERTICAL)
self.vbox.set_spacing(0)
self.add(self.vbox)
self.text_buffer = Gtk.TextBuffer()
self.text_buffer.insert_at_cursor(contents)
self.text_view = Gtk.TextView()
self.text_view.set_buffer(self.text_buffer)
self.text_view.set_editable(False)
self.text_view.set_cursor_visible(False)
self.text_view.set_justification(Gtk.Justification.LEFT)
self.text_view.set_wrap_mode(Gtk.WrapMode.WORD)
self.scrolledwindow = Gtk.ScrolledWindow()
self.scrolledwindow.set_hexpand(True)
self.scrolledwindow.set_vexpand(True)
self.scrolledwindow.add(self.text_view)
self.vbox.pack_start(self.scrolledwindow, True, True, 0)
self.close_button = Gtk.Button()
self.close_button_label = Gtk.Label()
self.close_button_label.set_text_with_mnemonic(_('_Close'))
self.close_button.add(self.close_button_label)
self.close_button.connect("clicked", self._on_close_button_clicked)
self.hbox = Gtk.HBox(spacing=0)
self.hbox.pack_end(self.close_button, False, False, 0)
self.vbox.pack_start(self.hbox, False, False, 5)
self.show_all()
def _on_close_button_clicked(self, _button: Gtk.Button) -> None:
'''
Close the input method help window when the close button is clicked
'''
self.destroy()
def quit_glib_main_loop(
signum: int, _frame: Optional[FrameType] = None) -> None:
'''Signal handler for signals from Python’s signal module
:param signum: The signal number
:param _frame: Almost never used (it’s for debugging).
'''
if signum is not None:
try:
signal_name = signal.Signals(signum).name
except ValueError: # In case signum isn't in Signals enum
signal_name = str(signum)
LOGGER.info('Received signal %s (%s), exiting...', signum, signal_name)
if GLIB_MAIN_LOOP is not None:
GLIB_MAIN_LOOP.quit()
else:
raise RuntimeError("GLIB_MAIN_LOOP not initialized!")
if __name__ == '__main__':
if _ARGS.no_debug:
LOG_HANDLER_NULL = logging.NullHandler()
else:
LOGFILE = os.path.join(
ibus_table_location.cache_home(), 'setup-debug.log')
LOG_HANDLER_TIME_ROTATE = logging.handlers.TimedRotatingFileHandler(
LOGFILE,
when='H',
interval=6,
backupCount=7,
encoding='UTF-8',
delay=False,
utc=False,
atTime=None)
LOG_FORMATTER = logging.Formatter(
'%(asctime)s %(filename)s '
'line %(lineno)d %(funcName)s %(levelname)s: '
'%(message)s')
LOG_HANDLER_TIME_ROTATE.setFormatter(LOG_FORMATTER)
LOGGER.setLevel(logging.DEBUG)
LOGGER.addHandler(LOG_HANDLER_TIME_ROTATE)
LOGGER.info('********** STARTING **********')
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error:
LOGGER.exception("IBUS-WARNING **: Using the fallback 'C' locale")
locale.setlocale(locale.LC_ALL, 'C')
i18n_init()
if IBus.get_address() is None:
DIALOG = Gtk.MessageDialog(
modal=True,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
text=_('ibus is not running.'))
DIALOG.run()
DIALOG.destroy()
sys.exit(1)
ENGINE_NAME = _ARGS.engine_name
if not ENGINE_NAME and 'IBUS_ENGINE_NAME' in os.environ:
ENGINE_NAME = os.environ['IBUS_ENGINE_NAME']
ENGINE_NAME = re.sub(r'^table:', '', ENGINE_NAME).replace(' ', '_')
if not ENGINE_NAME:
PARSER.print_help()
SETUP_UI = SetupUI(engine_name=ENGINE_NAME)
GLIB_MAIN_LOOP = GLib.MainLoop()
signal.signal(signal.SIGTERM, quit_glib_main_loop) # kill <pid>
# Ctrl+C (optional, can also use try/except KeyboardInterrupt)
# signal.signal(signal.SIGINT, quit_glib_main_loop)
try:
GLIB_MAIN_LOOP.run()
except KeyboardInterrupt:
# SIGNINT (Control+C) received
LOGGER.info('Control+C pressed, exiting ...')
GLIB_MAIN_LOOP.quit()
|