MuseScore/src/commonscene/exampleview.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

507 lines
14 KiB
C++
Raw Normal View History

//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2013 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
// as published by the Free Software Foundation and appearing in
// the file LICENSE.GPL
//=============================================================================
#include "exampleview.h"
2020-12-05 16:59:48 +01:00
#include <cmath>
#include <QMimeData>
#include "libmscore/score.h"
#include "libmscore/element.h"
#include "libmscore/page.h"
#include "libmscore/system.h"
#include "libmscore/icon.h"
#include "libmscore/chord.h"
2014-04-09 16:09:21 +02:00
#include "libmscore/xml.h"
2020-12-03 10:31:16 +01:00
#include "commonscenetypes.h"
2013-05-13 18:49:17 +02:00
namespace Ms {
//---------------------------------------------------------
// ExampleView
//---------------------------------------------------------
2020-05-29 18:47:27 +02:00
ExampleView::ExampleView(QWidget* parent)
: QFrame(parent)
{
2013-05-02 16:40:42 +02:00
_score = 0;
setAcceptDrops(true);
setFocusPolicy(Qt::StrongFocus);
resetMatrix();
_fgPixmap = nullptr;
2020-07-22 20:22:31 +02:00
_fgColor = Qt::white;
if (notationConfiguration()->foregroundUseColor()) {
_fgColor = notationConfiguration()->foregroundColor();
} else {
_fgPixmap = new QPixmap(notationConfiguration()->foregroundWallpaper().toQString());
if (_fgPixmap == 0 || _fgPixmap->isNull()) {
qDebug("no valid pixmap %s", qPrintable(notationConfiguration()->foregroundWallpaper().toQString()));
}
2020-05-26 15:54:26 +02:00
}
// setup drag canvas state
sm = new QStateMachine(this);
QState* stateActive = new QState;
2020-05-26 15:54:26 +02:00
QState* s1 = new QState(stateActive);
s1->setObjectName("example-normal");
s1->assignProperty(this, "cursor", QCursor(Qt::ArrowCursor));
2020-05-26 15:54:26 +02:00
QState* s = new QState(stateActive);
s->setObjectName("example-drag");
s->assignProperty(this, "cursor", QCursor(Qt::SizeAllCursor));
QEventTransition* cl = new QEventTransition(this, QEvent::MouseButtonRelease);
cl->setTargetState(s1);
s->addTransition(cl);
s1->addTransition(new DragTransitionExampleView(this));
2020-05-26 15:54:26 +02:00
sm->addState(stateActive);
stateActive->setInitialState(s1);
sm->setInitialState(stateActive);
2020-05-26 15:54:26 +02:00
sm->start();
m_defaultScaling = 0.9 * uiConfiguration()->physicalDotsPerInch() / DPI; // 90% of nominal
}
//---------------------------------------------------------
// ~ExampleView
//---------------------------------------------------------
ExampleView::~ExampleView()
{
if (_fgPixmap) {
delete _fgPixmap;
2020-05-26 15:54:26 +02:00
}
}
//---------------------------------------------------------
// resetMatrix
// used to reset scrolling in case time signature num or denom changed
//---------------------------------------------------------
void ExampleView::resetMatrix()
{
double mag = m_defaultScaling;
qreal _spatium = SPATIUM20 * mag;
// example would normally be 10sp from top of page; this leaves 3sp margin above
_matrix = QTransform(mag, 0.0, 0.0, mag, _spatium, -_spatium * 7.0);
imatrix = _matrix.inverted();
}
void ExampleView::layoutChanged()
{
}
void ExampleView::dataChanged(const QRectF&)
{
}
void ExampleView::updateAll()
{
update();
}
2020-08-04 10:29:03 +02:00
void ExampleView::adjustCanvasPosition(const Element* /*el*/, bool /*playBack*/, int)
{
}
//---------------------------------------------------------
// setScore
//---------------------------------------------------------
void ExampleView::setScore(Score* s)
{
delete _score;
_score = s;
_score->addViewer(this);
_score->setLayoutMode(LayoutMode::LINE);
2020-05-26 15:54:26 +02:00
ScoreLoad sl;
_score->doLayout();
update();
}
void ExampleView::removeScore()
{
}
void ExampleView::changeEditElement(Element*)
{
}
QCursor ExampleView::cursor() const
{
return QCursor();
}
void ExampleView::setCursor(const QCursor&)
{
}
void ExampleView::setDropRectangle(const QRectF&)
{
}
2014-01-15 22:05:05 +01:00
void ExampleView::cmdAddSlur(Note* /*firstNote*/, Note* /*lastNote*/)
{
}
Element* ExampleView::elementNear(QPointF)
{
return 0;
}
void ExampleView::drawBackground(QPainter* p, const QRectF& r) const
{
if (_fgPixmap == 0 || _fgPixmap->isNull()) {
p->fillRect(r, _fgColor);
} else {
p->drawTiledPixmap(r, *_fgPixmap, r.topLeft()
- QPoint(lrint(_matrix.dx()), lrint(_matrix.dy())));
}
}
//---------------------------------------------------------
// drawElements
//---------------------------------------------------------
void ExampleView::drawElements(QPainter& painter, const QList<Element*>& el)
{
for (Element* e : el) {
e->itemDiscovered = 0;
QPointF pos(e->pagePos());
painter.translate(pos);
e->draw(&painter);
painter.translate(-pos);
}
}
//---------------------------------------------------------
// paintEvent
//---------------------------------------------------------
void ExampleView::paintEvent(QPaintEvent* ev)
{
if (_score) {
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
p.setRenderHint(QPainter::TextAntialiasing, true);
const QRect r(ev->rect());
2020-05-26 15:54:26 +02:00
drawBackground(&p, r);
2020-05-26 15:54:26 +02:00
p.setTransform(_matrix);
QRectF fr = imatrix.mapRect(QRectF(r));
2020-05-26 15:54:26 +02:00
QRegion r1(r);
Page* page = _score->pages().front();
QList<Element*> ell = page->items(fr);
Fix Deprecation warnings with Qt 5.15 (and 5.14) * fix 383 warnings C4996 (deprecated) with one single line change * fix 5 'empty' warnings about invalid slots, not sure the remaining slots serve any purpose though * disable warnings C4127 and C4996 for thirdparty/google_analytics * fix 30 warnings C4996 using the suggested alternatives available since at least Qt 5.9, so won't break building against that * change as requested in code review plus some more fixes using the same idea * fixing yet more warnings * disable deprecation warnings in qoogle_analytics for MinGW too although I believe maxOS and Linux may use those too. Easy to extend to those, if need be. * Fix qml runtime warnings exactly following the advice given by that warning, but without really know what I'm doing here or whether that is still backwards compatible to Qt 5.9. * Use replacements as suggested, if available and no `endl` neded with `qDebug()` * fix 24 more * 2 more * 7 more (one only seen in DEBUG mode) * Fix the `endl` warnings * maybe more changes this way? * Add all warnings C5999 or bigger to the ignore list for telemetry with Qt 5.15 Beta 1 as avoiding only C26439, C26444,C 26452, C26495, C26498, C26812 isn't possible * fix 2 deprecation warning new with Qt 5.15 beta 1 * fix 4 new warnings and also Qt 5.12 builds, disable some dead code * fix deprecation warnings new with Qt 5.15's MSVC 2019 integration and revert some (`QNetworkReply::networkError()`) that Qt 5.15 beta 2 reverted too * fix warning reg. obsolete operator < for QVariants * revert changes needed prior to beta 3 And clarifying some earlier changes * mark ToDos * fix warning reg QString()::null * fix a 'regression' from a revert of an earlier change
2020-06-03 12:59:19 +02:00
std::stable_sort(ell.begin(), ell.end(), elementLessThan);
drawElements(p, ell);
}
QFrame::paintEvent(ev);
}
//---------------------------------------------------------
// dragEnterEvent
//---------------------------------------------------------
void ExampleView::dragEnterEvent(QDragEnterEvent* event)
{
2018-08-17 15:06:15 +02:00
const QMimeData* d = event->mimeData();
2020-08-27 13:20:03 +02:00
if (d->hasFormat(mu::MIME_SYMBOL_FORMAT)) {
event->acceptProposedAction();
2020-08-27 13:20:03 +02:00
QByteArray a = d->data(mu::MIME_SYMBOL_FORMAT);
// qDebug("ExampleView::dragEnterEvent Symbol: <%s>", a.data());
XmlReader e(a);
QPointF dragOffset;
Fraction duration; // dummy
2017-01-18 14:16:33 +01:00
ElementType type = Element::readType(e, &dragOffset, &duration);
2020-05-26 15:54:26 +02:00
dragElement = Element::create(type, _score);
if (dragElement) {
dragElement->setParent(0);
dragElement->read(e);
dragElement->layout();
}
return;
}
}
//---------------------------------------------------------
// dragLeaveEvent
//---------------------------------------------------------
void ExampleView::dragLeaveEvent(QDragLeaveEvent*)
{
if (dragElement) {
delete dragElement;
dragElement = 0;
}
setDropTarget(0);
}
//---------------------------------------------------------
// moveElement
//---------------------------------------------------------
static void moveElement(void* data, Element* e)
{
QPointF* pos = (QPointF*)data;
e->score()->addRefresh(e->canvasBoundingRect());
e->setPos(*pos);
e->score()->addRefresh(e->canvasBoundingRect());
}
//---------------------------------------------------------
// dragMoveEvent
//---------------------------------------------------------
void ExampleView::dragMoveEvent(QDragMoveEvent* event)
{
event->acceptProposedAction();
2020-05-26 15:54:26 +02:00
2017-01-18 14:16:33 +01:00
if (!dragElement || dragElement->type() != ElementType::ICON) {
return;
2020-05-26 15:54:26 +02:00
}
QPointF pos(imatrix.map(QPointF(event->pos())));
QList<Element*> el = elementsAt(pos);
bool found = false;
foreach (const Element* e, el) {
2017-01-18 14:16:33 +01:00
if (e->type() == ElementType::NOTE) {
setDropTarget(const_cast<Element*>(e));
found = true;
break;
}
}
if (!found) {
setDropTarget(0);
2020-05-26 15:54:26 +02:00
}
dragElement->scanElements(&pos, moveElement, false);
_score->update();
return;
}
//---------------------------------------------------------
// setDropTarget
//---------------------------------------------------------
void ExampleView::setDropTarget(const Element* el)
{
if (dropTarget != el) {
if (dropTarget) {
dropTarget->setDropTarget(false);
dropTarget = 0;
}
dropTarget = el;
if (dropTarget) {
dropTarget->setDropTarget(true);
}
}
if (!dropAnchor.isNull()) {
QRectF r;
r.setTopLeft(dropAnchor.p1());
r.setBottomRight(dropAnchor.p2());
dropAnchor = QLineF();
}
if (dropRectangle.isValid()) {
dropRectangle = QRectF();
}
update();
}
//---------------------------------------------------------
// dropEvent
//---------------------------------------------------------
void ExampleView::dropEvent(QDropEvent* event)
{
QPointF pos(imatrix.map(QPointF(event->pos())));
2020-05-26 15:54:26 +02:00
if (!dragElement) {
return;
2020-05-26 15:54:26 +02:00
}
2017-01-18 14:16:33 +01:00
if (dragElement->type() != ElementType::ICON) {
delete dragElement;
dragElement = 0;
return;
}
foreach (Element* e, elementsAt(pos)) {
2017-01-18 14:16:33 +01:00
if (e->type() == ElementType::NOTE) {
Icon* icon = static_cast<Icon*>(dragElement);
Chord* chord = static_cast<Note*>(e)->chord();
emit beamPropertyDropped(chord, icon);
switch (icon->iconType()) {
case IconType::SBEAM:
chord->setBeamMode(Beam::Mode::BEGIN);
break;
case IconType::MBEAM:
chord->setBeamMode(Beam::Mode::AUTO);
break;
case IconType::BEAM32:
chord->setBeamMode(Beam::Mode::BEGIN32);
break;
case IconType::BEAM64:
chord->setBeamMode(Beam::Mode::BEGIN64);
break;
default:
break;
}
score()->doLayout();
2020-05-26 15:54:26 +02:00
break;
}
}
event->acceptProposedAction();
delete dragElement;
dragElement = 0;
setDropTarget(0);
}
2013-05-03 10:11:50 +02:00
//---------------------------------------------------------
// mousePressEvent
//---------------------------------------------------------
void ExampleView::mousePressEvent(QMouseEvent* event)
{
startMove = imatrix.map(QPointF(event->pos()));
2013-05-03 10:11:50 +02:00
QPointF pos(imatrix.map(QPointF(event->pos())));
2020-05-26 15:54:26 +02:00
2013-05-03 10:11:50 +02:00
foreach (Element* e, elementsAt(pos)) {
2017-01-18 14:16:33 +01:00
if (e->type() == ElementType::NOTE) {
2013-05-03 10:11:50 +02:00
emit noteClicked(static_cast<Note*>(e));
break;
}
}
}
//---------------------------------------------------------
// sizeHint
//---------------------------------------------------------
QSize ExampleView::sizeHint() const
{
qreal mag = m_defaultScaling;
2015-12-03 07:26:52 +01:00
qreal _spatium = SPATIUM20 * mag;
// staff is 4sp tall with 3sp margin above; this leaves 3sp margin below
qreal height = 10.0 * _spatium;
if (score() && score()->pages().size() > 0) {
height = score()->pages()[0]->tbbox().height() * mag + (6 * _spatium);
2020-05-26 15:54:26 +02:00
}
return QSize(1000 * mag, height);
}
//---------------------------------------------------------
// dragExampleView
// constrained scrolling ensuring that this ExampleView won't be moved past the borders of its QFrame
//---------------------------------------------------------
void ExampleView::dragExampleView(QMouseEvent* ev)
{
QPoint d = ev->pos() - _matrix.map(startMove).toPoint();
int dx = d.x();
if (dx == 0) {
return;
2020-05-26 15:54:26 +02:00
}
2017-02-24 12:04:22 +01:00
constraintCanvas(&dx);
2020-05-26 15:54:26 +02:00
2017-02-24 12:04:22 +01:00
// Perform the actual scrolling
_matrix.setMatrix(_matrix.m11(), _matrix.m12(), _matrix.m13(), _matrix.m21(),
_matrix.m22(), _matrix.m23(), _matrix.dx() + dx, _matrix.dy(), _matrix.m33());
imatrix = _matrix.inverted();
scroll(dx, 0);
}
void DragTransitionExampleView::onTransition(QEvent* e)
{
QStateMachine::WrappedEvent* we = static_cast<QStateMachine::WrappedEvent*>(e);
QMouseEvent* me = static_cast<QMouseEvent*>(we->event());
canvas->dragExampleView(me);
}
//---------------------------------------------------------
// wheelEvent
//---------------------------------------------------------
void ExampleView::wheelEvent(QWheelEvent* event)
{
QPoint pixelsScrolled = event->pixelDelta();
QPoint stepsScrolled = event->angleDelta();
int dx = 0, dy = 0;
if (!pixelsScrolled.isNull()) {
dx = pixelsScrolled.x();
dy = pixelsScrolled.y();
} else if (!stepsScrolled.isNull()) {
dx = static_cast<qreal>(stepsScrolled.x()) * qMax(2, width() / 10) / 120;
dy = static_cast<qreal>(stepsScrolled.y()) * qMax(2, height() / 10) / 120;
}
2020-05-26 15:54:26 +02:00
2017-02-24 12:04:22 +01:00
if (dx == 0) {
if (dy == 0) {
return;
} else {
dx = dy;
2020-05-26 15:54:26 +02:00
}
2017-02-24 12:04:22 +01:00
}
constraintCanvas(&dx);
_matrix.setMatrix(_matrix.m11(), _matrix.m12(), _matrix.m13(), _matrix.m21(),
_matrix.m22(), _matrix.m23(), _matrix.dx() + dx, _matrix.dy(), _matrix.m33());
imatrix = _matrix.inverted();
scroll(dx, 0);
}
//-----------------------------------------------------------------------------
// constraintCanvas
//-----------------------------------------------------------------------------
void ExampleView::constraintCanvas(int* dxx)
{
int dx = *dxx;
2020-05-26 15:54:26 +02:00
Q_ASSERT(_score->pages().front()->system(0)); // should exist if doLayout ran
2020-05-26 15:54:26 +02:00
// form rectangle bounding the system with a spatium margin and translate relative to view space
qreal xstart = _score->pages().front()->system(0)->bbox().left() - SPATIUM20;
qreal xend = _score->pages().front()->system(0)->bbox().right() + 2.0 * SPATIUM20;
QRectF systemScaledViewRect(xstart * _matrix.m11(), 0, xend * _matrix.m11(), 0);
systemScaledViewRect.translate(_matrix.dx(), 0);
2020-05-26 15:54:26 +02:00
qreal frameWidth = static_cast<QFrame*>(this)->frameRect().width();
2020-05-26 15:54:26 +02:00
// constrain the dx of scrolling so that this ExampleView won't be moved past the borders of its QFrame
if (dx > 0) {
// when moving right, ensure the left edge of systemScaledViewRect won't be right of frame's left edge
if (systemScaledViewRect.left() + dx > 0) {
dx = -systemScaledViewRect.left();
}
} else {
// never move left if entire system already fits entirely within the frame
if (systemScaledViewRect.width() < frameWidth) {
dx = 0;
2020-05-26 15:54:26 +02:00
}
// when moving left, ensure the right edge of systemScaledViewRect won't be left of frame's right edge
else if (systemScaledViewRect.right() + dx < frameWidth) {
dx = frameWidth - systemScaledViewRect.right();
2020-05-26 15:54:26 +02:00
}
}
2020-05-26 15:54:26 +02:00
2017-02-24 12:04:22 +01:00
*dxx = dx;
}
2013-05-13 18:49:17 +02:00
}