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

Last change on this file since 5453 was 5453, checked in by mervart, 11 years ago
File size: 2.9 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////////////////////////////////////////////////////////////////////////////
[5453]28t_settings::t_settings(const QString& groupName) {
[4814]29 QMutexLocker locker(&_mutex);
30
[5453]31 _groupName = groupName;
32 _app = static_cast<t_app*>(qApp);
[4814]33
34 // First fill the options
35 // ----------------------
[4861]36 if (_app->_settings.size() == 0) {
[4814]37 reRead();
38 }
39}
40
41// Destructor
42////////////////////////////////////////////////////////////////////////////
[4861]43t_settings::~t_settings() {
[4814]44}
45
46// (Re-)read the Options from File or Set the Defaults
47////////////////////////////////////////////////////////////////////////////
[4861]48void t_settings::reRead() {
[4814]49
[4861]50 _app->_settings.clear();
[4814]51
[4861]52 QSettings settings(_app->confFileName(), QSettings::IniFormat);
[4814]53
54 // Read from File
55 // --------------
56 if (settings.allKeys().size() > 0) {
57 QStringListIterator it(settings.allKeys());
58 while (it.hasNext()) {
59 QString key = it.next();
[4861]60 _app->_settings[key] = settings.value(key);
[4814]61 }
62 }
63
64 // Set Defaults
65 // ------------
66 else {
67
68 }
69}
70
71//
72////////////////////////////////////////////////////////////////////////////
[4861]73QVariant t_settings::value(const QString& key,
[4814]74 const QVariant& defaultValue) const {
75 QMutexLocker locker(&_mutex);
[5452]76 QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
77 if (_app->_settings.contains(fullKey)) {
78 return _app->_settings[fullKey];
[4814]79 }
80 else {
81 return defaultValue;
82 }
83}
84
85//
86////////////////////////////////////////////////////////////////////////////
[4861]87void t_settings::setValue(const QString &key, const QVariant& value) {
[4814]88 QMutexLocker locker(&_mutex);
[5452]89 QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
90 _app->_settings[fullKey] = value;
[4814]91}
92
93//
94////////////////////////////////////////////////////////////////////////////
[4861]95void t_settings::remove(const QString& key ) {
[4814]96 QMutexLocker locker(&_mutex);
[5452]97 QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
98 _app->_settings.remove(fullKey);
[4814]99}
100
101//
102////////////////////////////////////////////////////////////////////////////
[4861]103void t_settings::sync() {
[4814]104 QMutexLocker locker(&_mutex);
[4861]105 QSettings settings(_app->confFileName(), QSettings::IniFormat);
[4814]106 settings.clear();
[4861]107 QMapIterator<QString, QVariant> it(_app->_settings);
[4814]108 while (it.hasNext()) {
109 it.next();
110 settings.setValue(it.key(), it.value());
111 }
112 settings.sync();
113}
Note: See TracBrowser for help on using the repository browser.