MuseScore/src/notation/internal/notationinteraction.cpp

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

4842 lines
142 KiB
C++
Raw Normal View History

/*
* 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/>.
*/
2020-06-16 10:33:18 +02:00
#include "notationinteraction.h"
#include "log.h"
2020-06-16 10:33:18 +02:00
#include <memory>
#include <QRectF>
2020-06-16 10:48:13 +02:00
#include <QPainter>
2020-12-05 16:59:48 +01:00
#include <QClipboard>
#include <QApplication>
#include <QKeyEvent>
2020-06-16 10:33:18 +02:00
#include "defer.h"
#include "ptrutils.h"
2021-12-02 14:36:30 +01:00
#include "engraving/rw/xml.h"
2021-08-26 13:16:41 +02:00
#include "engraving/infrastructure/draw/pen.h"
#include "engraving/infrastructure/draw/painterpath.h"
2021-07-28 17:20:54 +02:00
2021-07-23 16:06:21 +02:00
#include "libmscore/masterscore.h"
2020-06-16 10:33:18 +02:00
#include "libmscore/page.h"
#include "libmscore/shadownote.h"
#include "libmscore/staff.h"
#include "libmscore/measure.h"
#include "libmscore/part.h"
#include "libmscore/drumset.h"
#include "libmscore/rest.h"
#include "libmscore/slur.h"
#include "libmscore/system.h"
#include "libmscore/chord.h"
#include "libmscore/elementgroup.h"
#include "libmscore/textframe.h"
2021-02-10 14:24:56 +01:00
#include "libmscore/figuredbass.h"
#include "libmscore/stafflines.h"
2021-12-06 13:49:48 +01:00
#include "libmscore/stafftype.h"
#include "libmscore/actionicon.h"
2020-07-09 14:31:34 +02:00
#include "libmscore/undo.h"
#include "libmscore/navigate.h"
#include "libmscore/keysig.h"
2020-12-04 14:21:54 +01:00
#include "libmscore/instrchange.h"
2021-05-25 12:37:51 +02:00
#include "libmscore/lasso.h"
#include "libmscore/textedit.h"
#include "libmscore/lyrics.h"
#include "libmscore/bracket.h"
#include "libmscore/factory.h"
#include "libmscore/image.h"
#include "libmscore/mscore.h"
#include "libmscore/linkedobjects.h"
2021-12-07 13:58:55 +01:00
#include "libmscore/tuplet.h"
2020-06-16 10:33:18 +02:00
2020-08-05 10:37:49 +02:00
#include "masternotation.h"
2020-06-16 10:33:18 +02:00
#include "scorecallbacks.h"
#include "notationnoteinput.h"
#include "notationselection.h"
#include "notationerrors.h"
2020-06-16 10:33:18 +02:00
2020-08-27 13:20:03 +02:00
using namespace mu::notation;
using namespace mu::framework;
using namespace mu::engraving;
2020-06-16 10:33:18 +02:00
static int findSystemIndex(Ms::EngravingItem* element)
{
Ms::System* target = element->parent()->isSystem() ? toSystem(element->parent()) : element->findMeasureBase()->system();
return element->score()->systems().indexOf(target);
}
static int findBracketIndex(Ms::EngravingItem* element)
{
if (!element->isBracket()) {
return -1;
}
Ms::Bracket* bracket = toBracket(element);
return bracket->system()->brackets().indexOf(bracket);
}
2020-10-23 19:30:10 +02:00
NotationInteraction::NotationInteraction(Notation* notation, INotationUndoStackPtr undoStack)
: m_notation(notation), m_undoStack(undoStack), m_editData(&m_scoreCallbacks)
2020-06-16 10:33:18 +02:00
{
m_noteInput = std::make_shared<NotationNoteInput>(notation, this, m_undoStack);
m_selection = std::make_shared<NotationSelection>(notation);
2020-06-16 10:33:18 +02:00
m_noteInput->stateChanged().onNotify(this, [this]() {
if (!m_noteInput->isNoteInputMode()) {
hideShadowNote();
}
});
m_dragData.ed = Ms::EditData(&m_scoreCallbacks);
m_dropData.ed = Ms::EditData(&m_scoreCallbacks);
m_scoreCallbacks.setScore(notation->score());
m_scoreCallbacks.setNotationInteraction(this);
2020-06-16 10:33:18 +02:00
}
NotationInteraction::~NotationInteraction()
{
delete m_shadowNote;
}
void NotationInteraction::init()
{
2020-12-15 14:56:47 +01:00
m_shadowNote = new Ms::ShadowNote(score());
m_shadowNote->setVisible(false);
}
2020-06-16 10:33:18 +02:00
Ms::Score* NotationInteraction::score() const
{
2020-08-05 12:20:48 +02:00
return m_notation->score();
2020-06-16 10:33:18 +02:00
}
void NotationInteraction::startEdit()
{
m_notifyAboutDropChanged = false;
m_undoStack->prepareChanges();
m_selectionState = score()->selection().state();
}
void NotationInteraction::apply()
{
m_undoStack->commitChanges();
if (m_notifyAboutDropChanged) {
notifyAboutDropChanged();
} else {
notifyAboutNotationChanged();
}
if (m_selectionState != score()->selection().state()) {
notifyAboutSelectionChanged();
}
}
2021-12-20 09:35:09 +01:00
void NotationInteraction::rollback()
{
m_undoStack->rollbackChanges();
}
void NotationInteraction::notifyAboutDragChanged()
{
m_dragChanged.notify();
}
void NotationInteraction::notifyAboutDropChanged()
{
m_dropChanged.notify();
}
void NotationInteraction::notifyAboutNotationChanged()
{
m_notation->notifyAboutNotationChanged();
}
void NotationInteraction::notifyAboutTextEditingStarted()
{
m_textEditingStarted.notify();
}
void NotationInteraction::notifyAboutTextEditingChanged()
{
m_textEditingChanged.notify();
}
void NotationInteraction::notifyAboutSelectionChanged()
{
updateGripEdit();
m_selectionChanged.notify();
}
void NotationInteraction::paint(mu::draw::Painter* painter)
2020-06-16 10:33:18 +02:00
{
m_shadowNote->draw(painter);
2020-06-16 10:48:13 +02:00
drawAnchorLines(painter);
drawTextEditMode(painter);
drawSelectionRange(painter);
2021-01-28 12:27:36 +01:00
drawGripPoints(painter);
2021-09-06 15:30:03 +02:00
if (m_lasso && !m_lasso->isEmpty()) {
2021-05-25 12:37:51 +02:00
m_lasso->draw(painter);
}
2020-06-16 10:33:18 +02:00
}
INotationNoteInputPtr NotationInteraction::noteInput() const
2020-06-16 10:33:18 +02:00
{
return m_noteInput;
2020-06-16 10:33:18 +02:00
}
2021-06-07 19:25:41 +02:00
void NotationInteraction::showShadowNote(const PointF& pos)
2020-06-16 10:33:18 +02:00
{
const Ms::InputState& inputState = score()->inputState();
Ms::Position position;
if (!score()->getPosition(&position, pos, inputState.voice())) {
2020-06-16 10:33:18 +02:00
m_shadowNote->setVisible(false);
return;
}
Staff* staff = score()->staff(position.staffIdx);
const Ms::Instrument* instr = staff->part()->instrument();
Ms::Segment* segment = position.segment;
qreal segmentSkylineTopY = 0;
qreal segmentSkylineBottomY = 0;
Ms::Segment* shadowNoteActualSegment = position.segment->prev1enabled();
if (shadowNoteActualSegment) {
segment = shadowNoteActualSegment;
segmentSkylineTopY = shadowNoteActualSegment->elementsTopOffsetFromSkyline(position.staffIdx);
segmentSkylineBottomY = shadowNoteActualSegment->elementsBottomOffsetFromSkyline(position.staffIdx);
}
Fraction tick = segment->tick();
qreal mag = staff->staffMag(tick);
2020-06-16 10:33:18 +02:00
// in any empty measure, pos will be right next to barline
// so pad this by barNoteDistance
qreal relX = position.pos.x() - position.segment->measure()->canvasPos().x();
position.pos.rx() -= qMin(relX - score()->styleMM(Ms::Sid::barNoteDistance) * mag, 0.0);
2020-06-16 10:33:18 +02:00
2021-12-08 14:22:23 +01:00
Ms::NoteHeadGroup noteheadGroup = Ms::NoteHeadGroup::HEAD_NORMAL;
2021-12-06 17:15:22 +01:00
Ms::NoteHeadType noteHead = inputState.duration().headType();
int line = position.line;
2020-06-16 10:33:18 +02:00
if (instr->useDrumset()) {
2020-12-15 14:56:47 +01:00
const Ms::Drumset* ds = instr->drumset();
int pitch = inputState.drumNote();
2020-06-16 10:33:18 +02:00
if (pitch >= 0 && ds->isValid(pitch)) {
line = ds->line(pitch);
2020-06-16 10:33:18 +02:00
noteheadGroup = ds->noteHead(pitch);
}
}
int voice = 0;
if (inputState.drumNote() != -1 && inputState.drumset() && inputState.drumset()->isValid(inputState.drumNote())) {
voice = inputState.drumset()->voice(inputState.drumNote());
2020-06-16 10:33:18 +02:00
} else {
voice = inputState.voice();
2020-06-16 10:33:18 +02:00
}
m_shadowNote->setVisible(true);
m_shadowNote->setMag(mag);
m_shadowNote->setTick(tick);
m_shadowNote->setStaffIdx(position.staffIdx);
m_shadowNote->setVoice(voice);
m_shadowNote->setLineIndex(line);
2020-06-16 10:33:18 +02:00
Ms::SymId symNotehead;
Ms::TDuration duration(inputState.duration());
2020-12-01 15:19:42 +01:00
if (inputState.rest()) {
int yo = 0;
2021-09-08 13:13:20 +02:00
Ms::Rest* rest = mu::engraving::Factory::createRest(Ms::gpaletteScore->dummy()->segment(), duration.type());
rest->setTicks(duration.fraction());
symNotehead = rest->getSymbol(inputState.duration().type(), 0, staff->lines(position.segment->tick()), &yo);
m_shadowNote->setState(symNotehead, duration, true, segmentSkylineTopY, segmentSkylineBottomY);
2021-09-08 13:13:20 +02:00
delete rest;
2020-06-16 10:33:18 +02:00
} else {
2021-12-08 14:22:23 +01:00
if (Ms::NoteHeadGroup::HEAD_CUSTOM == noteheadGroup) {
symNotehead = instr->drumset()->noteHeads(inputState.drumNote(), noteHead);
2020-06-16 10:33:18 +02:00
} else {
symNotehead = Note::noteHead(0, noteheadGroup, noteHead);
}
m_shadowNote->setState(symNotehead, duration, false, segmentSkylineTopY, segmentSkylineBottomY);
2020-06-16 10:33:18 +02:00
}
m_shadowNote->layout();
m_shadowNote->setPos(position.pos);
2020-06-16 10:33:18 +02:00
}
void NotationInteraction::hideShadowNote()
{
m_shadowNote->setVisible(false);
}
2021-04-12 14:07:49 +02:00
void NotationInteraction::toggleVisible()
{
startEdit();
// TODO: Update `score()->cmdToggleVisible()` and call that here?
2021-09-02 15:26:45 +02:00
for (EngravingItem* el : selection()->elements()) {
2021-04-12 14:07:49 +02:00
if (el->isBracket()) {
continue;
}
el->undoChangeProperty(Ms::Pid::VISIBLE, !el->visible());
}
apply();
notifyAboutNotationChanged();
}
2021-09-02 15:26:45 +02:00
EngravingItem* NotationInteraction::hitElement(const PointF& pos, float width) const
2020-06-16 10:33:18 +02:00
{
2021-09-02 15:26:45 +02:00
QList<Ms::EngravingItem*> elements = hitElements(pos, width);
if (elements.isEmpty()) {
2020-06-16 10:33:18 +02:00
return nullptr;
}
m_selection->onElementHit(elements.first());
return elements.first();
2020-06-16 10:33:18 +02:00
}
Staff* NotationInteraction::hitStaff(const PointF& pos) const
{
return hitMeasure(pos).staff;
}
2021-06-07 19:25:41 +02:00
Ms::Page* NotationInteraction::point2page(const PointF& p) const
2020-06-16 10:48:13 +02:00
{
if (score()->linearMode()) {
2020-06-16 10:48:13 +02:00
return score()->pages().isEmpty() ? 0 : score()->pages().front();
}
foreach (Ms::Page* page, score()->pages()) {
if (page->bbox().translated(page->pos()).contains(p)) {
return page;
}
}
return nullptr;
}
2021-09-02 15:26:45 +02:00
QList<EngravingItem*> NotationInteraction::elementsAt(const PointF& p) const
{
2021-09-02 15:26:45 +02:00
QList<EngravingItem*> el;
2020-12-15 14:56:47 +01:00
Ms::Page* page = point2page(p);
if (page) {
el = page->items(p - page->pos());
std::sort(el.begin(), el.end(), NotationInteraction::elementIsLess);
}
return el;
}
2021-09-02 15:26:45 +02:00
EngravingItem* NotationInteraction::elementAt(const PointF& p) const
{
2021-09-02 15:26:45 +02:00
QList<EngravingItem*> el = elementsAt(p);
EngravingItem* e = el.value(0);
if (e && e->isPage()) {
e = el.value(1);
}
return e;
}
2021-09-02 15:26:45 +02:00
QList<Ms::EngravingItem*> NotationInteraction::hitElements(const PointF& p_in, float w) const
2020-06-16 10:48:13 +02:00
{
Ms::Page* page = point2page(p_in);
if (!page) {
2021-09-02 15:26:45 +02:00
return QList<Ms::EngravingItem*>();
2020-06-16 10:48:13 +02:00
}
2021-09-02 15:26:45 +02:00
QList<Ms::EngravingItem*> ll;
2020-06-16 10:48:13 +02:00
2021-06-07 19:25:41 +02:00
PointF p = p_in - page->pos();
2020-06-16 10:48:13 +02:00
if (isTextEditingStarted()) {
auto editW = w * 2;
RectF hitRect(p.x() - editW, p.y() - editW, 2.0 * editW, 2.0 * editW);
if (m_editData.element->intersects(hitRect)) {
ll.push_back(m_editData.element);
return ll;
}
}
2021-06-07 19:25:41 +02:00
RectF r(p.x() - w, p.y() - w, 3.0 * w, 3.0 * w);
2020-06-16 10:48:13 +02:00
2021-09-02 15:26:45 +02:00
QList<Ms::EngravingItem*> elements = page->items(r);
2020-06-16 10:48:13 +02:00
//! TODO
// for (int i = 0; i < MAX_HEADERS; i++)
// if (score()->headerText(i) != nullptr) // gives the ability to select the header
// el.push_back(score()->headerText(i));
// for (int i = 0; i < MAX_FOOTERS; i++)
// if (score()->footerText(i) != nullptr) // gives the ability to select the footer
// el.push_back(score()->footerText(i));
//! -------
2021-09-02 15:26:45 +02:00
for (Ms::EngravingItem* element : elements) {
2021-02-24 15:48:08 +01:00
element->itemDiscovered = 0;
if (!element->selectable() || element->isPage()) {
2020-06-16 10:48:13 +02:00
continue;
}
2021-02-24 15:48:08 +01:00
if (!element->isInteractionAvailable()) {
continue;
}
2021-02-24 15:48:08 +01:00
if (element->contains(p)) {
ll.append(element);
2020-06-16 10:48:13 +02:00
}
}
int n = ll.size();
if ((n == 0) || ((n == 1) && (ll[0]->isMeasure()))) {
//
// if no relevant element hit, look nearby
//
2021-09-02 15:26:45 +02:00
for (Ms::EngravingItem* element : elements) {
2021-02-24 15:48:08 +01:00
if (element->isPage() || !element->selectable()) {
2020-06-16 10:48:13 +02:00
continue;
}
2021-02-24 15:48:08 +01:00
if (!element->isInteractionAvailable()) {
continue;
}
2021-02-24 15:48:08 +01:00
if (element->intersects(r)) {
ll.append(element);
2020-06-16 10:48:13 +02:00
}
}
}
if (!ll.empty()) {
std::sort(ll.begin(), ll.end(), NotationInteraction::elementIsLess);
} else {
Ms::Measure* measure = hitMeasure(p_in).measure;
if (measure) {
ll << measure;
}
2020-06-16 10:48:13 +02:00
}
return ll;
}
2021-06-07 19:25:41 +02:00
NotationInteraction::HitMeasureData NotationInteraction::hitMeasure(const PointF& pos) const
{
int staffIndex = -1;
Ms::Segment* segment = nullptr;
2021-06-07 19:25:41 +02:00
PointF offset;
Measure* measure = score()->pos2measure(pos, &staffIndex, 0, &segment, &offset);
HitMeasureData result;
if (measure && measure->staffLines(staffIndex)->canvasBoundingRect().contains(pos)) {
result.measure = measure;
result.staff = score()->staff(staffIndex);
}
return result;
}
2021-09-02 15:26:45 +02:00
bool NotationInteraction::elementIsLess(const Ms::EngravingItem* e1, const Ms::EngravingItem* e2)
2020-06-16 10:48:13 +02:00
{
if (!e1->selectable()) {
return false;
}
if (!e2->selectable()) {
return true;
}
if (e1->isNote() && e2->isStem()) {
return true;
}
if (e2->isNote() && e1->isStem()) {
return false;
}
if (e1->z() == e2->z()) {
// same stacking order, prefer non-hidden elements
if (e1->type() == e2->type()) {
if (e1->type() == Ms::ElementType::NOTEDOT) {
const Ms::NoteDot* n1 = static_cast<const Ms::NoteDot*>(e1);
const Ms::NoteDot* n2 = static_cast<const Ms::NoteDot*>(e2);
if (n1->note() && n1->note()->hidden()) {
return false;
} else if (n2->note() && n2->note()->hidden()) {
return true;
}
} else if (e1->type() == Ms::ElementType::NOTE) {
const Ms::Note* n1 = static_cast<const Ms::Note*>(e1);
const Ms::Note* n2 = static_cast<const Ms::Note*>(e2);
if (n1->hidden()) {
return false;
} else if (n2->hidden()) {
return true;
}
}
}
// different types, or same type but nothing hidden - use track
return e1->track() < e2->track();
2020-06-16 10:48:13 +02:00
}
// default case, use stacking order
return e1->z() < e2->z();
2020-06-16 10:48:13 +02:00
}
const NotationInteraction::HitElementContext& NotationInteraction::hitElementContext() const
{
return m_hitElementContext;
}
void NotationInteraction::setHitElementContext(const HitElementContext& context)
{
m_hitElementContext = context;
}
2021-04-17 00:50:46 +02:00
void NotationInteraction::moveChordNoteSelection(MoveDirection d)
{
IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
return;
}
2021-09-02 15:26:45 +02:00
EngravingItem* current = selection()->element();
2021-04-17 00:50:46 +02:00
if (!current || !(current->isNote() || current->isRest())) {
return;
}
2021-09-02 15:26:45 +02:00
EngravingItem* chordElem;
2021-04-17 00:50:46 +02:00
if (d == MoveDirection::Up) {
chordElem = score()->upAlt(current);
} else {
chordElem = score()->downAlt(current);
}
if (chordElem == current) {
return;
}
2021-09-09 11:32:14 +02:00
select({ chordElem }, SelectType::SINGLE, chordElem->staffIdx());
2021-04-17 00:50:46 +02:00
}
void NotationInteraction::moveSegmentSelection(MoveDirection d)
{
IF_ASSERT_FAILED(MoveDirection::Left == d || MoveDirection::Right == d) {
return;
}
EngravingItem* e = selection()->element();
if (!e && !selection()->elements().empty()) {
e = d == MoveDirection::Left ? selection()->elements().front() : selection()->elements().back();
}
if (!e || (e = d == MoveDirection::Left ? e->prevSegmentElement() : e->nextSegmentElement()) == nullptr) {
e = d == MoveDirection::Left ? score()->firstElement() : score()->lastElement();
}
select({ e }, SelectType::SINGLE);
}
void NotationInteraction::selectTopOrBottomOfChord(MoveDirection d)
{
IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
return;
}
2021-09-02 15:26:45 +02:00
EngravingItem* current = selection()->element();
if (!current || !current->isNote()) {
return;
}
2021-09-02 15:26:45 +02:00
EngravingItem* target = d == MoveDirection::Up
? score()->upAltCtrl(toNote(current)) : score()->downAltCtrl(toNote(current));
if (target == current) {
return;
}
2021-09-09 11:32:14 +02:00
select({ target }, SelectType::SINGLE);
}
2021-09-02 15:26:45 +02:00
void NotationInteraction::doSelect(const std::vector<EngravingItem*>& elements, SelectType type, int staffIndex)
2020-06-16 10:33:18 +02:00
{
TRACEFUNC;
if (needEndTextEditing(elements)) {
2020-11-02 20:23:13 +01:00
endEditText();
} else if (isElementEditStarted() && (elements.size() != 1 || elements.front() != score()->selection().element())) {
endEditElement();
}
if (elements.size() == 1 && type == SelectType::ADD && QGuiApplication::keyboardModifiers() == Qt::KeyboardModifier::ControlModifier) {
if (score()->selection().isRange()) {
score()->selection().setState(Ms::SelState::LIST);
score()->setUpdateAll();
}
if (elements.front()->selected()) {
score()->deselect(elements.front());
return;
}
2020-11-02 20:23:13 +01:00
}
2021-01-28 12:27:36 +01:00
if (type == SelectType::REPLACE) {
score()->deselectAll();
type = SelectType::ADD;
}
2021-09-02 15:26:45 +02:00
for (EngravingItem* element: elements) {
2020-11-02 20:23:13 +01:00
score()->select(element, type, staffIndex);
}
}
2020-11-02 20:23:13 +01:00
2021-09-02 15:26:45 +02:00
void NotationInteraction::select(const std::vector<EngravingItem*>& elements, SelectType type, int staffIndex)
{
TRACEFUNC;
const Ms::Selection& selection = score()->selection();
QList<EngravingItem*> oldSelectedElements = selection.elements();
doSelect(elements, type, staffIndex);
if (oldSelectedElements != selection.elements()) {
notifyAboutSelectionChanged();
}
2020-11-02 20:23:13 +01:00
}
2020-11-06 09:58:13 +01:00
void NotationInteraction::selectAll()
{
if (isTextEditingStarted()) {
auto textBase = toTextBase(m_editData.element);
textBase->selectAll(textBase->cursorFromEditData(m_editData));
} else {
score()->cmdSelectAll();
}
2020-11-06 09:58:13 +01:00
notifyAboutSelectionChanged();
2020-11-06 09:58:13 +01:00
}
2021-03-04 16:25:10 +01:00
void NotationInteraction::selectSection()
{
score()->cmdSelectSection();
notifyAboutSelectionChanged();
}
2021-08-25 18:27:07 +02:00
void NotationInteraction::selectFirstElement(bool frame)
2021-04-17 00:51:20 +02:00
{
if (EngravingItem* element = score()->firstElement(frame)) {
select({ element }, SelectType::SINGLE, element->staffIdx());
}
2021-04-17 00:51:20 +02:00
}
void NotationInteraction::selectLastElement()
{
if (EngravingItem* element = score()->lastElement()) {
select({ element }, SelectType::SINGLE, element->staffIdx());
}
2021-04-17 00:51:20 +02:00
}
INotationSelectionPtr NotationInteraction::selection() const
2020-06-16 10:33:18 +02:00
{
return m_selection;
}
void NotationInteraction::clearSelection()
{
TRACEFUNC;
if (isElementEditStarted()) {
endEditElement();
} else if (m_editData.element) {
m_editData.element = nullptr;
}
if (isDragStarted()) {
doEndDrag();
2021-12-20 09:35:09 +01:00
rollback();
notifyAboutNotationChanged();
}
if (selection()->isNone()) {
return;
}
score()->deselectAll();
notifyAboutSelectionChanged();
setHitElementContext(HitElementContext());
}
2020-06-16 10:33:18 +02:00
mu::async::Notification NotationInteraction::selectionChanged() const
{
return m_selectionChanged;
}
2021-08-10 19:13:29 +02:00
bool NotationInteraction::isSelectionTypeFiltered(SelectionFilterType type) const
{
return score()->selectionFilter().isFiltered(type);
}
void NotationInteraction::setSelectionTypeFiltered(SelectionFilterType type, bool filtered)
{
score()->selectionFilter().setFiltered(type, filtered);
if (selection()->isRange()) {
score()->selection().updateSelectedElements();
notifyAboutSelectionChanged();
}
}
2020-06-16 10:33:18 +02:00
bool NotationInteraction::isDragStarted() const
{
2021-09-03 13:58:21 +02:00
if (m_dragData.dragGroups.size() > 0) {
return true;
}
2021-09-06 15:30:03 +02:00
if (m_lasso && !m_lasso->isEmpty()) {
2021-09-03 13:58:21 +02:00
return true;
}
return false;
2020-06-16 10:33:18 +02:00
}
void NotationInteraction::DragData::reset()
{
beginMove = QPointF();
elementOffset = QPointF();
ed = Ms::EditData(ed.view());
2020-06-16 10:33:18 +02:00
dragGroups.clear();
}
2021-09-02 15:26:45 +02:00
void NotationInteraction::startDrag(const std::vector<EngravingItem*>& elems,
2021-06-07 19:25:41 +02:00
const PointF& eoffset,
2020-06-16 10:33:18 +02:00
const IsDraggable& isDraggable)
{
m_dragData.reset();
2020-06-16 10:48:13 +02:00
m_dragData.elements = elems;
2020-06-16 10:33:18 +02:00
m_dragData.elementOffset = eoffset;
m_editData.modifiers = QGuiApplication::keyboardModifiers();
2020-06-16 10:33:18 +02:00
2021-09-02 15:26:45 +02:00
for (EngravingItem* e : m_dragData.elements) {
2020-06-16 10:33:18 +02:00
if (!isDraggable(e)) {
continue;
}
std::unique_ptr<Ms::ElementGroup> g = e->getDragGroup(isDraggable);
if (g && g->enabled()) {
m_dragData.dragGroups.push_back(std::move(g));
}
}
2021-02-01 18:48:18 +01:00
startEdit();
qreal zoom = configuration()->currentZoom().val / 100.f;
qreal proximity = configuration()->selectionProximity() * 0.5f / zoom;
m_scoreCallbacks.setScore(score());
m_scoreCallbacks.setSelectionProximity(proximity);
2021-01-28 12:27:36 +01:00
if (isGripEditStarted()) {
m_editData.element->startEditDrag(m_editData);
2021-01-28 12:27:36 +01:00
return;
}
m_dragData.ed.modifiers = QGuiApplication::keyboardModifiers();
2021-01-28 12:27:36 +01:00
for (auto& group : m_dragData.dragGroups) {
group->startDrag(m_dragData.ed);
2020-06-16 10:33:18 +02:00
}
}
2021-06-07 19:25:41 +02:00
void NotationInteraction::doDragLasso(const PointF& pt)
2021-05-25 12:37:51 +02:00
{
2021-08-30 11:17:45 +02:00
if (!m_lasso) {
m_lasso = new Ms::Lasso(score());
}
2021-05-25 12:37:51 +02:00
score()->addRefresh(m_lasso->canvasBoundingRect());
2021-06-07 19:25:41 +02:00
RectF r;
2021-05-25 12:37:51 +02:00
r.setCoords(m_dragData.beginMove.x(), m_dragData.beginMove.y(), pt.x(), pt.y());
m_lasso->setbbox(r.normalized());
score()->addRefresh(m_lasso->canvasBoundingRect());
score()->lassoSelect(m_lasso->bbox());
score()->update();
}
void NotationInteraction::endLasso()
{
2021-08-30 11:17:45 +02:00
if (!m_lasso) {
return;
}
2021-05-25 12:37:51 +02:00
score()->addRefresh(m_lasso->canvasBoundingRect());
2021-06-07 19:25:41 +02:00
m_lasso->setbbox(RectF());
2021-05-25 23:00:30 +02:00
score()->lassoSelectEnd(m_dragData.mode != DragMode::LassoList);
2021-05-25 12:37:51 +02:00
score()->update();
}
2021-06-07 19:25:41 +02:00
void NotationInteraction::drag(const PointF& fromPos, const PointF& toPos, DragMode mode)
2020-06-16 10:33:18 +02:00
{
if (m_dragData.beginMove.isNull()) {
m_dragData.beginMove = fromPos;
m_dragData.ed.pos = fromPos;
2020-06-16 10:33:18 +02:00
}
2021-05-25 23:00:30 +02:00
m_dragData.mode = mode;
2020-06-16 10:33:18 +02:00
2021-06-07 19:25:41 +02:00
PointF normalizedBegin = m_dragData.beginMove - m_dragData.elementOffset;
2020-06-16 10:33:18 +02:00
2021-06-07 19:25:41 +02:00
PointF delta = toPos - normalizedBegin;
PointF evtDelta = toPos - m_dragData.ed.pos;
2020-06-16 10:33:18 +02:00
switch (mode) {
case DragMode::BothXY:
2021-05-25 23:00:30 +02:00
case DragMode::LassoList:
2020-06-16 10:33:18 +02:00
break;
case DragMode::OnlyX:
delta.setY(m_dragData.ed.delta.y());
2020-06-16 10:33:18 +02:00
evtDelta.setY(0.0);
break;
case DragMode::OnlyY:
delta.setX(m_dragData.ed.delta.x());
2020-06-16 10:33:18 +02:00
evtDelta.setX(0.0);
break;
}
m_dragData.ed.lastPos = m_dragData.ed.pos;
2021-12-14 14:34:19 +01:00
m_dragData.ed.hRaster = configuration()->isSnappedToGrid(framework::Orientation::Horizontal);
m_dragData.ed.vRaster = configuration()->isSnappedToGrid(framework::Orientation::Vertical);
m_dragData.ed.delta = delta;
m_dragData.ed.moveDelta = delta - m_dragData.elementOffset;
m_dragData.ed.evtDelta = evtDelta;
m_dragData.ed.pos = toPos;
m_dragData.ed.modifiers = QGuiApplication::keyboardModifiers();
2020-06-16 10:33:18 +02:00
if (isTextEditingStarted()) {
m_editData.pos = toPos;
toTextBase(m_editData.element)->dragTo(m_editData);
notifyAboutTextEditingChanged();
return;
}
2021-01-28 12:27:36 +01:00
if (isGripEditStarted()) {
m_dragData.ed.curGrip = m_editData.curGrip;
2021-01-28 12:27:36 +01:00
m_dragData.ed.delta = m_dragData.ed.pos - m_dragData.ed.lastPos;
m_dragData.ed.moveDelta = m_dragData.ed.delta - m_dragData.elementOffset;
m_dragData.ed.addData(m_editData.getData(m_editData.element));
m_editData.element->editDrag(m_dragData.ed);
} else if (isElementEditStarted()) {
m_dragData.ed.delta = evtDelta;
m_editData.element->editDrag(m_dragData.ed);
2021-01-28 12:27:36 +01:00
} else {
for (auto& group : m_dragData.dragGroups) {
score()->addRefresh(group->drag(m_dragData.ed));
}
2020-06-16 10:33:18 +02:00
}
score()->update();
if (isGripEditStarted()) {
updateAnchorLines();
} else {
std::vector<LineF> anchorLines;
for (const EngravingItem* e : selection()->elements()) {
QVector<LineF> elAnchorLines = e->dragAnchorLines();
if (!elAnchorLines.isEmpty()) {
for (LineF& l : elAnchorLines) {
anchorLines.push_back(l);
}
2020-06-16 10:48:13 +02:00
}
}
setAnchorLines(anchorLines);
2020-06-16 10:48:13 +02:00
}
2021-05-25 12:37:51 +02:00
if (m_dragData.elements.size() == 0) {
doDragLasso(toPos);
}
notifyAboutDragChanged();
2020-06-16 10:33:18 +02:00
}
void NotationInteraction::doEndDrag()
2020-06-16 10:33:18 +02:00
{
2021-01-28 12:27:36 +01:00
if (isGripEditStarted()) {
m_editData.element->endEditDrag(m_editData);
m_editData.element->endEdit(m_editData);
2021-01-28 12:27:36 +01:00
} else {
for (auto& group : m_dragData.dragGroups) {
group->endDrag(m_dragData.ed);
}
2021-09-06 15:30:03 +02:00
if (m_lasso && !m_lasso->isEmpty()) {
2021-05-25 12:37:51 +02:00
endLasso();
}
2020-06-16 10:33:18 +02:00
}
m_dragData.reset();
resetAnchorLines();
}
void NotationInteraction::endDrag()
{
doEndDrag();
apply();
notifyAboutDragChanged();
// updateGrips();
2021-09-02 15:26:45 +02:00
// if (editData.element->normalModeEditBehavior() == EngravingItem::EditBehavior::Edit
// && score()->selection().element() == editData.element) {
// startEdit(/* editMode */ false);
// }
2020-06-16 10:33:18 +02:00
}
mu::async::Notification NotationInteraction::dragChanged() const
2020-06-16 10:33:18 +02:00
{
return m_dragChanged;
}
//! NOTE Copied from ScoreView::dragEnterEvent
void NotationInteraction::startDrop(const QByteArray& edata)
{
resetDropElement();
2020-12-15 14:56:47 +01:00
Ms::XmlReader e(edata);
m_dropData.ed.dragOffset = QPointF();
Fraction duration; // dummy
2021-09-02 15:26:45 +02:00
ElementType type = EngravingItem::readType(e, &m_dropData.ed.dragOffset, &duration);
2021-09-06 18:17:43 +02:00
EngravingItem* el = engraving::Factory::createItem(type, score()->dummy());
if (el) {
if (type == ElementType::BAR_LINE || type == ElementType::ARPEGGIO || type == ElementType::BRACKET) {
double spatium = score()->spatium();
el->setHeight(spatium * 5);
}
m_dropData.ed.dropElement = el;
m_dropData.ed.dropElement->setParent(0);
m_dropData.ed.dropElement->read(e);
m_dropData.ed.dropElement->layout();
}
}
bool NotationInteraction::startDrop(const QUrl& url)
{
if (url.scheme() != "file") {
return false;
}
auto image = static_cast<Ms::Image*>(Factory::createItem(Ms::ElementType::IMAGE, score()->dummy()));
if (!image->load(url.toLocalFile())) {
return false;
}
resetDropElement();
m_dropData.ed.dropElement = image;
m_dropData.ed.dragOffset = QPointF();
m_dropData.ed.dropElement->setParent(nullptr);
m_dropData.ed.dropElement->layout();
return true;
}
//! NOTE Copied from ScoreView::dragMoveEvent
2021-06-07 19:25:41 +02:00
bool NotationInteraction::isDropAccepted(const PointF& pos, Qt::KeyboardModifiers modifiers)
{
if (!m_dropData.ed.dropElement) {
return false;
}
m_dropData.ed.pos = pos;
m_dropData.ed.modifiers = modifiers;
switch (m_dropData.ed.dropElement->type()) {
case ElementType::VOLTA:
return dragMeasureAnchorElement(pos);
case ElementType::PEDAL:
case ElementType::LET_RING:
2022-01-25 18:52:19 +01:00
case ElementType::TEMPO_RANGED_CHANGE:
case ElementType::VIBRATO:
case ElementType::PALM_MUTE:
case ElementType::OTTAVA:
case ElementType::TRILL:
case ElementType::HAIRPIN:
case ElementType::TEXTLINE:
return dragTimeAnchorElement(pos);
case ElementType::IMAGE:
case ElementType::SYMBOL:
case ElementType::FSYMBOL:
case ElementType::DYNAMIC:
case ElementType::KEYSIG:
case ElementType::CLEF:
case ElementType::TIMESIG:
case ElementType::BAR_LINE:
case ElementType::ARPEGGIO:
case ElementType::BREATH:
case ElementType::GLISSANDO:
case ElementType::MEASURE_NUMBER:
case ElementType::BRACKET:
case ElementType::ARTICULATION:
case ElementType::FERMATA:
case ElementType::CHORDLINE:
case ElementType::BEND:
case ElementType::ACCIDENTAL:
case ElementType::TEXT:
case ElementType::FINGERING:
case ElementType::TEMPO_TEXT:
case ElementType::STAFF_TEXT:
case ElementType::SYSTEM_TEXT:
case ElementType::PLAYTECH_ANNOTATION:
case ElementType::NOTEHEAD:
case ElementType::TREMOLO:
case ElementType::LAYOUT_BREAK:
case ElementType::MARKER:
case ElementType::STAFF_STATE:
case ElementType::INSTRUMENT_CHANGE:
case ElementType::REHEARSAL_MARK:
case ElementType::JUMP:
case ElementType::MEASURE_REPEAT:
case ElementType::ACTION_ICON:
case ElementType::CHORD:
case ElementType::SPACER:
case ElementType::SLUR:
case ElementType::HARMONY:
case ElementType::BAGPIPE_EMBELLISHMENT:
case ElementType::AMBITUS:
case ElementType::TREMOLOBAR:
case ElementType::FIGURED_BASS:
case ElementType::LYRICS:
case ElementType::FRET_DIAGRAM:
case ElementType::STAFFTYPE_CHANGE: {
2021-09-02 15:26:45 +02:00
EngravingItem* e = dropTarget(m_dropData.ed);
if (e) {
if (!e->isMeasure()) {
setDropTarget(e);
}
return true;
} else {
return false;
}
}
break;
default:
break;
}
return false;
}
//! NOTE Copied from ScoreView::dropEvent
2021-06-07 19:25:41 +02:00
bool NotationInteraction::drop(const PointF& pos, Qt::KeyboardModifiers modifiers)
{
if (!m_dropData.ed.dropElement) {
return false;
}
IF_ASSERT_FAILED(m_dropData.ed.dropElement->score() == score()) {
return false;
}
bool accepted = false;
bool needNotifyAboutSelectionChanged = false;
m_dropData.ed.pos = pos;
m_dropData.ed.modifiers = modifiers;
m_dropData.ed.dropElement->styleChanged();
bool systemStavesOnly = false;
bool applyUserOffset = false;
startEdit();
score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
ElementType et = m_dropData.ed.dropElement->type();
switch (et) {
case ElementType::TEXTLINE:
systemStavesOnly = m_dropData.ed.dropElement->systemFlag();
// fall-thru
case ElementType::VOLTA:
// voltas drop to system staves by default, or closest staff if Control is held
systemStavesOnly = systemStavesOnly || !(m_dropData.ed.modifiers & Qt::ControlModifier);
// fall-thru
case ElementType::OTTAVA:
case ElementType::TRILL:
case ElementType::PEDAL:
case ElementType::LET_RING:
2022-01-25 18:52:19 +01:00
case ElementType::TEMPO_RANGED_CHANGE:
case ElementType::VIBRATO:
case ElementType::PALM_MUTE:
case ElementType::HAIRPIN:
{
2020-12-15 14:56:47 +01:00
Ms::Spanner* spanner = ptr::checked_cast<Ms::Spanner>(m_dropData.ed.dropElement);
score()->cmdAddSpanner(spanner, pos, systemStavesOnly);
score()->setUpdateAll();
accepted = true;
}
break;
case ElementType::SYMBOL:
case ElementType::FSYMBOL:
case ElementType::IMAGE:
applyUserOffset = true;
// fall-thru
case ElementType::DYNAMIC:
case ElementType::FRET_DIAGRAM:
case ElementType::HARMONY:
{
2021-09-02 15:26:45 +02:00
EngravingItem* el = elementAt(pos);
if (el == 0 || el->type() == ElementType::STAFF_LINES) {
int staffIdx;
2020-12-15 14:56:47 +01:00
Ms::Segment* seg;
2021-06-07 19:25:41 +02:00
PointF offset;
el = score()->pos2measure(pos, &staffIdx, 0, &seg, &offset);
if (el && el->isMeasure()) {
m_dropData.ed.dropElement->setTrack(staffIdx * Ms::VOICES);
m_dropData.ed.dropElement->setParent(seg);
if (applyUserOffset) {
m_dropData.ed.dropElement->setOffset(offset);
}
score()->undoAddElement(m_dropData.ed.dropElement);
} else {
qDebug("cannot drop here");
resetDropElement();
}
} else {
score()->addRefresh(el->canvasBoundingRect());
score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
if (!el->acceptDrop(m_dropData.ed)) {
qDebug("drop %s onto %s not accepted", m_dropData.ed.dropElement->name(), el->name());
break;
}
2021-09-02 15:26:45 +02:00
EngravingItem* dropElement = el->drop(m_dropData.ed);
score()->addRefresh(el->canvasBoundingRect());
if (dropElement) {
doSelect({ dropElement }, SelectType::SINGLE);
needNotifyAboutSelectionChanged = true;
score()->addRefresh(dropElement->canvasBoundingRect());
}
}
}
accepted = true;
break;
case ElementType::HBOX:
case ElementType::VBOX:
case ElementType::KEYSIG:
case ElementType::CLEF:
case ElementType::TIMESIG:
case ElementType::BAR_LINE:
case ElementType::ARPEGGIO:
case ElementType::BREATH:
case ElementType::GLISSANDO:
case ElementType::MEASURE_NUMBER:
case ElementType::BRACKET:
case ElementType::ARTICULATION:
case ElementType::FERMATA:
case ElementType::CHORDLINE:
case ElementType::BEND:
case ElementType::ACCIDENTAL:
case ElementType::TEXT:
case ElementType::FINGERING:
case ElementType::TEMPO_TEXT:
case ElementType::STAFF_TEXT:
case ElementType::SYSTEM_TEXT:
case ElementType::PLAYTECH_ANNOTATION:
case ElementType::NOTEHEAD:
case ElementType::TREMOLO:
case ElementType::LAYOUT_BREAK:
case ElementType::MARKER:
case ElementType::STAFF_STATE:
case ElementType::INSTRUMENT_CHANGE:
case ElementType::REHEARSAL_MARK:
case ElementType::JUMP:
case ElementType::MEASURE_REPEAT:
case ElementType::ACTION_ICON:
case ElementType::NOTE:
case ElementType::CHORD:
case ElementType::SPACER:
case ElementType::SLUR:
case ElementType::BAGPIPE_EMBELLISHMENT:
case ElementType::AMBITUS:
case ElementType::TREMOLOBAR:
case ElementType::FIGURED_BASS:
case ElementType::LYRICS:
case ElementType::STAFFTYPE_CHANGE: {
2021-09-02 15:26:45 +02:00
EngravingItem* el = dropTarget(m_dropData.ed);
if (!el) {
if (!dropCanvas(m_dropData.ed.dropElement)) {
qDebug("cannot drop %s(%p) to canvas", m_dropData.ed.dropElement->name(), m_dropData.ed.dropElement);
resetDropElement();
}
break;
}
score()->addRefresh(el->canvasBoundingRect());
// TODO: HACK ALERT!
if (el->isMeasure() && m_dropData.ed.dropElement->isLayoutBreak()) {
Measure* m = toMeasure(el);
if (m->isMMRest()) {
el = m->mmRestLast();
}
}
2021-09-02 15:26:45 +02:00
EngravingItem* dropElement = el->drop(m_dropData.ed);
if (dropElement && dropElement->isInstrumentChange()) {
2020-12-04 14:21:54 +01:00
selectInstrument(toInstrumentChange(dropElement));
}
score()->addRefresh(el->canvasBoundingRect());
if (dropElement) {
if (!score()->noteEntryMode()) {
doSelect({ dropElement }, SelectType::SINGLE);
needNotifyAboutSelectionChanged = true;
}
score()->addRefresh(dropElement->canvasBoundingRect());
}
accepted = true;
}
break;
default:
resetDropElement();
break;
}
m_dropData.ed.dropElement = nullptr;
setDropTarget(nullptr); // this also resets dropRectangle and dropAnchor
apply();
// update input cursor position (must be done after layout)
// if (noteEntryMode()) {
// moveCursor();
// }
if (accepted) {
notifyAboutDropChanged();
}
if (needNotifyAboutSelectionChanged) {
notifyAboutSelectionChanged();
}
return accepted;
}
void NotationInteraction::selectInstrument(Ms::InstrumentChange* instrumentChange)
2020-12-04 14:21:54 +01:00
{
if (!instrumentChange) {
return;
}
2021-08-09 17:38:58 +02:00
RetVal<Instrument> selectedInstrument = selectInstrumentScenario()->selectInstrument();
if (!selectedInstrument.ret) {
2020-12-04 14:21:54 +01:00
return;
}
instrumentChange->setInit(true);
instrumentChange->setupInstrument(&selectedInstrument.val);
2020-12-04 14:21:54 +01:00
}
2020-07-09 14:31:34 +02:00
//! NOTE Copied from Palette::applyPaletteElement
2021-09-02 15:26:45 +02:00
bool NotationInteraction::applyPaletteElement(Ms::EngravingItem* element, Qt::KeyboardModifiers modifiers)
2020-07-09 14:31:34 +02:00
{
IF_ASSERT_FAILED(element) {
return false;
}
2020-12-15 14:56:47 +01:00
Ms::Score* score = this->score();
2020-07-09 14:31:34 +02:00
if (!score) {
return false;
}
2020-12-15 14:56:47 +01:00
const Ms::Selection sel = score->selection(); // make a copy of selection state before applying the operation.
2020-07-09 14:31:34 +02:00
if (sel.isNone()) {
return false;
}
//#ifdef MSCORE_UNSTABLE
// if (ScriptRecorder* rec = adapter()->getScriptRecorder()) {
// if (modifiers == 0) {
// rec->recordPaletteElement(element);
// }
// }
//#endif
startEdit();
2020-07-09 14:31:34 +02:00
if (sel.isList()) {
ChordRest* cr1 = sel.firstChordRest();
ChordRest* cr2 = sel.lastChordRest();
bool addSingle = false; // add a single line only
if (cr1 && cr2 == cr1) {
// one chordrest selected, ok to add line
addSingle = true;
} else if (sel.elements().size() == 2 && cr1 && cr2 && cr1 != cr2) {
// two chordrests selected
// must be on same staff in order to add line, except for slur
if (element->isSlur() || cr1->staffIdx() == cr2->staffIdx()) {
addSingle = true;
}
}
auto isEntryDrumStaff = [score]() {
2020-12-23 10:26:43 +01:00
const Ms::InputState& is = score->inputState();
Ms::Staff* staff = score->staff(is.track() / Ms::VOICES);
2020-12-23 10:26:43 +01:00
return staff->staffType(is.tick())->group() == Ms::StaffGroup::PERCUSSION;
};
2020-07-09 14:31:34 +02:00
if (isEntryDrumStaff() && element->isChord()) {
2020-12-15 14:56:47 +01:00
Ms::InputState& is = score->inputState();
2021-09-02 15:26:45 +02:00
EngravingItem* e = nullptr;
if (!(modifiers & Qt::ShiftModifier)) {
// shift+double-click: add note to "chord"
// use input position rather than selection if possible
// look for a cr in the voice predefined for the drum in the palette
// back up if necessary
// TODO: refactor this with similar code in putNote()
if (is.segment()) {
2020-12-15 14:56:47 +01:00
Ms::Segment* seg = is.segment();
while (seg) {
if (seg->element(is.track())) {
break;
}
2020-12-15 14:56:47 +01:00
seg = seg->prev(Ms::SegmentType::ChordRest);
}
if (seg) {
is.setSegment(seg);
} else {
2020-12-15 14:56:47 +01:00
is.setSegment(is.segment()->measure()->first(Ms::SegmentType::ChordRest));
}
}
score->expandVoice();
e = is.cr();
}
2020-07-09 14:31:34 +02:00
if (!e) {
e = sel.elements().first();
}
if (e) {
// get note if selection was full chord
if (e->isChord()) {
e = toChord(e)->upNote();
}
2021-06-07 19:25:41 +02:00
applyDropPaletteElement(score, e, element, modifiers, PointF(), true);
// note has already been played (and what would play otherwise may be *next* input position)
score->setPlayNote(false);
score->setPlayChord(false);
2020-07-09 14:31:34 +02:00
// continue in same track
is.setTrack(e->track());
2020-07-09 14:31:34 +02:00
} else {
qDebug("nowhere to place drum note");
}
} else if (element->isLayoutBreak()) {
2020-12-15 14:56:47 +01:00
Ms::LayoutBreak* breakElement = toLayoutBreak(element);
2020-07-09 14:31:34 +02:00
score->cmdToggleLayoutBreak(breakElement->layoutBreakType());
} else if (element->isSlur() && addSingle) {
2020-11-25 19:03:54 +01:00
doAddSlur(toSlur(element));
2020-07-09 14:31:34 +02:00
} else if (element->isSLine() && !element->isGlissando() && addSingle) {
2020-12-15 14:56:47 +01:00
Ms::Segment* startSegment = cr1->segment();
Ms::Segment* endSegment = cr2->segment();
if (element->type() == Ms::ElementType::PEDAL && cr2 != cr1) {
2020-07-09 14:31:34 +02:00
endSegment = endSegment->nextCR(cr2->track());
}
// TODO - handle cross-voice selections
int idx = cr1->staffIdx();
2021-06-07 19:25:41 +02:00
QByteArray a = element->mimeData(PointF());
2020-07-09 14:31:34 +02:00
//printf("<<%s>>\n", a.data());
2020-12-15 14:56:47 +01:00
Ms::XmlReader e(a);
Ms::Fraction duration; // dummy
2021-06-07 19:25:41 +02:00
PointF dragOffset;
2021-09-02 15:26:45 +02:00
Ms::ElementType type = Ms::EngravingItem::readType(e, &dragOffset, &duration);
2021-09-06 18:17:43 +02:00
Ms::Spanner* spanner = static_cast<Ms::Spanner*>(engraving::Factory::createItem(type, score->dummy()));
2020-07-09 14:31:34 +02:00
spanner->read(e);
spanner->styleChanged();
score->cmdAddSpanner(spanner, idx, startSegment, endSegment);
} else {
2021-09-02 15:26:45 +02:00
for (EngravingItem* e : sel.elements()) {
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, e, element, modifiers);
2020-07-09 14:31:34 +02:00
}
}
} else if (sel.isRange()) {
if (element->type() == ElementType::BAR_LINE
|| element->type() == ElementType::MARKER
|| element->type() == ElementType::JUMP
|| element->type() == ElementType::SPACER
|| element->type() == ElementType::VBOX
|| element->type() == ElementType::HBOX
|| element->type() == ElementType::TBOX
|| element->type() == ElementType::MEASURE
|| element->type() == ElementType::BRACKET
|| element->type() == ElementType::STAFFTYPE_CHANGE
|| (element->type() == ElementType::ACTION_ICON
&& (toActionIcon(element)->actionType() == Ms::ActionIconType::VFRAME
|| toActionIcon(element)->actionType() == Ms::ActionIconType::HFRAME
|| toActionIcon(element)->actionType() == Ms::ActionIconType::TFRAME
|| toActionIcon(element)->actionType() == Ms::ActionIconType::MEASURE
|| toActionIcon(element)->actionType() == Ms::ActionIconType::BRACKETS))) {
2020-07-09 14:31:34 +02:00
Measure* last = sel.endSegment() ? sel.endSegment()->measure() : nullptr;
for (Measure* m = sel.startSegment()->measure(); m; m = m->nextMeasureMM()) {
2021-06-07 19:25:41 +02:00
RectF r = m->staffabbox(sel.staffStart());
PointF pt(r.x() + r.width() * .5, r.y() + r.height() * .5);
2020-07-09 14:31:34 +02:00
pt += m->system()->page()->pos();
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, m, element, modifiers, pt);
2020-07-09 14:31:34 +02:00
if (m == last) {
break;
}
}
} else if (element->type() == ElementType::LAYOUT_BREAK) {
2020-12-15 14:56:47 +01:00
Ms::LayoutBreak* breakElement = static_cast<Ms::LayoutBreak*>(element);
2020-07-09 14:31:34 +02:00
score->cmdToggleLayoutBreak(breakElement->layoutBreakType());
} else if (element->isClef() || element->isKeySig() || element->isTimeSig()) {
Measure* m1 = sel.startSegment()->measure();
Measure* m2 = sel.endSegment() ? sel.endSegment()->measure() : nullptr;
if (m2 == m1 && sel.startSegment()->rtick().isZero()) {
m2 = nullptr; // don't restore original if one full measure selected
} else if (m2) {
m2 = m2->nextMeasureMM();
}
// for clefs, apply to each staff separately
// otherwise just apply to top staff
int staffIdx1 = sel.staffStart();
int staffIdx2 = element->type() == ElementType::CLEF ? sel.staffEnd() : staffIdx1 + 1;
for (int i = staffIdx1; i < staffIdx2; ++i) {
// for clefs, use mid-measure changes if appropriate
2021-09-02 15:26:45 +02:00
EngravingItem* e1 = nullptr;
EngravingItem* e2 = nullptr;
2020-07-09 14:31:34 +02:00
// use mid-measure clef changes as appropriate
if (element->type() == ElementType::CLEF) {
if (sel.startSegment()->isChordRestType() && sel.startSegment()->rtick().isNotZero()) {
ChordRest* cr = static_cast<ChordRest*>(sel.startSegment()->nextChordRest(i * Ms::VOICES));
2020-07-09 14:31:34 +02:00
if (cr && cr->isChord()) {
2020-07-29 13:20:36 +02:00
e1 = static_cast<Ms::Chord*>(cr)->upNote();
2020-07-09 14:31:34 +02:00
} else {
e1 = cr;
}
}
2020-12-15 14:56:47 +01:00
if (sel.endSegment() && sel.endSegment()->segmentType() == Ms::SegmentType::ChordRest) {
ChordRest* cr = static_cast<ChordRest*>(sel.endSegment()->nextChordRest(i * Ms::VOICES));
2020-07-09 14:31:34 +02:00
if (cr && cr->isChord()) {
2020-07-29 13:20:36 +02:00
e2 = static_cast<Ms::Chord*>(cr)->upNote();
2020-07-09 14:31:34 +02:00
} else {
e2 = cr;
}
}
}
if (m2 || e2) {
// restore original clef/keysig/timesig
2020-12-15 14:56:47 +01:00
Ms::Staff* staff = score->staff(i);
Ms::Fraction tick1 = sel.startSegment()->tick();
2021-09-02 15:26:45 +02:00
Ms::EngravingItem* oelement = nullptr;
2020-07-09 14:31:34 +02:00
switch (element->type()) {
2020-12-15 14:56:47 +01:00
case Ms::ElementType::CLEF:
2020-07-09 14:31:34 +02:00
{
2021-09-06 18:17:43 +02:00
Ms::Clef* oclef = engraving::Factory::createClef(score->dummy()->segment());
2020-07-09 14:31:34 +02:00
oclef->setClefType(staff->clef(tick1));
oelement = oclef;
break;
}
2020-12-15 14:56:47 +01:00
case Ms::ElementType::KEYSIG:
2020-07-09 14:31:34 +02:00
{
2021-09-06 18:17:43 +02:00
Ms::KeySig* okeysig = engraving::Factory::createKeySig(score->dummy()->segment());
2020-07-09 14:31:34 +02:00
okeysig->setKeySigEvent(staff->keySigEvent(tick1));
2020-12-15 14:56:47 +01:00
if (!score->styleB(Ms::Sid::concertPitch) && !okeysig->isCustom() && !okeysig->isAtonal()) {
2020-07-09 14:31:34 +02:00
Ms::Interval v = staff->part()->instrument(tick1)->transpose();
if (!v.isZero()) {
Key k = okeysig->key();
okeysig->setKey(transposeKey(k, v, okeysig->part()->preferSharpFlat()));
}
}
oelement = okeysig;
break;
}
2020-12-15 14:56:47 +01:00
case Ms::ElementType::TIMESIG:
2020-07-09 14:31:34 +02:00
{
2021-09-08 10:05:53 +02:00
Ms::TimeSig* otimesig = engraving::Factory::createTimeSig(score->dummy()->segment());
2020-07-09 14:31:34 +02:00
otimesig->setFrom(staff->timeSig(tick1));
oelement = otimesig;
break;
}
default:
break;
}
if (oelement) {
if (e2) {
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, e2, oelement, modifiers);
2020-07-09 14:31:34 +02:00
} else {
2021-06-07 19:25:41 +02:00
RectF r = m2->staffabbox(i);
PointF pt(r.x() + r.width() * .5, r.y() + r.height() * .5);
2020-07-09 14:31:34 +02:00
pt += m2->system()->page()->pos();
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, m2, oelement, modifiers, pt);
2020-07-09 14:31:34 +02:00
}
delete oelement;
}
}
// apply new clef/keysig/timesig
if (e1) {
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, e1, element, modifiers);
2020-07-09 14:31:34 +02:00
} else {
2021-06-07 19:25:41 +02:00
RectF r = m1->staffabbox(i);
PointF pt(r.x() + r.width() * .5, r.y() + r.height() * .5);
2020-07-09 14:31:34 +02:00
pt += m1->system()->page()->pos();
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, m1, element, modifiers, pt);
2020-07-09 14:31:34 +02:00
}
}
} else if (element->isSlur()) {
2020-11-25 19:03:54 +01:00
doAddSlur(toSlur(element));
2020-07-09 14:31:34 +02:00
} else if (element->isSLine() && element->type() != ElementType::GLISSANDO) {
2020-12-15 14:56:47 +01:00
Ms::Segment* startSegment = sel.startSegment();
Ms::Segment* endSegment = sel.endSegment();
2020-07-09 14:31:34 +02:00
bool firstStaffOnly = element->isVolta() && !(modifiers & Qt::ControlModifier);
int startStaff = firstStaffOnly ? 0 : sel.staffStart();
int endStaff = firstStaffOnly ? 1 : sel.staffEnd();
for (int i = startStaff; i < endStaff; ++i) {
2020-12-15 14:56:47 +01:00
Ms::Spanner* spanner = static_cast<Ms::Spanner*>(element->clone());
2020-07-09 14:31:34 +02:00
spanner->setScore(score);
spanner->styleChanged();
score->cmdAddSpanner(spanner, i, startSegment, endSegment);
}
} else if (element->isTextBase()) {
Ms::Segment* firstSegment = sel.startSegment();
int firstStaffIndex = sel.staffStart();
int lastStaffIndex = sel.staffEnd();
// A text should only be added at the start of the selection
2021-10-27 17:06:57 +02:00
// There shouldn't be a text at each element
for (int staff = firstStaffIndex; staff < lastStaffIndex; staff++) {
applyDropPaletteElement(score, firstSegment->firstElement(staff), element, modifiers);
}
2020-07-09 14:31:34 +02:00
} else {
int track1 = sel.staffStart() * Ms::VOICES;
int track2 = sel.staffEnd() * Ms::VOICES;
2020-12-15 14:56:47 +01:00
Ms::Segment* startSegment = sel.startSegment();
Ms::Segment* endSegment = sel.endSegment(); //keep it, it could change during the loop
2020-07-09 14:31:34 +02:00
2020-12-15 14:56:47 +01:00
for (Ms::Segment* s = startSegment; s && s != endSegment; s = s->next1()) {
2020-07-09 14:31:34 +02:00
for (int track = track1; track < track2; ++track) {
2021-09-02 15:26:45 +02:00
Ms::EngravingItem* e = s->element(track);
2020-07-09 14:31:34 +02:00
if (e == 0 || !score->selectionFilter().canSelect(e)
|| !score->selectionFilter().canSelectVoice(track)) {
continue;
}
if (e->isChord()) {
2020-07-29 13:20:36 +02:00
Ms::Chord* chord = toChord(e);
2020-12-15 14:56:47 +01:00
for (Ms::Note* n : chord->notes()) {
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, n, element, modifiers);
2020-07-09 14:31:34 +02:00
if (!(element->isAccidental() || element->isNoteHead())) { // only these need to apply to every note
break;
}
}
} else {
// do not apply articulation to barline in a range selection
if (!e->isBarLine() || !element->isArticulation()) {
2020-07-11 11:00:29 +02:00
applyDropPaletteElement(score, e, element, modifiers);
2020-07-09 14:31:34 +02:00
}
}
}
if (!element->placeMultiple()) {
break;
}
}
}
} else {
qDebug("unknown selection state");
}
apply();
2020-10-23 19:30:10 +02:00
2020-07-09 14:31:34 +02:00
setDropTarget(nullptr);
return true;
}
//! NOTE Copied from Palette applyDrop
2021-09-02 15:26:45 +02:00
void NotationInteraction::applyDropPaletteElement(Ms::Score* score, Ms::EngravingItem* target, Ms::EngravingItem* e,
2020-07-09 14:31:34 +02:00
Qt::KeyboardModifiers modifiers,
2021-06-07 19:25:41 +02:00
PointF pt, bool pasteMode)
2020-07-09 14:31:34 +02:00
{
Ms::EditData newData(&m_scoreCallbacks);
Ms::EditData* dropData = &newData;
2020-07-09 14:31:34 +02:00
if (isTextEditingStarted()) {
dropData = &m_editData;
}
dropData->pos = pt.isNull() ? target->pagePos() : pt;
dropData->dragOffset = QPointF();
dropData->modifiers = modifiers;
dropData->dropElement = e;
if (target->acceptDrop(*dropData)) {
2020-07-09 14:31:34 +02:00
// use same code path as drag&drop
2021-06-07 19:25:41 +02:00
QByteArray a = e->mimeData(PointF());
2020-07-09 14:31:34 +02:00
2020-12-15 14:56:47 +01:00
Ms::XmlReader n(a);
2020-07-09 14:31:34 +02:00
n.setPasteMode(pasteMode);
Fraction duration; // dummy
2021-06-07 19:25:41 +02:00
PointF dragOffset;
2021-09-02 15:26:45 +02:00
ElementType type = EngravingItem::readType(n, &dragOffset, &duration);
2021-09-06 18:17:43 +02:00
dropData->dropElement = engraving::Factory::createItem(type, score->dummy());
2020-07-09 14:31:34 +02:00
dropData->dropElement->read(n);
dropData->dropElement->styleChanged(); // update to local style
2020-07-09 14:31:34 +02:00
2021-09-02 15:26:45 +02:00
Ms::EngravingItem* el = target->drop(*dropData);
2020-07-09 14:31:34 +02:00
if (el && el->isInstrumentChange()) {
2020-12-04 14:21:54 +01:00
selectInstrument(toInstrumentChange(el));
2020-07-09 14:31:34 +02:00
}
if (el && !score->inputState().noteEntryMode()) {
doSelect({ el }, Ms::SelectType::SINGLE, 0);
2020-07-09 14:31:34 +02:00
}
dropData->dropElement = nullptr;
2020-07-09 14:31:34 +02:00
m_notifyAboutDropChanged = true;
2020-07-09 14:31:34 +02:00
}
}
//! NOTE Copied from ScoreView::cmdAddSlur
2020-11-25 19:03:54 +01:00
void NotationInteraction::doAddSlur(const Ms::Slur* slurTemplate)
{
startEdit();
m_notifyAboutDropChanged = true;
2020-07-09 14:31:34 +02:00
Ms::ChordRest* firstChordRest = nullptr;
Ms::ChordRest* secondChordRest = nullptr;
2020-07-09 14:31:34 +02:00
const auto& sel = score()->selection();
auto el = sel.uniqueElements();
2020-07-09 14:31:34 +02:00
if (sel.isRange()) {
int startTrack = sel.staffStart() * Ms::VOICES;
int endTrack = sel.staffEnd() * Ms::VOICES;
2020-07-09 14:31:34 +02:00
for (int track = startTrack; track < endTrack; ++track) {
firstChordRest = nullptr;
secondChordRest = nullptr;
2021-09-02 15:26:45 +02:00
for (Ms::EngravingItem* e : el) {
2020-07-09 14:31:34 +02:00
if (e->track() != track) {
continue;
}
if (e->isNote()) {
e = toNote(e)->chord();
}
if (!e->isChord()) {
continue;
}
2020-12-15 14:56:47 +01:00
Ms::ChordRest* cr = Ms::toChordRest(e);
if (!firstChordRest || firstChordRest->tick() > cr->tick()) {
firstChordRest = cr;
2020-07-09 14:31:34 +02:00
}
if (!secondChordRest || secondChordRest->tick() < cr->tick()) {
secondChordRest = cr;
2020-07-09 14:31:34 +02:00
}
}
2020-11-25 19:03:54 +01:00
if (firstChordRest && (firstChordRest != secondChordRest)) {
doAddSlur(firstChordRest, secondChordRest, slurTemplate);
2020-07-09 14:31:34 +02:00
}
}
} else if (sel.isSingle()) {
if (sel.element()->isNote()) {
doAddSlur(toNote(sel.element())->chord(), nullptr, slurTemplate);
}
2020-07-09 14:31:34 +02:00
} else {
2021-09-02 15:26:45 +02:00
for (Ms::EngravingItem* e : el) {
2020-07-09 14:31:34 +02:00
if (e->isNote()) {
2020-12-15 14:56:47 +01:00
e = Ms::toNote(e)->chord();
2020-07-09 14:31:34 +02:00
}
if (!e->isChord()) {
continue;
}
2020-12-15 14:56:47 +01:00
Ms::ChordRest* cr = Ms::toChordRest(e);
if (!firstChordRest || cr->isBefore(firstChordRest)) {
firstChordRest = cr;
2020-07-09 14:31:34 +02:00
}
if (!secondChordRest || secondChordRest->isBefore(cr)) {
secondChordRest = cr;
2020-07-09 14:31:34 +02:00
}
}
if (firstChordRest == secondChordRest) {
secondChordRest = Ms::nextChordRest(firstChordRest);
2020-07-09 14:31:34 +02:00
}
if (firstChordRest) {
doAddSlur(firstChordRest, secondChordRest, slurTemplate);
2020-07-09 14:31:34 +02:00
}
}
apply();
2020-07-09 14:31:34 +02:00
}
void NotationInteraction::doAddSlur(ChordRest* firstChordRest, ChordRest* secondChordRest, const Ms::Slur* slurTemplate)
{
Ms::Slur* slur = firstChordRest->slur(secondChordRest);
if (!slur || slur->slurDirection() != Ms::DirectionV::AUTO) {
slur = score()->addSlur(firstChordRest, secondChordRest, slurTemplate);
}
if (m_noteInput->isNoteInputMode()) {
m_noteInput->addSlur(slur);
} else if (!secondChordRest) {
select({ slur->frontSegment() }, SelectType::SINGLE);
updateGripEdit();
}
}
2020-12-08 19:37:10 +01:00
bool NotationInteraction::scoreHasMeasure() const
{
2020-12-15 14:56:47 +01:00
Ms::Page* page = score()->pages().isEmpty() ? nullptr : score()->pages().front();
const QList<Ms::System*>* systems = page ? &page->systems() : nullptr;
2020-12-08 19:37:10 +01:00
if (systems == nullptr || systems->empty() || systems->front()->measures().empty()) {
return false;
}
return true;
}
2020-12-14 16:07:33 +01:00
bool NotationInteraction::notesHaveActiculation(const std::vector<Note*>& notes, SymbolId articulationSymbolId) const
{
for (Note* note: notes) {
Chord* chord = note->chord();
std::set<SymbolId> chordArticulations = chord->articulationSymbolIds();
2021-11-19 17:41:54 +01:00
chordArticulations = Ms::flipArticulations(chordArticulations, Ms::PlacementV::ABOVE);
2020-12-14 16:07:33 +01:00
chordArticulations = Ms::splitArticulations(chordArticulations);
if (chordArticulations.find(articulationSymbolId) == chordArticulations.end()) {
return false;
}
}
return true;
}
//! NOTE Copied from ScoreView::dragLeaveEvent
void NotationInteraction::endDrop()
{
if (m_dropData.ed.dropElement) {
score()->setUpdateAll();
resetDropElement();
score()->update();
}
setDropTarget(nullptr);
}
2020-07-10 10:46:56 +02:00
mu::async::Notification NotationInteraction::dropChanged() const
{
return m_dropChanged;
}
//! NOTE Copied from ScoreView::dropCanvas
2021-09-02 15:26:45 +02:00
bool NotationInteraction::dropCanvas(EngravingItem* e)
{
if (e->isActionIcon()) {
switch (Ms::toActionIcon(e)->actionType()) {
case Ms::ActionIconType::VFRAME:
score()->insertMeasure(ElementType::VBOX);
break;
case Ms::ActionIconType::HFRAME:
score()->insertMeasure(ElementType::HBOX);
break;
case Ms::ActionIconType::TFRAME:
score()->insertMeasure(ElementType::TBOX);
break;
case Ms::ActionIconType::FFRAME:
score()->insertMeasure(ElementType::FBOX);
break;
case Ms::ActionIconType::MEASURE:
score()->insertMeasure(ElementType::MEASURE);
break;
default:
return false;
}
delete e;
return true;
}
return false;
}
//! NOTE Copied from ScoreView::getDropTarget
2021-09-02 15:26:45 +02:00
EngravingItem* NotationInteraction::dropTarget(Ms::EditData& ed) const
{
2021-09-02 15:26:45 +02:00
QList<EngravingItem*> el = elementsAt(ed.pos);
for (EngravingItem* e : el) {
if (e->isStaffLines()) {
if (el.size() > 2) { // is not first class drop target
continue;
}
2020-12-15 14:56:47 +01:00
e = Ms::toStaffLines(e)->measure();
}
if (e->acceptDrop(ed)) {
return e;
}
}
return nullptr;
}
//! NOTE Copied from ScoreView::dragMeasureAnchorElement
2021-06-07 19:25:41 +02:00
bool NotationInteraction::dragMeasureAnchorElement(const PointF& pos)
{
int staffIdx;
2020-12-15 14:56:47 +01:00
Ms::Segment* seg;
Ms::MeasureBase* mb = score()->pos2measure(pos, &staffIdx, 0, &seg, 0);
if (!(m_dropData.ed.modifiers & Qt::ControlModifier)) {
staffIdx = 0;
}
int track = staffIdx * Ms::VOICES;
if (mb && mb->isMeasure()) {
2020-12-15 14:56:47 +01:00
Ms::Measure* m = Ms::toMeasure(mb);
Ms::System* s = m->system();
qreal y = s->staff(staffIdx)->y() + s->pos().y() + s->page()->pos().y();
2021-06-07 19:25:41 +02:00
RectF b(m->canvasBoundingRect());
if (pos.x() >= (b.x() + b.width() * .5) && m != score()->lastMeasureMM()
&& m->nextMeasure()->system() == m->system()) {
m = m->nextMeasure();
}
2021-06-07 19:25:41 +02:00
PointF anchor(m->canvasBoundingRect().x(), y);
setAnchorLines({ LineF(pos, anchor) });
m_dropData.ed.dropElement->score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
m_dropData.ed.dropElement->setTrack(track);
m_dropData.ed.dropElement->score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
m_notifyAboutDropChanged = true;
return true;
}
m_dropData.ed.dropElement->score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
setDropTarget(nullptr);
return false;
}
//! NOTE Copied from ScoreView::dragTimeAnchorElement
2021-06-07 19:25:41 +02:00
bool NotationInteraction::dragTimeAnchorElement(const PointF& pos)
{
int staffIdx;
2020-12-15 14:56:47 +01:00
Ms::Segment* seg;
Ms::MeasureBase* mb = score()->pos2measure(pos, &staffIdx, 0, &seg, 0);
int track = staffIdx * Ms::VOICES;
if (mb && mb->isMeasure() && seg->element(track)) {
2020-12-15 14:56:47 +01:00
Ms::Measure* m = Ms::toMeasure(mb);
Ms::System* s = m->system();
qreal y = s->staff(staffIdx)->y() + s->pos().y() + s->page()->pos().y();
2021-06-07 19:25:41 +02:00
PointF anchor(seg->canvasBoundingRect().x(), y);
setAnchorLines({ LineF(pos, anchor) });
m_dropData.ed.dropElement->score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
m_dropData.ed.dropElement->setTrack(track);
m_dropData.ed.dropElement->score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
notifyAboutDragChanged();
return true;
}
m_dropData.ed.dropElement->score()->addRefresh(m_dropData.ed.dropElement->canvasBoundingRect());
setDropTarget(nullptr);
return false;
}
2020-07-09 14:31:34 +02:00
//! NOTE Copied from ScoreView::setDropTarget
2021-09-02 15:26:45 +02:00
void NotationInteraction::setDropTarget(EngravingItem* el)
{
if (m_dropData.dropTarget != el) {
if (m_dropData.dropTarget) {
m_dropData.dropTarget->setDropTarget(false);
m_dropData.dropTarget = nullptr;
}
m_dropData.dropTarget = el;
if (m_dropData.dropTarget) {
m_dropData.dropTarget->setDropTarget(true);
}
}
m_anchorLines.clear();
//! TODO
// if (dropRectangle.isValid()) {
// dropRectangle = QRectF();
// }
//! ---
notifyAboutDragChanged();
}
void NotationInteraction::resetDropElement()
{
if (m_dropData.ed.dropElement) {
delete m_dropData.ed.dropElement;
m_dropData.ed.dropElement = nullptr;
}
}
2021-06-07 19:25:41 +02:00
void NotationInteraction::setAnchorLines(const std::vector<LineF>& anchorList)
{
m_anchorLines = anchorList;
}
void NotationInteraction::resetAnchorLines()
2020-06-16 10:33:18 +02:00
{
m_anchorLines.clear();
2020-06-16 10:33:18 +02:00
}
double NotationInteraction::currentScaling(mu::draw::Painter* painter) const
{
qreal guiScaling = configuration()->guiScaling();
return painter->worldTransform().m11() / guiScaling;
}
void NotationInteraction::drawAnchorLines(mu::draw::Painter* painter)
2020-06-16 10:33:18 +02:00
{
if (m_anchorLines.empty()) {
2020-06-16 10:48:13 +02:00
return;
2020-06-16 10:33:18 +02:00
}
const auto dropAnchorColor = configuration()->anchorLineColor();
mu::draw::Pen pen(dropAnchorColor, 2.0 / currentScaling(painter), mu::draw::PenStyle::DotLine);
2020-06-16 10:33:18 +02:00
2021-06-07 19:25:41 +02:00
for (const LineF& anchor : m_anchorLines) {
2020-06-16 10:48:13 +02:00
painter->setPen(pen);
painter->drawLine(anchor);
2020-06-16 10:33:18 +02:00
qreal d = 4.0 / currentScaling(painter);
2021-06-07 19:25:41 +02:00
RectF rect(-d, -d, 2 * d, 2 * d);
2020-06-16 10:33:18 +02:00
2021-07-01 12:19:08 +02:00
painter->setBrush(mu::draw::Brush(dropAnchorColor));
2021-03-02 12:43:26 +01:00
painter->setNoPen();
2021-06-07 19:25:41 +02:00
rect.moveCenter(anchor.p1());
2020-06-16 10:48:13 +02:00
painter->drawEllipse(rect);
2021-06-07 19:25:41 +02:00
rect.moveCenter(anchor.p2());
2020-06-16 10:48:13 +02:00
painter->drawEllipse(rect);
2020-06-16 10:33:18 +02:00
}
}
void NotationInteraction::drawTextEditMode(draw::Painter* painter)
{
if (!isTextEditingStarted()) {
return;
}
m_editData.element->drawEditMode(painter, m_editData, currentScaling(painter));
}
void NotationInteraction::drawSelectionRange(draw::Painter* painter)
2020-11-10 08:03:27 +01:00
{
2021-06-29 23:42:51 +02:00
using namespace draw;
2020-11-10 08:03:27 +01:00
if (!m_selection->isRange()) {
return;
}
2021-07-01 12:19:08 +02:00
painter->setBrush(BrushStyle::NoBrush);
2020-11-10 08:03:27 +01:00
QColor selectionColor = configuration()->selectionColor();
qreal penWidth = 3.0 / currentScaling(painter);
2020-11-10 08:03:27 +01:00
2021-06-29 23:42:51 +02:00
Pen pen;
2020-11-10 08:03:27 +01:00
pen.setColor(selectionColor);
pen.setWidthF(penWidth);
pen.setStyle(PenStyle::SolidLine);
2020-11-10 08:03:27 +01:00
painter->setPen(pen);
2021-06-07 19:25:41 +02:00
std::vector<RectF> rangeArea = m_selection->range()->boundingArea();
for (const RectF& rect: rangeArea) {
2021-07-16 12:51:19 +02:00
mu::PainterPath path;
path.addRoundedRect(rect, 6, 6);
2020-11-10 08:03:27 +01:00
QColor fillColor = selectionColor;
fillColor.setAlpha(10);
painter->fillPath(path, fillColor);
painter->drawPath(path);
}
}
void NotationInteraction::drawGripPoints(draw::Painter* painter)
2021-01-28 12:27:36 +01:00
{
Ms::EngravingItem* selectedElement = selection()->element();
int gripsCount = selectedElement ? selectedElement->gripsCount() : 0;
if (gripsCount == 0) {
2021-01-28 12:27:36 +01:00
return;
}
bool editingElement = m_editData.element != nullptr;
if (!editingElement) {
m_editData.element = selectedElement;
if (!m_editData.element->isTextBase() && !m_editData.getData(m_editData.element)) {
m_editData.element->startEdit(m_editData);
}
}
m_editData.grips = gripsCount;
m_editData.grip.resize(m_editData.grips);
2021-01-28 12:27:36 +01:00
constexpr qreal DEFAULT_GRIP_SIZE = 8;
qreal scaling = currentScaling(painter);
qreal gripSize = DEFAULT_GRIP_SIZE / scaling;
RectF newRect(-gripSize / 2, -gripSize / 2, gripSize, gripSize);
2021-02-01 18:48:18 +01:00
const EngravingItem* page = m_editData.element->findAncestor(ElementType::PAGE);
PointF pageOffset = page ? page->pos() : m_editData.element->pos();
2021-02-01 18:48:18 +01:00
for (RectF& gripRect: m_editData.grip) {
2021-02-01 18:48:18 +01:00
gripRect = newRect.translated(pageOffset);
2021-01-28 12:27:36 +01:00
}
m_editData.element->updateGrips(m_editData);
m_editData.element->drawEditMode(painter, m_editData, currentScaling(painter));
if (!editingElement) {
m_editData.element = nullptr;
}
}
ChordRest* activeCr(Ms::Score* score)
{
ChordRest* cr = score->selection().activeCR();
if (!cr) {
cr = score->selection().lastChordRest();
if (!cr && score->noteEntryMode()) {
cr = score->inputState().cr();
}
}
return cr;
}
void NotationInteraction::expandSelection(ExpandSelectionMode mode)
{
ChordRest* cr = activeCr(score());
if (!cr) {
return;
}
ChordRest* el = 0;
switch (mode) {
case ExpandSelectionMode::BeginSystem: {
Measure* measure = cr->segment()->measure()->system()->firstMeasure();
if (measure) {
el = measure->first()->nextChordRest(cr->track());
}
break;
}
case ExpandSelectionMode::EndSystem: {
Measure* measure = cr->segment()->measure()->system()->lastMeasure();
if (measure) {
el = measure->last()->nextChordRest(cr->track(), true);
}
break;
}
case ExpandSelectionMode::BeginScore: {
Measure* measure = score()->firstMeasureMM();
if (measure) {
el = measure->first()->nextChordRest(cr->track());
}
break;
}
case ExpandSelectionMode::EndScore: {
Measure* measure = score()->lastMeasureMM();
if (measure) {
el = measure->last()->nextChordRest(cr->track(), true);
}
break;
}
}
if (el) {
score()->select(el, SelectType::RANGE, el->staffIdx());
notifyAboutSelectionChanged();
}
}
void NotationInteraction::addToSelection(MoveDirection d, MoveSelectionType type)
{
ChordRest* cr = activeCr(score());
if (!cr) {
return;
}
ChordRest* el = 0;
switch (type) {
case MoveSelectionType::Chord:
if (d == MoveDirection::Right) {
el = Ms::nextChordRest(cr, true);
} else {
el = Ms::prevChordRest(cr, true);
}
break;
case MoveSelectionType::Measure:
if (d == MoveDirection::Right) {
el = score()->nextMeasure(cr, true, true);
} else {
el = score()->prevMeasure(cr, true);
}
break;
case MoveSelectionType::Track:
if (d == MoveDirection::Up) {
el = score()->upStaff(cr);
} else {
el = score()->downStaff(cr);
}
2021-12-04 14:37:36 +01:00
case MoveSelectionType::EngravingItem:
case MoveSelectionType::Frame:
case MoveSelectionType::System:
2021-12-06 14:20:16 +01:00
case MoveSelectionType::String:
2021-12-04 14:37:36 +01:00
case MoveSelectionType::Undefined:
break;
}
if (el) {
score()->select(el, SelectType::RANGE, el->staffIdx());
notifyAboutSelectionChanged();
}
2021-01-28 12:27:36 +01:00
}
void NotationInteraction::moveSelection(MoveDirection d, MoveSelectionType type)
{
2021-12-06 13:49:48 +01:00
IF_ASSERT_FAILED(MoveSelectionType::Undefined != type) {
return;
}
2021-12-06 13:49:48 +01:00
if (type != MoveSelectionType::String) {
IF_ASSERT_FAILED(MoveDirection::Left == d || MoveDirection::Right == d) {
return;
}
} else {
IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
return;
}
}
2021-09-02 15:26:45 +02:00
if (MoveSelectionType::EngravingItem == type) {
moveElementSelection(d);
return;
}
2021-12-06 13:49:48 +01:00
if (MoveSelectionType::String == type) {
moveStringSelection(d);
return;
}
// TODO: rewrite, Score::move( ) logic needs to be here
auto typeToString = [](MoveSelectionType type) {
2020-12-23 10:26:43 +01:00
switch (type) {
case MoveSelectionType::Undefined: return QString();
2021-09-02 15:26:45 +02:00
case MoveSelectionType::EngravingItem: return QString();
2020-12-23 10:26:43 +01:00
case MoveSelectionType::Chord: return QString("chord");
case MoveSelectionType::Measure: return QString("measure");
case MoveSelectionType::Track: return QString("track");
case MoveSelectionType::Frame: return QString("frame");
case MoveSelectionType::System: return QString("system");
2021-12-06 13:49:48 +01:00
case MoveSelectionType::String: return QString();
2020-12-23 10:26:43 +01:00
}
return QString();
};
QString cmd;
if (MoveDirection::Left == d) {
cmd = "prev-";
} else if (MoveDirection::Right == d) {
cmd = "next-";
}
cmd += typeToString(type);
score()->move(cmd);
notifyAboutSelectionChanged();
}
void NotationInteraction::selectTopStaff()
{
EngravingItem* el = score()->cmdTopStaff(activeCr(score()));
if (score()->noteEntryMode()) {
score()->inputState().moveInputPos(el);
}
2022-01-11 10:24:25 +01:00
if (el->type() == ElementType::CHORD) {
el = Ms::toChord(el)->upNote();
}
select({ el }, SelectType::SINGLE, 0);
}
void NotationInteraction::selectEmptyTrailingMeasure()
{
ChordRest* cr = activeCr(score());
const Measure* ftm = score()->firstTrailingMeasure(cr ? &cr : nullptr);
if (!ftm) {
ftm = score()->lastMeasure();
}
if (ftm) {
if (score()->styleB(Ms::Sid::createMultiMeasureRests) && ftm->hasMMRest()) {
ftm = ftm->mmRest1();
}
EngravingItem* el
= !cr ? ftm->first()->nextChordRest(0, false) : ftm->first()->nextChordRest(Ms::trackZeroVoice(cr->track()), false);
score()->inputState().moveInputPos(el);
select({ el }, SelectType::SINGLE);
}
}
2021-09-02 15:26:45 +02:00
static ChordRest* asChordRest(EngravingItem* e)
{
if (e && e->isNote()) {
return toNote(e)->chord();
} else if (e && e->isChordRest()) {
return toChordRest(e);
}
return nullptr;
}
void NotationInteraction::moveChordRestToStaff(MoveDirection dir)
{
startEdit();
2021-09-02 15:26:45 +02:00
for (EngravingItem* e: score()->selection().uniqueElements()) {
ChordRest* cr = asChordRest(e);
if (cr != nullptr) {
if (dir == MoveDirection::Up) {
score()->moveUp(cr);
} else if (dir == MoveDirection::Down) {
score()->moveDown(cr);
}
}
}
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::swapChordRest(MoveDirection direction)
{
ChordRest* cr = asChordRest(score()->getSelectedElement());
if (!cr) {
return;
}
QList<ChordRest*> crl;
if (cr->links()) {
for (auto* l : *cr->links()) {
crl.append(toChordRest(l));
}
} else {
crl.append(cr);
}
startEdit();
for (ChordRest* cr1 : crl) {
if (cr1->type() == ElementType::REST) {
Measure* m = toRest(cr1)->measure();
if (m && m->isMMRest()) {
break;
}
}
ChordRest* cr2;
// ensures cr1 is the left chord, useful in SwapCR::flip()
if (direction == MoveDirection::Left) {
cr2 = cr1;
cr1 = prevChordRest(cr2);
} else {
cr2 = nextChordRest(cr1);
}
if (cr1 && cr2 && cr1->measure() == cr2->measure() && !cr1->tuplet() && !cr2->tuplet()
&& cr1->durationType() == cr2->durationType() && cr1->ticks() == cr2->ticks()
// if two chords belong to different two-note tremolos, abort
&& !(cr1->isChord() && toChord(cr1)->tremolo() && toChord(cr1)->tremolo()->twoNotes()
&& cr2->isChord() && toChord(cr2)->tremolo() && toChord(cr2)->tremolo()->twoNotes()
&& toChord(cr1)->tremolo() != toChord(cr2)->tremolo())) {
score()->undo(new Ms::SwapCR(cr1, cr2));
}
}
apply();
}
void NotationInteraction::moveElementSelection(MoveDirection d)
{
2021-09-02 15:26:45 +02:00
EngravingItem* el = score()->selection().element();
if (!el && !score()->selection().elements().isEmpty()) {
el = score()->selection().elements().last();
}
if (!el) {
ChordRest* cr = score()->selection().currentCR();
if (cr) {
if (cr->isChord()) {
if (MoveDirection::Left == d) {
el = toChord(cr)->upNote();
} else {
el = toChord(cr)->downNote();
}
} else if (cr->isRest()) {
el = cr;
}
score()->select(el);
}
}
2021-09-02 15:26:45 +02:00
EngravingItem* toEl = nullptr;
if (el) {
toEl = (MoveDirection::Left == d) ? score()->prevElement() : score()->nextElement();
} else {
toEl = (MoveDirection::Left == d) ? score()->lastElement() : score()->firstElement();
}
2021-09-09 11:32:14 +02:00
if (!toEl) {
return;
}
2021-09-09 11:32:14 +02:00
select({ toEl }, SelectType::SINGLE);
if (toEl->type() == ElementType::NOTE || toEl->type() == ElementType::HARMONY) {
score()->setPlayNote(true);
}
}
2021-12-06 13:49:48 +01:00
void NotationInteraction::moveStringSelection(MoveDirection d)
{
Ms::InputState& is = score()->inputState();
Ms::Staff* staff = score()->staff(is.track() / Ms::VOICES);
int instrStrgs = staff->part()->instrument(is.tick())->stringData()->strings();
2021-12-06 14:20:16 +01:00
int delta = (staff->staffType(is.tick())->upsideDown() ? -1 : 1);
2021-12-06 13:49:48 +01:00
if (MoveDirection::Up == d) {
delta = -delta;
}
int strg = is.string() + delta;
if (strg >= 0 && strg < instrStrgs) {
is.setString(strg);
notifyAboutSelectionChanged();
}
}
inline Ms::DirectionV toDirection(MoveDirection d)
{
return d == MoveDirection::Up ? Ms::DirectionV::UP : Ms::DirectionV::DOWN;
}
void NotationInteraction::movePitch(MoveDirection d, PitchMode mode)
{
IF_ASSERT_FAILED(MoveDirection::Up == d || MoveDirection::Down == d) {
return;
}
startEdit();
if (score()->selection().element() && score()->selection().element()->isRest()) {
score()->cmdMoveRest(toRest(score()->selection().element()), toDirection(d));
} else {
score()->upDown(MoveDirection::Up == d, mode);
}
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::moveLyrics(MoveDirection d)
{
EngravingItem* el = score()->selection().element();
IF_ASSERT_FAILED(el && el->isLyrics()) {
return;
}
startEdit();
score()->cmdMoveLyrics(toLyrics(el), toDirection(d));
apply();
}
void NotationInteraction::nudge(MoveDirection d, bool quickly)
{
2021-09-02 15:26:45 +02:00
EngravingItem* el = score()->selection().element();
IF_ASSERT_FAILED(el && (el->isTextBase() || el->isArticulation())) {
return;
}
startEdit();
2020-12-15 14:56:47 +01:00
qreal step = quickly ? Ms::MScore::nudgeStep10 : Ms::MScore::nudgeStep;
step = step * el->spatium();
switch (d) {
case MoveDirection::Undefined:
IF_ASSERT_FAILED(d != MoveDirection::Undefined) {
return;
}
2020-06-26 15:09:20 +02:00
break;
case MoveDirection::Left:
2021-06-07 19:25:41 +02:00
el->undoChangeProperty(Ms::Pid::OFFSET, el->offset() - PointF(step, 0.0), Ms::PropertyFlags::UNSTYLED);
break;
case MoveDirection::Right:
2021-06-07 19:25:41 +02:00
el->undoChangeProperty(Ms::Pid::OFFSET, el->offset() + PointF(step, 0.0), Ms::PropertyFlags::UNSTYLED);
break;
case MoveDirection::Up:
2021-06-07 19:25:41 +02:00
el->undoChangeProperty(Ms::Pid::OFFSET, el->offset() - PointF(0.0, step), Ms::PropertyFlags::UNSTYLED);
break;
case MoveDirection::Down:
2021-06-07 19:25:41 +02:00
el->undoChangeProperty(Ms::Pid::OFFSET, el->offset() + PointF(0.0, step), Ms::PropertyFlags::UNSTYLED);
break;
}
apply();
notifyAboutDragChanged();
}
2021-11-11 14:48:02 +01:00
bool NotationInteraction::isTextSelected() const
{
EngravingItem* selectedElement = m_selection->element();
if (!selectedElement) {
return false;
}
if (!selectedElement->isTextBase()) {
return false;
}
return true;
}
bool NotationInteraction::isTextEditingStarted() const
{
return m_editData.element && m_editData.element->isTextBase();
}
bool NotationInteraction::textEditingAllowed(const EngravingItem* element) const
{
return element && element->isEditable() && (element->isTextBase() || element->isTBox());
}
2021-09-02 15:26:45 +02:00
void NotationInteraction::startEditText(EngravingItem* element, const PointF& cursorPos)
{
if (!element) {
return;
}
m_editData.startMove = cursorPos;
if (isTextEditingStarted()) {
// double click on a textBase element that is being edited - select word
Ms::TextBase* textBase = Ms::toTextBase(m_editData.element);
textBase->multiClickSelect(m_editData, Ms::MultiClick::Double);
textBase->endHexState(m_editData);
textBase->setPrimed(false);
} else {
m_editData.clearData();
if (element->isTBox()) {
m_editData.element = toTBox(element)->text();
} else {
m_editData.element = element;
}
m_editData.element->startEdit(m_editData);
}
notifyAboutTextEditingStarted();
notifyAboutTextEditingChanged();
}
//! NOTE: Copied from TextBase::inputTransition
void NotationInteraction::editText(QInputMethodEvent* event)
{
if (!isTextEditingStarted()) {
return;
}
Ms::TextBase* text = Ms::toTextBase(m_editData.element);
Ms::TextCursor* cursor = text->cursor();
QString& preeditString = m_editData.preeditString;
// remove preedit string
int n = preeditString.size();
while (n--) {
if (cursor->movePosition(Ms::TextCursor::MoveOperation::Left)) {
Ms::TextBlock& curLine = cursor->curLine();
curLine.remove(cursor->column(), cursor);
text->triggerLayout();
text->setTextInvalid();
}
}
if (!event->commitString().isEmpty()) {
score()->startCmd();
text->insertText(m_editData, event->commitString());
score()->endCmd();
preeditString.clear();
} else {
preeditString = event->preeditString();
if (!preeditString.isEmpty()) {
cursor->updateCursorFormat();
text->editInsertText(cursor, preeditString);
text->setTextInvalid();
text->layout1();
score()->update();
}
}
event->accept();
notifyAboutTextEditingChanged();
}
static qreal nudgeDistance(const Ms::EditData& editData)
{
qreal spatium = editData.element->spatium();
if (editData.element->isBeam()) {
if (editData.modifiers & Qt::ControlModifier) {
return spatium;
} else if (editData.modifiers & Qt::AltModifier) {
return spatium * 4;
} else {
return spatium * 0.25;
}
} else {
if (editData.modifiers & Qt::ControlModifier) {
return spatium * Ms::MScore::nudgeStep10;
} else if (editData.modifiers & Qt::AltModifier) {
return spatium * Ms::MScore::nudgeStep50;
} else {
return spatium * Ms::MScore::nudgeStep;
}
}
}
static qreal nudgeDistance(const Ms::EditData& editData, qreal raster)
{
qreal distance = nudgeDistance(editData);
if (raster > 0) {
raster = editData.element->spatium() / raster;
if (distance < raster) {
distance = raster;
}
}
return distance;
}
bool NotationInteraction::handleKeyPress(QKeyEvent* event)
{
if (event->modifiers() == Qt::KeyboardModifier::AltModifier && !isElementEditStarted()) {
return false;
}
if (m_editData.element->isTextBase()) {
return false;
}
qreal vRaster = Ms::MScore::vRaster(), hRaster = Ms::MScore::hRaster();
switch (event->key()) {
case Qt::Key_Tab:
if (!m_editData.element->nextGrip(m_editData)) {
return false;
}
updateAnchorLines();
return true;
case Qt::Key_Backtab:
if (!m_editData.element->prevGrip(m_editData)) {
return false;
}
updateAnchorLines();
return true;
case Qt::Key_Left:
m_editData.delta = QPointF(-nudgeDistance(m_editData, hRaster), 0);
break;
case Qt::Key_Right:
m_editData.delta = QPointF(nudgeDistance(m_editData, hRaster), 0);
break;
case Qt::Key_Up:
m_editData.delta = QPointF(0, -nudgeDistance(m_editData, vRaster));
break;
case Qt::Key_Down:
m_editData.delta = QPointF(0, nudgeDistance(m_editData, vRaster));
break;
default:
return false;
}
m_editData.evtDelta = m_editData.moveDelta = m_editData.delta;
m_editData.hRaster = hRaster;
m_editData.vRaster = vRaster;
if (m_editData.curGrip == Ms::Grip::NO_GRIP) {
m_editData.curGrip = m_editData.element->defaultGrip();
}
if (m_editData.curGrip != Ms::Grip::NO_GRIP && int(m_editData.curGrip) < m_editData.grips) {
m_editData.pos = m_editData.grip[int(m_editData.curGrip)].center() + m_editData.delta;
}
m_editData.element->startEditDrag(m_editData);
m_editData.element->editDrag(m_editData);
m_editData.element->endEditDrag(m_editData);
return true;
}
void NotationInteraction::endEditText()
{
IF_ASSERT_FAILED(m_editData.element) {
return;
}
if (!isTextEditingStarted()) {
return;
}
doEndEditElement();
notifyAboutTextEditingChanged();
2021-08-07 13:08:21 +02:00
notifyAboutSelectionChanged();
}
2021-06-07 19:25:41 +02:00
void NotationInteraction::changeTextCursorPosition(const PointF& newCursorPos)
{
IF_ASSERT_FAILED(isTextEditingStarted() && m_editData.element) {
return;
}
m_editData.startMove = newCursorPos;
Ms::TextBase* textEl = Ms::toTextBase(m_editData.element);
textEl->mousePress(m_editData);
if (m_editData.buttons == Qt::MiddleButton) {
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
QClipboard::Mode mode = QClipboard::Clipboard;
#else
QClipboard::Mode mode = QClipboard::Selection;
#endif
QString txt = QGuiApplication::clipboard()->text(mode);
textEl->paste(m_editData, txt);
}
notifyAboutTextEditingChanged();
}
const TextBase* NotationInteraction::editedText() const
{
return Ms::toTextBase(m_editData.element);
}
void NotationInteraction::undo()
{
m_undoStack->undo(&m_editData);
2021-08-07 13:08:21 +02:00
notifyAboutSelectionChanged();
}
void NotationInteraction::redo()
{
m_undoStack->redo(&m_editData);
2021-08-07 13:08:21 +02:00
notifyAboutSelectionChanged();
}
mu::async::Notification NotationInteraction::textEditingStarted() const
{
return m_textEditingStarted;
}
mu::async::Notification NotationInteraction::textEditingChanged() const
{
return m_textEditingChanged;
}
2020-10-13 14:06:26 +02:00
mu::async::Channel<ScoreConfigType> NotationInteraction::scoreConfigChanged() const
{
return m_scoreConfigChanged;
}
2021-01-28 12:27:36 +01:00
bool NotationInteraction::isGripEditStarted() const
{
return m_editData.element && m_editData.curGrip != Ms::Grip::NO_GRIP;
2021-01-28 12:27:36 +01:00
}
static int findGrip(const QVector<mu::RectF>& grips, const mu::PointF& canvasPos)
2021-01-28 12:27:36 +01:00
{
if (grips.empty()) {
return -1;
2021-01-28 12:27:36 +01:00
}
qreal align = grips[0].width() / 2;
for (int i = 0; i < grips.size(); ++i) {
if (grips[i].adjusted(-align, -align, align, align).contains(canvasPos)) {
return i;
2021-01-28 12:27:36 +01:00
}
}
return -1;
}
2021-01-28 12:27:36 +01:00
bool NotationInteraction::isHitGrip(const PointF& pos) const
{
return selection()->element() && findGrip(m_editData.grip, pos) != -1;
2021-01-28 12:27:36 +01:00
}
2021-06-07 19:25:41 +02:00
void NotationInteraction::startEditGrip(const PointF& pos)
2021-01-28 12:27:36 +01:00
{
int grip = findGrip(m_editData.grip, pos);
if (grip == -1) {
2021-01-28 12:27:36 +01:00
return;
}
startEditGrip(Ms::Grip(grip));
}
2021-01-28 12:27:36 +01:00
void NotationInteraction::updateAnchorLines()
{
std::vector<LineF> lines;
Ms::Grip anchorLinesGrip = m_editData.curGrip == Ms::Grip::NO_GRIP ? m_editData.element->defaultGrip() : m_editData.curGrip;
QVector<LineF> anchorLines = m_editData.element->gripAnchorLines(anchorLinesGrip);
2021-01-28 12:27:36 +01:00
if (!anchorLines.isEmpty()) {
for (LineF& line : anchorLines) {
if (line.p1() != line.p2()) {
lines.push_back(line);
}
2021-01-28 12:27:36 +01:00
}
}
2021-01-28 12:27:36 +01:00
setAnchorLines(lines);
}
2021-01-28 12:27:36 +01:00
void NotationInteraction::startEditGrip(Ms::Grip grip)
{
if (m_editData.element == nullptr) {
m_editData.element = score()->selection().element();
}
m_editData.curGrip = Ms::Grip(grip);
updateAnchorLines();
m_editData.element->startEdit(m_editData);
notifyAboutNotationChanged();
2021-01-28 12:27:36 +01:00
}
bool NotationInteraction::isElementEditStarted() const
{
return m_editData.element != nullptr && (m_editData.grips == 0 || m_editData.curGrip != Ms::Grip::NO_GRIP);
}
void NotationInteraction::startEditElement(EngravingItem* element)
{
if (!element) {
return;
}
if (isElementEditStarted()) {
return;
}
if (element->isTextBase()) {
startEditText(element, PointF());
} else if (m_editData.grips > 1) {
m_editData.element = element;
startEditGrip(Ms::Grip::END);
if (m_editData.element->generated()) {
m_editData.element = nullptr;
}
} else if (element->isEditable()) {
element->startEdit(m_editData);
m_editData.element = element;
}
}
void NotationInteraction::changeEditElement(EngravingItem* newElement)
{
IF_ASSERT_FAILED(newElement) {
return;
}
if (m_editData.element == newElement) {
return;
}
doEndEditElement();
startEditElement(newElement);
}
void NotationInteraction::editElement(QKeyEvent* event)
{
m_editData.modifiers = event->modifiers();
if (isDragStarted()) {
return; // ignore all key strokes while dragging
}
bool wasEditing = m_editData.element != nullptr;
if (!wasEditing && selection()->element()) {
m_editData.element = selection()->element();
}
if (!m_editData.element) {
return;
}
m_editData.key = event->key();
m_editData.s = event->text();
startEdit();
int systemIndex = findSystemIndex(m_editData.element);
int bracketIndex = findBracketIndex(m_editData.element);
bool handled = m_editData.element->edit(m_editData) || (wasEditing && handleKeyPress(event));
if (!wasEditing) {
m_editData.element = nullptr;
}
if (handled) {
event->accept();
apply();
} else {
m_undoStack->rollbackChanges();
}
if (isTextEditingStarted()) {
notifyAboutTextEditingChanged();
} else {
if (bracketIndex >= 0 && systemIndex < score()->systems().size()) {
Ms::System* sys = score()->systems()[systemIndex];
Ms::EngravingItem* bracket = sys->brackets()[bracketIndex];
score()->select(bracket);
notifyAboutSelectionChanged();
}
}
}
void NotationInteraction::endEditElement()
2021-01-28 12:27:36 +01:00
{
if (!m_editData.element) {
2021-01-28 12:27:36 +01:00
return;
}
if (isTextEditingStarted()) {
endEditText();
return;
}
if (isDragStarted()) {
doEndDrag();
2021-12-20 09:35:09 +01:00
rollback();
}
if (m_editData.curGrip == Ms::Grip::NO_GRIP) {
doEndEditElement();
resetAnchorLines();
} else {
m_editData.curGrip = Ms::Grip::NO_GRIP;
updateAnchorLines();
}
notifyAboutNotationChanged();
}
2021-01-28 12:27:36 +01:00
void NotationInteraction::doEndEditElement()
{
if (m_editData.element) {
m_editData.element->endEdit(m_editData);
m_editData.element = nullptr;
}
m_editData.clearData();
}
2020-11-06 13:50:21 +01:00
void NotationInteraction::splitSelectedMeasure()
{
2021-09-02 15:26:45 +02:00
EngravingItem* selectedElement = m_selection->element();
2020-11-06 13:50:21 +01:00
if (!selectedElement) {
return;
}
if (!selectedElement->isNote() && !selectedElement->isRest()) {
return;
}
if (selectedElement->isNote()) {
selectedElement = dynamic_cast<Note*>(selectedElement)->chord();
}
ChordRest* chordRest = dynamic_cast<ChordRest*>(selectedElement);
startEdit();
2020-11-06 13:50:21 +01:00
score()->cmdSplitMeasure(chordRest);
apply();
2020-11-06 13:50:21 +01:00
}
void NotationInteraction::joinSelectedMeasures()
{
if (!m_selection->isRange()) {
return;
}
Measure* measureStart = score()->crMeasure(m_selection->range()->startMeasureIndex());
Measure* measureEnd = score()->crMeasure(m_selection->range()->endMeasureIndex());
2020-11-06 13:50:21 +01:00
startEdit();
2020-11-06 13:50:21 +01:00
score()->cmdJoinMeasure(measureStart, measureEnd);
apply();
2020-11-06 13:50:21 +01:00
}
mu::Ret NotationInteraction::canAddBoxes() const
{
if (selection()->isRange()) {
return make_ok();
}
static const std::vector<ElementType> boxesTypes {
ElementType::VBOX, ElementType::HBOX, ElementType::TBOX
};
for (const EngravingItem* element: selection()->elements()) {
if (Ms::toMeasure(element->findMeasure())) {
return make_ok();
}
if (std::find(boxesTypes.cbegin(), boxesTypes.cend(), element->type()) != boxesTypes.cend()) {
return make_ok();
}
}
return make_ret(Err::MeasureIsNotSelected);
}
2020-11-27 12:47:39 +01:00
void NotationInteraction::addBoxes(BoxType boxType, int count, int beforeBoxIndex)
2020-11-06 13:40:25 +01:00
{
2020-11-27 12:47:39 +01:00
auto boxTypeToElementType = [](BoxType boxType) {
2020-12-23 10:26:43 +01:00
switch (boxType) {
case BoxType::Horizontal: return Ms::ElementType::HBOX;
case BoxType::Vertical: return Ms::ElementType::VBOX;
case BoxType::Text: return Ms::ElementType::TBOX;
case BoxType::Measure: return Ms::ElementType::MEASURE;
case BoxType::Unknown: return Ms::ElementType::INVALID;
}
return ElementType::INVALID;
};
2020-11-06 13:40:25 +01:00
2020-12-15 14:56:47 +01:00
Ms::ElementType elementType = boxTypeToElementType(boxType);
Ms::MeasureBase* beforeBox = beforeBoxIndex >= 0 ? score()->measure(beforeBoxIndex) : nullptr;
2020-11-06 13:40:25 +01:00
startEdit();
2020-11-06 13:40:25 +01:00
Ms::Score::InsertMeasureOptions options;
options.createEmptyMeasures = false;
options.moveSignaturesClef = true;
options.needDeselectAll = false;
for (int i = 0; i < count; ++i) {
score()->insertMeasure(elementType, beforeBox, options);
2020-11-06 13:40:25 +01:00
}
apply();
2020-11-06 13:40:25 +01:00
notifyAboutNotationChanged();
2020-11-06 13:40:25 +01:00
}
2020-11-10 08:03:27 +01:00
void NotationInteraction::copySelection()
{
if (!selection()->canCopy()) {
return;
}
if (isTextEditingStarted()) {
m_editData.element->editCopy(m_editData);
Ms::TextEditData* ted = static_cast<Ms::TextEditData*>(m_editData.getData(m_editData.element));
if (!ted->selectedText.isEmpty()) {
QGuiApplication::clipboard()->setText(ted->selectedText, QClipboard::Clipboard);
}
} else {
QMimeData* mimeData = selection()->mimeData();
if (!mimeData) {
return;
}
QApplication::clipboard()->setMimeData(mimeData);
2020-11-10 08:03:27 +01:00
}
}
void NotationInteraction::copyLyrics()
{
QString text = score()->extractLyrics();
QApplication::clipboard()->setText(text);
}
void NotationInteraction::pasteSelection(const Fraction& scale)
2020-11-10 08:03:27 +01:00
{
startEdit();
2020-11-10 08:03:27 +01:00
if (isTextEditingStarted()) {
#if defined(Q_OS_MAC) || defined(Q_OS_WIN)
QClipboard::Mode mode = QClipboard::Clipboard;
#else
QClipboard::Mode mode = QClipboard::Selection;
#endif
QString txt = QGuiApplication::clipboard()->text(mode);
toTextBase(m_editData.element)->paste(m_editData, txt);
} else {
const QMimeData* mimeData = QApplication::clipboard()->mimeData();
score()->cmdPaste(mimeData, nullptr, scale);
}
apply();
2020-11-10 08:03:27 +01:00
notifyAboutSelectionChanged();
2020-11-10 08:03:27 +01:00
}
void NotationInteraction::swapSelection()
{
if (!selection()->canCopy()) {
return;
}
Ms::Selection& selection = score()->selection();
QString mimeType = selection.mimeType();
2020-12-15 14:56:47 +01:00
if (mimeType == Ms::mimeStaffListFormat) { // determine size of clipboard selection
const QMimeData* mimeData = this->selection()->mimeData();
2020-12-15 14:56:47 +01:00
QByteArray data = mimeData ? mimeData->data(Ms::mimeStaffListFormat) : QByteArray();
Ms::XmlReader reader(data);
reader.readNextStartElement();
Fraction tickLen = Fraction(0, 1);
int stavesCount = 0;
if (reader.name() == "StaffList") {
2020-12-15 14:56:47 +01:00
tickLen = Ms::Fraction::fromTicks(reader.intAttribute("len", 0));
stavesCount = reader.intAttribute("staves", 0);
}
2020-12-15 14:56:47 +01:00
if (tickLen > Ms::Fraction(0, 1)) { // attempt to extend selection to match clipboard size
Ms::Segment* segment = selection.startSegment();
Ms::Fraction startTick = selection.tickStart() + tickLen;
Ms::Segment* segmentAfter = score()->tick2leftSegment(startTick);
int staffIndex = selection.staffStart() + stavesCount - 1;
if (staffIndex >= score()->nstaves()) {
staffIndex = score()->nstaves() - 1;
}
startTick = selection.tickStart();
2020-12-15 14:56:47 +01:00
Ms::Fraction endTick = startTick + tickLen;
selection.extendRangeSelection(segment, segmentAfter, staffIndex, startTick, endTick);
selection.update();
}
}
QByteArray currentSelectionBackup(selection.mimeData());
pasteSelection();
QMimeData* mimeData = new QMimeData();
mimeData->setData(mimeType, currentSelectionBackup);
QApplication::clipboard()->setMimeData(mimeData);
}
2020-10-13 14:06:26 +02:00
void NotationInteraction::deleteSelection()
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
startEdit();
if (isTextEditingStarted()) {
auto textBase = toTextBase(m_editData.element);
if (!textBase->deleteSelectedText(m_editData)) {
m_editData.key = Qt::Key_Backspace;
m_editData.modifiers = {};
textBase->edit(m_editData);
}
} else {
score()->cmdDeleteSelection();
}
apply();
2020-10-13 14:06:26 +02:00
resetGripEdit();
notifyAboutSelectionChanged();
notifyAboutNotationChanged();
2020-10-13 14:06:26 +02:00
}
2020-11-06 09:58:13 +01:00
2020-11-25 14:36:39 +01:00
void NotationInteraction::flipSelection()
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
startEdit();
2020-11-25 14:36:39 +01:00
score()->cmdFlip();
apply();
2020-11-25 14:36:39 +01:00
notifyAboutSelectionChanged();
2020-11-25 14:36:39 +01:00
}
2020-11-25 19:03:54 +01:00
void NotationInteraction::addTieToSelection()
{
startEdit();
score()->cmdToggleTie();
apply();
2020-11-25 19:03:54 +01:00
notifyAboutSelectionChanged();
2020-11-25 19:03:54 +01:00
}
2021-04-17 00:49:49 +02:00
void NotationInteraction::addTiedNoteToChord()
{
startEdit();
score()->cmdAddTie(true);
apply();
notifyAboutSelectionChanged();
}
2020-11-25 19:03:54 +01:00
void NotationInteraction::addSlurToSelection()
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
startEdit();
2020-11-25 19:03:54 +01:00
doAddSlur();
apply();
2020-11-25 19:03:54 +01:00
notifyAboutSelectionChanged();
2020-11-25 19:03:54 +01:00
}
2020-11-30 12:38:33 +01:00
void NotationInteraction::addOttavaToSelection(OttavaType type)
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
2020-11-30 12:38:33 +01:00
startEdit();
score()->cmdAddOttava(type);
apply();
notifyAboutSelectionChanged();
}
void NotationInteraction::addHairpinToSelection(HairpinType type)
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
2020-11-30 12:38:33 +01:00
startEdit();
score()->addHairpin(type);
apply();
notifyAboutSelectionChanged();
}
2020-12-09 11:28:49 +01:00
void NotationInteraction::addAccidentalToSelection(AccidentalType type)
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
Ms::EditData editData(&m_scoreCallbacks);
2020-12-09 11:28:49 +01:00
startEdit();
score()->toggleAccidental(type, editData);
apply();
notifyAboutSelectionChanged();
}
2021-04-17 00:50:18 +02:00
void NotationInteraction::putRestToSelection()
{
Ms::InputState& is = score()->inputState();
2021-04-17 00:51:20 +02:00
if (!is.duration().isValid() || is.duration().isZero() || is.duration().isMeasure()) {
2021-04-17 00:50:18 +02:00
is.setDuration(DurationType::V_QUARTER);
2021-04-17 00:51:20 +02:00
}
2021-04-17 00:50:18 +02:00
putRest(is.duration().type());
}
void NotationInteraction::putRest(DurationType duration)
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdEnterRest(Duration(duration));
apply();
notifyAboutSelectionChanged();
}
void NotationInteraction::addBracketsToSelection(BracketsType type)
{
if (selection()->isNone()) {
return;
}
startEdit();
switch (type) {
case BracketsType::Brackets:
score()->cmdAddBracket();
break;
case BracketsType::Braces:
score()->cmdAddBraces();
break;
case BracketsType::Parentheses:
score()->cmdAddParentheses();
break;
}
apply();
notifyAboutNotationChanged();
}
2020-12-01 15:19:42 +01:00
void NotationInteraction::changeSelectedNotesArticulation(SymbolId articulationSymbolId)
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
2020-12-15 14:56:47 +01:00
std::vector<Ms::Note*> notes = score()->selection().noteList();
2020-12-01 15:19:42 +01:00
2020-12-14 16:07:33 +01:00
auto updateMode = notesHaveActiculation(notes, articulationSymbolId)
? Ms::ArticulationsUpdateMode::Remove : Ms::ArticulationsUpdateMode::Insert;
2020-12-14 16:07:33 +01:00
std::set<Chord*> chords;
for (Note* note: notes) {
Chord* chord = note->chord();
if (chords.find(chord) == chords.end()) {
chords.insert(chord);
}
}
startEdit();
2020-12-14 16:07:33 +01:00
for (Chord* chord: chords) {
chord->updateArticulations({ articulationSymbolId }, updateMode);
2020-12-01 15:19:42 +01:00
}
apply();
notifyAboutSelectionChanged();
}
2021-03-03 14:11:21 +01:00
void NotationInteraction::addGraceNotesToSelectedNotes(GraceNoteType type)
2020-12-01 19:11:36 +01:00
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
int denominator = 1;
switch (type) {
case GraceNoteType::GRACE4:
case GraceNoteType::INVALID:
case GraceNoteType::NORMAL:
denominator = 1;
break;
case GraceNoteType::ACCIACCATURA:
case GraceNoteType::APPOGGIATURA:
case GraceNoteType::GRACE8_AFTER:
denominator = 2;
break;
case GraceNoteType::GRACE16:
case GraceNoteType::GRACE16_AFTER:
denominator = 4;
break;
case GraceNoteType::GRACE32:
case GraceNoteType::GRACE32_AFTER:
denominator = 8;
break;
}
startEdit();
2021-11-25 17:31:04 +01:00
score()->cmdAddGrace(type, Ms::Constant::division / denominator);
apply();
notifyAboutNotationChanged();
}
bool NotationInteraction::canAddTupletToSelectedChordRests() const
2021-12-07 13:58:55 +01:00
{
for (ChordRest* chordRest : score()->getSelectedChordRests()) {
if (chordRest->isGrace()) {
continue;
}
2021-12-17 17:55:55 +01:00
if (chordRest->durationType() < Ms::TDuration(Ms::DurationType::V_512TH)
&& chordRest->durationType() != Ms::TDuration(Ms::DurationType::V_MEASURE)) {
2021-12-07 13:58:55 +01:00
return false;
}
}
return true;
}
2021-03-03 14:11:21 +01:00
void NotationInteraction::addTupletToSelectedChordRests(const TupletOptions& options)
{
if (selection()->isNone()) {
return;
}
startEdit();
for (ChordRest* chordRest : score()->getSelectedChordRests()) {
if (!chordRest->isGrace()) {
score()->addTuplet(chordRest, options.ratio, options.numberType, options.bracketType);
}
}
2021-12-07 13:58:55 +01:00
2021-03-03 14:11:21 +01:00
apply();
notifyAboutSelectionChanged();
}
void NotationInteraction::addBeamToSelectedChordRests(BeamMode mode)
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdSetBeamMode(mode);
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::increaseDecreaseDuration(int steps, bool stepByDots)
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdIncDecDuration(steps, stepByDots);
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::toggleLayoutBreak(LayoutBreakType breakType)
{
startEdit();
score()->cmdToggleLayoutBreak(breakType);
apply();
notifyAboutNotationChanged();
}
2020-11-06 09:58:13 +01:00
void NotationInteraction::setBreaksSpawnInterval(BreaksSpawnIntervalType intervalType, int interval)
{
interval = intervalType == BreaksSpawnIntervalType::MeasuresInterval ? interval : 0;
bool afterEachSystem = intervalType == BreaksSpawnIntervalType::AfterEachSystem;
startEdit();
2020-11-06 09:58:13 +01:00
score()->addRemoveBreaks(interval, afterEachSystem);
apply();
2020-11-06 09:58:13 +01:00
notifyAboutNotationChanged();
2020-11-06 09:58:13 +01:00
}
bool NotationInteraction::transpose(const TransposeOptions& options)
{
startEdit();
bool ok = score()->transpose(options.mode, options.direction, options.key, options.interval,
options.needTransposeKeys, options.needTransposeChordNames, options.needTransposeDoubleSharpsFlats);
apply();
notifyAboutNotationChanged();
return ok;
}
2020-11-03 17:54:25 +01:00
void NotationInteraction::swapVoices(int voiceIndex1, int voiceIndex2)
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
2020-11-03 17:54:25 +01:00
if (voiceIndex1 == voiceIndex2) {
return;
}
if (!isVoiceIndexValid(voiceIndex1) || !isVoiceIndexValid(voiceIndex2)) {
return;
}
startEdit();
2020-11-03 17:54:25 +01:00
score()->cmdExchangeVoice(voiceIndex1, voiceIndex2);
apply();
2020-11-03 17:54:25 +01:00
notifyAboutNotationChanged();
2020-11-03 17:54:25 +01:00
}
void NotationInteraction::addIntervalToSelectedNotes(int interval)
{
if (!isNotesIntervalValid(interval)) {
return;
}
std::vector<Note*> notes;
if (score()->selection().isRange()) {
for (const ChordRest* chordRest : score()->getSelectedChordRests()) {
if (chordRest->isChord()) {
const Chord* chord = toChord(chordRest);
Note* note = interval > 0 ? chord->upNote() : chord->downNote();
notes.push_back(note);
}
}
} else {
notes = score()->selection().noteList();
}
if (notes.empty()) {
return;
}
startEdit();
score()->addInterval(interval, notes);
apply();
2021-06-22 19:57:53 +02:00
notifyAboutSelectionChanged();
}
void NotationInteraction::addFret(int fretIndex)
{
startEdit();
score()->cmdAddFret(fretIndex);
apply();
notifyAboutSelectionChanged();
}
2020-11-30 10:19:07 +01:00
void NotationInteraction::changeSelectedNotesVoice(int voiceIndex)
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
2020-11-30 10:19:07 +01:00
if (!isVoiceIndexValid(voiceIndex)) {
return;
}
startEdit();
2020-11-30 10:19:07 +01:00
score()->changeSelectedNotesVoice(voiceIndex);
apply();
2020-11-30 10:19:07 +01:00
notifyAboutSelectionChanged();
2020-11-30 10:19:07 +01:00
}
2020-11-30 12:38:33 +01:00
void NotationInteraction::addAnchoredLineToSelectedNotes()
2020-11-30 09:34:19 +01:00
{
2020-12-23 10:56:45 +01:00
if (selection()->isNone()) {
return;
}
startEdit();
2020-11-30 12:38:33 +01:00
score()->addNoteLine();
apply();
2020-11-30 09:34:19 +01:00
notifyAboutSelectionChanged();
2020-11-03 17:54:25 +01:00
}
mu::Ret NotationInteraction::canAddText(TextStyleType type) const
{
static const std::vector<TextStyleType> needSelectedNoteOrRestTypes {
TextStyleType::SYSTEM,
TextStyleType::STAFF,
TextStyleType::EXPRESSION,
TextStyleType::REHEARSAL_MARK,
TextStyleType::INSTRUMENT_CHANGE,
TextStyleType::FINGERING,
TextStyleType::STICKING,
TextStyleType::HARMONY_A,
TextStyleType::HARMONY_ROMAN,
TextStyleType::HARMONY_NASHVILLE,
TextStyleType::LYRICS_ODD,
TextStyleType::TEMPO
};
if (std::find(needSelectedNoteOrRestTypes.cbegin(), needSelectedNoteOrRestTypes.cend(), type) != needSelectedNoteOrRestTypes.cend()) {
2022-01-12 14:29:31 +01:00
bool isNoteOrRestSelected = elementsSelected({ ElementType::NOTE, ElementType::REST,
ElementType::MEASURE_REPEAT, ElementType::CHORD });
return isNoteOrRestSelected ? make_ok() : make_ret(Err::NoteOrRestIsNotSelected);
}
return make_ok();
}
2021-12-15 12:25:02 +01:00
void NotationInteraction::addText(TextStyleType type)
2020-12-08 19:37:10 +01:00
{
if (!scoreHasMeasure()) {
LOGE() << "Need to create measure";
return;
}
if (m_noteInput->isNoteInputMode()) {
m_noteInput->endNoteInput();
}
startEdit();
2020-12-15 14:56:47 +01:00
Ms::TextBase* textBox = score()->addText(type);
2020-12-08 19:37:10 +01:00
apply();
if (textBox) {
startEditText(textBox);
2020-12-08 19:37:10 +01:00
}
}
mu::Ret NotationInteraction::canAddFiguredBass() const
{
2022-01-12 14:29:31 +01:00
bool isNoteOrRestSelected = elementsSelected({ ElementType::NOTE, ElementType::FIGURED_BASS, ElementType::REST });
return isNoteOrRestSelected ? make_ok() : make_ret(Err::NoteOrFiguredBassIsNotSelected);
}
2021-02-10 14:24:56 +01:00
void NotationInteraction::addFiguredBass()
{
2021-12-20 09:35:09 +01:00
startEdit();
Ms::FiguredBass* figuredBass = score()->addFiguredBass();
2021-02-10 14:24:56 +01:00
if (figuredBass) {
2021-12-20 09:35:09 +01:00
apply();
2021-06-07 19:25:41 +02:00
startEditText(figuredBass, PointF());
2021-12-20 09:35:09 +01:00
notifyAboutSelectionChanged();
} else {
rollback();
2021-02-10 14:24:56 +01:00
}
}
void NotationInteraction::addStretch(qreal value)
{
startEdit();
score()->cmdAddStretch(value);
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::addTimeSignature(Measure* measure, int staffIndex, TimeSignature* timeSignature)
{
startEdit();
score()->cmdAddTimeSig(measure, staffIndex, timeSignature, true);
apply();
notifyAboutNotationChanged();
}
2021-02-25 13:27:58 +01:00
void NotationInteraction::explodeSelectedStaff()
{
if (!selection()->isRange()) {
return;
}
startEdit();
score()->cmdExplode();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::implodeSelectedStaff()
{
if (!selection()->isRange()) {
return;
}
startEdit();
score()->cmdImplode();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::realizeSelectedChordSymbols(bool literal, Voicing voicing, HarmonyDurationType durationType)
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdRealizeChordSymbols(literal, voicing, durationType);
apply();
notifyAboutNotationChanged();
}
2021-02-25 14:11:25 +01:00
void NotationInteraction::removeSelectedRange()
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdTimeDelete();
apply();
notifyAboutNotationChanged();
}
2021-02-25 15:44:49 +01:00
void NotationInteraction::removeEmptyTrailingMeasures()
{
startEdit();
score()->cmdRemoveEmptyTrailingMeasures();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::fillSelectionWithSlashes()
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdSlashFill();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::replaceSelectedNotesWithSlashes()
{
if (selection()->isNone()) {
return;
}
startEdit();
score()->cmdSlashRhythm();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::repeatSelection()
{
const Ms::Selection& selection = score()->selection();
if (score()->noteEntryMode() && selection.isSingle()) {
EngravingItem* el = selection.element();
if (el && el->type() == ElementType::NOTE && !score()->inputState().endOfScore()) {
startEdit();
Chord* c = toNote(el)->chord();
for (Note* note : c->notes()) {
Ms::NoteVal nval = note->noteVal();
score()->addPitch(nval, note != c->notes()[0]);
}
apply();
notifyAboutNotationChanged();
}
return;
}
if (!selection.isRange()) {
ChordRest* cr = score()->getSelectedChordRest();
if (!cr) {
return;
}
score()->select(cr, SelectType::RANGE);
}
Ms::XmlReader xml(selection.mimeData());
xml.setPasteMode(true);
int dStaff = selection.staffStart();
Ms::Segment* endSegment = selection.endSegment();
if (endSegment && endSegment->segmentType() != Ms::SegmentType::ChordRest) {
endSegment = endSegment->next1(Ms::SegmentType::ChordRest);
}
if (endSegment && endSegment->element(dStaff * Ms::VOICES)) {
EngravingItem* e = endSegment->element(dStaff * Ms::VOICES);
if (e) {
startEdit();
ChordRest* cr = toChordRest(e);
score()->pasteStaff(xml, cr->segment(), cr->staffIdx());
score()->endCmd();
apply();
notifyAboutNotationChanged();
}
}
}
void NotationInteraction::changeEnharmonicSpelling(bool both)
{
startEdit();
score()->changeEnharmonicSpelling(both);
apply();
notifyAboutNotationChanged();
}
2021-02-25 14:56:02 +01:00
void NotationInteraction::spellPitches()
{
startEdit();
score()->spell();
apply();
notifyAboutNotationChanged();
}
2021-02-25 15:07:16 +01:00
void NotationInteraction::regroupNotesAndRests()
{
startEdit();
score()->cmdResetNoteAndRestGroupings();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::resequenceRehearsalMarks()
{
startEdit();
score()->cmdResequenceRehearsalMarks();
apply();
notifyAboutNotationChanged();
}
2021-02-25 15:22:49 +01:00
void NotationInteraction::unrollRepeats()
{
if (!score()->masterScore()) {
return;
}
startEdit();
score()->masterScore()->unrollRepeats();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::resetStretch()
{
startEdit();
score()->resetUserStretch();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::resetTextStyleOverrides()
{
startEdit();
score()->cmdResetTextStyleOverrides();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::resetBeamMode()
{
startEdit();
score()->cmdResetBeamMode();
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::resetShapesAndPosition()
{
auto resetItem = [](EngravingItem* item) {
item->reset();
if (item->isSpanner()) {
for (Ms::SpannerSegment* spannerSegment : toSpanner(item)->spannerSegments()) {
spannerSegment->reset();
}
}
};
startEdit();
Defer defer([this] {
apply();
notifyAboutNotationChanged();
});
if (selection()->element()) {
resetItem(selection()->element());
return;
}
for (EngravingItem* item : selection()->elements()) {
resetItem(item);
2021-02-12 10:11:05 +01:00
}
}
ScoreConfig NotationInteraction::scoreConfig() const
{
ScoreConfig config;
config.isShowInvisibleElements = score()->showInvisible();
config.isShowUnprintableElements = score()->showUnprintable();
config.isShowFrames = score()->showFrames();
config.isShowPageMargins = score()->showPageborders();
config.isMarkIrregularMeasures = score()->markIrregularMeasures();
return config;
}
void NotationInteraction::setScoreConfig(ScoreConfig config)
{
startEdit();
score()->setShowInvisible(config.isShowInvisibleElements);
score()->setShowUnprintable(config.isShowUnprintableElements);
score()->setShowFrames(config.isShowFrames);
score()->setShowPageborders(config.isShowPageMargins);
score()->setMarkIrregularMeasures(config.isMarkIrregularMeasures);
2021-09-02 15:26:45 +02:00
EngravingItem* selectedElement = selection()->element();
2021-02-24 15:48:08 +01:00
if (selectedElement && !selectedElement->isInteractionAvailable()) {
clearSelection();
}
apply();
notifyAboutNotationChanged();
}
2021-09-02 15:26:45 +02:00
bool NotationInteraction::needEndTextEditing(const std::vector<EngravingItem*>& newSelectedElements) const
{
if (!isTextEditingStarted()) {
return false;
}
if (newSelectedElements.empty()) {
return false;
}
if (newSelectedElements.size() > 1) {
return true;
}
return newSelectedElements.front() != m_editData.element;
}
2021-01-28 12:27:36 +01:00
void NotationInteraction::updateGripEdit()
2021-01-28 12:27:36 +01:00
{
const auto& elements = score()->selection().elements();
if (elements.isEmpty() || elements.size() > 1) {
2021-01-28 12:27:36 +01:00
resetGripEdit();
return;
}
2021-09-02 15:26:45 +02:00
EngravingItem* element = elements.front();
2021-01-28 12:27:36 +01:00
if (element->gripsCount() <= 0) {
resetGripEdit();
return;
}
m_editData.grips = element->gripsCount();
m_editData.curGrip = Ms::Grip::NO_GRIP;
bool editingElement = m_editData.element != nullptr;
m_editData.element = element;
element->startEdit(m_editData);
updateAnchorLines();
if (!editingElement) {
m_editData.element = nullptr;
}
2021-01-28 12:27:36 +01:00
}
void NotationInteraction::resetGripEdit()
{
m_editData.grips = 0;
m_editData.curGrip = Ms::Grip::NO_GRIP;
m_editData.grip.clear();
2021-01-28 12:27:36 +01:00
resetAnchorLines();
}
2021-02-12 10:11:05 +01:00
bool NotationInteraction::elementsSelected(const std::vector<ElementType>& elementsTypes) const
{
2022-01-12 14:29:31 +01:00
const EngravingItem* element = selection()->element();
return element && std::find(elementsTypes.cbegin(), elementsTypes.cend(), element->type()) != elementsTypes.cend();
}
void NotationInteraction::navigateToLyrics(bool back, bool moveOnly, bool end)
{
if (!m_editData.element || !m_editData.element->isLyrics()) {
qWarning("nextLyric called with invalid current element");
return;
}
Ms::Lyrics* lyrics = toLyrics(m_editData.element);
int track = lyrics->track();
Ms::Segment* segment = lyrics->segment();
int verse = lyrics->no();
2021-11-19 17:41:54 +01:00
Ms::PlacementV placement = lyrics->placement();
Ms::PropertyFlags pFlags = lyrics->propertyFlags(Ms::Pid::PLACEMENT);
Ms::Segment* nextSegment = segment;
if (back) {
// search prev chord
while ((nextSegment = nextSegment->prev1(Ms::SegmentType::ChordRest))) {
2021-09-02 15:26:45 +02:00
EngravingItem* el = nextSegment->element(track);
if (el && el->isChord()) {
break;
}
}
} else {
// search next chord
while ((nextSegment = nextSegment->next1(Ms::SegmentType::ChordRest))) {
2021-09-02 15:26:45 +02:00
EngravingItem* el = nextSegment->element(track);
if (el && el->isChord()) {
break;
}
}
}
if (nextSegment == 0) {
return;
}
endEditText();
// look for the lyrics we are moving from; may be the current lyrics or a previous one
// if we are skipping several chords with spaces
Ms::Lyrics* fromLyrics = 0;
if (!back) {
while (segment) {
ChordRest* cr = toChordRest(segment->element(track));
if (cr) {
fromLyrics = cr->lyrics(verse, placement);
if (fromLyrics) {
break;
}
}
segment = segment->prev1(Ms::SegmentType::ChordRest);
}
}
ChordRest* cr = toChordRest(nextSegment->element(track));
if (!cr) {
qDebug("no next lyrics list: %s", nextSegment->element(track)->name());
return;
}
Ms::Lyrics* nextLyrics = cr->lyrics(verse, placement);
bool newLyrics = false;
if (!nextLyrics) {
nextLyrics = Factory::createLyrics(cr);
nextLyrics->setTrack(track);
cr = toChordRest(nextSegment->element(track));
nextLyrics->setParent(cr);
nextLyrics->setNo(verse);
nextLyrics->setPlacement(placement);
nextLyrics->setPropertyFlags(Ms::Pid::PLACEMENT, pFlags);
nextLyrics->setSyllabic(Ms::Lyrics::Syllabic::SINGLE);
newLyrics = true;
}
score()->startCmd();
if (fromLyrics && !moveOnly) {
switch (nextLyrics->syllabic()) {
// as we arrived at nextLyrics by a [Space], it can be the beginning
// of a multi-syllable, but cannot have syllabic dashes before
case Ms::Lyrics::Syllabic::SINGLE:
case Ms::Lyrics::Syllabic::BEGIN:
break;
case Ms::Lyrics::Syllabic::END:
nextLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::SINGLE));
break;
case Ms::Lyrics::Syllabic::MIDDLE:
nextLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::BEGIN));
break;
}
// as we moved away from fromLyrics by a [Space], it can be
// the end of a multi-syllable, but cannot have syllabic dashes after
switch (fromLyrics->syllabic()) {
case Ms::Lyrics::Syllabic::SINGLE:
case Ms::Lyrics::Syllabic::END:
break;
case Ms::Lyrics::Syllabic::BEGIN:
fromLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::SINGLE));
break;
case Ms::Lyrics::Syllabic::MIDDLE:
fromLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::END));
break;
}
// for the same reason, it cannot have a melisma
fromLyrics->undoChangeProperty(Ms::Pid::LYRIC_TICKS, 0);
}
if (newLyrics) {
score()->undoAddElement(nextLyrics);
}
score()->endCmd();
score()->select(nextLyrics, SelectType::SINGLE, 0);
score()->setLayoutAll();
startEditText(nextLyrics, PointF());
Ms::TextCursor* cursor = nextLyrics->cursor();
if (end) {
nextLyrics->selectAll(cursor);
} else {
cursor->movePosition(Ms::TextCursor::MoveOperation::End, Ms::TextCursor::MoveMode::MoveAnchor);
cursor->movePosition(Ms::TextCursor::MoveOperation::Start, Ms::TextCursor::MoveMode::KeepAnchor);
}
}
void NotationInteraction::navigateToLyrics(MoveDirection direction)
{
navigateToLyrics(direction == MoveDirection::Left, true, false);
}
void NotationInteraction::nagivateToNextSyllable()
{
if (!m_editData.element || !m_editData.element->isLyrics()) {
qWarning("nextSyllable called with invalid current element");
return;
}
Ms::Lyrics* lyrics = toLyrics(m_editData.element);
int track = lyrics->track();
Ms::Segment* segment = lyrics->segment();
int verse = lyrics->no();
2021-11-19 17:41:54 +01:00
Ms::PlacementV placement = lyrics->placement();
Ms::PropertyFlags pFlags = lyrics->propertyFlags(Ms::Pid::PLACEMENT);
// search next chord
Ms::Segment* nextSegment = segment;
while ((nextSegment = nextSegment->next1(Ms::SegmentType::ChordRest))) {
2021-09-02 15:26:45 +02:00
EngravingItem* el = nextSegment->element(track);
if (el && el->isChord()) {
break;
}
}
if (nextSegment == 0) {
return;
}
// look for the lyrics we are moving from; may be the current lyrics or a previous one
// we are extending with several dashes
Ms::Lyrics* fromLyrics = 0;
while (segment) {
ChordRest* cr = toChordRest(segment->element(track));
if (!cr) {
segment = segment->prev1(Ms::SegmentType::ChordRest);
continue;
}
fromLyrics = cr->lyrics(verse, placement);
if (fromLyrics) {
break;
}
segment = segment->prev1(Ms::SegmentType::ChordRest);
}
endEditText();
score()->startCmd();
ChordRest* cr = toChordRest(nextSegment->element(track));
Ms::Lyrics* toLyrics = cr->lyrics(verse, placement);
bool newLyrics = (toLyrics == 0);
if (!toLyrics) {
toLyrics = Factory::createLyrics(cr);
toLyrics->setTrack(track);
toLyrics->setParent(cr);
toLyrics->setNo(verse);
toLyrics->setPlacement(placement);
toLyrics->setPropertyFlags(Ms::Pid::PLACEMENT, pFlags);
toLyrics->setSyllabic(Ms::Lyrics::Syllabic::END);
} else {
// as we arrived at toLyrics by a dash, it cannot be initial or isolated
if (toLyrics->syllabic() == Ms::Lyrics::Syllabic::BEGIN) {
toLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::MIDDLE));
} else if (toLyrics->syllabic() == Ms::Lyrics::Syllabic::SINGLE) {
toLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::END));
}
}
if (fromLyrics) {
// as we moved away from fromLyrics by a dash,
// it can have syll. dashes before and after but cannot be isolated or terminal
switch (fromLyrics->syllabic()) {
case Ms::Lyrics::Syllabic::BEGIN:
case Ms::Lyrics::Syllabic::MIDDLE:
break;
case Ms::Lyrics::Syllabic::SINGLE:
fromLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::BEGIN));
break;
case Ms::Lyrics::Syllabic::END:
fromLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::MIDDLE));
break;
}
// for the same reason, it cannot have a melisma
fromLyrics->undoChangeProperty(Ms::Pid::LYRIC_TICKS, Fraction(0, 1));
}
if (newLyrics) {
score()->undoAddElement(toLyrics);
}
score()->endCmd();
score()->select(toLyrics, SelectType::SINGLE, 0);
score()->setLayoutAll();
startEditText(toLyrics, PointF());
toLyrics->selectAll(toLyrics->cursor());
}
void NotationInteraction::navigateToLyricsVerse(MoveDirection direction)
{
if (!m_editData.element || !m_editData.element->isLyrics()) {
qWarning("nextLyricVerse called with invalid current element");
return;
}
Ms::Lyrics* lyrics = toLyrics(m_editData.element);
int track = lyrics->track();
ChordRest* cr = lyrics->chordRest();
int verse = lyrics->no();
2021-11-19 17:41:54 +01:00
Ms::PlacementV placement = lyrics->placement();
Ms::PropertyFlags pFlags = lyrics->propertyFlags(Ms::Pid::PLACEMENT);
if (direction == MoveDirection::Up) {
if (verse == 0) {
return;
}
--verse;
} else {
++verse;
if (verse > cr->lastVerse(placement)) {
return;
}
}
endEditText();
lyrics = cr->lyrics(verse, placement);
if (!lyrics) {
lyrics = Factory::createLyrics(cr);
lyrics->setTrack(track);
lyrics->setParent(cr);
lyrics->setNo(verse);
lyrics->setPlacement(placement);
lyrics->setPropertyFlags(Ms::Pid::PLACEMENT, pFlags);
score()->startCmd();
score()->undoAddElement(lyrics);
score()->endCmd();
}
score()->select(lyrics, SelectType::SINGLE, 0);
startEditText(lyrics, PointF());
lyrics = toLyrics(m_editData.element);
score()->setLayoutAll();
score()->update();
lyrics->selectAll(lyrics->cursor());
}
//! NOTE: Copied from ScoreView::harmonyBeatsTab
void NotationInteraction::navigateToNearHarmony(MoveDirection direction, bool nearNoteOrRest)
{
Ms::Harmony* harmony = editedHarmony();
Ms::Segment* segment = harmony ? toSegment(harmony->parent()) : nullptr;
if (!segment) {
qDebug("no segment");
return;
}
Measure* measure = segment->measure();
Fraction tick = segment->tick();
int track = harmony->track();
bool backDirection = direction == MoveDirection::Left;
if (backDirection && tick == measure->tick()) {
// previous bar, if any
measure = measure->prevMeasure();
if (!measure) {
qDebug("no previous measure");
return;
}
}
Fraction f = measure->ticks();
int ticksPerBeat = f.ticks()
/ ((f.numerator() > 3 && (f.numerator() % 3) == 0 && f.denominator() > 4) ? f.numerator() / 3 : f.numerator());
Fraction tickInBar = tick - measure->tick();
Fraction newTick = measure->tick()
+ Fraction::fromTicks((
(tickInBar.ticks() + (backDirection ? -1 : ticksPerBeat)) / ticksPerBeat
)
* ticksPerBeat);
bool needAddSegment = false;
// look for next/prev beat, note, rest or chord
for (;;) {
segment = backDirection ? segment->prev1(Ms::SegmentType::ChordRest) : segment->next1(Ms::SegmentType::ChordRest);
if (!segment || (backDirection ? (segment->tick() < newTick) : (segment->tick() > newTick))) {
// no segment or moved past the beat - create new segment
if (!backDirection && newTick >= measure->tick() + f) {
// next bar, if any
measure = measure->nextMeasure();
if (!measure) {
qDebug("no next measure");
return;
}
}
segment = Factory::createSegment(measure, Ms::SegmentType::ChordRest, newTick - measure->tick());
if (!segment) {
qDebug("no prev segment");
return;
}
needAddSegment = true;
break;
}
if (segment->tick() == newTick) {
break;
}
if (nearNoteOrRest) {
int minTrack = (track / Ms::VOICES) * Ms::VOICES;
int maxTrack = minTrack + (Ms::VOICES - 1);
if (segment->hasAnnotationOrElement(ElementType::HARMONY, minTrack, maxTrack)) {
break;
}
}
}
startEdit();
if (needAddSegment) {
score()->undoAddElement(segment);
}
2021-12-16 15:56:17 +01:00
Ms::Harmony* nextHarmony = findHarmonyInSegment(segment, track, harmony->textStyleType());
if (!nextHarmony) {
nextHarmony = createHarmony(segment, track, harmony->harmonyType());
score()->undoAddElement(nextHarmony);
}
apply();
startEditText(nextHarmony);
}
//! NOTE: Copied from ScoreView::harmonyTab
void NotationInteraction::navigateToHarmonyInNearMeasure(MoveDirection direction)
{
Ms::Harmony* harmony = editedHarmony();
Ms::Segment* segment = harmony ? toSegment(harmony->parent()) : nullptr;
if (!segment) {
qDebug("harmonyTicksTab: no segment");
return;
}
// moving to next/prev measure
Measure* measure = segment->measure();
if (measure) {
if (direction == MoveDirection::Left) {
measure = measure->prevMeasure();
} else {
measure = measure->nextMeasure();
}
}
if (!measure) {
qDebug("no prev/next measure");
return;
}
segment = measure->findSegment(Ms::SegmentType::ChordRest, measure->tick());
if (!segment) {
qDebug("no ChordRest segment as measure");
return;
}
int track = harmony->track();
2021-12-16 15:56:17 +01:00
Ms::Harmony* nextHarmony = findHarmonyInSegment(segment, track, harmony->textStyleType());
if (!nextHarmony) {
nextHarmony = createHarmony(segment, track, harmony->harmonyType());
startEdit();
score()->undoAddElement(nextHarmony);
apply();
}
startEditText(nextHarmony);
}
//! NOTE: Copied from ScoreView::harmonyBeatsTab
void NotationInteraction::navigateToHarmony(const Fraction& ticks)
{
Ms::Harmony* harmony = editedHarmony();
Ms::Segment* segment = harmony ? toSegment(harmony->parent()) : nullptr;
if (!segment) {
qDebug("no segment");
return;
}
Measure* measure = segment->measure();
Fraction newTick = segment->tick() + ticks;
// find the measure containing the target tick
while (newTick >= measure->tick() + measure->ticks()) {
measure = measure->nextMeasure();
if (!measure) {
qDebug("no next measure");
return;
}
}
// look for a segment at this tick; if none, create one
while (segment && segment->tick() < newTick) {
segment = segment->next1(Ms::SegmentType::ChordRest);
}
startEdit();
if (!segment || segment->tick() > newTick) { // no ChordRest segment at this tick
segment = Factory::createSegment(measure, Ms::SegmentType::ChordRest, newTick - measure->tick());
score()->undoAddElement(segment);
}
int track = harmony->track();
2021-12-16 15:56:17 +01:00
Ms::Harmony* nextHarmony = findHarmonyInSegment(segment, track, harmony->textStyleType());
if (!nextHarmony) {
nextHarmony = createHarmony(segment, track, harmony->harmonyType());
score()->undoAddElement(nextHarmony);
}
apply();
startEditText(nextHarmony);
}
//! NOTE: Copied from ScoreView::figuredBassTab
void NotationInteraction::navigateToNearFiguredBass(MoveDirection direction)
{
Ms::FiguredBass* fb = Ms::toFiguredBass(m_editData.element);
Ms::Segment* segm = fb->segment();
int track = fb->track();
bool backDirection = direction == MoveDirection::Left;
if (!segm) {
qDebug("figuredBassTab: no segment");
return;
}
// search next chord segment in same staff
Ms::Segment* nextSegm = backDirection ? segm->prev1(Ms::SegmentType::ChordRest) : segm->next1(Ms::SegmentType::ChordRest);
int minTrack = (track / Ms::VOICES) * Ms::VOICES;
int maxTrack = minTrack + (Ms::VOICES - 1);
while (nextSegm) { // look for a ChordRest in the compatible track range
if (nextSegm->hasAnnotationOrElement(ElementType::FIGURED_BASS, minTrack, maxTrack)) {
break;
}
nextSegm = backDirection ? nextSegm->prev1(Ms::SegmentType::ChordRest) : nextSegm->next1(Ms::SegmentType::ChordRest);
}
if (!nextSegm) {
qDebug("figuredBassTab: no prev/next segment");
return;
}
bool bNew = false;
// add a (new) FB element, using chord duration as default duration
Ms::FiguredBass* fbNew = Ms::FiguredBass::addFiguredBassToSegment(nextSegm, track, Fraction(0, 1), &bNew);
if (bNew) {
startEdit();
score()->undoAddElement(fbNew);
apply();
}
startEditText(fbNew);
}
//! NOTE: Copied from ScoreView::figuredBassTab
void NotationInteraction::navigateToFiguredBassInNearMeasure(MoveDirection direction)
{
Ms::FiguredBass* fb = Ms::toFiguredBass(m_editData.element);
Ms::Segment* segm = fb->segment();
if (!segm) {
qDebug("figuredBassTab: no segment");
return;
}
// if moving to next/prev measure
Measure* meas = segm->measure();
if (meas) {
if (direction == MoveDirection::Left) {
meas = meas->prevMeasure();
} else {
meas = meas->nextMeasure();
}
}
if (!meas) {
qDebug("figuredBassTab: no prev/next measure");
return;
}
// find initial ChordRest segment
Ms::Segment* nextSegm = meas->findSegment(Ms::SegmentType::ChordRest, meas->tick());
if (!nextSegm) {
qDebug("figuredBassTab: no ChordRest segment at measure");
return;
}
bool bNew = false;
// add a (new) FB element, using chord duration as default duration
Ms::FiguredBass* fbNew = Ms::FiguredBass::addFiguredBassToSegment(nextSegm, fb->track(), Fraction(0, 1), &bNew);
if (bNew) {
startEdit();
score()->undoAddElement(fbNew);
apply();
}
startEditText(fbNew);
}
//! NOTE: Copied from ScoreView::figuredBassTicksTab
void NotationInteraction::navigateToFiguredBass(const Fraction& ticks)
{
Ms::FiguredBass* fb = Ms::toFiguredBass(m_editData.element);
int track = fb->track();
Ms::Segment* segm = fb->segment();
if (!segm) {
qDebug("figuredBassTicksTab: no segment");
return;
}
Measure* measure = segm->measure();
Fraction nextSegTick = segm->tick() + ticks;
// find the measure containing the target tick
while (nextSegTick >= measure->tick() + measure->ticks()) {
measure = measure->nextMeasure();
if (!measure) {
qDebug("figuredBassTicksTab: no next measure");
return;
}
}
// look for a segment at this tick; if none, create one
Ms::Segment* nextSegm = segm;
while (nextSegm && nextSegm->tick() < nextSegTick) {
nextSegm = nextSegm->next1(Ms::SegmentType::ChordRest);
}
bool needAddSegment = false;
if (!nextSegm || nextSegm->tick() > nextSegTick) { // no ChordRest segm at this tick
nextSegm = Factory::createSegment(measure, Ms::SegmentType::ChordRest, nextSegTick - measure->tick());
if (!nextSegm) {
qDebug("figuredBassTicksTab: no next segment");
return;
}
needAddSegment = true;
}
startEdit();
if (needAddSegment) {
score()->undoAddElement(nextSegm);
}
bool bNew = false;
Ms::FiguredBass* fbNew = Ms::FiguredBass::addFiguredBassToSegment(nextSegm, track, ticks, &bNew);
if (bNew) {
score()->undoAddElement(fbNew);
}
apply();
startEditText(fbNew);
}
//! NOTE: Copied from ScoreView::textTab
void NotationInteraction::navigateToNearText(MoveDirection direction)
{
Ms::EngravingItem* oe = m_editData.element;
if (!oe || !oe->isTextBase()) {
return;
}
Ms::EngravingItem* op = dynamic_cast<Ms::EngravingItem*>(oe->parent());
if (!op || !(op->isSegment() || op->isNote())) {
return;
}
TextBase* ot = Ms::toTextBase(oe);
Ms::TextStyleType textStyleType = ot->textStyleType();
ElementType type = ot->type();
int staffIdx = ot->staffIdx();
bool back = direction == MoveDirection::Left;
// get prev/next element now, as current element may be deleted if empty
Ms::EngravingItem* el = back ? score()->prevElement() : score()->nextElement();
// find new note to add text to
bool here = false; // prevent infinite loop (relevant if navigation is allowed to wrap around end of score)
while (el) {
if (el->isNote()) {
Note* n = Ms::toNote(el);
if (op->isNote() && n != op) {
break;
} else if (op->isSegment() && n->chord()->segment() != op) {
break;
} else if (here) {
break;
}
here = true;
} else if (el->isRest() && op->isSegment()) {
// skip rests, but still check for infinite loop
Rest* r = Ms::toRest(el);
if (r->segment() != op) {
} else if (here) {
break;
}
here = true;
}
// get prev/next note
score()->select(el);
Ms::EngravingItem* el2 = back ? score()->prevElement() : score()->nextElement();
// start/end of score reached
if (el2 == el) {
break;
}
el = el2;
}
if (!el || !el->isNote()) {
// nothing found, exit cleanly
if (op->selectable()) {
select({ op });
} else {
clearSelection();
}
return;
}
Note* nn = Ms::toNote(el);
// go to note
if (nn) {
score()->select(nn, SelectType::SINGLE);
}
// get existing text to edit
el = nullptr;
if (op->isNote()) {
// check element list of new note
for (Ms::EngravingItem* e : nn->el()) {
if (e->type() != type) {
continue;
}
TextBase* nt = Ms::toTextBase(e);
if (nt->textStyleType() == textStyleType) {
el = e;
break;
}
}
} else if (op->isSegment()) {
// check annotation list of new segment
Ms::Segment* ns = nn->chord()->segment();
for (Ms::EngravingItem* e : ns->annotations()) {
if (e->staffIdx() != staffIdx || e->type() != type) {
continue;
}
TextBase* nt = Ms::toTextBase(e);
if (nt->textStyleType() == textStyleType) {
el = e;
break;
}
}
}
if (el) {
// edit existing text
TextBase* text = dynamic_cast<TextBase*>(el);
if (text) {
startEditText(text);
}
} else {
// add new text if no existing element to edit
// TODO: for tempo text, mscore->addTempo() could be called
// but it pre-fills the text
// would be better to create empty tempo element
if (type != ElementType::TEMPO_TEXT) {
addText(textStyleType);
}
}
}
void NotationInteraction::addMelisma()
{
if (!m_editData.element || !m_editData.element->isLyrics()) {
qWarning("addMelisma called with invalid current element");
return;
}
Ms::Lyrics* lyrics = toLyrics(m_editData.element);
int track = lyrics->track();
Ms::Segment* segment = lyrics->segment();
int verse = lyrics->no();
2021-11-19 17:41:54 +01:00
Ms::PlacementV placement = lyrics->placement();
Ms::PropertyFlags pFlags = lyrics->propertyFlags(Ms::Pid::PLACEMENT);
Fraction endTick = segment->tick(); // a previous melisma cannot extend beyond this point
endEditText();
// search next chord
Ms::Segment* nextSegment = segment;
while ((nextSegment = nextSegment->next1(Ms::SegmentType::ChordRest))) {
2021-09-02 15:26:45 +02:00
EngravingItem* el = nextSegment->element(track);
if (el && el->isChord()) {
break;
}
}
// look for the lyrics we are moving from; may be the current lyrics or a previous one
// we are extending with several underscores
Ms::Lyrics* fromLyrics = 0;
while (segment) {
ChordRest* cr = toChordRest(segment->element(track));
if (cr) {
fromLyrics = cr->lyrics(verse, placement);
if (fromLyrics) {
break;
}
}
segment = segment->prev1(Ms::SegmentType::ChordRest);
// if the segment has a rest in this track, stop going back
2021-09-02 15:26:45 +02:00
EngravingItem* e = segment ? segment->element(track) : 0;
if (e && !e->isChord()) {
break;
}
}
// one-chord melisma?
// if still at melisma initial chord and there is a valid next chord (if not,
// there will be no melisma anyway), set a temporary melisma duration
if (fromLyrics == lyrics && nextSegment) {
score()->startCmd();
lyrics->undoChangeProperty(Ms::Pid::LYRIC_TICKS, Fraction::fromTicks(Ms::Lyrics::TEMP_MELISMA_TICKS));
score()->setLayoutAll();
score()->endCmd();
}
if (nextSegment == 0) {
score()->startCmd();
if (fromLyrics) {
switch (fromLyrics->syllabic()) {
case Ms::Lyrics::Syllabic::SINGLE:
case Ms::Lyrics::Syllabic::END:
break;
default:
fromLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::END));
break;
}
if (fromLyrics->segment()->tick() < endTick) {
fromLyrics->undoChangeProperty(Ms::Pid::LYRIC_TICKS, endTick - fromLyrics->segment()->tick());
}
}
if (fromLyrics) {
score()->select(fromLyrics, SelectType::SINGLE, 0);
}
score()->setLayoutAll();
score()->endCmd();
return;
}
// if a place for a new lyrics has been found, create a lyrics there
score()->startCmd();
ChordRest* cr = toChordRest(nextSegment->element(track));
Ms::Lyrics* toLyrics = cr->lyrics(verse, placement);
bool newLyrics = (toLyrics == 0);
if (!toLyrics) {
toLyrics = Factory::createLyrics(cr);
toLyrics->setTrack(track);
toLyrics->setParent(cr);
toLyrics->setNo(verse);
toLyrics->setPlacement(placement);
toLyrics->setPropertyFlags(Ms::Pid::PLACEMENT, pFlags);
toLyrics->setSyllabic(Ms::Lyrics::Syllabic::SINGLE);
}
// as we arrived at toLyrics by an underscore, it cannot have syllabic dashes before
else if (toLyrics->syllabic() == Ms::Lyrics::Syllabic::MIDDLE) {
toLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::BEGIN));
} else if (toLyrics->syllabic() == Ms::Lyrics::Syllabic::END) {
toLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::SINGLE));
}
if (fromLyrics) {
// as we moved away from fromLyrics by an underscore,
// it can be isolated or terminal but cannot have dashes after
switch (fromLyrics->syllabic()) {
case Ms::Lyrics::Syllabic::SINGLE:
case Ms::Lyrics::Syllabic::END:
break;
default:
fromLyrics->undoChangeProperty(Ms::Pid::SYLLABIC, int(Ms::Lyrics::Syllabic::END));
break;
}
// for the same reason, if it has a melisma, this cannot extend beyond toLyrics
if (fromLyrics->segment()->tick() < endTick) {
fromLyrics->undoChangeProperty(Ms::Pid::LYRIC_TICKS, endTick - fromLyrics->segment()->tick());
}
}
if (newLyrics) {
score()->undoAddElement(toLyrics);
}
score()->endCmd();
score()->select(toLyrics, SelectType::SINGLE, 0);
startEditText(toLyrics, PointF());
toLyrics->selectAll(toLyrics->cursor());
}
void NotationInteraction::addLyricsVerse()
{
if (!m_editData.element || !m_editData.element->isLyrics()) {
qWarning("nextLyricVerse called with invalid current element");
return;
}
Ms::Lyrics* lyrics = toLyrics(m_editData.element);
endEditText();
score()->startCmd();
int newVerse;
newVerse = lyrics->no() + 1;
Ms::Lyrics* oldLyrics = lyrics;
lyrics = Factory::createLyrics(oldLyrics->chordRest());
lyrics->setTrack(oldLyrics->track());
lyrics->setParent(oldLyrics->chordRest());
lyrics->setPlacement(oldLyrics->placement());
lyrics->setPropertyFlags(Ms::Pid::PLACEMENT, oldLyrics->propertyFlags(Ms::Pid::PLACEMENT));
lyrics->setNo(newVerse);
score()->undoAddElement(lyrics);
score()->endCmd();
score()->select(lyrics, SelectType::SINGLE, 0);
startEditText(lyrics, PointF());
}
2021-08-07 13:08:21 +02:00
Ms::Harmony* NotationInteraction::editedHarmony() const
{
Harmony* harmony = static_cast<Harmony*>(m_editData.element);
if (!harmony) {
return nullptr;
}
if (!harmony->parent() || !harmony->parent()->isSegment()) {
qDebug("no segment parent");
return nullptr;
}
return harmony;
}
2021-12-16 15:56:17 +01:00
Ms::Harmony* NotationInteraction::findHarmonyInSegment(const Ms::Segment* segment, int track, Ms::TextStyleType textStyleType) const
{
for (Ms::EngravingItem* e : segment->annotations()) {
2021-12-16 15:56:17 +01:00
if (e->isHarmony() && e->track() == track && toHarmony(e)->textStyleType() == textStyleType) {
return toHarmony(e);
}
}
return nullptr;
}
Ms::Harmony* NotationInteraction::createHarmony(Ms::Segment* segment, int track, Ms::HarmonyType type) const
{
Ms::Harmony* harmony = Factory::createHarmony(score()->dummy()->segment());
harmony->setScore(score());
harmony->setParent(segment);
harmony->setTrack(track);
harmony->setHarmonyType(type);
return harmony;
}
void NotationInteraction::startEditText(Ms::TextBase* text)
{
doEndEditElement();
select({ text }, SelectType::SINGLE);
startEditText(text, PointF());
text->cursor()->moveCursorToEnd();
}
bool NotationInteraction::needEndTextEdit() const
{
if (isTextEditingStarted()) {
const Ms::TextBase* text = Ms::toTextBase(m_editData.element);
return !text || !text->cursor()->editing();
}
return false;
}
2021-08-07 13:08:21 +02:00
void NotationInteraction::toggleFontStyle(Ms::FontStyle style)
{
if (!m_editData.element || !m_editData.element->isTextBase()) {
2021-08-07 13:08:21 +02:00
qWarning("toggleFontStyle called with invalid current element");
return;
}
Ms::TextBase* text = toTextBase(m_editData.element);
2021-08-07 13:08:21 +02:00
int currentStyle = text->getProperty(Ms::Pid::FONT_STYLE).toInt();
score()->startCmd();
text->undoChangeProperty(Ms::Pid::FONT_STYLE, PropertyValue::fromValue(
currentStyle ^ static_cast<int>(style)), Ms::PropertyFlags::UNSTYLED);
2021-08-07 13:08:21 +02:00
score()->endCmd();
notifyAboutTextEditingChanged();
}
void NotationInteraction::toggleBold()
{
toggleFontStyle(Ms::FontStyle::Bold);
}
void NotationInteraction::toggleItalic()
{
toggleFontStyle(Ms::FontStyle::Italic);
}
void NotationInteraction::toggleUnderline()
{
toggleFontStyle(Ms::FontStyle::Underline);
}
void NotationInteraction::toggleStrike()
{
toggleFontStyle(Ms::FontStyle::Strike);
}
template<typename P>
void NotationInteraction::execute(void (Ms::Score::* function)(P), P param)
{
startEdit();
(score()->*function)(param);
apply();
notifyAboutNotationChanged();
}
void NotationInteraction::toggleArticulation(Ms::SymId symId)
{
execute(&Ms::Score::toggleArticulation, symId);
}
void NotationInteraction::toggleAutoplace(bool all)
{
execute(&Ms::Score::cmdToggleAutoplace, all);
}
void NotationInteraction::insertClef(Ms::ClefType clef)
{
execute(&Ms::Score::cmdInsertClef, clef);
}
void NotationInteraction::changeAccidental(Ms::AccidentalType accidental)
{
execute(&Ms::Score::changeAccidental, accidental);
}
void NotationInteraction::transposeSemitone(int steps)
{
execute(&Ms::Score::transposeSemitone, steps);
}
void NotationInteraction::transposeDiatonicAlterations(Ms::TransposeDirection direction)
{
execute(&Ms::Score::transposeDiatonicAlterations, direction);
}
void NotationInteraction::toggleGlobalOrLocalInsert()
{
score()->inputState().setInsertMode(!score()->inputState().insertMode());
}
void NotationInteraction::getLocation()
{
auto* e = score()->selection().element();
if (!e) {
// no current selection - restore lost selection
e = score()->selection().currentCR();
if (e && e->isChord()) {
e = toChord(e)->upNote();
}
}
if (!e) {
e = score()->firstElement(false);
}
if (e) {
if (e->type() == ElementType::NOTE || e->type() == ElementType::HARMONY) {
score()->setPlayNote(true);
}
select({ e }, SelectType::SINGLE);
}
}
void NotationInteraction::execute(void (Ms::Score::* function)())
{
startEdit();
(score()->*function)();
apply();
notifyAboutNotationChanged();
}