MuseScore/src/notation/internal/notationselection.cpp
2021-06-08 17:04:11 +02:00

135 lines
3.2 KiB
C++

/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore BVBA 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 3 as
* published by the Free Software Foundation.
*
* 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, see <https://www.gnu.org/licenses/>.
*/
#include "notationselection.h"
#include "libmscore/score.h"
#include "libmscore/segment.h"
#include "libmscore/measure.h"
#include "notationselectionrange.h"
#include "log.h"
using namespace mu::notation;
NotationSelection::NotationSelection(IGetScore* getScore)
: m_getScore(getScore)
{
m_range = std::make_shared<NotationSelectionRange>(getScore);
}
bool NotationSelection::isNone() const
{
return score()->selection().isNone();
}
bool NotationSelection::isRange() const
{
return score()->selection().isRange();
}
bool NotationSelection::canCopy() const
{
return score()->selection().canCopy();
}
QMimeData* NotationSelection::mimeData() const
{
QString mimeType = score()->selection().mimeType();
if (mimeType.isEmpty()) {
return nullptr;
}
QMimeData* mimeData = new QMimeData();
mimeData->setData(mimeType, score()->selection().mimeData());
return mimeData;
}
Element* NotationSelection::element() const
{
return score()->selection().element();
}
std::vector<Element*> NotationSelection::elements() const
{
std::vector<Element*> els;
QList<Ms::Element*> list = score()->selection().elements();
els.reserve(list.count());
for (Ms::Element* e : list) {
els.push_back(e);
}
return els;
}
std::vector<Note*> NotationSelection::notes(NoteFilter filter) const
{
switch (filter) {
case NoteFilter::All: return score()->selection().noteList();
case NoteFilter::WithTie: return score()->cmdTieNoteList(score()->selection(), false);
case NoteFilter::WithSlur: {
NOT_IMPLEMENTED;
return {};
}
}
return {};
}
QRectF NotationSelection::canvasBoundingRect() const
{
if (isNone()) {
return QRectF();
}
Ms::Element* el = score()->selection().element();
if (el) {
return el->canvasBoundingRect().toQRectF();
}
QList<Ms::Element*> els = score()->selection().elements();
if (els.isEmpty()) {
LOGW() << "selection not none, but no elements";
return QRectF();
}
RectF rect;
for (const Ms::Element* elm: els) {
if (rect.isNull()) {
rect = elm->canvasBoundingRect();
} else {
rect = rect.united(elm->canvasBoundingRect());
}
}
return rect.toQRectF();
}
INotationSelectionRangePtr NotationSelection::range() const
{
return m_range;
}
Ms::Score* NotationSelection::score() const
{
return m_getScore->score();
}