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) 2025 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 "editstaff.h"
24 :
25 : #include "../dom/guitarbend.h"
26 : #include "../dom/masterscore.h"
27 : #include "../dom/measure.h"
28 : #include "../dom/score.h"
29 : #include "../dom/staff.h"
30 : #include "../dom/stafflines.h"
31 : #include "../dom/stafftype.h"
32 :
33 : using namespace mu::engraving;
34 :
35 : //---------------------------------------------------------
36 : // InsertStaff
37 : //---------------------------------------------------------
38 :
39 198 : InsertStaff::InsertStaff(Staff* p, staff_idx_t _ridx)
40 : {
41 198 : staff = p;
42 198 : ridx = _ridx;
43 198 : }
44 :
45 16 : void InsertStaff::undo()
46 : {
47 16 : staff->score()->removeStaff(staff);
48 16 : }
49 :
50 201 : void InsertStaff::redo()
51 : {
52 201 : staff->score()->insertStaff(staff, ridx);
53 201 : }
54 :
55 177 : void InsertStaff::cleanup(bool wasDone)
56 : {
57 177 : if (!wasDone) {
58 13 : delete staff;
59 13 : staff = nullptr;
60 : }
61 177 : }
62 :
63 : //---------------------------------------------------------
64 : // RemoveStaff
65 : //---------------------------------------------------------
66 :
67 34 : RemoveStaff::RemoveStaff(Staff* p)
68 : {
69 34 : staff = p;
70 34 : ridx = staff->rstaff();
71 34 : wasSystemObjectStaff = staff->isSystemObjectStaff();
72 34 : }
73 :
74 10 : void RemoveStaff::undo()
75 : {
76 10 : staff->score()->insertStaff(staff, ridx);
77 10 : if (wasSystemObjectStaff) {
78 0 : staff->score()->addSystemObjectStaff(staff);
79 : }
80 10 : }
81 :
82 35 : void RemoveStaff::redo()
83 : {
84 35 : staff->score()->removeStaff(staff);
85 35 : if (wasSystemObjectStaff) {
86 0 : staff->score()->removeSystemObjectStaff(staff);
87 : }
88 35 : }
89 :
90 19 : void RemoveStaff::cleanup(bool wasDone)
91 : {
92 19 : if (wasDone) {
93 10 : delete staff;
94 10 : staff = nullptr;
95 : }
96 19 : }
97 :
98 : //---------------------------------------------------------
99 : // AddSystemObjectStaff
100 : //---------------------------------------------------------
101 :
102 8 : AddSystemObjectStaff::AddSystemObjectStaff(Staff* s)
103 8 : : staff(s)
104 : {
105 8 : }
106 :
107 1 : void AddSystemObjectStaff::undo()
108 : {
109 1 : staff->score()->removeSystemObjectStaff(staff);
110 1 : }
111 :
112 8 : void AddSystemObjectStaff::redo()
113 : {
114 8 : staff->score()->addSystemObjectStaff(staff);
115 8 : }
116 :
117 : //---------------------------------------------------------
118 : // RemoveSystemObjectStaff
119 : //---------------------------------------------------------
120 :
121 5 : RemoveSystemObjectStaff::RemoveSystemObjectStaff(Staff* s)
122 5 : : staff(s)
123 : {
124 5 : }
125 :
126 0 : void RemoveSystemObjectStaff::undo()
127 : {
128 0 : staff->score()->addSystemObjectStaff(staff);
129 0 : }
130 :
131 5 : void RemoveSystemObjectStaff::redo()
132 : {
133 5 : staff->score()->removeSystemObjectStaff(staff);
134 5 : }
135 :
136 : //---------------------------------------------------------
137 : // InsertMStaff
138 : //---------------------------------------------------------
139 :
140 636 : InsertMStaff::InsertMStaff(Measure* m, MStaff* ms, staff_idx_t i)
141 : {
142 636 : measure = m;
143 636 : mstaff = ms;
144 636 : idx = i;
145 636 : }
146 :
147 84 : void InsertMStaff::undo()
148 : {
149 84 : measure->removeMStaff(mstaff, idx);
150 84 : }
151 :
152 701 : void InsertMStaff::redo()
153 : {
154 701 : measure->insertMStaff(mstaff, idx);
155 701 : }
156 :
157 : //---------------------------------------------------------
158 : // RemoveMStaff
159 : //---------------------------------------------------------
160 :
161 304 : RemoveMStaff::RemoveMStaff(Measure* m, MStaff* ms, int i)
162 : {
163 304 : measure = m;
164 304 : mstaff = ms;
165 304 : idx = i;
166 304 : }
167 :
168 83 : void RemoveMStaff::undo()
169 : {
170 83 : measure->insertMStaff(mstaff, idx);
171 83 : }
172 :
173 305 : void RemoveMStaff::redo()
174 : {
175 305 : measure->removeMStaff(mstaff, idx);
176 305 : }
177 :
178 : //---------------------------------------------------------
179 : // InsertStaves
180 : //---------------------------------------------------------
181 :
182 636 : InsertStaves::InsertStaves(Measure* m, staff_idx_t _a, staff_idx_t _b)
183 : {
184 636 : measure = m;
185 636 : a = _a;
186 636 : b = _b;
187 636 : }
188 :
189 84 : void InsertStaves::undo()
190 : {
191 84 : measure->removeStaves(a, b);
192 84 : }
193 :
194 701 : void InsertStaves::redo()
195 : {
196 701 : measure->insertStaves(a, b);
197 701 : }
198 :
199 : //---------------------------------------------------------
200 : // RemoveStaves
201 : //---------------------------------------------------------
202 :
203 304 : RemoveStaves::RemoveStaves(Measure* m, staff_idx_t _a, staff_idx_t _b)
204 : {
205 304 : measure = m;
206 304 : a = _a;
207 304 : b = _b;
208 304 : }
209 :
210 83 : void RemoveStaves::undo()
211 : {
212 83 : measure->insertStaves(a, b);
213 83 : }
214 :
215 305 : void RemoveStaves::redo()
216 : {
217 305 : measure->removeStaves(a, b);
218 305 : }
219 :
220 : //---------------------------------------------------------
221 : // SortStaves
222 : //---------------------------------------------------------
223 :
224 11 : SortStaves::SortStaves(Score* s, const std::vector<staff_idx_t>& l)
225 : {
226 11 : score = s;
227 :
228 41 : for (staff_idx_t i = 0; i < l.size(); i++) {
229 30 : rlist.push_back(muse::indexOf(l, i));
230 : }
231 11 : list = l;
232 11 : }
233 :
234 11 : void SortStaves::redo()
235 : {
236 11 : score->sortStaves(list);
237 11 : }
238 :
239 3 : void SortStaves::undo()
240 : {
241 3 : score->sortStaves(rlist);
242 3 : }
243 :
244 : //---------------------------------------------------------
245 : // ChangeStaff
246 : //---------------------------------------------------------
247 :
248 0 : ChangeStaff::ChangeStaff(Staff* _staff)
249 0 : : staff(_staff)
250 : {
251 0 : visible = staff->visible();
252 0 : clefType = staff->defaultClefType();
253 0 : userDist = staff->userDist();
254 0 : cutaway = staff->cutaway();
255 0 : hideSystemBarLine = staff->hideSystemBarLine();
256 0 : mergeMatchingRests = staff->mergeMatchingRests();
257 0 : reflectTranspositionInLinkedTab = staff->reflectTranspositionInLinkedTab();
258 0 : }
259 :
260 6 : ChangeStaff::ChangeStaff(Staff* _staff, bool _visible, ClefTypeList _clefType, Spatium _userDist, bool _cutaway, bool _hideSystemBarLine,
261 6 : AutoOnOff _mergeMatchingRests, bool _reflectTranspositionInLinkedTab)
262 : {
263 6 : staff = _staff;
264 6 : visible = _visible;
265 6 : clefType = _clefType;
266 6 : userDist = _userDist;
267 6 : cutaway = _cutaway;
268 6 : hideSystemBarLine = _hideSystemBarLine;
269 6 : mergeMatchingRests = _mergeMatchingRests;
270 6 : reflectTranspositionInLinkedTab = _reflectTranspositionInLinkedTab;
271 6 : }
272 :
273 : //---------------------------------------------------------
274 : // flip
275 : //---------------------------------------------------------
276 :
277 6 : void ChangeStaff::flip()
278 : {
279 6 : bool oldVisible = staff->visible();
280 6 : ClefTypeList oldClefType = staff->defaultClefType();
281 6 : Spatium oldUserDist = staff->userDist();
282 6 : bool oldCutaway = staff->cutaway();
283 6 : bool oldHideSystemBarLine = staff->hideSystemBarLine();
284 6 : AutoOnOff oldMergeMatchingRests = staff->mergeMatchingRests();
285 6 : bool oldReflectTranspositionInLinkedTab = staff->reflectTranspositionInLinkedTab();
286 :
287 6 : staff->setVisible(visible);
288 6 : staff->setDefaultClefType(clefType);
289 6 : staff->setUserDist(userDist);
290 6 : staff->setCutaway(cutaway);
291 6 : staff->setHideSystemBarLine(hideSystemBarLine);
292 6 : staff->setMergeMatchingRests(mergeMatchingRests);
293 6 : staff->setReflectTranspositionInLinkedTab(reflectTranspositionInLinkedTab);
294 :
295 6 : visible = oldVisible;
296 6 : clefType = oldClefType;
297 6 : userDist = oldUserDist;
298 6 : cutaway = oldCutaway;
299 6 : hideSystemBarLine = oldHideSystemBarLine;
300 6 : mergeMatchingRests = oldMergeMatchingRests;
301 6 : reflectTranspositionInLinkedTab = oldReflectTranspositionInLinkedTab;
302 :
303 6 : staff->triggerLayout();
304 6 : staff->masterScore()->rebuildMidiMapping();
305 6 : }
306 :
307 : //---------------------------------------------------------
308 : // ChangeStaffType::flip
309 : //---------------------------------------------------------
310 :
311 8 : void ChangeStaffType::flip()
312 : {
313 8 : StaffType oldStaffType = *staff->staffType(tick);
314 :
315 8 : staff->setStaffType(tick, staffType);
316 :
317 8 : bool invisibleChanged = oldStaffType.invisible() != staffType.invisible();
318 8 : bool fromTabToStandard = oldStaffType.isTabStaff() && !staffType.isTabStaff();
319 :
320 8 : staffType = oldStaffType;
321 :
322 8 : Score* score = staff->score();
323 8 : if (invisibleChanged) {
324 0 : staff_idx_t staffIdx = staff->idx();
325 0 : for (Measure* m = score->firstMeasure(); m; m = m->nextMeasure()) {
326 0 : m->staffLines(staffIdx)->setVisible(!staff->isLinesInvisible(tick));
327 : }
328 : }
329 :
330 8 : if (fromTabToStandard) {
331 0 : GuitarBend::adaptBendsFromTabToStandardStaff(staff);
332 : }
333 :
334 8 : staff->triggerLayout();
335 8 : }
336 :
337 : //---------------------------------------------------------
338 : // ChangeMStaffProperties
339 : //---------------------------------------------------------
340 :
341 0 : ChangeMStaffProperties::ChangeMStaffProperties(Measure* m, staff_idx_t i, bool v, bool s)
342 0 : : measure(m), staffIdx(i), visible(v), stemless(s)
343 : {
344 0 : }
345 :
346 0 : void ChangeMStaffProperties::flip()
347 : {
348 0 : bool v = measure->visible(staffIdx);
349 0 : bool s = measure->stemless(staffIdx);
350 0 : measure->setStaffVisible(staffIdx, visible);
351 0 : measure->setStaffStemless(staffIdx, stemless);
352 0 : visible = v;
353 0 : stemless = s;
354 0 : }
355 :
356 : //---------------------------------------------------------
357 : // ChangeMStaffHideIfEmpty
358 : //---------------------------------------------------------
359 :
360 0 : ChangeMStaffHideIfEmpty::ChangeMStaffHideIfEmpty(Measure* m, staff_idx_t i, AutoOnOff h)
361 0 : : measure(m), staffIdx(i), hideIfEmpty(h)
362 : {
363 0 : }
364 :
365 0 : void ChangeMStaffHideIfEmpty::flip()
366 : {
367 0 : AutoOnOff h = measure->hideStaffIfEmpty(staffIdx);
368 0 : measure->setHideStaffIfEmpty(staffIdx, hideIfEmpty);
369 0 : measure->triggerLayout(staffIdx);
370 0 : hideIfEmpty = h;
371 0 : }
|