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 "maintoolbarmodel.h"
24 :
25 : #include "translation.h"
26 :
27 : using namespace mu::appshell;
28 :
29 : static const QString HOME_PAGE("musescore://home");
30 : static const QString NOTATION_PAGE("musescore://notation");
31 : static const QString NOTATION_REVIEW_PAGE("musescore://notation/review");
32 : static const QString PUBLISH_PAGE("musescore://publish");
33 : static const QString DEVTOOLS_PAGE("musescore://devtools");
34 :
35 : static const QString TITLE_KEY("title");
36 : static const QString URI_KEY("uri");
37 : static const QString IS_TITLE_BOLD_KEY("isTitleBold");
38 : static const QString IS_CHECKED_KEY("isChecked");
39 :
40 0 : static bool isNotationPageUri(const QString& uri)
41 : {
42 0 : return uri == NOTATION_PAGE || uri == NOTATION_REVIEW_PAGE;
43 : }
44 :
45 0 : static bool isItemChecked(const QString& itemUri, const QString& currentUri)
46 : {
47 0 : return itemUri == currentUri || (isNotationPageUri(itemUri) && isNotationPageUri(currentUri));
48 : }
49 :
50 0 : inline QVariantMap buildItem(const QString& title, const QString& uri, const QString& currentUri)
51 : {
52 0 : QVariantMap item;
53 0 : item[TITLE_KEY] = title;
54 0 : item[URI_KEY] = uri;
55 0 : item[IS_TITLE_BOLD_KEY] = false;
56 0 : item[IS_CHECKED_KEY] = isItemChecked(uri, currentUri);
57 :
58 0 : return item;
59 0 : }
60 :
61 0 : MainToolBarModel::MainToolBarModel(QObject* parent)
62 0 : : QAbstractListModel(parent), muse::Contextable(muse::iocCtxForQmlObject(this)), m_currentUri(HOME_PAGE)
63 : {
64 0 : }
65 :
66 0 : QVariant MainToolBarModel::data(const QModelIndex& index, int role) const
67 : {
68 0 : if (!index.isValid() || index.row() >= rowCount()) {
69 0 : return QVariant();
70 : }
71 :
72 0 : const QVariantMap& item = m_items.at(index.row());
73 0 : switch (role) {
74 0 : case TitleRole: return item[TITLE_KEY];
75 0 : case UriRole: return item[URI_KEY];
76 0 : case IsTitleBoldRole: return item[IS_TITLE_BOLD_KEY];
77 0 : case IsCheckedRole: return item[IS_CHECKED_KEY];
78 : }
79 :
80 0 : return QVariant();
81 : }
82 :
83 0 : int MainToolBarModel::rowCount(const QModelIndex&) const
84 : {
85 0 : return m_items.size();
86 : }
87 :
88 0 : QHash<int, QByteArray> MainToolBarModel::roleNames() const
89 : {
90 : static const QHash<int, QByteArray> roles = {
91 0 : { TitleRole, TITLE_KEY.toUtf8() },
92 0 : { UriRole, URI_KEY.toUtf8() },
93 0 : { IsTitleBoldRole, IS_TITLE_BOLD_KEY.toUtf8() },
94 0 : { IsCheckedRole, IS_CHECKED_KEY.toUtf8() },
95 0 : };
96 :
97 0 : return roles;
98 0 : }
99 :
100 0 : void MainToolBarModel::load()
101 : {
102 0 : beginResetModel();
103 :
104 0 : m_items.clear();
105 0 : m_items << buildItem(muse::qtrc("appshell", "Home"), HOME_PAGE, m_currentUri);
106 0 : m_items << buildItem(muse::qtrc("appshell", "Score"), NOTATION_PAGE, m_currentUri);
107 0 : m_items << buildItem(muse::qtrc("appshell", "Publish"), PUBLISH_PAGE, m_currentUri);
108 :
109 0 : if (globalConfiguration()->devModeEnabled()) {
110 0 : m_items << buildItem(muse::qtrc("appshell", "DevTools"), DEVTOOLS_PAGE, m_currentUri);
111 : }
112 :
113 0 : endResetModel();
114 :
115 0 : updateNotationPageItem();
116 :
117 0 : context()->projectChanged().onNotify(this, [this]() {
118 0 : updateNotationPageItem();
119 0 : });
120 0 : }
121 :
122 0 : QString MainToolBarModel::currentUri() const
123 : {
124 0 : return m_currentUri;
125 : }
126 :
127 0 : void MainToolBarModel::setCurrentUri(const QString& uri)
128 : {
129 0 : if (m_currentUri == uri) {
130 0 : return;
131 : }
132 :
133 0 : m_currentUri = uri;
134 0 : emit currentUriChanged();
135 :
136 0 : updateCheckedState();
137 : }
138 :
139 0 : void MainToolBarModel::updateNotationPageItem()
140 : {
141 0 : for (int i = 0; i < m_items.size(); ++i) {
142 0 : QVariantMap& item = m_items[i];
143 :
144 0 : if (isNotationPageUri(item[URI_KEY].toString())) {
145 0 : item[URI_KEY] = QString::fromStdString(openProjectScenario()->resolveNotationPageUri().toString());
146 0 : item[IS_TITLE_BOLD_KEY] = context()->project() != nullptr;
147 :
148 0 : QModelIndex modelIndex = index(i);
149 0 : emit dataChanged(modelIndex, modelIndex, { UriRole, IsTitleBoldRole });
150 :
151 0 : break;
152 : }
153 : }
154 0 : }
155 :
156 0 : void MainToolBarModel::updateCheckedState()
157 : {
158 0 : for (int i = 0; i < m_items.size(); ++i) {
159 0 : QVariantMap& item = m_items[i];
160 :
161 0 : QVariant& checkedValue = item[IS_CHECKED_KEY];
162 0 : bool checked = isItemChecked(item[URI_KEY].toString(), m_currentUri);
163 0 : if (checkedValue.toBool() == checked) {
164 0 : continue;
165 : }
166 :
167 0 : checkedValue = checked;
168 :
169 0 : QModelIndex modelIndex = index(i);
170 0 : emit dataChanged(modelIndex, modelIndex, { IsCheckedRole });
171 : }
172 0 : }
|