MuseScore/mscore/navigator.cpp

396 lines
12 KiB
C++
Raw Normal View History

2012-05-26 14:49:10 +02:00
//=============================================================================
// MuseScore
2013-05-08 10:54:06 +02:00
// Music Composition & Notation
2012-05-26 14:49:10 +02:00
//
2013-05-08 10:54:06 +02:00
// Copyright (C) 2002-2013 Werner Schweer
2012-05-26 14:49:10 +02:00
//
// This program is free software; you can redistribute it and/or modify
2013-05-08 10:54:06 +02:00
// 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
2012-05-26 14:49:10 +02:00
//=============================================================================
#include "navigator.h"
#include "musescore.h"
#include "scoreview.h"
#include "libmscore/score.h"
#include "libmscore/page.h"
#include "preferences.h"
#include "libmscore/mscore.h"
#include "libmscore/system.h"
#include "libmscore/measurebase.h"
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// showNavigator
//---------------------------------------------------------
void MuseScore::showNavigator(bool visible)
{
Navigator* n = static_cast<Navigator*>(_navigator->widget());
if (n == 0 && visible) {
n = new Navigator(_navigator, this);
n->setScoreView(cv);
}
2012-05-31 16:24:35 +02:00
_navigator->setVisible(visible);
2012-05-26 14:49:10 +02:00
getAction("toggle-navigator")->setChecked(visible);
}
//---------------------------------------------------------
// NScrollArea
//---------------------------------------------------------
NScrollArea::NScrollArea(QWidget* w)
: QScrollArea(w)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
2012-05-26 14:49:10 +02:00
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setMinimumHeight(40);
setLineWidth(0);
}
//---------------------------------------------------------
// orientationChanged
//---------------------------------------------------------
void NScrollArea::orientationChanged()
{
if (MScore::verticalOrientation()) {
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}
else {
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// resizeEvent
//---------------------------------------------------------
void NScrollArea::resizeEvent(QResizeEvent* ev)
{
if (widget()) {
widget()->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
widget()->setMinimumSize(0, 0);
}
2013-05-08 10:54:06 +02:00
if (widget() && (ev->size().height() != ev->oldSize().height()))
2012-05-26 14:49:10 +02:00
widget()->resize(widget()->width(), ev->size().height());
if (widget() && (ev->size().width() != ev->oldSize().width()))
widget()->resize(ev->size().width(), widget()->height());
2012-05-26 14:49:10 +02:00
QScrollArea::resizeEvent(ev);
}
2012-12-05 17:46:08 +01:00
//---------------------------------------------------------
// ViewRect
//---------------------------------------------------------
ViewRect::ViewRect(QWidget* w)
: QWidget(w)
{
}
//---------------------------------------------------------
// paintEvent
//---------------------------------------------------------
void ViewRect::paintEvent(QPaintEvent* ev)
{
QPainter p(this);
QColor c(MScore::selectColor[0]);
QPen pen(c, 2.0);
2012-12-05 17:46:08 +01:00
p.setPen(pen);
p.setBrush(QColor(c.red(), c.green(), c.blue(), 40));
2012-12-05 17:46:08 +01:00
p.drawRect(ev->rect());
}
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
// Navigator
//---------------------------------------------------------
Navigator::Navigator(NScrollArea* sa, QWidget* parent)
: QWidget(parent)
{
2018-12-21 16:18:32 +01:00
setObjectName("Navigator");
2012-05-26 14:49:10 +02:00
setAttribute(Qt::WA_NoBackground);
_score = 0;
scrollArea = sa;
2013-05-08 10:54:06 +02:00
scrollArea->setWidgetResizable(true);
2012-05-26 14:49:10 +02:00
_cv = 0;
2012-12-05 17:46:08 +01:00
viewRect = new ViewRect(this);
2012-05-26 14:49:10 +02:00
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
sa->setWidget(this);
sa->setWidgetResizable(false);
_previewOnly = false;
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// resizeEvent
//---------------------------------------------------------
void Navigator::resizeEvent(QResizeEvent* /*ev*/)
2012-05-26 14:49:10 +02:00
{
if (_score) {
rescale();
2012-12-05 14:49:42 +01:00
updateViewRect();
2012-05-26 14:49:10 +02:00
}
}
//---------------------------------------------------------
// setScoreView
//---------------------------------------------------------
void Navigator::setScoreView(ScoreView* v)
{
if (_cv) {
disconnect(this, SIGNAL(viewRectMoved(const QRectF&)), _cv, SLOT(setViewRect(const QRectF&)));
disconnect(_cv, SIGNAL(viewRectChanged()), this, SLOT(updateViewRect()));
}
_cv = QPointer<ScoreView>(v);
if (v) {
_score = v->score();
rescale();
connect(this, SIGNAL(viewRectMoved(const QRectF&)), v, SLOT(setViewRect(const QRectF&)));
connect(_cv, SIGNAL(viewRectChanged()), this, SLOT(updateViewRect()));
rescale();
2013-05-08 10:54:06 +02:00
updateViewRect();
update();
2012-05-26 14:49:10 +02:00
}
else {
_score = 0;
updateViewRect();
//update() should be enough... see #21841
repaint();
2012-05-26 14:49:10 +02:00
}
}
//---------------------------------------------------------
// setScore
//---------------------------------------------------------
void Navigator::setScore(Score* v)
{
setScoreView(nullptr); // ensure all connections to ScoreView get disconnected
_score = v;
rescale();
2013-05-08 10:54:06 +02:00
updateViewRect();
2012-12-05 17:46:08 +01:00
update();
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// rescale
2013-05-08 10:54:06 +02:00
// recompute scale of score view
2012-05-26 14:49:10 +02:00
//---------------------------------------------------------
void Navigator::rescale()
{
2013-05-08 10:54:06 +02:00
if (!_score || _score->pages().isEmpty()) {
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
setMinimumSize(0, 0);
return;
}
2013-05-08 10:54:06 +02:00
Page* lp = _score->pages().back();
2012-05-26 14:49:10 +02:00
// reset the layout before setting fix size
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
setMinimumSize(0, 0);
2012-05-31 16:24:35 +02:00
if (MScore::verticalOrientation() && !_previewOnly) {
qreal scoreWidth = lp->width();
qreal scoreHeight = lp->y() + lp->height();
qreal m = width() / scoreWidth;
setFixedHeight(int(scoreHeight * m));
matrix = QTransform(m, 0, 0, m, 0, 0);
}
else {
qreal scoreWidth = lp->x() + lp->width();
qreal scoreHeight = lp->height();
if (_previewOnly)
scoreWidth = lp->width() * _score->pages().size();
qreal m = height() / scoreHeight;
setFixedWidth(int(scoreWidth * m));
matrix = QTransform(m, 0, 0, m, 0, 0);
}
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// updateViewRect
//---------------------------------------------------------
void Navigator::updateViewRect()
{
2013-05-08 10:54:06 +02:00
QRect r;
if (_cv)
r = _cv->toLogical(QRect(0.0, 0.0, _cv->width(), _cv->height())).toRect();
setViewRect(r);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// mousePressEvent
//---------------------------------------------------------
void Navigator::mousePressEvent(QMouseEvent* ev)
{
if (_cv == 0)
return;
startMove = ev->pos();
2012-12-05 17:46:08 +01:00
if (!viewRect->geometry().contains(startMove)) {
2012-05-26 14:49:10 +02:00
QPointF p = matrix.inverted().map(QPointF(ev->pos()));
QRectF r(_cv->toLogical(QRectF(0.0, 0.0, _cv->width(), _cv->height())));
double dx = p.x() - (r.x() + (r.width() * .5));
double dy = p.y() - (r.y() + (r.height() * .5));
r.translate(dx, dy);
setViewRect(r);
2012-12-05 17:46:08 +01:00
emit viewRectMoved(matrix.inverted().mapRect(viewRect->geometry()));
2012-05-26 14:49:10 +02:00
}
}
//---------------------------------------------------------
// mouseMoveEvent
//---------------------------------------------------------
void Navigator::mouseMoveEvent(QMouseEvent* ev)
{
QPoint delta = ev->pos() - startMove;
2012-12-05 17:46:08 +01:00
QRect r(viewRect->geometry().translated(delta));
2012-05-26 14:49:10 +02:00
startMove = ev->pos();
2012-12-05 17:46:08 +01:00
if (r.width() == width())
r.moveLeft(0);
else if (r.width() < width()) {
if (r.x() < 0)
r.moveLeft(0);
else if (r.right() > width())
r.moveRight(width());
2012-05-26 14:49:10 +02:00
}
else {
2012-12-05 17:46:08 +01:00
if (r.right() < width())
r.moveRight(width());
else if (r.left() > 0)
r.moveLeft(0);
2012-05-26 14:49:10 +02:00
}
2012-12-05 17:46:08 +01:00
if (r.height() == height())
r.moveTop(0);
else if (r.height() < height()) {
if (r.y() < 0)
r.moveTop(0);
else if (r.bottom() > height())
r.moveBottom(height());
2012-05-26 14:49:10 +02:00
}
else {
2012-12-05 17:46:08 +01:00
if (r.bottom() < height())
r.moveBottom(height());
else if (r.top() > 0)
r.moveTop(0);
2012-05-26 14:49:10 +02:00
}
2012-12-05 17:46:08 +01:00
viewRect->setGeometry(r);
emit viewRectMoved(matrix.inverted().mapRect(r));
if (MScore::verticalOrientation() && !_previewOnly) {
int y = delta.y() > 0 ? r.y() + r.height() : r.y();
scrollArea->ensureVisible(width()/2, y, 0, 0);
}
else {
int x = delta.x() > 0 ? r.x() + r.width() : r.x();
scrollArea->ensureVisible(x, height()/2, 0, 0);
}
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// setViewRect
//---------------------------------------------------------
void Navigator::setViewRect(const QRectF& _viewRect)
{
2012-12-05 17:46:08 +01:00
viewRect->setGeometry(matrix.mapRect(_viewRect).toRect());
if (MScore::verticalOrientation() && !_previewOnly)
scrollArea->ensureVisible(0, viewRect->y() + viewRect->height() / 2);
else
scrollArea->ensureVisible(viewRect->x(), 0);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// paintElement
//---------------------------------------------------------
static void paintElement(void* data, Element* e)
{
QPainter* p = static_cast<QPainter*>(data);
2012-12-05 14:49:42 +01:00
QPointF pos(e->pagePos());
p->translate(pos);
2012-05-26 14:49:10 +02:00
e->draw(p);
2012-12-05 14:49:42 +01:00
p->translate(-pos);
2012-05-26 14:49:10 +02:00
}
//---------------------------------------------------------
// layoutChanged
//---------------------------------------------------------
void Navigator::layoutChanged()
{
2012-12-05 17:46:08 +01:00
if (_score && !_score->pages().isEmpty())
rescale();
2012-05-26 14:49:10 +02:00
update();
}
//---------------------------------------------------------
// paintEvent
//---------------------------------------------------------
void Navigator::paintEvent(QPaintEvent* ev)
{
QPainter p(this);
QRect r(ev->rect());
p.fillRect(r, palette().color(QPalette::Window));
2012-12-05 14:49:42 +01:00
// qDebug("navigator paint x %d w %d h %d", r.x(), r.width(), r.height());
2013-07-09 15:12:50 +02:00
2012-12-05 14:49:42 +01:00
if (!_score)
return;
if (_score->pages().size() <= 0)
return;
// compute optimal size of page number
QFont font("FreeSans", 4000);
QFontMetrics fm (font);
Page* firstPage = _score->pages()[0];
qreal factor = (firstPage->width() * 0.5) / fm.width(QString::number(_score->pages().size()));
font.setPointSizeF(font.pointSizeF() * factor);
2012-05-26 14:49:10 +02:00
2012-12-05 14:49:42 +01:00
p.setTransform(matrix);
QRectF fr = matrix.inverted().mapRect(QRectF(r));
int i = 0;
for (Page* page : _score->pages()) {
2012-12-05 14:49:42 +01:00
QPointF pos(page->pos());
if (_previewOnly)
pos = QPointF(i * page->width(), 0);
2012-12-05 14:49:42 +01:00
QRectF pr(page->abbox().translated(pos));
if (pr.right() < fr.left())
continue;
if (pr.left() > fr.right())
break;
p.fillRect(pr, Qt::white);
p.translate(pos);
2016-01-04 14:48:58 +01:00
for (System* s : page->systems()) {
for (MeasureBase* m : s->measures())
2012-12-05 14:49:42 +01:00
m->scanElements(&p, paintElement, false);
2012-05-26 14:49:10 +02:00
}
2012-12-05 14:49:42 +01:00
page->scanElements(&p, paintElement, false);
if (page->score()->layoutMode() == LayoutMode::PAGE) {
p.setFont(font);
p.setPen(MScore::layoutBreakColor);
p.drawText(page->bbox(), Qt::AlignCenter, QString("%1").arg(page->no() + 1 + _score->pageNumberOffset()));
2012-12-05 14:49:42 +01:00
}
p.translate(-pos);
i++;
2012-05-26 14:49:10 +02:00
}
}
2013-05-13 18:49:17 +02:00
}
2012-12-10 12:06:13 +01:00