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) 2025 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 <string>
26 :
27 : #include "types/val.h"
28 : #include "async/channel.h"
29 : #include "io/path.h"
30 :
31 : #include "muse_framework_config.h"
32 : #ifdef MUSE_MODULE_MULTIWINDOWS
33 : #include "modularity/ioc.h"
34 : #include "multiwindows/imultiwindowsprovider.h"
35 : #endif
36 :
37 : //! NOTE We are gradually abandoning Qt in non-GUI classes.
38 : //! This settings interface is almost independent of Qt,
39 : //! QSettings are used only in the implementation for compatibility with current settings.
40 : //! Perhaps in the future this will be changed.
41 :
42 : class QSettings;
43 :
44 : namespace muse {
45 : class Settings
46 : {
47 : #ifdef MUSE_MODULE_MULTIWINDOWS
48 : GlobalInject<muse::mi::IMultiWindowsProvider> multiwindowsProvider;
49 : #endif
50 : public:
51 : static Settings* instance();
52 :
53 : struct Key
54 : {
55 : std::string moduleName;
56 : std::string key;
57 :
58 461 : Key() = default;
59 : Key(std::string moduleName, std::string key);
60 :
61 : bool isNull() const;
62 : bool operator==(const Key& k) const;
63 : bool operator<(const Key& k) const;
64 : };
65 :
66 : struct Item
67 : {
68 : Key key;
69 : Val value;
70 : Val defaultValue;
71 : std::string description;
72 :
73 : bool canBeManuallyEdited = false;
74 : Val minValue;
75 : Val maxValue;
76 :
77 2950 : bool isNull() const { return key.isNull(); }
78 : };
79 :
80 : using Items = std::map<Key, Item>;
81 :
82 : const Items& items() const;
83 :
84 : void reload();
85 : void load();
86 :
87 : void reset(bool keepDefaultSettings = false, bool notifyAboutChanges = true, bool notifyOtherInstances = true);
88 :
89 : const Val& value(const Key& key) const;
90 : const Val& defaultValue(const Key& key) const;
91 :
92 : //! NOTE Will be write to global config and sync between all instances
93 : void setSharedValue(const Key& key, const Val& value);
94 :
95 : //! NOTE Will be write to global config and NOT sync between instances
96 : void setLocalValue(const Key& key, const Val& value);
97 :
98 : void setDefaultValue(const Key& key, const Val& value);
99 :
100 : void setDescription(const Key& key, const std::string& value);
101 :
102 : void setCanBeManuallyEdited(const Settings::Key& key, bool canBeManuallyEdited, const Val& minValue = Val(),
103 : const Val& maxValue = Val());
104 :
105 : void beginTransaction(bool notifyToOtherInstances = true);
106 : void commitTransaction(bool notifyToOtherInstances = true);
107 : void rollbackTransaction(bool notifyToOtherInstances = true);
108 :
109 : async::Channel<Val> valueChanged(const Key& key) const;
110 :
111 : io::path_t filePath() const;
112 :
113 : private:
114 : Settings();
115 : ~Settings();
116 :
117 : Item& findItem(const Key& key) const;
118 : async::Channel<Val>& findChannel(const Key& key) const;
119 :
120 : void insertNewItem(const Key& key, const Val& value);
121 :
122 : Items readItems() const;
123 : void writeValue(const Key& key, const Val& value);
124 :
125 : QString dataPath() const;
126 :
127 : QSettings* m_settings = nullptr;
128 : mutable Items m_items;
129 : mutable Items m_localSettings;
130 : mutable bool m_isTransactionStarted = false;
131 : mutable std::map<Key, async::Channel<Val> > m_channels;
132 : };
133 :
134 41277 : inline Settings* settings()
135 : {
136 41277 : return Settings::instance();
137 : }
138 : }
|