Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit a19a445

Browse files
committed
use ISettingsAccessor for the settings viewmodels
1 parent eaa3e9a commit a19a445

15 files changed

+143
-115
lines changed

src/imports/mvvmcore/plugins.qmltypes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,16 @@ Module {
760760
exports: ["de.skycoder42.QtMvvm.Core/SettingsViewModel 1.0"]
761761
isCreatable: false
762762
exportMetaObjectRevisions: [0]
763+
Property { name: "accessor"; revision: 1; type: "QtMvvm::ISettingsAccessor"; isPointer: true }
763764
Property { name: "canRestoreDefaults"; type: "bool"; isReadonly: true }
764765
Property { name: "restoreConfig"; type: "QtMvvm::MessageConfig"; isReadonly: true }
765766
Property { name: "settingsSetupLoader"; type: "QtMvvm::ISettingsSetupLoader"; isPointer: true }
766767
Property { name: "__qtmvvm_inject_settingsSetupLoader"; type: "QByteArray"; isReadonly: true }
768+
Signal {
769+
name: "accessorChanged"
770+
revision: 1
771+
Parameter { name: "accessor"; type: "QtMvvm::ISettingsAccessor"; isPointer: true }
772+
}
767773
Signal {
768774
name: "settingsSetupLoaderChanged"
769775
Parameter { name: "settingsSetupLoader"; type: "QtMvvm::ISettingsSetupLoader"; isPointer: true }
@@ -780,6 +786,11 @@ Module {
780786
Parameter { name: "key"; type: "string" }
781787
Parameter { name: "parameters"; type: "QVariantMap" }
782788
}
789+
Method {
790+
name: "setAccessor"
791+
revision: 1
792+
Parameter { name: "accessor"; type: "QtMvvm::ISettingsAccessor"; isPointer: true }
793+
}
783794
Method {
784795
name: "setSettingsSetupLoader"
785796
Parameter { name: "settingsSetupLoader"; type: "QtMvvm::ISettingsSetupLoader"; isPointer: true }

src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QtMvvmDataSyncCore/DataSyncViewModel>
88
#include <QtMvvmDataSyncCore/NetworkExchangeViewModel>
99
#include <QtMvvmDataSyncCore/DataSyncSettingsViewModel>
10+
#include <QtMvvmDataSyncCore/DataSyncSettingsEntry>
1011

1112
#include <QtMvvmDataSyncCore/private/identityeditviewmodel_p.h>
1213
#include <QtMvvmDataSyncCore/private/changeremoteviewmodel_p.h>

src/mvvmcore/qsettingsaccessor.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class QSettingsAccessorPrivate
1515

1616
QSettingsAccessor::QSettingsAccessor(QObject *parent) :
1717
QSettingsAccessor{new QSettings{}, parent}
18-
{}
18+
{
19+
d->settings->setParent(this);
20+
}
1921

2022
QSettingsAccessor::QSettingsAccessor(QSettings *settings, QObject *parent) :
2123
ISettingsAccessor{parent},
2224
d{new QSettingsAccessorPrivate{settings}}
23-
{
24-
d->settings->setParent(this);
25-
}
25+
{}
2626

2727
QSettingsAccessor::~QSettingsAccessor() = default;
2828

@@ -53,6 +53,11 @@ void QSettingsAccessor::remove(const QString &key)
5353
emit entryRemoved(key);
5454
}
5555

56+
QSettings *QSettingsAccessor::settings() const
57+
{
58+
return d->settings;
59+
}
60+
5661
void QSettingsAccessor::sync()
5762
{
5863
d->settings->sync();

src/mvvmcore/qsettingsaccessor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Q_MVVMCORE_EXPORT QSettingsAccessor : public ISettingsAccessor
2525
void save(const QString &key, const QVariant &value) override;
2626
void remove(const QString &key) override;
2727

28+
QSettings *settings() const;
29+
2830
public Q_SLOTS:
2931
void sync() override;
3032

src/mvvmcore/settingsviewmodel.cpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include "coreapp.h"
44
#include "qtmvvm_logging_p.h"
55
#include "settingssetuploader_p.h"
6+
#include "qsettingsaccessor.h"
67

78
using namespace QtMvvm;
89

10+
const QString SettingsViewModel::paramAccessor(QStringLiteral("accessor"));
911
const QString SettingsViewModel::paramSettings(QStringLiteral("settings"));
1012
const QString SettingsViewModel::paramSetupFile(QStringLiteral("setupFile"));
1113

@@ -14,6 +16,11 @@ SettingsViewModel::SettingsViewModel(QObject *parent) :
1416
d(new SettingsViewModelPrivate())
1517
{}
1618

19+
ISettingsAccessor *SettingsViewModel::accessor() const
20+
{
21+
return d->accessor;
22+
}
23+
1724
SettingsViewModel::~SettingsViewModel() = default;
1825

1926
QVariantHash SettingsViewModel::showParams(QSettings *settings, const QString &setupFile)
@@ -56,24 +63,23 @@ SettingsElements::Setup SettingsViewModel::loadSetup(const QString &frontend) co
5663

5764
QSettings *SettingsViewModel::settings() const
5865
{
59-
return d->settings;
66+
auto qAccessor = qobject_cast<QSettingsAccessor*>(d->accessor);
67+
return qAccessor ? qAccessor->settings() : nullptr;
6068
}
6169

6270
QVariant SettingsViewModel::loadValue(const QString &key, const QVariant &defaultValue) const
6371
{
64-
return d->settings->value(key, defaultValue);
72+
return d->accessor->load(key, defaultValue);
6573
}
6674

6775
void SettingsViewModel::saveValue(const QString &key, const QVariant &value)
6876
{
69-
d->settings->setValue(key, value);
70-
emit valueChanged(key);
77+
d->accessor->save(key, value);
7178
}
7279

7380
void SettingsViewModel::resetValue(const QString &key)
7481
{
75-
d->settings->remove(key);
76-
emit valueChanged(key);
82+
d->accessor->remove(key);
7783
}
7884

7985
void SettingsViewModel::resetAll(const SettingsElements::Setup &setup)
@@ -105,6 +111,32 @@ void SettingsViewModel::callAction(const QString &key, const QVariantMap &parame
105111
<< "and parameters" << parameters;
106112
}
107113

114+
void QtMvvm::SettingsViewModel::setAccessor(ISettingsAccessor *accessor)
115+
{
116+
if(accessor == d->accessor)
117+
return;
118+
119+
if(d->accessor) {
120+
disconnect(d->accessor, &ISettingsAccessor::entryChanged,
121+
this, &SettingsViewModel::valueChanged);
122+
disconnect(d->accessor, &ISettingsAccessor::entryRemoved,
123+
this, &SettingsViewModel::valueChanged);
124+
}
125+
126+
if(d->accessor->parent() == this)
127+
d->accessor->deleteLater();
128+
d->accessor = accessor;
129+
130+
if(d->accessor) {
131+
connect(d->accessor, &ISettingsAccessor::entryChanged,
132+
this, &SettingsViewModel::valueChanged);
133+
connect(d->accessor, &ISettingsAccessor::entryRemoved,
134+
this, &SettingsViewModel::valueChanged);
135+
}
136+
137+
emit accessorChanged(d->accessor);
138+
}
139+
108140
void SettingsViewModel::setSettingsSetupLoader(ISettingsSetupLoader *settingsSetupLoader)
109141
{
110142
if (d->setupLoader == settingsSetupLoader)
@@ -118,9 +150,16 @@ void SettingsViewModel::onInit(const QVariantHash &params)
118150
{
119151
Q_ASSERT_X(d->setupLoader, Q_FUNC_INFO, "settingsSetupLoader must not be null");
120152

121-
d->settings = params.value(paramSettings).value<QSettings*>();
122-
if(!d->settings)
123-
d->settings = new QSettings(this);
153+
auto accessor = params.value(paramAccessor).value<ISettingsAccessor*>();
154+
if(!accessor) {
155+
auto settings = params.value(paramSettings).value<QSettings*>();
156+
if(settings)
157+
accessor = new QSettingsAccessor{settings, this};
158+
else
159+
accessor = new QSettingsAccessor{this};
160+
}
161+
setAccessor(accessor);
162+
124163
d->setupFile = params.value(paramSetupFile).toString();
125164
if(d->setupFile.isEmpty())
126165
d->setupFile = QStringLiteral(":/etc/settings.xml");

src/mvvmcore/settingsviewmodel.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "QtMvvmCore/qtmvvmcore_global.h"
1111
#include "QtMvvmCore/settingssetup.h"
12+
#include "QtMvvmCore/isettingsaccessor.h"
1213

1314
namespace QtMvvm {
1415

@@ -18,6 +19,8 @@ class Q_MVVMCORE_EXPORT SettingsViewModel : public ViewModel
1819
{
1920
Q_OBJECT
2021

22+
Q_PROPERTY(QtMvvm::ISettingsAccessor* accessor READ accessor WRITE setAccessor NOTIFY accessorChanged REVISION 1)
23+
2124
//! Specifies if restoring the defaults is generally allowed
2225
Q_PROPERTY(bool canRestoreDefaults READ canRestoreDefaults CONSTANT)
2326
//! The message configuration to be used to for a dialog to ask for settings restore
@@ -28,6 +31,7 @@ class Q_MVVMCORE_EXPORT SettingsViewModel : public ViewModel
2831
QTMVVM_INJECT(QtMvvm::ISettingsSetupLoader*, settingsSetupLoader)
2932

3033
public:
34+
static const QString paramAccessor;
3135
//! The parameter for a QSettings object for the onInit() method
3236
static const QString paramSettings;
3337
//! The parameter for a settings setup file for the onInit() method
@@ -40,6 +44,7 @@ class Q_MVVMCORE_EXPORT SettingsViewModel : public ViewModel
4044
Q_INVOKABLE explicit SettingsViewModel(QObject *parent = nullptr);
4145
~SettingsViewModel() override;
4246

47+
QtMvvm::ISettingsAccessor* accessor() const;
4348
//! @readAcFn{SettingsViewModel::canRestoreDefaults}
4449
virtual bool canRestoreDefaults() const;
4550
//! @readAcFn{SettingsViewModel::restoreConfig}
@@ -50,7 +55,7 @@ class Q_MVVMCORE_EXPORT SettingsViewModel : public ViewModel
5055
//! Loads the settings setup of the prepared file for the given frontend
5156
SettingsElements::Setup loadSetup(const QString &frontend) const;
5257

53-
//! Returns the settings this viewmodel operates on
58+
//! Returns the settings this viewmodel operates on (or null if not using QtMvvm::QSettingsAccessor)
5459
QSettings *settings() const;
5560

5661
//! Loads the value for the given key from the settings
@@ -65,10 +70,12 @@ public Q_SLOTS:
6570
//! Is called when an action type edit is pressed
6671
virtual void callAction(const QString &key, const QVariantMap &parameters);
6772

73+
Q_REVISION(1) void setAccessor(QtMvvm::ISettingsAccessor* accessor);
6874
//! @writeAcFn{SettingsViewModel::settingsSetupLoader}
6975
void setSettingsSetupLoader(QtMvvm::ISettingsSetupLoader* settingsSetupLoader);
7076

7177
Q_SIGNALS:
78+
Q_REVISION(1) void accessorChanged(QtMvvm::ISettingsAccessor* accessor);
7279
//! @notifyAcFn{SettingsViewModel::settingsSetupLoader}
7380
void settingsSetupLoaderChanged(QtMvvm::ISettingsSetupLoader* settingsSetupLoader, QPrivateSignal);
7481

src/mvvmcore/settingsviewmodel_p.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class SettingsViewModelPrivate
1010
{
1111
public:
1212
ISettingsSetupLoader *setupLoader = nullptr;
13+
ISettingsAccessor *accessor = nullptr;
1314

14-
QSettings *settings = nullptr;
1515
QString setupFile;
1616

1717
SettingsElements::Setup currentSetup;
Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "datasyncsettingsviewmodel.h"
2-
#include "datasyncsettingsviewmodel_p.h"
2+
#include "datasyncsettingsaccessor.h"
33

44
#undef logDebug
55
#undef logInfo
@@ -9,52 +9,46 @@
99

1010
using namespace QtMvvm;
1111

12-
DataSyncSettingsViewModel::DataSyncSettingsViewModel(QObject *parent) :
13-
SettingsViewModel{parent},
14-
d(new DataSyncSettingsViewModelPrivate{this})
15-
{
16-
connect(d->store, &QtDataSync::DataTypeStoreBase::dataChanged,
17-
this, &DataSyncSettingsViewModel::valueChanged);
18-
}
19-
20-
DataSyncSettingsViewModel::~DataSyncSettingsViewModel() = default;
21-
22-
QVariant DataSyncSettingsViewModel::loadValue(const QString &key, const QVariant &defaultValue) const
23-
{
24-
try {
25-
return d->store->load(key).value();
26-
} catch (QtDataSync::NoDataException &e) {
27-
Q_UNUSED(e)
28-
return defaultValue;
29-
} catch (QException &e) {
30-
logCritical() << "Failed to load entry" << key << "from datasync settings with error:"
31-
<< e.what();
32-
return defaultValue;
33-
}
34-
}
12+
const QString DataSyncSettingsViewModel::paramSetup(QStringLiteral("setup"));
13+
const QString DataSyncSettingsViewModel::paramDataStore(QStringLiteral("dataStore"));
14+
const QString DataSyncSettingsViewModel::paramDataTypeStore(QStringLiteral("dataTypeStore"));
3515

36-
void DataSyncSettingsViewModel::saveValue(const QString &key, const QVariant &value)
37-
{
38-
try {
39-
d->store->save({key, value});
40-
} catch (QException &e) {
41-
logCritical() << "Failed to save entry" << key << "to datasync settings with error:"
42-
<< e.what();
43-
}
44-
}
16+
DataSyncSettingsViewModel::DataSyncSettingsViewModel(QObject *parent) :
17+
SettingsViewModel{parent}
18+
{}
4519

46-
void DataSyncSettingsViewModel::resetValue(const QString &key)
20+
void DataSyncSettingsViewModel::onInit(const QVariantHash &params)
4721
{
48-
try {
49-
d->store->remove(key);
50-
} catch (QException &e) {
51-
logCritical() << "Failed to remove entry" << key << "from datasync settings with error:"
52-
<< e.what();
53-
}
22+
if(!params.contains(paramAccessor)) {
23+
auto subParams = params;
24+
try {
25+
DataSyncSettingsAccessor *accessor = nullptr;
26+
auto setup = params.value(paramSetup).toString();
27+
if(!setup.isNull())
28+
accessor = new DataSyncSettingsAccessor{setup ,this};
29+
30+
if(!accessor) {
31+
auto store = params.value(paramDataStore).value<QtDataSync::DataStore*>();
32+
if(store)
33+
accessor = new DataSyncSettingsAccessor{store ,this};
34+
}
35+
36+
if(!accessor) {
37+
auto store = params.value(paramDataStore).value<QtDataSync::DataTypeStoreBase*>();
38+
auto tStore = dynamic_cast<QtDataSync::DataTypeStore<DataSyncSettingsEntry>*>(store);
39+
if(tStore)
40+
accessor = new DataSyncSettingsAccessor{tStore ,this};
41+
}
42+
43+
if(!accessor)
44+
accessor = new DataSyncSettingsAccessor{this};
45+
46+
subParams.insert(paramAccessor, QVariant::fromValue(accessor));
47+
} catch(QtDataSync::Exception &e) {
48+
logCritical() << "Failed to create DataSyncSettingsAccessor with error:" << e.what();
49+
// will be initialize with the default as fallback
50+
}
51+
SettingsViewModel::onInit(subParams);
52+
} else
53+
SettingsViewModel::onInit(params);
5454
}
55-
56-
// ------------- Private Implementation -------------
57-
58-
DataSyncSettingsViewModelPrivate::DataSyncSettingsViewModelPrivate(DataSyncSettingsViewModel *q_ptr) :
59-
store{new QtDataSync::DataTypeStore<DataSyncSettingsEntry>{q_ptr}}
60-
{}

src/mvvmdatasynccore/datasyncsettingsviewmodel.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@
77
#include <QtMvvmCore/settingsviewmodel.h>
88

99
#include "QtMvvmDataSyncCore/qtmvvmdatasynccore_global.h"
10-
#include "QtMvvmDataSyncCore/datasyncsettingsentry.h"
1110

1211
namespace QtMvvm {
1312

14-
class DataSyncSettingsViewModelPrivate;
1513
class Q_MVVMDATASYNCCORE_EXPORT DataSyncSettingsViewModel : public QtMvvm::SettingsViewModel
1614
{
1715
Q_OBJECT
1816

1917
public:
20-
Q_INVOKABLE explicit DataSyncSettingsViewModel(QObject *parent = nullptr);
21-
~DataSyncSettingsViewModel() override;
18+
static const QString paramSetup;
19+
static const QString paramDataStore;
20+
static const QString paramDataTypeStore;
2221

23-
QVariant loadValue(const QString &key, const QVariant &defaultValue) const override;
24-
void saveValue(const QString &key, const QVariant &value) override;
25-
void resetValue(const QString &key) override;
22+
Q_INVOKABLE explicit DataSyncSettingsViewModel(QObject *parent = nullptr);
2623

27-
private:
28-
QScopedPointer<DataSyncSettingsViewModelPrivate> d;
24+
protected:
25+
void onInit(const QVariantHash &params) override;
2926
};
3027

3128
}

0 commit comments

Comments
 (0)