File: chord.cpp

package info (click to toggle)
musescore-snapshot 3.2.s20190704%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 218,116 kB
  • sloc: cpp: 290,563; xml: 200,238; sh: 3,706; ansic: 1,447; python: 393; makefile: 222; perl: 82; pascal: 79
file content (3545 lines) | stat: -rw-r--r-- 135,446 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2002-2013 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "chord.h"
#include "note.h"
#include "xml.h"
#include "style.h"
#include "segment.h"
#include "text.h"
#include "measure.h"
#include "system.h"
#include "tuplet.h"
#include "hook.h"
#include "tie.h"
#include "arpeggio.h"
#include "score.h"
#include "tremolo.h"
#include "glissando.h"
#include "staff.h"
#include "part.h"
#include "utils.h"
#include "articulation.h"
#include "undo.h"
#include "chordline.h"
#include "lyrics.h"
#include "navigate.h"
#include "stafftype.h"
#include "stem.h"
#include "mscore.h"
#include "accidental.h"
#include "noteevent.h"
#include "pitchspelling.h"
#include "stemslash.h"
#include "ledgerline.h"
#include "drumset.h"
#include "key.h"
#include "sym.h"
#include "stringdata.h"
#include "beam.h"
#include "slur.h"
#include "fingering.h"

namespace Ms {

//---------------------------------------------------------
//   LedgerLineData
//---------------------------------------------------------

struct LedgerLineData {
      int   line;
      qreal minX, maxX;
      bool  visible;
      bool  accidental;
      };

//---------------------------------------------------------
//   upNote
//---------------------------------------------------------

Note* Chord::upNote() const
      {
      Q_ASSERT(!_notes.empty());

      Note* result = _notes.back();
      if (!staff())
            return result;

      const Staff* stf = staff();
      const StaffType* st  = stf->staffType(tick());
      if (st->isDrumStaff()) {
            for (Note* n : _notes) {
                  if (n->line() < result->line()) {
                        result = n;
                        }
                  }
            }
      else if (st->isTabStaff()) {
            int line = st->lines() - 1;      // start at bottom line
            int noteLine;
            // scan each note: if TAB strings are not in sequential order,
            // visual order of notes might not correspond to pitch order
            for (Note* n : _notes) {
                  noteLine = st->physStringToVisual(n->string());
                  if (noteLine < line) {
                        line   = noteLine;
                        result = n;
                        }
                  }
            }
      return result;
      }

//---------------------------------------------------------
//   downNote
//---------------------------------------------------------

Note* Chord::downNote() const
      {
      Q_ASSERT(!_notes.empty());

      Note* result = _notes.front();
      if (!staff())
            return result;

      const Staff* stf = staff();
      const StaffType* st  = stf->staffType(tick());
      if (st->isDrumStaff()) {
            for (Note* n : _notes) {
                  if (n->line() > result->line()) {
                        result = n;
                        }
                  }
            }
      else if (st->isTabStaff()) {
            int line        = 0;      // start at top line
            int noteLine;
            // scan each note: if TAB strings are not in sequential order,
            // visual order of notes might not correspond to pitch order
            for (Note* n : _notes) {
                  noteLine = st->physStringToVisual(n->string());
                  if (noteLine > line) {
                        line = noteLine;
                        result = n;
                        }
                  }
            }
      return result;
      }

//---------------------------------------------------------
//   upLine / downLine
//---------------------------------------------------------

int Chord::upLine() const
      {
      return (staff() && staff()->isTabStaff(tick())) ? upString()*2 : upNote()->line();
      }

int Chord::downLine() const
      {
      return (staff() && staff()->isTabStaff(tick())) ? downString()*2 : downNote()->line();
      }

//---------------------------------------------------------
//   upString / downString
//
//    return the topmost / bottommost string used by chord
//    Top and bottom refer to the DRAWN position, not the position in the instrument
//    (i.e., upside-down TAB are taken into account)
//
//    If no staff, always return 0
//    If staf is not a TAB, always returns TOP and BOTTOM staff lines
//---------------------------------------------------------

int Chord::upString() const
      {
      // if no staff or staff not a TAB, return 0 (=topmost line)
      if(!staff() || !staff()->isTabStaff(tick()))
            return 0;
      const Staff* st = staff();
      const StaffType* tab = st->staffType(tick());
      int       line = tab->lines() - 1;      // start at bottom line
      int                     noteLine;
      // scan each note: if TAB strings are not in sequential order,
      // visual order of notes might not correspond to pitch order
      size_t n = _notes.size();
      for (size_t i = 0; i < n; ++i) {
            noteLine = tab->physStringToVisual(_notes.at(i)->string());
            if (noteLine < line)
                  line = noteLine;
            }
      return line;
      }

int Chord::downString() const
      {
      if (!staff())                              // if no staff, return 0
            return 0;
      if (!staff()->isTabStaff(tick()))                // if staff not a TAB, return bottom line
            return staff()->lines(tick())-1;
      const Staff* st = staff();
      const StaffType* tab = st->staffType(tick());
      int line = 0;         // start at top line
      int noteLine;
      size_t n = _notes.size();
      for (size_t i = 0; i < n; ++i) {
            noteLine = tab->physStringToVisual(_notes.at(i)->string());
            if (noteLine > line)
                  line = noteLine;
            }
      return line;
      }

//---------------------------------------------------------
//   Chord
//---------------------------------------------------------

Chord::Chord(Score* s)
   : ChordRest(s)
      {
      _ledgerLines      = 0;
      _stem             = 0;
      _hook             = 0;
      _stemDirection    = Direction::AUTO;
      _arpeggio         = 0;
      _tremolo          = 0;
      _endsGlissando    = false;
      _noteType         = NoteType::NORMAL;
      _stemSlash        = 0;
      _noStem           = false;
      _playEventType    = PlayEventType::Auto;
      _crossMeasure     = CrossMeasure::UNKNOWN;
      _graceIndex   = 0;
      }

Chord::Chord(const Chord& c, bool link)
   : ChordRest(c, link)
      {
      if (link)
            score()->undo(new Link(this, const_cast<Chord*>(&c)));
      _ledgerLines = 0;

      for (Note* onote : c._notes) {
            Note* nnote = new Note(*onote, link);
            add(nnote);
            }
      for (Chord* gn : c.graceNotes()) {
            Chord* nc = new Chord(*gn, link);
            add(nc);
            }
      for (Articulation* a : c._articulations) {    // make deep copy
            Articulation* na = new Articulation(*a);
            if (link)
                  na->linkTo(a);
            na->setParent(this);
            na->setTrack(track());
            _articulations.append(na);
            }
      _stem          = 0;
      _hook          = 0;
      _endsGlissando = false;
      _arpeggio      = 0;
      _stemSlash     = 0;
      _tremolo       = 0;

      _graceIndex     = c._graceIndex;
      _noStem         = c._noStem;
      _playEventType  = c._playEventType;
      _stemDirection  = c._stemDirection;
      _noteType       = c._noteType;
      _crossMeasure   = CrossMeasure::UNKNOWN;

      if (c._stem)
            add(new Stem(*(c._stem)));
      if (c._hook)
            add(new Hook(*(c._hook)));
      if (c._stemSlash)
            add(new StemSlash(*(c._stemSlash)));
      if (c._arpeggio) {
            Arpeggio* a = new Arpeggio(*(c._arpeggio));
            add(a);
            if (link)
                  score()->undo(new Link(a, const_cast<Arpeggio*>(c._arpeggio)));
            }
      if (c._tremolo) {
            Tremolo* t = new Tremolo(*(c._tremolo));
            if (link) {
                  score()->undo(new Link(t, const_cast<Tremolo*>(c._tremolo)));
                  if (c._tremolo->twoNotes()) {
                        if (c._tremolo->chord1() == &c)
                              t->setChords(this, nullptr);
                        else
                              t->setChords(nullptr, this);
                        }
                  }
            add(t);
            }

      for (Element* e : c.el()) {
            if (e->isChordLine()) {
                  ChordLine* cl = toChordLine(e);
                  ChordLine* ncl = new ChordLine(*cl);
                  add(ncl);
                  if (link)
                        score()->undo(new Link(ncl, cl));
                  }
            }
      }

//---------------------------------------------------------
//   undoUnlink
//---------------------------------------------------------

void Chord::undoUnlink()
      {
      ChordRest::undoUnlink();
      for (Note* n : _notes)
            n->undoUnlink();
      for (Chord* gn : graceNotes())
            gn->undoUnlink();
      for (Articulation* a : _articulations)
            a->undoUnlink();
/*      if (_glissando)
            _glissando->undoUnlink(); */
      if (_arpeggio)
            _arpeggio->undoUnlink();
      if (_tremolo && !_tremolo->twoNotes())
            _tremolo->undoUnlink();

      for (Element* e : el()) {
            if (e->type() == ElementType::CHORDLINE)
                  e->undoUnlink();
            }
      }

//---------------------------------------------------------
//   ~Chord
//---------------------------------------------------------

Chord::~Chord()
      {
      qDeleteAll(_articulations);
      delete _arpeggio;
      if (_tremolo && _tremolo->chord1() == this) {
            if (_tremolo->chord2())
                  _tremolo->chord2()->setTremolo(0);
            delete _tremolo;
            }
      delete _stemSlash;
      delete _stem;
      delete _hook;
      for (LedgerLine* ll = _ledgerLines; ll;) {
            LedgerLine* llNext = ll->next();
            delete ll;
            ll = llNext;
            }
      qDeleteAll(_graceNotes);
      qDeleteAll(_notes);
      }

//---------------------------------------------------------
//   noteHeadWidth
//---------------------------------------------------------

qreal Chord::noteHeadWidth() const
      {
      qreal nhw = score()->noteHeadWidth();
      if (_noteType != NoteType::NORMAL)
            nhw *= score()->styleD(Sid::graceNoteMag);
      return nhw * mag();
      }

//---------------------------------------------------------
//   stemPosX
//    return Chord coordinates. Based on nominal notehead
//---------------------------------------------------------

qreal Chord::stemPosX() const
      {
      const Staff* stf = staff();
      const StaffType* st = stf ? stf->staffType(tick()) : 0;
      if (st && st->isTabStaff())
            return st->chordStemPosX(this) * spatium();
      return _up ? noteHeadWidth() : 0.0;
      }

//---------------------------------------------------------
//   stemPos
//    return page coordinates
//---------------------------------------------------------

QPointF Chord::stemPos() const
      {
      QPointF p(pagePos());

      const Staff* stf = staff();
      const StaffType* st = stf ? stf->staffType(tick()) : 0;
      if (st && st->isTabStaff())
            return st->chordStemPos(this) * spatium() + p;

      if (_up) {
            qreal nhw = _notes.size() == 1 ? downNote()->bboxRightPos() : noteHeadWidth();
            p.rx() += nhw;
            p.ry() += downNote()->pos().y();
            }
      else
            p.ry() += upNote()->pos().y();
      return p;
      }

//---------------------------------------------------------
//   stemPosBeam
//    return stem position of note on beam side
//    return page coordinates
//---------------------------------------------------------

QPointF Chord::stemPosBeam() const
      {
      qreal _spatium = spatium();
      QPointF p(pagePos());

      const Staff* stf = staff();
      const StaffType* st = stf ? stf->staffType(tick()) : 0;

      if (st && st->isTabStaff())
            return st->chordStemPosBeam(this) * _spatium + p;

      if (_up) {
            qreal nhw = noteHeadWidth();
            p.rx() += nhw;
            p.ry() += upNote()->pos().y();
            }
      else
            p.ry() += downNote()->pos().y();

      return p;
      }

//---------------------------------------------------------
//   setTremolo
//---------------------------------------------------------

void Chord::setTremolo(Tremolo* tr)
      {
      if (_tremolo && tr && tr == _tremolo)
            return;

      if (_tremolo) {
            if (_tremolo->twoNotes()) {
                  TDuration d;
                  const Fraction f = ticks();
                  if (f.numerator() > 0)
                        d = TDuration(f);
                  else {
                        d = _tremolo->durationType();
                        const int dots = d.dots();
                        d = d.shift(1);
                        d.setDots(dots);
                        }

                  setDurationType(d);
                  Chord* other = _tremolo->chord1() == this ? _tremolo->chord2() : _tremolo->chord1();
                  _tremolo = nullptr;
                  if (other)
                        other->setTremolo(nullptr);
                  }
            else
                  _tremolo = nullptr;
            }

      if (tr) {
            if (tr->twoNotes()) {
                  TDuration d = tr->durationType();
                  if (!d.isValid()) {
                        d = durationType();
                        const int dots = d.dots();
                        d = d.shift(-1);
                        d.setDots(dots);
                        tr->setDurationType(d);
                        }

                  setDurationType(d);
                  Chord* other = tr->chord1() == this ? tr->chord2() : tr->chord1();
                  _tremolo = tr;
                  if (other)
                        other->setTremolo(tr);
                  }
            else
                  _tremolo = tr;
            }
      else {
            _tremolo = nullptr;
            }
      }

//---------------------------------------------------------
//   add
//---------------------------------------------------------

void Chord::add(Element* e)
      {
      e->setParent(this);
      e->setTrack(track());
      switch(e->type()) {
            case ElementType::NOTE:
                  {
                  Note* note = toNote(e);
                  bool found = false;

                  // _notes should be sorted by line position,
                  // but it's often not yet possible since line is unknown
                  // use pitch instead, and line as a second sort criteria.

                  for (unsigned idx = 0; idx < _notes.size(); ++idx) {
                        if (note->pitch() <= _notes[idx]->pitch()) {
                              if (note->pitch() == _notes[idx]->pitch() && note->line() > _notes[idx]->line())
                                    _notes.insert(_notes.begin()+idx+1, note);
                              else
                                    _notes.insert(_notes.begin()+idx, note);
                              found = true;
                              break;
                              }
                        }
                  if (!found)
                        _notes.push_back(note);
                  note->connectTiedNotes();
                  if (voice() && measure() && note->visible())
                        measure()->setHasVoices(staffIdx(), true);
                  }
                  break;
            case ElementType::ARPEGGIO:
                  _arpeggio = toArpeggio(e);
                  break;
            case ElementType::TREMOLO:
                  setTremolo(toTremolo(e));
                  break;
            case ElementType::GLISSANDO:
                  _endsGlissando = true;
                  break;
            case ElementType::STEM:
                  Q_ASSERT(!_stem);
                  _stem = toStem(e);
                  break;
            case ElementType::HOOK:
                  _hook = toHook(e);
                  break;
            case ElementType::CHORDLINE:
                  el().push_back(e);
                  break;
            case ElementType::STEM_SLASH:
                  Q_ASSERT(!_stemSlash);
                  _stemSlash = toStemSlash(e);
                  break;
            case ElementType::CHORD:
                  {
                  Chord* gc = toChord(e);
                  Q_ASSERT(gc->noteType() != NoteType::NORMAL);
                  int idx = gc->graceIndex();
                  gc->setFlag(ElementFlag::MOVABLE, true);
                  _graceNotes.insert(_graceNotes.begin() + idx, gc);
                  }
                  break;
            case ElementType::LEDGER_LINE:
                  qFatal("Chord::add ledgerline");
                  break;
            case ElementType::ARTICULATION:
                  {
                  Articulation* a = toArticulation(e);
                  if (a->layoutCloseToNote()) {
                        auto i = _articulations.begin();
                        while (i != _articulations.end() && (*i)->layoutCloseToNote())
                              i++;
                        _articulations.insert(i, a);
                        }
                  else
                        _articulations.push_back(a);
                  }
                  break;
            default:
                  ChordRest::add(e);
                  break;
            }
      }

//---------------------------------------------------------
//   remove
//---------------------------------------------------------

void Chord::remove(Element* e)
      {
      if (!e)
            return;

      switch (e->type()) {
            case ElementType::NOTE:
                  {
                  Note* note = toNote(e);
                  auto i = std::find(_notes.begin(), _notes.end(), note);
                  if (i != _notes.end()) {
                        _notes.erase(i);
                        note->disconnectTiedNotes();
                        for (Spanner* s : note->spannerBack())
                              note->removeSpannerBack(s);
                        for (Spanner* s : note->spannerFor())
                              note->removeSpannerFor(s);
                        }
                  else
                        qDebug("Chord::remove() note %p not found!", e);
                  if (voice() && measure() && note->visible())
                        measure()->checkMultiVoices(staffIdx());
                  }
                  break;

            case ElementType::ARPEGGIO:
                  _arpeggio = 0;
                  break;
            case ElementType::TREMOLO:
                  setTremolo(nullptr);
                  break;
            case ElementType::GLISSANDO:
                  _endsGlissando = false;
                  break;
            case ElementType::STEM:
                  _stem = 0;
                  break;
            case ElementType::HOOK:
                  _hook = 0;
                  break;
            case ElementType::STEM_SLASH:
                  Q_ASSERT(_stemSlash);
                  if (_stemSlash->selected() && score())
                        score()->deselect(_stemSlash);
                  _stemSlash = 0;
                  break;
            case ElementType::CHORDLINE:
                  el().remove(e);
                  break;
            case ElementType::CHORD:
                  {
                  auto i = std::find(_graceNotes.begin(), _graceNotes.end(), toChord(e));
                  Chord* grace = *i;
                  grace->setGraceIndex(i - _graceNotes.begin());
                  _graceNotes.erase(i);
                  }
                  break;
            case ElementType::ARTICULATION:
                  {
                  Articulation* a = toArticulation(e);
                  if (!_articulations.removeOne(a))
                        qDebug("ChordRest::remove(): articulation not found");
                  }
                  break;
            default:
                  ChordRest::remove(e);
                  break;
            }
      }

//---------------------------------------------------------
//   maxHeadWidth
//---------------------------------------------------------

qreal Chord::maxHeadWidth() const
      {
      // determine max head width in chord
      qreal hw = 0;
      for (const Note* n : _notes) {
            qreal t = n->headWidth();
            if (t > hw)
                  hw = t;
            }
      return hw;
      }

//---------------------------------------------------------
//   addLedgerLines
//---------------------------------------------------------

void Chord::addLedgerLines()
      {
      // initialize for palette
      int track          = 0;                   // the track lines belong to
      // the line pos corresponding to the bottom line of the staff
      int lineBelow      = 8;                   // assuming 5-lined "staff"
      qreal lineDistance = 1;
      qreal mag         = 1;
      bool staffVisible  = true;

      if (segment()) { //not palette
            Fraction tick = segment()->tick();
            int idx       = staffIdx() + staffMove();
            track         = staff2track(idx);
            Staff* st     = score()->staff(idx);
            lineBelow     = (st->lines(tick) - 1) * 2;
            lineDistance  = st->lineDistance(tick);
            mag           = staff()->mag(tick);
            staffVisible  = !staff()->invisible();
            }

      // need ledger lines?
      if (downLine() <= lineBelow + 1 && upLine() >= -1)
            return;

      // the extra length of a ledger line with respect to notehead (half of it on each side)
      qreal extraLen = score()->styleP(Sid::ledgerLineLength) * mag * 0.5;
      qreal hw;
      qreal minX, maxX;                         // note extrema in raster units
      int   minLine, maxLine;
      bool  visible = false;
      qreal x;

      // scan chord notes, collecting visibility and x and y extrema
      // NOTE: notes are sorted from bottom to top (line no. decreasing)
      // notes are scanned twice from outside (bottom or top) toward the staff
      // each pass stops at the first note without ledger lines
      size_t n = _notes.size();
      for (size_t j = 0; j < 2; j++) {             // notes are scanned twice...
            int from, delta;
            vector<LedgerLineData> vecLines;
            hw = 0.0;
            minX  = maxX = 0;
            minLine = 0;
            maxLine = lineBelow;
            if (j == 0) {                       // ...once from lowest up...
                  from  = 0;
                  delta = +1;
                  }
            else {
                  from = int(n)-1;                   // ...once from highest down
                  delta = -1;
                  }
            for (int i = from; i < int(n) && i >= 0 ; i += delta) {
                  Note* note = _notes.at(i);
                  int l = note->line();

                  // if 1st pass and note not below staff or 2nd pass and note not above staff
                  if ((!j && l <= lineBelow + 1) || (j && l >= -1))
                        break;                  // stop this pass
                  // round line number to even number toward 0
                  if (l < 0)
                        l = (l + 1) & ~ 1;
                  else
                        l = l & ~ 1;

                  if (note->visible())          // if one note is visible,
                        visible = true;         // all lines between it and the staff are visible
                  hw = qMax(hw, note->headWidth());

                  //
                  // Experimental:
                  //  shorten ledger line to avoid collisions with accidentals
                  //
                  // bool accid = (note->accidental() && note->line() >= (l-1) && note->line() <= (l+1) );
                  //
                  // TODO : do something with this accid flag in the following code!
                  //

                  // check if note horiz. pos. is outside current range
                  // if more length on the right, increase range
//                  note->layout();

                  //ledger lines need the leftmost point of the notehead with a respect of bbox
                  x = note->pos().x() + note->bboxXShift();
                  if (x - extraLen < minX) {
                        minX  = x - extraLen;
                        // increase width of all lines between this one and the staff
                        for (auto& d : vecLines) {
                              if (!d.accidental && ((l < 0 && d.line >= l) || (l > 0 && d.line <= l)) )
                                    d.minX = minX ;
                              }
                        }
                  // same for left side
                  if (x + hw + extraLen > maxX) {
                        maxX = x + hw + extraLen;
                        for (auto& d : vecLines)
                              if ( (l < 0 && d.line >= l) || (l > 0 && d.line <= l) )
                                    d.maxX = maxX;
                        }

                  LedgerLineData lld;
                  // check if note vert. pos. is outside current range
                  // and, if so, add data for new line(s)
                  if (l < minLine) {
                        for (int i1 = l; i1 < minLine; i1 += 2) {
                              lld.line = i1;
                              if (lineDistance != 1.0)
                                    lld.line *= lineDistance;
                              lld.minX = minX;
                              lld.maxX = maxX;
                              lld.visible = visible;
                              lld.accidental = false;
                              vecLines.push_back(lld);
                              }
                        minLine = l;
                        }
                  if (l > maxLine) {
                        for (int i1 = maxLine+2; i1 <= l; i1 += 2) {
                              lld.line = i1;
                              if (lineDistance != 1.0)
                                    lld.line *= lineDistance;
                              lld.minX = minX;
                              lld.maxX = maxX;
                              lld.visible = visible;
                              lld.accidental = false;
                              vecLines.push_back(lld);
                              }
                        maxLine = l;
                        }
                  }
            if (minLine < 0 || maxLine > lineBelow) {
                  qreal _spatium = spatium();
                  qreal stepDistance = 0.5;     // staff() ? staff()->lineDistance() * 0.5 : 0.5;
                  for (auto lld : vecLines) {
                        LedgerLine* h = new LedgerLine(score());
                        h->setParent(this);
                        h->setTrack(track);
                        h->setVisible(lld.visible && staffVisible);
                        h->setLen(lld.maxX - lld.minX);
                        h->setPos(lld.minX, lld.line * _spatium * stepDistance);
                        h->setNext(_ledgerLines);
                        _ledgerLines = h;
                        }
                  }
            }
      for (LedgerLine* ll = _ledgerLines; ll; ll = ll->next())
            ll->layout();
      }

//-----------------------------------------------------------------------------
//   computeUp
//    rules:
//      single note:
//          All notes beneath the middle line: upward stems
//          All notes on or above the middle line: downward stems
//      two notes:
//          If the interval above the middle line is greater than the interval
//             below the middle line: downward stems
//          If the interval below the middle line is greater than the interval
//             above the middle line: upward stems
//          If the two notes are the same distance from the middle line:
//             stem can go in either direction. but most engravers prefer
//             downward stems
//      > two notes:
//          If the interval of the highest note above the middle line is greater
//             than the interval of the lowest note below the middle line:
//             downward stems
//          If the interval of the lowest note below the middle line is greater
//             than the interval of the highest note above the middle line:
//             upward stem
//          If the highest and the lowest notes are the same distance from
//          the middle line:, use these rules to determine stem direction:
//             - If the majority of the notes are above the middle:
//               downward stems
//             - If the majority of the notes are below the middle:
//               upward stems
//    TABlatures:
//       stems beside staves:
//          All stems are up / down according to TAB::stemsDown() setting
//       stems through staves:
//          Same rules as per pitched staves
//-----------------------------------------------------------------------------

void Chord::computeUp()
      {
      Q_ASSERT(!_notes.empty());
      const Staff* st = staff();
      const StaffType* tab = st ? st->staffType(tick()) : 0;
      bool tabStaff  = tab && tab->isTabStaff();
      // TAB STAVES
      if (tabStaff) {
            // if no stems or stem beside staves
            if (tab->slashStyle() || !tab->stemThrough()) {
                  // if measure has voices, set stem direction according to voice
                  if (measure()->hasVoices(staffIdx()))
                        _up = !(track() % 2);
                  else                          // if only voice 1,
                        // unconditionally set to down if not stems or according to TAB stem direction otherwise
                        // (even with no stems, stem direction controls position of slurs and ties)
                        _up = tab->slashStyle() ? false : !tab->stemsDown();
                  return;
                  }
            // if TAB has stems through staves, chain into standard processing
            }

      // PITCHED STAVES (or TAB with stems through staves)
      if (_stemDirection != Direction::AUTO)
            _up = _stemDirection == Direction::UP;
      else if (!parent())
            // hack for palette and drumset editor
            _up = upNote()->line() > 4;
      else if (_noteType != NoteType::NORMAL) {
            //
            // stem direction for grace notes
            //
            if (measure()->hasVoices(staffIdx()))
                  _up = !(track() % 2);
            else
                  _up = true;
            }
      else if (staffMove())
            _up = staffMove() > 0;
      else if (measure()->hasVoices(staffIdx()))
            _up = !(track() % 2);
      else {
            int   dnMaxLine   = staff()->middleLine(tick());
            int   ud          = (tabStaff ? upString() * 2 : upNote()->line() ) - dnMaxLine;
            // standard case: if only 1 note or cross beaming
            if (_notes.size() == 1 || staffMove()) {
                  if (staffMove() > 0)
                        _up = true;
                  else if (staffMove() < 0)
                        _up = false;
                  else
                        _up = ud > 0;
                  }
            // if more than 1 note, compare extrema (topmost and bottommost notes)
            else {
                  int dd = (tabStaff ? downString() * 2 : downNote()->line() ) - dnMaxLine;
                  // if extrema symmetrical, average directions of intermediate notes
                  if (-ud == dd) {
                        int up = 0;
                        size_t n = _notes.size();
                        for (size_t i = 0; i < n; ++i) {
                              const Note* currentNote = _notes.at(i);
                              int l = tabStaff ? currentNote->string() * 2 : currentNote->line();
                              if (l <= dnMaxLine)
                                    --up;
                              else
                                    ++up;
                              }
                        _up = up > 0;
                        }
                  // if extrema not symmetrical, set _up to prevailing
                  else
                        _up = dd > -ud;
                  }
            }
      }

//---------------------------------------------------------
//   selectedNote
//---------------------------------------------------------

Note* Chord::selectedNote() const
      {
      Note* note = 0;
      size_t n = _notes.size();
      for (size_t i = 0; i < n; ++i) {
            Note* currentNote = _notes.at(i);
            if (currentNote->selected()) {
                  if (note)
                        return 0;
                  note = currentNote;
                  }
            }
      return note;
      }

//---------------------------------------------------------
//   Chord::write
//---------------------------------------------------------

void Chord::write(XmlWriter& xml) const
      {
      for (Chord* c : _graceNotes) {
            c->write(xml);
            }
      writeBeam(xml);
      xml.stag(this);
      ChordRest::writeProperties(xml);
      for (const Articulation* a : _articulations)
            a->write(xml);
      switch (_noteType) {
            case NoteType::NORMAL:
                  break;
            case NoteType::ACCIACCATURA:
                  xml.tagE("acciaccatura");
                  break;
            case NoteType::APPOGGIATURA:
                  xml.tagE("appoggiatura");
                  break;
            case NoteType::GRACE4:
                  xml.tagE("grace4");
                  break;
            case NoteType::GRACE16:
                  xml.tagE("grace16");
                  break;
            case NoteType::GRACE32:
                  xml.tagE("grace32");
                  break;
            case NoteType::GRACE8_AFTER:
                  xml.tagE("grace8after");
                  break;
            case NoteType::GRACE16_AFTER:
                  xml.tagE("grace16after");
                  break;
            case NoteType::GRACE32_AFTER:
                  xml.tagE("grace32after");
                  break;
            default:
                  break;
            }

      if (_noStem)
            xml.tag("noStem", _noStem);
      else if (_stem && (_stem->isUserModified() || (_stem->userLen() != 0.0)))
            _stem->write(xml);
      if (_hook && _hook->isUserModified())
            _hook->write(xml);
      if (_stemSlash && _stemSlash->isUserModified())
            _stemSlash->write(xml);
      writeProperty(xml, Pid::STEM_DIRECTION);
      for (Note* n : _notes)
            n->write(xml);
      if (_arpeggio)
            _arpeggio->write(xml);
      if (_tremolo && tremoloChordType() != TremoloChordType::TremoloSecondNote)
            _tremolo->write(xml);
      for (Element* e : el())
            e->write(xml);
      xml.etag();
      }

//---------------------------------------------------------
//   Chord::read
//---------------------------------------------------------

void Chord::read(XmlReader& e)
      {
      while (e.readNextStartElement()) {
            if (readProperties(e))
                  ;
            else
                  e.unknown();
            }
      }

//---------------------------------------------------------
//   readProperties
//---------------------------------------------------------

bool Chord::readProperties(XmlReader& e)
      {
      const QStringRef& tag(e.name());

      if (tag == "Note") {
            Note* note = new Note(score());
            // the note needs to know the properties of the track it belongs to
            note->setTrack(track());
            note->setChord(this);
            note->read(e);
            add(note);
            }
      else if (ChordRest::readProperties(e))
            ;
      else if (tag == "Stem") {
            Stem* s = new Stem(score());
            s->read(e);
            add(s);
            }
      else if (tag == "Hook") {
            _hook = new Hook(score());
            _hook->read(e);
            add(_hook);
            }
      else if (tag == "appoggiatura") {
            _noteType = NoteType::APPOGGIATURA;
            e.readNext();
            }
      else if (tag == "acciaccatura") {
            _noteType = NoteType::ACCIACCATURA;
            e.readNext();
            }
      else if (tag == "grace4") {
            _noteType = NoteType::GRACE4;
            e.readNext();
            }
      else if (tag == "grace16") {
            _noteType = NoteType::GRACE16;
            e.readNext();
            }
      else if (tag == "grace32") {
            _noteType = NoteType::GRACE32;
            e.readNext();
            }
      else if (tag == "grace8after") {
            _noteType = NoteType::GRACE8_AFTER;
            e.readNext();
            }
      else if (tag == "grace16after") {
            _noteType = NoteType::GRACE16_AFTER;
            e.readNext();
            }
      else if (tag == "grace32after") {
            _noteType = NoteType::GRACE32_AFTER;
            e.readNext();
            }
      else if (tag == "StemSlash") {
            StemSlash* ss = new StemSlash(score());
            ss->read(e);
            add(ss);
            }
      else if (readProperty(tag, e, Pid::STEM_DIRECTION))
            ;
      else if (tag == "noStem")
            _noStem = e.readInt();
      else if (tag == "Arpeggio") {
            _arpeggio = new Arpeggio(score());
            _arpeggio->setTrack(track());
            _arpeggio->read(e);
            _arpeggio->setParent(this);
            }
      else if (tag == "Tremolo") {
            _tremolo = new Tremolo(score());
            _tremolo->setTrack(track());
            _tremolo->read(e);
            _tremolo->setParent(this);
            _tremolo->setDurationType(durationType());
            }
      else if (tag == "tickOffset")       // obsolete
            ;
      else if (tag == "ChordLine") {
            ChordLine* cl = new ChordLine(score());
            cl->read(e);
            add(cl);
            }
      else
            return false;
      return true;
      }

//---------------------------------------------------------
//   upPos
//---------------------------------------------------------

qreal Chord::upPos() const
      {
      return upNote()->pos().y();
      }

//---------------------------------------------------------
//   downPos
//---------------------------------------------------------

qreal Chord::downPos() const
      {
      return downNote()->pos().y();
      }

//---------------------------------------------------------
//   centerX
//    return x position for attributes
//---------------------------------------------------------

qreal Chord::centerX() const
      {
      // TAB 'notes' are always centered on the stem
      const Staff* st = staff();
      if (st->isTabStaff(tick()))
            return st->staffType(tick())->chordStemPosX(this) * spatium();

      const Note* note = up() ? upNote() : downNote();
      qreal x = note->pos().x() + note->noteheadCenterX();
      if (note->mirror())
                  x += (note->headBodyWidth()) * (up() ? -1.0 : 1.0);
      return x;
      }

//---------------------------------------------------------
//   scanElements
//---------------------------------------------------------

void Chord::scanElements(void* data, void (*func)(void*, Element*), bool all)
      {
      for (Articulation* a : _articulations)
            func(data, a);
      if (_hook)
            func(data, _hook );
      if (_stem)
            func(data, _stem);
      if (_stemSlash)
            func(data, _stemSlash);
      if (_arpeggio)
            func(data, _arpeggio);
      if (_tremolo && (tremoloChordType() != TremoloChordType::TremoloSecondNote))
            func(data, _tremolo);
      const Staff* st = staff();
      if ((st && st->showLedgerLines(tick())) || !st)       // also for palette
            for (LedgerLine* ll = _ledgerLines; ll; ll = ll->next())
                  func(data, ll);
      size_t n = _notes.size();
      for (size_t i = 0; i < n; ++i)
            _notes.at(i)->scanElements(data, func, all);
      for (Chord* chord : _graceNotes)
            chord->scanElements(data, func, all);
      for (Element* e : el())
            e->scanElements(data, func, all);
      ChordRest::scanElements(data, func, all);
      }

//---------------------------------------------------------
//   processSiblings
//---------------------------------------------------------

void Chord::processSiblings(std::function<void(Element*)> func) const
      {
      if (_hook)
            func(_hook);
      if (_stem)
            func(_stem);
      if (_stemSlash)
            func(_stemSlash);
      if (_arpeggio)
            func(_arpeggio);
      if (_tremolo)
            func(_tremolo);
      for (LedgerLine* ll = _ledgerLines; ll; ll = ll->next())
            func(ll);
      for (Articulation* a : _articulations)
            func(a);
      for (Note* note : _notes)
            func(note);
      for (Element* e : el())
            func(e);
      for (Chord* chord : _graceNotes)    // process grace notes last, needed for correct shape calculation
            func(chord);
      }

//---------------------------------------------------------
//   setTrack
//---------------------------------------------------------

void Chord::setTrack(int val)
      {
      ChordRest::setTrack(val);
      processSiblings([val] (Element* e) { e->setTrack(val); } );
      }

//---------------------------------------------------------
//   setScore
//---------------------------------------------------------

void Chord::setScore(Score* s)
      {
      ChordRest::setScore(s);
      processSiblings([s] (Element* e) { e->setScore(s); } );
      }

//-----------------------------------------------------------------------------
//   hookAdjustment
//    Adjustment to the length of the stem in order to accomodate hooks
//    This function replaces this bit of code:
//      switch (hookIdx) {
//            case 3: normalStemLen += small() ? .5  : 0.75; break; //32nd notes
//            case 4: normalStemLen += small() ? 1.0 : 1.5;  break; //64th notes
//            case 5: normalStemLen += small() ? 1.5 : 2.25; break; //128th notes
//            }
//    which was not sufficient for two reasons:
//      1. It only lengthened the stem for 3, 4, or 5 hooks.
//      2. It was too general to produce good results for all combinations of factors.
//    This provides a way to take a number of factors into account. Further tweaking may be in order.
//-----------------------------------------------------------------------------

qreal hookAdjustment(QString font, int hooks, bool up, bool small)
      {
      bool fallback = MScore::useFallbackFont && (hooks > 5);

      if (font == "Emmentaler" && !fallback) {
            if (up) {
                  if (hooks > 2)
                        return (hooks - 2) * (small ? .75 : 1);
                  }
            else {
                  if (hooks == 3)
                        return (small ? .75 : 1);
                  else if (hooks > 3)
                        return (hooks - 2) * (small ? .5 : .75);
                  }
            }
      else if (font == "Gonville" && !fallback) {
            if (up) {
                  if (hooks > 2)
                        return (hooks - 2) * (small ? .5 : .75);
                  }
            else {
                  if (hooks > 1)
                        return (hooks - 1) * (small ? .5 : .75);
                  }
            }
      else if (font == "MuseJazz") {
            if (hooks > 2)
                  return (hooks - 2) * (small ? .75 : 1);
            }
      else {
            if (hooks > 2)
                  return (hooks - 2) * (small ? .5 : .75);
            }
      return 0;
      }

//-----------------------------------------------------------------------------
//   defaultStemLength
///   Get the default stem length for this chord
//-----------------------------------------------------------------------------

qreal Chord::defaultStemLength() const
      {
      Note* downnote;
      qreal stemLen;
      qreal _spatium     = spatium();
      int hookIdx        = durationType().hooks();
      downnote           = downNote();
      int ul             = upLine();
      int dl             = downLine();
      const Staff* st    = staff();
      qreal lineDistance = st ? st->lineDistance(tick()) : 1.0;

      const StaffType* tab = (st && st->isTabStaff(tick())) ? st->staffType(tick()) : nullptr;
      if (tab) {
            // require stems only if TAB is not stemless and this chord has a stem
            if (!tab->slashStyle() && _stem) {
                  // if stems are beside staff, apply special formatting
                  if (!tab->stemThrough()) {
                        // process stem:
                        return tab->chordStemLength(this) * _spatium;
                        }
                  }
            }
      else if (lineDistance != 1.0) {
            // convert to actual distance from top of staff in sp
            // ul *= lineDistance;
            // dl *= lineDistance;
            }

      if (tab && !tab->onLines()) {       // if TAB and frets above strings, move 1 position up
            --ul;
            --dl;
            }
      bool shortenStem = score()->styleB(Sid::shortenStem);
      if (hookIdx >= 2 || _tremolo)
            shortenStem = false;

      Spatium progression = score()->styleS(Sid::shortStemProgression);
      qreal shortest      = score()->styleS(Sid::shortestStem).val();
      if (hookIdx) {
            if (up()) {
                  if (shortest < 3.0)
                        shortest = 3.0;
                  }
            else {
                  if (shortest < 3.5)
                        shortest = 3.5;
                  }
            }

      qreal normalStemLen = small() ? 2.5 : 3.5;
      normalStemLen += hookAdjustment(score()->styleSt(Sid::MusicalSymbolFont), hookIdx, up(), small());
      if (hookIdx && tab == 0) {
            if (up() && durationType().dots()) {
                  //
                  // avoid collision of dot with hook
                  //
                  if (!(ul & 1))
                        normalStemLen += .5;
                  shortenStem = false;
                  }
            }

      if (isGrace()) {
            // grace notes stems are not subject to normal
            // stem rules
            stemLen =  qAbs(ul - dl) * .5;
            stemLen += normalStemLen * score()->styleD(Sid::graceNoteMag);
            if (up())
                  stemLen *= -1;
            }
      else {
            // normal note (not grace)
            qreal staffHeight = st ? st->lines(tick()) - 1 : 4;
            if (!tab)
                  staffHeight *= lineDistance;
            qreal staffHlfHgt = staffHeight * 0.5;
            if (up()) {                   // stem up
                  qreal dy  = dl * .5;                      // note-side vert. pos.
                  qreal sel = ul * .5 - normalStemLen;      // stem end vert. pos

                  // if stem ends above top line (with some exceptions), shorten it
                  if (shortenStem && (sel < 0.0) && (hookIdx == 0 || tab || !downnote->mirror()))
                        sel -= sel  * progression.val();
                  if (sel > staffHlfHgt)                    // if stem ends below ('>') staff mid position,
                        sel = staffHlfHgt;                  // stretch it to mid position
                  stemLen = sel - dy;                       // actual stem length
                  if (-stemLen < shortest)                  // is stem too short,
                        stemLen = -shortest;                // lengthen it to shortest possible length
                  }
            else {                        // stem down
                  qreal uy  = ul * .5;                      // note-side vert. pos.
                  qreal sel = dl * .5 + normalStemLen;      // stem end vert. pos.

                  // if stem ends below bottom line (with some exceptions), shorten it
                  if (shortenStem && (sel > staffHeight) && (hookIdx == 0 || tab || downnote->mirror()))
                        sel -= (sel - staffHeight)  * progression.val();
                  if (sel < staffHlfHgt)                    // if stem ends above ('<') staff mid position,
                        sel = staffHlfHgt;                  // stretch it to mid position
                  stemLen = sel - uy;                       // actual stem length
                  if (stemLen < shortest)                   // if stem too short,
                        stemLen = shortest;                 // lengthen it to shortest possible position
                  }
            }

      // adjust stem len for tremolo
      if (_tremolo && !_tremolo->twoNotes() && !_tremolo->placeMidStem()) {
            // Use the old algorithm for stem lengthening. It not always
            // optimal but still performs better when not placing the tremolo
            // at stem middle. TODO: rework minAbsStemLen() to perform
            // correctly in this case too.

            // hook up odd lines
            static const int tab1[2][2][2][4] = {
                  { { { 0, 0, 0,  1 },  // stem - down - even - lines
                      { 0, 0, 0,  2 }   // stem - down - odd - lines
                      },
                    { { 0, 0, 0, -1 },  // stem - up - even - lines
                      { 0, 0, 0, -2 }   // stem - up - odd - lines
                      }
                    },
                  { { { 0, 0, 1, 2 },   // hook - down - even - lines
                      { 0, 0, 1, 2 }    // hook - down - odd - lines
                      },
                    { { 0, 0, -1, -2 }, // hook - up - even - lines
                      { 0, 0, -1, -2 }  // hook - up - odd - lines
                      }
                    }
                  };
            int odd = (up() ? upLine() : downLine()) & 1;
            int n = tab1[hookIdx ? 1 : 0][up() ? 1 : 0][odd][_tremolo->lines()-1];
            stemLen += n * .5;
            }

      if (tab)
            stemLen *= lineDistance;

      const qreal sgn = up() ? -1.0 : 1.0;
      qreal stemLenPoints = stemLen * _spatium;
      const qreal minAbsStemLen = minAbsStemLength();
      if (sgn * stemLenPoints < minAbsStemLen)
            stemLenPoints = sgn * minAbsStemLen;

      return stemLenPoints;
      }

//---------------------------------------------------------
//   minAbsStemLength
//---------------------------------------------------------

qreal Chord::minAbsStemLength() const
      {
      if (!_tremolo || _tremolo->twoNotes() || !_tremolo->placeMidStem())
            return 0.0;

      int beamLvl = beams();
      const bool hasHook = (beamLvl > 0) && !beam();
      if (hasHook)
            ++beamLvl; // reserve more space for stem with both hook and tremolo
      const qreal beamDist = beam() ? beam()->beamDist() : (0.5 * spatium());
      const qreal tremoloSpacing = 0.5 * spatium(); // TODO: style setting

      return beamLvl * beamDist + _tremolo->height() + 2 * tremoloSpacing;
      }

//---------------------------------------------------------
//   layoutStem1
///   Layout _stem and _stemSlash
//
//    Called before layout spacing of notes.
//    Create stem if necessary.
//---------------------------------------------------------

void Chord::layoutStem1()
      {
      const Staff* stf = staff();
      const StaffType* st = stf ? stf->staffType(tick()) : 0;
      if (durationType().hasStem() && !(_noStem || (measure() && measure()->slashStyle(staffIdx())) || (st && st->isTabStaff() && st->slashStyle()))) {
            if (!_stem) {
                  Stem* stem = new Stem(score());
                  stem->setParent(this);
                  stem->setGenerated(true);
                  score()->undoAddElement(stem);
                  }
            if ((_noteType == NoteType::ACCIACCATURA) && !(beam() && beam()->elements().front() != this)) {
                  if (!_stemSlash)
                        add(new StemSlash(score()));
                  }
            else if (_stemSlash)
                  remove(_stemSlash);

            qreal stemWidth5 = _stem->lineWidth() * .5 * mag();
            _stem->rxpos()   = stemPosX() + (up() ? -stemWidth5 : +stemWidth5);
            _stem->setLen(defaultStemLength());
            }
      else {
            if (_stem)
                  score()->undoRemoveElement(_stem);
            if (_stemSlash)
                  score()->undoRemoveElement(_stemSlash);
            }
      }

//-----------------------------------------------------------------------------
//   layoutStem
///   Layout chord tremolo stem and hook.
//
//    hook: sets position
//-----------------------------------------------------------------------------

void Chord::layoutStem()
      {
      for (Chord* c : _graceNotes)
            c->layoutStem();
      if (_beam)
            return;

      // create hooks for unbeamed chords

      int hookIdx  = durationType().hooks();

      if (hookIdx && !(noStem() || measure()->slashStyle(staffIdx()))) {
            if (!hook()) {
                  Hook* hook = new Hook(score());
                  hook->setParent(this);
                  hook->setGenerated(true);
                  score()->undoAddElement(hook);
                  }
            hook()->setHookType(up() ? hookIdx : -hookIdx);
            }
      else if (hook())
            score()->undoRemoveElement(hook());

      //
      // TAB
      //
      const Staff* st = staff();
      const StaffType* tab = st ? st->staffType(tick()) : 0;
      if (tab && tab->isTabStaff()) {
            // if stemless TAB
            if (tab->slashStyle()) {
                  // if 'grid' duration symbol of MEDIALFINAL type, it is time to compute its width
                  if (_tabDur != nullptr && _tabDur->beamGrid() == TabBeamGrid::MEDIALFINAL)
                        _tabDur->layout2();
                  // in all other stemless cases, do nothing
                  return;
                  }
            // not a stemless TAB; if stems are beside staff, apply special formatting
            if (!tab->stemThrough()) {
                  if (_stem) { // (duplicate code with defaultStemLength())
                        // process stem:
                        _stem->setLen(tab->chordStemLength(this) * spatium());
                        // process hook
                        hookIdx = durationType().hooks();
                        if (!up())
                              hookIdx = -hookIdx;
                        if (hookIdx && _hook) {
                              _hook->setHookType(hookIdx);
#if 0
                              _hook->layout();
                              QPointF p(_stem->hookPos());
                              if (up()) {
                                    p.ry() -= _hook->bbox().top();
                                    p.rx() -= _stem->width();
                                    }
                              else {
                                    p.ry() -= _hook->bbox().bottom();
                                    p.rx() -= _stem->width();
                                    }
                              _hook->setPos(p);
#endif
                              }
                        }
                  return;
                  }
            // if stems are through staff, use standard formatting
            }

      //
      // NON-TAB (or TAB with stems through staff)
      //
      if (_stem) {
            if (_hook) {
                  _hook->layout();
                  QPointF p(_stem->hookPos());
                  if (up()) {
                        p.ry() -= _hook->bbox().top();
                        p.rx() -= _stem->width();
                        }
                  else {
                        p.ry() -= _hook->bbox().bottom();
                        p.rx() -= _stem->width();
                        }
                  _hook->setPos(p);
                  }
            if (_stemSlash)
                  _stemSlash->layout();
            }

      //-----------------------------------------
      //    process tremolo
      //-----------------------------------------

//      if (_tremolo)
//            _tremolo->layout();
      }

//---------------------------------------------------------
//    underBeam: true, if grace note is placed under a beam.
//---------------------------------------------------------

bool Chord::underBeam() const
      {
      if (_noteType == NoteType::NORMAL)
          return false;
      const Chord* cr = toChord(parent());
      Beam* beam = cr->beam();
      if(!beam || !cr->beam()->up())
            return false;
      int s = beam->elements().count();
      if (isGraceBefore()){
            if (beam->elements()[0] != cr)
                return true;
            }
      if (isGraceAfter()){
            if (beam->elements()[s - 1] != cr)
                return true;
            }
      return false;
      }

//---------------------------------------------------------
//   layout2
//    Called after horizontal positions of all elements
//    are fixed.
//---------------------------------------------------------

void Chord::layout2()
      {
      for (Chord* c : _graceNotes)
            c->layout2();

      qreal mag = staff()->mag(tick());

      //
      // position after-chord grace notes
      // room for them has been reserved in Chord::layout()
      //

      QVector<Chord*> gna = graceNotesAfter();
      if (!gna.empty()) {
            qreal minNoteDist = score()->styleP(Sid::minNoteDistance) * mag * score()->styleD(Sid::graceNoteMag);
            // position grace notes from the rightmost to the leftmost
            // get segment (of whatever type) at the end of this chord; if none, get measure last segment
            Segment* s = measure()->tick2segment(segment()->tick() + actualTicks(), SegmentType::All);
            if (s == nullptr)
                  s = measure()->last();
            if (s == segment())           // if our segment is the last, no adjacent segment found
                  s = nullptr;
            // start from the right (if next segment found, x of it relative to this chord;
            // chord right space otherwise)
            Chord* last = gna.last();
            qreal xOff =  s ? (s->pos().x() - s->staffShape(last->vStaffIdx()).left()) - (segment()->pos().x() + pos().x()) : _spaceRw;
            // final distance: if near to another chord, leave minNoteDist at right of last grace
            // else leave note-to-barline distance;
            xOff -= (s != nullptr && s->segmentType() != SegmentType::ChordRest)
                  ? score()->styleP(Sid::noteBarDistance) * mag
                  : minNoteDist;
            // scan grace note list from the end
            int n = gna.size();
            for (int i = n-1; i >= 0; i--) {
                  Chord* g = gna.value(i);
                  xOff -= g->_spaceRw;                  // move to left by grace note left space (incl. grace own width)
                  g->rxpos() = xOff;
                  xOff -= minNoteDist + g->_spaceLw;    // move to left by grace note right space and inter-grace distance
                  }
            }
      }

//---------------------------------------------------------
//   updatePercussionNotes
//---------------------------------------------------------

static void updatePercussionNotes(Chord* c, const Drumset* drumset)
      {
      for (Chord* ch : c->graceNotes())
            updatePercussionNotes(ch, drumset);
      std::vector<Note*> lnotes(c->notes());  // we need a copy!
      for (Note* note : lnotes) {
            if (!drumset)
                  note->setLine(0);
            else {
                  int pitch = note->pitch();
                  if (!drumset->isValid(pitch)) {
                        note->setLine(0);
                        qWarning("unmapped drum note %d", pitch);
                        }
                  else if (!note->fixed()) {
                        note->undoChangeProperty(Pid::HEAD_GROUP, int(drumset->noteHead(pitch)));
                        note->setLine(drumset->line(pitch));
                        }
                  }
            }
      }

//---------------------------------------------------------
//   cmdUpdateNotes
//---------------------------------------------------------

void Chord::cmdUpdateNotes(AccidentalState* as)
      {
      // TAB_STAFF is different, as each note has to be fretted
      // in the context of the all of the chords of the whole segment

      const Staff* st = staff();
      StaffGroup staffGroup = st->staffType(tick())->group();
      if (staffGroup == StaffGroup::TAB) {
            const Instrument* instrument = part()->instrument();
            for (Chord* ch : graceNotes())
                  instrument->stringData()->fretChords(ch);
            instrument->stringData()->fretChords(this);
            return;
            }

      // PITCHED_ and PERCUSSION_STAFF can go note by note

      if (staffGroup == StaffGroup::STANDARD) {
            for (Chord* ch : graceNotesBefore()) {
                  std::vector<Note*> notes(ch->notes());  // we need a copy!
                  for (Note* note : notes)
                        note->updateAccidental(as);
                  ch->sortNotes();
                  }
            std::vector<Note*> lnotes(notes());  // we need a copy!
            for (Note* note : lnotes) {
                  if (note->tieBack() && note->tpc() == note->tieBack()->startNote()->tpc()) {
                        // same pitch
                        if (note->accidental() && note->accidental()->role() == AccidentalRole::AUTO) {
                              // not courtesy
                              // TODO: remove accidental only if note is not
                              // on new system
                              score()->undoRemoveElement(note->accidental());
                              }
                        }
                  note->updateAccidental(as);
                  }
            for (Chord* ch : graceNotesAfter()) {
                  std::vector<Note*> notes(ch->notes());  // we need a copy!
                  for (Note* note : notes)
                        note->updateAccidental(as);
                  ch->sortNotes();
                  }
            }
      else if (staffGroup == StaffGroup::PERCUSSION) {
            const Instrument* instrument = part()->instrument();
            const Drumset* drumset = instrument->drumset();
            if (!drumset)
                  qWarning("no drumset");
            updatePercussionNotes(this, drumset);
            }

      sortNotes();
      }

//---------------------------------------------------------
//   pagePos
//---------------------------------------------------------

QPointF Chord::pagePos() const
      {
      if (isGrace()) {
            QPointF p(pos());
            if (parent() == 0)
                  return p;
            p.rx() = pageX();

            const Chord* pc = static_cast<const Chord*>(parent());
            System* system = pc->segment()->system();
            if (!system)
                  return p;
            p.ry() += system->staffYpage(vStaffIdx());
            return p;
            }
      return Element::pagePos();
      }

//---------------------------------------------------------
//   layout
//---------------------------------------------------------

void Chord::layout()
      {
      if (_notes.empty())
            return;
      if (staff() && staff()->isTabStaff(tick()))
            layoutTablature();
      else
            layoutPitched();
      }

//---------------------------------------------------------
//   layoutPitched
//---------------------------------------------------------

void Chord::layoutPitched()
      {
      int gi = 0;
      for (Chord* c : _graceNotes) {
            // HACK: graceIndex is not well-maintained on add & remove
            // so rebuild now
            c->setGraceIndex(gi++);
            if (c->isGraceBefore())
                  c->layoutPitched();
            }
      QVector<Chord*> graceNotesBefore = Chord::graceNotesBefore();
      int gnb = graceNotesBefore.size();

      // lay out grace notes after separately so they are processed left to right
      // (they are normally stored right to left)

      QVector<Chord*> gna = graceNotesAfter();
      for (Chord* c : gna)
            c->layoutPitched();

      qreal _spatium         = spatium();
      qreal mag_             = staff() ? staff()->mag(tick()) : 1.0;    // palette elements do not have a staff
      qreal dotNoteDistance  = score()->styleP(Sid::dotNoteDistance)  * mag_;
      qreal minNoteDistance  = score()->styleP(Sid::minNoteDistance)  * mag_;
      qreal minTieLength     = score()->styleP(Sid::MinTieLength)     * mag_;

      qreal graceMag         = score()->styleD(Sid::graceNoteMag);
      qreal chordX           = (_noteType == NoteType::NORMAL) ? ipos().x() : 0.0;

      while (_ledgerLines) {
            LedgerLine* l = _ledgerLines->next();
            delete _ledgerLines;
            _ledgerLines = l;
            }

      qreal lll    = 0.0;         // space to leave at left of chord
      qreal rrr    = 0.0;         // space to leave at right of chord
      qreal lhead  = 0.0;         // amount of notehead to left of chord origin
      Note* upnote = upNote();

      delete _tabDur;   // no TAB? no duration symbol! (may happen when converting a TAB into PITCHED)
      _tabDur = 0;

      if (!segment()) {
            //
            // hack for use in palette
            //
            size_t n = _notes.size();
            for (size_t i = 0; i < n; i++) {
                  Note* note = _notes.at(i);
                  note->layout();
                  qreal x = 0.0;
                  qreal y = note->line() * _spatium * .5;
                  note->setPos(x, y);
                  }
            computeUp();
            layoutStem1();
            if (_stem) { //false when dragging notes from drum palette
                  qreal stemWidth5 = _stem->lineWidth() * .5;
                  _stem->rxpos()   = up() ? (upNote()->headBodyWidth() - stemWidth5) : stemWidth5;
                  }
            addLedgerLines();
            return;
            }

      //-----------------------------------------
      //  process notes
      //-----------------------------------------

      for (Note* note : _notes) {
            note->layout();

            qreal x1 = note->pos().x() + chordX;
            qreal x2 = x1 + note->headWidth();
            lll      = qMax(lll, -x1);
            rrr      = qMax(rrr, x2);
            // track amount of space due to notehead only
            lhead    = qMax(lhead, -x1);

            Accidental* accidental = note->accidental();
            if (accidental && !note->fixed()) {
                  // convert x position of accidental to segment coordinate system
                  qreal x = accidental->pos().x() + note->pos().x() + chordX;
                  // distance from accidental to note already taken into account
                  // but here perhaps we create more padding in *front* of accidental?
                  x -= score()->styleP(Sid::accidentalDistance) * mag_;
                  lll = qMax(lll, -x);
                  }

            // allow extra space for shortened ties
            // this code must be kept synchronized
            // with the tie positioning code in Tie::slurPos()
            // but the allocation of space needs to be performed here
            Tie* tie;
            tie = note->tieBack();
            if (tie) {
                  tie->calculateDirection();
                  qreal overlap = 0.0;
                  bool shortStart = false;
                  Note* sn = tie->startNote();
                  Chord* sc = sn->chord();
                  if (sc && sc->measure() == measure() && sc == prevChordRest(this)) {
                        if (sc->notes().size() > 1 || (sc->stem() && sc->up() == tie->up())) {
                              shortStart = true;
                              if (sc->width() > sn->width()) {
                                    // chord with second?
                                    // account for noteheads further to right
                                    qreal snEnd = sn->x() + sn->bboxRightPos();
                                    qreal scEnd = snEnd;
                                    for (unsigned i = 0; i < sc->notes().size(); ++i)
                                          scEnd = qMax(scEnd, sc->notes().at(i)->x() + sc->notes().at(i)->bboxRightPos());
                                    overlap += scEnd - snEnd;
                                    }
                              else
                                    overlap -= sn->headWidth() * 0.12;
                              }
                        else
                              overlap += sn->headWidth() * 0.35;
                        if (notes().size() > 1 || (stem() && !up() && !tie->up())) {
                              // for positive offset:
                              //    use available space
                              // for negative x offset:
                              //    space is allocated elsewhere, so don't re-allocate here
                              if (note->ipos().x() != 0.0)
                                    overlap += qAbs(note->ipos().x());
                              else
                                    overlap -= note->headWidth() * 0.12;
                              }
                        else {
                              if (shortStart)
                                    overlap += note->headWidth() * 0.15;
                              else
                                    overlap += note->headWidth() * 0.35;
                              }
                        qreal d = qMax(minTieLength - overlap, 0.0);
                        lll = qMax(lll, d);
                        }
                  }

            // clear layout for note-based fingerings
            for (Element* e : note->el()) {
                  if (e->isFingering()) {
                        Fingering* f = toFingering(e);
                        if (f->layoutType() == ElementType::NOTE) {
                              f->setPos(QPointF());
                              f->setbbox(QRectF());
                              }
                        }
                  }

            }

      //-----------------------------------------
      //  create ledger lines
      //-----------------------------------------

      addLedgerLines();

      if (_arpeggio) {
            qreal arpeggioDistance = score()->styleP(Sid::ArpeggioNoteDistance) * mag_;
            _arpeggio->layout();    // only for width() !
            _arpeggio->setHeight(0.0);
            lll        += _arpeggio->width() + arpeggioDistance + chordX;
            qreal y1   = upnote->pos().y() - upnote->headHeight() * .5;
            _arpeggio->setPos(-lll, y1);
            // _arpeggio->layout() called in layoutArpeggio2()

            // handle the special case of _arpeggio->span() > 1
            // in layoutArpeggio2() after page layout has done so we
            // know the y position of the next staves
            }

      // allocate enough room for glissandi
      if (_endsGlissando) {
            if (!rtick().isZero()                           // if not at beginning of measure
                        || graceNotesBefore.size() > 0)     // or there are graces before
                  lll += _spatium * 0.5 + minTieLength;
            // special case of system-initial glissando final note is handled in Glissando::layout() itself
            }

      if (dots()) {
            qreal x = dotPosX() + dotNoteDistance
               + (dots()-1) * score()->styleP(Sid::dotDotDistance) * mag_;
            x += symWidth(SymId::augmentationDot);
            rrr = qMax(rrr, x);
            }

      if (_hook) {
            if (beam())
                  score()->undoRemoveElement(_hook);
            else {
                  _hook->layout();
                  if (up() && stem()) {
                        // hook position is not set yet
                        qreal x = _hook->bbox().right() + stem()->hookPos().x() + chordX;
                        rrr = qMax(rrr, x);
                        }
                  }
            }

#if 0
      if (!_articulations.isEmpty()) {
            // TODO: allocate space to avoid "staircase" effect
            // but we would need to determine direction in order to get correct symid & bbox
            // another alternative is to limit the width contribution of the articulation in layoutArticulations2()
            //qreal aWidth = 0.0;
            for (Articulation* a : articulations())
                  a->layout();      // aWidth = qMax(aWidth, a->width());
            //qreal w = width();
            //qreal aExtra = (qMax(aWidth, w) - w) * 0.5;
            //lll = qMax(lll, aExtra);
            //rrr = qMax(rrr, aExtra);
            }
#endif

      _spaceLw = lll;
      _spaceRw = rrr;

      if (gnb) {
              qreal xl = -(_spaceLw + minNoteDistance) - chordX;
              for (int i = gnb-1; i >= 0; --i) {
                    Chord* g = graceNotesBefore.value(i);
                    xl -= g->_spaceRw/* * 1.2*/;
                    g->setPos(xl, 0);
                    xl -= g->_spaceLw + minNoteDistance * graceMag;
                    }
              if (-xl > _spaceLw)
                    _spaceLw = -xl;
              }
       if (!gna.empty()) {
            qreal xr = _spaceRw;
            int n = gna.size();
            for (int i = 0; i <= n - 1; i++) {
                  Chord* g = gna.value(i);
                  xr += g->_spaceLw + g->_spaceRw + minNoteDistance * graceMag;
                  }
           if (xr > _spaceRw)
                 _spaceRw = xr;
           }

      for (Element* e : el()) {
            if (e->type() == ElementType::SLUR)     // we cannot at this time as chordpositions are not fixed
                  continue;
            e->layout();
            if (e->type() == ElementType::CHORDLINE) {
                  QRectF tbbox = e->bbox().translated(e->pos());
                  qreal lx = tbbox.left() + chordX;
                  qreal rx = tbbox.right() + chordX;
                  if (-lx > _spaceLw)
                        _spaceLw = -lx;
                  if (rx > _spaceRw)
                        _spaceRw = rx;
                  }
            }

      for (Note* note : _notes)
            note->layout2();

      // align note-based fingerings
      std::vector<Fingering*> alignNote;
      qreal xNote = 10000.0;
      for (Note* note : _notes) {
            bool leftFound = false;
            for (Element* e : note->el()) {
                  if (e->isFingering() && e->autoplace()) {
                        Fingering* f = toFingering(e);
                        if (f->layoutType() == ElementType::NOTE && f->tid() == Tid::LH_GUITAR_FINGERING) {
                              alignNote.push_back(f);
                              if (!leftFound) {
                                    leftFound = true;
                                    qreal xf = f->ipos().x();
                                    xNote = qMin(xNote, xf);
                                    }
                              }
                        }
                  }
            }
      for (Fingering* f : alignNote)
            f->rxpos() = xNote;
      }

//---------------------------------------------------------
//   layoutTablature
//---------------------------------------------------------

void Chord::layoutTablature()
      {
      qreal _spatium          = spatium();
      qreal dotNoteDistance   = score()->styleP(Sid::dotNoteDistance);
      qreal minNoteDistance   = score()->styleP(Sid::minNoteDistance);
      qreal minTieLength      = score()->styleP(Sid::MinTieLength);

      for (Chord* c : _graceNotes)
            c->layoutTablature();

      while (_ledgerLines) {
            LedgerLine* l = _ledgerLines->next();
            delete _ledgerLines;
            _ledgerLines = l;
            }

      qreal lll         = 0.0;                  // space to leave at left of chord
      qreal rrr         = 0.0;                  // space to leave at right of chord
      Note* upnote      = upNote();
      qreal headWidth   = symWidth(SymId::noteheadBlack);
      const Staff* st   = staff();
      const StaffType* tab = st->staffType(tick());
      qreal lineDist    = tab->lineDistance().val() *_spatium;
      qreal stemX       = tab->chordStemPosX(this) *_spatium;
      int   ledgerLines = 0;
      qreal llY         = 0.0;

      size_t numOfNotes = _notes.size();
      qreal minY        = 1000.0;               // just a very large value
      for (size_t i = 0; i < numOfNotes; ++i) {
            Note* note = _notes.at(i);
            note->layout();
            // set headWidth to max fret text width
            qreal fretWidth = note->bbox().width();
            if (headWidth < fretWidth)
                  headWidth = fretWidth;
            // centre fret string on stem
            qreal x = stemX - fretWidth*0.5;
            qreal y = note->fixed() ? note->line() * lineDist / 2 : tab->physStringToYOffset(note->string()) * _spatium;
            note->setPos(x, y);
            if (y < minY)
                  minY  = y;
            int   currLedgerLines   = tab->numOfTabLedgerLines(note->string());
            if (currLedgerLines > ledgerLines) {
                  ledgerLines = currLedgerLines;
                  llY         = y;
                  }

            // allow extra space for shortened ties; this code must be kept synchronized
            // with the tie positioning code in Tie::slurPos()
            // but the allocation of space needs to be performed here
            Tie* tie;
            tie = note->tieBack();
            if (tie) {
                  tie->calculateDirection();
                  qreal overlap = 0.0;          // how much tie can overlap start and end notes
                  bool shortStart = false;      // whether tie should clear start note or not
                  Note* startNote = tie->startNote();
                  Chord* startChord = startNote->chord();
                  if (startChord && startChord->measure() == measure() && startChord == prevChordRest(this)) {
                        qreal startNoteWidth = startNote->width();
                        // overlap into start chord?
                        // if in start chord, there are several notes or stem and tie in same direction
                        if (startChord->notes().size() > 1 || (startChord->stem() && startChord->up() == tie->up())) {
                              // clear start note (1/8 of fret mark width)
                              shortStart = true;
                              overlap -= startNoteWidth * 0.125;
                              }
                        else        // overlap start note (by ca. 1/3 of fret mark width)
                              overlap += startNoteWidth * 0.35;
                        // overlap into end chord (this)?
                        // if several notes or neither stem or tie are up
                        if (notes().size() > 1 || (stem() && !up() && !tie->up())) {
                              // for positive offset:
                              //    use available space
                              // for negative x offset:
                              //    space is allocated elsewhere, so don't re-allocate here
                              if (note->ipos().x() != 0.0)              // this probably does not work for TAB, as
                                    overlap += qAbs(note->ipos().x());  // _pos is used to centre the fret on the stem
                              else
                                    overlap -= fretWidth * 0.125;
                              }
                        else {
                              if (shortStart)
                                    overlap += fretWidth * 0.15;
                              else
                                    overlap += fretWidth * 0.35;
                              }
                        qreal d = qMax(minTieLength - overlap, 0.0);
                        lll = qMax(lll, d);
                        }
                  }
            }

      // create ledger lines, if required (in some historic styles)
      if (ledgerLines > 0) {
// there seems to be no need for widening 'ledger lines' beyond fret mark widths; more 'on the field'
// tests and usage will show if this depends on the metrics of the specific fonts used or not.
//            qreal extraLen    = score()->styleS(Sid::ledgerLineLength).val() * _spatium;
            qreal extraLen    = 0;
            qreal llX         = stemX - (headWidth + extraLen) * 0.5;
            for (int i = 0; i < ledgerLines; i++) {
                  LedgerLine* ldgLin = new LedgerLine(score());
                  ldgLin->setParent(this);
                  ldgLin->setTrack(track());
                  ldgLin->setVisible(visible());
                  ldgLin->setLen(headWidth + extraLen);
                  ldgLin->setPos(llX, llY);
                  ldgLin->setNext(_ledgerLines);
                  _ledgerLines = ldgLin;
                  ldgLin->layout();
                  llY += lineDist / ledgerLines;
                  }
            headWidth += extraLen;        // include ledger lines extra width in chord width
            }

      // horiz. spacing: leave half width at each side of the (potential) stem
      qreal halfHeadWidth = headWidth * 0.5;
      if (lll < stemX - halfHeadWidth)
            lll = stemX - halfHeadWidth;
      if (rrr < stemX + halfHeadWidth)
            rrr = stemX + halfHeadWidth;
      // align dots to the widest fret mark (not needed in all TAB styles, but harmless anyway)
      if (segment())
            segment()->setDotPosX(staffIdx(), headWidth);
      // if tab type is stemless or chord is stemless (possible when imported from MusicXML)
      // or measure is stemless
      // or duration longer than half (if halves have stems) or duration longer than crochet
      // remove stems
      if (tab->slashStyle() || _noStem || measure()->slashStyle(staffIdx()) || durationType().type() <
         (tab->minimStyle() != TablatureMinimStyle::NONE ? TDuration::DurationType::V_HALF : TDuration::DurationType::V_QUARTER) ) {
            if (_stem)
                  score()->undo(new RemoveElement(_stem));
            if (_hook)
                  score()->undo(new RemoveElement(_hook));
            if (_beam)
                  score()->undo(new RemoveElement(_beam));
            }
      // if stem is required but missing, add it;
      // set stem position (stem length is set in Chord:layoutStem() )
      else {
            if (_stem == 0) {
                  Stem* stem = new Stem(score());
                  stem->setParent(this);
                  score()->undo(new AddElement(stem));
                  }
            _stem->setPos(tab->chordStemPos(this) * _spatium);
            if (_hook) {
                  if (beam())
                        score()->undoRemoveElement(_hook);
                  else {
                        _hook->layout();
                        if (rrr < stemX + _hook->width())
                              rrr = stemX + _hook->width();

                        QPointF p(_stem->hookPos());
                        if (up()) {
                              p.ry() -= _hook->bbox().top();
                              p.rx() -= _stem->width();
                              }
                        else {
                              p.ry() -= _hook->bbox().bottom();
                              p.rx() -= _stem->width();
                              }
                        _hook->setPos(p);
                        }
                  }
            }
      if (!tab->genDurations()                         // if tab is not set for duration symbols
            || track2voice(track())                    // or not in first voice
            || (isGrace()                              // no tab duration symbols if grace notes
                && beamMode() == Beam::Mode::AUTO)) {  // and beammode == AUTO
                                                       //
            delete _tabDur;   // delete an existing duration symbol
            _tabDur = 0;
            }
      else {
            //
            // tab duration symbols
            //
            // check duration of prev. CR segm
            ChordRest * prevCR = prevChordRest(this);
            // if no previous CR
            // OR symbol repeat set to ALWAYS
            // OR symbol repeat condition is triggered
            // OR duration type and/or number of dots is different from current CR
            // OR chord beam mode not AUTO
            // OR previous CR is a rest
            // AND no not-stem
            // set a duration symbol (trying to re-use existing symbols where existing to minimize
            // symbol creation and deletion)
            TablatureSymbolRepeat symRepeat = tab->symRepeat();
            if (  (prevCR == 0
                  || symRepeat == TablatureSymbolRepeat::ALWAYS
                  || (symRepeat == TablatureSymbolRepeat::MEASURE && measure() != prevCR->measure())
                  || (symRepeat == TablatureSymbolRepeat::SYSTEM && measure()->system() != prevCR->measure()->system())
                  || beamMode() != Beam::Mode::AUTO
                  || prevCR->durationType().type() != durationType().type()
                  || prevCR->dots() != dots()
                  || prevCR->tuplet() != tuplet()
                  || prevCR->type() == ElementType::REST)
                        && !noStem() ) {
                  // symbol needed; if not exist, create; if exists, update duration
                  if (!_tabDur)
                        _tabDur = new TabDurationSymbol(score(), tab, durationType().type(), dots());
                  else
                        _tabDur->setDuration(durationType().type(), dots(), tab);
                  _tabDur->setParent(this);
//                  _tabDur->setMag(mag());           // useless to set grace mag: graces have no dur. symbol
                  _tabDur->layout();
                  if (minY < 0) {                     // if some fret extends above tab body (like bass strings)
                        _tabDur->rypos() += minY;     // raise duration symbol
                        _tabDur->bbox().translate(0, minY);
                        }
                  }
            else {                              // symbol not needed: if exists, delete
                  delete _tabDur;
                  _tabDur = 0;
                  }
            }                             // end of if(duration_symbols)

      if (_arpeggio) {
            qreal headHeight = upnote->headHeight();
            _arpeggio->layout();
            lll += _arpeggio->width() + _spatium * .5;
            qreal y = upNote()->pos().y() - headHeight * .5;
            qreal h = downNote()->pos().y() + downNote()->headHeight() - y;
            _arpeggio->setHeight(h);
            _arpeggio->setPos(-lll, y);

            // handle the special case of _arpeggio->span() > 1
            // in layoutArpeggio2() after page layout has done so we
            // know the y position of the next staves
            }

      // allocate enough room for glissandi
      if (_endsGlissando) {
            if (!rtick().isZero())                        // if not at beginning of measure
                  lll += (0.5 + score()->styleS(Sid::MinTieLength).val()) * _spatium;
            // special case of system-initial glissando final note is handled in Glissando::layout() itself
            }

      if (_hook) {
            if (beam())
                  score()->undoRemoveElement(_hook);
            else if(tab == 0) {
                  _hook->layout();
                  if (up()) {
                        // hook position is not set yet
                        qreal x = _hook->bbox().right() + stem()->hookPos().x();
                        rrr = qMax(rrr, x);
                        }
                  }
            }

      if (dots()) {
            qreal x = 0.0;
            // if stems are beside staff, dots are placed near to stem
            if (!tab->stemThrough()) {
                  // if there is an unbeamed hook, dots should start after the hook
                  if (_hook && !beam())
                        x = _hook->width() + dotNoteDistance;
                  // if not, dots should start at a fixed distance right after the stem
                  else
                        x = STAFFTYPE_TAB_DEFAULTDOTDIST_X * _spatium;
                  if (segment())
                        segment()->setDotPosX(staffIdx(), x);
                  }
            // if stems are through staff, use dot position computed above on fret mark widths
            else
                  x = dotPosX() + dotNoteDistance
                        + (dots()-1) * score()->styleS(Sid::dotDotDistance).val() * _spatium;
            x += symWidth(SymId::augmentationDot);
            rrr = qMax(rrr, x);
            }

#if 0
      if (!_articulations.isEmpty()) {
            // TODO: allocate space? see layoutPitched()
            for (Articulation* a : articulations())
                  a->layout();
            }
#endif

      _spaceLw = lll;
      _spaceRw = rrr;

      qreal graceMag = score()->styleD(Sid::graceNoteMag);

      QVector<Chord*> graceNotesBefore = Chord::graceNotesBefore();
      int nb = graceNotesBefore.size();
      if (nb) {
              qreal xl = -(_spaceLw + minNoteDistance);
              for (int i = nb-1; i >= 0; --i) {
                    Chord* c = graceNotesBefore.value(i);
                    xl -= c->_spaceRw/* * 1.2*/;
                    c->setPos(xl, 0);
                    xl -= c->_spaceLw + minNoteDistance * graceMag;
                    }
              if (-xl > _spaceLw)
                    _spaceLw = -xl;
              }
       QVector<Chord*> gna = graceNotesAfter();
       int na = gna.size();
       if (na) {
           // get factor for start distance after main note. Values found by testing.
           qreal fc;
           switch (durationType().type()) {
                 case TDuration::DurationType::V_LONG:    fc = 3.8; break;
                 case TDuration::DurationType::V_BREVE:   fc = 3.8; break;
                 case TDuration::DurationType::V_WHOLE:   fc = 3.8; break;
                 case TDuration::DurationType::V_HALF:    fc = 3.6; break;
                 case TDuration::DurationType::V_QUARTER: fc = 2.1; break;
                 case TDuration::DurationType::V_EIGHTH:  fc = 1.4; break;
                 case TDuration::DurationType::V_16TH:    fc = 1.2; break;
                 default: fc = 1;
                 }
           qreal xr = fc * (_spaceRw + minNoteDistance);
           for (int i = 0; i <= na - 1; i++) {
                 Chord* c = gna.value(i);
                 xr += c->_spaceLw * (i == 0 ? 1.3 : 1);
                 c->setPos(xr, 0);
                 xr += c->_spaceRw + minNoteDistance * graceMag;
                 }
           if (xr > _spaceRw)
                 _spaceRw = xr;
           }
      for (Element* e : el()) {
            e->layout();
            if (e->type() == ElementType::CHORDLINE) {
                  QRectF tbbox = e->bbox().translated(e->pos());
                  qreal lx = tbbox.left();
                  qreal rx = tbbox.right();
                  if (-lx > _spaceLw)
                        _spaceLw = -lx;
                  if (rx > _spaceRw)
                        _spaceRw = rx;
                  }
            }

      for (size_t i = 0; i < numOfNotes; ++i)
            _notes.at(i)->layout2();
      QRectF bb;
      processSiblings([&bb] (Element* e) { bb |= e->bbox().translated(e->pos()); } );
      if (_tabDur)
            bb |= _tabDur->bbox().translated(_tabDur->pos());
      setbbox(bb);
      }

//---------------------------------------------------------
//   crossMeasureSetup
//---------------------------------------------------------

void Chord::crossMeasureSetup(bool on)
      {
      if (!on) {
            if (_crossMeasure != CrossMeasure::UNKNOWN) {
                  _crossMeasure = CrossMeasure::UNKNOWN;
                  layoutStem1();
                  }
            return;
            }
      if (_crossMeasure == CrossMeasure::UNKNOWN) {
            CrossMeasure tempCross = CrossMeasure::NONE;  // assume no cross-measure modification
            // if chord has only one note and note is tied forward
            if (notes().size() == 1 && _notes[0]->tieFor()) {
                  Chord* tiedChord = _notes[0]->tieFor()->endNote()->chord();
                  // if tied note belongs to another measure and to a single-note chord
                  if (tiedChord->measure() != measure() && tiedChord->notes().size() == 1) {
                        // get total duration
                        std::vector<TDuration> durList = toDurationList(
                                    actualDurationType().fraction() +
                                    tiedChord->actualDurationType().fraction(), true);
                        // if duration can be expressed as a single duration
                        // apply cross-measure modification
                        if (durList.size() == 1) {
                              _crossMeasure = tempCross = CrossMeasure::FIRST;
                              _crossMeasureTDur = durList[0];
                              layoutStem1();
                              }
                        }
                  _crossMeasure = tempCross;
                  tiedChord->setCrossMeasure(tempCross == CrossMeasure::FIRST ?
                              CrossMeasure::SECOND : CrossMeasure::NONE);
                  }
            }
      }

//---------------------------------------------------------
//   layoutArpeggio2
//    called after layout of page
//---------------------------------------------------------

void Chord::layoutArpeggio2()
      {
      if (!_arpeggio)
            return;
      qreal y           = upNote()->pagePos().y() - upNote()->headHeight() * .5;
      int span          = _arpeggio->span();
      int btrack        = track() + (span - 1) * VOICES;
      ChordRest* bchord = toChordRest(segment()->element(btrack));
      Note* dnote       = (bchord && bchord->type() == ElementType::CHORD) ? toChord(bchord)->downNote() : downNote();

      qreal h = dnote->pagePos().y() + dnote->headHeight() * .5 - y;
      _arpeggio->setHeight(h);
      _arpeggio->layout();

#if 0 // collect notes for arpeggio
      QList<Note*> notes;
      int n = _notes.size();
      for (int j = n - 1; j >= 0; --j) {
            Note* note = _notes[j];
            if (note->tieBack())
                  continue;
            notes.prepend(note);
            }

      for (int i = 1; i < span; ++i) {
            ChordRest* c = toChordRest(segment()->element(track() + i * VOICES));
            if (c && c->type() == CHORD) {
                  QList<Note*> nl = toChord(c)->notes();
                  int n = nl.size();
                  for (int j = n - 1; j >= 0; --j) {
                        Note* note = nl[j];
                        if (note->tieBack())
                              continue;
                        notes.prepend(note);
                        }
                  }
            }
#endif
      }

//---------------------------------------------------------
//   findNote
//---------------------------------------------------------

Note* Chord::findNote(int pitch) const
      {
      size_t ns = _notes.size();
      for (size_t i = 0; i < ns; ++i) {
            Note* n = _notes.at(i);
            if (n->pitch() == pitch)
                  return n;
            }
      return 0;
      }

//---------------------------------------------------------
//   drop
//---------------------------------------------------------

Element* Chord::drop(EditData& data)
      {
      Element* e = data.dropElement;
      switch (e->type()) {
            case ElementType::ARTICULATION:
                  {
                  Articulation* atr = toArticulation(e);
                  Articulation* oa = hasArticulation(atr);
                  if (oa) {
                        delete atr;
                        atr = 0;
                        // if attribute is already there, remove
                        // score()->cmdRemove(oa); // unexpected behaviour?
                        score()->select(oa, SelectType::SINGLE, 0);
                        }
                  else {
                        atr->setParent(this);
                        atr->setTrack(track());
                        score()->undoAddElement(atr);
                        }
                  return atr;
                  }

            case ElementType::CHORDLINE:
                  e->setParent(this);
                  e->setTrack(track());
                  score()->undoAddElement(e);
                  break;

            case ElementType::TREMOLO:
                  {
                  Tremolo* t = toTremolo(e);
                  if (t->twoNotes()) {
                        Segment* s = segment()->next();
                        while (s) {
                              if (s->element(track()) && s->element(track())->isChord())
                                    break;
                              s = s->next();
                              }
                        if (s == 0) {
                              qDebug("no segment for second note of tremolo found");
                              delete e;
                              return 0;
                              }
                        Chord* ch2 = toChord(s->element(track()));
                        if (ch2->ticks() != ticks()) {
                              qDebug("no matching chord for second note of tremolo found");
                              delete e;
                              return 0;
                             }
                        t->setChords(this, ch2);
                        }
                  }
                  if (tremolo())
                        score()->undoRemoveElement(tremolo());
                  e->setParent(this);
                  e->setTrack(track());
                  score()->undoAddElement(e);
                  break;

            case ElementType::ARPEGGIO:
                  {
                  Arpeggio* a = toArpeggio(e);
                  if (arpeggio())
                        score()->undoRemoveElement(arpeggio());
                  a->setTrack(track());
                  a->setParent(this);
                  a->setHeight(spatium() * 5);   //DEBUG
                  score()->undoAddElement(a);
                  }
                  return e;

            default:
                  return ChordRest::drop(data);
            }
      return 0;
      }

//---------------------------------------------------------
//   dotPosX
//---------------------------------------------------------

qreal Chord::dotPosX() const
      {
      if (parent())
            return segment()->dotPosX(staffIdx());
      return -1000.0;
      }

//---------------------------------------------------------
//   localSpatiumChanged
//---------------------------------------------------------

void Chord::localSpatiumChanged(qreal oldValue, qreal newValue)
      {
      ChordRest::localSpatiumChanged(oldValue, newValue);
      for (Element* e : graceNotes())
            e->localSpatiumChanged(oldValue, newValue);
      if (_hook)
            _hook->localSpatiumChanged(oldValue, newValue);
      if (_stem)
            _stem->localSpatiumChanged(oldValue, newValue);
      if (_stemSlash)
            _stemSlash->localSpatiumChanged(oldValue, newValue);
      if (arpeggio())
            arpeggio()->localSpatiumChanged(oldValue, newValue);
      if (_tremolo && (tremoloChordType() != TremoloChordType::TremoloSecondNote))
            _tremolo->localSpatiumChanged(oldValue, newValue);
      for (Element* e : articulations())
            e->localSpatiumChanged(oldValue, newValue);
      for (Note* note : notes())
            note->localSpatiumChanged(oldValue, newValue);
      }

//---------------------------------------------------------
//   getProperty
//---------------------------------------------------------

QVariant Chord::getProperty(Pid propertyId) const
      {
      switch (propertyId) {
            case Pid::NO_STEM:        return noStem();
            case Pid::SMALL:          return small();
            case Pid::STEM_DIRECTION: return QVariant::fromValue<Direction>(stemDirection());
            default:
                  return ChordRest::getProperty(propertyId);
            }
      }

//---------------------------------------------------------
//   propertyDefault
//---------------------------------------------------------

QVariant Chord::propertyDefault(Pid propertyId) const
      {
      switch (propertyId) {
            case Pid::NO_STEM:        return false;
            case Pid::SMALL:          return false;
            case Pid::STEM_DIRECTION: return QVariant::fromValue<Direction>(Direction::AUTO);
            default:
                  return ChordRest::propertyDefault(propertyId);
            }
      }

//---------------------------------------------------------
//   setProperty
//---------------------------------------------------------

bool Chord::setProperty(Pid propertyId, const QVariant& v)
      {
      switch (propertyId) {
            case Pid::NO_STEM:
                  setNoStem(v.toBool());
                  break;
            case Pid::SMALL:
                  setSmall(v.toBool());
                  break;
            case Pid::STEM_DIRECTION:
                  setStemDirection(v.value<Direction>());
                  break;
            default:
                  return ChordRest::setProperty(propertyId, v);
            }
      triggerLayout();
      return true;
      }

//---------------------------------------------------------
//   hasArticulation
//---------------------------------------------------------

Articulation* Chord::hasArticulation(const Articulation* aa)
      {
      for (Articulation* a : _articulations) {
            if (a->subtype() == aa->subtype())
                  return a;
            }
      return 0;
      }

//---------------------------------------------------------
//   reset
//---------------------------------------------------------

void Chord::reset()
      {
      undoChangeProperty(Pid::STEM_DIRECTION, QVariant::fromValue<Direction>(Direction::AUTO));
      undoChangeProperty(Pid::BEAM_MODE, int(Beam::Mode::AUTO));
      score()->createPlayEvents(this);
      ChordRest::reset();
      }

//---------------------------------------------------------
//   slash
//---------------------------------------------------------

bool Chord::slash()
      {
      Note* n = upNote();
      return n->fixed();
      }

//---------------------------------------------------------
//   setSlash
//---------------------------------------------------------

void Chord::setSlash(bool flag, bool stemless)
      {
      int line = 0;
      NoteHead::Group head = NoteHead::Group::HEAD_SLASH;

      if (!flag) {
            // restore to normal
            undoChangeProperty(Pid::NO_STEM, false);
            undoChangeProperty(Pid::SMALL, false);
            undoChangeProperty(Pid::OFFSET, QPointF());
            for (Note* n : _notes) {
                  n->undoChangeProperty(Pid::HEAD_GROUP, int(NoteHead::Group::HEAD_NORMAL));
                  n->undoChangeProperty(Pid::FIXED, false);
                  n->undoChangeProperty(Pid::FIXED_LINE, 0);
                  n->undoChangeProperty(Pid::PLAY, true);
                  n->undoChangeProperty(Pid::VISIBLE, true);
                  if (staff()->isDrumStaff(tick())) {
                        const Drumset* ds = part()->instrument()->drumset();
                        int pitch = n->pitch();
                        if (ds && ds->isValid(pitch)) {
                              undoChangeProperty(Pid::STEM_DIRECTION, QVariant::fromValue<Direction>(ds->stemDirection(pitch)));
                              n->undoChangeProperty(Pid::HEAD_GROUP, int(ds->noteHead(pitch)));
                              }
                        }
                  }
            return;
            }

      // set stem to auto (mostly important for rhythmic notation on drum staves)
      undoChangeProperty(Pid::STEM_DIRECTION, QVariant::fromValue<Direction>(Direction::AUTO));

      // make stemless if asked
      if (stemless) {
            undoChangeProperty(Pid::NO_STEM, true);
            undoChangeProperty(Pid::BEAM_MODE, int(Beam::Mode::NONE));
            }

      // voice-dependent attributes - line, size, offset, head
      if (track() % VOICES < 2) {
            // use middle line
            line = staff()->middleLine(tick());
            }
      else {
            // set small
            undoChangeProperty(Pid::SMALL, true);
            // set outside the staff
            qreal y = 0.0;
            if (track() % 2) {
                  line = staff()->bottomLine(tick()) + 1;
                  y    = 0.5 * spatium();
                  }
            else {
                  line = -1;
                  if (!staff()->isDrumStaff(tick()))
                        y = -0.5 * spatium();
                  }
            // for non-drum staves, add an additional offset
            // for drum staves, no offset, but use normal head
            if (!staff()->isDrumStaff(tick()))
                  // undoChangeProperty(Pid::OFFSET, QPointF(0.0, y));
                  rypos() += y;
            else
                  head = NoteHead::Group::HEAD_NORMAL;
            }

      size_t ns = _notes.size();
      for (size_t i = 0; i < ns; ++i) {
            Note* n = _notes[i];
            n->undoChangeProperty(Pid::HEAD_GROUP, static_cast<int>(head));
            n->undoChangeProperty(Pid::FIXED, true);
            n->undoChangeProperty(Pid::FIXED_LINE, line);
            n->undoChangeProperty(Pid::PLAY, false);
            // hide all but first notehead
            if (i)
                  n->undoChangeProperty(Pid::VISIBLE, false);
            }
      }

//---------------------------------------------------------
//  updateEndsGlissando
//    sets/resets the chord _endsGlissando according any glissando (or more)
//    end into this chord or no.
//---------------------------------------------------------

void Chord::updateEndsGlissando()
      {
      _endsGlissando = false;       // assume no glissando ends here
      // scan all chord notes for glissandi ending on this chord
      for (Note* note : notes()) {
            for (Spanner* sp : note->spannerBack())
                  if (sp->type() == ElementType::GLISSANDO) {
                        _endsGlissando = true;
                        return;
                        }
            }
      }

//---------------------------------------------------------
//   removeMarkings
//    - this is normally called after cloning a chord to tie a note over the barline
//    - there is no special undo handling; the assumption is that undo will simply remove the cloned chord
//    - two note tremolos are converted into simple notes
//    - single note tremolos are optionally retained
//---------------------------------------------------------

void Chord::removeMarkings(bool keepTremolo)
      {
      if (tremolo() && !keepTremolo)
            remove(tremolo());
      if (arpeggio())
            remove(arpeggio());
      qDeleteAll(graceNotes());
      graceNotes().clear();
      qDeleteAll(articulations());
      articulations().clear();
      for (Note* n : notes()) {
            for (Element* e : n->el())
                  n->remove(e);
            }
      ChordRest::removeMarkings(keepTremolo);
      }

//---------------------------------------------------------
//   mag
//---------------------------------------------------------

qreal Chord::mag() const
      {
      qreal m = staff() ? staff()->mag(tick()) : 1.0;
      if (small())
            m *= score()->styleD(Sid::smallNoteMag);
      if (_noteType != NoteType::NORMAL)
            m *= score()->styleD(Sid::graceNoteMag);
      return m;
      }

//---------------------------------------------------------
//   segment
//---------------------------------------------------------

Segment* Chord::segment() const
      {
      Element* e = parent();
      for (; e && e->type() != ElementType::SEGMENT; e = e->parent())
            ;
      return toSegment(e);
      }

//---------------------------------------------------------
//   measure
//---------------------------------------------------------

Measure* Chord::measure() const
      {
      Element* e = parent();
      for (; e && e->type() != ElementType::MEASURE; e = e->parent())
            ;
      return toMeasure(e);
      }

//---------------------------------------------------------
//   graceNotesBefore
//---------------------------------------------------------

QVector<Chord*> Chord::graceNotesBefore() const
      {
      QVector<Chord*> cl;
      for (Chord* c : _graceNotes) {
            Q_ASSERT(c->noteType() != NoteType::NORMAL && c->noteType() != NoteType::INVALID);
            if (c->noteType() & (
                 NoteType::ACCIACCATURA
               | NoteType::APPOGGIATURA
               | NoteType::GRACE4
               | NoteType::GRACE16
               | NoteType::GRACE32)) {
                  cl.push_back(c);
                  }
            }
      return cl;
      }

//---------------------------------------------------------
//   graceNotesAfter
//---------------------------------------------------------

QVector<Chord*> Chord::graceNotesAfter() const
      {
      QVector<Chord*> cl;
      for (int i = _graceNotes.size() - 1; i >= 0; i--) {
            Chord* c = _graceNotes[i];
            Q_ASSERT(c->noteType() != NoteType::NORMAL && c->noteType() != NoteType::INVALID);
            if (c->noteType() & (NoteType::GRACE8_AFTER | NoteType::GRACE16_AFTER | NoteType::GRACE32_AFTER))
                  cl.push_back(c);
            }
      return cl;
      }

//---------------------------------------------------------
//   sortNotes
//---------------------------------------------------------

void Chord::sortNotes()
      {
      std::sort(notes().begin(), notes().end(),
         [](const Note* a,const Note* b)->bool { return b->line() < a->line(); }
         );
     }

//---------------------------------------------------------
//   nextTiedChord
//    Return next chord if all notes in this chord are tied to it.
//    Set backwards=true to return the previous chord instead.
//
//    Note: the next chord might have extra notes that are not tied
//    back to this one. Set sameSize=true to return 0 in this case.
//---------------------------------------------------------

Chord* Chord::nextTiedChord(bool backwards, bool sameSize)
      {
      Segment* nextSeg = backwards ? segment()->prev1(SegmentType::ChordRest) : segment()->next1(SegmentType::ChordRest);
      if (!nextSeg)
            return 0;
      ChordRest* nextCR = nextSeg->nextChordRest(track(), backwards);
      if (!nextCR || !nextCR->isChord())
            return 0;
      Chord* next = toChord(nextCR);
      if (sameSize && notes().size() != next->notes().size())
            return 0; // sizes don't match so some notes can't be tied
      if (tuplet() != next->tuplet())
            return 0; // next chord belongs to a different tuplet
      for (Note* n : _notes) {
            Tie* tie = backwards ? n->tieBack() : n->tieFor();
            if (!tie)
                  return 0; // not tied
            Note* nn = backwards ? tie->startNote() : tie->endNote();
            if (!nn || nn->chord() != next)
                  return 0; // tied to note in wrong voice, or tied over rest
            }
      return next; // all notes in this chord are tied to notes in next chord
      }

//---------------------------------------------------------
//   toGraceAfter
//---------------------------------------------------------

void Chord::toGraceAfter()
      {
      switch (noteType()) {
            case NoteType::APPOGGIATURA:  setNoteType(NoteType::GRACE8_AFTER);  break;
            case NoteType::GRACE16:       setNoteType(NoteType::GRACE16_AFTER); break;
            case NoteType::GRACE32:       setNoteType(NoteType::GRACE32_AFTER); break;
            default: break;
            }
      }

//---------------------------------------------------------
//   tremoloChordType
//---------------------------------------------------------

TremoloChordType Chord::tremoloChordType() const
      {
      if (_tremolo && _tremolo->twoNotes()) {
            if (_tremolo->chord1() == this)
                  return TremoloChordType::TremoloFirstNote;
            else if (_tremolo->chord2() == this)
                  return TremoloChordType::TremoloSecondNote;
            else
                  qFatal("Chord::tremoloChordType(): inconsistency %p - %p, this is %p", _tremolo->chord1(), _tremolo->chord2(), this);
            }
      return TremoloChordType::TremoloSingle;
      }

//---------------------------------------------------------
//   nextElement
//---------------------------------------------------------

Element* Chord::nextElement()
      {
      Element* e = score()->selection().element();
      if (!e && !score()->selection().elements().isEmpty())
            e = score()->selection().elements().first();

      switch(e->type()) {
            case ElementType::SYMBOL:
            case ElementType::IMAGE:
            case ElementType::FINGERING:
            case ElementType::TEXT:
            case ElementType::BEND: {
                  Note* n = toNote(e->parent());
                  if(n == _notes.front()) {
                        if (_arpeggio)
                              return _arpeggio;
                        else if (_tremolo)
                              return _tremolo;
                        break;
                        }
                  for (auto &i : _notes) {
                        if (i == n) {
                              return *(&i-1);
                              }
                        }
                  break;
                  }

            case ElementType::GLISSANDO_SEGMENT:
            case ElementType::TIE_SEGMENT: {
                  SpannerSegment* s = toSpannerSegment(e);
                  Spanner* sp = s->spanner();
                  Element* elSt = sp->startElement();
                  Q_ASSERT(elSt->type() == ElementType::NOTE);
                  Note* n = toNote(elSt);
                  Q_ASSERT(n != NULL);
                  if (n == _notes.front()) {
                        if (_arpeggio)
                              return _arpeggio;
                        else if (_tremolo)
                              return _tremolo;
                        break;
                        }
                  for (auto &i : _notes) {
                        if (i == n) {
                              return *(&i-1);
                              }
                        }
                  break;
                  }
            case ElementType::ARPEGGIO:
                  if (_tremolo)
                        return _tremolo;
                  break;

            case ElementType::ACCIDENTAL:
                  e = e->parent();
                  // fall through

            case ElementType::NOTE: {
                  if (e == _notes.front()) {
                        if (_arpeggio)
                              return _arpeggio;
                        else if (_tremolo)
                              return _tremolo;
                        break;
                        }
                  for (auto &i : _notes) {
                        if (i == e)
                              return *(&i -1);
                        }
                  }
                  break;

            case ElementType::CHORD:
                  return _notes.back();

            default:
                  break;
            }

      return ChordRest::nextElement();
      }

//---------------------------------------------------------
//   prevElement
//---------------------------------------------------------

Element* Chord::prevElement()
      {
      Element* e = score()->selection().element();
      if (!e && !score()->selection().elements().isEmpty())
            e = score()->selection().elements().last();
      switch (e->type()) {
            case ElementType::NOTE: {
                  if (e == _notes.back())
                        break;
                  Note* prevNote = nullptr;
                  for (auto &i : _notes) {
                        if (i == e) {
                              prevNote = *(&i+1);
                              }
                        }
                  Element* next = prevNote->lastElementBeforeSegment();
                  return next;
                  }

            case ElementType::CHORD:
                  return _notes.front();

            case ElementType::TREMOLO:
                  if (_arpeggio)
                        return _arpeggio;
                  // fall through

            case ElementType::ARPEGGIO: {
                  Note* n = _notes.front();
                  Element* elN = n->lastElementBeforeSegment();
                  Q_ASSERT(elN != NULL);
                  return elN;
                  }

            default:
                  break;
            }
      return ChordRest::prevElement();
      }

//---------------------------------------------------------
//   lastElementBeforeSegment
//---------------------------------------------------------

Element* Chord::lastElementBeforeSegment()
      {
      if (_tremolo) {
            return _tremolo;
            }
      else if (_arpeggio) {
            return _arpeggio;
            }
      else {
            Note* n = _notes.front();
            Element* elN = n->lastElementBeforeSegment();
            Q_ASSERT(elN != NULL);
            return elN;
            }
      }

//---------------------------------------------------------
//   nextSegmentElement
//---------------------------------------------------------

Element* Chord::nextSegmentElement()
      {
      for (int v = track() + 1; staffIdx() == v/VOICES; ++v) {
            Element* e = segment()->element(v);
            if (e) {
                  if (e->type() == ElementType::CHORD)
                        return toChord(e)->notes().back();

                  return e;
                  }
            }

      return ChordRest::nextSegmentElement();
      }

//---------------------------------------------------------
//   prevSegmentElement
//---------------------------------------------------------

Element* Chord::prevSegmentElement()
      {
      Element* el = score()->selection().element();
      if (!el && !score()->selection().elements().isEmpty() )
            el = score()->selection().elements().first();
      Element* e = segment()->lastInPrevSegments(el->staffIdx());
      if (e) {
            if (e->isChord())
                  return toChord(e)->notes().front();
            return e;
            }

      return ChordRest::prevSegmentElement();
      }

//---------------------------------------------------------
//   accessibleExtraInfo
//---------------------------------------------------------

QString Chord::accessibleExtraInfo() const
      {
      QString rez = "";

      for (const Chord* c : graceNotes()) {
            if (!score()->selectionFilter().canSelect(c))
                  continue;
            for (const Note* n : c->notes())
                  rez = QString("%1 %2").arg(rez).arg(n->screenReaderInfo());
            }

      if (arpeggio() && score()->selectionFilter().canSelect(arpeggio()))
            rez = QString("%1 %2").arg(rez).arg(arpeggio()->screenReaderInfo());

      if (tremolo() && score()->selectionFilter().canSelect(tremolo()))
            rez = QString("%1 %2").arg(rez).arg(tremolo()->screenReaderInfo());

      foreach (Element* e, el()) {
            if (!score()->selectionFilter().canSelect(e))
                  continue;
            rez = QString("%1 %2").arg(rez).arg(e->screenReaderInfo());
            }

      return QString("%1 %2").arg(rez).arg(ChordRest::accessibleExtraInfo());
      }

//---------------------------------------------------------
//   shape
//    does not contain articulations
//---------------------------------------------------------

Shape Chord::shape() const
      {
      Shape shape;
      if (_hook && _hook->addToSkyline())
            shape.add(_hook->shape().translated(_hook->pos()));
      if (_stem && _stem->addToSkyline()) {
            // stem direction is not known soon enough for cross staff beamed notes
            if (!(beam() && (staffMove() || beam()->cross())))
                  shape.add(_stem->shape().translated(_stem->pos()));
            }
      if (_stemSlash && _stemSlash->addToSkyline())
            shape.add(_stemSlash->shape().translated(_stemSlash->pos()));
      if (_arpeggio && _arpeggio->addToSkyline())
            shape.add(_arpeggio->shape().translated(_arpeggio->pos()));
//      if (_tremolo)
//            shape.add(_tremolo->shape().translated(_tremolo->pos()));
      for (Note* note : _notes) {
            shape.add(note->shape().translated(note->pos()));
            for (Element* e : note->el()) {
                  if (!e->addToSkyline())
                        continue;
                  if (e->isFingering() && toFingering(e)->layoutType() == ElementType::CHORD && e->bbox().isValid())
                        shape.add(e->bbox().translated(e->pos() + note->pos()));
                  }
            }
      for (Element* e : el()) {
            if (e->addToSkyline())
                  shape.add(e->shape().translated(e->pos()));
            }
      for (Chord* chord : _graceNotes)    // process grace notes last, needed for correct shape calculation
            shape.add(chord->shape().translated(chord->pos()));
      shape.add(ChordRest::shape());      // add lyrics
      for (LedgerLine* l = _ledgerLines; l; l = l->next())
            shape.add(l->shape().translated(l->pos()));
      if (_spaceLw || _spaceRw)
            shape.addHorizontalSpacing(Shape::SPACING_GENERAL, -_spaceLw, _spaceRw);
      return shape;
      }

//---------------------------------------------------------
//   layoutArticulations
//    layout tenuto and staccato
//    called before layouting slurs
//---------------------------------------------------------

void Chord::layoutArticulations()
      {
      for (Chord* gc : graceNotes())
            gc->layoutArticulations();

      if (_articulations.empty())
            return;
      const Staff* st = staff();
      const StaffType* staffType = st->staffType(tick());
      qreal mag            = (staffType->small() ? score()->styleD(Sid::smallStaffMag) : 1.0) * staffType->userMag();
      qreal _spatium       = score()->spatium() * mag;
      qreal _spStaff       = _spatium * staffType->lineDistance().val();

      //
      //    determine direction
      //    place tenuto and staccato
      //

      Articulation* prevArticulation = nullptr;
      for (Articulation* a : _articulations) {
            if (a->anchor() == ArticulationAnchor::CHORD) {
                  if (measure()->hasVoices(a->staffIdx()))
                        a->setUp(up()); // if there are voices place articulation at stem
                  else if (a->symId() >= SymId::articMarcatoAbove && a->symId() <= SymId::articMarcatoTenutoBelow)
                        a->setUp(true); // Gould, p. 117: strong accents above staff
                  else if (isGrace() && up() && !a->layoutCloseToNote() && downNote()->line() < 6)
                        a->setUp(true); // keep articulation close to grace note
                  else
                        a->setUp(!up()); // place articulation at note head
                  }
            else
                  a->setUp(a->anchor() == ArticulationAnchor::TOP_STAFF || a->anchor() == ArticulationAnchor::TOP_CHORD);

            if (!a->layoutCloseToNote())
                  continue;

            bool bottom = !a->up();  // true: articulation is below chord;  false: articulation is above chord
            a->layout();             // must be done after assigning direction, or else symId is not reliable

            bool headSide = bottom == up();
            qreal x = centerX();
            qreal y = 0.0;

            if (bottom) {
                  if (!headSide && stem()) {
                        y = upPos() + stem()->stemLen();
                        if (beam())
                              y += score()->styleS(Sid::beamWidth).val() * _spatium * .5;
                        int line   = lrint((y + 0.5 * _spStaff) / _spStaff);
                        if (line < staffType->lines())  // align between staff lines
                              y = line * _spStaff + _spatium * .5;
                        else
                              y += _spatium;
                        if (a->isStaccato() && articulations().size() == 1) {
                              if (_up)
                                    x = downNote()->bboxRightPos() - stem()->width() * .5;
                              else
                                    x = stem()->width() * .5;
                              }
                        }
                  else {
                        int line = downLine();
                        int lines = (staffType->lines() - 1) * 2;
                        if (line < lines)
                              y = ((line & ~1) + 3) * _spStaff;
                        else
                              y = line * _spStaff + 2 * _spatium;
                        y *= .5;
                        }
                  if (prevArticulation && (prevArticulation->up() == a->up()))
                        y += _spatium;
                  y -= a->height() * .5;        // center symbol
                  }
            else {
                  if (!headSide && stem()) {
                        y = downPos() + stem()->stemLen();
                        if (beam())
                              y -= score()->styleS(Sid::beamWidth).val() * _spatium * .5;
                        int line   = lrint((y-0.5*_spStaff) / _spStaff);
                        if (line >= 0)  // align between staff lines
                              y = line * _spStaff - _spatium * .5;
                        else
                              y -= _spatium;
                        if (a->isStaccato() && articulations().size() == 1) {
                              if (_up)
                                    x = downNote()->bboxRightPos() - stem()->width() * .5;
                              else
                                    x = stem()->width() * .5;
                              }
                        }
                  else {
                        int line = upLine();
                        if (line > 0)
                              y = (((line+1) & ~1) - 3) * _spStaff;
                        else
                              y = line * _spStaff - 2 * _spatium;
                        y *= .5;
                        }
                  if (prevArticulation && (prevArticulation->up() == a->up()))
                        y -= _spatium;
                  y += a->height() * .5;        // center symbol
                  }
            a->setPos(x, y);
            prevArticulation = a;
//            measure()->system()->staff(a->staffIdx())->skyline().add(a->shape().translated(a->pos() + segment()->pos() + measure()->pos()));
            }
      }

//---------------------------------------------------------
//   layoutArticulations2
//    Called after layouting systems
//    Tentatively layout all articulations
//    To be finished after laying out slurs
//---------------------------------------------------------

void Chord::layoutArticulations2()
      {
      for (Chord* gc : graceNotes())
            gc->layoutArticulations2();

      if (_articulations.empty())
            return;
      qreal _spatium  = spatium();
      qreal x         = centerX();
      qreal distance0 = score()->styleP(Sid::propertyDistance);
      qreal distance2 = score()->styleP(Sid::propertyDistanceStem);

      qreal chordTopY = upPos();    // note position of highest note
      qreal chordBotY = downPos();  // note position of lowest note

      qreal staffTopY = -distance2;
      qreal staffBotY = staff()->height() + distance2;

      // avoid collisions of staff articulations with chord notes:
      // gap between note and staff articulation is distance0 + 0.5 spatium

      if (stem()) {
            qreal y = stem()->pos().y() + pos().y() + stem()->stemLen();
            if (beam()) {
                  qreal bw = score()->styleS(Sid::beamWidth).val() * _spatium;
                  y += up() ? -bw : bw;
                  }
            if (up())
                  chordTopY = y;
            else
                  chordBotY = y;
            }

      //
      //    place all articulations with anchor at chord/rest
      //
      qreal distance1 = score()->styleP(Sid::propertyDistanceHead);
      chordTopY -= up() ? 0.5 * _spatium : distance1;
      chordBotY += up() ? distance1 : 0.5 * _spatium;
      for (Articulation* a : _articulations) {
            ArticulationAnchor aa = a->anchor();
            if (aa != ArticulationAnchor::CHORD && aa != ArticulationAnchor::TOP_CHORD && aa != ArticulationAnchor::BOTTOM_CHORD)
                  continue;

            if (a->up()) {
                  if (!a->layoutCloseToNote()) {
                        a->layout();
                        a->setPos(x, chordTopY);
                        a->doAutoplace();
                        }
                  chordTopY = a->y() - a->height() - 0.5 * _spatium;
                  }
            else {
                  if (!a->layoutCloseToNote()) {
                        a->layout();
                        a->setPos(x, chordBotY);
                        a->doAutoplace();
                        }
                  chordBotY = a->y() + a->height() + 0.5 * _spatium;
                  }
            }
      //
      //    now place all articulations with staff top or bottom anchor
      //

      staffTopY = qMin(staffTopY, chordTopY - distance0 - 0.5 * _spatium);
      staffBotY = qMax(staffBotY, chordBotY + distance0 + 0.5 * _spatium);
      for (Articulation* a : _articulations) {
            ArticulationAnchor aa = a->anchor();
            if (aa == ArticulationAnchor::TOP_STAFF || aa == ArticulationAnchor::BOTTOM_STAFF) {
                  a->layout();
                  if (a->up()) {
                        a->setPos(x, staffTopY);
                        staffTopY -= distance0;
                        }
                  else {
                        a->setPos(x, staffBotY);
                        staffBotY += distance0;
                        }
                  a->doAutoplace();
                  }
            }
      for (Articulation* a : _articulations) {
            if (a->addToSkyline()) {
                  // the segment shape has already been calculated
                  // so measure width and spacing is already determined
                  // in line mode, we cannot add to segment shape without throwing this off
                  // but adding to skyline is always good
                  Segment* s = segment();
                  Measure* m = s->measure();
                  QRectF r = a->bbox().translated(a->pos() + pos());
                  // TODO: limit to width of chord
                  // this avoids "staircase" effect due to space not having been allocated already
                  // ANOTHER alternative is to allocate the space in layoutPitched() / layoutTablature()
                  //qreal w = qMin(r.width(), width());
                  //r.translate((r.width() - w) * 0.5, 0.0);
                  //r.setWidth(w);
                  if (!score()->lineMode())
                        s->staffShape(staffIdx()).add(r);
                  r.translate(s->pos() + m->pos());
                  m->system()->staff(vStaffIdx())->skyline().add(r);
                  }
            }
      }

//---------------------------------------------------------
//   layoutArticulations3
//    Called after layouting slurs
//    Fix up articulations that need to go outside the slur
//---------------------------------------------------------

void Chord::layoutArticulations3(Slur* slur)
      {
      SlurSegment* ss;
      if (this == slur->startCR())
            ss = slur->frontSegment();
      else if (this == slur->endCR())
            ss = slur->backSegment();
      else
            return;
      Segment* s = segment();
      Measure* m = measure();
      SysStaff* sstaff = m->system() ? m->system()->staff(vStaffIdx()) : nullptr;
      for (Articulation* a : _articulations) {
            if (a->layoutCloseToNote() || !a->autoplace() || !slur->addToSkyline())
                  continue;
            Shape aShape = a->shape().translated(a->pos() + pos() + s->pos() + m->pos());
            Shape sShape = ss->shape().translated(ss->pos());
            if (aShape.intersects(sShape)) {
                  qreal d = score()->styleS(Sid::articulationMinDistance).val() * spatium();
                  if (slur->up()) {
                        d += qMax(aShape.minVerticalDistance(sShape), 0.0);
                        a->rypos() -= d;
                        aShape.translateY(-d);
                        }
                  else {
                        d += qMax(sShape.minVerticalDistance(aShape), 0.0);
                        a->rypos() += d;
                        aShape.translateY(d);
                        }
                  if (sstaff && a->addToSkyline())
                        sstaff->skyline().add(aShape);
                  }
            }
      }

}