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 "braillemodel.h"
24 :
25 : using namespace mu::braille;
26 : using namespace mu::notation;
27 :
28 0 : BrailleModel::BrailleModel(QObject* parent)
29 0 : : QObject(parent), muse::Contextable(muse::iocCtxForQmlObject(this))
30 : {
31 0 : }
32 :
33 0 : QString BrailleModel::brailleInfo() const
34 : {
35 0 : return notationBraille() ? QString::fromStdString(notationBraille()->brailleInfo().val) : QString();
36 : }
37 :
38 0 : QString BrailleModel::keys() const
39 : {
40 0 : return notationBraille() ? QString::fromStdString(notationBraille()->keys().val) : QString();
41 : }
42 :
43 0 : int BrailleModel::cursorPosition() const
44 : {
45 0 : return notationBraille() ? notationBraille()->cursorPosition().val : 0;
46 : }
47 :
48 0 : int BrailleModel::currentItemPositionStart() const
49 : {
50 0 : return notationBraille() ? notationBraille()->currentItemPositionStart().val : 0;
51 : }
52 :
53 0 : int BrailleModel::currentItemPositionEnd() const
54 : {
55 0 : return notationBraille() ? notationBraille()->currentItemPositionEnd().val : 0;
56 : }
57 :
58 0 : void BrailleModel::setCursorPosition(int pos)
59 : {
60 0 : if (!notationBraille()) {
61 0 : return;
62 : }
63 :
64 0 : if (notationBraille()->cursorPosition().val == pos) {
65 0 : return;
66 : }
67 :
68 0 : notationBraille()->setCursorPosition(pos);
69 : }
70 :
71 0 : void BrailleModel::setKeys(const QString& sequence)
72 : {
73 0 : if (!notationBraille()) {
74 0 : return;
75 : }
76 0 : notationBraille()->setKeys(sequence);
77 : }
78 :
79 0 : bool BrailleModel::enabled() const
80 : {
81 0 : return brailleConfiguration()->braillePanelEnabled();
82 : }
83 :
84 0 : void BrailleModel::setEnabled(bool e)
85 : {
86 0 : if (!notationBraille()) {
87 0 : return;
88 : }
89 0 : if (notationBraille()->enabled().val == e) {
90 0 : return;
91 : }
92 0 : notationBraille()->setEnabled(e);
93 0 : emit braillePanelEnabledChanged();
94 : }
95 :
96 0 : int BrailleModel::mode() const
97 : {
98 0 : return notationBraille()->mode().val;
99 : }
100 :
101 0 : void BrailleModel::setMode(int m)
102 : {
103 0 : if (!notationBraille()) {
104 0 : return;
105 : }
106 :
107 0 : if (notationBraille()->mode().val == m) {
108 0 : return;
109 : }
110 :
111 0 : notationBraille()->setMode(static_cast<BrailleMode>(m));
112 :
113 0 : emit brailleModeChanged();
114 : }
115 :
116 0 : bool BrailleModel::isNavigationMode()
117 : {
118 0 : if (!notationBraille()) {
119 0 : return false;
120 : }
121 0 : return notationBraille()->isNavigationMode();
122 : }
123 :
124 0 : bool BrailleModel::isBrailleInputMode()
125 : {
126 0 : if (!notationBraille()) {
127 0 : return false;
128 : }
129 0 : return notationBraille()->isBrailleInputMode();
130 : }
131 :
132 0 : QString BrailleModel::cursorColor() const
133 : {
134 0 : QString color = notationBraille() ? QString::fromStdString(notationBraille()->cursorColor().val) : QString();
135 0 : return color;
136 : }
137 :
138 0 : void BrailleModel::load()
139 : {
140 0 : TRACEFUNC;
141 :
142 0 : if (notationBraille()) {
143 0 : notationBraille()->setMode(BrailleMode::Navigation);
144 : }
145 :
146 0 : onCurrentNotationChanged();
147 0 : context()->currentNotationChanged().onNotify(this, [this]() {
148 0 : onCurrentNotationChanged();
149 0 : });
150 0 : }
151 :
152 0 : void BrailleModel::onCurrentNotationChanged()
153 : {
154 0 : if (!notation()) {
155 0 : return;
156 : }
157 :
158 0 : listenChangesInnotationBraille();
159 0 : listenCurrentItemChanges();
160 0 : listenKeys();
161 0 : listenBraillePanelEnabledChanges();
162 0 : listenBrailleModeChanges();
163 0 : listenCursorPositionChanges();
164 : }
165 :
166 0 : void BrailleModel::listenChangesInnotationBraille()
167 : {
168 0 : if (!notationBraille()) {
169 0 : return;
170 : }
171 :
172 0 : notationBraille()->brailleInfo().ch.onReceive(this, [this](const std::string&) {
173 0 : emit brailleInfoChanged();
174 0 : }, Mode::SetReplace);
175 : }
176 :
177 0 : void BrailleModel::listenKeys()
178 : {
179 0 : if (!notationBraille()) {
180 0 : return;
181 : }
182 :
183 0 : notationBraille()->keys().ch.onReceive(this, [this](const std::string&) {
184 0 : emit keysFired();
185 0 : }, Mode::SetReplace);
186 : }
187 :
188 0 : void BrailleModel::listenCursorPositionChanges()
189 : {
190 0 : if (!notationBraille()) {
191 0 : return;
192 : }
193 0 : notationBraille()->cursorPosition().ch.onReceive(this, [this](const int) {
194 0 : emit cursorPositionChanged();
195 0 : }, Mode::SetReplace);
196 : }
197 :
198 0 : void BrailleModel::listenCurrentItemChanges()
199 : {
200 0 : if (!notationBraille()) {
201 0 : return;
202 : }
203 0 : notationBraille()->currentItemPositionStart().ch.onReceive(this, [this](int) {
204 0 : emit currentItemChanged();
205 0 : }, Mode::SetReplace);
206 :
207 0 : notationBraille()->currentItemPositionEnd().ch.onReceive(this, [this](int) {
208 0 : emit currentItemChanged();
209 0 : }, Mode::SetReplace);
210 : }
211 :
212 0 : void BrailleModel::listenBraillePanelEnabledChanges()
213 : {
214 0 : if (!notationBraille()) {
215 0 : return;
216 : }
217 0 : notationBraille()->enabled().ch.onReceive(this, [this](const int) {
218 0 : emit braillePanelEnabledChanged();
219 0 : }, Mode::SetReplace);
220 : }
221 :
222 0 : void BrailleModel::listenBrailleModeChanges()
223 : {
224 0 : if (!notationBraille()) {
225 0 : return;
226 : }
227 0 : notationBraille()->mode().ch.onReceive(this, [this](const int) {
228 0 : emit brailleModeChanged();
229 0 : }, Mode::SetReplace);
230 : }
231 :
232 0 : INotationPtr BrailleModel::notation() const
233 : {
234 0 : return context()->currentNotation();
235 : }
|