MuseScore/mscore/magbox.cpp

264 lines
8.4 KiB
C++
Raw Normal View History

2012-05-26 14:49:10 +02:00
//=============================================================================
// 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.
//=============================================================================
2015-11-23 16:02:33 +01:00
#include "preferences.h"
2012-05-26 14:49:10 +02:00
#include "magbox.h"
#include "scoreview.h"
#include "libmscore/page.h"
#include "musescore.h"
#include "libmscore/score.h"
#include "libmscore/mscore.h"
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// magTable
// list of strings shown in QComboBox "MagBox"
//---------------------------------------------------------
2015-11-23 16:02:33 +01:00
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 },
2012-05-26 14:49:10 +02:00
};
//---------------------------------------------------------
// 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);
2012-05-26 14:49:10 +02:00
2015-11-23 16:02:33 +01:00
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)
2012-05-26 14:49:10 +02:00
setCurrentIndex(i);
2015-11-23 16:02:33 +01:00
++i;
2012-05-26 14:49:10 +02:00
}
2015-11-23 16:02:33 +01:00
setMaxCount(i+1);
addItem(QString("%1%").arg(freeMag * 100), int(MagIdx::MAG_FREE));
2015-11-23 16:02:33 +01:00
setFocusPolicy(Qt::StrongFocus);
setAccessibleName(tr("Zoom"));
setFixedHeight(preferences.getInt(PREF_UI_THEME_ICONHEIGHT) + 8); // hack
2012-05-26 14:49:10 +02:00
connect(this, SIGNAL(currentIndexChanged(int)), SLOT(indexChanged(int)));
2015-11-23 16:02:33 +01:00
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);
}
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// indexChanged
//---------------------------------------------------------
void MagBox::indexChanged(int idx)
{
2015-11-23 16:02:33 +01:00
emit magChanged(itemData(idx).value<MagIdx>());
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
2015-11-23 16:02:33 +01:00
// getLMag
// get logical scale
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
2015-11-23 16:02:33 +01:00
double MagBox::getLMag(ScoreView* canvas) const
2012-05-26 14:49:10 +02:00
{
2015-11-23 16:02:33 +01:00
return getMag(canvas) / (mscore->physicalDotsPerInch() / DPI);
}
//---------------------------------------------------------
// getMag
// get physical scale
//---------------------------------------------------------
2012-05-26 14:49:10 +02:00
2015-11-23 16:02:33 +01:00
double MagBox::getMag(ScoreView* canvas) const
{
2012-05-26 14:49:10 +02:00
Score* score = canvas->score();
if (score == 0)
return 1.0;
2015-11-23 16:02:33 +01:00
MagIdx idx = MagIdx(currentIndex());
qreal pmag = mscore->physicalDotsPerInch() / DPI;
double cw = canvas->width();
double ch = canvas->height();
2018-03-27 15:36:00 +02:00
qreal pw = score->styleD(Sid::pageWidth);
qreal ph = score->styleD(Sid::pageHeight);
2015-11-23 16:02:33 +01:00
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 / (pw * DPI);
2012-05-26 14:49:10 +02:00
break;
2015-11-23 16:02:33 +01:00
case MagIdx::MAG_PAGE: // page
2012-05-26 14:49:10 +02:00
{
double mag1 = cw / (pw * DPI);
double mag2 = ch / (ph * DPI);
2015-11-23 16:02:33 +01:00
nmag = (mag1 > mag2) ? mag2 : mag1;
2012-05-26 14:49:10 +02:00
}
break;
2015-11-23 16:02:33 +01:00
case MagIdx::MAG_DBL_PAGE: // double page
2012-05-26 14:49:10 +02:00
{
double mag1 = 0;
double mag2 = 0;
if (MScore::verticalOrientation()) {
mag1 = ch / (ph * 2 * DPI + MScore::verticalPageGap);
mag2 = cw / (pw * DPI);
}
else {
mag1 = cw / (pw * 2 * DPI + 50);
mag2 = ch / (ph * DPI);
}
2015-11-23 16:02:33 +01:00
nmag = (mag1 > mag2) ? mag2 : mag1;
2012-05-26 14:49:10 +02:00
}
break;
2015-11-23 16:02:33 +01:00
case MagIdx::MAG_FREE:
2015-11-23 16:02:33 +01:00
nmag = freeMag * pmag;
break;
default:
nmag = 0.0;
break;
2012-05-26 14:49:10 +02:00
}
if (nmag < 0.0001)
nmag = canvas->mag();
2015-11-23 16:02:33 +01:00
2012-05-26 14:49:10 +02:00
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));
2012-05-26 14:49:10 +02:00
freeMag = val;
setItemText(int(MagIdx::MAG_FREE), QString("%1%").arg(freeMag * 100));
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// setMagIdx
//---------------------------------------------------------
void MagBox::setMagIdx(MagIdx idx)
2012-05-26 14:49:10 +02:00
{
const QSignalBlocker blocker(this);
setCurrentIndex(int(idx));
2012-05-26 14:49:10 +02:00
}
2013-05-13 18:49:17 +02:00
}
2012-05-26 14:49:10 +02:00