source: ntrip/trunk/GnssCenter/main/settings.cpp@ 5452

Last change on this file since 5452 was 5452, checked in by mervart, 11 years ago
File size: 3.2 KB
RevLine 
[4814]1/* -------------------------------------------------------------------------
2 * RTNet GUI
3 * -------------------------------------------------------------------------
4 *
[4861]5 * Class: t_settings
[4814]6 *
7 * Purpose: Subclasses the QSettings
8 *
9 * Author: L. Mervart
10 *
11 * Created: 05-Jan-2013
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <QSettings>
18
[4861]19#include "settings.h"
20#include "app.h"
[4814]21
[5001]22using namespace GnssCenter;
[4814]23
[4861]24QMutex t_settings::_mutex; // static mutex
25
[4814]26// Constructor
27////////////////////////////////////////////////////////////////////////////
[4861]28t_settings::t_settings() {
[4814]29 QMutexLocker locker(&_mutex);
30
[4861]31 _app = static_cast<t_app*>(qApp);
[4814]32
33 // First fill the options
34 // ----------------------
[4861]35 if (_app->_settings.size() == 0) {
[4814]36 reRead();
37 }
38}
39
40// Destructor
41////////////////////////////////////////////////////////////////////////////
[4861]42t_settings::~t_settings() {
[4814]43}
44
45// (Re-)read the Options from File or Set the Defaults
46////////////////////////////////////////////////////////////////////////////
[4861]47void t_settings::reRead() {
[4814]48
[4861]49 _app->_settings.clear();
[4814]50
[4861]51 QSettings settings(_app->confFileName(), QSettings::IniFormat);
[4814]52
53 // Read from File
54 // --------------
55 if (settings.allKeys().size() > 0) {
56 QStringListIterator it(settings.allKeys());
57 while (it.hasNext()) {
58 QString key = it.next();
[4861]59 _app->_settings[key] = settings.value(key);
[4814]60 }
61 }
62
63 // Set Defaults
64 // ------------
65 else {
66
67 }
68}
69
70//
71////////////////////////////////////////////////////////////////////////////
[4861]72QVariant t_settings::value(const QString& key,
[4814]73 const QVariant& defaultValue) const {
74 QMutexLocker locker(&_mutex);
[5452]75 QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
76 if (_app->_settings.contains(fullKey)) {
77 return _app->_settings[fullKey];
[4814]78 }
79 else {
80 return defaultValue;
81 }
82}
83
84//
85////////////////////////////////////////////////////////////////////////////
[4861]86void t_settings::setValue(const QString &key, const QVariant& value) {
[4814]87 QMutexLocker locker(&_mutex);
[5452]88 QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
89 _app->_settings[fullKey] = value;
[4814]90}
91
92//
93////////////////////////////////////////////////////////////////////////////
[4861]94void t_settings::remove(const QString& key ) {
[4814]95 QMutexLocker locker(&_mutex);
[5452]96 QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
97 _app->_settings.remove(fullKey);
[4814]98}
99
100//
101////////////////////////////////////////////////////////////////////////////
[4861]102void t_settings::sync() {
[4814]103 QMutexLocker locker(&_mutex);
[4861]104 QSettings settings(_app->confFileName(), QSettings::IniFormat);
[4814]105 settings.clear();
[4861]106 QMapIterator<QString, QVariant> it(_app->_settings);
[4814]107 while (it.hasNext()) {
108 it.next();
109 settings.setValue(it.key(), it.value());
110 }
111 settings.sync();
112}
[5452]113
114//
115////////////////////////////////////////////////////////////////////////////
116void t_settings::beginGroup(const QString& groupName) {
117 QMutexLocker locker(&_mutex);
118 _groupName = groupName;
119}
120
121//
122////////////////////////////////////////////////////////////////////////////
123void t_settings::endGroup() {
124 QMutexLocker locker(&_mutex);
125 _groupName.clear();
126}
Note: See TracBrowser for help on using the repository browser.