Debian Codegraph @

src/Settings.cpp

raw symbols SCIP
1 #include "Settings.h"
2 #include <QApplication>
3 #include <QDebug>
4
5 static const char* DebuggerConfigNames[Settings::CONFIG_END] = {
6 "AutoReloadSymbols", "PreserveLostSymbols", "PreserveBreakpointSymbol"
7 };
8
9 static const char* DebuggerFontNames[Settings::FONT_END] = {
10 "Application Font", "Default Fixed Font", "Code Font",
11 "Label Font", "Hex viewer font"
12 };
13
14 static QString configLocation(Settings::DebuggerConfig c)
15 {
16 return QString("Config/").append(DebuggerConfigNames[c]);
17 }
18
19 static QString fontLocation(Settings::DebuggerFont f)
20 {
21 return QString("Fonts/").append(DebuggerFontNames[f]);
22 }
23
24 static QString fontColorLocation(Settings::DebuggerFont f)
25 {
26 return QString("Fonts/%1 Color").arg(DebuggerFontNames[f]);
27 }
28
29 Settings::Settings()
30 : QSettings("openMSX", "debugger")
31 {
32 getConfigFromSettings();
33 getFontsFromSettings();
34 }
35
36 Settings& Settings::get()
37 {
38 static Settings instance;
39 return instance;
40 }
41
42 void Settings::getBoolFromSetting(DebuggerConfig c, bool defaultValue)
43 {
44 QVariant v = value(configLocation(c));
45
46 if (v.canConvert<bool>()) {
47 config[c] = v;
48 } else {
49 // default value
50 config[c] = defaultValue;
51 }
52 }
53
54 void Settings::getConfigFromSettings()
55 {
56 getBoolFromSetting(AUTO_RELOAD_SYMBOLS, false);
57 getBoolFromSetting(PRESERVE_LOST_SYMBOLS, true);
58 getBoolFromSetting(PRESERVE_BREAKPOINT_SYMBOL, false);
59 }
60
61 void Settings::setConfig(DebuggerConfig c, const QVariant& v)
62 {
63 config[c] = v;
64 setValue(configLocation(c), config[c]);
65 }
66
67 void Settings::getFontsFromSettings()
68 {
69 // first get application default
70 QVariant v = value(fontLocation(APP_FONT));
71 if (v.type() == QVariant::Font) {
72 fonts[APP_FONT] = v.value<QFont>();
73 fontTypes[APP_FONT] = CUSTOM;
74 } else {
75 fontTypes[APP_FONT] = APPLICATION_DEFAULT;
76 fonts[APP_FONT] = qApp->font();
77 }
78
79 // then get default fixed spacing font
80 v = value(fontLocation(FIXED_FONT));
81 if (v.type() == QVariant::Font) {
82 fonts[FIXED_FONT] = v.value<QFont>();
83 fontTypes[FIXED_FONT] = CUSTOM;
84 } else {
85 fontTypes[FIXED_FONT] = APPLICATION_DEFAULT;
86 fonts[FIXED_FONT] = fonts[APP_FONT];
87 }
88
89 // then the rest
90 for (int f = CODE_FONT; f < FONT_END; ++f) {
91 v = value(fontLocation((DebuggerFont)f));
92 if (v.type() == QVariant::Font) {
93 fonts[f] = v.value<QFont>();
94 fontTypes[f] = CUSTOM;
95 } else if (v.toString() == "FixedDefault") {
96 fonts[f] = fonts[FIXED_FONT];
97 fontTypes[f] = FIXED_DEFAULT;
98 } else {
99 fonts[f] = fonts[APP_FONT];
100 fontTypes[f] = CUSTOM;
101 }
102 }
103
104 // read colors
105 for (int f =CODE_FONT; f < FONT_END; ++f) {
106 fontColors[f] =
107 value(fontColorLocation((DebuggerFont)f), QColor(Qt::black))
108 .value<QColor>();
109 }
110 }
111
112 QString Settings::fontName(DebuggerFont f) const
113 {
114 return DebuggerFontNames[f];
115 }
116
117 const QFont& Settings::font(DebuggerFont f) const
118 {
119 return fonts[f];
120 }
121
122 void Settings::setFont(DebuggerFont f, const QFont& ft)
123 {
124 fontTypes[f] = CUSTOM;
125 fonts[f] = ft;
126 setValue(fontLocation(f), fonts[f]);
127 if (f <= FIXED_FONT) updateFonts();
128 }
129
130 Settings::DebuggerFontType Settings::fontType(DebuggerFont f) const
131 {
132 return fontTypes[f];
133 }
134
135 void Settings::setFontType(DebuggerFont f, DebuggerFontType t)
136 {
137 if (fontTypes[f] == t) return;
138
139 fontTypes[f] = t;
140 switch (t) {
141 case APPLICATION_DEFAULT:
142 setValue(fontLocation(f), "AppDefault");
143 if (f == APP_FONT) {
144 fonts[f] = qApp->font();
145 } else {
146 fonts[f] = fonts[APP_FONT];
147 }
148 break;
149 case FIXED_DEFAULT:
150 if (f > FIXED_FONT) {
151 setValue(fontLocation(f), "FixedDefault");
152 fonts[f] = fonts[FIXED_FONT];
153 }
154 break;
155 case CUSTOM:
156 setValue(fontLocation(f), fonts[f]);
157 break;
158 }
159 if (f > FIXED_FONT) updateFonts();
160 }
161
162 const QColor& Settings::fontColor(DebuggerFont f) const
163 {
164 return fontColors[f];
165 }
166
167 void Settings::setFontColor(DebuggerFont f, const QColor& c)
168 {
169 if (f > FIXED_FONT) {
170 fontColors[f] = c;
171 setValue(fontColorLocation(f), c);
172 }
173 }
174
175 bool Settings::autoReloadSymbols() const
176 {
177 return config[AUTO_RELOAD_SYMBOLS].value<bool>();
178 }
179
180 void Settings::setAutoReloadSymbols(bool b)
181 {
182 setConfig(AUTO_RELOAD_SYMBOLS, b);
183 }
184
185 bool Settings::preserveLostSymbols() const
186 {
187 return config[PRESERVE_LOST_SYMBOLS].value<bool>();
188 }
189
190 void Settings::setPreserveLostSymbols(bool b)
191 {
192 setConfig(PRESERVE_LOST_SYMBOLS, b);
193 }
194
195 bool Settings::preserveBreakpointSymbol() const
196 {
197 return config[PRESERVE_BREAKPOINT_SYMBOL].value<bool>();
198 }
199
200 void Settings::setPreserveBreakpointSymbol(bool b)
201 {
202 setConfig(PRESERVE_BREAKPOINT_SYMBOL, b);
203 }
204
205 void Settings::updateFonts()
206 {
207 for (int f = CODE_FONT; f < FONT_END; ++f) {
208 switch (fontTypes[f]) {
209 case APPLICATION_DEFAULT:
210 fonts[f] = fonts[APP_FONT];
211 break;
212 case FIXED_DEFAULT:
213 fonts[f] = fonts[FIXED_FONT];
214 break;
215 default:
216 break;
217 }
218 }
219 }