Browse Source

added DataSyncSettingsViewModel

pull/2/head
Skycoder42 7 years ago
parent
commit
ab604204ba
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 21
      src/imports/mvvmdatasynccore/plugins.qmltypes
  2. 4
      src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp
  3. 2
      src/mvvmcore/settingsviewmodel.h
  4. 94
      src/mvvmdatasynccore/datasyncsettingsviewmodel.cpp
  5. 76
      src/mvvmdatasynccore/datasyncsettingsviewmodel.h
  6. 21
      src/mvvmdatasynccore/datasyncsettingsviewmodel_p.h
  7. 7
      src/mvvmdatasynccore/mvvmdatasynccore.pro

21
src/imports/mvvmdatasynccore/plugins.qmltypes

@ -105,6 +105,27 @@ Module {
Parameter { name: "index"; type: "int" }
}
}
Component {
name: "QtMvvm::DataSyncSettingsEntry"
exports: [
"de.skycoder42.QtMvvm.DataSync.Core/DataSyncSettingsEntry 1.1"
]
isCreatable: false
exportMetaObjectRevisions: [0]
Property { name: "key"; type: "string" }
Property { name: "dataVersion"; type: "int" }
Property { name: "value"; type: "QByteArray" }
Property { name: "varValue"; type: "QVariant" }
}
Component {
name: "QtMvvm::DataSyncSettingsViewModel"
prototype: "QtMvvm::SettingsViewModel"
exports: [
"de.skycoder42.QtMvvm.DataSync.Core/DataSyncSettingsViewModel 1.1"
]
isCreatable: false
exportMetaObjectRevisions: [0]
}
Component {
name: "QtMvvm::DataSyncViewModel"
prototype: "QtMvvm::ViewModel"

4
src/imports/mvvmdatasynccore/qtmvvmdatasynccore_plugin.cpp

@ -6,6 +6,7 @@
#include <QtMvvmDataSyncCore/ExchangeDevicesModel>
#include <QtMvvmDataSyncCore/DataSyncViewModel>
#include <QtMvvmDataSyncCore/NetworkExchangeViewModel>
#include <QtMvvmDataSyncCore/DataSyncSettingsViewModel>
#include <QtMvvmDataSyncCore/private/identityeditviewmodel_p.h>
#include <QtMvvmDataSyncCore/private/changeremoteviewmodel_p.h>
@ -32,7 +33,8 @@ void QtMvvmDataSyncCoreDeclarativeModule::registerTypes(const char *uri)
qmlRegisterUncreatableType<QtMvvm::ExportSetupViewModel>(uri, 1, 0, "PExportSetupViewModel", QStringLiteral("ViewModels cannot be created from QML"));
//Version 1.1
qmlRegisterModule(uri, 1, 1);
qmlRegisterUncreatableType<QtMvvm::DataSyncSettingsEntry>(uri, 1, 1, "DataSyncSettingsEntry", QStringLiteral("Q_GADGETs cannot be created from QML"));
qmlRegisterUncreatableType<QtMvvm::DataSyncSettingsViewModel>(uri, 1, 1, "DataSyncSettingsViewModel", QStringLiteral("ViewModels cannot be created from QML"));
// Check to make shure no module update is forgotten
static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 1, "QML module version needs to be updated");

2
src/mvvmcore/settingsviewmodel.h

@ -59,7 +59,7 @@ public:
Q_INVOKABLE virtual void saveValue(const QString &key, const QVariant &value);
//! Resets the value or group identified by the key
Q_INVOKABLE virtual void resetValue(const QString &key);
Q_REVISION(1) Q_INVOKABLE virtual void resetAll(const SettingsElements::Setup &setup);
Q_REVISION(1) Q_INVOKABLE void resetAll(const SettingsElements::Setup &setup);
public Q_SLOTS:
//! Is called when an action type edit is pressed

94
src/mvvmdatasynccore/datasyncsettingsviewmodel.cpp

@ -0,0 +1,94 @@
#include "datasyncsettingsviewmodel.h"
#include "datasyncsettingsviewmodel_p.h"
#include <QtCore/QDataStream>
#include <QtCore/QDebug>
#undef logDebug
#undef logInfo
#undef logWarning
#undef logCritical
#include <QtMvvmCore/private/qtmvvm_logging_p.h>
using namespace QtMvvm;
DataSyncSettingsViewModel::DataSyncSettingsViewModel(QObject *parent) :
SettingsViewModel{parent},
d(new DataSyncSettingsViewModelPrivate{this})
{
connect(d->store, &QtDataSync::DataTypeStoreBase::dataChanged,
this, &DataSyncSettingsViewModel::valueChanged);
}
DataSyncSettingsViewModel::~DataSyncSettingsViewModel() = default;
QVariant DataSyncSettingsViewModel::loadValue(const QString &key, const QVariant &defaultValue) const
{
try {
return d->store->load(key).value();
} catch (QtDataSync::NoDataException &e) {
Q_UNUSED(e)
return defaultValue;
} catch (QException &e) {
logCritical() << "Failed to load entry" << key << "from datasync settings with error:"
<< e.what();
return defaultValue;
}
}
void DataSyncSettingsViewModel::saveValue(const QString &key, const QVariant &value)
{
try {
d->store->save({key, value});
} catch (QException &e) {
qCritical() << "Failed to save entry" << key << "to datasync settings with error:"
<< e.what();
}
}
void DataSyncSettingsViewModel::resetValue(const QString &key)
{
try {
d->store->remove(key);
} catch (QException &e) {
qCritical() << "Failed to remove entry" << key << "from datasync settings with error:"
<< e.what();
}
}
DataSyncSettingsEntry::DataSyncSettingsEntry() = default;
DataSyncSettingsEntry::DataSyncSettingsEntry(QString key, const QVariant &value) :
_key{std::move(key)}
{
setValue(value);
}
QString DataSyncSettingsEntry::key() const
{
return _key;
}
QVariant DataSyncSettingsEntry::value() const
{
QVariant value;
QDataStream stream(_value);
stream.setVersion(_dataVersion);
stream >> value;
return value;
}
void DataSyncSettingsEntry::setValue(const QVariant &value)
{
_value.clear();
QDataStream stream(&_value, QIODevice::WriteOnly);
_dataVersion = stream.version();
stream << value;
}
// ------------- Private Implementation -------------
DataSyncSettingsViewModelPrivate::DataSyncSettingsViewModelPrivate(DataSyncSettingsViewModel *q_ptr) :
store{new QtDataSync::DataTypeStore<DataSyncSettingsEntry>{q_ptr}}
{}

