Menu

[efa3d9]: / src / mapeditor.cpp  Maximize  Restore  History

Download this file

2866 lines (2466 with data), 100.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
#include "mapeditor.h"
#include <QApplication>
#include <QGraphicsProxyWidget>
#include <QMenuBar>
#include <QObject>
#include <QPrintDialog>
#include <QPrinter>
#include <QScrollBar>
#include "animpoint.h"
#include "branchitem.h"
#include "geometry.h"
#include "heading-container.h"
#include "image-container.h"
#include "mainwindow.h"
#include "misc.h"
#include "shortcuts.h"
#include "winter.h"
#include "xlink.h"
#include "xlinkitem.h"
#include "xlinkobj.h"
extern Main *mainWindow;
extern QString clipboardDir;
extern QString clipboardFile;
extern bool debug;
extern QPrinter *printer;
extern QMenu *branchContextMenu;
extern QMenu *canvasContextMenu;
extern QMenu *floatimageContextMenu;
extern QMenu *taskContextMenu;
extern Switchboard switchboard;
extern Settings settings;
extern QTextStream vout;
extern QString editorFocusStyle;
extern FlagRowMaster *systemFlagsMaster;
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
MapEditor::MapEditor(VymModel *vm)
{
// qDebug() << "Constructor ME " << this;
QString shortcutScope = tr("Map Editor", "Shortcut scope");
mapScene = new QGraphicsScene(nullptr);
mapScene->setBackgroundBrush(QBrush(Qt::white, Qt::SolidPattern));
mapScene->setItemIndexMethod(QGraphicsScene::NoIndex); // FIXME-3 Avoiding crashes...
// Alternatively call removeFromIndex() in destructor
// or maybe also prepareGeometryChange()
// Origin for view transformations (rotation, scaling)
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
transformationOrigin = QPointF(0, 0);
useTransformationOrigin = false;
zoomDelta = 0.20;
if (debug) {
// Add cross in origin for debugging
QPointF p;
qreal size = 100;
QGraphicsRectItem *x_axis = new QGraphicsRectItem(p.x() - size, p.y(), size * 2, 1 );
QGraphicsRectItem *y_axis = new QGraphicsRectItem(p.x(), p.y() - size, 1, size * 2);
x_axis->setBrush(Qt::NoBrush);
y_axis->setBrush(Qt::NoBrush);
x_axis->setPen(QColor(Qt::blue));
y_axis->setPen(QColor(Qt::blue));
mapScene->addItem(x_axis);
mapScene->addItem(y_axis);
// Add another cross
/*
p = QPointF(200,0);
size = 20;
QGraphicsRectItem *x_axis2 = new QGraphicsRectItem(p.x() - size, p.y(), size * 2, 1 );
QGraphicsRectItem *y_axis2 = new QGraphicsRectItem(p.x(), p.y() - size, 1, size * 2);
x_axis2->setBrush(Qt::NoBrush);
y_axis2->setBrush(Qt::NoBrush);
x_axis2->setPen(QColor(Qt::gray));
y_axis2->setPen(QColor(Qt::gray));
mapScene->addItem(x_axis2);
mapScene->addItem(y_axis2);
*/
}
zoomFactorInt = zoomFactorTargetInt = 1;
rotationInt = rotationTargetInt = 0;
model = vm;
model->registerMapEditor(this);
setScene(mapScene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
selectionMode = AutoSelection;
setStyleSheet("QGraphicsView:focus {" + editorFocusStyle + "}");
// Create bitmap cursors, platform dependant
HandOpenCursor = QCursor(QPixmap(":/mode-move-view.png"), 1, 1);
PickColorCursor = QCursor(QPixmap(":/cursorcolorpicker.png"), 5, 27);
XLinkCursor = QCursor(QPixmap(":/cursorxlink.png"), 1, 7);
editingBO = nullptr;
printFrame = true;
printFooter = true;
setAcceptDrops(true);
// Container used for temporary moving and relinking branches
tmpParentContainer = new TmpParentContainer ();
mapScene->addItem(tmpParentContainer);
tmpParentContainer->setName("tmpParentContainer");
// When moving objects, draw then on top of everything else
tmpParentContainer->setZValue(10000);
// Shortcuts and actions
QAction *a;
a = new QAction("Select upper branch", this);
a->setShortcut(Qt::Key_Up);
a->setShortcutContext(Qt::WidgetShortcut);
connect(a, SIGNAL(triggered()), this, SLOT(cursorUp()));
addAction(a);
a = new QAction("Add upper branch to selection", this);
a->setShortcut(Qt::Key_Up | Qt::SHIFT);
a->setShortcutContext(Qt::WidgetShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorUpToggleSelection()));
a = new QAction("Select lower branch", this);
a->setShortcut(Qt::Key_Down);
a->setShortcutContext(Qt::WidgetShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorDown()));
a = new QAction("Add lower branch to selection", this);
a->setShortcut(Qt::Key_Down | Qt::SHIFT);
a->setShortcutContext(Qt::WidgetShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorDownToggleSelection()));
a = new QAction("Select left branch", this);
a->setShortcut(Qt::Key_Left);
// a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorLeft()));
a = new QAction("Select child branch", this);
a->setShortcut(Qt::Key_Right);
// a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorRight()));
a = new QAction("Select first branch", this);
a->setShortcut(Qt::Key_Home);
a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorFirst()));
a = new QAction("Select last branch", this);
a->setShortcut(Qt::Key_End);
a->setShortcutContext(Qt::WidgetWithChildrenShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(cursorLast()));
// Action to embed LineEdit for heading in Scene
lineEdit = nullptr;
a = new QAction(tr("Edit heading", "MapEditor"), this);
a->setShortcut(Qt::Key_Return); // Edit heading
a->setShortcutContext(Qt::WidgetShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(editHeading()));
a = new QAction(tr("Edit heading", "MapEditor"), this);
a->setShortcut(Qt::Key_Enter); // Edit heading
a->setShortcutContext(Qt::WidgetShortcut);
addAction(a);
connect(a, SIGNAL(triggered()), this, SLOT(editHeading()));
// Panning
panningTimer = new QTimer(this);
vPan = QPointF();
connect(panningTimer, SIGNAL(timeout()), this, SLOT(panView()));
// Clone actions defined in MainWindow
foreach (QAction *qa, mainWindow->mapEditorActions) {
a = new QAction(this);
a->setShortcut(qa->shortcut());
a->setShortcutContext(qa->shortcutContext());
connect(a, SIGNAL(triggered()), qa, SLOT(trigger()));
addAction(a);
}
setState(Neutral);
winter = nullptr;
// animations
animationUse = settings.value("/animation/use", true) .toBool();
animationTicks = settings.value("/animation/snapback/ticks", 50).toInt();
animationInterval = settings.value("/animation/snapback/interval", 15).toInt();
animatedContainers.clear();
animationTimer = new QTimer(this);
connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
}
MapEditor::~MapEditor()
{
// qDebug ()<<"Destr MapEditor this="<<this;
if (winter) {
delete winter;
winter = nullptr;
}
stopViewAnimations();
stopContainerAnimations();
}
VymModel *MapEditor::getModel() { return model; }
QGraphicsScene *MapEditor::getScene() { return mapScene; }
void MapEditor::panView()
{
if (!vPan.isNull()) {
// Scroll if needed
// To avoid jumping of the sceneView, only
// show selection, if not tmp linked
qreal px = 0;
qreal py = 0;
if (vPan.x() < 0)
px = vPan.x();
else if (vPan.x() > 0)
px = width() + vPan.x();
if (vPan.y() < 0)
py = vPan.y();
else if (vPan.y() > 0)
py = height() + vPan.y();
QPointF q = mapToScene(QPoint(px, py));
QRectF r = QRectF(q, QPointF(q.x() + 1, q.y() + 1));
// Expand view if necessary
setScrollBarPosTarget(r);
// Stop possible other animations
if (scrollBarPosAnimation.state() == QAbstractAnimation::Running)
scrollBarPosAnimation.stop();
// Do linear animation
horizontalScrollBar()->setValue(horizontalScrollBar()->value() +
vPan.x());
verticalScrollBar()->setValue(verticalScrollBar()->value() + vPan.y());
}
}
void MapEditor::ensureAreaVisibleAnimated(
const QRectF &area,
bool scaled,
bool rotated,
qreal new_rotation)
{
// Changes viewCenter to make sure that
// r is within the margins of the viewport
//
// Only zooms, if r NOT fit into viewport
// view is centered then on bounding box.
//
// Similar to QGraphicsItem::ensureVisible,
// but with animation and (if necessary)
// zooming
int xmargin = settings.value("/mapeditor/scrollToMarginX/", 50).toInt();
int ymargin = settings.value("/mapeditor/scrollToMarginY/", 50).toInt();
// Do we need to zoom out to show area?
QRect areaViewCoord = mapFromScene(area).boundingRect();
// Visible area within margins
QRect visibleViewCoord = rect();
visibleViewCoord -= QMargins(xmargin, ymargin, xmargin, ymargin);
if (!rotated)
// Use current view rotation, if we do not plan to rotate
new_rotation = rotationInt;
// Calculate required width and height considering rotation of view
qreal a = new_rotation / 180 * M_PI;
qreal area_w_viewCoord = abs(sin(a) * area.height()) + abs(cos(a) * area.width());
qreal area_h_viewCoord = abs(sin(a) * area.width()) + abs(cos(a) * area.height());
qreal z_x = 1.0 * visibleViewCoord.width() / area_w_viewCoord;
qreal z_y = 1.0 * visibleViewCoord.height() / area_h_viewCoord;
qreal zf = min (z_x, z_y);
bool zoomOutRequired =
(visibleViewCoord.width() < areaViewCoord.width() ||
visibleViewCoord.height() < areaViewCoord.height());
int animDuration = 2000;
QEasingCurve easingCurve = QEasingCurve::OutQuint;
//qDebug() << " zoom out: " << zoomOutRequired;
//qDebug() << " zoom in: " << zoomInRequired << " zoomFactor=" << zoomFactorInt << " zf=" << zf;
if (zoomOutRequired || scaled) {
setViewCenterTarget(
area.center(),
zf,
new_rotation,
animDuration,
easingCurve);
return;
}
// After zooming bbox would fit into margins of viewport
long view_dx = 0;
long view_dy = 0;
if (areaViewCoord.left() < xmargin)
// move left
view_dx = areaViewCoord.left() - xmargin;
else if (areaViewCoord.right() > viewport()->width())
// move right
view_dx = areaViewCoord.x() + areaViewCoord.width() - viewport()->width() + xmargin;
if (areaViewCoord.top() < ymargin)
// move up
view_dy = areaViewCoord.top() - ymargin;
else if (areaViewCoord.bottom() > viewport()->height() - ymargin)
// move down
view_dy = areaViewCoord.y() + areaViewCoord.height() - viewport()->height() + ymargin;
if (abs(view_dx) > 5 || abs(view_dy) > 5 || rotated)
setViewCenterTarget(
mapToScene(viewport()->geometry().center() + QPoint (view_dx, view_dy)),
zoomFactorInt,
new_rotation,
animDuration,
easingCurve);
}
void MapEditor::ensureSelectionVisibleAnimated(bool scaled, bool rotated)
{
// Changes viewCenter to make sure that bounding box of all currently
// selected items is within the margins of the viewport
//
// Only zooms, if bounding box of items does NOT fit into viewport
// view is centered then on bounding box. (Useful also for big images)
//
// Similar to QGraphicsItem::ensureVisible, but with animation and (if necessary)
// zooming
QList <TreeItem*> selis = model->getSelectedItems();
// Nothing to do, if nothing is selected
if (selis.isEmpty()) return;
// Calculate total bounding box
QRectF bbox;
bool firstIteration = true;
foreach (TreeItem *ti, selis) {
Container *c = nullptr;
QRectF c_bbox;
if (ti->hasTypeAttribute())
ti = ti->parent();
if (ti->hasTypeBranch()) {
c = ((BranchItem*)ti)->getBranchContainer()->getHeadingContainer();
c_bbox = c->mapToScene(c->rect()).boundingRect();
} else if (ti->getType() == TreeItem::Image) {
c = ((ImageItem*)ti)->getImageContainer();
c_bbox = c->mapToScene(c->rect()).boundingRect();
} else if (ti->hasTypeXLink()) {
XLinkObj *xlo = ((XLinkItem*)ti)->getXLinkObj();
if (xlo)
c_bbox = xlo->boundingRect();
}
if (firstIteration) {
bbox = c_bbox;
firstIteration = false;
} else
bbox = bbox.united(c_bbox);
}
int new_rotation = round_int(rotationInt) % 360;
if (rotated && selis.count() == 1) {
if (selis.first()->hasTypeBranch()) {
BranchContainer *bc = ((BranchItem*)selis.first())->getBranchContainer();
// Avoid rotations > 360
setRotation(new_rotation);
qreal rotScene = bc->rotationHeadingInScene();
int d_rotation = new_rotation + round_int(rotScene) % 360;
if (d_rotation > 180)
d_rotation = d_rotation - 360;
new_rotation = new_rotation - d_rotation;
}
}
ensureAreaVisibleAnimated(bbox, scaled, rotated, new_rotation);
}
void MapEditor::setScrollBarPosTarget(QRectF rect)
{
// Expand viewport, if rect is not contained
if (!sceneRect().contains(rect))
setSceneRect(sceneRect().united(rect));
int xmargin = settings.value("/mapeditor/scrollToMarginX/", 80).toInt();
int ymargin = settings.value("/mapeditor/scrollToMarginX/", 80).toInt();
// Prepare scrolling
qreal width = viewport()->width();
qreal height = viewport()->height();
QRectF viewRect = transform().
scale(zoomFactorTargetInt, zoomFactorTargetInt).
mapRect(rect);
qreal left = horizontalScrollBar()->value();
qreal right = left + width;
qreal top = verticalScrollBar()->value();
qreal bottom = top + height;
scrollBarPosTarget = getScrollBarPos();
if (viewRect.left() <= left + xmargin) {
// need to scroll from the left
scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
}
if (viewRect.right() >= right - xmargin) {
// need to scroll from the right
scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
}
if (viewRect.top() <= top + ymargin) {
// need to scroll from the top
scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
}
if (viewRect.bottom() >= bottom - ymargin) {
// need to scroll from the bottom
scrollBarPosTarget.setY(
int(viewRect.bottom() - height + ymargin + 0.5));
}
}
QPointF MapEditor::getScrollBarPosTarget() { return scrollBarPosTarget; }
void MapEditor::setScrollBarPos(const QPointF &p)
{
scrollBarPos = p;
horizontalScrollBar()->setValue(int(p.x()));
verticalScrollBar()->setValue(int(p.y()));
}
QPointF MapEditor::getScrollBarPos()
{
return QPointF(horizontalScrollBar()->value(),
verticalScrollBar()->value());
// return scrollBarPos;
}
void MapEditor::animateScrollBars()
{
qDebug() << "ME::animateScrollBars";
if (scrollBarPosAnimation.state() == QAbstractAnimation::Running)
scrollBarPosAnimation.stop();
if (settings.value("/animation/use/", true).toBool()) {
scrollBarPosAnimation.setTargetObject(this);
scrollBarPosAnimation.setPropertyName("scrollBarPos");
scrollBarPosAnimation.setDuration(
settings.value("/animation/duration/scrollbar", 2000).toInt());
scrollBarPosAnimation.setEasingCurve(QEasingCurve::OutQuint);
scrollBarPosAnimation.setStartValue(QPointF(
horizontalScrollBar()->value(), verticalScrollBar()->value()));
scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
scrollBarPosAnimation.start();
}
else
setScrollBarPos(scrollBarPosTarget);
}
void MapEditor::animate()
{
animationTimer->stop();
foreach (Container *c, animatedContainers) {
c->animate();
if (c->getContainerType() == Container::Branch)
((BranchContainer*)c)->updateUpLink();
if (!c->isAnimated())
animatedContainers.removeAll(c);
}
if (!animatedContainers.isEmpty())
animationTimer->start(animationInterval);
model->repositionXLinks();
}
void MapEditor::startAnimation(Container *c, const QPointF &v) // FIXME-3 only used in ME::autoLayout
{
if (!c) return;
startAnimation(c, c->pos(), c->pos() + v);
}
void MapEditor::startAnimation(Container *c, const QPointF &start,
const QPointF &dest)
{
if (start == dest) return;
if (c) {
c->setPos(start);
AnimPoint ap;
ap.setStart(start);
ap.setDest(dest);
ap.setTicks(animationTicks);
ap.setAnimated(true);
c->setAnimation(ap);
if (!animatedContainers.contains(c))
animatedContainers.append(c);
animationTimer->setSingleShot(true);
animationTimer->start(animationInterval);
}
}
void MapEditor::stopContainerAnimation(Container *c)
{
int i = animatedContainers.indexOf(c);
if (i >= 0)
animatedContainers.removeAt(i);
}
void MapEditor::stopContainerAnimations()
{
animationTimer->stop();
Container *c;
while (!animatedContainers.isEmpty()) {
c = animatedContainers.takeFirst();
c->stopAnimation();
}
}
void MapEditor::stopViewAnimations()
{
if (viewCenterAnimation.state() == QAbstractAnimation::Running)
viewCenterAnimation.stop();
if (rotationAnimation.state() == QAbstractAnimation::Running)
rotationAnimation.stop();
if (zoomAnimation.state() == QAbstractAnimation::Running)
zoomAnimation.stop();
}
void MapEditor::zoomIn()
{
qreal f_zf = 1 + zoomDelta; // view transformation grows
useTransformationOrigin = false;
setZoomFactorTarget(zoomFactorTargetInt * f_zf);
}
void MapEditor::zoomOut()
{
qreal f_zf = 1 - zoomDelta; // view transformation grows
useTransformationOrigin = false;
setZoomFactorTarget(zoomFactorTargetInt * f_zf);
}
void MapEditor::setZoomFactorTarget(const qreal &zft)
{
zoomFactorTargetInt = zft;
if (zoomAnimation.state() == QAbstractAnimation::Running)
zoomAnimation.stop();
if (settings.value("/animation/use/", true).toBool()) {
zoomAnimation.setTargetObject(this);
zoomAnimation.setPropertyName("zoomFactorInt");
zoomAnimation.setDuration(
settings.value("/animation/duration/zoom", 2000).toInt());
zoomAnimation.setEasingCurve(QEasingCurve::OutQuint);
zoomAnimation.setStartValue(zoomFactorInt);
zoomAnimation.setEndValue(zft);
zoomAnimation.start();
}
else
setZoomFactor(zft);
}
qreal MapEditor::zoomFactorTarget() { return zoomFactorTargetInt; }
void MapEditor::setZoomFactor(const qreal &zf)
{
zoomFactorInt = zf;
updateMatrix();
}
qreal MapEditor::zoomFactor() { return zoomFactorInt; }
void MapEditor::setRotationTarget(const qreal &at)
{
rotationTargetInt = at;
if (rotationAnimation.state() == QAbstractAnimation::Running)
rotationAnimation.stop();
if (settings.value("/animation/use/", true).toBool()) {
rotationAnimation.setTargetObject(this);
rotationAnimation.setPropertyName("rotationInt");
rotationAnimation.setDuration(
settings.value("/animation/duration/rotation", 2000).toInt());
rotationAnimation.setEasingCurve(QEasingCurve::OutQuint);
rotationAnimation.setStartValue(rotationInt);
rotationAnimation.setEndValue(at);
rotationAnimation.start();
}
else
setRotation(rotationTargetInt);
}
qreal MapEditor::rotationTarget() { return rotationTargetInt; }
void MapEditor::setRotation(const qreal &a)
{
rotationInt = a;
updateMatrix();
if (winter)
winter->updateView();
}
qreal MapEditor::rotation() { return rotationInt; }
void MapEditor::setViewCenterTarget(const QPointF &p, const qreal &zft,
const qreal &at, const int duration,
const QEasingCurve &easingCurve)
{
viewCenterTarget = p;
zoomFactorTargetInt = zft;
rotationTargetInt = at;
viewCenter = mapToScene(viewport()->geometry()).boundingRect().center();
stopViewAnimations();
if (settings.value("/animation/use/", true).toBool()) {
viewCenterAnimation.setTargetObject(this);
viewCenterAnimation.setPropertyName("viewCenter");
viewCenterAnimation.setDuration(
settings.value("/animation/duration/scrollbar", duration).toInt());
viewCenterAnimation.setEasingCurve(easingCurve);
viewCenterAnimation.setStartValue(viewCenter);
viewCenterAnimation.setEndValue(viewCenterTarget);
viewCenterAnimation.start();
rotationAnimation.setTargetObject(this);
rotationAnimation.setPropertyName("rotationInt");
rotationAnimation.setDuration(
settings.value("/animation/duration/rotation", duration).toInt());
rotationAnimation.setEasingCurve(easingCurve);
rotationAnimation.setStartValue(rotationInt);
rotationAnimation.setEndValue(rotationTargetInt);
rotationAnimation.start();
zoomAnimation.setTargetObject(this);
zoomAnimation.setPropertyName("zoomFactorInt");
zoomAnimation.setDuration(
settings.value("/animation/duration/zoom", duration).toInt());
zoomAnimation.setEasingCurve(easingCurve);
zoomAnimation.setStartValue(zoomFactorInt);
zoomAnimation.setEndValue(zoomFactorTargetInt);
zoomAnimation.start();
} else {
setRotation(rotationTargetInt);
setZoomFactor(zft);
setViewCenter(viewCenterTarget);
}
}
void MapEditor::setViewCenterTarget()
{
MapItem *selti = (MapItem *)(model->getSelectedItem());
if (selti) {
Container *c = nullptr;
if (selti->hasTypeBranch())
c = ((BranchItem*)selti)->getBranchContainer()->getHeadingContainer();
else if (selti->hasTypeImage())
c = ((ImageItem*)selti)->getImageContainer();
else
return;
setViewCenterTarget(c->mapToScene(c->rect().center()), 1, 0);
}
}
QPointF MapEditor::getViewCenterTarget() { return viewCenterTarget; }
void MapEditor::setViewCenter(const QPointF &vc) {
// For wheel events // useTransFormationOrigin == true
// and thus we will need vp_center in updateMatrix() later
vp_center = vc;
centerOn(vc);
}
QPointF MapEditor::getViewCenter() { return viewCenter; }
void MapEditor::updateMatrix()
{
if (useTransformationOrigin) {
//qDebug() << " vp_center=" << toS(vp_center);
centerOn(transformationOrigin);
}
QTransform t;
t.rotate(rotationInt);
t.scale(zoomFactorInt, zoomFactorInt);
setTransform(t);
if (useTransformationOrigin)
centerOn(vp_center);
}
void MapEditor::minimizeView() {
// If we only would set scene rectangle to existing items, then
// view would "jump", when Qt automatically tries to center.
// Better consider the currently visible viewport (with slight offset)
QRectF r = mapToScene(viewport()->geometry()).boundingRect();
r.translate(-2,-3);
setSceneRect(scene()->itemsBoundingRect().united(r));
//qDebug() << "ME::minimizeView"; // FIXME-2 check when and how often minimizeView is called
// Used to be called also from VymModel::reposition()
}
void MapEditor::print()
{
QRectF totalBBox = getTotalBBox();
if (!printer)
printer = mainWindow->setupPrinter();
// Try to set orientation automagically
// Note: Interpretation of generated postscript is amibiguous, if
// there are problems with landscape mode, see
// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
if (totalBBox.width() > totalBBox.height())
// recommend landscape
printer->setPageOrientation(QPageLayout::Landscape);
else
// recommend portrait
printer->setPageOrientation(QPageLayout::Portrait);
QPrintDialog dialog(printer, this);
dialog.setWindowTitle(tr("Print vym map", "MapEditor"));
if (dialog.exec() == QDialog::Accepted) {
QPainter pp(printer);
pp.setRenderHint(QPainter::Antialiasing, true);
// Don't print the visualisation of selection
model->unselectAll();
QRectF mapRect = totalBBox;
QGraphicsRectItem *frame = nullptr;
if (printFrame) {
// Print frame around map
mapRect.setRect(totalBBox.x() - 10, totalBBox.y() - 10,
totalBBox.width() + 20, totalBBox.height() + 20);
frame = mapScene->addRect(mapRect, QPen(Qt::black),
QBrush(Qt::NoBrush));
frame->show();
}
double paperAspect =
(double)printer->width() / (double)printer->height();
double mapAspect = (double)mapRect.width() / (double)mapRect.height();
int viewBottom;
if (mapAspect >= paperAspect) {
// Fit horizontally to paper width
// pp.setViewport(0,0,
// printer->width(),(int)(printer->width()/mapAspect) );
viewBottom = (int)(printer->width() / mapAspect);
}
else {
// Fit vertically to paper height
// pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());
viewBottom = printer->height();
}
if (printFooter) {
// Print footer below map
QFont font;
font.setPointSize(10);
pp.setFont(font);
QRectF footerBox(0, viewBottom, printer->width(), 15);
pp.drawText(footerBox, Qt::AlignLeft,
"VYM - " + model->getFileName());
pp.drawText(footerBox, Qt::AlignRight,
QDate::currentDate().toString(Qt::TextDate));
}
mapScene->render(&pp,
QRectF(0, 0, printer->width(), printer->height() - 15),
QRectF(mapRect.x(), mapRect.y(), mapRect.width(),
mapRect.height()));
// Viewport has paper dimension
if (frame)
delete (frame);
// Restore selection
model->reselect();
}
}
QRectF MapEditor::getTotalBBox() // FIXME-2 really needed? Overlaps with scene and VM... (compare getImage)
{
minimizeView();
return sceneRect();
}
QImage MapEditor::getImage(QPointF &offset)
{
QRectF sceneRect = scene()->itemsBoundingRect();
int d = 0; // border around sceneRect
QRect imageRect;
imageRect.setWidth(sceneRect.width() + 2 * d - 2);
imageRect.setHeight(sceneRect.height() + 2 * d);
offset = QPointF(sceneRect.left() - d, sceneRect.top() - d);
QImage pix(imageRect.width(), imageRect.height(), QImage::Format_RGB32);
//qDebug() << "ME::getImage offset="<< offset << " imageRect=" << toS(imageRect,0) << " sceneRect=" << toS(sceneRect,0);
QPainter pp(&pix);
pp.setRenderHints(renderHints());
mapScene->render(&pp,
// Destination:
QRectF(0, 0, imageRect.width(), imageRect.height()),
// Source in scene:
QRectF(sceneRect.x() - d, sceneRect.y() - d,
sceneRect.width() + 2 * d, sceneRect.height() + 2 * d ));
return pix;
}
void MapEditor::setAntiAlias(bool b)
{
setRenderHint(QPainter::Antialiasing, b);
}
void MapEditor::setSmoothPixmap(bool b)
{
setRenderHint(QPainter::SmoothPixmapTransform, b);
}
void MapEditor::autoLayout() // FIXME-3 not ported yet to containers. Review use case ("brainstorming")
{
/*
// Create list with all bounding polygons
QList<LinkableMapObj *> mapobjects;
QList<ConvexPolygon> polys;
ConvexPolygon p;
QList<Vector> vectors;
QList<Vector> orgpos;
QStringList headings; // FIXME testing only
Vector v;
BranchItem *bi;
BranchItem *bi2;
BranchObj *bo;
// Outer loop: Iterate until we have no more changes in orientation
bool orientationChanged = true;
while (orientationChanged) {
BranchItem *ri = model->getRootItem();
for (int i = 0; i < ri->branchCount(); ++i) {
bi = ri->getBranchNum(i);
bo = (BranchObj *)bi->getLMO();
if (bo) {
mapobjects.append(bo);
p = bo->getBoundingPolygon();
p.calcCentroid();
polys.append(p);
vectors.append(QPointF(0, 0));
orgpos.append(p.at(0));
headings.append(bi->headingPlain());
}
for (int j = 0; j < bi->branchCount(); ++j) {
bi2 = bi->getBranchNum(j);
bo = (BranchObj *)bi2->getLMO();
if (bo) {
mapobjects.append(bo);
p = bo->getBoundingPolygon();
p.calcCentroid();
polys.append(p);
vectors.append(QPointF(0, 0));
orgpos.append(p.at(0));
headings.append(bi2->headingPlain());
}
}
}
// Iterate moving bounding polygons until we have no more collisions
int collisions = 1;
while (collisions > 0) {
collisions = 0;
for (int i = 0; i < polys.size() - 1; ++i) {
for (int j = i + 1; j < polys.size(); ++j) {
if (polygonCollision(polys.at(i), polys.at(j),
QPointF(0, 0))
.intersect) {
collisions++;
if (debug)
qDebug() << "Collision: " << headings[i] << " - "
<< headings[j];
v = polys.at(j).centroid() - polys.at(i).centroid();
v.normalize();
// Add random direction, if only two polygons with
// identical y or x
if (v.x() == 0 || v.y() == 0) {
Vector w(cos(double((int)rand() % 1000)),
sin(double((int)rand() % 1000)));
w.normalize();
v = v + w;
}
// Scale translation vector by area of polygons
vectors[j] = v * 10000 / polys.at(j).weight();
vectors[i] = v * 10000 / polys.at(i).weight();
vectors[i].invert();
// FIXME outer loop, "i" get's changed several
// times...
// Better not move away from centroid of 2 colliding
// polys, but from centroid of _all_
}
}
}
for (int i = 0; i < vectors.size(); i++) {
// qDebug() << " v="<<vectors[i]<<" "<<headings[i];
if (!vectors[i].isNull())
polys[i].translate(vectors[i]);
}
// if (debug) qDebug()<< "Collisions total: "<<collisions;
// collisions=0;
}
// Finally move the real objects and update
QList<LinkableMapObj::Orientation> orients;
for (int i = 0; i < polys.size(); i++) {
Vector v = polys[i].at(0) - orgpos[i];
orients.append(mapobjects[i]->getOrientation());
if (!v.isNull()) {
if (debug)
qDebug() << " Moving " << polys.at(i).weight() << " "
<< mapobjects[i]->getAbsPos() << " -> "
<< mapobjects[i]->getAbsPos() + v << " "
<< headings[i];
// mapobjects[i]->moveBy(v.x(),v.y() );
// mapobjects[i]->setRelPos();
startAnimation((BranchObj *)mapobjects[i], v);
if (debug)
qDebug() << i << " Weight: " << polys.at(i).weight() << " "
<< v << " " << headings.at(i);
}
}
/ *
model->reposition();
orientationChanged=false;
for (int i=0;i<polys.size();i++)
if (orients[i]!=mapobjects[i]->getOrientation())
{
orientationChanged=true;
break;
}
* /
break;
// orientationChanged=false;
} // loop if orientation has changed
*/
}
TreeItem *MapEditor::findMapItem(
QPointF p,
const QList <TreeItem*> &excludedItems,
bool findNearCenter)
{
// Search XLinks
XLink *xlink;
for (int i = 0; i < model->xlinkCount(); i++) {
xlink = model->getXLinkNum(i);
if (xlink) {
XLinkObj *xlo = xlink->getXLinkObj();
if (xlo) {
XLinkObj::SelectionType xlinkSelection = xlo->couldSelect(p);
if (xlinkSelection == XLinkObj::Path) {
// Found path of XLink, now return the nearest XLinkItem of p
qreal d0 = Geometry::distance(p, xlo->getBeginPos());
qreal d1 = Geometry::distance(p, xlo->getEndPos());
if (d0 < d1)
return xlink->beginXLinkItem();
else
return xlink->endXLinkItem();
}
if (xlinkSelection == XLinkObj::C0)
return xlink->beginXLinkItem();
if (xlinkSelection == XLinkObj::C1)
return xlink->endXLinkItem();
}
}
}
// Search branches (and their childs, e.g. images
// Start with mapcenter, no images allowed at rootItem
BranchItem *nearestFloatingCenter = nullptr;
qreal d = 0;
int i = 0;
BranchItem *bi = model->getRootItem()->getFirstBranch();
TreeItem *found = nullptr;
while (bi) {
found = bi->findMapItem(p, excludedItems);
if (found)
return found;
if (findNearCenter) {
// Try to find nearest MapCenter // FIXME-3 or branch with floating layout.
// Currently only MapCenters are searched
Container *hc = bi->getBranchContainer()->getHeadingContainer();
QPointF q = hc->mapToScene(hc->center());
if (!nearestFloatingCenter) {
nearestFloatingCenter = bi;
d = Geometry::distance(p, q);
} else {
qreal d2 = Geometry::distance(p, q);
if (d2 < d) {
d = d2;
nearestFloatingCenter = bi;
}
}
}
i++;
bi = model->getRootItem()->getBranchNum(i);
}
if (nearestFloatingCenter && d < 80 && !excludedItems.contains(nearestFloatingCenter))
return nearestFloatingCenter;
return nullptr;
}
BranchItem *MapEditor::findMapBranchItem(
QPointF p,
const QList <TreeItem*> &excludedItems,
bool findNearCenter)
{
TreeItem *ti = findMapItem(p, excludedItems, findNearCenter);
if (ti && ti->hasTypeBranch())
return (BranchItem*)ti;
else
return nullptr;
}
void MapEditor::testFunction1()
{
//autoLayout();
qDebug() << "ME::test";
qDebug() << " hor_scrollbar=" << horizontalScrollBar()->value();
qDebug() << " ver_scrollbar=" << verticalScrollBar()->value();
qDebug() << " rect=" << toS(rect());
qDebug() << " sceneRect=" << toS(sceneRect());
qDebug() << " tf=" << zoomFactorInt;
qDebug() << " pageStep=" << horizontalScrollBar()->pageStep();
}
void MapEditor::testFunction2()
{
TreeItem *selti = model->getSelectedItem();
if (selti)
{
if (selti->hasTypeBranch()) {
BranchContainer *bc = ((BranchItem*)selti)->getBranchContainer();
bc->setScrollOpacity(bc->getScrollOpacity() * 0.9); // FIXME-3 animation test
} else if (selti->hasTypeImage()) {
ImageContainer *ic = ((ImageItem*)selti)->getImageContainer();
qDebug() << ic->info() << ic;
} else
qDebug() << "Unknown type";
} else
qWarning() << "Nothing selected";
}
void MapEditor::toggleWinter()
{
if (winter) {
delete winter;
winter = nullptr;
}
else {
winter = new Winter(this);
QList<QRectF> obstacles;
BranchContainer *bc;
BranchItem *cur = nullptr;
BranchItem *prev = nullptr;
model->nextBranch(cur, prev);
while (cur) {
if (!cur->hasHiddenParent()) { // FIXME-3 avoid recursive calls here in winter
// Branches
bc = cur->getBranchContainer();
if (bc->isVisible()) {
HeadingContainer *hc = bc->getHeadingContainer();
obstacles.append(hc->mapRectToScene(hc->boundingRect()));
}
}
model->nextBranch(cur, prev);
}
winter->setObstacles(obstacles);
}
}
bool MapEditor::isContainerCloserInDirection(Container *c1, Container *c2, const qreal &d_min, const QPoint &v, RadarDirection radarDir)
{
qreal d = c1->distance(c2);
switch(radarDir) {
case UpDirection:
if (v.y() < 0 && (d_min < 0 || d < d_min))
return true;
break;
case DownDirection:
if (v.y() > 0 && (d_min < 0 || d < d_min))
return true;
break;
case LeftDirection:
if (v.x() < 0 && (d_min < 0 || d < d_min))
return true;
break;
case RightDirection:
if (v.x() > 0 && (d_min < 0 || d < d_min))
return true;
break;
default:
qWarning() << "MapEditor::isContainerCloserInDirection undefined radar";
}
return false;
}
TreeItem* MapEditor::getItemInDirection(TreeItem *ti, RadarDirection radarDir)
{
SelectionMode selMode = currentSelectionMode(ti);
//qDebug() << "ME::getItemInDir selMode=" << selMode;
if (selMode == GeometricSelection)
return getItemFromGeometry(ti, radarDir);
if (selMode == OrgChartSelection)
return getItemFromOrgChart(ti, radarDir);
return getItemFromClassicMap(ti, radarDir);
}
TreeItem* MapEditor::getItemFromGeometry(TreeItem *ti, RadarDirection radarDir) // FIXME-2 does not really work
{
TreeItem *nearestItem = nullptr;
if (ti) {
// Calculate reference points: Nearest corners of selected towards radarDir
QPoint rp_view;
Container *c = nullptr;
if (ti->hasTypeBranch())
c = ((BranchItem*)ti)->getBranchContainer()->getHeadingContainer();
else if (ti->hasTypeImage())
c = ((ImageItem*)ti)->getImageContainer();
if (!c) {
qWarning() << __func__ << "No container found";
return nullptr;
}
rp_view = mapFromScene(c->mapToScene(c->center()));
qreal d_min = -1;
qreal d;
BranchItem *cur = nullptr;
BranchItem *prev = nullptr;
model->nextBranch(cur, prev);
while (cur) {
// Interate over all branches in map
BranchContainer *bc;
bc = cur->getBranchContainer();
if (bc && bc->isVisible()) {
HeadingContainer *hc = bc->getHeadingContainer();
QPointF p_scene = hc->mapToScene(hc->center());
QPoint v = mapFromScene(p_scene) - rp_view; // Direction between centers
d = c->distance(hc);
if (cur != ti && isContainerCloserInDirection(c, hc, d_min, v, radarDir)) {
d_min = d;
nearestItem = cur;
}
// Iterate over images
for (int i = 0; i < cur->imageCount(); i++) {
ImageItem *ii = cur->getImageNum(i);
ImageContainer *ic = ii->getImageContainer();
p_scene = ic->mapToScene(ic->center());
v = mapFromScene(p_scene) - rp_view;
d = c->distance(ic);
if (ii != ti && isContainerCloserInDirection(c, ic, d_min, v, radarDir)) {
d_min = d;
nearestItem = ii;
}
}
}
model->nextBranch(cur, prev);
}
}
return nearestItem;
}
TreeItem* MapEditor::getItemFromOrgChart(TreeItem *ti, RadarDirection radarDir)
{
BranchItem *bi = nullptr;
if (ti->hasTypeBranch())
bi = (BranchItem*)ti;
if (!bi)
return nullptr;
if (radarDir == UpDirection)
return bi->parentBranch();
else if (radarDir == DownDirection)
return bi->getLastSelectedBranch();
else if (radarDir == LeftDirection)
return getItemFromClassicMap(ti, UpDirection);
else if (radarDir == RightDirection)
return getItemFromClassicMap(ti, DownDirection);
return nullptr;
}
TreeItem* MapEditor::getItemFromClassicMap(TreeItem *selti, RadarDirection radarDir)
{
if (!selti)
return nullptr;
BranchItem * bi = nullptr;
if (selti->hasTypeBranch())
bi = (BranchItem*)selti;
int d = selti->depth(); // original depth
if (radarDir == UpDirection) {
TreeItem *ti = getItemDirectAbove(selti);
if (ti)
// direct predecessor
return ti;
// Go towards center and look for predecessor
while (selti->depth() > 0) {
selti = selti->parent();
ti = getItemDirectAbove(selti);
if (ti) {
// turn
selti = ti;
while (selti->depth() < d) {
// try to get back to original depth dz
ti = selti->getLastItem();
if (!ti)
return selti;
selti = ti;
}
return selti;
}
}
} else if (radarDir == DownDirection) {
TreeItem *ti = getItemDirectBelow(selti);
if (ti) return ti;
// Go towards center and look for siblings
while (selti->depth() > 0) {
selti = selti->parent();
ti = getItemDirectBelow(selti);
if (ti) {
// turn
selti = ti;
while (selti->depth() < d) {
// try to get back to original depth d
ti = selti->getFirstItem();
if (!ti)
return selti;
selti = ti;
}
return selti;
}
}
} else if (radarDir == LeftDirection) {
if (bi) {
// Branch selected
if (d == 0) {
// Special case: use alternative selection index
BranchItem *newbi = bi->getLastSelectedBranchAlt();
if (!newbi) {
BranchContainer *bc;
// Try to find a mainbranch left of center
for (int i = 0; i < bi->branchCount(); i++) {
newbi = bi->getBranchNum(i);
bc = newbi->getBranchContainer();
if (bc && bc->getOrientation() == BranchContainer::LeftOfParent)
break;
}
}
return newbi;
}
if (bi->getBranchContainer()->getOrientation() ==
BranchContainer::RightOfParent)
// right of center
return bi->parentBranch();
else {
// left of center
TreeItem *ri = bi->getLastSelectedBranch();
if (ri)
// Return last selected branch
return ri;
else
// Look for image
return getItemFromGeometry(bi, LeftDirection);
}
}
} else if (radarDir == RightDirection) {
if (bi) {
// Branch selected
if (d == 0) {
// Special case: use alternative selection index
BranchItem *newbi = bi->getLastSelectedBranch();
if (!newbi) {
BranchContainer *bc;
// Try to find a mainbranch right of center
for (int i = 0; i < bi->branchCount(); i++) {
newbi = bi->getBranchNum(i);
bc = newbi->getBranchContainer();
if (bc && bc->getOrientation() == BranchContainer::RightOfParent)
break;
}
}
return newbi;
}
if (bi->getBranchContainer()->getOrientation() == BranchContainer::LeftOfParent)
// left of center
return bi->parentBranch();
else {
// right of center
TreeItem *ri = bi->getLastSelectedBranch();
if (ri)
// Return last selected branch
return ri;
else
// Look for image
return getItemFromGeometry(bi, RightDirection);
}
}
}
return nullptr;
}
TreeItem *MapEditor::getItemDirectAbove(TreeItem *ti)
{
if (ti) {
if (ti->hasTypeBranch()) {
BranchItem *bi = (BranchItem*)ti;
int i = bi->num();
if (i > 0)
return bi->parent()->getBranchNum(i - 1);
} else if (ti->hasTypeImage()) {
ImageItem *ii = (ImageItem*)ti;
int i = ii->num();
if (i > 0)
return ii->parent()->getImageNum(i - 1);
}
}
return nullptr;
}
TreeItem *MapEditor::getItemDirectBelow(TreeItem *ti)
{
if (ti) {
if (ti->hasTypeBranch()) {
BranchItem *bi = (BranchItem*)ti;
int i = bi->num();
if (i + 1 < bi->parent()->branchCount())
return bi->parent()->getBranchNum(i + 1);
} else if (ti->hasTypeImage()) {
ImageItem *ii = (ImageItem*)ti;
int i = ii->num();
if (i + 1 < ii->parent()->imageCount())
return ii->parent()->getImageNum(i + 1);
}
}
return nullptr;
}
void MapEditor::cursorUp()
{
if (editorState == MapEditor::EditingHeading)
return;
TreeItem *selti = model->getSelectedItem();
if (selti) {
selti = getItemInDirection(selti, UpDirection);
if (selti)
model->select(selti);
}
}
void MapEditor::cursorUpToggleSelection()
{
if (editorState == MapEditor::EditingHeading)
return;
QList <TreeItem*> seltis = model->getSelectedItems();
TreeItem *ti;
if (seltis.size() == 0)
return;
else if (seltis.size() == 1) {
ti = getItemInDirection(seltis.first(), UpDirection);
if (ti) model->selectToggle(ti);
} else {
// Nothing selected or already multiple selections
TreeItem *last_ti = model->lastToggledItem();
if (last_ti && (last_ti->hasTypeBranch() || last_ti->hasTypeImage())) {
if (lastToggleDirection == toggleUp)
ti = getItemInDirection(last_ti, UpDirection);
else
ti = last_ti;
if (ti)
model->selectToggle(ti);
}
}
lastToggleDirection = toggleUp;
}
void MapEditor::cursorDown()
{
if (editorState == MapEditor::EditingHeading)
return;
TreeItem *selti = model->getSelectedItem();
if (selti) {
selti = getItemInDirection(selti, DownDirection);
if (selti)
model->select(selti);
}
}
void MapEditor::cursorDownToggleSelection()
{
if (editorState == MapEditor::EditingHeading)
return;
TreeItem *selti = model->getSelectedItem();
TreeItem *ti;
if (selti) {
ti = getItemInDirection(selti, DownDirection);
if (ti) {
model->selectToggle(ti);
}
} else {
// Nothing selected or already multiple selections
TreeItem *last_ti = model->lastToggledItem();
if (last_ti && (last_ti->hasTypeBranch() || last_ti->hasTypeImage())) {
if (lastToggleDirection == toggleDown)
ti = getItemInDirection(last_ti, DownDirection);
else
ti = last_ti;
if (ti)
model->selectToggle(ti);
}
}
lastToggleDirection = toggleDown;
}
void MapEditor::cursorLeft()
{
TreeItem *selti = model->getSelectedItem();
if (!selti) {
// If multiple items are selected, select the next to last toggled one
selti = model->lastToggledItem();
if (!selti) return;
}
TreeItem *ti = getItemInDirection(selti, LeftDirection);
if (ti)
model->select(ti);
}
void MapEditor::cursorRight()
{
TreeItem *selti = model->getSelectedItem();
if (!selti) {
// If multiple items are selected, select the next to last toggled one
selti = model->lastToggledItem();
if (!selti) return;
}
TreeItem *ti = getItemInDirection(selti, RightDirection);
if (ti)
model->select(ti);
else {
ImageItem *ii = selti->getFirstImage();
if (ii)
model->select(ii);
}
}
void MapEditor::cursorFirst() { model->selectFirstBranch(); } // FIXME-3 adapt for images and container layouts
void MapEditor::cursorLast() { model->selectLastBranch(); } // FIXME-3 adapt for images and container layouts
void MapEditor::editHeading(BranchItem *selbi)
{
if (editorState == EditingHeading) {
editHeadingFinished();
return;
}
if (!selbi) selbi = model->getSelectedBranch();
if (selbi) {
VymText heading = selbi->heading();
if (heading.isRichText() || selbi->headingPlain().contains("\n")) {
mainWindow->windowShowHeadingEditor();
ensureSelectionVisibleAnimated();
return;
}
model->setSelectionBlocked(true);
lineEdit = new QLineEdit;
QGraphicsProxyWidget *proxyWidget = mapScene->addWidget(lineEdit);
// FIXME-3-FT get total rotation XXX for BC in scene and do "proxyWidget->setRotation(XXX);
lineEdit->setCursor(Qt::IBeamCursor);
lineEdit->setCursorPosition(1);
lineEdit->grabKeyboard();
#if defined(Q_OS_WINDOWS)
QFont font = lineEdit->font();
font.setPointSize(font.pointSize() + 4);
lineEdit->setFont(font);
#endif
QPointF tl;
QPointF br;
qreal w = 230;
qreal h = 30;
BranchContainer *bc = selbi->getBranchContainer();
if (bc->getOrientation() == BranchContainer::RightOfParent) {
tl = bc->headingRect().topLeft();
br = tl + QPointF(w, h);
}
else {
br = bc->headingRect().bottomRight();
tl = br - QPointF(w, h);
}
// Qt bug when using QProxyWdiget in scaled QGraphicsView
// https://bugreports.qt.io/browse/QTBUG-48681
// Still present in Qt 6.5
QRectF r(tl, br);
lineEdit->setGeometry(r.toRect());
proxyWidget->setGeometry(r.toRect());
minimizeView();
// Set focus to MapEditor first
// To avoid problems with Cursor up/down
setFocus();
ensureAreaVisibleAnimated(r);
if (heading.getTextASCII() == " ")
heading.setPlainText("");
lineEdit->setText(heading.getTextASCII());
lineEdit->setFocus();
lineEdit->selectAll(); // Hack to enable cursor in lineEdit
lineEdit->deselect(); // probably a Qt bug...
setState(EditingHeading);
}
}
void MapEditor::editHeadingFinished()
{
if (editorState != EditingHeading || !lineEdit ) {
qWarning() << "ME::editHeadingFinished not editing heading!";
} else {
lineEdit->clearFocus();
lineEdit->releaseKeyboard();
QString s = lineEdit->text();
s.replace(QRegularExpression("\\n"), " "); // Don't paste newline chars
if (s.length() == 0)
s = " "; // Don't allow empty lines, which would screw up drawing
model->setHeadingPlainText(s);
delete (lineEdit);
lineEdit = nullptr;
// Maybe reselect previous branch
mainWindow->editHeadingFinished(model);
// Autolayout to avoid overlapping branches with longer headings
if (settings.value("/mainwindow/autoLayout/use", "true") == "true")
autoLayout();
}
model->setSelectionBlocked(false);
setState(Neutral);
}
void MapEditor::contextMenuEvent(QContextMenuEvent *e)
{
// Lineedits are already closed by preceding
// mouseEvent, we don't need to close here.
QPointF p = mapToScene(e->pos());
TreeItem *ti = findMapItem(p);
if (ti) {
model->select(ti);
BranchItem *selbi = model->getSelectedBranch();
// Context Menu
if (selbi) {
QString sysFlagName;
QUuid uid = selbi->getBranchContainer()->findFlagByPos(p);
if (!uid.isNull()) {
Flag *flag = systemFlagsMaster->findFlagByUid(uid);
if (flag)
sysFlagName = flag->getName();
}
if (sysFlagName.startsWith("system-task"))
taskContextMenu->popup(e->globalPos());
else
// Context Menu on branch or mapcenter
branchContextMenu->popup(e->globalPos());
}
else {
if (model->getSelectedImage()) {
// Context Menu on floatimage
floatimageContextMenu->popup(e->globalPos());
}
else {
if (model->getSelectedXLink())
// Context Menu on XLink
model->editXLink();
}
}
}
else { // No object or container found, we are on the Canvas itself
// Context Menu on scene
// Open context menu synchronously to position new mapcenter
model->setContextPos(p);
canvasContextMenu->exec(e->globalPos());
model->unsetContextPos();
}
e->accept();
}
void MapEditor::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_PageUp || e->key() == Qt::Key_PageDown)
// Ignore PageUP/Down to avoid scrolling with keys
return;
if (e->modifiers() & Qt::ShiftModifier)
updateCursor();
QGraphicsView::keyPressEvent(e);
}
void MapEditor::keyReleaseEvent(QKeyEvent *e)
{
if (!(e->modifiers() & Qt::ShiftModifier))
setCursor(Qt::ArrowCursor);
}
void MapEditor::startPanningView(QMouseEvent *e)
{
setState(PanningView);
panning_initialPointerPos = e->globalPosition().toPoint();
panning_initialScrollBarValues = // Used for scrollbars when moving view
QPoint(horizontalScrollBar()->value(), verticalScrollBar()->value());
setCursor(Qt::ClosedHandCursor);
}
void MapEditor::mousePressEvent(QMouseEvent *e) // FIXME-3 Drop down dialog, if multiple tree items are found to select the "right" one
{
// Ignore right clicks
if (e->button() == Qt::RightButton) {
e->ignore();
QGraphicsView::mousePressEvent(e);
return;
}
// Check if we need to reset zoomFactor for middle button + Ctrl
if (e->button() == Qt::MiddleButton && e->modifiers() & Qt::ControlModifier) {
setZoomFactorTarget(1);
setRotationTarget(0);
return;
}
// Initial position of pointer in scene coordinates. See also e->globalPos (!)
movingObj_initialScenePos = mapToScene(e->pos());
TreeItem *ti_found = findMapItem(movingObj_initialScenePos);
// Allow selecting text in QLineEdit if necessary
if (model->isSelectionBlocked()) {
e->ignore();
QGraphicsView::mousePressEvent(e);
return;
}
// Stop editing in LineEdit
if (editorState == EditingHeading) editHeadingFinished();
/*
qDebug() << "ME::mouse pressed\n";
qDebug() << " ti_found=" << ti_found;
*/
//if (ti_found) qDebug() << " ti_found="<<ti_found->heading();
// If Modifier mode "view" is set, all other clicks can be ignored,
// nothing will be selected
if ((e->modifiers() & Qt::ShiftModifier) &&
mainWindow->getModMode() == Main::ModModeMoveView) {
startPanningView(e);
return;
}
BranchItem *selbi = model->getSelectedBranch();
BranchContainer *selbc = nullptr;
if (selbi) {
selbc = selbi->getBranchContainer();
// XLink modifier, create new XLink
if (mainWindow->getModMode() == Main::ModModeXLink &&
(e->modifiers() & Qt::ShiftModifier)) {
setState(CreatingXLink);
tmpXLink = new XLink(model);
tmpXLink->setBeginBranch(selbi);
tmpXLink->createXLinkObj();
tmpXLink->setStyleBegin("None");
tmpXLink->setStyleEnd("None");
tmpXLink->setEndPoint(movingObj_initialScenePos);
tmpXLink->updateXLink();
return;
}
}
QString sysFlagName;
if (ti_found) {
// Check modifier key (before selecting object!)
if (e->modifiers() & Qt::ShiftModifier) {
if (mainWindow->getModMode() == Main::ModModeColor) {
setState(PickingColor);
mainWindow->setCurrentColor(ti_found->headingColor());
if (e->modifiers() & Qt::ControlModifier)
model->colorBranch(ti_found->headingColor());
else
model->colorSubtree(ti_found->headingColor());
return;
}
}
// Check for flags on MousePress
if (selbc) {
QUuid uid = selbc->findFlagByPos(movingObj_initialScenePos);
if (!uid.isNull()) {
Flag *flag = systemFlagsMaster->findFlagByUid(uid);
if (flag)
sysFlagName = flag->getName();
}
}
// Check vymlink modifier (before selecting object!)
if (sysFlagName == "system-vymLink") {
model->select(ti_found);
if (e->modifiers() & Qt::ControlModifier) {
if (e->modifiers() & Qt::ShiftModifier)
model->deleteVymLink();
else
mainWindow->editOpenVymLink(true);
} else
mainWindow->editOpenVymLink(false);
return;
}
// Select the clicked object, if not moving without linking
if (e->modifiers() & Qt::ShiftModifier) {
if (mainWindow->getModMode() == Main::ModModePoint) {
lastToggleDirection = toggleUndefined;
model->selectToggle(ti_found);
}
} else {
if (model->getSelectedItems().count() < 2 || !model->getSelectedItems().contains(ti_found))
// Only add ti_found, if we don't have multi-selection yet, which we
// want to move around. In that case we would ignore the "pressed" event
model->select(ti_found);
}
movingItems = model->getSelectedItemsReduced();
// Make sure currently clicked item is first in list
int i = movingItems.indexOf(ti_found);
if (i > 0)
movingItems.move(i, 0);
// Left Button Move Branches
if (e->button() == Qt::LeftButton) {
// No system flag clicked, take care of moving modes or simply
// start moving
if (ti_found->hasTypeBranch())
{
BranchContainer *bc = ((BranchItem*)ti_found)->getBranchContainer();
movingObj_initialContainerOffset = movingObj_initialScenePos - bc->getHeadingContainer()->mapToScene(QPointF(0,0));
}
if (mainWindow->getModMode() == Main::ModModeMoveObject &&
e->modifiers() & Qt::ShiftModifier) {
setState(MovingObjectWithoutLinking);
}
else
setState(MovingObject);
// Set initial position and size of tmpParentContainer
// Required when ONLY moving images.
tmpParentContainer->setPos(movingObj_initialScenePos - movingObj_initialContainerOffset);
if (movingItems.count() > 0) {
qreal w = 0;
qreal h = 0;
BranchContainer *bc_first = nullptr;
foreach (TreeItem *ti, movingItems) {
if (ti->hasTypeBranch()) {
BranchContainer* bc = ((BranchItem*)ti)->getBranchContainer();
if (!bc_first)
bc_first = bc;
w = max(w, bc->rect().width());
h += bc->rect().height();
}
}
if (bc_first) tmpParentContainer->setRect(bc_first->rect().left(), bc_first->rect().top(), w, h);
}
}
else
// Middle Button - Toggle Scroll
//
// (On Mac OS X this won't work, but we still have
// a button in the toolbar)
if (e->button() == Qt::MiddleButton)
model->toggleScroll();
} else {
// No ti_found, we are on the scene itself
// Left Button move Pos of sceneView
if (e->button() == Qt::LeftButton ||
e->button() == Qt::MiddleButton) {
startPanningView(e);
return;
}
}
e->accept();
// Take care of remaining system flags _or_ modifier modes
if (selbc) {
if (!sysFlagName.isEmpty()) {
// systemFlag clicked
if (sysFlagName.contains("system-url") ||
sysFlagName.contains("system-jira") ) {
if (e->modifiers() & Qt::ControlModifier)
mainWindow->editOpenURLTab();
else
mainWindow->editOpenURL();
} else if (sysFlagName == "system-note")
mainWindow->windowToggleNoteEditor();
else if (sysFlagName == "hideInExport")
model->toggleHideExport();
else if (sysFlagName.startsWith("system-task-"))
model->cycleTaskStatus();
return;
}
} // system flags or modModes
else { // No selbc found, check XLinks
if (ti_found) {
if (ti_found->getType() == TreeItem::XLinkItemType) {
XLinkObj *xlo = ((XLinkItem *)ti_found)->getXLink()->getXLinkObj();
if (xlo)
setState(EditingXLink);
}
}
}
}
void MapEditor::mouseMoveEvent(QMouseEvent *e)
{
QPointF p_event = mapToScene(e->pos());
// Show mouse position for debugging in statusBar
if (debug && e->modifiers() & Qt::ControlModifier)
mainWindow->statusMessage(
QString("ME::mouseMoveEvent Scene: %1 - Viewport: %2")
.arg(toS(p_event, 0), toS(e->pos())));
// Allow selecting text in QLineEdit if necessary
if (model->isSelectionBlocked()) {
e->ignore();
QGraphicsView::mouseMoveEvent(e);
return;
}
// Pan view
if (editorState == PanningView) {
QPointF pg = e->globalPosition();
QPoint v_pan;
v_pan.setX(-pg.x() + panning_initialPointerPos.x());
v_pan.setY(-pg.y() + panning_initialPointerPos.y());
horizontalScrollBar()->setSliderPosition(
(int)(panning_initialScrollBarValues.x() + v_pan.x()));
verticalScrollBar()->setSliderPosition(
(int)(panning_initialScrollBarValues.y() + v_pan.y()));
// Avoid flickering
scrollBarPosAnimation.stop();
viewCenterAnimation.stop();
// rotationAnimation.stop();
// zoomAnimation.stop();
return;
}
// After clicking object shift might have been pressed, adjust state then
if (mainWindow->getModMode() == Main::ModModeMoveObject &&
e->modifiers() & Qt::ShiftModifier && editorState == MovingObject) {
setState(MovingObjectWithoutLinking);
}
// Move the selected items
if (movingItems.count() > 0 &&
(editorState == MovingObject ||
editorState == MovingObjectTmpLinked ||
editorState == MovingObjectWithoutLinking ||
editorState == EditingXLink)) {
if (!(e->buttons() & Qt::LeftButton)) {
// Sometimes at least within a VM there might be a
// release event lost, while still the mousePress event is processed
//
// So moving without a pressed left button is considered a "release"
mouseReleaseEvent(e);
return;
}
int margin = 50;
// Check if we have to scroll
QPointF p = e->position();
vPan.setX(0);
vPan.setY(0);
if (p.y() >= 0 && p.y() <= margin)
vPan.setY(p.y() - margin);
else if (p.y() <= height() && p.y() > height() - margin)
vPan.setY(p.y() - height() + margin);
if (p.x() >= 0 && p.x() <= margin)
vPan.setX(p.x() - margin);
else if (p.x() <= width() && p.x() > width() - margin)
vPan.setX(p.x() - width() + margin);
moveObject(e, p_event);
} // selection && moving_obj
// Draw a link from one branch to another
if (editorState == CreatingXLink) {
tmpXLink->setEndPoint(p_event);
tmpXLink->updateXLink();
}
}
void MapEditor::moveObject(QMouseEvent *e, const QPointF &p_event)
{
bool repositionRequired = false;
bool updateUpLinksRequired = false;
// If necessary pan the view using animation
if (!panningTimer->isActive())
panningTimer->start(50);
// reset cursor if we are moving and don't copy
// Check if we could link (temporary). Consider also "near" mapCenters.
TreeItem *targetItem = findMapItem(p_event, movingItems, true);
// Check, if targetItem is a child of one of the moving items
if (targetItem) {
foreach (TreeItem *ti, movingItems) {
if (targetItem->isChildOf(ti)) {
// qWarning() << "ME::moveObject " << targetItem->headingPlain() << "is child of " << ti->headingPlain();
targetItem = nullptr;
break;
}
}
}
// Add selected branches and images temporary to tmpParentContainer,
// if they are not there yet:
BranchContainer *bc_first = nullptr;
BranchContainer *bc_prev = nullptr;
if (movingItems.count() > 0 && (tmpParentContainer->childrenCount() == 0)) {
BranchContainer *bc;
foreach (TreeItem *ti, movingItems)
{
// The item structure in VymModel remaines untouched so far,
// only containers will be reparented temporarily!
if (ti->hasTypeBranch()) {
BranchItem *bi = (BranchItem*)ti;
bc = bi->getBranchContainer();
if (!bc_first) {
bc_first = bc;
// Initially set orientation of tmpParentContainer to first BranchContainer
if (bc->getOrientation() == BranchContainerBase::UndefinedOrientation) {
// Orientation undefined for MapCenters, assume RightOfParent
tmpParentContainer->setOrientation(BranchContainerBase::RightOfParent);
} else
tmpParentContainer->setOrientation(bc->getOrientation());
}
if (tmpParentContainer->branchCount() == 0 ||
bc->parentItem() != tmpParentContainer->getBranchesContainer()) {
// Save position of children branches in case we only want to
// move this branch and keep children unchanged using CTRL modifier
if (bc->hasFloatingBranchesLayout()) {
foreach(BranchContainer *bc2, bc->childBranches())
bc2->setOriginalScenePos();
}
bc->setOriginalPos();
bc->setOriginalOrientation(); // Also sets originalParentBranchContainer
tmpParentContainer->addToBranchesContainer(bc);
}
if (bc_first && bc_first != bc) {
QPointF p;
// Animate other items to position horizontally centered below first one
if (bc_first->getOriginalOrientation() == BranchContainer::RightOfParent) {
p = tmpParentContainer->mapFromItem(bc,
bc->alignTo(Container::TopLeft, bc_prev, Container::BottomLeft));
} else if (bc_first->getOriginalOrientation() == BranchContainer::LeftOfParent)
p = tmpParentContainer->mapFromItem(bc,
bc->alignTo(Container::TopRight, bc_prev, Container::BottomRight));
else
p = tmpParentContainer->mapFromItem(bc,
bc->alignTo(Container::TopCenter, bc_prev, Container::BottomCenter));
startAnimation ( bc, bc->pos(), p);
}
bc_prev = bc;
} else if (ti->hasTypeImage()) {
ImageContainer *ic = ((ImageItem*)ti)->getImageContainer();
if (ic->parentItem() != tmpParentContainer->getImagesContainer()) {
ic->setOriginalPos();
tmpParentContainer->addToImagesContainer(ic);
}
}
else if (ti->getType() == TreeItem::XLinkItemType) {
// Move XLink control point
XLinkObj *xlo = ((XLinkItem *)ti)->getXLinkObj();
if (xlo) {
xlo->setSelectedCtrlPoint(p_event); // FIXME-3 Missing savestate
model->setChanged();
}
}
else
qWarning("ME::moveObject Huh? I'm confused. No BC, IC or XLink moved");
}
} // add to tmpParentContainer
if (tmpParentContainer->childBranches().count() > 0)
// If ME::moveObject is called AFTER tPC has been filled previously,
// bc_first still might be unset here
bc_first = tmpParentContainer->childBranches().first();
BranchContainer *targetBranchContainer = nullptr;
// Check if we are moving a branch and could relink. Position tmpParentContainer
if (targetItem && targetItem->hasTypeBranch() && bc_first &&
!(mainWindow->getModMode() == Main::ModModeMoveObject &&
(e->modifiers() & Qt::ShiftModifier))) {
setState(MovingObjectTmpLinked);
targetBranchContainer = ((BranchItem*)targetItem)->getBranchContainer();
Container *targetRefContainer = targetBranchContainer->getHeadingContainer();
Container *movingRefContainer = bc_first->getHeadingContainer();
Container::PointName targetRefPointName;
Container::PointName movingRefPointName;
QPointF linkOffset; // Distance for temporary link
BranchContainer *tbc = targetBranchContainer->parentBranchContainer();
if (e->modifiers() & Qt::ShiftModifier && tbc) {
qreal dy = targetBranchContainer->rect().height() / 2;
targetBranchContainer = targetBranchContainer->parentBranchContainer();
if (targetBranchContainer->getOrientation() == BranchContainer::RightOfParent) {
// Shift modifier: Link right above
targetRefPointName = Container::BottomRight;
movingRefPointName = Container::BottomLeft;
linkOffset = QPointF(model->mapDesign()->linkWidth(), - dy);
} else if (targetBranchContainer->getOrientation() == BranchContainer::LeftOfParent) {
// Shift modifier: Link left above
targetRefPointName = Container::TopLeft;
movingRefPointName = Container::BottomRight;
linkOffset = QPointF(- model->mapDesign()->linkWidth(), dy);
} // else: Undefined orientation is handled with hasFloatingLayout() below!
} else if (e->modifiers() & Qt::ControlModifier && tbc) {
qreal dy = targetBranchContainer->rect().height() / 2;
targetBranchContainer = targetBranchContainer->parentBranchContainer();
if (targetBranchContainer->getOrientation() == BranchContainer::RightOfParent) {
// Control modifier: Link right below
targetRefPointName = Container::TopRight;
movingRefPointName = Container::TopLeft;
linkOffset = QPointF(model->mapDesign()->linkWidth(), dy);
} else if (targetBranchContainer->getOrientation() == BranchContainer::LeftOfParent) {
// Control modifier: Link left below
targetRefPointName = Container::TopLeft;
movingRefPointName = Container::TopRight;
linkOffset = QPointF(- model->mapDesign()->linkWidth(), dy);
} // else: Undefined orientation is handled with hasFloatingLayout() below!
} else {
// No modifier used, temporary link to target itself
targetRefContainer = targetBranchContainer->getBranchesContainer();
movingRefContainer = tmpParentContainer;
if (targetBranchContainer->getOrientation() == BranchContainer::RightOfParent) {
if (targetBranchContainer->branchCount() == 0) {
// vertically centered besides target
targetRefPointName = Container::RightCenter;
movingRefPointName = Container::LeftCenter;
linkOffset = QPointF(model->mapDesign()->linkWidth(), 0);
} else {
// Below target
targetRefPointName = Container::BottomLeft;
movingRefPointName = Container::TopLeft;
}
} else if (targetBranchContainer->getOrientation() == BranchContainer::LeftOfParent) {
if (targetBranchContainer->branchCount() == 0) {
// vertically centered besides target
targetRefPointName = Container::LeftCenter;
movingRefPointName = Container::RightCenter;
linkOffset = QPointF(- model->mapDesign()->linkWidth(), 0);
} else {
// Below target
targetRefPointName = Container::BottomRight;
movingRefPointName = Container::TopRight;
}
} // else: Undefined orientation is handled with hasFloatingLayout() below!
}
if (!targetRefContainer)
targetRefContainer = ((BranchItem*)targetItem)->getBranchContainer();
// Align tmpParentContainer
if (targetBranchContainer->hasFloatingBranchesLayout()) {
// When temporary linking e.g. to MapCenter, position on a circle around MC
qreal radius = 80;
QPointF center_sp = targetBranchContainer->getHeadingContainer()->mapToScene(QPointF(0,0));
qreal a = getAngle(p_event - center_sp);
QPointF p_hint = center_sp + QPointF (radius * cos(a), - radius * sin(a));
tmpParentContainer->setPos(p_hint);
} else
// Temporary link to branchContainers of targetRefContainer. Use position calculated above
tmpParentContainer->setPos(
linkOffset + movingRefContainer->mapToScene(
movingRefContainer->alignTo(
movingRefPointName, targetRefContainer, targetRefPointName)));
// Set states of MapEditor and tPC
if (tmpParentContainer->movingState() != BranchContainerBase::TemporaryLinked) {
// Link tmpParentContainer temporarily to targetBranchContainer
tmpParentContainer->setMovingState(SelectableContainer::TemporaryLinked, targetBranchContainer);
setState(MovingObjectTmpLinked);
}
repositionRequired = true;
} // tmp linking to target
else {
// Move without temporary relinking to a target
//
// Update state of MapEditor
if (mainWindow->getModMode() == Main::ModModeMoveObject &&
e->modifiers() & Qt::ShiftModifier)
setState(MovingObjectWithoutLinking);
else
setState(MovingObject);
if (tmpParentContainer->movingState() == BranchContainerBase::TemporaryLinked)
tmpParentContainer->setMovingState(SelectableContainer::Moving);
updateUpLinksRequired = true;
}
// Update states of children branch containers (for updating links later)
foreach (BranchContainer *bc, tmpParentContainer->childBranches()) {
if (tmpParentContainer->movingState() == BranchContainerBase::TemporaryLinked)
bc->setMovingState(SelectableContainer::TemporaryLinked, targetBranchContainer);
else
bc->setMovingState(SelectableContainer::Moving);
}
foreach (ImageContainer *ic, tmpParentContainer->childImages()) {
if (tmpParentContainer->movingState() == BranchContainerBase::TemporaryLinked)
ic->setMovingState(SelectableContainer::TemporaryLinked, targetBranchContainer);
else
ic->setMovingState(SelectableContainer::Moving);
}
// Set orientation
BranchContainer::Orientation newOrientation;
Container *tpc_bc = tmpParentContainer->getBranchesContainer();
if (targetBranchContainer && tpc_bc && !tpc_bc->childItems().contains(targetBranchContainer)) {
// tmpParentContainer has children and these do NOT contain targetBranchContainer
if (targetBranchContainer->hasFloatingBranchesLayout()) {
if (p_event.x() > targetBranchContainer->getHeadingContainer()->mapToScene(QPointF(0,0)).x())
newOrientation = BranchContainer::RightOfParent;
else
newOrientation = BranchContainer::LeftOfParent;
} else {
// Relinking to other branch
newOrientation = targetBranchContainer->getOrientation();
}
} else {
// No target branch
if (bc_first) {
// Set new orientation for branches (not mapCenters): Consider pointer pos relative to first moving branch
if (p_event.x() > bc_first->getOriginalParentPos().x() && !(e->modifiers() & Qt::ControlModifier))
newOrientation = BranchContainer::RightOfParent;
else
newOrientation = BranchContainer::LeftOfParent;
} else
// No target and no branch moving. No orientation change.
newOrientation = tmpParentContainer->getOrientation();
}
// Reposition if required
if (newOrientation != tmpParentContainer->getOrientation()) {
// tPC has BoundingFloats layout, still children need orientation
tmpParentContainer->setOrientation(newOrientation);
repositionRequired = true;
}
if (repositionRequired) {
if (bc_first && targetBranchContainer && targetBranchContainer->hasFloatingBranchesLayout()) {
foreach(BranchContainer *bc, tmpParentContainer->childBranches())
bc->setOrientation(newOrientation);
}
tmpParentContainer->reposition();
}
if (!targetBranchContainer) {
// Above tPC was positioned only if there is a target, so now tPC->setPos() is required if there is no target
// Since orientation might have changed and position depends on orientation, only do this now
if (bc_first) {
QPointF hc_center = tmpParentContainer->mapFromItem(bc_first->getHeadingContainer(), bc_first->getHeadingContainer()->pos());
tmpParentContainer->setPos(p_event - hc_center - movingObj_initialContainerOffset);
// When moving with Ctrl modifier, don't children branches (in scene)
if (e->modifiers() & Qt::ControlModifier) {
foreach(BranchContainer *bc, tmpParentContainer->childBranches()) {
if (bc->hasFloatingBranchesLayout()) {
foreach(BranchContainer *bc2, bc->childBranches()) {
QPointF q = bc->getHeadingContainer()->sceneTransform().inverted().map(bc2->getOriginalPos());
bc2->setPos(q);
bc2->updateUpLink();
}
}
}
}
} else
// No branches, only image
tmpParentContainer->setPos(p_event - movingObj_initialContainerOffset);
}
if (updateUpLinksRequired) {
foreach(BranchContainer *bc, tmpParentContainer->childBranches())
bc->updateUpLink();
foreach(ImageContainer *ic, tmpParentContainer->childImages())
ic->updateUpLink();
}
model->repositionXLinks();
}
void MapEditor::mouseReleaseEvent(QMouseEvent *e)
{
// Allow selecting text in QLineEdit if necessary
if (model->isSelectionBlocked()) {
e->ignore();
QGraphicsView::mouseReleaseEvent(e);
return;
}
QPointF p = mapToScene(e->pos());
BranchItem *destinationBranch;
destinationBranch = findMapBranchItem(p, movingItems, true);
bool repositionNeeded = false;
// Have we been picking color?
if (editorState == PickingColor) {
setCursor(Qt::ArrowCursor);
// Check if we are over another branch
if (destinationBranch) {
if (e->modifiers() & Qt::ShiftModifier)
model->colorBranch(mainWindow->getCurrentColor());
else
model->colorSubtree(mainWindow->getCurrentColor());
}
setState(Neutral);
return;
}
// Have we been drawing a link?
if (editorState == CreatingXLink) {
setState(Neutral);
// Check if we are over another branch
if (destinationBranch) {
tmpXLink->setEndBranch(destinationBranch);
tmpXLink->activate();
tmpXLink->updateXLink();
if (model->createXLink(tmpXLink)) return;
}
delete (tmpXLink);
tmpXLink = nullptr;
return;
}
// Have we been moving something?
if (editorState == MovingObject || editorState == MovingObjectTmpLinked) {
panningTimer->stop();
// Check if we have a destination and should relink
if (destinationBranch && editorState != MovingObjectWithoutLinking) {
// Restore list of selected items later
// Prepare relinking
BranchItem *dst_branch = destinationBranch;
int dst_num = -1;
if (e->modifiers() & Qt::ShiftModifier && destinationBranch->parent()) {
// Link above dst
dst_branch = destinationBranch->parentBranch();
dst_num = destinationBranch->num();
} else if (e->modifiers() & Qt::ControlModifier && destinationBranch->parent()) {
// Link below dst
dst_branch = destinationBranch->parentBranch();
dst_num = destinationBranch->num() + 1;
}
// Tell VymModel to relink
QList <BranchItem*> movingBranches;
foreach(BranchContainer *bc, tmpParentContainer->childBranches()) {
bc->setMovingState(SelectableContainer::NotMoving);
movingBranches << bc->getBranchItem();
}
if (!movingBranches.isEmpty())
model->relinkBranches(
movingBranches,
dst_branch,
dst_num);
// If dst is scrolled, select it
if (dst_branch->isScrolled())
model->select(dst_branch);
// Loop over images // FIXME-3 refactor in VM similar to relinkBranches
foreach(ImageContainer *ic, tmpParentContainer->childImages()) {
ImageItem *ii = ic->getImageItem();
model->relinkImage(ii, destinationBranch);
}
if (!tmpParentContainer->childImages().isEmpty()) {
foreach(ImageContainer *ic, tmpParentContainer->childImages()) {
ImageItem *ii = ic->getImageItem();
model->selectToggle(ii);
}
}
} else {
// Branches moved, but not relinked
QList <BranchContainer*> childBranches = tmpParentContainer->childBranches();
QList <QPointF> animationCurrentPositions; // After reposition start animations
QList <BranchContainer*> animationContainers;
if (!childBranches.isEmpty()) {
repositionNeeded = true;
// We begin a saveStateScript. If nothing is really moved, this
// Script will be discarded later
model->saveStateBeginScript(
QString("Move %1 branch(es)").arg(childBranches.count())
);
// Empty the tmpParentContainer, which is used for moving
// Updating the stacking order also resets the original parents
foreach(BranchContainer *bc, childBranches) {
BranchItem *bi = bc->getBranchItem();
bc->setMovingState(SelectableContainer::NotMoving);
if (bc->isAnimated())
bc->stopAnimation();
// Relink container to original parent container
// and keep (!) current absolute position
bi->updateContainerStackingOrder();
// Floating layout e.g. MapCenter
if (bc->isFloating())
{
if (bi->depth() == 0)
// MapCenter
bc->setPos(bc->getHeadingContainer()->mapToScene(QPointF(0, 0)));
// Save position change
QString uc, rc;
uc = QString("setPos%1;").arg(toS(bc->getOriginalPos(), 5));
rc = QString("setPos%1;").arg(toS(bc->pos(), 5)),
model->saveStateBranch(bi, uc, rc, QString("Move branch to %1").arg(toS(bc->pos())));
} else {
if (!(e->modifiers() & Qt::ControlModifier)) {
// only animate snappack if not Ctrl-moving e.g. MC
animationContainers << bc;
animationCurrentPositions << bc->pos();
}
}
} // children of tmpParentContainer
model->saveStateEndScript();
} // Empty tmpParenContainer
if (animationUse && animationContainers.count() > 0) {
int i = 0;
foreach(BranchContainer *bc, animationContainers) {
startAnimation(bc, animationCurrentPositions.at(i), bc->getOriginalPos());
i++;
}
}
} // Branches moved, but not relinked
// Let's see if we moved images with tmpParentContainer
if (tmpParentContainer->childImages().count() > 0 ) {
repositionNeeded = true;
}
foreach(ImageContainer *ic, tmpParentContainer->childImages()) {
ImageItem *ii = ic->getImageItem();
BranchItem *pi = ii->parentBranch();
// Update parent of moved container to original imageContainer
// in parent branch
pi->addToImagesContainer(ic);
QString pold = toS(ic->getOriginalPos(), 5);
QString pnew = toS(ic->pos(), 5);
QString com = QString("Move image to %1").arg(pnew);
QString iv = model->setImageVar(ii);
QString uc = iv + QString("i.setPos%1;").arg(pold);
QString rc = iv + QString("i.setPos%1;").arg(pnew);
model->saveState(uc, rc, com);
} // Image moved, but not relinked
// Finally resize scene, if needed
scene()->update();
vPan = QPoint();
} // MovingObject or MovingObjecttmpXLinked
if (editorState != EditingHeading) {
setState(Neutral); // Continue editing after double click!
}
// Restore cursor
updateCursor();
movingItems.clear();
QGraphicsView::mouseReleaseEvent(e);
if (repositionNeeded) {
model->reposition(); // FIXME-3 really reposition whole model? Or only affected MapCenters?
minimizeView();
}
}
void MapEditor::mouseDoubleClickEvent(QMouseEvent *e)
{
// Allow selecting text in QLineEdit if necessary
if (model->isSelectionBlocked()) {
e->ignore();
QGraphicsView::mouseDoubleClickEvent(e);
return;
}
if (e->button() == Qt::LeftButton) {
QPointF p = mapToScene(e->pos());
TreeItem *ti = findMapItem(p);
if (ti) {
if (editorState == EditingHeading)
editHeadingFinished();
model->select(ti);
BranchItem *selbi = model->getSelectedBranch();
if (selbi) {
BranchContainer *bc = selbi->getBranchContainer();
QUuid uid = bc->findFlagByPos(p);
// Don't edit heading when double clicking flag:
if (!uid.isNull())
return;
}
e->accept();
editHeading();
}
}
}
void MapEditor::wheelEvent(QWheelEvent *e)
{
if (e->modifiers() & Qt::ControlModifier &&
e->angleDelta().y() != 0) {
qreal f_vp;
qreal f_zf;
if (e->angleDelta().y() > 0) {
// Zoom in
f_vp = 1 - zoomDelta; // vector to center of viewport shrinks
f_zf = 1 + zoomDelta + 0.046; // view transformation grows
} else {
// Zoom out
f_vp = 1 + zoomDelta;
f_zf = 1 - zoomDelta + 0.046;
}
if (rotationAnimation.state() == QAbstractAnimation::Running)
rotationAnimation.stop();
transformationOrigin = mapToScene(e->position().toPoint());
vp_center = mapToScene(viewport()->rect().center());
// Calculate center of scaled viewport with p as transformation origin
vp_center = (vp_center - transformationOrigin) * f_vp + transformationOrigin;
useTransformationOrigin = true;
//setZoomFactorTarget(zoomFactorTargetInt * f_zf);
setViewCenterTarget(vp_center, zoomFactorTargetInt * f_zf, rotationTargetInt);
}
else {
stopViewAnimations();
QGraphicsView::wheelEvent(e);
}
}
void MapEditor::focusOutEvent(QFocusEvent *)
{
// qDebug()<<"ME::focusOutEvent"<<e->reason();
if (editorState == EditingHeading)
editHeadingFinished();
}
void MapEditor::resizeEvent(QResizeEvent *e) { QGraphicsView::resizeEvent(e); }
void MapEditor::dragEnterEvent(QDragEnterEvent *event)
{
// for (unsigned int i=0;event->format(i);i++) // Debug mime type
// cerr << event->format(i) << endl;
if (event->mimeData()->hasImage())
event->acceptProposedAction();
else if (event->mimeData()->hasUrls())
event->acceptProposedAction();
}
void MapEditor::dragMoveEvent(QDragMoveEvent *) {}
void MapEditor::dragLeaveEvent(QDragLeaveEvent *event) { event->accept(); }
void MapEditor::dropEvent(QDropEvent *event)
{
BranchItem *selbi = model->getSelectedBranch();
if (selbi) {
if (debug) {
foreach (QString format, event->mimeData()->formats())
qDebug() << "MapEditor: Dropped format: " << qPrintable(format);
foreach (QUrl url, event->mimeData()->urls()) {
qDebug() << " URL-path:" << url.path();
qDebug() << "URL-string:" << url.toString();
qDebug() << " enc:" << url.toEncoded();
qDebug() << " valid:" << url.isValid();
}
qDebug() << "-------------- mimeData -------------------";
qDebug() << "has-img : " << event->mimeData()->hasImage();
qDebug() << "has-urls: " << event->mimeData()->hasUrls();
qDebug() << " text: " << event->mimeData()->text();
qDebug() << "-------------------------------------------";
}
if (event->mimeData()->hasUrls()) {
// Try text representation first, which works on windows, but in
// Linux only for https, not local images
QString url = event->mimeData()->text();
if (url.isEmpty()) {
QByteArray ba =
event->mimeData()->urls().first().path().toLatin1();
QByteArray ba2;
for (int i = 0; i < ba.size(); i++)
if (ba.at(i) != 0)
ba2.append(ba.at(i));
url = ba2;
}
BranchItem *bi = nullptr;
// Workaround to avoid adding empty branches
if (!url.isEmpty()) {
if (url.startsWith("file://"))
url.remove(0, 7);
#if defined(Q_OS_WIN32)
if (url.startsWith("/"))
url.remove(0, 1);
#endif
if (isImage(url)) {
if (debug)
qDebug() << "dropped url seems to be image: " << url;
// Image, try to download or set image from local file
if (url.startsWith("http"))
model->downloadImage(url);
else
model->loadImage(bi, url);
if (debug)
qDebug() << "finished loading image";
}
else {
bi = model->addNewBranch();
if (bi) {
model->select(bi);
if (url.endsWith(".vym", Qt::CaseInsensitive))
model->setVymLink(url);
else {
model->setUrl(url);
// Shorten long URLs for heading
int i = url.indexOf("?");
QString url_short = url.left(i);
if (i > 0)
url_short = url_short + "...";
model->setHeadingPlainText(url_short);
}
model->select(bi->parent());
}
}
}
}
}
event->acceptProposedAction();
}
void MapEditor::updateCursor()
{
if (qApp->queryKeyboardModifiers() & Qt::ShiftModifier) {
switch (mainWindow->getModMode()) {
case Main::ModModePoint:
setCursor(Qt::ArrowCursor);
break;
case Main::ModModeColor:
setCursor(PickColorCursor);
break;
case Main::ModModeXLink:
setCursor(XLinkCursor);
break;
case Main::ModModeMoveObject:
setCursor(Qt::PointingHandCursor);
break;
case Main::ModModeMoveView:
setCursor(Qt::OpenHandCursor);
break;
default:
setCursor(Qt::ArrowCursor);
break;
}
} else {
setCursor(Qt::ArrowCursor);
}
}
void MapEditor::setState(EditorState s)
{
editorState = s;
/* if (debug) {
QString s;
switch (state) {
case Neutral:
s = "Neutral";
break;
case EditingHeading:
s = "EditingHeading";
break;
case EditingLink:
s = "EditingLink";
break;
case MovingObject:
s = "MovingObject";
break;
case MovingObjectWithoutLinking:
s = "MovingObjectWithoutLinking";
break;
case MovingView:
s = "MovingView";
break;
case PickingColor:
s = "PickingColor";
break;
case CreatingXLink:
s = "CreatingXLink";
break;
default:
s = "Unknown editor state";
break;
}
qDebug() << "MapEditor: State " << s << " of " << model->getMapName();
}
*/
}
MapEditor::EditorState MapEditor::state() { return editorState; }
MapEditor::SelectionMode MapEditor::currentSelectionMode(TreeItem *selti)
{
// Selections should consider logical relations, e.g. siblings and parents
// but also geometric. Return the most appropriate mode depending on
// rotation of view and layout of selected item.
if (!selti) {
// qDebug() << "ME::selectionMode: Classic";
return ClassicSelection;
}
if (rotationInt != 0) {
// qDebug() << "ME::selectionMode: Geometric";
return GeometricSelection;
}
Container *c = nullptr;
if (selti->hasTypeBranch()) {
BranchContainer *bc = ((BranchItem*)selti)->getBranchContainer();
if (bc->branchesContainerLayout() == Container::Horizontal) {
// qDebug() << "ME::selectionMode: OrgChart";
return OrgChartSelection;
}
if (bc->isFloating()) {
// qDebug() << "ME::selectionMode: Geometric";
return GeometricSelection;
}
} else if (selti->hasTypeImage()) {
// qDebug() << "ME::selectionMode: Geometric";
return GeometricSelection;
}
if (!c) {
// qDebug() << "ME::selectionMode: Classic";
return ClassicSelection;
}
if (c->isFloating()) {
// qDebug() << "ME::selectionMode: Geometric";
return GeometricSelection;
}
qDebug() << "ME::selectionMode: Classic";
return ClassicSelection;
}
void MapEditor::updateData(const QModelIndex &sel)
{
TreeItem *ti = static_cast<TreeItem *>(sel.internalPointer());
if (ti && ti->hasTypeBranch())
((BranchItem*)ti)->updateVisuals();
if (winter) {
QList<QRectF> obstacles;
BranchContainer *bc;
BranchItem *cur = nullptr;
BranchItem *prev = nullptr;
model->nextBranch(cur, prev);
while (cur) {
if (!cur->hasHiddenParent()) { // FIXME-4 avoid recursive calls here in winter
// Branches
bc = cur->getBranchContainer();
if (bc && bc->isVisible()) {
HeadingContainer *hc = bc->getHeadingContainer();
obstacles.append(hc->mapRectToScene(hc->boundingRect()));
}
}
model->nextBranch(cur, prev);
}
winter->setObstacles(obstacles);
}
}
void MapEditor::togglePresentationMode()
{
mainWindow->togglePresentationMode();
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.