Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "notegroupsexampleview.h"
24 :
25 : #include <QMimeData>
26 :
27 : #include "engraving/dom/actionicon.h"
28 : #include "engraving/dom/chord.h"
29 : #include "engraving/dom/engravingitem.h"
30 : #include "engraving/dom/factory.h"
31 : #include "engraving/dom/score.h"
32 : #include "engraving/rw/rwregister.h"
33 :
34 : using namespace mu::palette;
35 : using namespace mu::notation;
36 : using namespace mu::engraving;
37 :
38 0 : NoteGroupsExampleView::NoteGroupsExampleView(QWidget* parent)
39 0 : : notation::ExampleView(parent)
40 : {
41 0 : setAcceptDrops(true);
42 0 : }
43 :
44 0 : void NoteGroupsExampleView::dragEnterEvent(QDragEnterEvent* event)
45 : {
46 0 : const QMimeData* d = event->mimeData();
47 0 : if (d->hasFormat(mimeSymbolFormat)) {
48 0 : event->acceptProposedAction();
49 :
50 0 : QByteArray a = d->data(mimeSymbolFormat);
51 :
52 : // LOGD("NoteGroupsExampleView::dragEnterEvent Symbol: <%s>", a.data());
53 :
54 0 : XmlReader e(muse::ByteArray::fromQByteArrayNoCopy(a));
55 0 : PointF dragOffset;
56 0 : Fraction duration; // dummy
57 0 : ElementType type = EngravingItem::readType(e, &dragOffset, &duration);
58 :
59 0 : m_dragElement = Factory::createItem(type, m_score->dummy());
60 0 : if (m_dragElement) {
61 0 : rw::RWRegister::reader()->readItem(m_dragElement, e);
62 0 : engravingRender()->layoutItem(m_dragElement);
63 : }
64 0 : return;
65 0 : }
66 : }
67 :
68 0 : void NoteGroupsExampleView::dragLeaveEvent(QDragLeaveEvent*)
69 : {
70 0 : if (m_dragElement) {
71 0 : delete m_dragElement;
72 0 : m_dragElement = 0;
73 : }
74 0 : setDropTarget(0);
75 0 : }
76 :
77 : struct MoveContext
78 : {
79 : PointF pos;
80 : mu::engraving::Score* score = nullptr;
81 : };
82 :
83 0 : void NoteGroupsExampleView::dragMoveEvent(QDragMoveEvent* event)
84 : {
85 0 : event->acceptProposedAction();
86 :
87 0 : if (!m_dragElement || !m_dragElement->isActionIcon()) {
88 0 : return;
89 : }
90 :
91 0 : EngravingItem* newDropTarget = nullptr;
92 :
93 0 : PointF position = toLogical(event->position());
94 0 : std::vector<EngravingItem*> el = elementsAt(position);
95 :
96 0 : for (EngravingItem* e : el) {
97 0 : if (e->isNote()) {
98 0 : newDropTarget = e;
99 0 : break;
100 : }
101 : }
102 :
103 0 : setDropTarget(newDropTarget);
104 :
105 0 : MoveContext ctx{ position, m_score };
106 0 : auto moveElement = [&](EngravingItem* e) {
107 0 : if (!e->visible() && !e->score()->isShowInvisible()) {
108 0 : return;
109 : }
110 0 : ctx.score->addRefresh(e->canvasBoundingRect());
111 0 : e->setPos(ctx.pos);
112 0 : ctx.score->addRefresh(e->canvasBoundingRect());
113 0 : };
114 0 : m_dragElement->scanElements(moveElement);
115 0 : m_score->update();
116 0 : return;
117 0 : }
118 :
119 0 : void NoteGroupsExampleView::setDropTarget(EngravingItem* el)
120 : {
121 0 : if (m_dropTarget != el) {
122 0 : if (m_dropTarget) {
123 0 : m_dropTarget->setDropTarget(false);
124 0 : m_dropTarget = 0;
125 : }
126 0 : m_dropTarget = el;
127 0 : if (m_dropTarget) {
128 0 : m_dropTarget->setDropTarget(true);
129 : }
130 : }
131 0 : if (!m_dropAnchor.isNull()) {
132 0 : QRectF r;
133 0 : r.setTopLeft(m_dropAnchor.p1());
134 0 : r.setBottomRight(m_dropAnchor.p2());
135 0 : m_dropAnchor = QLineF();
136 : }
137 :
138 0 : update();
139 0 : }
140 :
141 0 : void NoteGroupsExampleView::dropEvent(QDropEvent* event)
142 : {
143 0 : PointF position = toLogical(event->position());
144 :
145 0 : if (!m_dragElement) {
146 0 : return;
147 : }
148 :
149 0 : if (!m_dragElement->isActionIcon()) {
150 0 : delete m_dragElement;
151 0 : m_dragElement = nullptr;
152 0 : return;
153 : }
154 :
155 0 : for (EngravingItem* e : elementsAt(position)) {
156 0 : if (e->isNote()) {
157 0 : ActionIcon* icon = toActionIcon(m_dragElement);
158 0 : Chord* chord = toNote(e)->chord();
159 0 : emit beamPropertyDropped(chord, icon);
160 0 : break;
161 : }
162 0 : }
163 :
164 0 : event->acceptProposedAction();
165 0 : delete m_dragElement;
166 0 : m_dragElement = nullptr;
167 0 : setDropTarget(nullptr);
168 : }
169 :
170 0 : void NoteGroupsExampleView::mousePressEvent(QMouseEvent* event)
171 : {
172 0 : ExampleView::mousePressEvent(event);
173 :
174 0 : PointF position = toLogical(event->position());
175 :
176 0 : for (EngravingItem* e : elementsAt(position)) {
177 0 : if (e->isNote()) {
178 0 : emit noteClicked(toNote(e));
179 0 : break;
180 : }
181 0 : }
182 0 : }
|