added settingsdialog cancel

This commit is contained in:
fourtf 2017-01-24 19:51:57 +01:00
parent ad4c2901af
commit de8f6d1e82
8 changed files with 112 additions and 37 deletions

View file

@ -108,7 +108,8 @@ HEADERS += account.h \
widgets/signallabel.h \ widgets/signallabel.h \
widgets/textinputdialog.h \ widgets/textinputdialog.h \
windows.h \ windows.h \
widgets/resizingtextedit.h widgets/resizingtextedit.h \
settingssnapshot.h
PRECOMPILED_HEADER = PRECOMPILED_HEADER =

View file

@ -15,8 +15,8 @@ public:
{ {
} }
virtual void save(QSettings &settings) = 0; virtual QVariant getVariant() = 0;
virtual void load(const QSettings &settings) = 0; virtual void setVariant(QVariant value) = 0;
const QString & const QString &
getName() const getName() const
@ -56,19 +56,18 @@ public:
} }
} }
virtual void virtual QVariant
save(QSettings &settings) final getVariant() final
{ {
settings.setValue(this->getName(), QVariant::fromValue(this->value)); return QVariant::fromValue(value);
} }
virtual void virtual void
load(const QSettings &settings) final setVariant(QVariant value) final
{ {
QVariant newValue = settings.value(this->getName(), QVariant()); if (value.isValid()) {
if (newValue.isValid()) { assert(value.canConvert<T>());
assert(newValue.canConvert<T>()); this->set(value.value<T>());
this->set(newValue.value<T>());
} }
} }

View file

@ -16,7 +16,7 @@ Settings::Settings()
, portable(false) , portable(false)
, wordTypeMask(messages::Word::Default) , wordTypeMask(messages::Word::Default)
, theme(this->settingsItems, "theme", "dark") , theme(this->settingsItems, "theme", "dark")
, user(this->settingsItems, "selectedUser", "") , selectedUser(this->settingsItems, "selectedUser", "")
, emoteScale(this->settingsItems, "emoteScale", 1.0) , emoteScale(this->settingsItems, "emoteScale", 1.0)
, scaleEmotesByLineHeight(this->settingsItems, "scaleEmotesByLineHeight", , scaleEmotesByLineHeight(this->settingsItems, "scaleEmotesByLineHeight",
false) false)
@ -65,7 +65,7 @@ void
Settings::save() Settings::save()
{ {
for (auto &item : settingsItems) { 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) { for (auto &item : settingsItems) {
qDebug() << "Loading settings for " << item.get().getName(); qDebug() << "Loading settings for " << item.get().getName();
item.get().load(settings);
item.get().setVariant(this->settings.value(item.get().getName()));
} }
} }

View file

@ -3,6 +3,7 @@
#include "messages/word.h" #include "messages/word.h"
#include "setting.h" #include "setting.h"
#include "settingssnapshot.h"
#include <QSettings> #include <QSettings>
@ -42,6 +43,24 @@ public:
portable = value; portable = value;
} }
QSettings &
getQSettings()
{
return settings;
}
SettingsSnapshot
createSnapshot()
{
SettingsSnapshot snapshot;
for (auto &item : this->settingsItems) {
snapshot.addItem(item, item.get().getVariant());
}
return snapshot;
}
signals: signals:
void wordTypeMaskChanged(); void wordTypeMaskChanged();
@ -55,21 +74,13 @@ private:
QSettings settings; QSettings settings;
std::vector<std::reference_wrapper<BaseSetting>> settingsItems; std::vector<std::reference_wrapper<BaseSetting>> settingsItems;
// template <class T>
// T
// addSetting(T setting)
// {
// settingsItems.push_back(setting);
// return setting;
// }
bool portable; bool portable;
messages::Word::Type wordTypeMask; messages::Word::Type wordTypeMask;
public: public:
Setting<QString> theme; Setting<QString> theme;
Setting<QString> user; Setting<QString> selectedUser;
Setting<float> emoteScale; Setting<float> emoteScale;
Setting<bool> scaleEmotesByLineHeight; Setting<bool> scaleEmotesByLineHeight;
Setting<bool> showTimestamps; Setting<bool> showTimestamps;

36
settingssnapshot.h Normal file
View file

@ -0,0 +1,36 @@
#ifndef SETTINGSSNAPSHOT_H
#define SETTINGSSNAPSHOT_H
#include "setting.h"
struct SettingsSnapshot {
private:
std::vector<
std::pair<std::reference_wrapper<chatterino::BaseSetting>, QVariant>>
items;
public:
SettingsSnapshot()
: items()
{
}
void
addItem(std::reference_wrapper<chatterino::BaseSetting> setting,
const QVariant &value)
{
items.push_back(
std::pair<std::reference_wrapper<chatterino::BaseSetting>,
QVariant>(setting.get(), value));
}
void
apply()
{
for (auto &item : this->items) {
item.first.get().setVariant(item.second);
}
}
};
#endif // SETTINGSSNAPSHOT_H

View file