76
src/mvvmdatasynccore/datasyncsettingsviewmodel.h

@ -0,0 +1,76 @@
#ifndef QTMVVM_DATASYNCSETTINGSVIEWMODEL_H
#define QTMVVM_DATASYNCSETTINGSVIEWMODEL_H
#include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
#include <QtMvvmCore/settingsviewmodel.h>
#include "QtMvvmDataSyncCore/qtmvvmdatasynccore_global.h"
namespace QtMvvm {
struct Q_MVVMDATASYNCCORE_EXPORT DataSyncSettingsEntry
{
Q_GADGET
Q_PROPERTY(QString key MEMBER _key USER true)
Q_PROPERTY(int dataVersion MEMBER _dataVersion)
Q_PROPERTY(QByteArray value MEMBER _value)
Q_PROPERTY(QVariant varValue READ value WRITE setValue STORED false)
public:
DataSyncSettingsEntry();
DataSyncSettingsEntry(QString key, const QVariant &value);
template <typename T>
DataSyncSettingsEntry(QString key, const T &value);
QString key() const;
QVariant value() const;
template <typename T>
T value() const;
void setValue(const QVariant &value);
private:
QString _key;
int _dataVersion = 0;
QByteArray _value;
};
class DataSyncSettingsViewModelPrivate;
class Q_MVVMDATASYNCCORE_EXPORT DataSyncSettingsViewModel : public QtMvvm::SettingsViewModel
{
Q_OBJECT
public:
Q_INVOKABLE explicit DataSyncSettingsViewModel(QObject *parent = nullptr);
~DataSyncSettingsViewModel() override;
QVariant loadValue(const QString &key, const QVariant &defaultValue) const override;
void saveValue(const QString &key, const QVariant &value) override;
void resetValue(const QString &key) override;
private:
QScopedPointer<DataSyncSettingsViewModelPrivate> d;
};
// ------------- Generic Implementation -------------
template<typename T>
DataSyncSettingsEntry::DataSyncSettingsEntry(QString key, const T &value) :
DataSyncSettingsEntry{std::move(key), QVariant::fromValue(value)}
{}
template<typename T>
T DataSyncSettingsEntry::value() const
{
return value().template value<T>();
}
}
Q_DECLARE_METATYPE(QtMvvm::DataSyncSettingsEntry)
#endif // QTMVVM_DATASYNCSETTINGSVIEWMODEL_H

21
src/mvvmdatasynccore/datasyncsettingsviewmodel_p.h

@ -0,0 +1,21 @@
#ifndef QTMVVM_DATASYNCSETTINGSVIEWMODEL_P_H
#define QTMVVM_DATASYNCSETTINGSVIEWMODEL_P_H
#include "qtmvvmdatasynccore_global.h"
#include "datasyncsettingsviewmodel.h"
#include <QtDataSync/DataTypeStore>
namespace QtMvvm {
class DataSyncSettingsViewModelPrivate
{
public:
DataSyncSettingsViewModelPrivate(DataSyncSettingsViewModel *q_ptr);
QtDataSync::DataTypeStore<DataSyncSettingsEntry> *store;
};
}
#endif // QTMVVM_DATASYNCSETTINGSVIEWMODEL_P_H

7
src/mvvmdatasynccore/mvvmdatasynccore.pro

@ -14,7 +14,9 @@ HEADERS += \
networkexchangeviewmodel.h \
networkexchangeviewmodel_p.h \
exchangedevicesmodel.h \
exchangedevicesmodel_p.h
exchangedevicesmodel_p.h \
datasyncsettingsviewmodel.h \
datasyncsettingsviewmodel_p.h
SOURCES += \
datasyncviewmodel.cpp \
@ -23,7 +25,8 @@ SOURCES += \
changeremoteviewmodel.cpp \
identityeditviewmodel.cpp \
networkexchangeviewmodel.cpp \
exchangedevicesmodel.cpp
exchangedevicesmodel.cpp \
datasyncsettingsviewmodel.cpp
TRANSLATIONS += \
translations/qtmvvmdatasynccore_de.ts \

Loading…
Cancel
Save