Repository navigation
Expand file tree
/
Copy pathmidipp_scores.cpp
More file actions
2469 lines (2043 loc) · 56 KB
/
Copy pathmidipp_scores.cpp
File metadata and controls
2469 lines (2043 loc) · 56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2009-2019 Hans Petter Selasky. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "midipp_mainwindow.h"
#include "midipp_scores.h"
#include "midipp_looptab.h"
#include "midipp_bpm.h"
#include "midipp_mode.h"
#include "midipp_decode.h"
#include "midipp_import.h"
#include "midipp_replace.h"
#include "midipp_spinbox.h"
#include "midipp_groupbox.h"
#include "midipp_button.h"
#include "midipp_show.h"
#include "midipp_tabbar.h"
#include "midipp_gridlayout.h"
#include "midipp_sheet.h"
static int
MppCountNewline(const QString &str)
{
int x;
int y = 0;
QChar ch = '\0';
if (str.size() == 0)
return (0);
for (x = 0; x != str.size(); x++) {
ch = str[x];
if (ch == '\n')
y++;
}
if (ch != '\n')
y++;
return (y);
}
MppScoreView :: MppScoreView(MppScoreMain *parent)
{
pScores = parent;
delta_v = 0;
setFocusPolicy(Qt::ClickFocus);
}
void
MppScoreView :: mousePressEvent(QMouseEvent *e)
{
pScores->viewMousePressEvent(e);
}
void
MppScoreView :: wheelEvent(QWheelEvent *event)
{
QScrollBar *ps = pScores->viewScroll;
if (event->orientation() == Qt::Vertical) {
delta_v -= event->delta();
int delta = delta_v / MPP_WHEEL_STEP;
delta_v %= MPP_WHEEL_STEP;
if (delta != 0) {
delta += ps->value();
if (delta < 0)
delta = 0;
else if (delta > ps->maximum())
delta = ps->maximum();
ps->setValue(delta);
}
}
event->accept();
}
void
MppScoreView :: keyPressEvent(QKeyEvent *event)
{
QScrollBar *ps = pScores->viewScroll;
int value;
value = ps->value();
switch (event->key()) {
case Qt::Key_PageDown:
case Qt::Key_Down:
value ++;
break;
case Qt::Key_Up:
case Qt::Key_PageUp:
value --;
break;
case Qt::Key_End:
value = ps->maximum();
break;
case Qt::Key_Home:
value = 0;
break;
default:
goto done;
}
if (value >= ps->minimum() && value <= ps->maximum())
ps->setValue(value);
done:
event->accept();
}
void
MppScoreView :: paintEvent(QPaintEvent *event)
{
pScores->viewPaintEvent(event);
}
MppScoreTextEdit :: MppScoreTextEdit(MppScoreMain *parent)
{
sm = parent;
}
MppScoreTextEdit :: ~MppScoreTextEdit(void)
{
}
void
MppScoreTextEdit :: mouseDoubleClickEvent(QMouseEvent *e)
{
/* edit the line */
sm->handleEditLine();
}
MppScoreMain :: MppScoreMain(MppMainWindow *parent, int _unit)
{
QString defaultText;
char buf[32];
if (_unit == 0) {
defaultText = tr(
"K5.1 /* Micro tune the chords */\n\n"
"S\"L0 - verse: \"\n"
"\n"
"S\".(C)Welcome .(C)to .(D)MIDI .(E)Player .(C)Pro! \"\n"
"\n"
"L0:\n"
"U1 C3 C4 C5 E5 G5 /* C */\n"
"U1 C3 C4 C5 E5 G5 /* C */\n"
"U1 D3 D4 D5 G5B A5 /* D */\n"
"U1 E3 E4 E5 A5B H5 /* E */\n"
"U1 C3 C4 C5 E5 G5 /* C */\n"
"J0\n");
}
snprintf(buf, sizeof(buf), "%c-scores", 'A' + _unit);
/* set memory default */
memset(auto_zero_start, 0, auto_zero_end - auto_zero_start);
/* set valid non-zero value */
visual_y_max = 1;
/* all devices are input */
baseKey = MPP_DEFAULT_BASE_KEY;
delayNoise = 25;
chordContrast = 128;
chordNormalize = 1;
inputChannel = -1;
synthChannel = ((_unit == 1) ? 9 : 0);
synthChannelBase = -1;
synthChannelTreb = -1;
auxChannel = -1;
auxChannelBase = -1;
auxChannelTreb = -1;
synthDevice = -1;
synthDeviceBase = -1;
synthDeviceTreb = -1;
unit = _unit;
/* Set parent */
mainWindow = parent;
/* Buttons */
butScoreFileNew = new QPushButton(tr("New"));
butScoreFileOpen = new QPushButton(tr("Open"));
butScoreFileSave = new QPushButton(tr("Save"));
butScoreFileSaveAs = new QPushButton(tr("Save As"));
butScoreFilePrint = new QPushButton(tr("Print"));
butScoreFileAlign = new QPushButton(tr("Align\nScores"));
spnScoreFileBassOffset = new QSpinBox();
spnScoreFileBassOffset->setRange(0,15);
spnScoreFileBassOffset->setValue(0);
butScoreFileBassOffset = new QPushButton(tr("Bass\nOffset"));
spnScoreFileAlign = new MppSpinBox(0,0);
spnScoreFileAlign->setValue((MPP_F0 + 12 * 5) * MPP_BAND_STEP_12);
butScoreFileScale = new QPushButton(tr("Scale"));
spnScoreFileScale = new QSpinBox();
spnScoreFileScale->setRange(0, 60000);
spnScoreFileScale->setSuffix(" ms");
spnScoreFileScale->setValue(1000);
butScoreFileStepUpHalf = new QPushButton(tr("Step Up\n12 scale"));
butScoreFileStepDownHalf = new QPushButton(tr("Step Down\n12 scale"));
butScoreFileStepUpSingle = new QPushButton(tr("Step Up\n192 scale"));
butScoreFileStepDownSingle = new QPushButton(tr("Step Down\n192 scale"));
butScoreFileSetSharp = new QPushButton(tr("Set #"));
butScoreFileSetFlat = new QPushButton(tr("Set b"));
butScoreFileReplaceAll = new QPushButton(tr("Replace all"));
butScoreFileExport = new QPushButton(tr("To Lyrics with chords"));
butScoreFileExportNoChords = new QPushButton(tr("To Lyrics no chords"));
#ifndef HAVE_PRINTER
butScoreFilePrint->hide();
#endif
gbScoreFile = new MppGroupBox(tr(buf));
gbScoreFile->addWidget(butScoreFileNew, 0, 0, 1, 2);
gbScoreFile->addWidget(butScoreFileOpen, 1, 0, 1, 2);
gbScoreFile->addWidget(butScoreFileSave, 2, 0, 1, 2);
gbScoreFile->addWidget(butScoreFileSaveAs, 3, 0, 1, 2);
gbScoreFile->addWidget(butScoreFilePrint, 4, 0, 1, 2);
gbScoreFile->addWidget(butScoreFileBassOffset, 5, 0, 1, 1);
gbScoreFile->addWidget(spnScoreFileBassOffset, 5, 1, 1, 1);
gbScoreFile->addWidget(butScoreFileAlign, 6, 0, 1, 1);
gbScoreFile->addWidget(spnScoreFileAlign, 6, 1, 1, 1);
gbScoreFile->addWidget(butScoreFileScale, 7, 0, 1, 1);
gbScoreFile->addWidget(spnScoreFileScale, 7, 1, 1, 1);
gbScoreFile->addWidget(butScoreFileStepUpHalf, 8, 0, 1, 1);
gbScoreFile->addWidget(butScoreFileStepDownHalf, 8, 1, 1, 1);
gbScoreFile->addWidget(butScoreFileStepUpSingle, 9, 0, 1, 1);
gbScoreFile->addWidget(butScoreFileStepDownSingle, 9, 1, 1, 1);
gbScoreFile->addWidget(butScoreFileSetSharp, 10, 0, 1, 1);
gbScoreFile->addWidget(butScoreFileSetFlat, 10, 1, 1, 1);
gbScoreFile->addWidget(butScoreFileReplaceAll, 11, 0, 1, 2);
gbScoreFile->addWidget(butScoreFileExport, 12, 0, 1, 2);
gbScoreFile->addWidget(butScoreFileExportNoChords, 13, 0, 1, 2);
connect(butScoreFileNew, SIGNAL(released()), this, SLOT(handleScoreFileNew()));
connect(butScoreFileOpen, SIGNAL(released()), this, SLOT(handleScoreFileOpen()));
connect(butScoreFileSave, SIGNAL(released()), this, SLOT(handleScoreFileSave()));
connect(butScoreFileSaveAs, SIGNAL(released()), this, SLOT(handleScoreFileSaveAs()));
connect(butScoreFilePrint, SIGNAL(released()), this, SLOT(handleScorePrint()));
connect(butScoreFileBassOffset, SIGNAL(released()), this, SLOT(handleScoreFileBassOffset()));
connect(butScoreFileAlign, SIGNAL(released()), this, SLOT(handleScoreFileAlign()));
connect(butScoreFileStepUpHalf, SIGNAL(released()), this, SLOT(handleScoreFileStepUpHalf()));
connect(butScoreFileStepDownHalf, SIGNAL(released()), this, SLOT(handleScoreFileStepDownHalf()));
connect(butScoreFileStepUpSingle, SIGNAL(released()), this, SLOT(handleScoreFileStepUpSingle()));
connect(butScoreFileStepDownSingle, SIGNAL(released()), this, SLOT(handleScoreFileStepDownSingle()));
connect(butScoreFileSetSharp, SIGNAL(released()), this, SLOT(handleScoreFileSetSharp()));
connect(butScoreFileSetFlat, SIGNAL(released()), this, SLOT(handleScoreFileSetFlat()));
connect(butScoreFileScale, SIGNAL(released()), this, SLOT(handleScoreFileScale()));
connect(butScoreFileReplaceAll, SIGNAL(released()), this, SLOT(handleScoreFileReplaceAll()));
connect(butScoreFileExport, SIGNAL(released()), this, SLOT(handleScoreFileExport()));
connect(butScoreFileExportNoChords, SIGNAL(released()), this, SLOT(handleScoreFileExportNoChords()));
/* Editor */
editWidget = new MppScoreTextEdit(this);
editWidget->setFont(parent->editFont);
editWidget->setPlainText(defaultText);
editWidget->setCursorWidth(4);
editWidget->setLineWrapMode(QPlainTextEdit::NoWrap);
/* GridLayout */
gl_view = new MppGridLayout();
gl_view->setSpacing(0);
viewScroll = new QScrollBar(Qt::Vertical);
viewScroll->setValue(0);
viewScroll->setRange(0,0);
viewScroll->setPageStep(1);
connect(viewScroll, SIGNAL(valueChanged(int)), this, SLOT(handleScrollChanged(int)));
/* Visual */
viewWidgetSub = new MppScoreView(this);
gl_view->addWidget(viewWidgetSub, 0, 0, 1, 1);
gl_view->addWidget(viewScroll, 0, 1, 1, 1);
sheet = new MppSheet(parent, _unit);
/* Initial compile */
handleCompile(1);
QPainter paint;
picChord[0] = new QPicture();
paint.begin(picChord[0]);
paint.setRenderHints(QPainter::Antialiasing, 1);
paint.setPen(QPen(Mpp.ColorBlack, 1));
paint.setBrush(QColor(Mpp.ColorBlack));
paint.drawEllipse(QRect(0,0,MPP_VISUAL_C_MAX,MPP_VISUAL_C_MAX));
paint.end();
picChord[1] = new QPicture();
paint.begin(picChord[1]);
paint.setRenderHints(QPainter::Antialiasing, 1);
paint.setPen(QPen(Mpp.ColorBlack, 1));
paint.setBrush(QColor(Mpp.ColorBlack));
paint.drawEllipse(QRect(MPP_VISUAL_C_MAX,0,MPP_VISUAL_C_MAX,MPP_VISUAL_C_MAX));
paint.end();
}
MppScoreMain :: ~MppScoreMain()
{
handleScoreFileNew();
}
void
MppScoreMain :: handlePrintSub(QPrinter *pd, QPoint orig)
{
#ifdef HAVE_PRINTER
enum { PAGES_MAX = 128 };
int pageStart[PAGES_MAX];
int pageNum;
int pageLimit;
#endif
MppVisualDot *pdot;
MppElement *ptr;
MppElement *next;
QPainter paint;
QString linebuf;
QString chord;
QRectF box;
QFont fnt_a;
QFont fnt_b;
qreal chord_x_max;
qreal offset;
qreal scale_x;
qreal scale_y;
int cmax_y;
int rmax_x;
int rmax_y;
int margin_x;
int margin_y;
int vmax_y;
int last_dot;
int dur;
int x;
int y;
int z;
#ifdef HAVE_PRINTER
if (pd != NULL) {
fnt_a = mainWindow->printFont;
fnt_a.setPointSize(mainWindow->printFont.pixelSize());
fnt_b = mainWindow->printFont;
fnt_b.setPointSize(mainWindow->printFont.pixelSize() + 2);
scale_x = (qreal)pd->logicalDpiX() / (qreal)mainWindow->logicalDpiX();
scale_y = (qreal)pd->logicalDpiY() / (qreal)mainWindow->logicalDpiY();
/* translate printing area */
paint.begin(pd);
paint.translate(orig);
}
#endif
if (pd == NULL) {
fnt_a = mainWindow->defaultFont;
fnt_a.setPixelSize(mainWindow->defaultFont.pixelSize());
fnt_b = mainWindow->defaultFont;
fnt_b.setPixelSize(mainWindow->defaultFont.pixelSize() + 4);
scale_x = 1.0;
scale_y = 1.0;
}
/* extract all text */
for (x = 0; x != visual_max; x++) {
QString *pstr = pVisual[x].str;
/* delete old and allocate a new string */
delete pstr;
pstr = new QString();
/* parse through the text */
for (ptr = pVisual[x].start; ptr != pVisual[x].stop;
ptr = TAILQ_NEXT(ptr, entry)) {
switch (ptr->type) {
case MPP_T_STRING_DESC:
*pstr += ptr->txt;
break;
default:
break;
}
}
/* Trim string */
*pstr = pstr->trimmed();
/* store new string */
pVisual[x].str = pstr;
}
rmax_x = MPP_VISUAL_R_MAX * scale_x;
rmax_y = MPP_VISUAL_R_MAX * scale_y;
margin_x = MPP_VISUAL_MARGIN * scale_x;
margin_y = MPP_VISUAL_MARGIN * scale_y;
#ifdef HAVE_PRINTER
if (pd != NULL) {
paint.translate(QPoint(-margin_x, -margin_y));
/* count all pages */
pageNum = 0;
pageStart[pageNum++] = 0;
for (x = 0; x != visual_max; x++) {
QString &str = *pVisual[x].str;
if (pageNum < PAGES_MAX &&
str.length() > 1 && str[0] == 'L' && str[1].isDigit())
pageStart[pageNum++] = x;
}
if (pageNum < PAGES_MAX)
pageStart[pageNum++] = visual_max;
pageNum = 0;
}
#endif
QFontMetricsF fm_b(fnt_b, paint.device());
vmax_y = MPP_VISUAL_C_MAX * scale_y + 3 * fm_b.height();
cmax_y = MPP_VISUAL_C_MAX * scale_y;
/* sanity check */
if (vmax_y < 1)
vmax_y = 1;
/* store copy of maximum Y value */
if (pd == NULL)
visual_y_max = vmax_y;
#ifdef HAVE_PRINTER
if (pd != NULL) {
pageLimit = (pd->height() - 2 * margin_y) / vmax_y;
if (pageLimit < 1)
pageLimit = 1;
}
#endif
for (x = y = 0; x != visual_max; x++) {
#ifdef HAVE_PRINTER
if (pd != NULL) {
while (pageNum < PAGES_MAX && pageStart[pageNum] == x) {
pageNum++;
if (pageNum < PAGES_MAX &&
(pageStart[pageNum] - x) >= (pageLimit - y) && y != 0) {
pd->newPage();
paint.translate(QPoint(0, -vmax_y * y));
y = 0;
}
}
if (y != 0 && (y >= pageLimit || pVisual[x].newpage != 0)) {
pd->newPage();
paint.translate(QPoint(0, -vmax_y * y));
y = 0;
}
}
#endif
if (pd == NULL) {
delete (pVisual[x].pic);
pVisual[x].pic = new QPicture();
paint.begin(pVisual[x].pic);
paint.setRenderHints(QPainter::Antialiasing, 1);
}
paint.setPen(QPen(Mpp.ColorBlack, 1));
paint.setBrush(QColor(Mpp.ColorBlack));
linebuf = QString();
z = 0;
last_dot = 0;
chord_x_max = 0;
for (ptr = pVisual[x].start; ptr != pVisual[x].stop;
ptr = TAILQ_NEXT(ptr, entry)) {
paint.setFont(fnt_a);
if (ptr->type == MPP_T_STRING_DOT) {
if (last_dot != 0)
linebuf += ' ';
last_dot = 1;
} else {
last_dot = 0;
}
retry:
box = paint.boundingRect(QRectF(0,0,0,0),
Qt::TextSingleLine | Qt::AlignLeft, linebuf);
if (ptr->type != MPP_T_STRING_DESC &&
box.width() < chord_x_max && linebuf.size() < 256) {
int t;
for (t = linebuf.size() - 1; t > -1; t--) {
if (!linebuf[t].isSpace())
break;
if (linebuf[t] == '-') {
/* insert space before last dash */
linebuf = linebuf.mid(0,t) + ' ' +
linebuf.mid(t, linebuf.size() - t);
break;
}
}
linebuf += ' ';
goto retry;
}
switch (ptr->type) {
case MPP_T_STRING_DESC:
linebuf += ptr->txt;
break;
case MPP_T_STRING_DOT:
pdot = &pVisual[x].pdot[z++];
if (z > pVisual[x].ndot)
break;
pdot->x_off = margin_x + box.width();
pdot->y_off = margin_y + (vmax_y / 3);
for (next = ptr; next != pVisual[x].stop;
next = TAILQ_NEXT(next, entry)) {
if (next->type == MPP_T_STRING_CHORD &&
next->txt.size() > 1 && next->txt[0] == '(')
break;
}
if (next != pVisual[x].stop)
pdot->x_off += (fm_b.width(next->txt[1]) - rmax_x - 2.0 * scale_x) / 2.0;
dur = ptr->value[0];
paint.drawEllipse(QRectF(pdot->x_off, pdot->y_off, rmax_x, rmax_y));
if (dur <= 0)
break;
if (dur > 5)
dur = 5;
offset = 0;
paint.drawLine(
pdot->x_off + rmax_x, pdot->y_off + (rmax_y / 2),
pdot->x_off + rmax_x, pdot->y_off + (rmax_y / 2) - (3 * rmax_y));
while (dur--) {
paint.drawLine(
pdot->x_off + rmax_x, pdot->y_off + (rmax_y / 2) - (3 * rmax_y) + offset,
pdot->x_off, pdot->y_off + rmax_y - (3 * rmax_y) + offset);
offset += (rmax_y / 2);
}
break;
case MPP_T_STRING_CHORD:
chord = MppDeQuoteChord(ptr->txt);
paint.setFont(fnt_b);
paint.drawText(QPointF(margin_x + box.width(),
margin_y + (vmax_y / 3) - (cmax_y / 4)), chord);
chord_x_max = box.width() +
paint.boundingRect(QRectF(0,0,0,0), Qt::TextSingleLine | Qt::AlignLeft,
chord + QChar(' ')).width();
break;
default:
break;
}
}
paint.setFont(fnt_a);
paint.drawText(QPointF(margin_x, margin_y + vmax_y - (vmax_y / 3) - (cmax_y / 4)), linebuf);
#ifdef HAVE_PRINTER
if (pd != NULL) {
paint.translate(QPoint(0, vmax_y));
y++;
}
#endif
if (pd == NULL)
paint.end();
}
if (pd != NULL)
paint.end();
}
void
MppScoreMain :: viewMousePressEvent(QMouseEvent *e)
{
int yi;
yi = ((e->y() - MPP_VISUAL_MARGIN) / visual_y_max) + picScroll;
if (yi < 0 || yi >= visual_max)
return;
mainWindow->handle_tab_changed(1);
mainWindow->atomic_lock();
head.jumpPointer(pVisual[yi].start);
head.syncLast();
mainWindow->handle_stop();
mainWindow->atomic_unlock();
}
void
MppScoreMain :: locateVisual(MppElement *ptr, int *pindex,
int *pnext, MppVisualDot **ppdot)
{
MppElement *start;
MppElement *stop;
MppElement *elem;
int x;
int y;
for (x = y = 0; x != visual_max; x++) {
if (ptr->compare(pVisual[x].start) < 0 ||
ptr->compare(pVisual[x].stop) >= 0)
continue;
for (start = stop = pVisual[x].start;
head.foreachLine(&start, &stop); ) {
if (start->compare(pVisual[x].stop) >= 0)
break;
if (ptr->compare(start) >= 0 &&
ptr->compare(stop) < 0)
break;
for (elem = start; elem != stop;
elem = TAILQ_NEXT(elem, entry)) {
/* check if scores in line */
if (elem->type == MPP_T_SCORE_SUBDIV ||
elem->type == MPP_T_MACRO) {
y++;
break;
}
}
}
break;
}
if (pindex != 0) {
*pindex = x;
}
if (pnext != 0) {
if (x != visual_max && (x + 1) != visual_max &&
pVisual[x].stop == pVisual[x + 1].start)
*pnext = x + 1;
else
*pnext = x;
}
if (ppdot != 0) {
if (x != visual_max && y < pVisual[x].ndot)
*ppdot = &pVisual[x].pdot[y];
else
*ppdot = 0;
}
}
void
MppScoreMain :: viewPaintEvent(QPaintEvent *event)
{
QPainter paint(viewWidgetSub);
MppVisualDot *pcdot;
MppVisualDot *podot;
MppElement *curr;
MppElement *last;
int scroll;
int y_blocks;
int y_div;
int y_rem;
int yc_div;
int yc_rem;
int yo_div;
int yo_rem;
int x;
paint.fillRect(event->rect(), Mpp.ColorWhite);
mainWindow->atomic_lock();
curr = head.state.curr_start;
last = head.state.last_start;
scroll = picScroll;
mainWindow->atomic_unlock();
y_blocks = (viewWidgetSub->height() / visual_y_max);
if (y_blocks == 0)
y_blocks = 1;
/* locate current play position */
locateVisual(curr, &yc_rem, 0, &pcdot);
/* locate last play position */
locateVisual(last, &yo_rem, 0, &podot);
y_div = 0;
yc_div = 0;
yo_div = 0;
/* compute scrollbar */
y_div = scroll / y_blocks;
y_rem = scroll % y_blocks;
/* align current position with scroll bar */
if (yc_rem < y_rem) {
yc_rem = 0;
yc_div = visual_max;
} else {
yc_rem -= y_rem;
yc_div = yc_rem / y_blocks;
yc_rem = yc_rem % y_blocks;
}
/* align last position with scroll bar */
if (yo_rem < y_rem) {
yo_rem = 0;
yo_div = visual_max;
} else {
yo_rem -= y_rem;
yo_div = yo_rem / y_blocks;
yo_rem = yo_rem % y_blocks;
}
/* paint pictures */
for (x = 0; x != y_blocks; x++) {
int y = ((y_div * y_blocks) + y_rem + x);
if (y >= visual_max)
break;
paint.drawPicture(
QPoint(0, x * visual_y_max),
*(pVisual[y].pic));
}
/* overlay (last) */
if ((curr != last) && (yo_div == y_div) && (podot != 0)) {
paint.setPen(QPen(Mpp.ColorGreen, 4));
paint.setBrush(QColor(Mpp.ColorGreen));
paint.drawEllipse(QRect(podot->x_off,
podot->y_off + (yo_rem * visual_y_max),
MPP_VISUAL_R_MAX, MPP_VISUAL_R_MAX));
}
/* overlay (current) */
if ((yc_div == y_div) && (pcdot != 0)) {
paint.setPen(QPen(Mpp.ColorLogo, 4));
paint.setBrush(QColor(Mpp.ColorLogo));
paint.drawEllipse(QRect(pcdot->x_off,
pcdot->y_off + (yc_rem * visual_y_max),
MPP_VISUAL_R_MAX, MPP_VISUAL_R_MAX));
}
/* overlay (active chord) */
if (keyMode == MM_PASS_NONE_CHORD_PIANO ||
keyMode == MM_PASS_NONE_CHORD_AUX) {
int width = viewWidgetSub->width();
int mask;
mainWindow->atomic_lock();
mask = ((mainWindow->get_time_offset() % 1000) >= 500);
mainWindow->atomic_unlock();
if (width >= (2 * MPP_VISUAL_C_MAX)) {
if (pressed_future != 1 || mask) {
paint.drawPicture(QPoint(width -
(2 * MPP_VISUAL_C_MAX), 0),
*picChord[0]);
}
if (pressed_future != 0 || mask) {
paint.drawPicture(QPoint(width -
(2 * MPP_VISUAL_C_MAX), 0),
*picChord[1]);
}
}
}
}
/* The following function must be called locked */
void
MppScoreMain :: handleParse(const QString &pstr)
{
MppElement *start;
MppElement *stop;
MppElement *ptr;
int key_mode;
int auto_melody;
int auto_utune;
int has_string;
int index;
int x;
int num_dot;
/* reset head structure */
head.clear();
/* add string to input */
head += pstr;
/* flush last element, if any */
head.flush();
/* set initial mask for active channels */
active_channels = 1;
/* no automatic melody */
auto_melody = 0;
/* no automatic micro tuning */
auto_utune = -1;
/* no key mode selection */
key_mode = -1;
/* cleanup visual entries */
for (x = 0; x != visual_max; x++) {
delete (pVisual[x].str);
delete (pVisual[x].pic);
free (pVisual[x].pdot);
}
visual_max = 0;
visual_p_max = 0;
index = 0;
free(pVisual);
pVisual = 0;
for (start = stop = 0; head.foreachLine(&start, &stop); ) {
has_string = 0;
for (ptr = start; ptr != stop;
ptr = TAILQ_NEXT(ptr, entry)) {
if (ptr->type == MPP_T_COMMAND) {
switch (ptr->value[0]) {
case MPP_CMD_BPM_REF:
/* update BPM timer */
mainWindow->atomic_lock();
mainWindow->dlg_bpm->period_ref = ptr->value[1];
mainWindow->dlg_bpm->period_cur = ptr->value[2];
mainWindow->dlg_bpm->handle_update();
mainWindow->atomic_unlock();
break;
case MPP_CMD_AUTO_MELODY:
auto_melody = ptr->value[1];
break;
case MPP_CMD_KEY_MODE:
key_mode = ptr->value[1];
break;
case MPP_CMD_MICRO_TUNE:
auto_utune = ptr->value[1];
break;
default:
break;
}
} else if (ptr->type == MPP_T_CHANNEL) {
if (ptr->value[0] > -1 && ptr->value[0] < 16)
active_channels |= (1 << ptr->value[0]);
} else if (ptr->type == MPP_T_STRING_DESC ||
ptr->type == MPP_T_STRING_DOT ||
ptr->type == MPP_T_STRING_CHORD) {
has_string = 1;
} else if (ptr->type == MPP_T_JUMP) {
if (ptr->value[1] & MPP_FLAG_JUMP_PAGE)
index = 0;
}
}
/* compute maximum number of score lines */
if (has_string) {
index++;
visual_max++;
if (visual_p_max < index)
visual_p_max = index;
}
}
if (visual_max != 0) {
size_t size = sizeof(MppVisualScore) * visual_max;
pVisual = (MppVisualScore *)malloc(size);
memset(pVisual, 0, size);
}
head.dotReorder();
index = 0;
for (start = stop = 0; head.foreachLine(&start, &stop); ) {
has_string = 0;
num_dot = 0;
for (ptr = start; ptr != stop;
ptr = TAILQ_NEXT(ptr, entry)) {
if (ptr->type == MPP_T_STRING_DOT) {
num_dot++;
has_string = 1;
} else if (ptr->type == MPP_T_STRING_DESC ||
ptr->type == MPP_T_STRING_CHORD) {
has_string = 1;
} else if (ptr->type == MPP_T_JUMP) {
if ((ptr->value[1] & MPP_FLAG_JUMP_PAGE) &&
(index > 0 && index <= visual_max)) {
pVisual[index - 1].newpage = 1;
}
}
}
if (has_string && (index < visual_max)) {
/* extend region of previous visual */
if (index > 0)
pVisual[index - 1].stop = start;
pVisual[index].start = start;
pVisual[index].stop = stop;
pVisual[index].ndot = num_dot;
if (num_dot != 0) {
size_t size = sizeof(MppVisualDot) * num_dot;
pVisual[index].pdot = (MppVisualDot *)
malloc(size);
memset(pVisual[index].pdot, 0, size);
}
index++;
}
}
/* extend region of first and last visual */
if (visual_max != 0) {
pVisual[0].start = TAILQ_FIRST(&head.head);
pVisual[visual_max - 1].stop = 0;
}
/* compile before auto-melody */
sheet->compile(head);
if (auto_utune > 0)
head.tuneScore();
/* check if key-mode should be applied */
switch (key_mode) {
case 0:
keyMode = MM_PASS_ALL;
mainWindow->keyModeUpdated = 1;
break;
case 2:
keyMode = MM_PASS_NONE_FIXED;
mainWindow->keyModeUpdated = 1;
break;
case 3:
keyMode = MM_PASS_NONE_TRANS;
mainWindow->keyModeUpdated = 1;
break;
case 4:
keyMode = MM_PASS_NONE_CHORD_PIANO;
mainWindow->keyModeUpdated = 1;
break;
case 5:
keyMode = MM_PASS_NONE_CHORD_AUX;
mainWindow->keyModeUpdated = 1;
break;
case 6:
keyMode = MM_PASS_NONE_CHORD_TRANS;
mainWindow->keyModeUpdated = 1;