Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 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 "palettecommandsstate.h"
24 :
25 : #include "../palettecommands.h"
26 :
27 : #include "log.h"
28 :
29 : using namespace muse;
30 : using namespace muse::rcommand;
31 : using namespace mu::palette;
32 :
33 0 : std::string PaletteCommandsState::moduleName() const
34 : {
35 0 : return "palette";
36 : }
37 :
38 0 : void PaletteCommandsState::init()
39 : {
40 0 : m_moduleRegister = commandsRegister()->moduleRegister(moduleName());
41 0 : IF_ASSERT_FAILED(m_moduleRegister) {
42 0 : return;
43 : }
44 :
45 0 : projectSession()->projectChanged().onNotify(this, [this]() {
46 0 : updateCommandStates();
47 0 : });
48 :
49 0 : configuration()->isSingleClickToOpenPalette().ch.onReceive(this, [this](bool) {
50 0 : updateCommandStates({ PALETTE_TOGGLE_SINGLE_CLICK_TO_OPEN_COMMAND });
51 0 : });
52 :
53 0 : configuration()->isSinglePalette().ch.onReceive(this, [this](bool) {
54 0 : updateCommandStates({ PALETTE_TOGGLE_SINGLE_PALETTE_COMMAND });
55 0 : });
56 :
57 0 : configuration()->isPaletteDragEnabled().ch.onReceive(this, [this](bool) {
58 0 : updateCommandStates({ PALETTE_TOGGLE_DRAG_ENABLED_COMMAND });
59 0 : });
60 :
61 0 : updateCommandStates();
62 : }
63 :
64 0 : void PaletteCommandsState::deinit()
65 : {
66 0 : projectSession()->projectChanged().disconnect(this);
67 0 : }
68 :
69 0 : void PaletteCommandsState::updateCommandStates(const std::vector<Command>& commands)
70 : {
71 0 : IF_ASSERT_FAILED(m_moduleRegister) {
72 0 : return;
73 : }
74 :
75 0 : const auto& commandList = commands.empty() ? m_moduleRegister->commandList() : commands;
76 :
77 0 : for (const auto& command : commandList) {
78 0 : CommandState newState = commandState(command);
79 0 : if (m_commandStates[command] != newState) {
80 0 : m_commandStates[command] = newState;
81 0 : m_commandStateChanged.send(command, newState);
82 : }
83 : }
84 : }
85 :
86 0 : CommandState PaletteCommandsState::commandState(const Command& command) const
87 : {
88 0 : if (!isProjectOpened()) {
89 0 : return CommandState(false, false);
90 : }
91 :
92 0 : if (command == PALETTE_TOGGLE_SINGLE_CLICK_TO_OPEN_COMMAND) {
93 0 : return CommandState(true, configuration()->isSingleClickToOpenPalette().val);
94 0 : } else if (command == PALETTE_TOGGLE_SINGLE_PALETTE_COMMAND) {
95 0 : return CommandState(true, configuration()->isSinglePalette().val);
96 0 : } else if (command == PALETTE_TOGGLE_DRAG_ENABLED_COMMAND) {
97 0 : return CommandState(true, configuration()->isPaletteDragEnabled().val);
98 : }
99 :
100 : UNUSED(command);
101 0 : return CommandState(true, false);
102 : }
103 :
104 0 : async::Channel<Command, CommandState> PaletteCommandsState::commandStateChanged() const
105 : {
106 0 : return m_commandStateChanged;
107 : }
108 :
109 0 : bool PaletteCommandsState::isProjectOpened() const
110 : {
111 0 : if (projectSession()->project()) {
112 0 : return true;
113 : }
114 :
115 0 : return false;
116 : }
|