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 "dockwindowactionscontroller.h"
24 :
25 : #include "../idockwindow.h"
26 : #include "../dockcommands.h"
27 :
28 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
29 : #include "rcommand/actiontocommand.h"
30 : #endif
31 :
32 : using namespace muse;
33 : using namespace muse::dock;
34 : using namespace muse::rcommand;
35 :
36 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
37 : using namespace muse::actions;
38 0 : static CommandQuery setOpenConv(const Command& command, const ActionData& args)
39 : {
40 0 : CommandQuery query(command);
41 0 : if (args.count() < 2) {
42 0 : return query;
43 : }
44 0 : query.addParam("dock_name", Val(args.arg<QString>(0).toStdString()));
45 0 : query.addParam("open", Val(args.arg<bool>(1)));
46 0 : return query;
47 0 : }
48 :
49 0 : static CommandQuery toggleConv(const Command& command, const ActionData& args)
50 : {
51 0 : CommandQuery query(command);
52 0 : if (args.count() < 1) {
53 0 : return query;
54 : }
55 0 : query.addParam("dock_name", Val(args.arg<QString>(0).toStdString()));
56 0 : return query;
57 0 : }
58 :
59 : #endif
60 :
61 0 : void DockWindowActionsController::init()
62 : {
63 0 : auto cd = commandsDispatcher();
64 0 : cd->onRequest(this, DOCK_SET_OPEN_COMMAND, [this](const rcommand::Params& params) { return setDockOpen(params); });
65 0 : cd->onRequest(this, DOCK_TOGGLE_COMMAND, [this](const rcommand::Params& params) { return toggleOpened(params); });
66 0 : cd->onRequest(this, DOCK_TOGGLE_FLOATING_COMMAND, [this](const rcommand::Params& params) { return toggleFloating(params); });
67 0 : cd->onRequest(this, DOCK_RESTORE_DEFAULT_LAYOUT_COMMAND, [this]() { return restoreDefaultLayout(); });
68 :
69 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
70 : // compat
71 : {
72 : static std::vector<ActionToCommand> actionsToCommands = {
73 0 : { "dock-set-open", DOCK_SET_OPEN_COMMAND, setOpenConv },
74 : { "dock-toggle", DOCK_TOGGLE_COMMAND, toggleConv },
75 : { "dock-toggle-floating", DOCK_TOGGLE_FLOATING_COMMAND, toggleConv },
76 : { "dock-restore-default-layout", DOCK_RESTORE_DEFAULT_LAYOUT_COMMAND, {} },
77 0 : };
78 :
79 0 : rcommand::registerActionToCommand(this, actionsToCommands, commandsDispatcher(), dispatcher());
80 : }
81 : #endif
82 0 : }
83 :
84 0 : muse::Ret DockWindowActionsController::setDockOpen(const rcommand::Params& params)
85 : {
86 0 : if (!(params.contains("dock_name") && params.contains("open"))) {
87 0 : return muse::make_ret(Ret::Code::BadArgs);
88 : }
89 :
90 0 : QString dockName = QString::fromStdString(params.at("dock_name").toString());
91 0 : bool open = params.at("open").toBool();
92 :
93 0 : window()->setDockOpen(dockName, open);
94 0 : return muse::make_ok();
95 0 : }
96 :
97 0 : muse::Ret DockWindowActionsController::toggleOpened(const rcommand::Params& params)
98 : {
99 0 : if (!params.contains("dock_name")) {
100 0 : return muse::make_ret(Ret::Code::BadArgs);
101 : }
102 :
103 0 : QString dockName = QString::fromStdString(params.at("dock_name").toString());
104 0 : window()->toggleDock(dockName);
105 0 : return muse::make_ok();
106 0 : }
107 :
108 0 : muse::Ret DockWindowActionsController::toggleFloating(const rcommand::Params& params)
109 : {
110 0 : if (!params.contains("dock_name")) {
111 0 : return muse::make_ret(Ret::Code::BadArgs);
112 : }
113 :
114 0 : QString dockName = QString::fromStdString(params.at("dock_name").toString());
115 0 : window()->toggleDockFloating(dockName);
116 0 : return muse::make_ok();
117 0 : }
118 :
119 0 : IDockWindow* DockWindowActionsController::window() const
120 : {
121 0 : return dockWindowProvider()->window();
122 : }
123 :
124 0 : muse::Ret DockWindowActionsController::restoreDefaultLayout()
125 : {
126 0 : window()->restoreDefaultLayout();
127 0 : return muse::make_ok();
128 : }
|