Browse Source

moved reset settings to viewmodel

pull/2/head
Skycoder42 7 years ago
parent
commit
7b8c788200
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 6
      src/imports/mvvmcore/plugins.qmltypes
  2. 29
      src/imports/mvvmquick/settingsuibuilder.cpp
  3. 4
      src/imports/mvvmquick/settingsuibuilder.h
  4. 8
      src/mvvmcore/coreapp.cpp
  5. 22
      src/mvvmcore/settingsviewmodel.cpp
  6. 2
      src/mvvmcore/settingsviewmodel.h
  7. 19
      src/mvvmwidgets/settingsdialog.cpp
  8. 2
      src/mvvmwidgets/settingsdialog_p.h

6
src/imports/mvvmcore/plugins.qmltypes

@ -485,6 +485,7 @@ Module {
revision: 1 revision: 1
Parameter { name: "key"; type: "string" } Parameter { name: "key"; type: "string" }
} }
Signal { name: "resetAccepted"; revision: 1 }
Method { Method {
name: "callAction" name: "callAction"
Parameter { name: "key"; type: "string" } Parameter { name: "key"; type: "string" }
@ -514,6 +515,11 @@ Module {
name: "resetValue" name: "resetValue"
Parameter { name: "key"; type: "string" } Parameter { name: "key"; type: "string" }
} }
Method {
name: "resetAll"
revision: 1
Parameter { name: "setup"; type: "SettingsElements::Setup" }
}
} }
Component { Component {
name: "QtMvvm::ViewModel" name: "QtMvvm::ViewModel"

29
src/imports/mvvmquick/settingsuibuilder.cpp

@ -64,21 +64,7 @@ void SettingsUiBuilder::restoreDefaults()
{ {
if(!_viewModel->canRestoreDefaults()) if(!_viewModel->canRestoreDefaults())
return; return;
_viewModel->resetAll(_currentSetup);
auto result = CoreApp::showDialog(_viewModel->restoreConfig());
connect(result, &MessageResult::dialogDone, this, [this](MessageConfig::StandardButton btn) {
if(btn != MessageConfig::Yes)
return;
for(const auto &category : qAsConst(_currentSetup.categories)) {
for(const auto &section : category.sections) {
for(const auto &group : section.groups) {
for(const auto &entry : group.entries)
_viewModel->resetValue(entry.key);
}
}
}
emit closeSettings();
}, Qt::QueuedConnection);
} }
void SettingsUiBuilder::setFilterText(QString filterText) void SettingsUiBuilder::setFilterText(QString filterText)
@ -118,3 +104,16 @@ void SettingsUiBuilder::startBuildUi()
emit presentOverview(_sectionFilterModel, _sectionModel->hasSections()); emit presentOverview(_sectionFilterModel, _sectionModel->hasSections());
} }
} }
void SettingsUiBuilder::setViewModel(SettingsViewModel *viewModel)
{
if(_viewModel == viewModel)
return;
disconnect(_viewModel, &SettingsViewModel::resetAccepted,
this, &SettingsUiBuilder::closeSettings);
_viewModel = viewModel;
connect(_viewModel, &SettingsViewModel::resetAccepted,
this, &SettingsUiBuilder::closeSettings);
emit viewModelChanged(_viewModel);
}

4
src/imports/mvvmquick/settingsuibuilder.h

