File: magbox.cpp

package info (click to toggle)
musescore 2.3.2%2Bdfsg2-7~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 187,932 kB
  • sloc: cpp: 264,610; xml: 176,707; sh: 3,311; ansic: 1,384; python: 356; makefile: 188; perl: 82; pascal: 78
file content (262 lines) | stat: -rw-r--r-- 8,615 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
//=============================================================================
//  MuseScore
//  Linux Music Score Editor
//  $Id: magbox.cpp 5568 2012-04-22 10:08:43Z wschweer $
//
//  Copyright (C) 2002-2008 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "preferences.h"
#include "magbox.h"
#include "scoreview.h"
#include "libmscore/page.h"
#include "musescore.h"
#include "libmscore/score.h"
#include "libmscore/mscore.h"

namespace Ms {

//---------------------------------------------------------
//   magTable
//    list of strings shown in QComboBox "MagBox"
//---------------------------------------------------------

struct MagEntry {
      const char* txt;
      MagIdx idx;
      };

static MagIdx startMag = MagIdx::MAG_100;

static const MagEntry magTable[] = {
     {  "25%",   MagIdx::MAG_25 },
     {  "50%",   MagIdx::MAG_50 },
     {  "75%",   MagIdx::MAG_75 },
     {  "100%",  MagIdx::MAG_100 },
     {  "150%",  MagIdx::MAG_150 },
     {  "200%",  MagIdx::MAG_200 },
     {  "400%",  MagIdx::MAG_400 },
     {  "800%",  MagIdx::MAG_800 },
     {  "1600%", MagIdx::MAG_1600 },
     {  QT_TRANSLATE_NOOP("magTable","Page Width"), MagIdx::MAG_PAGE_WIDTH },
     {  QT_TRANSLATE_NOOP("magTable","Whole Page"), MagIdx::MAG_PAGE },
     {  QT_TRANSLATE_NOOP("magTable","Two Pages"),  MagIdx::MAG_DBL_PAGE },
     };

//---------------------------------------------------------
//   MagBox
//---------------------------------------------------------

MagBox::MagBox(QWidget* parent)
   : QComboBox(parent)
      {
      freeMag = 1.0;
      setEditable(true);
      setInsertPolicy(QComboBox::InsertAtBottom);
      setToolTip(tr("Zoom"));
      setWhatsThis(tr("Zoom"));
      setValidator(new MagValidator(this));
      setAutoCompletion(false);

      int i = 0;
      for (const MagEntry& e : magTable) {
            QString ts(QCoreApplication::translate("magTable", e.txt));
            addItem(ts, QVariant::fromValue(e.idx));
            if (e.idx == startMag)
                  setCurrentIndex(i);
            ++i;
            }
      setMaxCount(i+1);
      addItem(QString("%1%").arg(freeMag * 100), int(MagIdx::MAG_FREE));
      setFocusPolicy(Qt::StrongFocus);
      setAccessibleName(tr("Zoom"));
      setFixedHeight(preferences.iconHeight + 10);  // hack
      connect(this, SIGNAL(currentIndexChanged(int)), SLOT(indexChanged(int)));
      connect(lineEdit(), SIGNAL(returnPressed()), SLOT(textChanged()));
      }

//---------------------------------------------------------
//   textChanged
//---------------------------------------------------------

void MagBox::textChanged()
      {
      if (!mscore->currentScoreView() || currentText().isEmpty())
            return;
      QString s = currentText();
      if (s.right(1) == "%")
            s = s.left(s.length()-1);

      bool ok;
      qreal magVal = s.toFloat(&ok);
      if (ok) {
            setMag((double)(magVal/100.0));
            emit magChanged(MagIdx::MAG_FREE);
            }
      }

//---------------------------------------------------------
//   indexChanged
//---------------------------------------------------------

void MagBox::indexChanged(int idx)
      {
      emit magChanged(itemData(idx).value<MagIdx>());
      }

//---------------------------------------------------------
//   getLMag
//    get logical scale
//---------------------------------------------------------

double MagBox::getLMag(ScoreView* canvas) const
      {
      return getMag(canvas) / (mscore->physicalDotsPerInch() / DPI);
      }

//---------------------------------------------------------
//   getMag
//    get physical scale
//---------------------------------------------------------

double MagBox::getMag(ScoreView* canvas) const
      {
      Score* score   = canvas->score();
      if (score == 0)
            return 1.0;

      MagIdx idx           = MagIdx(currentIndex());
      qreal pmag           = mscore->physicalDotsPerInch() / DPI;
      double cw            = canvas->width();
      double ch            = canvas->height();
      const PageFormat* pf = score->pageFormat();
      double nmag;

      switch (idx) {
            case MagIdx::MAG_25:      nmag = 0.25 * pmag; break;
            case MagIdx::MAG_50:      nmag = 0.5  * pmag; break;
            case MagIdx::MAG_75:      nmag = 0.75 * pmag; break;
            case MagIdx::MAG_100:     nmag = 1.0  * pmag; break;
            case MagIdx::MAG_150:     nmag = 1.5  * pmag; break;
            case MagIdx::MAG_200:     nmag = 2.0  * pmag; break;
            case MagIdx::MAG_400:     nmag = 4.0  * pmag; break;
            case MagIdx::MAG_800:     nmag = 8.0  * pmag; break;
            case MagIdx::MAG_1600:    nmag = 16.0 * pmag; break;

            case MagIdx::MAG_PAGE_WIDTH:      // page width
                  nmag = cw / (pf->width() * DPI);
                  break;

            case MagIdx::MAG_PAGE:     // page
                  {
                  double mag1 = cw / (pf->width() *  DPI);
                  double mag2 = ch / (pf->height() * DPI);
                  nmag  = (mag1 > mag2) ? mag2 : mag1;
                  }
                  break;

            case MagIdx::MAG_DBL_PAGE:    // double page
                  {
                  double mag1 = 0;
                  double mag2 = 0;
                  if (MScore::verticalOrientation()) {
                        mag1 = ch / (pf->height() * 2 * DPI +  MScore::verticalPageGap);
                        mag2 = cw / (pf->width() * DPI);
                        }
                  else {
                        mag1 = cw / (pf->width() * 2 * DPI + 50);
                        mag2 = ch / (pf->height() * DPI);
                        }
                  nmag  = (mag1 > mag2) ? mag2 : mag1;
                  }
                  break;

            case MagIdx::MAG_FREE:
                  nmag = freeMag * pmag;
                  break;

            default:
                  nmag = 0.0;
                  break;
            }
      if (nmag < 0.0001)
            nmag = canvas->mag();

      return nmag;
      }

//---------------------------------------------------------
//   MagValidator
//---------------------------------------------------------

MagValidator::MagValidator(QObject* parent)
   : QValidator(parent)
      {
      }

//---------------------------------------------------------
//   validate
//---------------------------------------------------------

QValidator::State MagValidator::validate(QString& input, int& /*pos*/) const
      {
      QComboBox* cb = (QComboBox*)parent();
      int mn = sizeof(magTable)/sizeof(*magTable);
      for (int i = 0; i < mn; ++i) {
            if (input == cb->itemText(i))
                  return QValidator::Acceptable;
            }
      QString d;
      for (int i = 0; i < input.size(); ++i) {
            QChar c = input[i];
            if (c.isDigit() || c == '.')
                  d.append(c);
            else if (c != '%')
                  return QValidator::Invalid;
            }
      if (d.isEmpty())
            return QValidator::Intermediate;
      bool ok;
      double nmag = d.toDouble(&ok);
      if (!ok)
            return QValidator::Invalid;
      if (nmag < 25.0 || nmag > 1600.0)
            return QValidator::Intermediate;
      return QValidator::Acceptable;
      }

//---------------------------------------------------------
//   setMag
//---------------------------------------------------------

void MagBox::setMag(double val)
      {
      const QSignalBlocker blocker(this);
      setCurrentIndex(int(MagIdx::MAG_FREE));
      freeMag = val;
      setItemText(int(MagIdx::MAG_FREE), QString("%1%").arg(freeMag * 100));
      }

//---------------------------------------------------------
//   setMagIdx
//---------------------------------------------------------

void MagBox::setMagIdx(MagIdx idx)
      {
      const QSignalBlocker blocker(this);
      setCurrentIndex(int(idx));
      }
}