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) 2022 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 "abstractelementpopupmodel.h"
24 :
25 : #include "engraving/dom/property.h"
26 : #include "engraving/dom/score.h"
27 :
28 : #include "notation/inotation.h"
29 : #include "notation/inotationinteraction.h"
30 : #include "notation/inotationselection.h"
31 : #include "notation/inotationundostack.h"
32 : #include "notation/inotationviewstate.h"
33 :
34 : #include "elementpopups/partialtiepopupmodel.h"
35 : #include "elementpopups/shadownotepopupmodel.h"
36 :
37 : #include "log.h"
38 :
39 : using namespace mu::notation;
40 :
41 : static const QMap<mu::engraving::ElementType, PopupModelType> ELEMENT_POPUP_TYPES = {
42 : { mu::engraving::ElementType::HARP_DIAGRAM, PopupModelType::TYPE_HARP_DIAGRAM },
43 : { mu::engraving::ElementType::CAPO, PopupModelType::TYPE_CAPO },
44 : { mu::engraving::ElementType::STRING_TUNINGS, PopupModelType::TYPE_STRING_TUNINGS },
45 : { mu::engraving::ElementType::SOUND_FLAG, PopupModelType::TYPE_SOUND_FLAG },
46 : { mu::engraving::ElementType::STAFF_VISIBILITY_INDICATOR, PopupModelType::TYPE_STAFF_VISIBILITY },
47 : { mu::engraving::ElementType::DYNAMIC, PopupModelType::TYPE_DYNAMIC },
48 : { mu::engraving::ElementType::TEXT, PopupModelType::TYPE_TEXT },
49 : { mu::engraving::ElementType::STAFF_TEXT, PopupModelType::TYPE_TEXT },
50 : { mu::engraving::ElementType::SYSTEM_TEXT, PopupModelType::TYPE_TEXT },
51 : { mu::engraving::ElementType::EXPRESSION, PopupModelType::TYPE_TEXT },
52 : { mu::engraving::ElementType::PLAYTECH_ANNOTATION, PopupModelType::TYPE_TEXT },
53 : { mu::engraving::ElementType::REHEARSAL_MARK, PopupModelType::TYPE_TEXT },
54 : { mu::engraving::ElementType::INSTRUMENT_CHANGE, PopupModelType::TYPE_TEXT },
55 : { mu::engraving::ElementType::FINGERING, PopupModelType::TYPE_TEXT },
56 : { mu::engraving::ElementType::STICKING, PopupModelType::TYPE_TEXT },
57 : { mu::engraving::ElementType::LYRICS, PopupModelType::TYPE_TEXT },
58 : { mu::engraving::ElementType::FIGURED_BASS, PopupModelType::TYPE_TEXT },
59 : { mu::engraving::ElementType::TEMPO_TEXT, PopupModelType::TYPE_TEXT },
60 : { mu::engraving::ElementType::PLAY_COUNT_TEXT, PopupModelType::TYPE_TEXT },
61 : { mu::engraving::ElementType::TIE_SEGMENT, PopupModelType::TYPE_PARTIAL_TIE },
62 : { mu::engraving::ElementType::PARTIAL_TIE_SEGMENT, PopupModelType::TYPE_PARTIAL_TIE },
63 : { mu::engraving::ElementType::SHADOW_NOTE, PopupModelType::TYPE_SHADOW_NOTE },
64 : { mu::engraving::ElementType::STAVE_SHARING_CHANGE, PopupModelType::TYPE_STAVE_SHARING_CHANGE },
65 : };
66 :
67 : static const QHash<PopupModelType, mu::engraving::ElementTypeSet> POPUP_DEPENDENT_ELEMENT_TYPES = {
68 : { PopupModelType::TYPE_HARP_DIAGRAM, { mu::engraving::ElementType::HARP_DIAGRAM } },
69 : { PopupModelType::TYPE_CAPO, { mu::engraving::ElementType::CAPO } },
70 : { PopupModelType::TYPE_STRING_TUNINGS, { mu::engraving::ElementType::STRING_TUNINGS } },
71 : { PopupModelType::TYPE_SOUND_FLAG, { mu::engraving::ElementType::SOUND_FLAG, mu::engraving::ElementType::STAFF_TEXT } },
72 : { PopupModelType::TYPE_STAFF_VISIBILITY, { mu::engraving::ElementType::STAFF_VISIBILITY_INDICATOR } },
73 : { PopupModelType::TYPE_DYNAMIC, { mu::engraving::ElementType::DYNAMIC } },
74 : { PopupModelType::TYPE_TEXT,
75 : { mu::engraving::ElementType::TEXT,
76 : mu::engraving::ElementType::STAFF_TEXT,
77 : mu::engraving::ElementType::SYSTEM_TEXT,
78 : mu::engraving::ElementType::EXPRESSION,
79 : mu::engraving::ElementType::PLAYTECH_ANNOTATION,
80 : mu::engraving::ElementType::REHEARSAL_MARK,
81 : mu::engraving::ElementType::INSTRUMENT_CHANGE,
82 : mu::engraving::ElementType::FINGERING,
83 : mu::engraving::ElementType::STICKING,
84 : mu::engraving::ElementType::LYRICS,
85 : mu::engraving::ElementType::FIGURED_BASS,
86 : mu::engraving::ElementType::TEMPO_TEXT,
87 : mu::engraving::ElementType::PLAY_COUNT_TEXT } },
88 : { PopupModelType::TYPE_PARTIAL_TIE, { mu::engraving::ElementType::PARTIAL_TIE_SEGMENT, mu::engraving::ElementType::TIE_SEGMENT } },
89 : { PopupModelType::TYPE_STAVE_SHARING_CHANGE, { mu::engraving::ElementType::STAVE_SHARING_CHANGE } },
90 : };
91 :
92 0 : AbstractElementPopupModel::AbstractElementPopupModel(PopupModelType modelType, QObject* parent)
93 0 : : QObject(parent), muse::Contextable(muse::iocCtxForQmlObject(this)), m_modelType(modelType)
94 : {
95 0 : }
96 :
97 0 : PopupModelType AbstractElementPopupModel::modelType() const
98 : {
99 0 : return m_modelType;
100 : }
101 :
102 0 : QRect AbstractElementPopupModel::itemRect() const
103 : {
104 0 : return m_itemRect;
105 : }
106 :
107 0 : bool AbstractElementPopupModel::hasElementEditPopup(const EngravingItem* element)
108 : {
109 0 : if (!element) {
110 0 : return false;
111 : }
112 :
113 0 : const PopupModelType modelType = modelTypeFromElement(element->type());
114 0 : if (modelType == PopupModelType::TYPE_UNDEFINED) {
115 0 : return false;
116 : }
117 :
118 0 : if (modelType == PopupModelType::TYPE_TEXT) {
119 : // Text style popup is only opened when making a selection during text editing
120 0 : return false;
121 : }
122 :
123 0 : switch (modelType) {
124 0 : case PopupModelType::TYPE_PARTIAL_TIE:
125 0 : return PartialTiePopupModel::canOpen(element);
126 0 : case PopupModelType::TYPE_SHADOW_NOTE:
127 0 : return ShadowNotePopupModel::canOpen(element);
128 0 : default:
129 0 : return true;
130 : }
131 : }
132 :
133 1 : bool AbstractElementPopupModel::hasTextStylePopup(const EngravingItem* element)
134 : {
135 1 : if (!element) {
136 1 : return false;
137 : }
138 :
139 0 : const PopupModelType modelType = modelTypeFromElement(element->type());
140 0 : return modelType == PopupModelType::TYPE_TEXT;
141 : }
142 :
143 0 : PopupModelType AbstractElementPopupModel::modelTypeFromElement(const engraving::ElementType& elementType)
144 : {
145 0 : return ELEMENT_POPUP_TYPES.value(elementType, PopupModelType::TYPE_UNDEFINED);
146 : }
147 :
148 0 : muse::PointF AbstractElementPopupModel::fromLogical(muse::PointF point) const
149 : {
150 0 : return currentNotation() ? currentNotation()->viewState()->matrix().map(point) : muse::PointF();
151 : }
152 :
153 0 : muse::RectF AbstractElementPopupModel::fromLogical(muse::RectF rect) const
154 : {
155 0 : return currentNotation() ? currentNotation()->viewState()->matrix().map(rect) : muse::RectF();
156 : }
157 :
158 0 : INotationUndoStackPtr AbstractElementPopupModel::undoStack() const
159 : {
160 0 : return currentNotation() ? currentNotation()->undoStack() : nullptr;
161 : }
162 :
163 0 : void AbstractElementPopupModel::beginCommand(const muse::TranslatableString& actionName)
164 : {
165 0 : if (undoStack()) {
166 0 : undoStack()->prepareChanges(actionName);
167 : }
168 0 : }
169 :
170 0 : void AbstractElementPopupModel::beginMultiCommands(const muse::TranslatableString& actionName)
171 : {
172 0 : beginCommand(actionName);
173 :
174 0 : if (undoStack()) {
175 0 : undoStack()->lock();
176 : }
177 0 : }
178 :
179 0 : void AbstractElementPopupModel::endCommand()
180 : {
181 0 : if (undoStack()) {
182 0 : undoStack()->commitChanges();
183 : }
184 0 : }
185 :
186 0 : void AbstractElementPopupModel::endMultiCommands()
187 : {
188 0 : if (undoStack()) {
189 0 : undoStack()->unlock();
190 : }
191 :
192 0 : endCommand();
193 0 : }
194 :
195 0 : void AbstractElementPopupModel::updateNotation()
196 : {
197 0 : if (!currentNotation()) {
198 0 : return;
199 : }
200 :
201 0 : currentNotation()->notationChanged().send(muse::RectF());
202 : }
203 :
204 0 : INotationPtr AbstractElementPopupModel::currentNotation() const
205 : {
206 0 : return projectSession()->currentNotation();
207 : }
208 :
209 0 : void AbstractElementPopupModel::changeItemProperty(mu::engraving::Pid id, const engraving::PropertyValue& value)
210 : {
211 0 : IF_ASSERT_FAILED(m_item) {
212 0 : return;
213 : }
214 :
215 0 : mu::engraving::PropertyFlags flags = m_item->propertyFlags(id);
216 0 : if (flags == mu::engraving::PropertyFlags::STYLED) {
217 0 : flags = mu::engraving::PropertyFlags::UNSTYLED;
218 : }
219 :
220 0 : beginCommand(muse::TranslatableString("undoableAction", "Edit %1").arg(mu::engraving::propertyUserName(id)));
221 0 : m_item->undoChangeProperty(id, value, flags);
222 0 : endCommand();
223 0 : updateNotation();
224 : }
225 :
226 0 : void AbstractElementPopupModel::changeItemProperty(mu::engraving::Pid id, const engraving::PropertyValue& value,
227 : mu::engraving::PropertyFlags flags)
228 : {
229 0 : IF_ASSERT_FAILED(m_item) {
230 0 : return;
231 : }
232 :
233 0 : beginCommand(muse::TranslatableString("undoableAction", "Edit %1").arg(mu::engraving::propertyUserName(id)));
234 0 : m_item->undoChangeProperty(id, value, flags);
235 0 : endCommand();
236 0 : updateNotation();
237 : }
238 :
239 0 : INotationInteractionPtr AbstractElementPopupModel::interaction() const
240 : {
241 0 : INotationPtr notation = projectSession()->currentNotation();
242 0 : return notation ? notation->interaction() : nullptr;
243 0 : }
244 :
245 0 : INotationSelectionPtr AbstractElementPopupModel::selection() const
246 : {
247 0 : INotationInteractionPtr interaction = this->interaction();
248 0 : return interaction ? interaction->selection() : nullptr;
249 0 : }
250 :
251 0 : void AbstractElementPopupModel::init()
252 : {
253 0 : m_item = nullptr;
254 :
255 0 : INotationSelectionPtr selection = this->selection();
256 0 : if (!selection) {
257 0 : return;
258 : }
259 :
260 0 : INotationUndoStackPtr undoStack = this->undoStack();
261 0 : if (!undoStack) {
262 0 : return;
263 : }
264 :
265 0 : m_item = selection->element();
266 :
267 0 : undoStack->changesChannel().onReceive(this, [this] (const engraving::ScoreChanges& changes) {
268 0 : if (ignoreTextEditingChanges() && changes.isTextEditing) {
269 0 : return;
270 : }
271 :
272 0 : for (ElementType type : dependentElementTypes()) {
273 0 : if (muse::contains(changes.changedTypes, type)) {
274 0 : emit dataChanged();
275 0 : updateItemRect();
276 0 : return;
277 : }
278 : }
279 : });
280 :
281 0 : updateItemRect();
282 0 : }
283 :
284 0 : mu::engraving::ElementType AbstractElementPopupModel::elementType() const
285 : {
286 0 : return ELEMENT_POPUP_TYPES.key(m_modelType, ElementType::INVALID);
287 : }
288 :
289 0 : const mu::engraving::ElementTypeSet& AbstractElementPopupModel::dependentElementTypes() const
290 : {
291 0 : auto it = POPUP_DEPENDENT_ELEMENT_TYPES.find(m_modelType);
292 0 : if (it != POPUP_DEPENDENT_ELEMENT_TYPES.end()) {
293 0 : return it.value();
294 : }
295 :
296 0 : static const engraving::ElementTypeSet dummy;
297 0 : return dummy;
298 : }
299 :
300 0 : void AbstractElementPopupModel::updateItemRect()
301 : {
302 0 : const QRect rect = m_item ? fromLogical(m_item->canvasBoundingRect()).toQRect() : QRect();
303 :
304 0 : if (m_itemRect != rect) {
305 0 : m_itemRect = rect;
306 0 : emit itemRectChanged(rect);
307 : }
308 0 : }
|