From de8f6d1e82ef150b5cdfa39945bd7e307a74ea4e Mon Sep 17 00:00:00 2001 From: fourtf Date: Tue, 24 Jan 2017 19:51:57 +0100 Subject: [PATCH] added settingsdialog cancel --- chatterino.pro | 3 ++- setting.h | 19 +++++++++---------- settings.cpp | 7 ++++--- settings.h | 29 ++++++++++++++++++++--------- settingssnapshot.h | 36 ++++++++++++++++++++++++++++++++++++ widgets/notebookpage.cpp | 22 ++++++++++++---------- widgets/settingsdialog.cpp | 24 ++++++++++++++++++++++-- widgets/settingsdialog.h | 9 +++++++-- 8 files changed, 112 insertions(+), 37 deletions(-) create mode 100644 settingssnapshot.h diff --git a/chatterino.pro b/chatterino.pro index 895b3735a..f9dcbee56 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -108,7 +108,8 @@ HEADERS += account.h \ widgets/signallabel.h \ widgets/textinputdialog.h \ windows.h \ - widgets/resizingtextedit.h + widgets/resizingtextedit.h \ + settingssnapshot.h PRECOMPILED_HEADER = diff --git a/setting.h b/setting.h index b30272756..6c0b1e61c 100644 --- a/setting.h +++ b/setting.h @@ -15,8 +15,8 @@ public: { } - virtual void save(QSettings &settings) = 0; - virtual void load(const QSettings &settings) = 0; + virtual QVariant getVariant() = 0; + virtual void setVariant(QVariant value) = 0; const QString & getName() const @@ -56,19 +56,18 @@ public: } } - virtual void - save(QSettings &settings) final + virtual QVariant + getVariant() final { - settings.setValue(this->getName(), QVariant::fromValue(this->value)); + return QVariant::fromValue(value); } virtual void - load(const QSettings &settings) final + setVariant(QVariant value) final { - QVariant newValue = settings.value(this->getName(), QVariant()); - if (newValue.isValid()) { - assert(newValue.canConvert()); - this->set(newValue.value()); + if (value.isValid()) { + assert(value.canConvert()); + this->set(value.value()); } } diff --git a/settings.cpp b/settings.cpp index ef5cb0d1d..b8fcc5daf 100644 --- a/settings.cpp +++ b/settings.cpp @@ -16,7 +16,7 @@ Settings::Settings() , portable(false) , wordTypeMask(messages::Word::Default) , theme(this->settingsItems, "theme", "dark") - , user(this->settingsItems, "selectedUser", "") + , selectedUser(this->settingsItems, "selectedUser", "") , emoteScale(this->settingsItems, "emoteScale", 1.0) , scaleEmotesByLineHeight(this->settingsItems, "scaleEmotesByLineHeight", false) @@ -65,7 +65,7 @@ void Settings::save() { for (auto &item : settingsItems) { - item.get().save(settings); + this->settings.setValue(item.get().getName(), item.get().getVariant()); } } @@ -74,7 +74,8 @@ Settings::load() { for (auto &item : settingsItems) { qDebug() << "Loading settings for " << item.get().getName(); - item.get().load(settings); + + item.get().setVariant(this->settings.value(item.get().getName())); } } diff --git a/settings.h b/settings.h index 4fe370dc6..18d08f772 100644 --- a/settings.h +++ b/settings.h @@ -3,6 +3,7 @@ #include "messages/word.h" #include "setting.h" +#include "settingssnapshot.h" #include @@ -42,6 +43,24 @@ public: portable = value; } + QSettings & + getQSettings() + { + return settings; + } + + SettingsSnapshot + createSnapshot() + { + SettingsSnapshot snapshot; + + for (auto &item : this->settingsItems) { + snapshot.addItem(item, item.get().getVariant()); + } + + return snapshot; + } + signals: void wordTypeMaskChanged(); @@ -55,21 +74,13 @@ private: QSettings settings; std::vector> settingsItems; - // template - // T - // addSetting(T setting) - // { - // settingsItems.push_back(setting); - // return setting; - // } - bool portable; messages::Word::Type wordTypeMask; public: Setting theme; - Setting user; + Setting selectedUser; Setting emoteScale; Setting scaleEmotesByLineHeight; Setting showTimestamps; diff --git a/settingssnapshot.h b/settingssnapshot.h new file mode 100644 index 000000000..18a83737b --- /dev/null +++ b/settingssnapshot.h @@ -0,0 +1,36 @@ +#ifndef SETTINGSSNAPSHOT_H +#define SETTINGSSNAPSHOT_H + +#include "setting.h" + +struct SettingsSnapshot { +private: + std::vector< + std::pair, QVariant>> + items; + +public: + SettingsSnapshot() + : items() + { + } + + void + addItem(std::reference_wrapper setting, + const QVariant &value) + { + items.push_back( + std::pair, + QVariant>(setting.get(), value)); + } + + void + apply() + { + for (auto &item : this->items) { + item.first.get().setVariant(item.second); + } + } +}; + +#endif // SETTINGSSNAPSHOT_H diff --git a/widgets/notebookpage.cpp b/widgets/notebookpage.cpp index 4c3199f1d..daaf2a199 100644 --- a/widgets/notebookpage.cpp +++ b/widgets/notebookpage.cpp @@ -145,7 +145,8 @@ NotebookPage::dragEnterEvent(QDragEnterEvent *event) for (int i = 0; i < this->hbox.count() + 1; ++i) { this->dropRegions.push_back(DropRegion( QRect(((i * 4 - 1) * width() / this->hbox.count()) / 4, 0, - width() / this->hbox.count() / 2, height()), + width() / this->hbox.count() / 2 + 1, height() + 1), + std::pair(i, -1))); } @@ -156,8 +157,9 @@ NotebookPage::dragEnterEvent(QDragEnterEvent *event) this->dropRegions.push_back(DropRegion( QRect(i * width() / this->hbox.count(), ((j * 2 - 1) * height() / vbox->count()) / 2, - width() / this->hbox.count(), - height() / vbox->count()), + width() / this->hbox.count() + 1, + height() / vbox->count() + 1), + std::pair(i, j))); } } @@ -181,19 +183,19 @@ NotebookPage::setPreviewRect(QPoint mousePos) for (DropRegion region : this->dropRegions) { if (region.rect.contains(mousePos)) { this->preview.setBounds(region.rect); - // this->preview.move(region.rect.x(), region.rect.y()); - // this->preview.resize(region.rect.width(), - // region.rect.height()); - this->preview.show(); - this->preview.raise(); + + if (!this->preview.isVisible()) { + this->preview.show(); + this->preview.raise(); + } dropPosition = region.position; return; - } else { - this->preview.hide(); } } + + this->preview.hide(); } void diff --git a/widgets/settingsdialog.cpp b/widgets/settingsdialog.cpp index 01d12bbf2..fc72b34c3 100644 --- a/widgets/settingsdialog.cpp +++ b/widgets/settingsdialog.cpp @@ -13,6 +13,7 @@ namespace chatterino { namespace widgets { SettingsDialog::SettingsDialog() + : snapshot(Settings::getInstance().createSnapshot()) { QFile file(":/qss/settings.qss"); file.open(QFile::ReadOnly); @@ -43,6 +44,12 @@ SettingsDialog::SettingsDialog() buttonBox.addButton(&okButton, QDialogButtonBox::ButtonRole::AcceptRole); buttonBox.addButton(&cancelButton, QDialogButtonBox::ButtonRole::RejectRole); + + QObject::connect(&okButton, &QPushButton::clicked, this, + &SettingsDialog::okButtonClicked); + QObject::connect(&cancelButton, &QPushButton::clicked, this, + &SettingsDialog::cancelButtonClicked); + okButton.setText("OK"); cancelButton.setText("Cancel"); @@ -226,8 +233,7 @@ SettingsDialog::select(SettingsDialogTab *tab) /// Widget creation helpers QCheckBox * -SettingsDialog::createCheckbox(const QString &title, - Setting &setting) +SettingsDialog::createCheckbox(const QString &title, Setting &setting) { auto checkbox = new QCheckBox(title); @@ -240,5 +246,19 @@ SettingsDialog::createCheckbox(const QString &title, return checkbox; } +void +SettingsDialog::okButtonClicked() +{ + this->close(); +} + +void +SettingsDialog::cancelButtonClicked() +{ + snapshot.apply(); + + this->close(); +} + } // namespace widgets } // namespace chatterino diff --git a/widgets/settingsdialog.h b/widgets/settingsdialog.h index 52a57fbc8..ff60c40ab 100644 --- a/widgets/settingsdialog.h +++ b/widgets/settingsdialog.h @@ -2,6 +2,7 @@ #define SETTINGSDIALOG_H #include "settings.h" +#include "settingssnapshot.h" #include "widgets/settingsdialogtab.h" #include @@ -26,6 +27,8 @@ public: void select(SettingsDialogTab *tab); private: + SettingsSnapshot snapshot; + QVBoxLayout tabs; QVBoxLayout vbox; QHBoxLayout hbox; @@ -41,8 +44,10 @@ private: SettingsDialogTab *selectedTab = NULL; /// Widget creation helpers - QCheckBox *createCheckbox(const QString &title, - Setting &setting); + QCheckBox *createCheckbox(const QString &title, Setting &setting); + + void okButtonClicked(); + void cancelButtonClicked(); }; } // namespace widgets