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 <QObject>
26 : #include <QVariant>
27 : #include <QMap>
28 : #include <QStack>
29 :
30 : #include "async/asyncable.h"
31 :
32 : #include "modularity/ioc.h"
33 : #include "ui/imainwindow.h"
34 : #include "ui/iuiconfiguration.h"
35 :
36 : #include "../iinteractive.h"
37 : #include "iinteractiveprovider.h"
38 : #include "iinteractiveuriregister.h"
39 :
40 : #include "muse_framework_config.h"
41 : #ifdef MUSE_MODULE_SHORTCUTS_V2
42 : #include "shortcuts_v2/ishortcutscontroller.h"
43 : #else
44 : #include "shortcuts/ishortcutsregister.h"
45 : #endif
46 :
47 : namespace muse::interactive {
48 : class Interactive : public QObject, public IInteractive, public IInteractiveProvider, public Contextable, public async::Asyncable
49 : {
50 : Q_OBJECT
51 :
52 : GlobalInject<ui::IUiConfiguration> uiConfiguration;
53 : GlobalInject<interactive::IInteractiveUriRegister> uriRegister;
54 : #ifdef MUSE_MODULE_SHORTCUTS_V2
55 : ContextInject<shortcuts::IShortcutsController> shortcutsController = { this };
56 : #else
57 : ContextInject<shortcuts::IShortcutsRegister> shortcutsRegister = { this };
58 : #endif
59 : ContextInject<ui::IMainWindow> mainWindow = { this };
60 :
61 : public:
62 : explicit Interactive(const muse::modularity::ContextPtr& ctx);
63 :
64 : ButtonData buttonData(Button b) const override;
65 :
66 : // question
67 : [[deprecated("Use question() instead")]]
68 : Result questionSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons, int defBtn = int(Button::NoButton),
69 : const Options& options = {}, const std::string& dialogTitle = "") override;
70 :
71 : async::Promise<Result> question(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons,
72 : int defBtn = int(Button::NoButton), const Options& options = {},
73 : const std::string& dialogTitle = "") override;
74 :
75 : // info
76 : [[deprecated("Use info() instead")]]
77 : Result infoSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons, int defBtn = int(Button::NoButton),
78 : const Options& options = {}, const std::string& dialogTitle = "") override;
79 :
80 : async::Promise<Result> info(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
81 : int defBtn = int(Button::NoButton), const Options& options = {},
82 : const std::string& dialogTitle = "") override;
83 :
84 : // warning
85 : [[deprecated("Use warning() instead")]]
86 : Result warningSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons, int defBtn = int(Button::NoButton),
87 : const Options& options = {}, const std::string& dialogTitle = "") override;
88 :
89 : async::Promise<Result> warning(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
90 : int defBtn = int(Button::NoButton), const Options& options = {},
91 : const std::string& dialogTitle = "") override;
92 :
93 : // error
94 : [[deprecated("Use error() instead")]]
95 : Result errorSync(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons, int defBtn = int(Button::NoButton),
96 : const Options& options = { WithIcon }, const std::string& dialogTitle = "") override;
97 :
98 : async::Promise<Result> error(const std::string& contentTitle, const Text& text, const ButtonDatas& buttons = {},
99 : int defBtn = int(Button::NoButton), const Options& options = { WithIcon },
100 : const std::string& dialogTitle = "") override;
101 :
102 : // progress
103 : void showProgress(const std::string& title, Progress progress) override;
104 :
105 : // files
106 : async::Promise<io::path_t> selectOpeningFile(const std::string& title, const io::path_t& dir,
107 : const std::vector<std::string>& filter) override;
108 : io::path_t selectOpeningFileSync(const std::string& title, const io::path_t& dir, const std::vector<std::string>& filter,
109 : const int options) override;
110 : io::paths_t selectOpeningFilesSync(const std::string& title, const io::path_t& dir, const std::vector<std::string>& filter,
111 : const int options) override;
112 : async::Promise<io::path_t> selectSavingFile(const std::string& title, const io::path_t& path, const std::vector<std::string>& filter,
113 : bool confirmOverwrite = true) override;
114 : io::path_t selectSavingFileSync(const std::string& title, const io::path_t& path, const std::vector<std::string>& filter,
115 : bool confirmOverwrite = true) override;
116 :
117 : // dirs
118 : io::path_t selectDirectory(const std::string& title, const io::path_t& dir) override;
119 : io::paths_t selectMultipleDirectories(const std::string& title, const io::path_t& dir, const io::paths_t& selectedDirectories) override;
120 :
121 : // color
122 : async::Promise<Color> selectColor(const Color& color = Color::WHITE, const std::string& title = {}, bool allowAlpha = false) override;
123 : bool isSelectColorOpened() const override;
124 :
125 : // custom
126 : RetVal<Val> openSync(const UriQuery& uri) override;
127 : async::Promise<Val> open(const UriQuery& uri) override;
128 : RetVal<bool> isOpened(const UriQuery& uri) const override;
129 : RetVal<bool> isOpened(const Uri& uri) const override;
130 : async::Channel<Uri> opened() const override;
131 :
132 : void raise(const UriQuery& uri) override;
133 :
134 : async::Promise<Ret> close(const UriQuery& uri) override;
135 : async::Promise<Ret> close(const Uri& uri) override;
136 : Ret closeSync(const UriQuery& uri) override;
137 : async::Promise<Ret> closeAllDialogs() override;
138 : Ret closeAllDialogsSync() override;
139 :
140 : // state
141 : ValCh<Uri> currentUri() const override;
142 : RetVal<bool> isCurrentUriDialog() const override;
143 : async::Notification currentUriAboutToBeChanged() const override;
144 : std::vector<Uri> stack() const override;
145 :
146 : QWindow* topWindow() const override;
147 : bool topWindowIsWidget() const override;
148 :
149 : // IInteractiveProvider interface
150 : QString objectId(const QVariant& val) const override;
151 :
152 : void onOpen(const QVariant& type, const QVariant& objectId, QObject* window = nullptr) override;
153 : void onClose(const QString& objectId, const QVariant& rv) override;
154 :
155 0 : async::Channel<QmlLaunchData*> openRequested() const override { return m_openRequested; }
156 0 : async::Channel<QVariant> closeRequested() const override { return m_closeRequested; }
157 0 : async::Channel<QVariant> raiseRequested() const override { return m_raiseRequested; }
158 :
159 : private:
160 : struct OpenData {
161 : QString objectId;
162 : };
163 :
164 : struct ObjectInfo {
165 : UriQuery query;
166 : async::Promise<Val>::Resolve resolve;
167 : async::Promise<Val>::Reject reject;
168 : QVariant objectId;
169 : QObject* window = nullptr;
170 : };
171 :
172 : async::Promise<Val> openAsync(const UriQuery& uri);
173 : async::Promise<Val> openAsync(const Uri& uri, const QVariantMap& params);
174 : async::Promise<Val> doOpenAsync(const async::Promise<Val>::BodyResolveReject& open);
175 :
176 : async::Promise<Val>::BodyResolveReject openFunc(const UriQuery& q);
177 : async::Promise<Val>::BodyResolveReject openFunc(const UriQuery& q, const QVariantMap& params);
178 :
179 : void raiseWindowInStack(QObject* newActiveWindow);
180 :
181 : void fillData(QmlLaunchData* data, const Uri& uri, const QVariantMap& params) const;
182 : void fillData(QObject* object, const QVariantMap& params) const;
183 :
184 : Ret toRet(const QVariant& jsr) const;
185 : RetVal<Val> toRetVal(const QVariant& jsrv) const;
186 :
187 : RetVal<bool> isOpened(const QString& objectId) const;
188 :
189 : RetVal<OpenData> openWidgetDialog(const Uri& uri, const QVariantMap& params);
190 : RetVal<OpenData> openQml(const Uri& uri, const QVariantMap& params);
191 :
192 : async::Promise<Ret> closeObjects(const std::vector<ObjectInfo>& objs);
193 : Ret closeObjectsSync(const std::vector<ObjectInfo>& objs);
194 :
195 : void closeQml(const QVariant& objectId);
196 : void raiseQml(const QVariant& objectId);
197 :
198 : std::vector<ObjectInfo> allOpenObjects() const;
199 : std::vector<ObjectInfo> collectOpenObjects(std::function<bool(const ObjectInfo&)> accepted) const;
200 : std::vector<ObjectInfo> openDialogs() const;
201 :
202 : void notifyAboutCurrentUriChanged();
203 : void notifyAboutCurrentUriWillBeChanged();
204 :
205 : UriQuery makeQuery(const std::string& type, const std::string& contentTitle, const Text& text, const ButtonDatas& buttons, int defBtn,
206 : const Options& options, const std::string& dialogTitle) const;
207 :
208 : IInteractive::Result makeResult(const Val& val) const;
209 :
210 : async::Promise<IInteractive::Result> openStandardAsync(const std::string& type, const std::string& contentTitle, const Text& text,
211 : const ButtonDatas& buttons, int defBtn, const Options& options,
212 : const std::string& dialogTitle);
213 :
214 : IInteractive::Result openStandardSync(const std::string& type, const std::string& contentTitle, const Text& text,
215 : const ButtonDatas& buttons, int defBtn, const Options& options, const std::string& dialogTitle);
216 :
217 : ObjectInfo m_openingObject;
218 :
219 : QStack<ObjectInfo> m_stack;
220 : std::vector<ObjectInfo> m_floatingObjects;
221 :
222 : async::Channel<Uri> m_currentUriChanged;
223 : async::Notification m_currentUriAboutToBeChanged;
224 : async::Channel<Uri> m_opened;
225 :
226 : async::Channel<QmlLaunchData*> m_openRequested;
227 : async::Channel<QVariant> m_closeRequested;
228 : async::Channel<QVariant> m_raiseRequested;
229 :
230 : std::map<QString /*objectId*/, std::function<void(const Ret&)> > m_onClosedFuncs;
231 :
232 : bool m_isSelectColorOpened = false;
233 : };
234 : }
|