Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-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 : #include "navigationapi.h"
23 :
24 : #include "navigationcommands.h"
25 :
26 : #include "log.h"
27 :
28 : using namespace muse::api;
29 : using namespace muse::ui;
30 :
31 0 : NavigationApi::NavigationApi(api::IApiEngine* e)
32 0 : : api::ApiObject(e)
33 : {
34 0 : }
35 :
36 0 : NavigationApi::~NavigationApi()
37 : {
38 0 : }
39 :
40 0 : void NavigationApi::nextPanel()
41 : {
42 0 : dispatcher()->dispatch(NEXT_PANEL_COMMAND);
43 0 : }
44 :
45 0 : void NavigationApi::prevPanel()
46 : {
47 0 : dispatcher()->dispatch(PREV_PANEL_COMMAND);
48 0 : }
49 :
50 0 : void NavigationApi::right()
51 : {
52 0 : dispatcher()->dispatch(RIGHT_COMMAND);
53 0 : }
54 :
55 0 : void NavigationApi::left()
56 : {
57 0 : dispatcher()->dispatch(LEFT_COMMAND);
58 0 : }
59 :
60 0 : void NavigationApi::up()
61 : {
62 0 : dispatcher()->dispatch(UP_COMMAND);
63 0 : }
64 :
65 0 : void NavigationApi::down()
66 : {
67 0 : dispatcher()->dispatch(DOWN_COMMAND);
68 0 : }
69 :
70 0 : void NavigationApi::escape()
71 : {
72 0 : dispatcher()->dispatch(ESCAPE_COMMAND);
73 0 : }
74 :
75 0 : bool NavigationApi::goToControl(const QString& section, const QString& panel, const QJSValue& controlNameOrIndex)
76 : {
77 0 : bool ok = false;
78 0 : if (controlNameOrIndex.isString()) {
79 0 : ok = navigation()->requestActivateByName(section.toStdString(), panel.toStdString(), controlNameOrIndex.toString().toStdString());
80 0 : } else if (controlNameOrIndex.isArray()) {
81 0 : if (controlNameOrIndex.property("length").toInt() == 2) {
82 0 : INavigation::Index idx;
83 0 : idx.row = controlNameOrIndex.property(0).toInt();
84 0 : idx.column = controlNameOrIndex.property(1).toInt();
85 0 : ok = navigation()->requestActivateByIndex(section.toStdString(), panel.toStdString(), idx);
86 : } else {
87 0 : LOGE() << "bad argument `control`: " << controlNameOrIndex.toString();
88 0 : ok = false;
89 : }
90 : } else {
91 0 : LOGE() << "bad argument `control`: " << controlNameOrIndex.toString();
92 0 : ok = false;
93 : }
94 :
95 0 : return ok;
96 : }
97 :
98 0 : void NavigationApi::trigger()
99 : {
100 0 : dispatcher()->dispatch(TRIGGER_CONTROL_COMMAND);
101 0 : }
102 :
103 0 : bool NavigationApi::triggerControl(const QString& section, const QString& panel, const QJSValue& controlNameOrIndex)
104 : {
105 0 : bool ok = goToControl(section, panel, controlNameOrIndex);
106 0 : if (ok) {
107 0 : trigger();
108 : }
109 0 : return ok;
110 : }
111 :
112 0 : QString NavigationApi::activeSection() const
113 : {
114 0 : INavigationSection* sec = navigation()->activeSection();
115 0 : return sec ? sec->name() : QString();
116 : }
117 :
118 0 : QString NavigationApi::activePanel() const
119 : {
120 0 : INavigationPanel* p = navigation()->activePanel();
121 0 : return p ? p->name() : QString();
122 : }
123 :
124 0 : QString NavigationApi::activeControl() const
125 : {
126 0 : INavigationControl* c = navigation()->activeControl();
127 0 : return c ? c->name() : QString();
128 : }
129 :
130 0 : static QJSValue toQJSValue(const INavigation* n, IApiEngine* e)
131 : {
132 0 : QJSValue o = e->newObject();
133 0 : o.setProperty("name", n->name());
134 :
135 0 : QJSValue io = e->newObject();
136 0 : io.setProperty("column", n->index().column);
137 0 : io.setProperty("row", n->index().row);
138 0 : o.setProperty("index", io);
139 :
140 0 : o.setProperty("enabled", n->enabled());
141 0 : o.setProperty("active", n->active());
142 :
143 0 : return o;
144 0 : }
145 :
146 0 : QJSValue NavigationApi::sections() const
147 : {
148 0 : const std::set<INavigationSection*>& sects = navigation()->sections();
149 0 : QJSValue arr = engine()->newArray(sects.size());
150 0 : quint32 i = 0;
151 0 : for (const INavigationSection* s : sects) {
152 0 : arr.setProperty(i, toQJSValue(s, engine()));
153 0 : ++i;
154 : }
155 :
156 0 : return arr;
157 0 : }
158 :
159 0 : QJSValue NavigationApi::panels(const QString& sectionName) const
160 : {
161 0 : const INavigationSection* s = navigation()->findSection(sectionName.toStdString());
162 0 : QJSValue arr = engine()->newArray(s->panels().size());
163 0 : quint32 i = 0;
164 0 : for (const INavigationPanel* p : s->panels()) {
165 0 : arr.setProperty(i, toQJSValue(p, engine()));
166 0 : ++i;
167 : }
168 :
169 0 : return arr;
170 0 : }
171 :
172 0 : QJSValue NavigationApi::controls(const QString& sectionName, const QString& panelName) const
173 : {
174 0 : const INavigationPanel* p = navigation()->findPanel(sectionName.toStdString(), panelName.toStdString());
175 0 : QJSValue arr = engine()->newArray(p->controls().size());
176 0 : quint32 i = 0;
177 0 : for (const INavigationControl* c : p->controls()) {
178 0 : arr.setProperty(i, toQJSValue(c, engine()));
179 0 : ++i;
180 : }
181 :
182 0 : return arr;
183 0 : }
184 :
185 0 : void NavigationApi::dump() const
186 : {
187 0 : navigation()->dump();
188 0 : }
|