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 "keysig.h"
24 :
25 : #include "types/typesconv.h"
26 :
27 : #include "masterscore.h"
28 : #include "measure.h"
29 : #include "score.h"
30 : #include "segment.h"
31 : #include "staff.h"
32 : #include "part.h"
33 :
34 : #include "editing/editkeysig.h"
35 : #include "editing/transaction/transaction.h"
36 : #include "editing/transpose.h"
37 :
38 : #include "log.h"
39 :
40 : using namespace mu;
41 : using namespace mu::engraving;
42 :
43 : namespace mu::engraving {
44 : //---------------------------------------------------------
45 : // KeySig
46 : //---------------------------------------------------------
47 :
48 7666 : KeySig::KeySig(DummyParentOr<Segment> s)
49 7666 : : EngravingItem(ElementType::KEYSIG, s, ElementFlag::ON_STAFF)
50 : {
51 7666 : m_showCourtesy = true;
52 7666 : m_hideNaturals = false;
53 7666 : }
54 :
55 224 : KeySig::KeySig(const KeySig& k)
56 224 : : EngravingItem(k)
57 : {
58 224 : m_showCourtesy = k.m_showCourtesy;
59 224 : m_sig = k.m_sig;
60 224 : m_hideNaturals = false;
61 224 : }
62 :
63 : //---------------------------------------------------------
64 : // mag
65 : //---------------------------------------------------------
66 :
67 565981 : double KeySig::mag() const
68 : {
69 565981 : return staff() ? staff()->staffMag(this) : 1.0;
70 : }
71 :
72 : //---------------------------------------------------------
73 : // acceptDrop
74 : //---------------------------------------------------------
75 :
76 0 : bool KeySig::acceptDrop(EditData& data) const
77 : {
78 0 : return data.dropElement->isKeySig();
79 : }
80 :
81 : //---------------------------------------------------------
82 : // drop
83 : //---------------------------------------------------------
84 :
85 0 : EngravingItem* KeySig::drop(Transaction& tx, EditData& data)
86 : {
87 0 : KeySig* ks = toKeySig(data.dropElement);
88 0 : if (!ks->isKeySig()) {
89 0 : delete ks;
90 0 : return 0;
91 : }
92 0 : KeySigEvent k = ks->keySigEvent();
93 0 : delete ks;
94 0 : if (data.modifiers & ControlModifier) {
95 : // apply only to this stave
96 0 : if (!(k == keySigEvent())) {
97 0 : EditKeySig::undoChangeKeySig(tx, score(), staff(), tick(), k);
98 : }
99 : } else {
100 : // apply to all staves:
101 0 : for (Staff* s : score()->masterScore()->staves()) {
102 0 : EditKeySig::undoChangeKeySig(tx, score(), s, tick(), k);
103 : }
104 : }
105 0 : return this;
106 0 : }
107 :
108 : //---------------------------------------------------------
109 : // setKey
110 : //---------------------------------------------------------
111 :
112 910 : void KeySig::setKey(Key concertKey)
113 : {
114 910 : KeySigEvent e;
115 910 : e.setConcertKey(concertKey);
116 910 : if (staff() && !style().styleB(Sid::concertPitch)) {
117 342 : const Part* part = staff()->originPart(tick());
118 342 : Interval v = part->instrument(tick())->transpose();
119 342 : if (!v.isZero()) {
120 292 : v.flip();
121 292 : Key transposedKey = Transpose::transposeKey(concertKey, v, part->preferSharpFlat());
122 292 : e.setKey(transposedKey);
123 : }
124 : }
125 910 : setKeySigEvent(e);
126 910 : }
127 :
128 : //---------------------------------------------------------
129 : // setKey
130 : //---------------------------------------------------------
131 :
132 4 : void KeySig::setKey(Key concertKey, Key transposedKey)
133 : {
134 4 : KeySigEvent e;
135 4 : e.setConcertKey(concertKey);
136 4 : e.setKey(transposedKey);
137 4 : setKeySigEvent(e);
138 4 : }
139 :
140 : //---------------------------------------------------------
141 : // operator==
142 : //---------------------------------------------------------
143 :
144 10 : bool KeySig::operator==(const KeySig& k) const
145 : {
146 10 : return m_sig == k.m_sig;
147 : }
148 :
149 : //---------------------------------------------------------
150 : // isChange
151 : //---------------------------------------------------------
152 :
153 19 : bool KeySig::isChange() const
154 : {
155 19 : if (!staff()) {
156 0 : return false;
157 : }
158 19 : if (!segment() || segment()->segmentType() != SegmentType::KeySig) {
159 0 : return false;
160 : }
161 19 : const Fraction keyTick = tick();
162 19 : return staff()->currentKeyTick(keyTick) == keyTick;
163 : }
164 :
165 : //---------------------------------------------------------
166 : // changeKeySigEvent
167 : //---------------------------------------------------------
168 :
169 0 : void KeySig::changeKeySigEvent(const KeySigEvent& t)
170 : {
171 0 : if (m_sig == t) {
172 0 : return;
173 : }
174 0 : setKeySigEvent(t);
175 : }
176 :
177 78248 : PointF KeySig::staffOffset() const
178 : {
179 78248 : const Segment* seg = segment();
180 78248 : const Measure* meas = seg ? seg->measure() : nullptr;
181 78248 : if (meas && meas->endTick() == tick()) {
182 : // Courtesy key sig should be adjusted by the following staffType's offset
183 1933 : return EngravingItem::staffOffset();
184 : }
185 76315 : return PointF(0.0, 0.0);
186 : }
187 :
188 32315 : PropertyDelegate KeySig::resolvePropertyDelegate(Pid propertyId) const
189 : {
190 32315 : if (!_isCourtesy) {
191 32315 : return PropertyDelegate();
192 : }
193 0 : switch (propertyId) {
194 0 : case Pid::KEY:
195 : case Pid::KEY_CONCERT:
196 : case Pid::SHOW_COURTESY:
197 : case Pid::KEYSIG_MODE:
198 : case Pid::JIANPU_NUMBERING:
199 : case Pid::JIANPU_TONIC_KEY:
200 : case Pid::JIANPU_TONIC_MODE:
201 : {
202 0 : Segment* thisSeg = segment();
203 0 : Segment* nextKSSeg = thisSeg ? thisSeg->next1(SegmentType::KeySig) : nullptr;
204 0 : if (nextKSSeg && nextKSSeg->tick() == thisSeg->tick()) {
205 0 : EngravingItem* next = nextKSSeg->element(track());
206 0 : return next ? PropertyDelegate(next) : PropertyDelegate();
207 : }
208 0 : break;
209 : }
210 0 : default:
211 0 : break;
212 : }
213 :
214 0 : return PropertyDelegate();
215 : }
216 :
217 : //---------------------------------------------------------
218 : // getProperty
219 : //---------------------------------------------------------
220 :
221 13670 : PropertyValue KeySig::getProperty(Pid propertyId) const
222 : {
223 13670 : if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
224 0 : return d.delegate ? d.delegate->getProperty(propertyId) : propertyDefault(propertyId);
225 : }
226 13670 : switch (propertyId) {
227 0 : case Pid::KEY:
228 0 : return int(key());
229 0 : case Pid::KEY_CONCERT:
230 0 : return int(concertKey());
231 1231 : case Pid::SHOW_COURTESY:
232 1231 : return showCourtesy();
233 11 : case Pid::KEYSIG_MODE:
234 11 : return mode();
235 28 : case Pid::JIANPU_NUMBERING:
236 28 : return jianpuNumbering();
237 27 : case Pid::JIANPU_TONIC_KEY:
238 27 : return jianpuTonicKey();
239 25 : case Pid::JIANPU_TONIC_MODE:
240 25 : return jianpuTonicMode();
241 1231 : case Pid::IS_COURTESY:
242 1231 : return _isCourtesy;
243 11117 : default:
244 11117 : return EngravingItem::getProperty(propertyId);
245 : }
246 : }
247 :
248 : //---------------------------------------------------------
249 : // setProperty
250 : //---------------------------------------------------------
251 :
252 44 : bool KeySig::setProperty(Pid propertyId, const PropertyValue& v)
253 : {
254 44 : if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
255 0 : return d.delegate ? d.delegate->setProperty(propertyId, v) : false;
256 : }
257 44 : switch (propertyId) {
258 0 : case Pid::KEY:
259 0 : if (generated()) {
260 0 : return false;
261 : }
262 0 : setKey(m_sig.concertKey(), Key(v.toInt()));
263 0 : break;
264 0 : case Pid::KEY_CONCERT:
265 0 : if (generated()) {
266 0 : return false;
267 : }
268 0 : setKey(Key(v.toInt()));
269 0 : break;
270 4 : case Pid::SHOW_COURTESY:
271 4 : if (generated()) {
272 0 : return false;
273 : }
274 4 : setShowCourtesy(v.toBool());
275 4 : break;
276 6 : case Pid::KEYSIG_MODE:
277 6 : if (generated()) {
278 1 : return false;
279 : }
280 5 : setMode(v.value<KeyMode>());
281 5 : staff()->setKey(tick(), keySigEvent());
282 5 : break;
283 9 : case Pid::JIANPU_NUMBERING:
284 9 : if (generated()) {
285 1 : return false;
286 : }
287 8 : setJianpuNumbering(v.value<JianpuNumbering>());
288 8 : staff()->setKey(tick(), keySigEvent());
289 8 : break;
290 9 : case Pid::JIANPU_TONIC_KEY:
291 9 : if (generated()) {
292 1 : return false;
293 : }
294 8 : setJianpuTonicKey(v.value<Key>());
295 8 : staff()->setKey(tick(), keySigEvent());
296 8 : break;
297 9 : case Pid::JIANPU_TONIC_MODE:
298 9 : if (generated()) {
299 1 : return false;
300 : }
301 8 : setJianpuTonicMode(v.value<KeyMode>());
302 8 : staff()->setKey(tick(), keySigEvent());
303 8 : break;
304 0 : case Pid::IS_COURTESY:
305 0 : _isCourtesy = v.toBool();
306 0 : break;
307 7 : default:
308 7 : if (!EngravingItem::setProperty(propertyId, v)) {
309 0 : return false;
310 : }
311 7 : break;
312 : }
313 40 : triggerLayoutAll();
314 40 : setGenerated(false);
315 40 : return true;
316 : }
317 :
318 : //---------------------------------------------------------
319 : // undoChangeProperty
320 : //---------------------------------------------------------
321 :
322 27 : void KeySig::undoChangeProperty(Pid id, const PropertyValue& v, PropertyFlags ps)
323 : {
324 27 : if (const PropertyDelegate d = resolvePropertyDelegate(id); d.hasDelegate) {
325 0 : EngravingObject* e = d.delegate;
326 0 : if (!e) {
327 0 : return;
328 : }
329 : /* If this is a courtesy, we must change the property of the _real_ key signature,
330 : * and we want to avoid EngravingObject::undoChangeProperty unsetting GENERATED
331 : * unless something really has changed about the courtesy itself: */
332 0 : e->undoChangeProperty(id, v, ps);
333 : // If removing the courtesy, deselect it first:
334 0 : if (id == Pid::SHOW_COURTESY && _isCourtesy && !v.toBool() && selected() && e->isKeySig()) {
335 0 : score()->deselect(this);
336 0 : score()->select(toEngravingItem(e), SelectType::ADD, staffIdx());
337 : }
338 0 : return;
339 : }
340 27 : EngravingItem::undoChangeProperty(id, v, ps);
341 : }
342 :
343 : //---------------------------------------------------------
344 : // propertyDefault
345 : //---------------------------------------------------------
346 :
347 13544 : PropertyValue KeySig::propertyDefault(Pid id) const
348 : {
349 13544 : switch (id) {
350 0 : case Pid::KEY:
351 0 : return int(Key::INVALID);
352 0 : case Pid::KEY_CONCERT:
353 0 : return int(Key::INVALID);
354 1231 : case Pid::SHOW_COURTESY:
355 1231 : return true;
356 0 : case Pid::KEYSIG_MODE:
357 0 : return KeyMode::UNKNOWN;
358 1 : case Pid::JIANPU_NUMBERING:
359 1 : return JianpuNumbering::FOLLOW_SCORE_KEY;
360 1 : case Pid::JIANPU_TONIC_KEY:
361 1 : return Key::C;
362 1 : case Pid::JIANPU_TONIC_MODE:
363 1 : return KeyMode::MAJOR;
364 1231 : case Pid::IS_COURTESY:
365 1231 : return false;
366 11079 : default:
367 11079 : return EngravingItem::propertyDefault(id);
368 : }
369 : }
370 :
371 : //---------------------------------------------------------
372 : // nextSegmentElement
373 : //---------------------------------------------------------
374 :
375 31 : EngravingItem* KeySig::nextSegmentElement()
376 : {
377 31 : return segment()->firstInNextSegments(staffIdx());
378 : }
379 :
380 : //---------------------------------------------------------
381 : // prevSegmentElement
382 : //---------------------------------------------------------
383 :
384 29 : EngravingItem* KeySig::prevSegmentElement()
385 : {
386 29 : return segment()->lastInPrevSegments(staffIdx());
387 : }
388 :
389 : //---------------------------------------------------------
390 : // accessibleInfo
391 : //---------------------------------------------------------
392 :
393 0 : String KeySig::accessibleInfo() const
394 : {
395 0 : String keySigType = TConv::translatedUserName(key(), isAtonal(), isCustom());
396 0 : return String(u"%1: %2").arg(EngravingItem::accessibleInfo(), keySigType);
397 0 : }
398 :
399 : //---------------------------------------------------------
400 : // translatedSubtypeUserName
401 : //---------------------------------------------------------
402 :
403 1 : muse::TranslatableString KeySig::subtypeUserName() const
404 : {
405 1 : return TConv::userName(key(), isAtonal(), isCustom());
406 : }
407 : }
|