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
|
/************************************************************************
*
* Copyright (C) 2009-2025 IRCAD France
* Copyright (C) 2012-2020 IHU Strasbourg
*
* This file is part of Sight.
*
* Sight 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 3 of the License, or
* (at your option) any later version.
*
* Sight 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "parameters.hpp"
#include <core/com/signal.hxx>
#include <core/com/slots.hxx>
#include <core/object.hpp>
#include <core/runtime/path.hpp>
#include <data/tools/color.hpp>
#include <ui/qt/container/widget.hpp>
#include <ui/qt/widget/non_linear_slider.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/tokenizer.hpp>
#include <QAbstractButton>
#include <QApplication>
#include <QButtonGroup>
#include <QColorDialog>
#include <QEvent>
#include <QFormLayout>
#include <QGraphicsColorizeEffect>
#include <QGroupBox>
#include <QScrollArea>
#include <QScrollBar>
#include <QSpinBox>
#include <QString>
#include <QStyle>
#include <QToolButton>
namespace sight::module::ui::qt
{
//------------------------------------------------------------------------------
namespace
{
// Internal static function to split a string using separator (usually =), mainly used to split enum into value, data
std::pair<std::string, std::string> split_string(const std::string& _str, const std::string& _sep = "=")
{
std::string left;
std::string right;
const boost::char_separator<char> subsep(_sep.c_str());
const boost::tokenizer<boost::char_separator<char> > subtokens {_str, subsep};
auto it = subtokens.begin();
if(it != subtokens.end())
{
left = *it;
++it;
// check if we have another part
if(it != subtokens.end())
{
right = *it;
}
}
return {left, right};
}
} // anonymous namespace
//-----------------------------------------------------------------------------
parameters::parameters() noexcept
{
new_signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG);
new_signal<signals::boolean_changed_signal_t>(signals::BOOLEAN_CHANGED_SIG);
new_signal<signals::color_changed_signal_t>(signals::COLOR_CHANGED_SIG);
new_signal<signals::double_changed_signal_t>(signals::DOUBLE_CHANGED_SIG);
new_signal<signals::double2_changed_signal_t>(signals::DOUBLE2_CHANGED_SIG);
new_signal<signals::double3_changed_signal_t>(signals::DOUBLE3_CHANGED_SIG);
new_signal<signals::integer_changed_signal_t>(signals::INTEGER_CHANGED_SIG);
new_signal<signals::integer2_changed_signal_t>(signals::INTEGER2_CHANGED_SIG);
new_signal<signals::integer3_changed_signal_t>(signals::INTEGER3_CHANGED_SIG);
new_signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG);
new_signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG);
new_slot(slots::SET_PARAMETER_SLOT, ¶meters::set_parameter, this);
new_slot(slots::SET_BOOL_PARAMETER_SLOT, ¶meters::set_bool_parameter, this);
new_slot(slots::SET_COLOR_PARAMETER_SLOT, ¶meters::set_color_parameter, this);
new_slot(slots::SET_DOUBLE_PARAMETER_SLOT, ¶meters::set_double_parameter, this);
new_slot(slots::SET_DOUBLE2_PARAMETER_SLOT, ¶meters::set_double2_parameter, this);
new_slot(slots::SET_DOUBLE3_PARAMETER_SLOT, ¶meters::set_double3_parameter, this);
new_slot(slots::SET_INT_PARAMETER_SLOT, ¶meters::set_int_parameter, this);
new_slot(slots::SET_INT2_PARAMETER_SLOT, ¶meters::set_int2_parameter, this);
new_slot(slots::SET_INT3_PARAMETER_SLOT, ¶meters::set_int3_parameter, this);
new_slot(slots::SET_ENUM_PARAMETER_SLOT, ¶meters::set_enum_parameter, this);
new_slot(slots::SET_ENUM_INDEX_PARAMETER_SLOT, ¶meters::set_enum_index_parameter, this);
new_slot(slots::UPDATE_ENUM_RANGE_SLOT, ¶meters::update_enum_range, this);
new_slot(slots::UPDATE_INT_MIN_PARAMETER_SLOT, ¶meters::update_int_min_parameter, this);
new_slot(slots::UPDATE_INT_MAX_PARAMETER_SLOT, ¶meters::update_int_max_parameter, this);
new_slot(slots::UPDATE_DOUBLE_MIN_PARAMETER_SLOT, ¶meters::update_double_min_parameter, this);
new_slot(slots::UPDATE_DOUBLE_MAX_PARAMETER_SLOT, ¶meters::update_double_max_parameter, this);
}
//-----------------------------------------------------------------------------
void parameters::configuring()
{
this->initialize();
}
//-----------------------------------------------------------------------------
void parameters::starting()
{
this->create();
const std::string service_id = base_id();
auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(this->get_container());
qt_container->get_qt_container()->setObjectName(QString::fromStdString(service_id));
QScrollArea* scroll_area = nullptr;
QWidget* viewport = nullptr;
service::config_t config = this->get_config();
const service::config_t& parameters_cfg = config.get_child("parameters");
const bool scrollable = parameters_cfg.get<bool>("<xmlattr>.scrollable", false);
if(scrollable)
{
scroll_area = new QScrollArea(qt_container->get_qt_container()->parentWidget());
scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
viewport = new QWidget(qt_container->get_qt_container());
}
auto* layout = new QFormLayout {};
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->setAlignment(Qt::AlignCenter);
this->blockSignals(true);
// We don't support having multiple parameters with the same key (this isn't a good idea any way).
// This set keeps tracks of the ones we add, and triggers and assert if it already existed.
[[maybe_unused]] std::set<std::string> keys;
// Create widgets
for(const auto& param : boost::make_iterator_range(parameters_cfg.equal_range("param")))
{
const service::config_t& cfg = param.second;
const auto orientation = cfg.get<std::string>("<xmlattr>.orientation", "horizontal") == "horizontal"
? Qt::Horizontal
: Qt::Vertical;
const auto param_box_direction =
orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
const auto type = cfg.get<std::string>("<xmlattr>.type");
const auto min_width = cfg.get<int>("<xmlattr>.min_width", 0);
const auto min_height = cfg.get<int>("<xmlattr>.min_height", 0);
const param_widget widget
{
.name = cfg.get<std::string>("<xmlattr>.name"),
.key = cfg.get<std::string>("<xmlattr>.key"),
.default_value = cfg.get<std::string>("<xmlattr>.defaultValue"),
.reset_button = cfg.get<bool>("<xmlattr>.reset", true),
.hide_min_max = cfg.get<bool>("<xmlattr>.hideMinMax", false),
.min_size = {min_width, min_height},
};
SIGHT_ASSERT(
get_id() << ": Key " << std::quoted(widget.key) << " already exists.",
keys.insert(widget.key).second
);
auto* const param_box = new QGroupBox {scroll_area == nullptr ? qt_container->get_qt_container() : scroll_area};
{
const auto qt_key = QString::fromStdString(widget.key) + "_box";
// This intermediate widget shouldn't be visible
// Note that because we're setting this stylesheet on the fly on this "top-level" widget, we must
// apply it again on its children: children inherits the stylesheet of their parents
// Note that these widgets aren't required for display purposes, but having them greatly simplifies the
// search done in get_param_widget.
param_box->setStyleSheet("background-color: rgba(0,0,0,0); border: 0px;");
param_box->setContentsMargins(0, 0, 0, 0);
param_box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
param_box->setProperty("key", qt_key);
param_box->setObjectName(qt_key);
m_param_boxes.emplace_back(param_box);
}
auto* const param_box_layout = new QBoxLayout {param_box_direction, param_box};
auto* const row_layout = new QHBoxLayout {};
row_layout->addWidget(param_box);
// Label
{
QLabel* parameter_label = nullptr;
if(!widget.name.empty())
{
parameter_label = new QLabel(QString::fromStdString(widget.name));
parameter_label->setWordWrap(true);
parameter_label->setAlignment(Qt::AlignCenter);
parameter_label->setStyleSheet("margin: -0.1em;");
}
if(parameter_label != nullptr)
{
// When horizontal, the label is added to the "main" widget layout, otherwise, at the top of the box
if(orientation == Qt::Vertical)
{
param_box_layout->addWidget(parameter_label);
layout->addRow(row_layout);
}
else
{
layout->addRow(parameter_label, row_layout);
}
}
else
{
layout->addRow(row_layout);
}
}
if(type == "bool")
{
if(auto* reset = this->create_bool_widget(*param_box_layout, widget))
{
row_layout->addWidget(reset);
}
}
else if(type == "color")
{
if(auto* reset = this->create_color_widget(*param_box_layout, widget))
{
row_layout->addWidget(reset);
}
}
else if(type == "double" || type == "double2" || type == "double3")
{
const std::string widget_type = cfg.get<std::string>("<xmlattr>.widget", "spin");
const double min = cfg.get<double>("<xmlattr>.min", 0.);
const double max = cfg.get<double>("<xmlattr>.max", 1.);
const double_widget widget_double = {widget, min, max};
const int count = (type == "double3") ? 3 : (type == "double2" ? 2 : 1);
if(widget_type == "spin")
{
if(auto* reset = this->create_double_widget(*param_box_layout, widget_double, count, orientation))
{
row_layout->addWidget(reset);
}
}
else if(widget_type == "slider")
{
// TODO: this could be supported now
SIGHT_ASSERT(get_id() << ": Count > 1 is not supported with sliders", count == 1);
const std::uint8_t decimals = cfg.get<std::uint8_t>("<xmlattr>.decimals", 2);
const bool on_release = cfg.get<bool>("<xmlattr>.emitOnRelease", false);
if(auto* reset =
this->create_double_slider_widget(
*param_box_layout,
widget_double,
decimals,
orientation,
on_release
)
)
{
// Looks better under the rest
if(orientation == Qt::Vertical)
{
param_box_layout->addWidget(reset, /*stretch = */ 0, Qt::AlignCenter);
}
else
{
row_layout->addWidget(reset);
}
}
}
else
{
SIGHT_ERROR(
get_id() << ": Unknown widget type : " << std::quoted(widget_type) << " for "
<< std::quoted(widget.name)
<< ". Must be 'spin' or 'slider'."
);
}
}
else if(type == "int" || type == "int2" || type == "int3")
{
const std::string widget_type = cfg.get<std::string>("<xmlattr>.widget", "slider");
const int min = cfg.get<int>("<xmlattr>.min", 0);
const int max = cfg.get<int>("<xmlattr>.max", 100);
const int_widget widget_int = {widget, min, max};
const int count = (type == "int3") ? 3 : (type == "int2" ? 2 : 1);
if(widget_type == "spin")
{
if(auto* reset = this->create_integer_spin_widget(*param_box_layout, widget_int, count, orientation))
{
row_layout->addWidget(reset);
}
}
else if(widget_type == "slider")
{
// TODO: this could be supported now
SIGHT_ASSERT(get_id() << ": Count > 1 is not supported with sliders", count == 1);
const bool on_release = cfg.get<bool>("<xmlattr>.emitOnRelease", false);
if(auto* reset =
this->create_integer_slider_widget(*param_box_layout, widget_int, orientation, on_release))
{
// Looks better under the rest
if(orientation == Qt::Vertical)
{
param_box_layout->addWidget(reset, /*stretch = */ 0, Qt::AlignCenter);
}
else
{
row_layout->addWidget(reset);
}
}
}
else
{
SIGHT_ERROR(
get_id() << ": Unknown widget type : " << std::quoted(widget_type) << " for " << widget.name
<< ". Must be 'spin' or 'slider'."
);
}
}
else if(type == "enum")
{
const auto widget_type = cfg.get<std::string>("<xmlattr>.widget", "combobox");
if(widget_type == "combobox")
{
const auto options = cfg.get<std::string>("<xmlattr>.values");
// split values separated by ',', ' ', ';'
std::vector<std::string> values;
std::vector<std::string> data;
sight::module::ui::qt::parameters::parse_enum_string(options, values, data);
this->create_enum_widget(*param_box_layout, widget, values, data);
}
else if(widget_type == "slider")
{
const auto options = cfg.get<std::string>("<xmlattr>.values");
// split values separated by ',', ' ', ';'
std::vector<std::string> values;
std::vector<std::string> data;
sight::module::ui::qt::parameters::parse_enum_string(options, values, data);
const bool on_release = cfg.get<bool>("<xmlattr>.emitOnRelease", false);
this->create_slider_enum_widget(*param_box_layout, widget, values, orientation, on_release);
}
else if(widget_type == "buttonBar")
{
const int h_offset = cfg.get<int>("<xmlattr>.hOffset", 0);
const int width = cfg.get<int>("<xmlattr>.width", 0);
const int height = cfg.get<int>("<xmlattr>.height", 0);
const std::string style = cfg.get<std::string>("<xmlattr>.style", "iconOnly");
const auto value_config = cfg.equal_range("item");
std::vector<enum_button_param> button_list;
for(auto value_config_it = value_config.first ; value_config_it != value_config.second ;
++value_config_it)
{
const auto value = value_config_it->second.get<std::string>("<xmlattr>.value");
const auto label = value_config_it->second.get<std::string>("<xmlattr>.label", "");
const auto icon_path_relative = value_config_it->second.get<std::string>("<xmlattr>.icon", "");
const std::string icon_path =
core::runtime::get_module_resource_file_path(icon_path_relative).generic_string();
button_list.push_back(enum_button_param({value, label, icon_path}));
}
this->create_button_bar_enum_widget(
*param_box_layout,
widget,
button_list,
width,
height,
h_offset,
style,
orientation
);
}
}
}
for(const auto& param : boost::make_iterator_range(parameters_cfg.equal_range("param")))
{
const service::config_t& cfg = param.second;
const auto key = cfg.get<std::string>("<xmlattr>.key");
const std::string depends = cfg.get<std::string>("<xmlattr>.depends", "");
const std::string depends_value = cfg.get<std::string>("<xmlattr>.dependsValue", "");
const bool dependsreverse = cfg.get<bool>("<xmlattr>.dependsReverse", false);
if(!depends.empty())
{
auto* const depends_widget = get_param_widget(depends);
// Note: we disable the QGroupBox containing the widget**s** instead of individual ones, because
// some parameter create several widgets (3 spinboxes, etc.)
const auto widget_container_it = std::ranges::find_if(
m_param_boxes,
[qt_key = QString::fromStdString(key) + "_box"](const QWidget* const _w)
{
return _w->objectName() == qt_key;
});
SIGHT_ASSERT(
get_id() << ": unknown parameter " << std::quoted(key) << ".",
widget_container_it != m_param_boxes.cend()
);
if(auto* const widget = qobject_cast<QWidget*>((*widget_container_it)))
{
widget->installEventFilter(this);
auto* check_box = qobject_cast<QCheckBox*>(depends_widget);
if(check_box != nullptr)
{
QObject::connect(
check_box,
&QCheckBox::stateChanged,
this,
[ = ]{on_depends_changed(check_box, widget, dependsreverse);});
on_depends_changed(check_box, widget, dependsreverse);
}
else
{
auto* combo_box = qobject_cast<QComboBox*>(depends_widget);
if(combo_box != nullptr)
{
QObject::connect(
combo_box,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
[ = ]{on_depends_changed(combo_box, widget, depends_value, dependsreverse);});
on_depends_changed(combo_box, widget, depends_value, dependsreverse);
}
}
}
}
}
if(scroll_area != nullptr)
{
viewport->setLayout(layout);
scroll_area->setWidgetResizable(true);
scroll_area->setWidget(viewport);
auto* main_layout = new QGridLayout();
qt_container->set_layout(main_layout);
main_layout->addWidget(scroll_area);
// The size of the vertical scroll bar isn't taken into account when the QGridLayout fill the space, as such
// some buttons (particularly reset buttons) get hidden. The workaround is to biggen the right margin a little.
int scroll_bar_width = QScrollBar().sizeHint().width();
QMargins margins = layout->contentsMargins();
margins.setRight(scroll_bar_width);
layout->setContentsMargins(margins);
}
else
{
qt_container->set_layout(layout);
}
this->blockSignals(false);
}
//-----------------------------------------------------------------------------
void parameters::updating()
{
auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(this->get_container());
service::config_t config = this->get_config();
const service::config_t& parameters_cfg = config.get_child("parameters");
// emit signal for each widget
for(const auto& param : boost::make_iterator_range(parameters_cfg.equal_range("param")))
{
const service::config_t& cfg = param.second;
const auto key = cfg.get<std::string>("<xmlattr>.key");
const auto type = cfg.get<std::string>("<xmlattr>.type");
if(auto* const child = get_param_widget(key); child != nullptr)
{
if(type == "bool")
{
const QCheckBox* box = dynamic_cast<QCheckBox*>(child);
SIGHT_ASSERT(get_id() << ": Widget must be a QCheckBox", box);
const bool state = (box->checkState() == Qt::Checked);
if(!m_block_signals)
{
this->signal<signals::boolean_changed_signal_t>(signals::BOOLEAN_CHANGED_SIG)
->async_emit(state, key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)->async_emit(state, key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::BOOLEAN_CHANGED_SIG << "(" << std::boolalpha << state
<< std::noboolalpha << ", " << key << ")"
);
}
}
else if(type == "color")
{
const auto color_qt = child->property("color").value<QColor>();
this->emit_color_signal(color_qt, key);
}
else if(type == "double" || type == "double2" || type == "double3")
{
this->emit_double_signal(child);
}
else if(type == "int" || type == "int2" || type == "int3")
{
this->emit_integer_signal(child);
}
else if(type == "enum")
{
if(auto* box = qobject_cast<QComboBox*>(child); box != nullptr)
{
const QString data = box->itemData(box->currentIndex()).toString();
if(!m_block_signals)
{
this->signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG)
->async_emit(data.toStdString(), key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(data.toStdString(), key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_CHANGED_SIG << "(" << data.toStdString() << ", "
<< key
<< ")"
);
this->signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG)
->async_emit(box->currentIndex(), key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(box->currentIndex(), key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_INDEX_CHANGED_SIG << "(" << box->currentIndex()
<< ", "
<< key << ")"
);
}
}
else if(auto* slider = qobject_cast<sight::ui::qt::widget::non_linear_slider*>(child);
slider != nullptr)
{
if(!m_block_signals)
{
int value = slider->value();
this->signal<signals::integer_changed_signal_t>(signals::INTEGER_CHANGED_SIG)
->async_emit(value, key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)->async_emit(value, key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::INTEGER_CHANGED_SIG << "(" << value << ", " << key
<< ")"
);
this->signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG)
->async_emit(std::to_string(value), key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(std::to_string(value), key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_CHANGED_SIG << "(" << value << ", " << key
<< ")"
);
this->signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG)
->async_emit(int(slider->index()), key);
}
}
else if(auto* button_group = qobject_cast<QButtonGroup*>(child); button_group != nullptr)
{
if(auto* checked_button = button_group->checkedButton(); checked_button != nullptr)
{
// Find the currently checked button
const int button_index = static_cast<int>(button_group->buttons().indexOf(checked_button));
const std::string value = checked_button->property("value").toString().toStdString();
this->signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG)->async_emit(value, key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)->async_emit(value, key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_CHANGED_SIG << "(" << value << ", " << key
<< ")"
);
this->signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG)
->async_emit(button_index, key);
}
}
else
{
SIGHT_ASSERT(get_id() << ": Widget must either be a ComboBox or non_linear_slider", false);
}
}
}
}
}
//-----------------------------------------------------------------------------
void parameters::stopping()
{
m_param_boxes.clear(); // Avoid keeping dangling pointers
this->destroy();
}
//-----------------------------------------------------------------------------
bool parameters::eventFilter(QObject* _watched, QEvent* _event)
{
if(_event->type() == ::QEvent::EnabledChange)
{
auto* check_box = qobject_cast<QCheckBox*>(_watched);
if(check_box != nullptr)
{
check_box->stateChanged(check_box->isChecked() ? Qt::Checked : Qt::Unchecked);
}
else
{
auto* combo_box = qobject_cast<QComboBox*>(_watched);
if(combo_box != nullptr)
{
combo_box->currentIndexChanged(combo_box->currentIndex());
}
}
}
return false;
}
//-----------------------------------------------------------------------------
void parameters::on_depends_changed(QCheckBox* _check_box, QWidget* _widget, bool _reverse)
{
if(!_check_box->isEnabled())
{
_widget->setDisabled(true);
}
else if(_reverse)
{
_widget->setDisabled(_check_box->checkState() != 0U);
}
else
{
_widget->setEnabled(_check_box->checkState() != 0U);
}
}
//------------------------------------------------------------------------------
void parameters::on_depends_changed(QComboBox* _combo_box, QWidget* _widget, const std::string& _value, bool _reverse)
{
if(!_combo_box->isEnabled())
{
_widget->setDisabled(true);
}
else if(_reverse)
{
_widget->setDisabled(_combo_box->currentText().toStdString() == _value);
}
else
{
_widget->setEnabled(_combo_box->currentText().toStdString() == _value);
}
}
//-----------------------------------------------------------------------------
void parameters::on_change_enum(int _value) const
{
const QObject* sender = this->sender();
const QString key = sender->property("key").toString();
const auto* box = dynamic_cast<const QComboBox*>(sender);
SIGHT_ASSERT(get_id() << ": Wrong widget type", box);
const QString data = box->itemData(_value).toString();
if(!m_block_signals)
{
this->signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG)
->async_emit(data.toStdString(), key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(data.toStdString(), key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_CHANGED_SIG << "(" << data.toStdString() << ", "
<< key.toStdString()
<< ")"
);
this->signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG)
->async_emit(_value, key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(box->currentIndex(), key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_INDEX_CHANGED_SIG << "(" << _value << ", " << key.toStdString()
<< ")"
);
}
}
//-----------------------------------------------------------------------------
void parameters::on_change_boolean(int _value) const
{
const QObject* sender = this->sender();
const QString key = sender->property("key").toString();
const bool checked = _value == Qt::Checked;
if(!m_block_signals)
{
this->signal<signals::boolean_changed_signal_t>(signals::BOOLEAN_CHANGED_SIG)
->async_emit(checked, key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)->async_emit(checked, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::BOOLEAN_CHANGED_SIG << "(" << (checked ? "true" : "false") << ", "
<< key.toStdString() << ")"
);
}
}
//------------------------------------------------------------------------------
void parameters::on_color_button()
{
QObject* sender = this->sender();
// Create Color choice dialog.
auto qt_container = std::dynamic_pointer_cast<sight::ui::qt::container::widget>(this->get_container());
QWidget* const container = qt_container->get_qt_container();
SIGHT_ASSERT(get_id() << ": Container not instantiated yet.", container);
const auto old_color = sender->property("color").value<QColor>();
const QColor color_qt = QColorDialog::getColor(old_color, container);
if(color_qt.isValid())
{
const QString key = sender->property("key").toString();
auto* colour_button = dynamic_cast<QPushButton*>(sender);
colour_button->setProperty("color", color_qt);
int icon_size = colour_button->style()->pixelMetric(QStyle::PM_LargeIconSize);
QPixmap pix(icon_size, icon_size);
pix.fill(color_qt);
colour_button->setIcon(QIcon(pix));
this->emit_color_signal(color_qt, key.toStdString());
}
}
//-----------------------------------------------------------------------------
void parameters::on_change_integer(int /*unused*/) const
{
QObject* sender = this->sender();
this->emit_integer_signal(sender);
}
//------------------------------------------------------------------------------
void parameters::emit_integer_signal(QObject* _widget) const
{
if(!m_block_signals)
{
SIGHT_ASSERT(get_id() << ": Widget is null", _widget != nullptr);
const QString key = _widget->property("key").toString();
const int count = _widget->property("count").toInt();
SIGHT_ASSERT(get_id() << ": Invalid widgets count, must be <= 3", count <= 3);
const auto* spinbox = dynamic_cast<const QSpinBox*>(_widget);
const auto* slider = dynamic_cast<const QSlider*>(_widget);
SIGHT_ASSERT(get_id() << ": Wrong widget type", spinbox || slider);
if(count == 1)
{
int value = 0;
if(spinbox != nullptr)
{
value = spinbox->value();
}
else
{
value = slider->value();
}
this->signal<signals::integer_changed_signal_t>(signals::INTEGER_CHANGED_SIG)
->async_emit(value, key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(value, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::INTEGER_CHANGED_SIG << "(" << value << ", " << key.toStdString()
<< ")"
);
}
else
{
int value1 = 0;
int value2 = 0;
if(spinbox != nullptr)
{
const QSpinBox* spin1 = _widget->property("widget#0").value<QSpinBox*>();
const QSpinBox* spin2 = _widget->property("widget#1").value<QSpinBox*>();
value1 = spin1->value();
value2 = spin2->value();
}
else
{
const QSlider* spin1 = _widget->property("widget#0").value<QSlider*>();
const QSlider* spin2 = _widget->property("widget#1").value<QSlider*>();
value1 = spin1->value();
value2 = spin2->value();
}
if(count == 2)
{
this->signal<signals::integer2_changed_signal_t>(signals::INTEGER2_CHANGED_SIG)
->async_emit(value1, value2, key.toStdString());
const sight::ui::int2_t values = {value1, value2};
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(values, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::INTEGER2_CHANGED_SIG << "(" << value1 << ", " << value2 << ", "
<< key.toStdString() << ")"
);
}
else
{
int value3 = 0;
if(spinbox != nullptr)
{
const QSpinBox* spin3 = _widget->property("widget#2").value<QSpinBox*>();
value3 = spin3->value();
}
else
{
const QSlider* spin3 = _widget->property("widget#2").value<QSlider*>();
value3 = spin3->value();
}
this->signal<signals::integer3_changed_signal_t>(signals::INTEGER3_CHANGED_SIG)
->async_emit(value1, value2, value3, key.toStdString());
const sight::ui::int3_t values = {value1, value2, value3};
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(values, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::INTEGER3_CHANGED_SIG << "(" << value1 << ", " << value2 << ", "
<< value3 << ", " << key.toStdString() << ")"
);
}
}
}
}
//-----------------------------------------------------------------------------
void parameters::on_change_double(double /*unused*/) const
{
QObject* sender = this->sender();
this->emit_double_signal(sender);
}
//------------------------------------------------------------------------------
void parameters::emit_double_signal(QObject* _widget) const
{
if(!m_block_signals)
{
const QString key = _widget->property("key").toString();
const int count = _widget->property("count").toInt();
auto* spinbox = qobject_cast<QDoubleSpinBox*>(_widget);
auto* slider = qobject_cast<QSlider*>(_widget);
if(slider != nullptr)
{
const double value = sight::module::ui::qt::parameters::get_double_slider_value(slider);
this->signal<signals::double_changed_signal_t>(signals::DOUBLE_CHANGED_SIG)
->async_emit(value, key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(value, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::DOUBLE_CHANGED_SIG << "(" << value << ", " << key.toStdString()
<< ")"
);
}
else if(spinbox != nullptr)
{
SIGHT_ASSERT(get_id() << ": Invalid widgets count, must be <= 3", count <= 3);
if(count == 1)
{
this->signal<signals::double_changed_signal_t>(signals::DOUBLE_CHANGED_SIG)
->async_emit(spinbox->value(), key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(spinbox->value(), key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::DOUBLE_CHANGED_SIG << "(" << spinbox->value() << ", "
<< key.toStdString() << ")"
);
}
else
{
const QDoubleSpinBox* spin1 = spinbox->property("widget#0").value<QDoubleSpinBox*>();
const QDoubleSpinBox* spin2 = spinbox->property("widget#1").value<QDoubleSpinBox*>();
const double value1 = spin1->value();
const double value2 = spin2->value();
if(count == 2)
{
this->signal<signals::double2_changed_signal_t>(signals::DOUBLE2_CHANGED_SIG)
->async_emit(value1, value2, key.toStdString());
const sight::ui::double2_t values = {value1, value2};
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(values, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::DOUBLE2_CHANGED_SIG << "(" << value1 << ", " << value2
<< ", "
<< key.toStdString() << ")"
);
}
else
{
const QDoubleSpinBox* spin3 = spinbox->property("widget#2").value<QDoubleSpinBox*>();
const double value3 = spin3->value();
this->signal<signals::double3_changed_signal_t>(signals::DOUBLE3_CHANGED_SIG)
->async_emit(value1, value2, value3, key.toStdString());
const sight::ui::double3_t values = {value1, value2, value3};
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(values, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::DOUBLE3_CHANGED_SIG << "(" << value1 << ", " << value2
<< ", "
<< value3 << ", " << key.toStdString() << ")"
);
}
}
}
}
}
//-----------------------------------------------------------------------------
void parameters::on_change_double_slider(int /*unused*/) const
{
auto* sender = qobject_cast<QSlider*>(this->sender());
this->emit_double_signal(sender);
}
//-----------------------------------------------------------------------------
void parameters::on_slider_mapped(QLabel* _label, QSlider* _slider)
{
_label->setText(QString::number(_slider->value()));
}
//-----------------------------------------------------------------------------
void parameters::on_slider_range_mapped(QLabel* _min_label, QLabel* _max_label, QSlider* _slider)
{
const int min = _slider->minimum();
const int max = _slider->maximum();
_min_label->setText(QString::number(min));
_max_label->setText(QString::number(max));
}
//-----------------------------------------------------------------------------
void parameters::on_double_slider_mapped(QLabel* _label, QSlider* _slider)
{
const double new_value = get_double_slider_value(_slider);
const int decimals = _slider->property("decimals").toInt();
_label->setText(QString::number(new_value, 'f', decimals));
}
//-----------------------------------------------------------------------------
void parameters::on_double_slider_range_mapped(QLabel* _min_label, QLabel* _max_label, QSlider* _slider)
{
const double min = _slider->property("min").toDouble();
const double max = _slider->property("max").toDouble();
const int decimals = _slider->property("decimals").toInt();
_min_label->setText(QString::number(min, 'g', decimals));
_max_label->setText(QString::number(max, 'g', decimals));
}
//-----------------------------------------------------------------------------
void parameters::on_reset_boolean_mapped(QWidget* _widget) const
{
auto* checkbox = qobject_cast<QCheckBox*>(_widget);
if(checkbox != nullptr)
{
int value = checkbox->property("defaultValue").toInt();
checkbox->setCheckState(Qt::CheckState(value));
const QString key = checkbox->property("key").toString();
if(!m_block_signals)
{
this->signal<signals::boolean_changed_signal_t>(signals::BOOLEAN_CHANGED_SIG)
->async_emit(value != 0, key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(value != 0, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::BOOLEAN_CHANGED_SIG << "(" << (value ? "true" : "false") << ", "
<< key.toStdString() << ")"
);
}
}
}
//-----------------------------------------------------------------------------
void parameters::on_reset_color_mapped(QWidget* _widget) const
{
auto* colour_button = qobject_cast<QPushButton*>(_widget);
if(colour_button != nullptr)
{
const auto color = colour_button->property("defaultValue").value<QColor>();
const QString key = colour_button->property("key").toString();
int icon_size = colour_button->style()->pixelMetric(QStyle::PM_LargeIconSize);
QPixmap pix(icon_size, icon_size);
pix.fill(color);
colour_button->setIcon(QIcon(pix));
colour_button->setProperty("color", color);
const std::array<std::uint8_t, 4> new_color =
{
static_cast<std::uint8_t>(color.red()),
static_cast<std::uint8_t>(color.green()),
static_cast<std::uint8_t>(color.blue()),
static_cast<std::uint8_t>(color.alpha())
};
if(!m_block_signals)
{
this->signal<signals::color_changed_signal_t>(signals::COLOR_CHANGED_SIG)
->async_emit(new_color, key.toStdString());
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(new_color, key.toStdString());
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::COLOR_CHANGED_SIG << "(" << int(new_color[0]) << ", "
<< int(new_color[1])
<< ", " << int(new_color[2]) << ", " << int(new_color[3]) << ", " << key.toStdString()
<< ")"
);
}
}
}
//-----------------------------------------------------------------------------
void parameters::on_reset_integer_mapped(QWidget* _widget)
{
auto* slider = dynamic_cast<QSlider*>(_widget);
auto* spinbox = dynamic_cast<QSpinBox*>(_widget);
this->blockSignals(true);
if(slider != nullptr)
{
const int value = slider->property("defaultValue").toInt();
slider->setValue(value);
}
else if(spinbox != nullptr)
{
const QString key = spinbox->property("key").toString();
const int value = spinbox->property("defaultValue").toInt();
const int count = spinbox->property("count").toInt();
SIGHT_ASSERT(get_id() << ": Invalid widgets count, must be <= 3", count <= 3);
auto* spin1 = spinbox->property("widget#0").value<QSpinBox*>();
spin1->setValue(value);
if(count > 1)
{
auto* spin2 = spinbox->property("widget#1").value<QSpinBox*>();
spin2->setValue(value);
if(count == 3)
{
auto* spin3 = spinbox->property("widget#2").value<QSpinBox*>();
spin3->setValue(value);
}
}
}
this->blockSignals(false);
this->emit_integer_signal(_widget);
}
//-----------------------------------------------------------------------------
void parameters::on_reset_double_mapped(QWidget* _widget)
{
auto* spinbox = qobject_cast<QDoubleSpinBox*>(_widget);
auto* slider = qobject_cast<QSlider*>(_widget);
this->blockSignals(true);
if(slider != nullptr)
{
const double value = slider->property("defaultValue").toDouble();
const double min = slider->property("min").toDouble();
const double max = slider->property("max").toDouble();
const double value_range = max - min;
const int slider_val = int(std::round(((value - min) / value_range) * double(slider->maximum())));
slider->setValue(slider_val);
}
else if(spinbox != nullptr)
{
const QString key = spinbox->property("key").toString();
const double value = spinbox->property("defaultValue").toDouble();
const int count = spinbox->property("count").toInt();
SIGHT_ASSERT(get_id() << ": Invalid widgets count, must be <= 3", count <= 3);
auto* spin1 = spinbox->property("widget#0").value<QDoubleSpinBox*>();
spin1->setValue(value);
if(count > 1)
{
auto* spin2 = spinbox->property("widget#1").value<QDoubleSpinBox*>();
spin2->setValue(value);
if(count == 3)
{
auto* spin3 = spinbox->property("widget#2").value<QDoubleSpinBox*>();
spin3->setValue(value);
}
}
}
this->blockSignals(false);
this->emit_double_signal(_widget);
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_reset_button(const std::string& _key, std::function<void()> _on_click) const
{
std::string service_id = base_id();
auto* reset_button = new QPushButton("R");
reset_button->setObjectName(QString::fromStdString(service_id + "/Reset " + _key));
reset_button->setFocusPolicy(Qt::NoFocus);
reset_button->setToolTip("Reset to the default value.");
reset_button->setMaximumWidth(20);
reset_button->setStyleSheet(qApp->styleSheet());
QObject::connect(reset_button, &QPushButton::clicked, this, _on_click);
return reset_button;
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_bool_widget(QBoxLayout& _layout, const param_widget& _setup) const
{
auto* checkbox = new QCheckBox();
{
const auto key = QString::fromStdString(_setup.key);
checkbox->setTristate(false);
if(_setup.default_value == "true")
{
checkbox->setCheckState(Qt::Checked);
}
checkbox->setObjectName(key);
checkbox->setProperty("key", key);
checkbox->setProperty("defaultValue", checkbox->checkState());
checkbox->setMinimumSize(_setup.min_size);
checkbox->setStyleSheet(qApp->styleSheet());
checkbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
_layout.addWidget(checkbox);
// Forward to the Sight signal
QObject::connect(checkbox, &QCheckBox::stateChanged, this, ¶meters::on_change_boolean);
// Reset button
if(_setup.reset_button)
{
return this->create_reset_button(_setup.key, [this, checkbox](){on_reset_boolean_mapped(checkbox);});
}
return nullptr;
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_color_widget(QBoxLayout& _layout, const param_widget& _setup) const
{
auto* colour_button = new QPushButton("Color");
{
colour_button->setObjectName(QString::fromStdString(_setup.key));
colour_button->setToolTip(tr("Selected color"));
std::string color_str = "#ffffffff";
if(!_setup.default_value.empty())
{
std::array<std::uint8_t, 4> color {};
data::tools::color::hexa_string_to_rgba(_setup.default_value, color);
color_str = _setup.default_value;
}
std::array<std::uint8_t, 4> color {};
data::tools::color::hexa_string_to_rgba(color_str, color);
const int icon_size = colour_button->style()->pixelMetric(QStyle::PM_LargeIconSize);
const QColor color_qt(color[0], color[1], color[2], color[3]);
QPixmap pix(icon_size, icon_size);
pix.fill(color_qt);
colour_button->setIcon(QIcon(pix));
colour_button->setProperty("key", QString::fromStdString(_setup.key));
colour_button->setProperty("defaultValue", color_qt);
colour_button->setProperty("color", color_qt);
colour_button->setMinimumSize(_setup.min_size);
colour_button->setStyleSheet(qApp->styleSheet());
colour_button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
_layout.addWidget(colour_button);
QObject::connect(colour_button, &QPushButton::clicked, this, ¶meters::on_color_button);
// Reset button
if(_setup.reset_button)
{
return this->create_reset_button(_setup.key, [this, colour_button]{on_reset_color_mapped(colour_button);});
}
return nullptr;
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_double_widget(
QBoxLayout& _layout,
const double_widget& _setup,
int _count,
Qt::Orientation _orientation
)
{
const auto layout_direction = _orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
auto* sub_layout = new QBoxLayout {layout_direction};
{
_layout.addLayout(sub_layout);
}
std::array<QDoubleSpinBox*, 3> spinboxes {};
// Spinboxes
for(std::size_t i = 0 ; i < std::size_t(_count) ; ++i)
{
auto* spinbox = new QDoubleSpinBox();
spinbox->setObjectName(QString::fromStdString(_setup.key + "/" + std::to_string(i)));
spinboxes[i] = spinbox;
auto count_decimals = [](double _num) -> int
{
std::stringstream out;
out << _num;
const std::string s = out.str();
const std::string t = s.substr(s.find('.') + 1);
return static_cast<int>(t.length());
};
spinbox->setDecimals(std::max(std::max(count_decimals(_setup.min), count_decimals(_setup.max)), 2));
spinbox->setRange(_setup.min, _setup.max);
// Beware, set setSingleStep after setRange() and setDecimals() otherwise it may fail
spinbox->setSingleStep(std::abs(spinbox->maximum() - spinbox->minimum()) / 100.);
spinbox->setMinimumSize(_setup.min_size);
// Set value last only after setting range and decimals, otherwise the value may be truncated
double initial_value = std::midpoint(_setup.min, _setup.max);
if(not _setup.default_value.empty())
{
try
{
initial_value = boost::lexical_cast<double>(_setup.default_value);
}
catch([[maybe_unused]] const boost::bad_lexical_cast& e)
{
SIGHT_ASSERT(
"Invalid value " << std::quoted(_setup.default_value) << ": not convertible to 'int/double': "
<< e.what(),
false
);
}
}
spinbox->setValue(initial_value);
spinbox->setProperty("key", QString::fromStdString(_setup.key));
spinbox->setProperty("count", _count);
spinbox->setProperty("defaultValue", spinbox->value());
spinbox->setStyleSheet(qApp->styleSheet());
spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sub_layout->addWidget(spinbox);
QObject::connect(
spinbox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
¶meters::on_change_double
);
}
QDoubleSpinBox* spinbox = spinboxes[0];
spinbox->setObjectName(QString::fromStdString(_setup.key));
// Set a property with a pointer on each member of the group
for(std::size_t i = 0 ; i < std::size_t(_count) ; ++i)
{
for(std::size_t j = 0 ; j < std::size_t(_count) ; ++j)
{
const std::string prop_name = std::string("widget#") + std::to_string(j);
spinboxes[i]->setProperty(prop_name.c_str(), QVariant::fromValue<QDoubleSpinBox*>(spinboxes[j]));
}
}
// Reset button
if(_setup.reset_button)
{
return this->create_reset_button(_setup.key, [this, spinbox]{on_reset_double_mapped(spinbox);});
}
return nullptr;
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_double_slider_widget(
QBoxLayout& _layout,
const double_widget& _setup,
std::uint8_t _decimals,
Qt::Orientation _orientation,
bool _on_release
)
{
const auto layout_direction = _orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
double initial_value = std::midpoint(_setup.min, _setup.max);
if(not _setup.default_value.empty())
{
try
{
initial_value = boost::lexical_cast<double>(_setup.default_value);
}
catch([[maybe_unused]] const boost::bad_lexical_cast& e)
{
SIGHT_ASSERT(
"Invalid value " << std::quoted(_setup.default_value) << ": not convertible to 'int/double': "
<< e.what(),
false
);
}
}
auto* const sub_layout = new QBoxLayout {layout_direction};
sub_layout->setSpacing(0);
sub_layout->setContentsMargins(0, 0, 0, 0);
_layout.addLayout(sub_layout);
auto* const slider = new QSlider();
{
const double value_range = _setup.max - _setup.min;
slider->setOrientation(_orientation);
slider->setObjectName(QString::fromStdString(_setup.key));
slider->setProperty("key", slider->objectName());
slider->setProperty("count", 1);
slider->setProperty("defaultValue", initial_value);
slider->setProperty("decimals", _decimals);
slider->setProperty("min", _setup.min);
slider->setProperty("max", _setup.max);
// tracking true: emit signal when value change, false: emit signal when slider released.
slider->setTracking(!_on_release);
set_double_slider_range(slider, initial_value);
const int default_slider_value =
int(std::round(((initial_value - _setup.min) / value_range) * double(slider->maximum())));
slider->setValue(default_slider_value);
slider->setMinimumSize(_setup.min_size);
slider->setProperty("widget#0", QVariant::fromValue<QSlider*>(slider));
slider->setStyleSheet(qApp->styleSheet());
slider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}
QFont min_max_labels_font;
min_max_labels_font.setPointSize(7);
min_max_labels_font.setItalic(true);
auto* const min_value_label = new QLabel();
min_value_label->setFont(min_max_labels_font);
min_value_label->setText(QString::number(_setup.min, 'g', _decimals));
min_value_label->setToolTip("Minimum value.");
min_value_label->setObjectName(QString::fromStdString(_setup.key + "/minValueLabel"));
min_value_label->setAlignment(Qt::AlignCenter);
min_value_label->setStyleSheet(qApp->styleSheet());
auto* const max_value_label = new QLabel();
max_value_label->setFont(min_max_labels_font);
max_value_label->setText(QString::number(_setup.max, 'g', _decimals));
max_value_label->setToolTip("Maximum value.");
max_value_label->setObjectName(QString::fromStdString(_setup.key + "/maxValueLabel"));
max_value_label->setAlignment(Qt::AlignCenter);
max_value_label->setStyleSheet(qApp->styleSheet());
auto* const value_label = new QLabel();
value_label->setStyleSheet("QLabel { font: bold; }");
value_label->setText(QString::number(initial_value, 'f', _decimals));
value_label->setToolTip("Current value.");
sight::module::ui::qt::parameters::set_label_minimum_size(value_label, _setup.min, _setup.max, _decimals);
value_label->setObjectName(QString::fromStdString(_setup.key + "/valueLabel"));
value_label->setAlignment(Qt::AlignCenter);
value_label->setStyleSheet(qApp->styleSheet());
// Slots
{
// Forward to the corresponding Sight signal
QObject::connect(slider, &QSlider::valueChanged, this, ¶meters::on_change_double_slider);
// Update the labels when the value or the range changes
QObject::connect(
slider,
&QSlider::valueChanged,
this,
[value_label, slider]
{
sight::module::ui::qt::parameters::on_double_slider_mapped(value_label, slider);
});
QObject::connect(
slider,
&QSlider::rangeChanged,
this,
[ = ]{on_double_slider_range_mapped(min_value_label, max_value_label, slider);});
}
// Sub layout setup
{
const auto alignment = _orientation == Qt::Vertical ? Qt::AlignCenter : Qt::Alignment {};
auto* const first_label = _orientation == Qt::Vertical ? max_value_label : min_value_label;
auto* const second_label = _orientation == Qt::Vertical ? min_value_label : max_value_label;
sub_layout->addWidget(first_label, /*scretch=*/ 0, alignment);
sub_layout->addWidget(slider, /*scretch=*/ 0, alignment);
sub_layout->addWidget(second_label, /*scretch=*/ 0, alignment);
sub_layout->addWidget(value_label, /*scretch=*/ 0, alignment);
sub_layout->setAlignment(alignment);
if(_setup.hide_min_max)
{
min_value_label->hide();
max_value_label->hide();
}
}
// Reset button
if(_setup.reset_button)
{
return this->create_reset_button(_setup.key, [this, slider]{on_reset_double_mapped(slider);});
}
return nullptr;
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_integer_slider_widget(
QBoxLayout& _layout,
const int_widget& _setup,
Qt::Orientation _orientation,
bool _on_release
)
{
int initial_value = std::midpoint(_setup.min, _setup.max);
if(not _setup.default_value.empty())
{
try
{
initial_value = boost::lexical_cast<int>(_setup.default_value);
}
catch([[maybe_unused]] const boost::bad_lexical_cast& e)
{
SIGHT_ASSERT(
"Invalid value " << std::quoted(_setup.default_value) << ": not convertible to 'int/double': "
<< e.what(),
false
);
}
}
const auto layout_direction = _orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
auto* sub_layout = new QBoxLayout {layout_direction};
_layout.addLayout(sub_layout);
sub_layout->setSpacing(0);
sub_layout->setContentsMargins(0, 0, 0, 0);
auto* slider = new QSlider();
slider->setOrientation(_orientation);
slider->setObjectName(QString::fromStdString(_setup.key));
slider->setMinimum(_setup.min);
slider->setMaximum(_setup.max);
slider->setValue(initial_value);
slider->setMinimumSize(_setup.min_size);
// tracking true: emit signal when value change, false: emit signal when slider released.
slider->setTracking(!_on_release);
slider->setProperty("key", QString::fromStdString(_setup.key));
slider->setProperty("count", 1);
slider->setProperty("defaultValue", slider->value());
slider->setProperty("widget#0", QVariant::fromValue<QSlider*>(slider));
slider->setStyleSheet(qApp->styleSheet());
slider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QFont min_max_labels_font;
min_max_labels_font.setPointSize(7);
min_max_labels_font.setItalic(true);
auto* min_value_label = new QLabel();
min_value_label->setFont(min_max_labels_font);
min_value_label->setText(QString::number(slider->minimum()));
min_value_label->setToolTip("Minimum value.");
min_value_label->setObjectName(QString::fromStdString(_setup.key + "/minValueLabel"));
min_value_label->setAlignment(Qt::AlignCenter);
min_value_label->setStyleSheet(qApp->styleSheet());
auto* max_value_label = new QLabel();
max_value_label->setFont(min_max_labels_font);
max_value_label->setText(QString::number(slider->maximum()));
max_value_label->setToolTip("Maximum value.");
max_value_label->setObjectName(QString::fromStdString(_setup.key + "/maxValueLabel"));
max_value_label->setAlignment(Qt::AlignCenter);
max_value_label->setStyleSheet(qApp->styleSheet());
auto* value_label = new QLabel();
value_label->setStyleSheet("QLabel { font: bold; }");
value_label->setText(QString("%1").arg(slider->value()));
value_label->setToolTip("Current value.");
sight::module::ui::qt::parameters::set_label_minimum_size(value_label, _setup.min, _setup.max);
value_label->setObjectName(QString::fromStdString(_setup.key + "/valueLabel"));
value_label->setAlignment(Qt::AlignCenter);
value_label->setStyleSheet(qApp->styleSheet());
// Sub layout setup
{
const auto alignment = _orientation == Qt::Vertical ? Qt::AlignCenter : Qt::Alignment {};
auto* const first_label = _orientation == Qt::Vertical ? max_value_label : min_value_label;
auto* const second_label = _orientation == Qt::Vertical ? min_value_label : max_value_label;
sub_layout->addWidget(first_label, /*scretch=*/ 0, alignment);
sub_layout->addWidget(slider, /*scretch=*/ 0, alignment);
sub_layout->addWidget(second_label, /*scretch=*/ 0, alignment);
sub_layout->addWidget(value_label, /*scretch=*/ 0, alignment);
sub_layout->setAlignment(alignment);
if(_setup.hide_min_max)
{
min_value_label->hide();
max_value_label->hide();
}
}
// Connections
{
// Forward to the corresponding Sight signal
QObject::connect(slider, &QSlider::valueChanged, this, ¶meters::on_change_integer);
// Update the labels when the value or the range changes
QObject::connect(
slider,
&QSlider::valueChanged,
this,
[value_label, slider]{sight::module::ui::qt::parameters::on_slider_mapped(value_label, slider);});
QObject::connect(
slider,
&QSlider::rangeChanged,
this,
[ = ]{on_slider_range_mapped(min_value_label, max_value_label, slider);});
}
// Reset button
if(_setup.reset_button)
{
return this->create_reset_button(_setup.key, [this, slider]{on_reset_integer_mapped(slider);});
}
return nullptr;
}
//-----------------------------------------------------------------------------
[[nodiscard]]
QPushButton* parameters::create_integer_spin_widget(
QBoxLayout& _layout,
const int_widget& _setup,
int _count,
Qt::Orientation _orientation
)
{
int initial_value = std::midpoint(_setup.min, _setup.max);
if(not _setup.default_value.empty())
{
try
{
initial_value = boost::lexical_cast<int>(_setup.default_value);
}
catch([[maybe_unused]] const boost::bad_lexical_cast& e)
{
SIGHT_ASSERT(
"Invalid value " << std::quoted(_setup.default_value) << ": not convertible to 'int/double': "
<< e.what(),
false
);
}
}
const auto layout_direction = _orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
auto* sub_layout = new QBoxLayout {layout_direction};
_layout.addLayout(sub_layout);
std::array<QSpinBox*, 3> spinboxes {};
// Spinboxes
for(std::size_t i = 0 ; i < std::size_t(_count) ; ++i)
{
auto* spinbox = new QSpinBox();
spinboxes[i] = spinbox;
spinbox->setObjectName(QString::fromStdString(_setup.key + "/" + std::to_string(i)));
spinbox->setMinimum(_setup.min);
spinbox->setMaximum(_setup.max);
spinbox->setValue(initial_value);
spinbox->setMinimumSize(_setup.min_size);
spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
spinbox->setProperty("key", QString::fromStdString(_setup.key));
spinbox->setProperty("count", _count);
spinbox->setProperty("defaultValue", spinbox->value());
spinbox->setStyleSheet(qApp->styleSheet());
spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
sub_layout->addWidget(spinbox);
// Connect spinbox value with our editor
QObject::connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), this, ¶meters::on_change_integer);
}
QSpinBox* first_spinbox = spinboxes[0];
first_spinbox->setObjectName(QString::fromStdString(_setup.key));
// Set a property with a pointer on each member of the group
for(std::size_t i = 0 ; i < std::size_t(_count) ; ++i)
{
for(std::size_t j = 0 ; j < std::size_t(_count) ; ++j)
{
const std::string prop_name = std::string("widget#") + std::to_string(j);
spinboxes[i]->setProperty(prop_name.c_str(), QVariant::fromValue<QSpinBox*>(spinboxes[j]));
}
}
// Reset button
if(_setup.reset_button)
{
return this->create_reset_button(
_setup.key,
[this, first_spinbox](){on_reset_integer_mapped(first_spinbox);});
}
return nullptr;
}
//-----------------------------------------------------------------------------
void parameters::update_enum_list(const std::vector<std::string>& _list, const std::string _key)
{
std::vector<std::string> labels;
std::vector<std::string> keys;
for(const auto& s : _list)
{
const auto [left_part, right_part] = split_string(s);
labels.push_back(left_part);
if(!right_part.empty())
{
keys.push_back(right_part);
}
else
{
keys.push_back(left_part);
}
}
this->blockSignals(true);
QObject* widget = this->get_param_widget(_key);
auto* combobox = qobject_cast<QComboBox*>(widget);
if(combobox != nullptr)
{
combobox->clear();
int idx = 0;
for(const auto& l : labels)
{
combobox->insertItem(idx, QString::fromStdString(l));
++idx;
}
// Add optional data
idx = 0;
for(const auto& k : keys)
{
combobox->setItemData(idx, QString::fromStdString(k));
++idx;
}
}
else if(auto* non_linear_slider = qobject_cast<sight::ui::qt::widget::non_linear_slider*>(widget);
non_linear_slider != nullptr)
{
std::vector<int> values;
values.reserve(labels.size());
std::ranges::transform(labels, std::back_inserter(values), [](const std::string& _s){return std::stoi(_s);});
non_linear_slider->set_values(values);
non_linear_slider->set_value(values[0]);
}
this->blockSignals(false);
}
//-----------------------------------------------------------------------------
void parameters::parse_enum_string(
const std::string& _options,
std::vector<std::string>& _labels,
std::vector<std::string>& _keys,
std::string _separators
)
{
const boost::char_separator<char> sep(_separators.c_str());
const boost::tokenizer<boost::char_separator<char> > tokens {_options, sep};
for(const auto& token : tokens)
{
const auto [left_part, right_part] = split_string(token);
_labels.push_back(left_part);
if(!right_part.empty())
{
_keys.push_back(right_part);
}
else
{
_keys.push_back(left_part);
}
}
}
//------------------------------------------------------------------------------
void parameters::create_enum_widget(
QBoxLayout& _layout,
const param_widget& _setup,
const std::vector<std::string>& _values,
const std::vector<std::string>& _data
) const
{
auto* menu = new QComboBox();
menu->setObjectName(QString::fromStdString(_setup.key));
menu->setStyleSheet(qApp->styleSheet());
menu->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
menu->setProperty("key", QString::fromStdString(_setup.key));
int idx = 0;
for(const auto& value : _values)
{
menu->insertItem(idx, QString::fromStdString(value));
++idx;
}
// Add optional data
idx = 0;
for(const auto& choice : _data)
{
menu->setItemData(idx, QString::fromStdString(choice));
++idx;
}
_layout.addWidget(menu);
QObject::connect(menu, QOverload<int>::of(&QComboBox::currentIndexChanged), this, ¶meters::on_change_enum);
// Set the comboBox to the default value
menu->setCurrentText(QString::fromStdString(_setup.default_value));
menu->setMinimumSize(_setup.min_size);
}
//------------------------------------------------------------------------------
void parameters::create_slider_enum_widget(
QBoxLayout& _layout,
const param_widget& _setup,
const std::vector<std::string>& _values,
Qt::Orientation _orientation,
bool _on_release
) const
{
const auto layout_direction = _orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
auto* sub_layout = new QBoxLayout {layout_direction};
{
_layout.addLayout(sub_layout);
}
std::vector<int> int_values;
std::ranges::transform(
_values,
std::back_inserter(int_values),
[](const std::string& _s){return std::stoi(_s);});
auto* const slider = new sight::ui::qt::widget::non_linear_slider();
slider->set_orientation(_orientation);
slider->setMinimumSize(_setup.min_size);
slider->setObjectName(QString::fromStdString(_setup.key));
slider->setProperty("key", QString::fromStdString(_setup.key));
slider->set_values(int_values);
slider->set_value(std::stoi(_setup.default_value));
slider->set_tracking(!_on_release);
slider->setStyleSheet(qApp->styleSheet());
slider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QFont min_max_labels_font;
min_max_labels_font.setPointSize(7);
min_max_labels_font.setItalic(true);
auto* const min_value_label = new QLabel();
min_value_label->setFont(min_max_labels_font);
min_value_label->setText(QString::fromStdString(_values.front()));
min_value_label->setToolTip("Minimum value.");
min_value_label->setObjectName(QString::fromStdString(_setup.key + "/minValueLabel"));
min_value_label->setAlignment(Qt::AlignCenter);
min_value_label->setStyleSheet(qApp->styleSheet());
auto* const max_value_label = new QLabel();
max_value_label->setFont(min_max_labels_font);
max_value_label->setText(QString::fromStdString(_values.back()));
max_value_label->setToolTip("Maximum value.");
max_value_label->setObjectName(QString::fromStdString(_setup.key + "/maxValueLabel"));
max_value_label->setAlignment(Qt::AlignCenter);
max_value_label->setStyleSheet(qApp->styleSheet());
auto* value_label = new QLabel();
value_label->setStyleSheet("QLabel { font: bold; }");
value_label->setText(QString::number(slider->value()));
value_label->setToolTip("Current value.");
set_label_minimum_size(value_label, int_values.front(), int_values.back());
value_label->setObjectName(QString::fromStdString(_setup.key + "/valueLabel"));
value_label->setAlignment(Qt::AlignCenter);
// Sub layout setup
{
const auto alignment = _orientation == Qt::Vertical ? Qt::AlignCenter : Qt::Alignment {};
auto* const first_label = _orientation == Qt::Vertical ? max_value_label : min_value_label;
auto* const second_label = _orientation == Qt::Vertical ? min_value_label : max_value_label;
sub_layout->addWidget(first_label, /*scretch=*/ 0, alignment);
sub_layout->addWidget(slider, /*stretch=*/ 0, alignment);
sub_layout->addWidget(second_label, /*scretch=*/ 0, alignment);
sub_layout->addWidget(value_label, /*scretch=*/ 0, alignment);
if(_setup.hide_min_max)
{
min_value_label->hide();
max_value_label->hide();
}
}
// Connections
{
// Forward to the Sight signal
QObject::connect(
slider,
&sight::ui::qt::widget::non_linear_slider::value_changed,
[this, key = _setup.key, slider, value_label](int _value)
{
if(!m_block_signals)
{
this->signal<signals::integer_changed_signal_t>(signals::INTEGER_CHANGED_SIG)
->async_emit(_value, key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)->async_emit(_value, key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::INTEGER_CHANGED_SIG << "(" << _value << ", "
<< key << ")"
);
this->signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG)
->async_emit(std::to_string(_value), key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(std::to_string(_value), key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_CHANGED_SIG << "(" << _value << ", " << key
<< ")"
);
this->signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG)
->async_emit(int(slider->index()), key);
}
value_label->setText(QString::number(_value));
});
// Update the labels
QObject::connect(
slider,
&sight::ui::qt::widget::non_linear_slider::range_changed,
[ = ](int _min, int _max)
{
min_value_label->setText(QString::number(_min));
max_value_label->setText(QString::number(_max));
});
}
}
//-----------------------------------------------------------------------------
void parameters::create_button_bar_enum_widget(
QBoxLayout& _layout,
const param_widget& _setup,
const std::vector<enum_button_param>& _button_list,
const int _width,
const int _height,
const int _spacing,
const std::string& _style,
Qt::Orientation _orientation
) const
{
const auto layout_direction = _orientation == Qt::Vertical ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight;
auto* sub_layout = new QBoxLayout {layout_direction};
_layout.addLayout(sub_layout);
if(_spacing != 0)
{
sub_layout->setSpacing(_spacing);
}
// create a button group to deactivate the buttons on selection
auto* button_bar_group = new QButtonGroup(sub_layout);
button_bar_group->setObjectName(QString::fromStdString(_setup.key));
// create the buttons from the provided list
int button_index = 0;
for(const auto& button_param : _button_list)
{
auto* enum_button = new QToolButton();
button_bar_group->addButton(enum_button);
// The name needs to be the ky_value, to find it when the service is updated through a slot
enum_button->setObjectName((QString::fromStdString(_setup.key + "_" + button_param.value)));
enum_button->setIcon(QIcon(QString::fromStdString(button_param.icon_path)));
enum_button->setToolTip(QString::fromStdString(button_param.label));
enum_button->setCheckable(true);
enum_button->setProperty("class", "buttonBar");
enum_button->setProperty("value", QString::fromStdString(button_param.value));
enum_button->setText(QString::fromStdString(button_param.label));
enum_button->setStyleSheet(qApp->styleSheet());
enum_button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
if(_style == "ToolButtonTextOnly")
{
enum_button->setToolButtonStyle(Qt::ToolButtonTextOnly);
}
else if(_style == "ToolButtonTextBesideIcon")
{
enum_button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
}
else if(_style == "ToolButtonTextUnderIcon")
{
enum_button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
}
else if(_style == "ToolButtonFollowStyle")
{
enum_button->setToolButtonStyle(Qt::ToolButtonFollowStyle);
}
else
{
enum_button->setToolButtonStyle(Qt::ToolButtonIconOnly);
}
// create an effect to make it gray when not selected, and full color when selected
auto* effect = new QGraphicsColorizeEffect;
effect->setColor(QColor(10, 10, 10));
effect->setStrength(0.7);
enum_button->setGraphicsEffect(effect);
// the size depends on the configuration. xml > qss
if(_width != 0 || _height != 0)
{
// the size is provided through the xml config. Don't use the qss and ignore _setup.min_size
const int width = _width == 0 ? _height : _width;
const int height = _height == 0 ? _width : _height;
enum_button->setIconSize({width, height});
}
else
{
// the size is not provided through the xml. Use the qss style.
enum_button->setProperty("class", "buttonBarTouchFriendly");
}
// add the button in the grid at its place
sub_layout->addWidget(enum_button);
// create the connection to fire signals when the button is clicked
QObject::connect(
enum_button,
&QToolButton::clicked,
[this, button_param, key = _setup.key, button_index]
{
if(!m_block_signals)
{
this->signal<signals::enum_changed_signal_t>(signals::ENUM_CHANGED_SIG)
->async_emit(button_param.value, key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)
->async_emit(button_param.value, key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::ENUM_CHANGED_SIG << "(" << button_param.value << ", "
<< key << ")"
);
this->signal<signals::enum_changed_index_signal_t>(signals::ENUM_INDEX_CHANGED_SIG)
->async_emit(button_index, key);
}
});
// create connection to change the display (gray/full color) when the selection state changes
QObject::connect(
enum_button,
&QAbstractButton::toggled,
[ = ](bool _checked)
{
QGraphicsEffect* effect = enum_button->graphicsEffect();
effect->setEnabled(!_checked);
});
// set the default value
if(button_param.value == _setup.default_value)
{
enum_button->toggle();
}
++button_index;
}
}
//-----------------------------------------------------------------------------
double parameters::get_double_slider_value(const QSlider* _slider)
{
const double min = _slider->property("min").toDouble();
const double max = _slider->property("max").toDouble();
const double value_range = max - min;
double double_value = min;
if(_slider->maximum() != 0)
{
double_value = (double(_slider->value()) / _slider->maximum()) * value_range + min;
}
return double_value;
}
//------------------------------------------------------------------------------
void parameters::set_parameter(sight::ui::parameter_t _val, std::string _key)
{
this->blockSignals(true);
if(std::holds_alternative<bool>(_val))
{
this->set_bool_parameter(std::get<bool>(_val), _key);
}
else if(std::holds_alternative<sight::ui::color_t>(_val))
{
this->set_color_parameter(std::get<sight::ui::color_t>(_val), _key);
}
else if(std::holds_alternative<double>(_val))
{
this->set_double_parameter(std::get<double>(_val), _key);
}
else if(std::holds_alternative<sight::ui::double2_t>(_val))
{
const auto values = std::get<sight::ui::double2_t>(_val);
this->set_double2_parameter(values[0], values[1], _key);
}
else if(std::holds_alternative<sight::ui::double3_t>(_val))
{
const auto values = std::get<sight::ui::double3_t>(_val);
this->set_double3_parameter(values[0], values[1], values[2], _key);
}
else if(std::holds_alternative<sight::ui::int2_t>(_val))
{
const auto values = std::get<sight::ui::int2_t>(_val);
this->set_int2_parameter(values[0], values[1], _key);
}
else if(std::holds_alternative<sight::ui::int3_t>(_val))
{
const auto values = std::get<sight::ui::int3_t>(_val);
this->set_int3_parameter(values[0], values[1], values[2], _key);
}
else if(std::holds_alternative<std::string>(_val))
{
this->set_enum_parameter(std::get<std::string>(_val), _key);
}
else if(std::holds_alternative<int>(_val))
{
// This can be either an int or a enum index
// Solve the ambiguity by testing the widget type
QObject* child = this->get_param_widget(_key);
auto* spinbox = qobject_cast<QSpinBox*>(child);
auto* slider = qobject_cast<QSlider*>(child);
auto* non_linear_slider = qobject_cast<sight::ui::qt::widget::non_linear_slider*>(child);
if(spinbox != nullptr || slider != nullptr || non_linear_slider != nullptr)
{
this->set_int_parameter(std::get<int>(_val), _key);
}
else
{
this->set_enum_index_parameter(std::get<int>(_val), _key);
}
}
else if(std::holds_alternative<sight::ui::enum_list_t>(_val))
{
this->update_enum_list(std::get<sight::ui::enum_list_t>(_val), _key);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_bool_parameter(bool _val, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
auto* checkbox = qobject_cast<QCheckBox*>(child);
if(checkbox != nullptr)
{
checkbox->setCheckState(_val ? Qt::Checked : Qt::Unchecked);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_color_parameter(std::array<std::uint8_t, 4> _color, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
auto* color_button = qobject_cast<QPushButton*>(child);
if(color_button != nullptr)
{
const int icon_size = color_button->style()->pixelMetric(QStyle::PM_LargeIconSize);
QPixmap pix(icon_size, icon_size);
QColor color_qt(_color[0], _color[1], _color[2], _color[3]);
pix.fill(color_qt);
color_button->setIcon(QIcon(pix));
color_button->setProperty("color", color_qt);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_double_parameter(double _val, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
if(auto* spinbox = qobject_cast<QDoubleSpinBox*>(child); spinbox != nullptr)
{
spinbox->setValue(_val);
}
else if(auto* slider = qobject_cast<QSlider*>(child); slider != nullptr)
{
const double min = slider->property("min").toDouble();
const double max = slider->property("max").toDouble();
const double value_range = max - min;
const int slider_val = int(std::round(((_val - min) / value_range) * double(slider->maximum())));
slider->setValue(slider_val);
}
else
{
SIGHT_ERROR(get_id() << ": Widget " << std::quoted(_key) << " must be a QSlider or a QDoubleSpinBox");
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_double2_parameter(double _val0, double _val1, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
if(child != nullptr)
{
auto* spin0 = child->property("widget#0").value<QDoubleSpinBox*>();
auto* spin1 = child->property("widget#1").value<QDoubleSpinBox*>();
spin0->setValue(_val0);
spin1->setValue(_val1);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_double3_parameter(double _val0, double _val1, double _val2, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
if(child != nullptr)
{
auto* spin0 = child->property("widget#0").value<QDoubleSpinBox*>();
auto* spin1 = child->property("widget#1").value<QDoubleSpinBox*>();
auto* spin2 = child->property("widget#2").value<QDoubleSpinBox*>();
spin0->setValue(_val0);
spin1->setValue(_val1);
spin2->setValue(_val2);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_int_parameter(int _val, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
auto* spinbox = qobject_cast<QSpinBox*>(child);
auto* slider = qobject_cast<QSlider*>(child);
if(spinbox != nullptr)
{
spinbox->setValue(_val);
}
else if(slider != nullptr)
{
slider->setValue(_val);
}
else if(auto* non_linear_slider = qobject_cast<sight::ui::qt::widget::non_linear_slider*>(child);
non_linear_slider != nullptr)
{
non_linear_slider->set_value(_val);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_int2_parameter(int _val0, int _val1, std::string _key)
{
this->blockSignals(true);
QObject* child = this->get_param_widget(_key);
if(child != nullptr)
{
auto* spin0 = child->property("widget#0").value<QSpinBox*>();
auto* spin1 = child->property("widget#1").value<QSpinBox*>();
spin0->setValue(_val0);
spin1->setValue(_val1);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_int3_parameter(int _val0, int _val1, int _val2, std::string _key)
{
this->blockSignals(true);
QObject* widget = this->get_param_widget(_key);
if(widget != nullptr)
{
auto* spin0 = widget->property("widget#0").value<QSpinBox*>();
auto* spin1 = widget->property("widget#1").value<QSpinBox*>();
auto* spin2 = widget->property("widget#2").value<QSpinBox*>();
spin0->setValue(_val0);
spin1->setValue(_val1);
spin2->setValue(_val2);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_enum_parameter(std::string _val, std::string _key)
{
this->blockSignals(true);
QObject* widget = this->get_param_widget(_key);
auto* combobox = qobject_cast<QComboBox*>(widget);
if(combobox != nullptr)
{
// Find first in text
auto res = combobox->findText(QString::fromStdString(_val));
if(res == -1)
{
// fallback, try to find in optional data.
res = combobox->findData(QString::fromStdString(_val));
}
if(res >= 0)
{
combobox->setCurrentIndex(res);
}
else
{
SIGHT_WARN(
get_id() << "value " << std::quoted(_val) << "doesn't exist for parameter " << std::quoted(
_key
) << "."
);
}
}
else if(auto* non_linear_slider = qobject_cast<sight::ui::qt::widget::non_linear_slider*>(widget);
non_linear_slider != nullptr)
{
non_linear_slider->set_value(std::stoi(_val));
}
else if(auto* button_group = qobject_cast<QButtonGroup*>(widget);
button_group != nullptr)
{
const auto enum_key = QString::fromStdString(_key + "_" + _val);
const auto& buttons = button_group->buttons();
auto button_it = std::ranges::find_if(
buttons,
[&enum_key](const auto* const _b)
{
return _b->objectName() == enum_key;
});
SIGHT_ASSERT(
get_id() << ": Value " << std::quoted(_val) << " does not appear in parameter widget "
<< std::quoted(_key),
button_it != buttons.cend()
);
(*button_it)->toggle();
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::set_enum_index_parameter(int _val, std::string _key)
{
this->blockSignals(true);
QObject* widget = this->get_param_widget(_key);
auto* combobox = qobject_cast<QComboBox*>(widget);
if(combobox != nullptr)
{
combobox->setCurrentIndex(_val);
}
this->blockSignals(false);
}
//------------------------------------------------------------------------------
void parameters::update_enum_range(std::string _options, std::string _key)
{
this->blockSignals(true);
QObject* widget = this->get_param_widget(_key);
auto* combobox = qobject_cast<QComboBox*>(widget);
if(combobox != nullptr)
{
combobox->clear();
std::vector<std::string> values;
std::vector<std::string> data;
sight::module::ui::qt::parameters::parse_enum_string(_options, values, data);
int idx = 0;
for(const auto& value : values)
{
combobox->insertItem(idx, QString::fromStdString(value));
++idx;
}
// Add optional data
idx = 0;
for(const auto& choice : data)
{
combobox->setItemData(idx, QString::fromStdString(choice));
++idx;
}
}
this->blockSignals(false);
}
//-----------------------------------------------------------------------------
void parameters::emit_color_signal(const QColor _color, const std::string& _key) const
{
const std::array<std::uint8_t, 4> new_color = {{static_cast<std::uint8_t>(_color.red()),
static_cast<std::uint8_t>(_color.green()),
static_cast<std::uint8_t>(_color.blue()),
static_cast<std::uint8_t>(_color.alpha())
}
};
if(!m_block_signals)
{
this->signal<signals::color_changed_signal_t>(signals::COLOR_CHANGED_SIG)->async_emit(new_color, _key);
this->signal<signals::changed_signal_t>(signals::PARAMETER_CHANGED_SIG)->async_emit(new_color, _key);
SIGHT_DEBUG(
get_id() << ": [EMIT] " << signals::COLOR_CHANGED_SIG << "(" << int(new_color[0]) << ", "
<< int(new_color[1]) << ", " << int(new_color[2]) << ", " << int(new_color[3]) << ", " << _key << ")"
);
}
}
//-----------------------------------------------------------------------------
void parameters::blockSignals(bool _block)
{
m_block_signals = _block;
}
//------------------------------------------------------------------------------
void parameters::update_int_min_parameter(int _min, std::string _key)
{
QObject* child = this->get_param_widget(_key);
auto* spinbox = qobject_cast<QSpinBox*>(child);
auto* slider = qobject_cast<QSlider*>(child);
if(spinbox != nullptr)
{
const int count = child->property("count").toInt();
auto* spin0 = child->property("widget#0").value<QSpinBox*>();
spin0->setMinimum(_min);
if(count >= 2)
{
auto* spin1 = child->property("widget#1").value<QSpinBox*>();
spin1->setMinimum(_min);
}
if(count >= 3)
{
auto* spin2 = child->property("widget#2").value<QSpinBox*>();
spin2->setMinimum(_min);
}
}
else if(slider != nullptr)
{
slider->setMinimum(_min);
}
else
{
SIGHT_ERROR(get_id() << ": Widget " << std::quoted(_key) << " must be a QSlider or a QDoubleSpinBox");
}
}
//------------------------------------------------------------------------------
void parameters::update_int_max_parameter(int _max, std::string _key)
{
QObject* child = this->get_param_widget(_key);
auto* spinbox = qobject_cast<QSpinBox*>(child);
auto* slider = qobject_cast<QSlider*>(child);
if(spinbox != nullptr)
{
const int count = child->property("count").toInt();
auto* spin0 = child->property("widget#0").value<QSpinBox*>();
spin0->setMaximum(_max);
if(count >= 2)
{
auto* spin1 = child->property("widget#1").value<QSpinBox*>();
spin1->setMaximum(_max);
}
if(count >= 3)
{
auto* spin2 = child->property("widget#2").value<QSpinBox*>();
spin2->setMaximum(_max);
}
}
else if(slider != nullptr)
{
slider->setMaximum(_max);
}
else
{
SIGHT_ERROR(get_id() << ": Widget " << std::quoted(_key) << " must be a QSlider or a QDoubleSpinBox");
}
}
//------------------------------------------------------------------------------
void parameters::update_double_min_parameter(double _min, std::string _key)
{
QObject* child = this->get_param_widget(_key);
auto* spinbox = qobject_cast<QDoubleSpinBox*>(child);
auto* slider = qobject_cast<QSlider*>(child);
if(spinbox != nullptr)
{
const int count = child->property("count").toInt();
auto* spin0 = child->property("widget#0").value<QDoubleSpinBox*>();
spin0->setMinimum(_min);
if(count >= 2)
{
auto* spin1 = child->property("widget#1").value<QDoubleSpinBox*>();
spin1->setMinimum(_min);
}
if(count >= 3)
{
auto* spin2 = child->property("widget#2").value<QDoubleSpinBox*>();
spin2->setMinimum(_min);
}
}
else if(slider != nullptr)
{
const double value = get_double_slider_value(slider);
slider->setProperty("min", _min);
set_double_slider_range(slider, value);
}
else
{
SIGHT_ERROR(get_id() << ": Widget " << std::quoted(_key) << " must be a QSlider or a QDoubleSpinBox");
}
}
//------------------------------------------------------------------------------
void parameters::update_double_max_parameter(double _max, std::string _key)
{
QObject* child = this->get_param_widget(_key);
auto* spinbox = qobject_cast<QDoubleSpinBox*>(child);
auto* slider = qobject_cast<QSlider*>(child);
if(spinbox != nullptr)
{
const int count = child->property("count").toInt();
auto* spin0 = child->property("widget#0").value<QDoubleSpinBox*>();
spin0->setMaximum(_max);
if(count >= 2)
{
auto* spin1 = child->property("widget#1").value<QDoubleSpinBox*>();
spin1->setMaximum(_max);
}
if(count >= 3)
{
auto* spin2 = child->property("widget#2").value<QDoubleSpinBox*>();
spin2->setMaximum(_max);
}
}
else if(slider != nullptr)
{
const double value = get_double_slider_value(slider);
slider->setProperty("max", _max);
set_double_slider_range(slider, value);
}
else
{
SIGHT_ERROR(get_id() << ": Widget " << std::quoted(_key) << " must be a QSlider or a QDoubleSpinBox");
}
}
//-----------------------------------------------------------------------------
void parameters::set_double_slider_range(QSlider* _slider, double _current_value)
{
const std::string key = _slider->property("key").toString().toStdString();
const double min = _slider->property("min").toDouble();
const double max = _slider->property("max").toDouble();
const std::uint8_t decimals = static_cast<std::uint8_t>(_slider->property("decimals").toUInt());
int max_slider_value = 1;
for(std::uint8_t i = 0 ; i < decimals ; ++i)
{
max_slider_value *= 10;
}
const double value_range = max - min;
max_slider_value = int(max_slider_value * value_range);
// The slider's maximum internal range is [0; 2 147 483 647]
// We could technically extend this range by setting the minimum to std::numeric_limits<int>::min()
// but it would be ridiculous to use a slider handling so many values.
_slider->setMinimum(0);
SIGHT_ERROR_IF(
": The requested value range for " << std::quoted(
key
) << " is too large to be handled by a double slider. "
"Please reduce your range, the number of decimals or use a 'spin' widget.",
max_slider_value < std::numeric_limits<double>::epsilon()
);
if(max_slider_value < std::numeric_limits<double>::epsilon())
{
max_slider_value = 1.;
}
_slider->setMaximum(max_slider_value);
// Update the slider integer value according to the new mix/max
if(_current_value <= min)
{
_slider->setValue(0);
// qt does not emit the signal if the value does not change, we have to force qt signal to update the displayed
// value and emit 'doubleChanged' signal
Q_EMIT _slider->valueChanged(0);
}
else if(_current_value > max)
{
_slider->setValue(max_slider_value);
}
else
{
const int slider_val = int(std::round(((_current_value - min) / value_range) * double(_slider->maximum())));
_slider->setValue(slider_val);
}
}
//-----------------------------------------------------------------------------
QObject* parameters::get_param_widget(const std::string& _key)
{
const auto key = QString::fromStdString(_key);
const auto it = std::ranges::find_if(
m_param_boxes,
[box_key = key + "_box"](const QWidget* const _w){return _w->objectName() == box_key;});
if(it == m_param_boxes.cend())
{
SIGHT_ERROR(get_id() << ": No param with '" + _key + "' not found");
return nullptr;
}
auto* widget = (*it)->findChild<QObject*>(key);
// The child widget *MUST* exist if a parameter exists with this name, if it does not exist, this is a regression
// caused by modifications in create_xxx_widgets.
SIGHT_ASSERT(get_id() << ": Widget " << std::quoted(_key) << " not found in its parent box.", widget != nullptr);
return widget;
}
//-----------------------------------------------------------------------------
} //namespace sight::module::ui::qt
|