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 :
23 : #include "interactiveapi.h"
24 :
25 : #include <QUrl>
26 : #include <QMetaEnum>
27 :
28 : using namespace muse::interactive;
29 :
30 : /** APIDOC
31 : * User interaction - informational messages, error messages, questions and other dialogs.
32 : * @namespace Interactive
33 : * @example
34 : * const Interactive = require("MuseApi.Interactive");
35 : *
36 : * function main() {
37 : * Interactive.info("Quick start", "called main function")
38 : * }
39 : */
40 :
41 0 : InteractiveApi::InteractiveApi(api::IApiEngine* e)
42 0 : : ApiObject(e)
43 : {
44 0 : }
45 :
46 : /** APIDOC
47 : * Show information message
48 : * @method
49 : * @param {String} title Title
50 : * @param {String} text Message
51 : * @return {Promise} - resolved when the message is closed
52 : * @example
53 : * Interactive.info("My title", "Some information").then(function() {
54 : * ...
55 : * })
56 : */
57 0 : QJSValue InteractiveApi::info(const QString& contentTitle, const QString& text)
58 : {
59 0 : api::JsPromise promise = engine()->newPromise();
60 :
61 0 : interactive()->info(contentTitle.toStdString(), IInteractive::Text(text.toStdString()))
62 0 : .onResolve(this, [resolve = promise.resolve](const IInteractive::Result&) {
63 0 : resolve.call();
64 0 : });
65 :
66 0 : return promise.promise;
67 0 : }
68 :
69 : /** APIDOC
70 : * Show warning message
71 : * @method
72 : * @param {String} title Title
73 : * @param {String} text Message
74 : * @return {Promise} - resolved when the message is closed
75 : * @example
76 : * Interactive.warning("My title", "Some warning").then(function() {
77 : * ...
78 : * })
79 : */
80 0 : QJSValue InteractiveApi::warning(const QString& contentTitle, const QString& text)
81 : {
82 0 : api::JsPromise promise = engine()->newPromise();
83 :
84 0 : interactive()->warning(contentTitle.toStdString(), IInteractive::Text(text.toStdString()))
85 0 : .onResolve(this, [resolve = promise.resolve](const IInteractive::Result&) {
86 0 : resolve.call();
87 0 : });
88 :
89 0 : return promise.promise;
90 0 : }
91 :
92 : /** APIDOC
93 : * Show error message
94 : * @method
95 : * @param {String} title Title
96 : * @param {String} text Message
97 : * @return {Promise} - resolved when the message is closed
98 : * @example
99 : * Interactive.error("My title", "Some error").then(function() {
100 : * ...
101 : * })
102 : */
103 0 : QJSValue InteractiveApi::error(const QString& contentTitle, const QString& text)
104 : {
105 0 : api::JsPromise promise = engine()->newPromise();
106 :
107 0 : interactive()->error(contentTitle.toStdString(), IInteractive::Text(text.toStdString()))
108 0 : .onResolve(this, [resolve = promise.resolve](const IInteractive::Result&) {
109 0 : resolve.call();
110 0 : });
111 :
112 0 : return promise.promise;
113 0 : }
114 :
115 0 : static muse::IInteractive::Button buttonFromString(const QString& str)
116 : {
117 0 : static QMetaEnum meta = QMetaEnum::fromType<InteractiveApi::ButtonCode>();
118 0 : int val = meta.keyToValue(str.toLatin1().constData());
119 0 : if (val == -1) {
120 0 : return muse::IInteractive::Button::NoButton;
121 : }
122 0 : return static_cast<muse::IInteractive::Button>(val);
123 : }
124 :
125 0 : static QString buttonToString(const muse::IInteractive::Button& btn)
126 : {
127 0 : static QMetaEnum meta = QMetaEnum::fromType<InteractiveApi::ButtonCode>();
128 0 : const char* key = meta.valueToKey(static_cast<int>(btn));
129 0 : return QString::fromLatin1(key);
130 : }
131 :
132 0 : std::vector<muse::IInteractive::Button> InteractiveApi::buttons(const QJSValueList& btns) const
133 : {
134 0 : std::vector<muse::IInteractive::Button> result;
135 0 : for (const QJSValue& btn : btns) {
136 0 : QString str = btn.toString();
137 0 : result.push_back(buttonFromString(str));
138 0 : }
139 0 : return result;
140 0 : }
141 :
142 : /** APIDOC
143 : * Ask a question
144 : * @method
145 : * @param {String} title Title
146 : * @param {String} text Message
147 : * @param {ButtonCode[]} buttons Code of buttons
148 : * @return {Promise<ButtonCode>} - selected button
149 : * @example
150 : * api.interactive.question("My question", "Yes or No?", [ButtonCode.Yes, ButtonCode.No]).then(function(btn) {
151 : * if (btn === ButtonCode.Yes) {
152 : * ...
153 : * }
154 : * })
155 : */
156 0 : QJSValue InteractiveApi::question(const QString& contentTitle, const QString& text, const QJSValueList& btns)
157 : {
158 0 : api::JsPromise promise = engine()->newPromise();
159 :
160 0 : interactive()->question(contentTitle.toStdString(), text.toStdString(), buttons(btns))
161 0 : .onResolve(this, [resolve = promise.resolve](const IInteractive::Result& res) {
162 0 : resolve.call({ QJSValue(buttonToString(res.standardButton())) });
163 0 : });
164 :
165 0 : return promise.promise;
166 0 : }
167 :
168 : /** APIDOC
169 : * Open URL in external browser
170 : * @method
171 : * @param {String} url URL
172 : */
173 0 : void InteractiveApi::openUrl(const QString& url)
174 : {
175 0 : platformInteractive()->openUrl(QUrl(url));
176 0 : }
|