-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathnotationaccessibility.cpp
213 lines (175 loc) · 6.12 KB
/
notationaccessibility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-Studio-CLA-applies
*
* MuseScore Studio
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "notationaccessibility.h"
#include "translation.h"
#include "igetscore.h"
#include "notation.h"
#include "engraving/dom/masterscore.h"
#include "engraving/dom/spanner.h"
#include "engraving/dom/segment.h"
#include "engraving/dom/slur.h"
#include "engraving/dom/staff.h"
#include "engraving/dom/part.h"
#include "engraving/dom/sig.h"
#include "engraving/dom/measure.h"
#include "accessibility/accessibleroot.h"
using namespace mu::notation;
using namespace muse::async;
using namespace mu::engraving;
using namespace muse::accessibility;
NotationAccessibility::NotationAccessibility(const Notation* notation)
: m_getScore(notation)
{
notation->interaction()->selectionChanged().onNotify(this, [this]() {
setTriggeredCommand("");
updateAccessibilityInfo();
});
notation->notationChanged().onNotify(this, [this]() {
updateAccessibilityInfo();
});
}
const mu::engraving::Score* NotationAccessibility::score() const
{
return m_getScore->score();
}
const mu::engraving::Selection* NotationAccessibility::selection() const
{
return &score()->selection();
}
muse::ValCh<std::string> NotationAccessibility::accessibilityInfo() const
{
return m_accessibilityInfo;
}
void NotationAccessibility::setMapToScreenFunc(const AccessibleMapToScreenFunc& func)
{
#ifndef ENGRAVING_NO_ACCESSIBILITY
score()->rootItem()->accessible()->accessibleRoot()->setMapToScreenFunc(func);
score()->dummy()->rootItem()->accessible()->accessibleRoot()->setMapToScreenFunc(func);
#else
UNUSED(func)
#endif
}
void NotationAccessibility::setEnabled(bool enabled)
{
#ifndef ENGRAVING_NO_ACCESSIBILITY
std::vector<AccessibleRoot*> roots {
score()->rootItem()->accessible()->accessibleRoot(),
score()->dummy()->rootItem()->accessible()->accessibleRoot()
};
EngravingItem* selectedElement = selection()->element();
AccessibleItemPtr selectedElementAccItem = selectedElement ? selectedElement->accessible() : nullptr;
for (AccessibleRoot* root : roots) {
root->setEnabled(enabled);
if (!enabled) {
root->setFocusedElement(nullptr);
continue;
}
if (!selectedElementAccItem) {
continue;
}
if (selectedElementAccItem->accessibleRoot() == root) {
root->setFocusedElement(selectedElementAccItem);
}
}
#else
UNUSED(enabled)
#endif
}
void NotationAccessibility::setTriggeredCommand(const std::string& command)
{
#ifndef ENGRAVING_NO_ACCESSIBILITY
score()->rootItem()->accessible()->accessibleRoot()->setCommandInfo(QString::fromStdString(command));
score()->dummy()->rootItem()->accessible()->accessibleRoot()->setCommandInfo(QString::fromStdString(command));
#else
UNUSED(command)
#endif
}
void NotationAccessibility::updateAccessibilityInfo()
{
if (!score()) {
return;
}
QString newAccessibilityInfo;
if (selection()->isSingle()) {
newAccessibilityInfo = singleElementAccessibilityInfo();
} else if (selection()->isRange()) {
newAccessibilityInfo = rangeAccessibilityInfo();
} else if (selection()->isList()) {
newAccessibilityInfo = muse::qtrc("notation", "List selection");
}
// Simplify whitespace and remove newlines
newAccessibilityInfo = newAccessibilityInfo.simplified();
setAccessibilityInfo(newAccessibilityInfo);
}
void NotationAccessibility::setAccessibilityInfo(const QString& info)
{
std::string infoStd = info.toStdString();
if (m_accessibilityInfo.val == infoStd) {
return;
}
m_accessibilityInfo.set(infoStd);
}
QString NotationAccessibility::rangeAccessibilityInfo() const
{
const mu::engraving::Segment* endSegment = selection()->endSegment();
if (!endSegment) {
endSegment = score()->lastSegment();
} else {
endSegment = endSegment->prev1MM();
}
std::pair<int, float> startBarbeat = selection()->startSegment()->barbeat();
QString start = muse::qtrc("notation", "Start measure: %1; Start beat: %2")
.arg(startBarbeat.first)
.arg(startBarbeat.second);
std::pair<int, float> endBarbeat = endSegment->barbeat();
QString end = muse::qtrc("notation", "End measure: %1; End beat: %2")
.arg(endBarbeat.first)
.arg(endBarbeat.second);
return muse::qtrc("notation", "Range selection; %1; %2")
.arg(start)
.arg(end);
}
QString NotationAccessibility::singleElementAccessibilityInfo() const
{
const EngravingItem* element = selection()->element();
if (!element) {
return QString();
}
QString accessibilityInfo = element->accessibleInfo();
QString barsAndBeats = element->formatBarsAndBeats();
if (!barsAndBeats.isEmpty()) {
accessibilityInfo += "; " + barsAndBeats;
}
if (element->hasStaff()) {
QString staff = muse::qtrc("notation", "Staff %1").arg(QString::number(element->staffIdx() + 1));
QString staffName = element->staff()->part()->longName(element->tick());
if (staffName.isEmpty()) {
staffName = element->staff()->partName();
}
if (staffName.isEmpty()) {
accessibilityInfo = QString("%1; %2").arg(accessibilityInfo).arg(staff);
} else {
accessibilityInfo = QString("%1; %2 (%3)").arg(accessibilityInfo).arg(staff).arg(staffName);
}
}
return accessibilityInfo;
}