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 : */
52 0 : void InteractiveApi::info(const QString& contentTitle, const QString& text)
53 : {
54 0 : interactive()->infoSync(contentTitle.toStdString(), text.toStdString());
55 0 : }
56 :
57 : /** APIDOC
58 : * Show warning message
59 : * @method
60 : * @param {String} title Title
61 : * @param {String} text Message
62 : */
63 0 : void InteractiveApi::warning(const QString& contentTitle, const QString& text)
64 : {
65 0 : interactive()->warningSync(contentTitle.toStdString(), text.toStdString());
66 0 : }
67 :
68 : /** APIDOC
69 : * Show error message
70 : * @method
71 : * @param {String} title Title
72 : * @param {String} text Message
73 : */
74 0 : void InteractiveApi::error(const QString& contentTitle, const QString& text)
75 : {
76 0 : interactive()->errorSync(contentTitle.toStdString(), text.toStdString());
77 0 : }
78 :
79 0 : static muse::IInteractive::Button buttonFromString(const QString& str)
80 : {
81 0 : static QMetaEnum meta = QMetaEnum::fromType<InteractiveApi::ButtonCode>();
82 0 : int val = meta.keyToValue(str.toLatin1().constData());
83 0 : if (val == -1) {
84 0 : return muse::IInteractive::Button::NoButton;
85 : }
86 0 : return static_cast<muse::IInteractive::Button>(val);
87 : }
88 :
89 0 : static QString buttonToString(const muse::IInteractive::Button& btn)
90 : {
91 0 : static QMetaEnum meta = QMetaEnum::fromType<InteractiveApi::ButtonCode>();
92 0 : const char* key = meta.valueToKey(static_cast<int>(btn));
93 0 : return QString::fromLatin1(key);
94 : }
95 :
96 0 : std::vector<muse::IInteractive::Button> InteractiveApi::buttons(const QJSValueList& btns) const
97 : {
98 0 : std::vector<muse::IInteractive::Button> result;
99 0 : for (const QJSValue& btn : btns) {
100 0 : QString str = btn.toString();
101 0 : result.push_back(buttonFromString(str));
102 0 : }
103 0 : return result;
104 0 : }
105 :
106 : /** APIDOC
107 : * Ask a question
108 : * @method
109 : * @param {String} title Title
110 : * @param {String} text Message
111 : * @param {ButtonCode[]} buttons Code of buttons
112 : * @return {ButtonCode} - selected button
113 : * @example
114 : * let btn = api.interactive.question("My question", "Yes or No?", [ButtonCode.Yes, ButtonCode.No]);
115 : * if (btn === ButtonCode.Yes) {
116 : * ...
117 : * }
118 : */
119 0 : QString InteractiveApi::question(const QString& contentTitle, const QString& text, const QJSValueList& btns)
120 : {
121 0 : IInteractive::Result res = interactive()->questionSync(contentTitle.toStdString(),
122 0 : text.toStdString(),
123 0 : buttons(btns));
124 0 : return buttonToString(res.standardButton());
125 : }
126 :
127 : /** APIDOC
128 : * Open URL in external browser
129 : * @method
130 : * @param {String} url URL
131 : */
132 0 : void InteractiveApi::openUrl(const QString& url)
133 : {
134 0 : platformInteractive()->openUrl(QUrl(url));
135 0 : }
|