Skycoder42
7 years ago
7 changed files with 221 additions and 4 deletions
@ -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}} |
|||
{} |
@ -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
|
@ -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
|
Loading…
Reference in new issue