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 "notationstatusbarmodel.h"
24 :
25 : #include "defer.h"
26 : #include "log.h"
27 : #include "thirdparty/kors_logger/src/log_base.h"
28 : #include "types/translatablestring.h"
29 :
30 : #include "muse_framework_config.h"
31 : #include <algorithm>
32 :
33 : #ifdef MUSE_MODULE_WORKSPACE
34 : #include "workspace/qml/Muse/Workspace/workspacesmenumodel.h"
35 : #endif
36 :
37 : #include "engraving/dom/score.h"
38 :
39 : #include "notation/inotation.h"
40 : #include "notation/inotationaccessibility.h"
41 : #include "notation/inotationstyle.h" // IWYU pragma: keep
42 : #include "notation/inotationundostack.h" // IWYU pragma: keep
43 : #include "notation/inotationviewstate.h" // IWYU pragma: keep
44 :
45 : #include "notationscene/notationcommands.h"
46 : #include "workspace/workspacecommands.h"
47 :
48 : using namespace mu::notation;
49 : using namespace muse;
50 : using namespace muse::rcommand;
51 : using namespace muse::ui;
52 : using namespace muse::uicomponents;
53 : using namespace muse::workspace;
54 :
55 : static const QString ICON_KEY("icon");
56 : static const QString SELECTABLE_KEY("selectable");
57 : static const QString SELECTED_KEY("selected");
58 : static const QString TYPE_KEY("type");
59 : static const QString ID_KEY("id");
60 : static const QString VALUE_KEY("value");
61 :
62 : static constexpr int MIN_DISPLAYED_ZOOM_PERCENTAGE = 25;
63 :
64 : static const std::map<ViewMode, Command> ALL_VIEW_MODE_MAP {
65 : { ViewMode::PAGE, VIEW_MODE_PAGE_COMMAND },
66 : { ViewMode::LINE, VIEW_MODE_CONTINUOUS_COMMAND },
67 : { ViewMode::SYSTEM, VIEW_MODE_SINGLE_COMMAND },
68 : { ViewMode::FLOAT, VIEW_MODE_FLOAT_COMMAND },
69 : };
70 :
71 0 : static Command zoomTypeToCommand(ZoomType type)
72 : {
73 0 : switch (type) {
74 0 : case ZoomType::Percentage: return ZOOM_TO_PERCENT_COMMAND;
75 0 : case ZoomType::PageWidth: return ZOOM_TO_PAGE_WIDTH_COMMAND;
76 0 : case ZoomType::WholePage: return ZOOM_TO_WHOLE_PAGE_COMMAND;
77 0 : case ZoomType::TwoPages: return ZOOM_TO_TWO_PAGES_COMMAND;
78 : }
79 :
80 0 : return {};
81 : }
82 :
83 0 : NotationStatusBarModel::NotationStatusBarModel(QObject* parent)
84 0 : : QObject(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
85 : {
86 : #ifdef MUSE_MODULE_WORKSPACE
87 0 : m_workspacesMenuModel = std::make_shared<WorkspacesMenuModel>(this);
88 : #endif
89 0 : }
90 :
91 0 : void NotationStatusBarModel::classBegin()
92 : {
93 0 : init();
94 0 : }
95 :
96 0 : void NotationStatusBarModel::init()
97 : {
98 0 : TRACEFUNC;
99 :
100 0 : m_concertPitchItem = makeMenuItem(TOGGLE_CONCERT_PITCH_COMMAND);
101 : #ifdef MUSE_MODULE_WORKSPACE
102 0 : m_currentWorkspaceItem = makeMenuItem(WORKSPACES_CONFIGURE_COMMAND);
103 : #else
104 : m_currentWorkspaceItem = new MenuItem(this);
105 : #endif
106 :
107 0 : setNotation(context()->currentNotation());
108 :
109 0 : context()->currentNotationChanged().onNotify(this, [this]() {
110 0 : setNotation(context()->currentNotation());
111 0 : });
112 :
113 : #ifdef MUSE_MODULE_WORKSPACE
114 0 : m_workspacesMenuModel->load();
115 0 : connect(m_workspacesMenuModel.get(), &WorkspacesMenuModel::itemsChanged, this, [this]() {
116 0 : updateCurrentWorkspaceItem();
117 0 : });
118 0 : workspaceConfiguration()->currentWorkspaceNameChanged().onReceive(this, [this](const std::string&){
119 0 : updateCurrentWorkspaceItem();
120 0 : });
121 : #endif
122 :
123 0 : updateCurrentWorkspaceItem();
124 0 : }
125 :
126 0 : QString NotationStatusBarModel::accessibilityInfo() const
127 : {
128 0 : return accessibility() ? QString::fromStdString(accessibility()->accessibilityInfo().val) : QString();
129 : }
130 :
131 0 : MenuItem* NotationStatusBarModel::concertPitchItem()
132 : {
133 0 : return m_concertPitchItem;
134 : }
135 :
136 0 : void NotationStatusBarModel::updateConcertPitchItem()
137 : {
138 0 : m_concertPitchItem->setEnabled(m_notation ? true : false);
139 0 : m_concertPitchItem->setChecked(m_notation ? m_notation->style()->styleValue(StyleId::concertPitch).toBool() : false);
140 0 : }
141 :
142 0 : bool NotationStatusBarModel::isWorkspaceAvailable() const
143 : {
144 : #ifdef MUSE_MODULE_WORKSPACE
145 0 : return true;
146 : #else
147 : return false;
148 : #endif
149 : }
150 :
151 0 : MenuItem* NotationStatusBarModel::currentWorkspaceItem()
152 : {
153 0 : return m_currentWorkspaceItem;
154 : }
155 :
156 0 : void NotationStatusBarModel::updateCurrentWorkspaceItem()
157 : {
158 : #ifdef MUSE_MODULE_WORKSPACE
159 0 : m_currentWorkspaceItem->setTitle(
160 0 : muse::TranslatableString::untranslatable("%1 %2")
161 0 : .arg(muse::TranslatableString("workspace", "Workspace:"),
162 0 : String::fromStdString(workspaceConfiguration()->currentWorkspaceName())));
163 :
164 0 : m_currentWorkspaceItem->setSubitems(m_workspacesMenuModel->items());
165 : #endif
166 0 : }
167 :
168 0 : MenuItem* NotationStatusBarModel::makeMenuItem(const Command& command)
169 : {
170 0 : MenuItem* item = new MenuItem(commandsRegister()->commandInfo(command), this);
171 0 : item->setCommandState(commandsState()->commandState(command));
172 0 : return item;
173 : }
174 :
175 0 : MenuItem* NotationStatusBarModel::currentViewMode()
176 : {
177 0 : ViewMode viewMode = m_notation ? m_notation->viewMode() : ViewMode::PAGE;
178 :
179 0 : for (MenuItem* modeItem : m_availableViewModeList) {
180 0 : ViewMode mode = muse::key(ALL_VIEW_MODE_MAP, modeItem->command());
181 :
182 0 : if (mode == viewMode) {
183 0 : if (viewMode == ViewMode::LINE || viewMode == ViewMode::SYSTEM) {
184 : // In continuous view, we don't want to see "horizontal" or "vertical" (those should only be visible in the menu)
185 0 : modeItem->setTitle(muse::TranslatableString("notation", "Continuous view"));
186 : }
187 :
188 0 : return modeItem;
189 : }
190 : }
191 :
192 0 : return nullptr;
193 : }
194 :
195 0 : void NotationStatusBarModel::initAvailableViewModeList()
196 : {
197 0 : TRACEFUNC;
198 :
199 0 : qDeleteAll(m_availableViewModeList);
200 0 : m_availableViewModeList.clear();
201 :
202 0 : DEFER {
203 0 : emit availableViewModeListChanged();
204 0 : emit currentViewModeChanged();
205 0 : };
206 :
207 0 : if (!m_notation) {
208 0 : return;
209 : }
210 :
211 0 : ViewMode currentViewMode = m_notation->viewMode();
212 :
213 0 : for (const auto& pair: ALL_VIEW_MODE_MAP) {
214 0 : if (pair.first == ViewMode::FLOAT && !globalConfiguration()->devModeEnabled()) {
215 0 : continue;
216 : }
217 :
218 0 : MenuItem* viewModeItem = makeMenuItem(pair.second);
219 0 : viewModeItem->setEnabled(true);
220 0 : viewModeItem->setSelectable(true);
221 0 : viewModeItem->setSelected(currentViewMode == pair.first);
222 :
223 0 : m_availableViewModeList << viewModeItem;
224 : }
225 0 : }
226 :
227 0 : bool NotationStatusBarModel::zoomEnabled() const
228 : {
229 0 : return m_notation != nullptr;
230 : }
231 :
232 0 : int NotationStatusBarModel::currentZoomPercentage() const
233 : {
234 0 : if (!m_notation) {
235 0 : return 100;
236 : }
237 :
238 0 : return m_notation->viewState()->zoomPercentage().val;
239 : }
240 :
241 0 : void NotationStatusBarModel::setCurrentZoomPercentage(int zoomPercentage)
242 : {
243 0 : if (zoomPercentage == currentZoomPercentage()) {
244 0 : return;
245 : }
246 :
247 0 : dispatch(zoomTypeToCommand(ZoomType::Percentage), { { "percent", Val(zoomPercentage) } });
248 0 : }
249 :
250 0 : ZoomType NotationStatusBarModel::currentZoomType() const
251 : {
252 0 : if (!m_notation) {
253 0 : return ZoomType::Percentage;
254 : }
255 :
256 0 : return m_notation->viewState()->zoomType().val;
257 : }
258 :
259 0 : void NotationStatusBarModel::setNotation(const INotationPtr& notation)
260 : {
261 0 : if (m_notation == notation) {
262 0 : return;
263 : }
264 :
265 0 : if (m_notation) {
266 0 : m_notation->undoStack()->changesChannel().disconnect(this);
267 0 : m_notation->viewModeChanged().disconnect(this);
268 0 : m_notation->viewState()->zoomPercentage().ch.disconnect(this);
269 0 : m_notation->viewState()->zoomType().ch.disconnect(this);
270 0 : m_notation->accessibility()->accessibilityInfo().ch.disconnect(this);
271 : }
272 :
273 0 : m_notation = notation;
274 :
275 0 : emit zoomEnabledChanged();
276 0 : updateConcertPitchItem();
277 :
278 0 : initAvailableViewModeList();
279 0 : initAvailableZoomList();
280 :
281 0 : if (!notation) {
282 0 : return;
283 : }
284 :
285 0 : notation->undoStack()->changesChannel().onReceive(this, [this](const mu::engraving::ScoreChanges& changes) {
286 0 : if (muse::contains(changes.changedStyleIdSet, mu::engraving::Sid::concertPitch)) {
287 0 : updateConcertPitchItem();
288 : }
289 0 : });
290 :
291 0 : notation->viewModeChanged().onNotify(this, [this]() {
292 0 : initAvailableViewModeList();
293 0 : });
294 :
295 0 : notation->viewState()->zoomPercentage().ch.onReceive(this, [this](int) {
296 0 : initAvailableZoomList();
297 0 : });
298 :
299 0 : notation->viewState()->zoomType().ch.onReceive(this, [this](ZoomType) {
300 0 : initAvailableZoomList();
301 0 : });
302 :
303 0 : emit accessibilityInfoChanged();
304 :
305 0 : notation->accessibility()->accessibilityInfo().ch.onReceive(this, [this](const std::string&) {
306 0 : emit accessibilityInfoChanged();
307 0 : });
308 : }
309 :
310 0 : void NotationStatusBarModel::toggleConcertPitch()
311 : {
312 0 : dispatch(TOGGLE_CONCERT_PITCH_COMMAND);
313 0 : }
314 :
315 0 : void NotationStatusBarModel::setCurrentViewMode(const QString& modeCode)
316 : {
317 0 : dispatch(Command(modeCode.toStdString()));
318 0 : }
319 :
320 0 : void NotationStatusBarModel::initAvailableZoomList()
321 : {
322 0 : TRACEFUNC;
323 :
324 0 : qDeleteAll(m_availableZoomList);
325 0 : m_availableZoomList.clear();
326 :
327 0 : DEFER {
328 0 : emit availableZoomListChanged();
329 0 : emit currentZoomPercentageChanged();
330 0 : };
331 :
332 0 : if (!m_notation) {
333 0 : return;
334 : }
335 :
336 0 : int currZoomPercentage = currentZoomPercentage();
337 0 : ZoomType currZoomType = currentZoomType();
338 :
339 0 : auto zoomPercentageTitle = [](int percentage) {
340 0 : return muse::TranslatableString::untranslatable("%1%").arg(percentage);
341 : };
342 :
343 : auto buildZoomItem
344 0 : = [this, currZoomType, currZoomPercentage](ZoomType type, const muse::TranslatableString& title = {}, int value = 0) {
345 0 : MenuItem* menuItem = new MenuItem(this);
346 0 : menuItem->setId(QString::number(static_cast<int>(type)) + QString::number(value));
347 0 : menuItem->setTitle(title.isEmpty() ? zoomTypeTitle(type) : title);
348 0 : menuItem->setSelectable(true);
349 0 : if (currZoomType == type) {
350 0 : menuItem->setSelected(type == ZoomType::Percentage ? value == currZoomPercentage : true);
351 : }
352 :
353 0 : menuItem->setParams({ { "type", Val(type) }, { "percent", Val(value) } });
354 :
355 0 : return menuItem;
356 0 : };
357 :
358 0 : QList<int> possibleZoomList = possibleZoomPercentageList();
359 :
360 0 : for (int zoom : possibleZoomList) {
361 0 : m_availableZoomList << buildZoomItem(ZoomType::Percentage, zoomPercentageTitle(zoom), zoom);
362 : }
363 :
364 0 : m_availableZoomList << buildZoomItem(ZoomType::PageWidth);
365 0 : m_availableZoomList << buildZoomItem(ZoomType::WholePage);
366 0 : m_availableZoomList << buildZoomItem(ZoomType::TwoPages);
367 :
368 0 : bool isCustomZoom = currZoomType == ZoomType::Percentage && !possibleZoomList.contains(currZoomPercentage);
369 0 : if (isCustomZoom) {
370 0 : MenuItem* customZoom = buildZoomItem(ZoomType::Percentage, zoomPercentageTitle(currZoomPercentage), currZoomPercentage);
371 0 : customZoom->setSelected(true);
372 0 : m_availableZoomList << customZoom;
373 : }
374 0 : }
375 :
376 0 : void NotationStatusBarModel::setCurrentZoom(const QString& zoomId)
377 : {
378 0 : auto it = std::find_if(m_availableZoomList.begin(), m_availableZoomList.end(), [zoomId](const MenuItem* item) {
379 0 : return item->id() == zoomId;
380 : });
381 :
382 0 : if (it == m_availableZoomList.end()) {
383 0 : return;
384 : }
385 :
386 0 : const Params& params = (*it)->params();
387 0 : ZoomType type = params.at("type").toEnum<ZoomType>();
388 0 : int percent = params.at("percent").toInt();
389 :
390 0 : dispatch(zoomTypeToCommand(type), { { "percent", Val(percent) } });
391 :
392 0 : emit availableZoomListChanged();
393 0 : }
394 :
395 0 : int NotationStatusBarModel::minZoomPercentage() const
396 : {
397 0 : return 5;
398 : }
399 :
400 0 : int NotationStatusBarModel::maxZoomPercentage() const
401 : {
402 0 : return possibleZoomPercentageList().last();
403 : }
404 :
405 0 : void NotationStatusBarModel::zoomIn()
406 : {
407 0 : dispatch(ZOOM_IN_COMMAND);
408 0 : }
409 :
410 0 : void NotationStatusBarModel::zoomOut()
411 : {
412 0 : dispatch(ZOOM_OUT_COMMAND);
413 0 : }
414 :
415 0 : void NotationStatusBarModel::handleMenuItem(const QString& itemId)
416 : {
417 0 : LOGDA() << itemId;
418 0 : if (m_concertPitchItem->id() == itemId) {
419 0 : dispatch(m_concertPitchItem->command());
420 0 : } else if (m_currentWorkspaceItem->id() == itemId) {
421 0 : dispatch(m_currentWorkspaceItem->command());
422 : } else {
423 0 : UNREACHABLE;
424 : }
425 0 : }
426 :
427 0 : void NotationStatusBarModel::handleWorkspacesMenuItem(const QString& itemId)
428 : {
429 : #ifdef MUSE_MODULE_WORKSPACE
430 0 : m_workspacesMenuModel->handleMenuItem(itemId);
431 : #else
432 : UNUSED(itemId);
433 : #endif
434 0 : }
435 :
436 0 : INotationPtr NotationStatusBarModel::notation() const
437 : {
438 0 : return context()->currentNotation();
439 : }
440 :
441 0 : INotationAccessibilityPtr NotationStatusBarModel::accessibility() const
442 : {
443 0 : return notation() ? notation()->accessibility() : nullptr;
444 : }
445 :
446 0 : void NotationStatusBarModel::dispatch(const muse::rcommand::Command& command, const muse::rcommand::Params& params)
447 : {
448 0 : dispatcher()->dispatch(command, params);
449 0 : }
450 :
451 0 : QList<int> NotationStatusBarModel::possibleZoomPercentageList() const
452 : {
453 0 : QList<int> result;
454 :
455 0 : for (int zoom : notationConfiguration()->possibleZoomPercentageList()) {
456 0 : if (zoom >= MIN_DISPLAYED_ZOOM_PERCENTAGE) {
457 0 : result << zoom;
458 : }
459 0 : }
460 :
461 0 : return result;
462 0 : }
463 :
464 0 : QVariantList NotationStatusBarModel::availableViewModeList_property()
465 : {
466 0 : return menuItemListToVariantList(m_availableViewModeList);
467 : }
468 :
469 0 : QVariantList NotationStatusBarModel::availableZoomList_property()
470 : {
471 0 : return menuItemListToVariantList(m_availableZoomList);
472 : }
473 :
474 0 : QVariantList NotationStatusBarModel::menuItemListToVariantList(const MenuItemList& list) const
475 : {
476 0 : QVariantList result;
477 0 : for (MenuItem* item: list) {
478 0 : result << QVariant::fromValue(item);
479 : }
480 :
481 0 : return result;
482 0 : }
|