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/textinputdialog.h \
windows.h \
widgets/resizingtextedit.h
widgets/resizingtextedit.h \
settingssnapshot.h
PRECOMPILED_HEADER =

View file

@ -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<T>());
this->set(newValue.value<T>());
if (value.isValid()) {
assert(value.canConvert<T>());
this->set(value.value<T>());
}
}

View file

@ -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()));
}
}

View file

@ -3,6 +3,7 @@
#include "messages/word.h"
#include "setting.h"
#include "settingssnapshot.h"
#include <QSettings>
@ -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<std::reference_wrapper<BaseSetting>> settingsItems;
// template <class T>
// T
// addSetting(T setting)
// {
// settingsItems.push_back(setting);
// return setting;
// }
bool portable;
messages::Word::Type wordTypeMask;
public:
Setting<QString> theme;
Setting<QString> user;
Setting<QString> selectedUser;
Setting<float> emoteScale;
Setting<bool> scaleEmotesByLineHeight;
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) {
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<int, int>(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<int, int>(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

View file

@ -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<bool> &setting)
SettingsDialog::createCheckbox(const QString &title, Setting<bool> &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

View file

@ -2,6 +2,7 @@
#define SETTINGSDIALOG_H
#include "settings.h"
#include "settingssnapshot.h"
#include "widgets/settingsdialogtab.h"
#include <QButtonGroup>
@ -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<bool> &setting);
QCheckBox *createCheckbox(const QString &title, Setting<bool> &setting);
void okButtonClicked();
void cancelButtonClicked();
};
} // namespace widgets