| 1 | /* -------------------------------------------------------------------------
|
|---|
| 2 | * RTNet GUI
|
|---|
| 3 | * -------------------------------------------------------------------------
|
|---|
| 4 | *
|
|---|
| 5 | * Class: t_settings
|
|---|
| 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 |
|
|---|
| 19 | #include "settings.h"
|
|---|
| 20 | #include "app.h"
|
|---|
| 21 |
|
|---|
| 22 | using namespace GnssCenter;
|
|---|
| 23 |
|
|---|
| 24 | QMutex t_settings::_mutex; // static mutex
|
|---|
| 25 |
|
|---|
| 26 | // Constructor
|
|---|
| 27 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 28 | t_settings::t_settings(const QString& groupName) {
|
|---|
| 29 | QMutexLocker locker(&_mutex);
|
|---|
| 30 |
|
|---|
| 31 | _groupName = groupName;
|
|---|
| 32 | _app = static_cast<t_app*>(qApp);
|
|---|
| 33 |
|
|---|
| 34 | // First fill the options
|
|---|
| 35 | // ----------------------
|
|---|
| 36 | if (_app->_settings.size() == 0) {
|
|---|
| 37 | reRead();
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | // Destructor
|
|---|
| 42 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 43 | t_settings::~t_settings() {
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | // (Re-)read the Options from File or Set the Defaults
|
|---|
| 47 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 48 | void t_settings::reRead() {
|
|---|
| 49 |
|
|---|
| 50 | _app->_settings.clear();
|
|---|
| 51 |
|
|---|
| 52 | QSettings settings(_app->confFileName(), QSettings::IniFormat);
|
|---|
| 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();
|
|---|
| 60 | _app->_settings[key] = settings.value(key);
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | // Set Defaults
|
|---|
| 65 | // ------------
|
|---|
| 66 | else {
|
|---|
| 67 |
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | //
|
|---|
| 72 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 73 | QVariant t_settings::value(const QString& key,
|
|---|
| 74 | const QVariant& defaultValue) const {
|
|---|
| 75 | QMutexLocker locker(&_mutex);
|
|---|
| 76 | QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
|
|---|
| 77 | if (_app->_settings.contains(fullKey)) {
|
|---|
| 78 | return _app->_settings[fullKey];
|
|---|
| 79 | }
|
|---|
| 80 | else {
|
|---|
| 81 | return defaultValue;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | //
|
|---|
| 86 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 87 | void t_settings::setValue(const QString &key, const QVariant& value) {
|
|---|
| 88 | QMutexLocker locker(&_mutex);
|
|---|
| 89 | QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
|
|---|
| 90 | _app->_settings[fullKey] = value;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | //
|
|---|
| 94 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 95 | void t_settings::remove(const QString& key ) {
|
|---|
| 96 | QMutexLocker locker(&_mutex);
|
|---|
| 97 | QString fullKey = _groupName.isEmpty() ? key : _groupName + '/' + key;
|
|---|
| 98 | _app->_settings.remove(fullKey);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | //
|
|---|
| 102 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 103 | void t_settings::sync() {
|
|---|
| 104 | QMutexLocker locker(&_mutex);
|
|---|
| 105 | QSettings settings(_app->confFileName(), QSettings::IniFormat);
|
|---|
| 106 | settings.clear();
|
|---|
| 107 | QMapIterator<QString, QVariant> it(_app->_settings);
|
|---|
| 108 | while (it.hasNext()) {
|
|---|
| 109 | it.next();
|
|---|
| 110 | settings.setValue(it.key(), it.value());
|
|---|
| 111 | }
|
|---|
| 112 | settings.sync();
|
|---|
| 113 | }
|
|---|