1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2002-2017 Werner Schweer
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2
9 //  as published by the Free Software Foundation and appearing in
10 //  the file LICENCE.GPL
11 //=============================================================================
12 
13 #include "globals.h"
14 #include "pagesettings.h"
15 #include "libmscore/page.h"
16 #include "libmscore/style.h"
17 #include "libmscore/score.h"
18 #include "navigator.h"
19 #include "libmscore/mscore.h"
20 #include "libmscore/excerpt.h"
21 #include "musescore.h"
22 
23 namespace Ms {
24 
25 //---------------------------------------------------------
26 //   PageSettings
27 //---------------------------------------------------------
28 
PageSettings(QWidget * parent)29 PageSettings::PageSettings(QWidget* parent)
30    : AbstractDialog(parent)
31       {
32       clonedScore = 0;
33       setObjectName("PageSettings");
34       setupUi(this);
35       setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
36       setModal(true);
37 
38       NScrollArea* sa = new NScrollArea;
39       preview = new Navigator(sa, this);
40 //      preview->setPreviewOnly(true);
41 
42       static_cast<QVBoxLayout*>(previewGroup->layout())->insertWidget(0, sa);
43 
44       mmUnit = true;      // should be made a global configuration item
45       _changeFlag = false;
46 
47       if (mmUnit)
48             mmButton->setChecked(true);
49       else
50             inchButton->setChecked(true);
51 
52       MuseScore::restoreGeometry(this);
53 
54       for (int i = 0; i < QPageSize::LastPageSize; ++i)
55             pageGroup->addItem(QPageSize::name(QPageSize::PageSizeId(i)), i);
56 
57       connect(mmButton,             SIGNAL(clicked()),            SLOT(mmClicked()));
58       connect(inchButton,           SIGNAL(clicked()),            SLOT(inchClicked()));
59       connect(buttonBox,            SIGNAL(clicked(QAbstractButton*)), SLOT(buttonBoxClicked(QAbstractButton*)));
60       connect(buttonApplyToAllParts,SIGNAL(clicked()),            SLOT(applyToAllParts()));
61       connect(portraitButton,       SIGNAL(clicked()),            SLOT(orientationClicked()));
62       connect(landscapeButton,      SIGNAL(clicked()),            SLOT(orientationClicked()));
63       connect(twosided,             SIGNAL(toggled(bool)),        SLOT(twosidedToggled(bool)));
64       connect(pageHeight,           SIGNAL(valueChanged(double)), SLOT(pageHeightChanged(double)));
65       connect(pageWidth,            SIGNAL(valueChanged(double)), SLOT(pageWidthChanged(double)));
66       connect(oddPageTopMargin,     SIGNAL(valueChanged(double)), SLOT(otmChanged(double)));
67       connect(oddPageBottomMargin,  SIGNAL(valueChanged(double)), SLOT(obmChanged(double)));
68       connect(oddPageLeftMargin,    SIGNAL(valueChanged(double)), SLOT(olmChanged(double)));
69       connect(oddPageRightMargin,   SIGNAL(valueChanged(double)), SLOT(ormChanged(double)));
70       connect(evenPageTopMargin,    SIGNAL(valueChanged(double)), SLOT(etmChanged(double)));
71       connect(evenPageBottomMargin, SIGNAL(valueChanged(double)), SLOT(ebmChanged(double)));
72       connect(evenPageRightMargin,  SIGNAL(valueChanged(double)), SLOT(ermChanged(double)));
73       connect(evenPageLeftMargin,   SIGNAL(valueChanged(double)), SLOT(elmChanged(double)));
74       connect(pageGroup,            SIGNAL(activated(int)),       SLOT(pageFormatSelected(int)));
75       connect(spatiumEntry,         SIGNAL(valueChanged(double)), SLOT(spatiumChanged(double)));
76       connect(pageOffsetEntry,      SIGNAL(valueChanged(int)),    SLOT(pageOffsetChanged(int)));
77       }
78 
79 //---------------------------------------------------------
80 //   PageSettings
81 //---------------------------------------------------------
82 
~PageSettings()83 PageSettings::~PageSettings()
84       {
85       delete clonedScore;
86       }
87 
88 //---------------------------------------------------------
89 //   hideEvent
90 //---------------------------------------------------------
91 
hideEvent(QHideEvent * ev)92 void PageSettings::hideEvent(QHideEvent* ev)
93       {
94       MuseScore::saveGeometry(this);
95       QWidget::hideEvent(ev);
96       }
97 
98 //---------------------------------------------------------
99 //   setScore
100 //---------------------------------------------------------
101 
setScore(Score * s)102 void PageSettings::setScore(Score* s)
103       {
104       cs = s;
105       delete clonedScore;
106       clonedScore = s->clone();
107       clonedScore->setLayoutMode(LayoutMode::PAGE);
108 
109       clonedScore->doLayout();
110       preview->setScore(clonedScore);
111       buttonApplyToAllParts->setEnabled(!cs->isMaster());
112       updateValues();
113       updatePreview();
114       _changeFlag = false;
115       }
116 
117 //---------------------------------------------------------
118 //   blockSignals
119 //---------------------------------------------------------
120 
blockSignals(bool block)121 void PageSettings::blockSignals(bool block)
122       {
123       for (auto w : { oddPageTopMargin, oddPageBottomMargin, oddPageLeftMargin, oddPageRightMargin,
124          evenPageTopMargin, evenPageBottomMargin, evenPageLeftMargin, evenPageRightMargin, spatiumEntry,
125          pageWidth, pageHeight } )
126             {
127             w->blockSignals(block);
128             }
129       pageOffsetEntry->blockSignals(block);
130       pageGroup->blockSignals(block);
131       }
132 
133 //---------------------------------------------------------
134 //   setMarginsMax
135 //---------------------------------------------------------
136 
setMarginsMax(double pw)137 void PageSettings::setMarginsMax(double pw)
138       {
139       oddPageLeftMargin->setMaximum(pw);
140       oddPageRightMargin->setMaximum(pw);
141       evenPageLeftMargin->setMaximum(pw);
142       evenPageRightMargin->setMaximum(pw);
143       }
144 
145 //---------------------------------------------------------
146 //   updateValues
147 //    set gui values from style
148 //---------------------------------------------------------
149 
updateValues()150 void PageSettings::updateValues()
151       {
152       Score* score = preview->score();
153       bool mm = mmButton->isChecked();
154 
155       blockSignals(true);
156 
157       const char* suffix;
158       double singleStepSize;
159       double singleStepScale;
160       if (mm) {
161             suffix = "mm";
162             singleStepSize = 1.0;
163             singleStepScale = 0.2;
164             }
165       else {
166             suffix = "in";
167             singleStepSize = 0.05;
168             singleStepScale = 0.005;
169             }
170       for (auto w : { oddPageTopMargin, oddPageBottomMargin, oddPageLeftMargin, oddPageRightMargin, evenPageTopMargin,
171          evenPageBottomMargin, evenPageLeftMargin, evenPageRightMargin, spatiumEntry, pageWidth, pageHeight } )
172             {
173             w->setSuffix(suffix);
174             w->setSingleStep(singleStepSize);
175             }
176       spatiumEntry->setSingleStep(singleStepScale);
177 
178       double f = mm ? INCH : 1.0;
179 
180       double w = score->styleD(Sid::pageWidth);
181       double h = score->styleD(Sid::pageHeight);
182       setMarginsMax(w * f);
183 
184       double pw = score->styleD(Sid::pagePrintableWidth);
185       pageGroup->setCurrentIndex(int(QPageSize::id(QSizeF(w, h), QPageSize::Inch, QPageSize::FuzzyOrientationMatch)));
186 
187       oddPageTopMargin->setValue(score->styleD(Sid::pageOddTopMargin) * f);
188       oddPageBottomMargin->setValue(score->styleD(Sid::pageOddBottomMargin) * f);
189       oddPageLeftMargin->setValue(score->styleD(Sid::pageOddLeftMargin) * f);
190       oddPageRightMargin->setValue((w - pw - score->styleD(Sid::pageOddLeftMargin)) * f);
191 
192       evenPageTopMargin->setValue(score->styleD(Sid::pageEvenTopMargin) * f);
193       evenPageBottomMargin->setValue(score->styleD(Sid::pageEvenBottomMargin) * f);
194       evenPageLeftMargin->setValue(score->styleD(Sid::pageEvenLeftMargin) * f);
195       evenPageRightMargin->setValue((w - pw - score->styleD(Sid::pageEvenLeftMargin)) * f);
196       pageHeight->setValue(h * f);
197       pageWidth->setValue(w * f);
198 
199       double f1 = mm ? 1.0/DPMM : 1.0/DPI;
200       spatiumEntry->setValue(score->spatium() * f1);
201 
202 
203       bool _twosided = score->styleB(Sid::pageTwosided);
204       evenPageTopMargin->setEnabled(_twosided);
205       evenPageBottomMargin->setEnabled(_twosided);
206       evenPageLeftMargin->setEnabled(_twosided);
207       evenPageRightMargin->setEnabled(_twosided);
208 
209       if (twosided->isChecked()) {
210             evenPageRightMargin->setValue(oddPageLeftMargin->value());
211             evenPageLeftMargin->setValue(oddPageRightMargin->value());
212             }
213       else {
214             evenPageRightMargin->setValue(oddPageRightMargin->value());
215             evenPageLeftMargin->setValue(oddPageLeftMargin->value());
216             }
217 
218       landscapeButton->setChecked(score->styleD(Sid::pageWidth) > score->styleD(Sid::pageHeight));
219       portraitButton->setChecked(score->styleD(Sid::pageWidth) <= score->styleD(Sid::pageHeight));
220 
221       twosided->setChecked(_twosided);
222 
223       pageOffsetEntry->setValue(score->pageNumberOffset() + 1);
224 
225       blockSignals(false);
226       _changeFlag = true;
227       }
228 
229 //---------------------------------------------------------
230 //   inchClicked
231 //---------------------------------------------------------
232 
inchClicked()233 void PageSettings::inchClicked()
234       {
235       mmUnit = false;
236       updateValues();
237       }
238 
239 //---------------------------------------------------------
240 //   mmClicked
241 //---------------------------------------------------------
242 
mmClicked()243 void PageSettings::mmClicked()
244       {
245       mmUnit = true;
246       updateValues();
247       }
248 
249 //---------------------------------------------------------
250 //   orientationClicked
251 //    swap width/height
252 //---------------------------------------------------------
253 
orientationClicked()254 void PageSettings::orientationClicked()
255       {
256       qreal w = preview->score()->styleD(Sid::pageWidth);
257       qreal h = preview->score()->styleD(Sid::pageHeight);
258 
259       preview->score()->style().set(Sid::pageWidth, h);
260       preview->score()->style().set(Sid::pageHeight, w);
261 
262       double f = mmUnit ? 1.0/INCH : 1.0;
263       preview->score()->style().set(Sid::pagePrintableWidth, h - (oddPageLeftMargin->value() + oddPageRightMargin->value()) * f);
264       updateValues();
265       updatePreview();
266       }
267 
on_resetPageStyleButton_clicked()268 void PageSettings::on_resetPageStyleButton_clicked()
269 {
270     preview->score()->style().resetStyles(preview->score(), pageStyles());
271     updatePreview();
272 }
273 
274 //---------------------------------------------------------
275 //   twosidedToggled
276 //---------------------------------------------------------
277 
twosidedToggled(bool flag)278 void PageSettings::twosidedToggled(bool flag)
279       {
280       preview->score()->style().set(Sid::pageTwosided, flag);
281       updateValues();
282       updatePreview();
283       }
284 
285 //---------------------------------------------------------
286 //   buttonBoxClicked
287 //---------------------------------------------------------
288 
buttonBoxClicked(QAbstractButton * button)289 void PageSettings::buttonBoxClicked(QAbstractButton* button)
290       {
291       switch (buttonBox->buttonRole(button)) {
292             case QDialogButtonBox::ApplyRole:
293                   PageSettings::apply();
294                   break;
295             case QDialogButtonBox::AcceptRole:
296                   PageSettings::apply();
297                   // fall through
298             case QDialogButtonBox::RejectRole:
299                   close();
300             default:
301                   break;
302             }
303       }
304 
305 //---------------------------------------------------------
306 //   apply
307 //---------------------------------------------------------
308 
apply()309 void PageSettings::apply()
310       {
311       if (!_changeFlag)
312             return;
313       cs->startCmd();
314       applyToScore(cs);
315       cs->endCmd();
316       mscore->endCmd();
317       _changeFlag = false;
318       }
319 
320 //---------------------------------------------------------
321 //   applyToScore
322 //---------------------------------------------------------
323 
applyToScore(Score * s)324 void PageSettings::applyToScore(Score* s)
325       {
326       double f  = mmUnit ? 1.0/INCH : 1.0;
327       double f1 = mmUnit ? DPMM : DPI;
328 
329       s->undoChangeStyleVal(Sid::pageWidth, pageWidth->value() * f);
330       s->undoChangeStyleVal(Sid::pageHeight, pageHeight->value() * f);
331       s->undoChangeStyleVal(Sid::pagePrintableWidth, (pageWidth->value() - oddPageLeftMargin->value() - oddPageRightMargin->value())  * f);
332       s->undoChangeStyleVal(Sid::pageEvenTopMargin, evenPageTopMargin->value() * f);
333       s->undoChangeStyleVal(Sid::pageEvenBottomMargin, evenPageBottomMargin->value() * f);
334       s->undoChangeStyleVal(Sid::pageEvenLeftMargin, evenPageLeftMargin->value() * f);
335       s->undoChangeStyleVal(Sid::pageOddTopMargin, oddPageTopMargin->value() * f);
336       s->undoChangeStyleVal(Sid::pageOddBottomMargin, oddPageBottomMargin->value() * f);
337       s->undoChangeStyleVal(Sid::pageOddLeftMargin, oddPageLeftMargin->value() * f);
338       s->undoChangeStyleVal(Sid::pageTwosided, twosided->isChecked());
339       s->undoChangeStyleVal(Sid::spatium, spatiumEntry->value() * f1);
340       s->undoChangePageNumberOffset(pageOffsetEntry->value() - 1);
341       }
342 
343 //---------------------------------------------------------
344 //   applyToAllParts
345 //---------------------------------------------------------
346 
applyToAllParts()347 void PageSettings::applyToAllParts()
348       {
349       if (!_changeFlag)
350             return;
351       cs->startCmd();
352       for (Excerpt* e : cs->excerpts())
353             applyToScore(e->partScore());
354       cs->endCmd();
355       _changeFlag = false;
356       }
357 
358 
359 //---------------------------------------------------------
360 //   pageFormatSelected
361 //---------------------------------------------------------
362 
pageFormatSelected(int size)363 void PageSettings::pageFormatSelected(int size)
364       {
365       if (size >= 0) {
366             Score* s  = preview->score();
367             int id    = pageGroup->currentData().toInt();
368             QSizeF sz = QPageSize::size(QPageSize::PageSizeId(id), QPageSize::Inch);
369 
370             s->style().set(Sid::pageWidth, sz.width());
371             s->style().set(Sid::pageHeight, sz.height());
372 
373             double f  = mmUnit ? 1.0/INCH : 1.0;
374             s->style().set(Sid::pagePrintableWidth, sz.width() - (oddPageLeftMargin->value() + oddPageRightMargin->value())  * f);
375             updateValues();
376             updatePreview();
377             }
378       }
379 
380 //---------------------------------------------------------
381 //   otmChanged
382 //---------------------------------------------------------
383 
otmChanged(double val)384 void PageSettings::otmChanged(double val)
385       {
386       if (mmUnit)
387             val /= INCH;
388       preview->score()->style().set(Sid::pageOddTopMargin, val);
389       updatePreview();
390       }
391 
392 //---------------------------------------------------------
393 //   olmChanged
394 //---------------------------------------------------------
395 
olmChanged(double val)396 void PageSettings::olmChanged(double val)
397       {
398       if (mmUnit)
399             val /= INCH;
400 
401       if (twosided->isChecked()) {
402             evenPageRightMargin->blockSignals(true);
403             evenPageRightMargin->setValue(val * (mmUnit ? INCH : 1.0));
404             evenPageRightMargin->blockSignals(false);
405             }
406       else {
407             evenPageLeftMargin->blockSignals(true);
408             evenPageLeftMargin->setValue(val * (mmUnit ? INCH : 1.0));
409             evenPageLeftMargin->blockSignals(false);
410             }
411       Score* s = preview->score();
412       s->style().set(Sid::pageOddLeftMargin, val);
413       s->style().set(Sid::pagePrintableWidth, s->styleD(Sid::pageWidth) - s->styleD(Sid::pageEvenLeftMargin) - val);
414 
415       updatePreview();
416       }
417 
418 //---------------------------------------------------------
419 //   ormChanged
420 //---------------------------------------------------------
421 
ormChanged(double val)422 void PageSettings::ormChanged(double val)
423       {
424       if (mmUnit)
425             val /= INCH;
426 
427       Score* s = preview->score();
428       if (twosided->isChecked()) {
429             evenPageLeftMargin->blockSignals(true);
430             evenPageLeftMargin->setValue(val * (mmUnit ? INCH : 1.0));
431             s->style().set(Sid::pageEvenLeftMargin, val);
432             evenPageLeftMargin->blockSignals(false);
433             }
434       else {
435             evenPageRightMargin->blockSignals(true);
436             evenPageRightMargin->setValue(val * (mmUnit ? INCH : 1.0));
437             evenPageRightMargin->blockSignals(false);
438             }
439       s->style().set(Sid::pagePrintableWidth, s->styleD(Sid::pageWidth) - s->styleD(Sid::pageOddLeftMargin) - val);
440       updatePreview();
441       }
442 
443 //---------------------------------------------------------
444 //   obmChanged
445 //---------------------------------------------------------
446 
obmChanged(double val)447 void PageSettings::obmChanged(double val)
448       {
449       if (mmUnit)
450             val /= INCH;
451       preview->score()->style().set(Sid::pageOddBottomMargin, val);
452       updatePreview();
453       }
454 
455 //---------------------------------------------------------
456 //   etmChanged
457 //---------------------------------------------------------
458 
etmChanged(double val)459 void PageSettings::etmChanged(double val)
460       {
461       if (mmUnit)
462             val /= INCH;
463       preview->score()->style().set(Sid::pageEvenTopMargin, val);
464       updatePreview();
465       }
466 
467 //---------------------------------------------------------
468 //   elmChanged
469 //---------------------------------------------------------
470 
elmChanged(double val)471 void PageSettings::elmChanged(double val)
472       {
473       double f  = mmUnit ? 1.0/INCH : 1.0;
474       val *= f;
475 
476       if (twosided->isChecked()) {
477             oddPageRightMargin->blockSignals(true);
478             oddPageRightMargin->setValue(val * (mmUnit ? INCH : 1.0));
479             oddPageRightMargin->blockSignals(false);
480             }
481       Score* s = preview->score();
482       s->style().set(Sid::pageEvenLeftMargin, val);
483       s->style().set(Sid::pagePrintableWidth, s->styleD(Sid::pageWidth) - evenPageRightMargin->value() * f - val);
484       updatePreview();
485       }
486 
487 //---------------------------------------------------------
488 //   ermChanged
489 //---------------------------------------------------------
490 
ermChanged(double val)491 void PageSettings::ermChanged(double val)
492       {
493       if (mmUnit)
494             val /= INCH;
495 
496       if (twosided->isChecked()) {
497             oddPageLeftMargin->blockSignals(true);
498             oddPageLeftMargin->setValue(val * (mmUnit ? INCH : 1.0));
499             oddPageLeftMargin->blockSignals(false);
500             }
501       Score* s = preview->score();
502       s->style().set(Sid::pagePrintableWidth, s->styleD(Sid::pageWidth) - s->styleD(Sid::pageEvenLeftMargin) - val);
503       s->style().set(Sid::pageOddLeftMargin, val);
504       updatePreview();
505       }
506 
507 //---------------------------------------------------------
508 //   ebmChanged
509 //---------------------------------------------------------
510 
ebmChanged(double val)511 void PageSettings::ebmChanged(double val)
512       {
513       if (mmUnit)
514             val /= INCH;
515       preview->score()->style().set(Sid::pageEvenBottomMargin, val);
516       updatePreview();
517       }
518 
519 //---------------------------------------------------------
520 //   spatiumChanged
521 //---------------------------------------------------------
522 
spatiumChanged(double val)523 void PageSettings::spatiumChanged(double val)
524       {
525       val *= mmUnit ? DPMM : DPI;
526       double oldVal = preview->score()->spatium();
527       preview->score()->setSpatium(val);
528       preview->score()->spatiumChanged(oldVal, val);
529       updatePreview();
530       }
531 
532 //---------------------------------------------------------
533 //   pageOffsetChanged
534 //---------------------------------------------------------
535 
pageOffsetChanged(int val)536 void PageSettings::pageOffsetChanged(int val)
537       {
538       preview->score()->setPageNumberOffset(val-1);
539       updatePreview();
540       }
541 
542 //---------------------------------------------------------
543 //   pageHeightChanged
544 //---------------------------------------------------------
545 
pageHeightChanged(double val)546 void PageSettings::pageHeightChanged(double val)
547       {
548       double val2 = pageWidth->value();
549       if (mmUnit) {
550             val /= INCH;
551             val2 /= INCH;
552             }
553       pageGroup->setCurrentIndex(0);      // custom
554       preview->score()->style().set(Sid::pageHeight, val);
555       preview->score()->style().set(Sid::pageWidth, val2);
556 
557       updatePreview();
558       }
559 
560 //---------------------------------------------------------
561 //   pageWidthChanged
562 //---------------------------------------------------------
563 
pageWidthChanged(double val)564 void PageSettings::pageWidthChanged(double val)
565       {
566       setMarginsMax(val);
567 
568       double f    = mmUnit ? 1.0/INCH : 1.0;
569       double val2 = pageHeight->value() * f;
570       val        *= f;
571       pageGroup->setCurrentIndex(0);      // custom
572       preview->score()->style().set(Sid::pageWidth, val);
573       preview->score()->style().set(Sid::pageHeight, val2);
574       preview->score()->style().set(Sid::pagePrintableWidth, val - (oddPageLeftMargin->value() + evenPageLeftMargin->value()) * f);
575       updatePreview();
576       }
577 
578 //---------------------------------------------------------
579 //   updatePreview
580 //---------------------------------------------------------
581 
updatePreview()582 void PageSettings::updatePreview()
583       {
584       updateValues();
585       preview->score()->doLayout();
586       preview->layoutChanged();
587       }
588 }
589 
590