@ -19,7 +19,7 @@ class SettingsUiBuilder : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QQuickItem* buildView MEMBER _buildView NOTIFY buildViewChanged) Q_PROPERTY(QQuickItem* buildView MEMBER _buildView NOTIFY buildViewChanged)
Q_PROPERTY(SettingsViewModel* viewModel MEMBER _viewModel NOTIFY viewModelChanged) Q_PROPERTY(SettingsViewModel* viewModel MEMBER _viewModel WRITE setViewModel NOTIFY viewModelChanged)
Q_PROPERTY(QString filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged) Q_PROPERTY(QString filterText READ filterText WRITE setFilterText NOTIFY filterTextChanged)
Q_PROPERTY(bool allowSearch MEMBER _allowSearch NOTIFY allowSearchChanged) Q_PROPERTY(bool allowSearch MEMBER _allowSearch NOTIFY allowSearchChanged)
Q_PROPERTY(bool allowRestore MEMBER _allowRestore NOTIFY allowRestoreChanged) Q_PROPERTY(bool allowRestore MEMBER _allowRestore NOTIFY allowRestoreChanged)
@ -50,6 +50,8 @@ Q_SIGNALS:
private Q_SLOTS: private Q_SLOTS:
void startBuildUi(); void startBuildUi();
void setViewModel(SettingsViewModel *viewModel);
private: private:
QQuickItem* _buildView = nullptr; QQuickItem* _buildView = nullptr;
SettingsViewModel *_viewModel = nullptr; SettingsViewModel *_viewModel = nullptr;

8
src/mvvmcore/coreapp.cpp

@ -2,6 +2,7 @@
#include "coreapp_p.h" #include "coreapp_p.h"
#include "qtmvvm_logging_p.h" #include "qtmvvm_logging_p.h"
#include "serviceregistry_p.h" #include "serviceregistry_p.h"
#include "settingssetup.h"
#include <QtCore/QCommandLineParser> #include <QtCore/QCommandLineParser>
#include <QtCore/QRegularExpression> #include <QtCore/QRegularExpression>
@ -32,6 +33,13 @@ void CoreApp::registerApp()
//register metatypes //register metatypes
qRegisterMetaType<const QMetaObject*>("const QMetaObject*"); qRegisterMetaType<const QMetaObject*>("const QMetaObject*");
qRegisterMetaType<MessageConfig::StandardButton>(); qRegisterMetaType<MessageConfig::StandardButton>();
qRegisterMetaType<QtMvvm::SettingsElements::Entry>();
qRegisterMetaType<QtMvvm::SettingsElements::Group>();
qRegisterMetaType<QtMvvm::SettingsElements::Section>();
qRegisterMetaType<QtMvvm::SettingsElements::Category>();
qRegisterMetaType<QtMvvm::SettingsElements::Setup>();
registerInterfaceConverter<IPresenter>(); registerInterfaceConverter<IPresenter>();
//setup //setup

22
src/mvvmcore/settingsviewmodel.cpp