@ -145,7 +145,8 @@ NotebookPage::dragEnterEvent(QDragEnterEvent *event)
for (int i = 0; i < this->hbox.count() + 1; ++i) { for (int i = 0; i < this->hbox.count() + 1; ++i) {
this->dropRegions.push_back(DropRegion( this->dropRegions.push_back(DropRegion(
QRect(((i * 4 - 1) * width() / this->hbox.count()) / 4, 0, 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<int, int>(i, -1))); std::pair<int, int>(i, -1)));
} }
@ -156,8 +157,9 @@ NotebookPage::dragEnterEvent(QDragEnterEvent *event)
this->dropRegions.push_back(DropRegion( this->dropRegions.push_back(DropRegion(
QRect(i * width() / this->hbox.count(), QRect(i * width() / this->hbox.count(),
((j * 2 - 1) * height() / vbox->count()) / 2, ((j * 2 - 1) * height() / vbox->count()) / 2,
width() / this->hbox.count(), width() / this->hbox.count() + 1,
height() / vbox->count()), height() / vbox->count() + 1),
std::pair<int, int>(i, j))); std::pair<int, int>(i, j)));
} }
} }
@ -181,19 +183,19 @@ NotebookPage::setPreviewRect(QPoint mousePos)
for (DropRegion region : this->dropRegions) { for (DropRegion region : this->dropRegions) {
if (region.rect.contains(mousePos)) { if (region.rect.contains(mousePos)) {
this->preview.setBounds(region.rect); this->preview.setBounds(region.rect);
// this->preview.move(region.rect.x(), region.rect.y());
// this->preview.resize(region.rect.width(), if (!this->preview.isVisible()) {
// region.rect.height()); this->preview.show();
this->preview.show(); this->preview.raise();
this->preview.raise(); }
dropPosition = region.position; dropPosition = region.position;
return; return;
} else {
this->preview.hide();
} }
} }
this->preview.hide();
} }
void void

View file

@ -13,6 +13,7 @@ namespace chatterino {
namespace widgets { namespace widgets {
SettingsDialog::SettingsDialog() SettingsDialog::SettingsDialog()
: snapshot(Settings::getInstance().createSnapshot())
{ {
QFile file(":/qss/settings.qss"); QFile file(":/qss/settings.qss");
file.open(QFile::ReadOnly); file.open(QFile::ReadOnly);
@ -43,6 +44,12 @@ SettingsDialog::SettingsDialog()
buttonBox.addButton(&okButton, QDialogButtonBox::ButtonRole::AcceptRole); buttonBox.addButton(&okButton, QDialogButtonBox::ButtonRole::AcceptRole);
buttonBox.addButton(&cancelButton, buttonBox.addButton(&cancelButton,
QDialogButtonBox::ButtonRole::RejectRole); QDialogButtonBox::ButtonRole::RejectRole);
QObject::connect(&okButton, &QPushButton::clicked, this,
&SettingsDialog::okButtonClicked);
QObject::connect(&cancelButton, &QPushButton::clicked, this,
&SettingsDialog::cancelButtonClicked);
okButton.setText("OK"); okButton.setText("OK");
cancelButton.setText("Cancel"); cancelButton.setText("Cancel");
@ -226,8 +233,7 @@ SettingsDialog::select(SettingsDialogTab *tab)
/// Widget creation helpers /// Widget creation helpers
QCheckBox * QCheckBox *
SettingsDialog::createCheckbox(const QString &title, SettingsDialog::createCheckbox(const QString &title, Setting<bool> &setting)
Setting<bool> &setting)
{ {
auto checkbox = new QCheckBox(title); auto checkbox = new QCheckBox(title);
@ -240,5 +246,19 @@ SettingsDialog::createCheckbox(const QString &title,
return checkbox; return checkbox;
} }
void
SettingsDialog::okButtonClicked()
{
this->close();
}
void
SettingsDialog::cancelButtonClicked()
{
snapshot.apply();
this->close();
}
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino

View file

@ -2,6 +2,7 @@
#define SETTINGSDIALOG_H #define SETTINGSDIALOG_H
#include "settings.h" #include "settings.h"
#include "settingssnapshot.h"
#include "widgets/settingsdialogtab.h" #include "widgets/settingsdialogtab.h"
#include <QButtonGroup> #include <QButtonGroup>
@ -26,6 +27,8 @@ public:
void select(SettingsDialogTab *tab); void select(SettingsDialogTab *tab);
private: private:
SettingsSnapshot snapshot;
QVBoxLayout tabs; QVBoxLayout tabs;
QVBoxLayout vbox; QVBoxLayout vbox;
QHBoxLayout hbox; QHBoxLayout hbox;
@ -41,8 +44,10 @@ private:
SettingsDialogTab *selectedTab = NULL; SettingsDialogTab *selectedTab = NULL;
/// Widget creation helpers /// Widget creation helpers
QCheckBox *createCheckbox(const QString &title, QCheckBox *createCheckbox(const QString &title, Setting<bool> &setting);
Setting<bool> &setting);
void okButtonClicked();
void cancelButtonClicked();
}; };
} // namespace widgets } // namespace widgets