Some settings can now be "reverted" by pressing cancel in the settings dialog

Modify visibility of some members of SettingsDialog
update external libraries
Progress on #180
This commit is contained in:
Rasmus Karlsson
2017-12-28 19:03:52 +01:00
parent 7d259fe7e6
commit 258288bad9
5 changed files with 132 additions and 25 deletions
+49 -10
View File
@@ -1,7 +1,7 @@
#include "settingsmanager.hpp" #include "settingsmanager.hpp"
#include "appdatapath.hpp" #include "appdatapath.hpp"
#include "debug/log.hpp"
#include <QDebug>
#include <QDir> #include <QDir>
#include <QStandardPaths> #include <QStandardPaths>
@@ -9,6 +9,13 @@ using namespace chatterino::messages;
namespace chatterino { namespace chatterino {
std::vector<std::weak_ptr<pajlada::Settings::ISettingData>> _settings;
void _registerSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting)
{
_settings.push_back(setting);
}
SettingsManager::SettingsManager() SettingsManager::SettingsManager()
: streamlinkPath("/behaviour/streamlink/path", "") : streamlinkPath("/behaviour/streamlink/path", "")
, preferredQuality("/behaviour/streamlink/quality", "Choose") , preferredQuality("/behaviour/streamlink/quality", "Choose")
@@ -17,6 +24,7 @@ SettingsManager::SettingsManager()
, highlightProperties(this->settingsItems, "highlightProperties", , highlightProperties(this->settingsItems, "highlightProperties",
QMap<QString, QPair<bool, bool>>()) QMap<QString, QPair<bool, bool>>())
, highlightUserBlacklist(this->settingsItems, "highlightUserBlacklist", "") , highlightUserBlacklist(this->settingsItems, "highlightUserBlacklist", "")
, snapshot(nullptr)
, settings(Path::getAppdataPath() + "settings.ini", QSettings::IniFormat) , settings(Path::getAppdataPath() + "settings.ini", QSettings::IniFormat)
{ {
this->wordMaskListener.addSetting(this->showTimestamps); this->wordMaskListener.addSetting(this->showTimestamps);
@@ -62,7 +70,6 @@ void SettingsManager::load()
} else { } else {
this->settings.beginGroup("Highlights"); this->settings.beginGroup("Highlights");
QStringList list = this->settings.childGroups(); QStringList list = this->settings.childGroups();
qDebug() << list.join(",");
for (auto string : list) { for (auto string : list) {
this->settings.beginGroup(string); this->settings.beginGroup(string);
highlightProperties.insertMap(string, highlightProperties.insertMap(string,
@@ -129,19 +136,51 @@ void SettingsManager::updateWordTypeMask()
} }
} }
SettingsSnapshot SettingsManager::createSnapshot() void SettingsManager::saveSnapshot()
{ {
SettingsSnapshot snapshot; rapidjson::Document *d = new rapidjson::Document(rapidjson::kObjectType);
rapidjson::Document::AllocatorType &a = d->GetAllocator();
for (auto &item : this->settingsItems) { for (const auto &weakSetting : _settings) {
if (item.get().getName() != "highlightProperties") { auto setting = weakSetting.lock();
snapshot.addItem(item, item.get().getVariant()); if (!setting) {
} else { continue;
snapshot.mapItems = highlightProperties.get();
} }
rapidjson::Value key(setting->getPath().c_str(), a);
rapidjson::Value val = setting->marshalInto(*d);
d->AddMember(key.Move(), val.Move(), a);
} }
return snapshot; this->snapshot.reset(d);
debug::Log("hehe: {}", pajlada::Settings::SettingManager::stringify(*d));
}
void SettingsManager::recallSnapshot()
{
if (!this->snapshot) {
return;
}
const auto &snapshotObject = this->snapshot->GetObject();
for (const auto &weakSetting : _settings) {
auto setting = weakSetting.lock();
if (!setting) {
debug::Log("Error stage 1 of loading");
continue;
}
const char *path = setting->getPath().c_str();
if (!snapshotObject.HasMember(path)) {
debug::Log("Error stage 2 of loading");
continue;
}
setting->unmarshalValue(snapshotObject[path]);
}
} }
} // namespace chatterino } // namespace chatterino
+71 -4
View File
@@ -2,7 +2,6 @@
#include "messages/word.hpp" #include "messages/word.hpp"
#include "setting.hpp" #include "setting.hpp"
#include "settingssnapshot.hpp"
#include <QSettings> #include <QSettings>
#include <pajlada/settings/setting.hpp> #include <pajlada/settings/setting.hpp>
@@ -10,12 +9,76 @@
namespace chatterino { namespace chatterino {
static void _registerSetting(std::weak_ptr<pajlada::Settings::ISettingData> setting);
template <typename Type>
class ChatterinoSetting : public pajlada::Settings::Setting<Type>
{
public:
ChatterinoSetting(const std::string &_path, const Type &_defaultValue)
: pajlada::Settings::Setting<Type>(_path, _defaultValue)
{
_registerSetting(this->data);
}
void saveRecall();
ChatterinoSetting &operator=(const Type &newValue)
{
assert(this->data != nullptr);
this->setValue(newValue);
return *this;
}
template <typename T2>
ChatterinoSetting &operator=(const T2 &newValue)
{
assert(this->data != nullptr);
this->setValue(newValue);
return *this;
}
ChatterinoSetting &operator=(Type &&newValue) noexcept
{
assert(this->data != nullptr);
this->setValue(std::move(newValue));
return *this;
}
bool operator==(const Type &rhs) const
{
assert(this->data != nullptr);
return this->getValue() == rhs;
}
bool operator!=(const Type &rhs) const
{
assert(this->data != nullptr);
return this->getValue() != rhs;
}
operator const Type() const
{
assert(this->data != nullptr);
return this->getValue();
}
};
class SettingsManager : public QObject class SettingsManager : public QObject
{ {
Q_OBJECT Q_OBJECT
using BoolSetting = pajlada::Settings::Setting<bool>; using BoolSetting = ChatterinoSetting<bool>;
using FloatSetting = pajlada::Settings::Setting<float>; using FloatSetting = ChatterinoSetting<float>;
public: public:
void load(); void load();
@@ -24,7 +87,6 @@ public:
messages::Word::Flags getWordTypeMask(); messages::Word::Flags getWordTypeMask();
bool isIgnoredEmote(const QString &emote); bool isIgnoredEmote(const QString &emote);
QSettings &getQSettings(); QSettings &getQSettings();
SettingsSnapshot createSnapshot();
/// Appearance /// Appearance
BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true}; BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true};
@@ -87,10 +149,15 @@ public:
} }
void updateWordTypeMask(); void updateWordTypeMask();
void saveSnapshot();
void recallSnapshot();
signals: signals:
void wordTypeMaskChanged(); void wordTypeMaskChanged();
private: private:
std::unique_ptr<rapidjson::Document> snapshot;
SettingsManager(); SettingsManager();
QSettings settings; QSettings settings;
+8 -7
View File
@@ -27,7 +27,6 @@ namespace widgets {
SettingsDialog::SettingsDialog() SettingsDialog::SettingsDialog()
: BaseWidget() : BaseWidget()
, snapshot(SettingsManager::getInstance().createSnapshot())
, usernameDisplayMode( , usernameDisplayMode(
"/appearance/messages/usernameDisplayMode", "/appearance/messages/usernameDisplayMode",
twitch::TwitchMessageBuilder::UsernameDisplayMode::UsernameAndLocalizedName) twitch::TwitchMessageBuilder::UsernameDisplayMode::UsernameAndLocalizedName)
@@ -615,6 +614,8 @@ void SettingsDialog::showDialog(PreferredTab preferredTab)
void SettingsDialog::refresh() void SettingsDialog::refresh()
{ {
this->ui.accountSwitchWidget->refresh(); this->ui.accountSwitchWidget->refresh();
SettingsManager::getInstance().saveSnapshot();
} }
void SettingsDialog::dpiMultiplierChanged(float oldDpi, float newDpi) void SettingsDialog::dpiMultiplierChanged(float oldDpi, float newDpi)
@@ -683,7 +684,9 @@ QCheckBox *SettingsDialog::createCheckbox(const QString &title,
auto checkbox = new QCheckBox(title); auto checkbox = new QCheckBox(title);
// Set checkbox initial state // Set checkbox initial state
checkbox->setChecked(setting.getValue()); setting.connect([checkbox](const bool &value, auto) {
checkbox->setChecked(value); //
});
QObject::connect(checkbox, &QCheckBox::toggled, this, [&setting](bool state) { QObject::connect(checkbox, &QCheckBox::toggled, this, [&setting](bool state) {
qDebug() << "update checkbox value"; qDebug() << "update checkbox value";
@@ -751,13 +754,11 @@ void SettingsDialog::okButtonClicked()
void SettingsDialog::cancelButtonClicked() void SettingsDialog::cancelButtonClicked()
{ {
// TODO: Re-implement the snapshot feature properly auto &settings = SettingsManager::getInstance();
auto &instance = SettingsManager::getInstance();
this->snapshot.apply(); settings.recallSnapshot();
instance.highlightProperties.set(this->snapshot.mapItems);
QStringList list = instance.highlightProperties.get().keys(); QStringList list = settings.highlightProperties.get().keys();
list.removeDuplicates(); list.removeDuplicates();
while (globalHighlights->count() > 0) { while (globalHighlights->count() > 0) {
delete globalHighlights->takeItem(0); delete globalHighlights->takeItem(0);
+3 -3
View File
@@ -1,7 +1,6 @@
#pragma once #pragma once
#include "settingsmanager.hpp" #include "settingsmanager.hpp"
#include "settingssnapshot.hpp"
#include "widgets/accountswitchwidget.hpp" #include "widgets/accountswitchwidget.hpp"
#include "widgets/helper/settingsdialogtab.hpp" #include "widgets/helper/settingsdialogtab.hpp"
@@ -27,11 +26,13 @@ namespace widgets {
class SettingsDialog : public BaseWidget class SettingsDialog : public BaseWidget
{ {
public:
SettingsDialog(); SettingsDialog();
void select(SettingsDialogTab *tab); void select(SettingsDialogTab *tab);
friend class SettingsDialogTab;
public:
enum class PreferredTab { enum class PreferredTab {
NoPreference, NoPreference,
Accounts, Accounts,
@@ -45,7 +46,6 @@ protected:
private: private:
void refresh(); void refresh();
SettingsSnapshot snapshot;
std::vector<SettingsDialogTab *> tabs; std::vector<SettingsDialogTab *> tabs;
pajlada::Settings::Setting<int> usernameDisplayMode; pajlada::Settings::Setting<int> usernameDisplayMode;