1 |
/* |
2 |
Copyright (c) 2014-2016 Christian Schoenebeck |
3 |
|
4 |
This file is part of "gigedit" and released under the terms of the |
5 |
GNU General Public License version 2. |
6 |
*/ |
7 |
|
8 |
#ifndef GIGEDIT_SETTINGS |
9 |
#define GIGEDIT_SETTINGS |
10 |
|
11 |
#include <typeinfo> |
12 |
#include <glibmm/object.h> |
13 |
#include <glibmm/property.h> |
14 |
#include <vector> |
15 |
|
16 |
/** |
17 |
* Reflects, saves and restores all settings for the gigedit application. |
18 |
* |
19 |
* This class holds a bunch of custom Property objects which can be accessed |
20 |
* as if they were basic data types (i.e. by using assignment operator, etc.). |
21 |
* As soon as a property gets modified this way, it will automatically be saved |
22 |
* to a local config file. |
23 |
*/ |
24 |
class Settings : public Glib::Object { |
25 |
public: |
26 |
/** |
27 |
* Data types for the individual settings. |
28 |
*/ |
29 |
enum RawValueType_t { |
30 |
BOOLEAN, |
31 |
INTEGER, |
32 |
UNKNOWN |
33 |
}; |
34 |
|
35 |
/** |
36 |
* All settings are grouped into these settings groups. |
37 |
*/ |
38 |
enum Group_t { |
39 |
GLOBAL |
40 |
}; |
41 |
|
42 |
/** |
43 |
* Extension of regular Glib::Property template class; this one |
44 |
* automatically calls Settings::onPropertyChanged() method passing this |
45 |
* property object as pointer; and it allows to assign new values to this |
46 |
* property by using the regular assignment operator; and requires a |
47 |
* "Setting group" to be assigned to the property at construction time. |
48 |
*/ |
49 |
template<typename T> |
50 |
class Property : public Glib::Property<T> { |
51 |
public: |
52 |
Property(Settings& object, Group_t group, const Glib::ustring& name) |
53 |
: Glib::Property<T>::Property(object, name) |
54 |
{ |
55 |
m_settings = &object; |
56 |
m_group = group; |
57 |
const RawValueType_t type = rawValueType(); |
58 |
Glib::Property<T>::get_proxy().signal_changed().connect( |
59 |
sigc::bind( |
60 |
sigc::bind( |
61 |
sigc::bind( |
62 |
sigc::mem_fun(m_settings, &Settings::onPropertyChanged), |
63 |
m_group |
64 |
), |
65 |
type |
66 |
), |
67 |
this |
68 |
) |
69 |
); |
70 |
} |
71 |
|
72 |
Property(Settings& object, Group_t group, const Glib::ustring& name, const T& default_value) |
73 |
: Glib::Property<T>::Property(object, name, default_value) |
74 |
{ |
75 |
m_settings = &object; |
76 |
m_group = group; |
77 |
const RawValueType_t type = rawValueType(); |
78 |
Glib::Property<T>::get_proxy().signal_changed().connect( |
79 |
sigc::bind( |
80 |
sigc::bind( |
81 |
sigc::bind( |
82 |
sigc::mem_fun(m_settings, &Settings::onPropertyChanged), |
83 |
m_group |
84 |
), |
85 |
type |
86 |
), |
87 |
this |
88 |
) |
89 |
); |
90 |
} |
91 |
|
92 |
Property<T>& operator=(const T value) { |
93 |
Glib::Property<T>::set_value(value); |
94 |
return *this; |
95 |
} |
96 |
|
97 |
RawValueType_t rawValueType() const { |
98 |
const std::string name = typeid(T).name(); |
99 |
if (name == "bool" || name == "b") return BOOLEAN; |
100 |
if (name == "int" || name == "i") return INTEGER; |
101 |
return UNKNOWN; |
102 |
} |
103 |
|
104 |
Group_t group() const { return m_group; } |
105 |
|
106 |
private: |
107 |
Settings* m_settings; |
108 |
Group_t m_group; |
109 |
}; |
110 |
|
111 |
Property<bool> warnUserOnExtensions; ///< if enabled, the user shall he be warned if he is trying to use a gig format extension that will not work with Gigasampler/GigaStudio |
112 |
Property<bool> syncSamplerInstrumentSelection; ///< if enabled, the sampler's current instrument will automatically be switched whenever another instrument was selected in gigedit |
113 |
Property<bool> moveRootNoteWithRegionMoved; ///< if enabled, the root note(s) of regions are automatically moving when the user drags a region around at the virtual keyboard |
114 |
|
115 |
static Settings* singleton(); |
116 |
Settings(); |
117 |
void load(); |
118 |
|
119 |
protected: |
120 |
void onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group); |
121 |
|
122 |
private: |
123 |
std::vector<Glib::PropertyBase*> m_boolProps; ///< Pointers to all 'bool' type properties this Setting class manages. |
124 |
std::vector<Glib::PropertyBase*> m_intProps; ///< Pointers to all 'int' type properties this Setting class manages. |
125 |
bool m_ignoreNotifies; |
126 |
}; |
127 |
|
128 |
#endif // GIGEDIT_SETTINGS |