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 : #pragma once
24 :
25 : #include "modularity/imoduleinterface.h"
26 : #include "io/path.h"
27 : #include "types/val.h"
28 : #include "types/retval.h"
29 : #include "types/uri.h"
30 : #include "types/flags.h"
31 : #include "types/color.h"
32 : #include "async/promise.h"
33 : #include "progress.h"
34 :
35 : class QWindow;
36 :
37 : namespace muse {
38 : class IInteractive : MODULE_CONTEXT_INTERFACE
39 : {
40 0 : INTERFACE_ID(IInteractive)
41 :
42 : public:
43 476 : virtual ~IInteractive() = default;
44 :
45 : // question
46 : enum class Button {
47 : NoButton,
48 : Ok,
49 : Continue,
50 : RestoreDefaults,
51 : Reset,
52 : Apply,
53 : Help,
54 : Discard,
55 : Cancel,
56 : Close,
57 : Ignore,
58 : Retry,
59 : Abort,
60 : NoToAll,
61 : No,
62 : YesToAll,
63 : Yes,
64 : Open,
65 : DontSave,
66 : SaveAll,
67 : Save,
68 : Next,
69 : Back,
70 : Select,
71 : Clear,
72 : Done,
73 :
74 : CustomButton,
75 : };
76 : using Buttons = std::vector<Button>;
77 :
78 : enum ButtonRole { // Keep updated with ButtonRole in buttonboxmodel.h
79 : AcceptRole,
80 : RejectRole,
81 : DestructiveRole,
82 : ResetRole,
83 : ApplyRole,
84 : RetryRole,
85 : HelpRole,
86 : ContinueRole,
87 : BackRole,
88 : CustomRole
89 : };
90 :
91 : struct ButtonData {
92 : int btn = int(Button::CustomButton);
93 : std::string text;
94 : bool accent = false;
95 : bool leftSide = false;
96 : ButtonRole role = ButtonRole::CustomRole;
97 : int icon = 0; // ui::IconCode::Code, 0 = no icon
98 :
99 77 : ButtonData(int btn, const std::string& text)
100 77 : : btn(btn), text(text) {}
101 133 : ButtonData(Button btn, const std::string& text)
102 133 : : btn(int(btn)), text(text) {}
103 33 : ButtonData(int btn, const std::string& text, bool accent, bool leftSide = false, ButtonRole role = ButtonRole::CustomRole)
104 33 : : btn(btn), text(text), accent(accent), leftSide(leftSide), role(role) {}
105 8 : ButtonData(Button btn, const std::string& text, bool accent, bool leftSide = false, ButtonRole role = ButtonRole::CustomRole)
106 8 : : btn(int(btn)), text(text), accent(accent), leftSide(leftSide), role(role) {}
107 : };
108 : using ButtonDatas = std::vector<ButtonData>;
109 :
110 : enum class TextFormat {
111 : Auto = 0,
112 : PlainText,
113 : RichText
114 : };
115 :
116 : struct Text {
117 : std::string text;
118 : TextFormat format = TextFormat::Auto;
119 : std::string detailedText;
120 :
121 11 : Text() = default;
122 0 : Text(const char* t)
123 0 : : text(t), format(TextFormat::Auto) {}
124 160 : Text(const std::string& t, const TextFormat& f = TextFormat::Auto)
125 113 : : text(t), format(f) {}
126 : };
127 :
128 : struct Result
129 : {
130 23 : Result() = default;
131 98 : Result(const int& button)
132 98 : : m_button(button) {}
133 2 : Result(const int& button, bool showAgain)
134 2 : : m_button(button), m_showAgain(showAgain) {}
135 :
136 26 : Button standardButton() const { return static_cast<Button>(m_button); }
137 17 : int button() const { return m_button; }
138 37 : bool isButton(int b) const { return b == m_button; }
139 14 : bool isButton(Button b) const { return static_cast<int>(b) == m_button; }
140 :
141 6 : bool showAgain() const { return m_showAgain; }
142 :
143 : private:
144 : int m_button = int(Button::NoButton);
145 : bool m_showAgain = true;
146 : };
147 :
148 : enum Option {
149 : NoOptions = 0x0,
150 : WithIcon = 0x1,
151 : WithDontShowAgainCheckBox = 0x2
152 : };
153 : DECLARE_FLAGS(Options, Option)
154 :
155 : // buttons data
156 : virtual ButtonData buttonData(Button b) const = 0;
157 25 : inline ButtonDatas buttonDataList(const Buttons& buttons) const
158 : {
159 25 : ButtonDatas result;
160 25 : result.reserve(buttons.size());
161 :
162 80 : for (Button b : buttons) {
163 55 : result.push_back(buttonData(b));
164 : }
165 :
166 25 : return result;
167 0 : }
168 :
169 : // question
170 : virtual async::Promise<Result> question(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons,
171 : int defBtn = int(Button::NoButton), const Options& options = {},
172 : const std::string& dialogTitle = {}) = 0;
173 :
174 10 : async::Promise<Result> question(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
175 : Button defBtn = Button::NoButton, const Options& options = {}, const std::string& dialogTitle = {})
176 : {
177 10 : return question(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
178 : }
179 :
180 : // info
181 : virtual async::Promise<Result> info(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
182 : int defBtn = int(Button::NoButton), const Options& options = {},
183 : const std::string& dialogTitle = {}) = 0;
184 :
185 : async::Promise<Result> info(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
186 : Button defBtn, const Options& options = {}, const std::string& dialogTitle = {})
187 : {
188 : return info(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
189 : }
190 :
191 : // warning
192 : virtual async::Promise<Result> warning(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
193 : int defBtn = int(Button::NoButton), const Options& options = { WithIcon },
194 : const std::string& dialogTitle = {}) = 0;
195 :
196 14 : async::Promise<Result> warning(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
197 : Button defBtn = Button::NoButton, const Options& options = { WithIcon },
198 : const std::string& dialogTitle = {})
199 : {
200 14 : return warning(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
201 : }
202 :
203 : //! Asks whether to continue with what `ret`, returned by a can-check, is about
204 4 : async::Promise<bool> askToContinue(const Ret& ret)
205 : {
206 8 : return async::make_promise<bool>([this, ret](auto resolve) {
207 16 : warning(std::string(), ret.text(), { Button::Cancel, Button::Ok })
208 4 : .onResolve(nullptr, [resolve](const Result& result) {
209 4 : (void)resolve(result.standardButton() == Button::Ok);
210 : });
211 4 : return async::Promise<bool>::dummy_result();
212 8 : }, async::PromiseType::AsyncByBody);
213 : }
214 :
215 : // error
216 : virtual async::Promise<Result> error(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
217 : int defBtn = int(Button::NoButton), const Options& options = { WithIcon },
218 : const std::string& dialogTitle = {}) = 0;
219 :
220 1 : async::Promise<Result> error(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
221 : Button defBtn = Button::NoButton, const Options& options = { WithIcon },
222 : const std::string& dialogTitle = {})
223 : {
224 1 : return error(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
225 : }
226 :
227 : // progress
228 : virtual void showProgress(const std::string& title, Progress progress) = 0;
229 :
230 : // files
231 : virtual async::Promise<io::path_t> selectOpeningFile(const std::string& title, const io::path_t& dir,
232 : const std::vector<std::string>& filter) = 0;
233 : virtual io::path_t selectOpeningFileSync(const std::string& title, const io::path_t& dir, const std::vector<std::string>& filter,
234 : const int options = 0) = 0;
235 : virtual io::paths_t selectOpeningFilesSync(const std::string& title, const io::path_t& dir, const std::vector<std::string>& filter,
236 : const int options = 0) = 0;
237 : virtual async::Promise<io::path_t> selectSavingFile(const std::string& title, const io::path_t& path,
238 : const std::vector<std::string>& filter, bool confirmOverwrite = true) = 0;
239 : virtual io::path_t selectSavingFileSync(const std::string& title, const io::path_t& path, const std::vector<std::string>& filter,
240 : bool confirmOverwrite = true) = 0;
241 :
242 : // dirs
243 : virtual io::path_t selectDirectory(const std::string& title, const io::path_t& dir) = 0;
244 : virtual io::paths_t selectMultipleDirectories(const std::string& title, const io::path_t& dir,
245 : const io::paths_t& selectedDirectories) = 0;
246 :
247 : // color
248 : virtual async::Promise<Color> selectColor(const Color& color = Color::WHITE, const std::string& title = {},
249 : bool allowAlpha = false) = 0;
250 : virtual bool isSelectColorOpened() const = 0;
251 :
252 : // custom
253 : virtual async::Promise<Val> open(const UriQuery& uri) = 0;
254 5 : async::Promise<Val> open(const std::string& uri) { return open(UriQuery(uri)); }
255 23 : async::Promise<Val> open(const Uri& uri) { return open(UriQuery(uri)); }
256 : virtual RetVal<bool> isOpened(const UriQuery& uri) const = 0;
257 : virtual RetVal<bool> isOpened(const Uri& uri) const = 0;
258 : virtual async::Channel<Uri> opened() const = 0;
259 :
260 : virtual void raise(const UriQuery& uri) = 0;
261 :
262 : virtual async::Promise<Ret> close(const UriQuery& uri) = 0;
263 : virtual async::Promise<Ret> close(const Uri& uri) = 0;
264 : virtual Ret closeSync(const UriQuery& uri) = 0;
265 : virtual async::Promise<Ret> closeAllDialogs() = 0;
266 : virtual Ret closeAllDialogsSync() = 0;
267 :
268 : // state
269 : virtual ValCh<Uri> currentUri() const = 0;
270 : virtual RetVal<bool> isCurrentUriDialog() const = 0;
271 : virtual async::Notification currentUriAboutToBeChanged() const = 0;
272 : virtual std::vector<Uri> stack() const = 0;
273 :
274 : virtual QWindow* topWindow() const = 0;
275 : virtual bool topWindowIsWidget() const = 0;
276 :
277 : //! =================================
278 : //! NOTE Please don't use this
279 : //! =================================
280 : [[deprecated("Use question() instead")]]
281 : virtual Result questionSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons,
282 : int defBtn = int(Button::NoButton), const Options& options = {}, const std::string& dialogTitle = {}) = 0;
283 :
284 : [[deprecated("Use question() instead")]]
285 : Result questionSync(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
286 : const Button& defBtn = Button::NoButton, const Options& options = {}, const std::string& dialogTitle = {})
287 : {
288 : return questionSync(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
289 : }
290 :
291 : [[deprecated("Use info() instead")]]
292 : virtual Result infoSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
293 : int defBtn = int(Button::NoButton), const Options& options = {}, const std::string& dialogTitle = {}) = 0;
294 :
295 : [[deprecated("Use info() instead")]]
296 : Result infoSync(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
297 : const Button& defBtn = Button::NoButton, const Options& options = {}, const std::string& dialogTitle = {})
298 : {
299 : return infoSync(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
300 : }
301 :
302 : [[deprecated("Use warning() instead")]]
303 : virtual Result warningSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
304 : int defBtn = int(Button::NoButton), const Options& options = { WithIcon },
305 : const std::string& dialogTitle = {}) = 0;
306 :
307 : [[deprecated("Use warning() instead")]]
308 : Result warningSync(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
309 : const Button& defBtn = Button::NoButton, const Options& options = { WithIcon }, const std::string& dialogTitle = {})
310 : {
311 : return warningSync(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
312 : }
313 :
314 : [[deprecated("Use error() instead")]]
315 : virtual Result errorSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
316 : int defBtn = int(Button::NoButton), const Options& options = { WithIcon },
317 : const std::string& dialogTitle = {}) = 0;
318 :
319 : [[deprecated("Use error() instead")]]
320 : Result errorSync(const std::string& contentTitle, const std::string& text, const Buttons& buttons,
321 : const Button& defBtn = Button::NoButton, const Options& options = { WithIcon }, const std::string& dialogTitle = {})
322 : {
323 : return errorSync(contentTitle, Text(text), buttonDataList(buttons), (int)defBtn, options, dialogTitle);
324 : }
325 :
326 : virtual RetVal<Val> openSync(const UriQuery& uri) = 0;
327 12 : RetVal<Val> openSync(const std::string& uri) { return openSync(UriQuery(uri)); }
328 : RetVal<Val> openSync(const Uri& uri) { return openSync(UriQuery(uri)); }
329 : //! ==============================
330 : };
331 6 : DECLARE_OPERATORS_FOR_FLAGS(IInteractive::Options)
332 : }
|