@ -76,6 +76,28 @@ void SettingsViewModel::resetValue(const QString &key)
emit valueChanged(key); emit valueChanged(key);
} }
void SettingsViewModel::resetAll(const SettingsElements::Setup &setup)
{
if(!canRestoreDefaults())
return;
auto result = CoreApp::showDialog(restoreConfig());
connect(result, &MessageResult::dialogDone, this, [this, setup](MessageConfig::StandardButton btn) {
if(btn != MessageConfig::Yes)
return;
for(const auto &category : setup.categories) {
for(const auto &section : category.sections) {
for(const auto &group : section.groups) {
for(const auto &entry : group.entries) {
resetValue(entry.key);
}
}
}
}
emit resetAccepted({});
}, Qt::QueuedConnection);
}
void SettingsViewModel::callAction(const QString &key, const QVariantMap &parameters) void SettingsViewModel::callAction(const QString &key, const QVariantMap &parameters)
{ {
Q_UNUSED(parameters) Q_UNUSED(parameters)

2
src/mvvmcore/settingsviewmodel.h

@ -59,6 +59,7 @@ public:
Q_INVOKABLE virtual void saveValue(const QString &key, const QVariant &value); Q_INVOKABLE virtual void saveValue(const QString &key, const QVariant &value);
//! Resets the value or group identified by the key //! Resets the value or group identified by the key
Q_INVOKABLE virtual void resetValue(const QString &key); Q_INVOKABLE virtual void resetValue(const QString &key);
Q_REVISION(1) Q_INVOKABLE virtual void resetAll(const SettingsElements::Setup &setup);
public Q_SLOTS: public Q_SLOTS:
//! Is called when an action type edit is pressed //! Is called when an action type edit is pressed
@ -75,6 +76,7 @@ Q_SIGNALS:
void beginLoadSetup(); void beginLoadSetup();
Q_REVISION(1) void valueChanged(const QString &key); //TODO add to save/reset doc Q_REVISION(1) void valueChanged(const QString &key); //TODO add to save/reset doc
Q_REVISION(1) void resetAccepted(QPrivateSignal);
protected: protected:
void onInit(const QVariantHash &params) override; void onInit(const QVariantHash &params) override;

19
src/mvvmwidgets/settingsdialog.cpp

@ -27,6 +27,8 @@ SettingsDialog::SettingsDialog(ViewModel *viewModel, QWidget *parent) :
connect(d->viewModel, &SettingsViewModel::valueChanged, connect(d->viewModel, &SettingsViewModel::valueChanged,
d, &SettingsDialogPrivate::entryChanged, d, &SettingsDialogPrivate::entryChanged,
Qt::QueuedConnection); // to detach updated from the bulk save operation Qt::QueuedConnection); // to detach updated from the bulk save operation
connect(d->viewModel, &SettingsViewModel::resetAccepted,
this, &SettingsDialog::accept);
if(parentWidget()) { if(parentWidget()) {
setWindowModality(Qt::WindowModal); setWindowModality(Qt::WindowModal);
@ -95,7 +97,7 @@ void SettingsDialogPrivate::createUi()
auto icoUrl = q->iconOverwrite(); auto icoUrl = q->iconOverwrite();
if(icoUrl.isValid()) if(icoUrl.isValid())
viewModel->settingsSetupLoader()->changeDefaultIcon(icoUrl); viewModel->settingsSetupLoader()->changeDefaultIcon(icoUrl);
auto setup = viewModel->loadSetup(QStringLiteral("widgets")); setup = viewModel->loadSetup(QStringLiteral("widgets"));
ui->filterLineEdit->setVisible(setup.allowSearch); ui->filterLineEdit->setVisible(setup.allowSearch);
ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setVisible(setup.allowRestore && viewModel->canRestoreDefaults()); ui->buttonBox->button(QDialogButtonBox::RestoreDefaults)->setVisible(setup.allowRestore && viewModel->canRestoreDefaults());
@ -246,8 +248,9 @@ void SettingsDialogPrivate::saveValues()
void SettingsDialogPrivate::restoreValues() void SettingsDialogPrivate::restoreValues()
{ {
for(const auto &info : qAsConst(entryMap)) if(!viewModel->canRestoreDefaults())
viewModel->resetValue(info.first.key); return;
viewModel->resetAll(setup);
} }
int SettingsDialogPrivate::calcSpacing(Qt::Orientation orientation) int SettingsDialogPrivate::calcSpacing(Qt::Orientation orientation)
@ -434,15 +437,7 @@ void SettingsDialogPrivate::buttonBoxClicked(QAbstractButton *button)
saveValues(); saveValues();
break; break;
case QDialogButtonBox::RestoreDefaults: case QDialogButtonBox::RestoreDefaults:
if(viewModel->canRestoreDefaults()) { restoreValues();
auto result = CoreApp::showDialog(viewModel->restoreConfig());
connect(result, &MessageResult::dialogDone, this, [this](MessageConfig::StandardButton btnResult) {
if(btnResult == MessageConfig::Yes) {
restoreValues();
q->accept();
}
});
}
break; break;
default: default:
Q_UNREACHABLE(); Q_UNREACHABLE();

2
src/mvvmwidgets/settingsdialog_p.h

@ -54,6 +54,8 @@ public:
CategoryItemDelegate *delegate = nullptr; CategoryItemDelegate *delegate = nullptr;
int maxWidthBase = 0; int maxWidthBase = 0;
SettingsElements::Setup setup;
using EntryInfo = QPair<SettingsElements::Entry, QMetaProperty>; using EntryInfo = QPair<SettingsElements::Entry, QMetaProperty>;
QHash<QWidget*, EntryInfo> entryMap; QHash<QWidget*, EntryInfo> entryMap;
QHash<QString, QWidget*> keyMap; QHash<QString, QWidget*> keyMap;

Loading…
Cancel
Save