comparison base/Preferences.cpp @ 156:059b0322009c

* Replace all uses of ConfigFile with QSettings
author Chris Cannam
date Thu, 03 Aug 2006 16:04:00 +0000
parents 4b2ea82fd0ed
children da3701a3953e
comparison
equal deleted inserted replaced
155:ae9be6b6b522 156:059b0322009c
17 17
18 #include "Exceptions.h" 18 #include "Exceptions.h"
19 19
20 #include "TempDirectory.h" 20 #include "TempDirectory.h"
21 21
22 #include "ConfigFile.h" //!!! reorg
23
24 #include <QDir> 22 #include <QDir>
25 #include <QFileInfo> 23 #include <QFileInfo>
24 #include <QMutex>
25 #include <QSettings>
26 26
27 Preferences * 27 Preferences *
28 Preferences::m_instance = new Preferences(); 28 Preferences::m_instance = 0;
29
30 Preferences *
31 Preferences::getInstance()
32 {
33 if (!m_instance) m_instance = new Preferences();
34 return m_instance;
35 }
29 36
30 Preferences::Preferences() : 37 Preferences::Preferences() :
31 m_smoothSpectrogram(true), 38 m_smoothSpectrogram(true),
32 m_tuningFrequency(440), 39 m_tuningFrequency(440),
33 m_propertyBoxLayout(VerticallyStacked), 40 m_propertyBoxLayout(VerticallyStacked),
34 m_windowType(HanningWindow), 41 m_windowType(HanningWindow)
35 m_configFile(0) 42 {
36 { 43 QSettings settings;
37 // Let TempDirectory do its thing first, so as to avoid any race 44 settings.beginGroup("Preferences");
38 // condition if it happens that we both want to use the same directory 45 m_smoothSpectrogram = settings.value("smooth-spectrogram", true).toBool();
39 TempDirectory::getInstance(); 46 m_tuningFrequency = settings.value("tuning-frequency", 440.f).toDouble();
40
41 QString svDirBase = ".sv1";
42 QString svDir = QDir::home().filePath(svDirBase);
43 if (!QFileInfo(svDir).exists()) {
44 if (!QDir::home().mkdir(svDirBase)) {
45 throw DirectoryCreationFailed(QString("%1 directory in $HOME")
46 .arg(svDirBase));
47 }
48 } else if (!QFileInfo(svDir).isDir()) {
49 throw DirectoryCreationFailed(QString("$HOME/%1 is not a directory")
50 .arg(svDirBase));
51 }
52
53 m_configFile = new ConfigFile(QDir(svDir).filePath("preferences.cfg"));
54
55 m_smoothSpectrogram =
56 m_configFile->getBool("preferences-smooth-spectrogram", true);
57 m_tuningFrequency =
58 m_configFile->getFloat("preferences-tuning-frequency", 440.f);
59 m_propertyBoxLayout = PropertyBoxLayout 47 m_propertyBoxLayout = PropertyBoxLayout
60 (m_configFile->getInt("preferences-property-box-layout", 48 (settings.value("property-box-layout", int(VerticallyStacked)).toInt());
61 int(VerticallyStacked)));
62 m_windowType = WindowType 49 m_windowType = WindowType
63 (m_configFile->getInt("preferences-window-type", int(HanningWindow))); 50 (settings.value("window-type", int(HanningWindow)).toInt());
51 settings.endGroup();
64 } 52 }
65 53
66 Preferences::~Preferences() 54 Preferences::~Preferences()
67 { 55 {
68 delete m_configFile;
69 } 56 }
70 57
71 Preferences::PropertyList 58 Preferences::PropertyList
72 Preferences::getProperties() const 59 Preferences::getProperties() const
73 { 60 {
195 void 182 void
196 Preferences::setSmoothSpectrogram(bool smooth) 183 Preferences::setSmoothSpectrogram(bool smooth)
197 { 184 {
198 if (m_smoothSpectrogram != smooth) { 185 if (m_smoothSpectrogram != smooth) {
199 m_smoothSpectrogram = smooth; 186 m_smoothSpectrogram = smooth;
200 m_configFile->set("preferences-smooth-spectrogram", smooth); 187 QSettings settings;
188 settings.beginGroup("Preferences");
189 settings.setValue("smooth-spectrogram", smooth);
190 settings.endGroup();
201 emit propertyChanged("Smooth Spectrogram"); 191 emit propertyChanged("Smooth Spectrogram");
202 } 192 }
203 } 193 }
204 194
205 void 195 void
206 Preferences::setTuningFrequency(float freq) 196 Preferences::setTuningFrequency(float freq)
207 { 197 {
208 if (m_tuningFrequency != freq) { 198 if (m_tuningFrequency != freq) {
209 m_tuningFrequency = freq; 199 m_tuningFrequency = freq;
210 m_configFile->set("preferences-tuning-frequency", freq); 200 QSettings settings;
201 settings.beginGroup("Preferences");
202 settings.setValue("tuning-frequency", freq);
203 settings.endGroup();
211 emit propertyChanged("Tuning Frequency"); 204 emit propertyChanged("Tuning Frequency");
212 } 205 }
213 } 206 }
214 207
215 void 208 void
216 Preferences::setPropertyBoxLayout(PropertyBoxLayout layout) 209 Preferences::setPropertyBoxLayout(PropertyBoxLayout layout)
217 { 210 {
218 if (m_propertyBoxLayout != layout) { 211 if (m_propertyBoxLayout != layout) {
219 m_propertyBoxLayout = layout; 212 m_propertyBoxLayout = layout;
220 m_configFile->set("preferences-property-box-layout", int(layout)); 213 QSettings settings;
214 settings.beginGroup("Preferences");
215 settings.setValue("property-box-layout", int(layout));
216 settings.endGroup();
221 emit propertyChanged("Property Box Layout"); 217 emit propertyChanged("Property Box Layout");
222 } 218 }
223 } 219 }
224 220
225 void 221 void
226 Preferences::setWindowType(WindowType type) 222 Preferences::setWindowType(WindowType type)
227 { 223 {
228 if (m_windowType != type) { 224 if (m_windowType != type) {
229 m_windowType = type; 225 m_windowType = type;
230 m_configFile->set("preferences-window-type", int(type)); 226 QSettings settings;
227 settings.beginGroup("Preferences");
228 settings.setValue("window-type", int(type));
229 settings.endGroup();
231 emit propertyChanged("Window Type"); 230 emit propertyChanged("Window Type");
232 } 231 }
233 } 232 }
234 233