forked from musescore/MuseScore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.cpp
2820 lines (2533 loc) · 111 KB
/
file.cpp
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
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2014 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
//=============================================================================
/**
File handling: loading and saving.
*/
#include "config.h"
#include "globals.h"
#include "musescore.h"
#include "scoreview.h"
#include "exportmidi.h"
#include "libmscore/xml.h"
#include "libmscore/element.h"
#include "libmscore/note.h"
#include "libmscore/rest.h"
#include "libmscore/sig.h"
#include "libmscore/clef.h"
#include "libmscore/key.h"
#include "instrdialog.h"
#include "libmscore/score.h"
#include "libmscore/page.h"
#include "libmscore/dynamic.h"
#include "file.h"
#include "libmscore/style.h"
#include "libmscore/tempo.h"
#include "libmscore/select.h"
#include "preferences.h"
#include "playpanel.h"
#include "libmscore/staff.h"
#include "libmscore/part.h"
#include "libmscore/utils.h"
#include "libmscore/barline.h"
#include "palette.h"
#include "symboldialog.h"
#include "libmscore/slur.h"
#include "libmscore/hairpin.h"
#include "libmscore/ottava.h"
#include "libmscore/textline.h"
#include "libmscore/pedal.h"
#include "libmscore/trill.h"
#include "libmscore/volta.h"
#include "newwizard.h"
#include "libmscore/timesig.h"
#include "libmscore/box.h"
#include "libmscore/excerpt.h"
#include "libmscore/system.h"
#include "libmscore/tuplet.h"
#include "libmscore/keysig.h"
#include "magbox.h"
#include "libmscore/measure.h"
#include "libmscore/undo.h"
#include "libmscore/repeatlist.h"
#include "scoretab.h"
#include "libmscore/beam.h"
#include "libmscore/stafftype.h"
#include "seq.h"
#include "libmscore/revisions.h"
#include "libmscore/lyrics.h"
#include "libmscore/segment.h"
#include "libmscore/tempotext.h"
#include "libmscore/sym.h"
#include "libmscore/image.h"
#include "libmscore/stafflines.h"
#include "synthesizer/msynthesizer.h"
#include "svggenerator.h"
#include "scorePreview.h"
#ifdef OMR
#include "omr/omr.h"
#include "omr/omrpage.h"
#include "omr/importpdf.h"
#endif
#include "diff/diff_match_patch.h"
#include "libmscore/chordlist.h"
#include "libmscore/mscore.h"
#include "thirdparty/qzip/qzipreader_p.h"
namespace Ms {
extern void importSoundfont(QString name);
extern bool savePositions(Score*, const QString& name, bool segments);
extern MasterSynthesizer* synti;
//---------------------------------------------------------
// paintElement(s)
//---------------------------------------------------------
static void paintElement(QPainter& p, const Element* e)
{
QPointF pos(e->pagePos());
p.translate(pos);
e->draw(&p);
p.translate(-pos);
}
static void paintElements(QPainter& p, const QList<Element*>& el)
{
for (Element* e : el) {
if (!e->visible())
continue;
paintElement(p, e);
}
}
//---------------------------------------------------------
// createDefaultFileName
//---------------------------------------------------------
static QString createDefaultFileName(QString fn)
{
//
// special characters in filenames are a constant source
// of trouble, this replaces some of them common in german:
//
fn = fn.simplified();
fn = fn.replace(QChar(' '), "_");
fn = fn.replace(QChar('\n'), "_");
fn = fn.replace(QChar(0xe4), "ae"); // ä
fn = fn.replace(QChar(0xf6), "oe"); // ö
fn = fn.replace(QChar(0xfc), "ue"); // ü
fn = fn.replace(QChar(0xdf), "ss"); // ß
fn = fn.replace(QChar(0xc4), "Ae"); // Ä
fn = fn.replace(QChar(0xd6), "Oe"); // Ö
fn = fn.replace(QChar(0xdc), "Ue"); // Ü
fn = fn.replace(QChar(0x266d),"b"); // musical flat sign, happen in instrument names, so can happen in part (file) names
fn = fn.replace(QChar(0x266f),"#"); // musical sharp sign, can happen in titles, so can happen in score (file) names
fn = fn.replace( QRegExp( "[" + QRegExp::escape( "\\/:*?\"<>|" ) + "]" ), "_" ); //FAT/NTFS special chars
return fn;
}
//---------------------------------------------------------
// readScoreError
// if "ask" is true, ask to ignore; returns true if
// ignore is pressed by user
// returns true if -f is used in converter mode
//---------------------------------------------------------
static bool readScoreError(const QString& name, Score::FileError error, bool ask)
{
QString msg = QObject::tr("Cannot read file %1:\n").arg(name);
QString detailedMsg;
bool canIgnore = false;
switch(error) {
case Score::FileError::FILE_NO_ERROR:
return false;
case Score::FileError::FILE_BAD_FORMAT:
msg += QObject::tr("bad format");
detailedMsg = MScore::lastError;
break;
case Score::FileError::FILE_UNKNOWN_TYPE:
msg += QObject::tr("unknown type");
break;
case Score::FileError::FILE_NO_ROOTFILE:
break;
case Score::FileError::FILE_TOO_OLD:
msg += QObject::tr("It was last saved with a version older than 2.0.0.\n"
"You can convert this score by opening and then\n"
"saving with MuseScore version 2.x.\n"
"Visit the %1MuseScore download page%2 to obtain such a 2.x version.")
.arg("<a href=\"https://musescore.org/download#older-versions\">")
.arg("</a>");
canIgnore = true;
break;
case Score::FileError::FILE_TOO_NEW:
msg += QObject::tr("This score was saved using a newer version of MuseScore.\n"
"Visit the %1MuseScore website%2 to obtain the latest version.")
.arg("<a href=\"https://musescore.org\">")
.arg("</a>");
canIgnore = true;
break;
case Score::FileError::FILE_NOT_FOUND:
msg = QObject::tr("File \"%1\" not found.").arg(name);
break;
case Score::FileError::FILE_CORRUPTED:
msg = QObject::tr("File \"%1\" corrupted.").arg(name);
detailedMsg = MScore::lastError;
canIgnore = true;
break;
case Score::FileError::FILE_ERROR:
case Score::FileError::FILE_OPEN_ERROR:
default:
msg += MScore::lastError;
break;
}
if (converterMode && canIgnore && ignoreWarnings) {
fprintf(stderr, "%s\n\nWarning ignored, forcing score to load\n", qPrintable(msg));
return true;
}
if (converterMode || pluginMode) {
fprintf(stderr, "%s\n", qPrintable(msg));
return false;
}
QMessageBox msgBox;
msgBox.setWindowTitle(QObject::tr("Load Error"));
msgBox.setText(msg.replace("\n", "<br/>"));
msgBox.setDetailedText(detailedMsg);
msgBox.setTextFormat(Qt::RichText);
if (canIgnore && ask) {
msgBox.setIcon(QMessageBox::Warning);
msgBox.setStandardButtons(
QMessageBox::Cancel | QMessageBox::Ignore
);
return msgBox.exec() == QMessageBox::Ignore;
}
else {
msgBox.setIcon(QMessageBox::Critical);
msgBox.setStandardButtons(
QMessageBox::Ok
);
msgBox.exec();
}
return false;
}
//---------------------------------------------------------
// checkDirty
// if dirty, save score
// return true on cancel
//---------------------------------------------------------
bool MuseScore::checkDirty(MasterScore* s)
{
if (s->dirty() || s->created()) {
QMessageBox::StandardButton n = QMessageBox::warning(this, tr("MuseScore"),
tr("Save changes to the score \"%1\"\n"
"before closing?").arg(s->fileInfo()->completeBaseName()),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
QMessageBox::Save);
if (n == QMessageBox::Save) {
if (s->masterScore()->isSavable()) {
if (!saveFile(s))
return true;
}
else {
if (!saveAs(s, false))
return true;
}
}
else if (n == QMessageBox::Cancel)
return true;
}
return false;
}
//---------------------------------------------------------
// loadFile
//---------------------------------------------------------
/**
Create a modal file open dialog.
If a file is selected, load it.
Handles the GUI's file-open action.
*/
void MuseScore::loadFiles()
{
QStringList files = getOpenScoreNames(
#ifdef OMR
tr("All Supported Files") + " (*.mscz *.mscx *.mxl *.musicxml *.xml *.mid *.midi *.kar *.md *.mgu *.sgu *.cap *.capx *.pdf *.ove *.scw *.bww *.gtp *.gp3 *.gp4 *.gp5 *.gpx);;" +
#else
tr("All Supported Files") + " (*.mscz *.mscx *.mxl *.musicxml *.xml *.mid *.midi *.kar *.md *.mgu *.sgu *.cap *.capx *.ove *.scw *.bww *.gtp *.gp3 *.gp4 *.gp5 *.gpx);;" +
#endif
tr("MuseScore Files") + " (*.mscz *.mscx);;" +
tr("MusicXML Files") + " (*.mxl *.musicxml *.xml);;" +
tr("MIDI Files") + " (*.mid *.midi *.kar);;" +
tr("MuseData Files") + " (*.md);;" +
tr("Capella Files") + " (*.cap *.capx);;" +
tr("BB Files <experimental>") + " (*.mgu *.sgu);;" +
#ifdef OMR
tr("PDF Files <experimental OMR>") + " (*.pdf);;" +
#endif
tr("Overture / Score Writer Files <experimental>") + " (*.ove *.scw);;" +
tr("Bagpipe Music Writer Files <experimental>") + " (*.bww);;" +
tr("Guitar Pro") + " (*.gtp *.gp3 *.gp4 *.gp5 *.gpx)",
tr("Load Score")
);
for (const QString& s : files)
openScore(s);
}
//---------------------------------------------------------
// openScore
//---------------------------------------------------------
Score* MuseScore::openScore(const QString& fn)
{
//
// make sure we load a file only once
//
QFileInfo fi(fn);
QString path = fi.canonicalFilePath();
for (Score* s : scoreList) {
if (s->masterScore()->fileInfo()->canonicalFilePath() == path)
return 0;
}
MasterScore* score = readScore(fn);
if (score) {
setCurrentScoreView(appendScore(score));
writeSessionFile(false);
}
return score;
}
//---------------------------------------------------------
// readScore
//---------------------------------------------------------
MasterScore* MuseScore::readScore(const QString& name)
{
if (name.isEmpty())
return 0;
MasterScore* score = new MasterScore(MScore::defaultStyle());
setMidiReopenInProgress(name);
Score::FileError rv = Ms::readScore(score, name, false);
if (rv == Score::FileError::FILE_TOO_OLD || rv == Score::FileError::FILE_TOO_NEW || rv == Score::FileError::FILE_CORRUPTED) {
if (readScoreError(name, rv, true)) {
if (rv != Score::FileError::FILE_CORRUPTED) {
// dont read file again if corrupted
// the check routine may try to fix it
delete score;
score = new MasterScore();
score->setMovements(new Movements());
score->setStyle(MScore::defaultStyle());
rv = Ms::readScore(score, name, true);
}
else
rv = Score::FileError::FILE_NO_ERROR;
}
else {
delete score;
return 0;
}
}
if (rv != Score::FileError::FILE_NO_ERROR) {
// in case of user abort while reading, the error has already been reported
// else report it now
if (rv != Score::FileError::FILE_USER_ABORT && rv != Score::FileError::FILE_IGNORE_ERROR)
readScoreError(name, rv, false);
delete score;
score = 0;
return 0;
}
allowShowMidiPanel(name);
if (score)
addRecentScore(score);
return score;
}
//---------------------------------------------------------
// saveFile
/// Save the current score.
/// Handles the GUI's file-save action.
//
// return true on success
//---------------------------------------------------------
bool MuseScore::saveFile()
{
return saveFile(cs->masterScore());
}
//---------------------------------------------------------
// saveFile
/// Save the score.
//
// return true on success
//---------------------------------------------------------
bool MuseScore::saveFile(MasterScore* score)
{
if (score == 0)
return false;
if (score->created()) {
QString fn = score->masterScore()->fileInfo()->fileName();
Text* t = score->getText(SubStyle::TITLE);
if (t)
fn = t->plainText();
QString name = createDefaultFileName(fn);
QString f1 = tr("MuseScore File") + " (*.mscz)";
QString f2 = tr("Uncompressed MuseScore File") + " (*.mscx)";
QSettings settings;
if (mscore->lastSaveDirectory.isEmpty())
mscore->lastSaveDirectory = settings.value("lastSaveDirectory", preferences.getString(PREF_APP_PATHS_MYSCORES)).toString();
QString saveDirectory = mscore->lastSaveDirectory;
if (saveDirectory.isEmpty())
saveDirectory = preferences.getString(PREF_APP_PATHS_MYSCORES);
QString fname = QString("%1/%2").arg(saveDirectory).arg(name);
QString filter = f1 + ";;" + f2;
if (QFileInfo(fname).suffix().isEmpty())
fname += ".mscz";
fn = mscore->getSaveScoreName(tr("Save Score"), fname, filter);
if (fn.isEmpty())
return false;
score->masterScore()->fileInfo()->setFile(fn);
mscore->lastSaveDirectory = score->masterScore()->fileInfo()->absolutePath();
if (!score->masterScore()->saveFile()) {
QMessageBox::critical(mscore, tr("Save File"), MScore::lastError);
return false;
}
addRecentScore(score);
writeSessionFile(false);
}
else if (!score->masterScore()->saveFile()) {
QMessageBox::critical(mscore, tr("Save File"), MScore::lastError);
return false;
}
score->setCreated(false);
setWindowTitle(QString(MUSESCORE_NAME_VERSION) + ": " + score->fileInfo()->completeBaseName());
int idx = scoreList.indexOf(score->masterScore());
tab1->setTabText(idx, score->fileInfo()->completeBaseName());
if (tab2)
tab2->setTabText(idx, score->fileInfo()->completeBaseName());
QString tmp = score->tmpName();
if (!tmp.isEmpty()) {
QFile f(tmp);
if (!f.remove())
qDebug("cannot remove temporary file <%s>", qPrintable(f.fileName()));
score->setTmpName("");
}
writeSessionFile(false);
return true;
}
//---------------------------------------------------------
// createDefaultName
//---------------------------------------------------------
QString MuseScore::createDefaultName() const
{
QString name(tr("Untitled"));
int n;
for (n = 1; ; ++n) {
bool nameExists = false;
QString tmpName;
if (n == 1)
tmpName = name;
else
tmpName = QString("%1-%2").arg(name).arg(n);
for (MasterScore* s : scoreList) {
if (s->fileInfo()->completeBaseName() == tmpName) {
nameExists = true;
break;
}
}
if (!nameExists) {
name = tmpName;
break;
}
}
return name;
}
//---------------------------------------------------------
// getNewFile
// create new score
//---------------------------------------------------------
MasterScore* MuseScore::getNewFile()
{
if (!newWizard)
newWizard = new NewWizard(this);
else
newWizard->restart();
if (newWizard->exec() != QDialog::Accepted)
return 0;
int measures = newWizard->measures();
Fraction timesig = newWizard->timesig();
TimeSigType timesigType = newWizard->timesigType();
KeySigEvent ks = newWizard->keysig();
VBox* nvb = nullptr;
int pickupTimesigZ;
int pickupTimesigN;
bool pickupMeasure = newWizard->pickupMeasure(&pickupTimesigZ, &pickupTimesigN);
if (pickupMeasure)
measures += 1;
MasterScore* score = new MasterScore(MScore::defaultStyle());
QString tp = newWizard->templatePath();
QList<Excerpt*> excerpts;
if (!newWizard->emptyScore()) {
MasterScore* tscore = new MasterScore(MScore::defaultStyle());
Score::FileError rv = Ms::readScore(tscore, tp, false);
if (rv != Score::FileError::FILE_NO_ERROR) {
readScoreError(newWizard->templatePath(), rv, false);
delete tscore;
delete score;
return 0;
}
// create instruments from template
for (Part* tpart : tscore->parts()) {
Part* part = new Part(score);
part->setInstrument(tpart->instrument());
part->setPartName(tpart->partName());
for (Staff* tstaff : *tpart->staves()) {
Staff* staff = new Staff(score);
staff->setPart(part);
staff->init(tstaff);
if (tstaff->linkedStaves() && !part->staves()->isEmpty()) {
Staff* linkedStaff = part->staves()->back();
staff->linkTo(linkedStaff);
}
part->insertStaff(staff, -1);
score->staves().append(staff);
}
score->appendPart(part);
}
for (Excerpt* ex : tscore->excerpts()) {
Excerpt* x = new Excerpt(score);
x->setTitle(ex->title());
for (Part* p : ex->parts()) {
int pidx = tscore->parts().indexOf(p);
if (pidx == -1)
qDebug("newFile: part not found");
else
x->parts().append(score->parts()[pidx]);
}
excerpts.append(x);
}
MeasureBase* mb = tscore->first();
if (mb && mb->isVBox()) {
VBox* tvb = toVBox(mb);
nvb = new VBox(score);
nvb->setBoxHeight(tvb->boxHeight());
nvb->setBoxWidth(tvb->boxWidth());
nvb->setTopGap(tvb->topGap());
nvb->setBottomGap(tvb->bottomGap());
nvb->setTopMargin(tvb->topMargin());
nvb->setBottomMargin(tvb->bottomMargin());
nvb->setLeftMargin(tvb->leftMargin());
nvb->setRightMargin(tvb->rightMargin());
}
delete tscore;
}
else {
score = new MasterScore(MScore::defaultStyle());
newWizard->createInstruments(score);
}
score->setCreated(true);
score->fileInfo()->setFile(createDefaultName());
if (!score->style().chordList()->loaded()) {
if (score->styleB(StyleIdx::chordsXmlFile))
score->style().chordList()->read("chords.xml");
score->style().chordList()->read(score->styleSt(StyleIdx::chordDescriptionFile));
}
if (!newWizard->title().isEmpty())
score->fileInfo()->setFile(newWizard->title());
score->sigmap()->add(0, timesig);
int firstMeasureTicks = pickupMeasure ? Fraction(pickupTimesigZ, pickupTimesigN).ticks() : timesig.ticks();
for (int i = 0; i < measures; ++i) {
int tick = firstMeasureTicks + timesig.ticks() * (i - 1);
if (i == 0)
tick = 0;
QList<Rest*> puRests;
for (Score* _score : score->scoreList()) {
Rest* rest = 0;
Measure* measure = new Measure(_score);
measure->setTimesig(timesig);
measure->setLen(timesig);
measure->setTick(tick);
if (pickupMeasure && tick == 0) {
measure->setIrregular(true); // dont count pickup measure
measure->setLen(Fraction(pickupTimesigZ, pickupTimesigN));
}
_score->measures()->add(measure);
for (Staff* staff : _score->staves()) {
int staffIdx = staff->idx();
if (tick == 0) {
TimeSig* ts = new TimeSig(_score);
ts->setTrack(staffIdx * VOICES);
ts->setSig(timesig, timesigType);
Measure* m = _score->firstMeasure();
Segment* s = m->getSegment(SegmentType::TimeSig, 0);
s->add(ts);
Part* part = staff->part();
if (!part->instrument()->useDrumset()) {
//
// transpose key
//
KeySigEvent nKey = ks;
if (!nKey.custom() && !nKey.isAtonal() && part->instrument()->transpose().chromatic && !score->styleB(StyleIdx::concertPitch)) {
int diff = -part->instrument()->transpose().chromatic;
nKey.setKey(transposeKey(nKey.key(), diff));
}
// do not create empty keysig unless custom or atonal
if (nKey.custom() || nKey.isAtonal() || nKey.key() != Key::C) {
staff->setKey(0, nKey);
KeySig* keysig = new KeySig(score);
keysig->setTrack(staffIdx * VOICES);
keysig->setKeySigEvent(nKey);
Segment* s = measure->getSegment(SegmentType::KeySig, 0);
s->add(keysig);
}
}
}
// determined if this staff is linked to previous so we can reuse rests
bool linkedToPrevious = staffIdx && staff->isLinked(_score->staff(staffIdx - 1));
if (measure->timesig() != measure->len()) {
if (!linkedToPrevious)
puRests.clear();
std::vector<TDuration> dList = toDurationList(measure->len(), false);
if (!dList.empty()) {
int ltick = tick;
int k = 0;
foreach (TDuration d, dList) {
if (k < puRests.count())
rest = static_cast<Rest*>(puRests[k]->linkedClone());
else {
rest = new Rest(score, d);
puRests.append(rest);
}
rest->setScore(_score);
rest->setDuration(d.fraction());
rest->setTrack(staffIdx * VOICES);
Segment* seg = measure->getSegment(SegmentType::ChordRest, ltick);
seg->add(rest);
ltick += rest->actualTicks();
k++;
}
}
}
else {
if (linkedToPrevious && rest)
rest = static_cast<Rest*>(rest->linkedClone());
else
rest = new Rest(score, TDuration(TDuration::DurationType::V_MEASURE));
rest->setScore(_score);
rest->setDuration(measure->len());
rest->setTrack(staffIdx * VOICES);
Segment* seg = measure->getSegment(SegmentType::ChordRest, tick);
seg->add(rest);
}
}
}
}
//TODO score->lastMeasure()->setEndBarLineType(BarLineType::END, false);
//
// select first rest
//
Measure* m = score->firstMeasure();
for (Segment* s = m->first(); s; s = s->next()) {
if (s->segmentType() == SegmentType::ChordRest) {
if (s->element(0)) {
score->select(s->element(0), SelectType::SINGLE, 0);
break;
}
}
}
QString title = newWizard->title();
QString subtitle = newWizard->subtitle();
QString composer = newWizard->composer();
QString poet = newWizard->poet();
QString copyright = newWizard->copyright();
if (!title.isEmpty() || !subtitle.isEmpty() || !composer.isEmpty() || !poet.isEmpty()) {
MeasureBase* measure = score->measures()->first();
if (measure->type() != ElementType::VBOX) {
MeasureBase* nm = nvb ? nvb : new VBox(score);
nm->setTick(0);
nm->setNext(measure);
score->measures()->add(nm);
measure = nm;
}
else if (nvb) {
delete nvb;
}
if (!title.isEmpty()) {
Text* s = new Text(SubStyle::TITLE, score);
s->setPlainText(title);
measure->add(s);
score->setMetaTag("workTitle", title);
}
if (!subtitle.isEmpty()) {
Text* s = new Text(SubStyle::SUBTITLE, score);
s->setPlainText(subtitle);
measure->add(s);
}
if (!composer.isEmpty()) {
Text* s = new Text(SubStyle::COMPOSER, score);
s->setPlainText(composer);
measure->add(s);
score->setMetaTag("composer", composer);
}
if (!poet.isEmpty()) {
Text* s = new Text(SubStyle::POET, score);
s->setPlainText(poet);
measure->add(s);
// the poet() functions returns data called lyricist in the dialog
score->setMetaTag("lyricist", poet);
}
}
else if (nvb) {
delete nvb;
}
if (newWizard->createTempo()) {
double tempo = newWizard->tempo();
TempoText* tt = new TempoText(score);
tt->setXmlText(QString("<sym>metNoteQuarterUp</sym> = %1").arg(tempo));
tempo /= 60; // bpm -> bps
tt->setTempo(tempo);
tt->setFollowText(true);
tt->setTrack(0);
Segment* seg = score->firstMeasure()->first(SegmentType::ChordRest);
seg->add(tt);
score->setTempo(0, tempo);
}
if (!copyright.isEmpty())
score->setMetaTag("copyright", copyright);
score->rebuildMidiMapping();
score->doLayout();
for (Excerpt* x : excerpts) {
Score* xs = new Score(static_cast<MasterScore*>(score));
xs->style().set(StyleIdx::createMultiMeasureRests, true);
x->setPartScore(xs);
xs->setExcerpt(x);
score->excerpts().append(x);
Excerpt::createExcerpt(x);
}
score->setExcerptsChanged(true);
return score;
}
//---------------------------------------------------------
// newFile
// create new score
//---------------------------------------------------------
void MuseScore::newFile()
{
MasterScore* score = getNewFile();
if (score)
setCurrentScoreView(appendScore(score));
}
//---------------------------------------------------------
// addScorePreview
// add a score preview to the file dialog
//---------------------------------------------------------
static void addScorePreview(QFileDialog* dialog)
{
QSplitter* splitter = dialog->findChild<QSplitter*>("splitter");
if (splitter) {
ScorePreview* preview = new ScorePreview;
splitter->addWidget(preview);
dialog->connect(dialog, SIGNAL(currentChanged(const QString&)), preview, SLOT(setScore(const QString&)));
}
}
//---------------------------------------------------------
// sidebarUrls
// return a list of standard file dialog sidebar urls
//---------------------------------------------------------
static QList<QUrl> sidebarUrls()
{
QList<QUrl> urls;
urls.append(QUrl::fromLocalFile(QDir::homePath()));
QFileInfo myScores(preferences.getString(PREF_APP_PATHS_MYSCORES));
urls.append(QUrl::fromLocalFile(myScores.absoluteFilePath()));
urls.append(QUrl::fromLocalFile(QDir::currentPath()));
return urls;
}
//---------------------------------------------------------
// getOpenScoreNames
//---------------------------------------------------------
QStringList MuseScore::getOpenScoreNames(const QString& filter, const QString& title)
{
QSettings settings;
QString dir = settings.value("lastOpenPath", preferences.getString(PREF_APP_PATHS_MYSCORES)).toString();
if (preferences.getBool(PREF_UI_APP_USENATIVEDIALOGS)) {
QStringList fileList = QFileDialog::getOpenFileNames(this,
title, dir, filter);
if (fileList.count() > 0) {
QFileInfo fi(fileList[0]);
settings.setValue("lastOpenPath", fi.absolutePath());
}
return fileList;
}
QFileInfo myScores(preferences.getString(PREF_APP_PATHS_MYSCORES));
if (myScores.isRelative())
myScores.setFile(QDir::home(), preferences.getString(PREF_APP_PATHS_MYSCORES));
if (loadScoreDialog == 0) {
loadScoreDialog = new QFileDialog(this);
loadScoreDialog->setFileMode(QFileDialog::ExistingFiles);
loadScoreDialog->setOption(QFileDialog::DontUseNativeDialog, true);
loadScoreDialog->setWindowTitle(title);
addScorePreview(loadScoreDialog);
loadScoreDialog->setNameFilter(filter);
restoreDialogState("loadScoreDialog", loadScoreDialog);
loadScoreDialog->setAcceptMode(QFileDialog::AcceptOpen);
loadScoreDialog->setDirectory(dir);
}
else {
// dialog already exists, but set title and filter
loadScoreDialog->setWindowTitle(title);
loadScoreDialog->setNameFilter(filter);
}
// setup side bar urls
QList<QUrl> urls = sidebarUrls();
urls.append(QUrl::fromLocalFile(mscoreGlobalShare+"/demos"));
loadScoreDialog->setSidebarUrls(urls);
QStringList result;
if (loadScoreDialog->exec())
result = loadScoreDialog->selectedFiles();
settings.setValue("lastOpenPath", loadScoreDialog->directory().absolutePath());
return result;
}
//---------------------------------------------------------
// getSaveScoreName
//---------------------------------------------------------
QString MuseScore::getSaveScoreName(const QString& title, QString& name, const QString& filter, bool selectFolder)
{
QFileInfo myName(name);
if (myName.isRelative())
myName.setFile(QDir::home(), name);
name = myName.absoluteFilePath();
if (preferences.getBool(PREF_UI_APP_USENATIVEDIALOGS)) {
QString s;
QFileDialog::Options options = selectFolder ? QFileDialog::ShowDirsOnly : QFileDialog::Options(0);
return QFileDialog::getSaveFileName(this, title, name, filter, &s, options);
}
QFileInfo myScores(preferences.getString(PREF_APP_PATHS_MYSCORES));
if (myScores.isRelative())
myScores.setFile(QDir::home(), preferences.getString(PREF_APP_PATHS_MYSCORES));
if (saveScoreDialog == 0) {
saveScoreDialog = new QFileDialog(this);
saveScoreDialog->setFileMode(QFileDialog::AnyFile);
saveScoreDialog->setOption(QFileDialog::DontConfirmOverwrite, false);
saveScoreDialog->setOption(QFileDialog::DontUseNativeDialog, true);
saveScoreDialog->setAcceptMode(QFileDialog::AcceptSave);
addScorePreview(saveScoreDialog);
restoreDialogState("saveScoreDialog", saveScoreDialog);
}
// setup side bar urls
saveScoreDialog->setSidebarUrls(sidebarUrls());
if (selectFolder)
saveScoreDialog->setFileMode(QFileDialog::Directory);
saveScoreDialog->setWindowTitle(title);
saveScoreDialog->setNameFilter(filter);
saveScoreDialog->selectFile(name);
if (!selectFolder) {
connect(saveScoreDialog, SIGNAL(filterSelected(const QString&)),
SLOT(saveScoreDialogFilterSelected(const QString&)));
}
QString s;
if (saveScoreDialog->exec())
s = saveScoreDialog->selectedFiles().front();
return s;
}
//---------------------------------------------------------
// saveScoreDialogFilterSelected
// update selected file name extensions, when filter
// has changed
//---------------------------------------------------------
void MuseScore::saveScoreDialogFilterSelected(const QString& s)
{
QRegExp rx(QString(".+\\(\\*\\.(.+)\\)"));
if (rx.exactMatch(s)) {
QFileInfo fi(saveScoreDialog->selectedFiles().front());
saveScoreDialog->selectFile(fi.completeBaseName() + "." + rx.cap(1));
}
}
//---------------------------------------------------------
// getStyleFilename
//---------------------------------------------------------
QString MuseScore::getStyleFilename(bool open, const QString& title)
{
QFileInfo myStyles(preferences.getString(PREF_APP_PATHS_MYSTYLES));
if (myStyles.isRelative())
myStyles.setFile(QDir::home(), preferences.getString(PREF_APP_PATHS_MYSTYLES));
QString defaultPath = myStyles.absoluteFilePath();
if (preferences.getBool(PREF_UI_APP_USENATIVEDIALOGS)) {
QString fn;
if (open) {
fn = QFileDialog::getOpenFileName(
this, tr("Load Style"),
defaultPath,
tr("MuseScore Styles") + " (*.mss)"
);
}
else {
fn = QFileDialog::getSaveFileName(
this, tr("Save Style"),
defaultPath,
tr("MuseScore Style File") + " (*.mss)"
);
}
return fn;
}
QFileDialog* dialog;
QList<QUrl> urls;
QString home = QDir::homePath();
urls.append(QUrl::fromLocalFile(home));
urls.append(QUrl::fromLocalFile(defaultPath));
urls.append(QUrl::fromLocalFile(QDir::currentPath()));
if (open) {
if (loadStyleDialog == 0) {
loadStyleDialog = new QFileDialog(this);
loadStyleDialog->setFileMode(QFileDialog::ExistingFile);
loadStyleDialog->setOption(QFileDialog::DontUseNativeDialog, true);
loadStyleDialog->setWindowTitle(title.isEmpty() ? tr("Load Style") : title);
loadStyleDialog->setNameFilter(tr("MuseScore Style File") + " (*.mss)");
loadStyleDialog->setDirectory(defaultPath);
restoreDialogState("loadStyleDialog", loadStyleDialog);
loadStyleDialog->setAcceptMode(QFileDialog::AcceptOpen);
}
urls.append(QUrl::fromLocalFile(mscoreGlobalShare+"/styles"));
dialog = loadStyleDialog;
}
else {
if (saveStyleDialog == 0) {
saveStyleDialog = new QFileDialog(this);
saveStyleDialog->setAcceptMode(QFileDialog::AcceptSave);
saveStyleDialog->setFileMode(QFileDialog::AnyFile);
saveStyleDialog->setOption(QFileDialog::DontConfirmOverwrite, false);
saveStyleDialog->setOption(QFileDialog::DontUseNativeDialog, true);
saveStyleDialog->setWindowTitle(title.isEmpty() ? tr("Save Style") : title);
saveStyleDialog->setNameFilter(tr("MuseScore Style File") + " (*.mss)");
saveStyleDialog->setDirectory(defaultPath);
restoreDialogState("saveStyleDialog", saveStyleDialog);
saveStyleDialog->setAcceptMode(QFileDialog::AcceptSave);
}
dialog = saveStyleDialog;
}
// setup side bar urls
dialog->setSidebarUrls(urls);
if (dialog->exec()) {
QStringList result = dialog->selectedFiles();
return result.front();
}
return QString();
}
//---------------------------------------------------------
// getChordStyleFilename
//---------------------------------------------------------
QString MuseScore::getChordStyleFilename(bool open)
{
QString filter = tr("Chord Symbols Style File") + " (*.xml)";