1 //=============================================================================
2 //  MuseScore
3 //  Linux Music Score Editor
4 //
5 //  Copyright (C) 2002-2017 Werner Schweer and others
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __PREFERENCESLISTWIDGET_H__
21 #define __PREFERENCESLISTWIDGET_H__
22 
23 #include "awl/colorlabel.h"
24 #include "preferences.h"
25 
26 #define PREF_VALUE_COLUMN 1
27 
28 namespace Ms {
29 
30 //---------------------------------------------------------
31 //   PreferenceItem
32 //---------------------------------------------------------
33 class PreferenceItem : public QObject, public QTreeWidgetItem {
34 
35       Q_OBJECT
36 
37       QString _name;
38 
39     protected:
40       void apply(QVariant value);
41 
42     public:
43       PreferenceItem(QString name);
44 
45       virtual void apply() = 0;
46       virtual void update(bool setup = false) = 0;
47       virtual void setDefaultValue() = 0;
48       virtual QWidget* editor() const = 0;
49       virtual bool isModified() const = 0;
50       virtual void setInitialValueToEditor() = 0;
51 
name()52       QString name() const {return _name;}
53 
54    signals:
55       void editorValueModified();
56       };
57 
58 //---------------------------------------------------------
59 //   BoolPreferenceItem
60 //---------------------------------------------------------
61 class BoolPreferenceItem : public PreferenceItem {
62       bool _initialValue                        { false };
63       QCheckBox* _editorCheckBox                { nullptr };
64       QGroupBox* _editorGroupBox                { nullptr };
65       QRadioButton* _editorRadioButton          { nullptr };
66       std::function<void()> _applyFunction      { nullptr };
67       std::function<void()> _updateFunction     { nullptr };
68 
69    public:
70       BoolPreferenceItem(QString name, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
71       BoolPreferenceItem(QString name, QCheckBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
72       BoolPreferenceItem(QString name, QGroupBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
73       BoolPreferenceItem(QString name, QRadioButton* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
74 
75       void apply() override;
76       void update(bool setup = false) override;
77       void setDefaultValue() override;
78       QWidget* editor() const override;
79       bool isModified() const override;
80       void setInitialValueToEditor() override;
81       };
82 
83 
84 //---------------------------------------------------------
85 //   IntPreferenceItem
86 //---------------------------------------------------------
87 class IntPreferenceItem : public PreferenceItem {
88       int _initialValue                         { 0 };
89       int _initialEditorIndex                   { -1 };
90       QSpinBox* _editorSpinBox                  { nullptr };
91       QComboBox* _editorComboBox                { nullptr };
92       std::function<void()> _applyFunction      { nullptr };
93       std::function<void()> _updateFunction     { nullptr };
94 
95    public:
96       IntPreferenceItem(QString name, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
97       IntPreferenceItem(QString name, QSpinBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
98       IntPreferenceItem(QString name, QComboBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
99 
100       void apply() override;
101       void update(bool setup = false) override;
102       void setDefaultValue() override;
103       QWidget* editor() const override;
104       bool isModified() const override;
105       void setInitialValueToEditor() override;
106       };
107 
108 //---------------------------------------------------------
109 //   DoublePreferenceItem
110 //---------------------------------------------------------
111 class DoublePreferenceItem : public PreferenceItem {
112       double _initialValue                      { 0 };
113       int _initialEditorIndex                   { -1 };
114       QDoubleSpinBox* _editorDoubleSpinBox      { nullptr };
115       QComboBox* _editorComboBox                { nullptr };
116       QSpinBox* _editorSpinBox                  { nullptr };
117       std::function<void()> _applyFunction      { nullptr };
118       std::function<void()> _updateFunction     { nullptr };
119 
120    public:
121       DoublePreferenceItem(QString name, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
122       DoublePreferenceItem(QString name, QDoubleSpinBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
123       DoublePreferenceItem(QString name, QComboBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
124       DoublePreferenceItem(QString name, QSpinBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
125 
126       void apply() override;
127       void update(bool setup = false) override;
128       void setDefaultValue() override;
129       QWidget* editor() const override;
130       bool isModified() const override;
131       void setInitialValueToEditor() override;
132       };
133 
134 //---------------------------------------------------------
135 //   StringPreferenceItem
136 //---------------------------------------------------------
137 class StringPreferenceItem : public PreferenceItem {
138       QString _initialValue                     { "" };
139       int _initialEditorIndex                   { -1 };
140       bool _initialIsChecked                    { false };
141       QLineEdit* _editorLineEdit                { nullptr };
142       QFontComboBox* _editorFontComboBox        { nullptr };
143       QComboBox* _editorComboBox                { nullptr };
144       QRadioButton* _editorRadioButton          { nullptr };
145       std::function<void()> _applyFunction      { nullptr };
146       std::function<void()> _updateFunction     { nullptr };
147 
148    public:
149       StringPreferenceItem(QString name, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
150       StringPreferenceItem(QString name, QLineEdit* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
151       StringPreferenceItem(QString name, QFontComboBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
152       StringPreferenceItem(QString name, QComboBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
153       StringPreferenceItem(QString name, QRadioButton* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr); // remove nullptr default since you cannot not have an apply and update func
154 
155       void apply() override;
156       void update(bool setup = false) override;
157       void setDefaultValue() override;
158       QWidget* editor() const override;
159       bool isModified() const override;
160       void setInitialValueToEditor() override;
161       };
162 
163 //---------------------------------------------------------
164 //   ColorPreferenceItem
165 //---------------------------------------------------------
166 class ColorPreferenceItem : public PreferenceItem {
167       QColor _initialValue;
168       Awl::ColorLabel* _editorColorLabel        { nullptr };
169       std::function<void()> _applyFunction      { nullptr };
170       std::function<void()> _updateFunction     { nullptr };
171 
172    public:
173       ColorPreferenceItem(QString name, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
174       ColorPreferenceItem(QString name, Awl::ColorLabel* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
175 
176       void apply() override;
177       void update(bool setup = false) override;
178       void setDefaultValue() override;
179       QWidget* editor() const override;
180       bool isModified() const override;
181       void setInitialValueToEditor() override;
182       };
183 
184 //---------------------------------------------------------
185 //   CustomPreferenceItem
186 //---------------------------------------------------------
187 class CustomPreferenceItem : public PreferenceItem {
188       int _initialEditorIndex                   { -1 };
189       bool _initialIsChecked                    { false };
190       QRadioButton* _editorRadioButton          { nullptr };
191       QComboBox* _editorComboBox                { nullptr };
192       std::function<void()> _applyFunction      { nullptr };
193       std::function<void()> _updateFunction     { nullptr };
194 
195    public:
196       CustomPreferenceItem(QString name, QRadioButton* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
197       CustomPreferenceItem(QString name, QComboBox* editor, std::function<void()> applyFunc = nullptr, std::function<void()> updateFunc = nullptr);
198 
199       void apply() override;
200       void update(bool setup = false) override;
201       void setDefaultValue() override;
202       QWidget* editor() const override;
203       bool isModified() const override;
204       void setInitialValueToEditor() override;
205       };
206 
207 //---------------------------------------------------------
208 //   PreferencesListWidget
209 //---------------------------------------------------------
210 
211 class PreferencesListWidget : public QTreeWidget, public PreferenceVisitor {
212       Q_OBJECT
213 
214       QHash<QString, PreferenceItem*> _preferenceItems;
215 
216       void addPreference(PreferenceItem* item);
217 
218    public:
219       explicit PreferencesListWidget(QWidget* parent = 0);
220       void loadPreferences();
221       void updatePreferences();
222 
223       std::vector<QString> save();
224 
225       void visit(QString key, IntPreference*);
226       void visit(QString key, DoublePreference*);
227       void visit(QString key, BoolPreference*);
228       void visit(QString key, StringPreference*);
229       void visit(QString key, ColorPreference*);
230 
preferenceItems()231       QHash<QString, PreferenceItem*> preferenceItems() const { return _preferenceItems; };
232       };
233 
234 } // namespace Ms
235 
236 #endif // __PREFERENCESLISTWIDGET_H__
237