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

@ -1 +1 @@
Subproject commit 89f767a5ed2ea5724a4bf5b6170365e5d069634a
Subproject commit 75aca034359fc0e2145908beb459267e8be3d7ca

View file

@ -1,7 +1,7 @@
#include "settingsmanager.hpp"
#include "appdatapath.hpp"
#include "debug/log.hpp"
#include <QDebug>
#include <QDir>
#include <QStandardPaths>
@ -9,6 +9,13 @@ using namespace chatterino::messages;
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()
: streamlinkPath("/behaviour/streamlink/path", "")
, preferredQuality("/behaviour/streamlink/quality", "Choose")
@ -17,6 +24,7 @@ SettingsManager::SettingsManager()
, highlightProperties(this->settingsItems, "highlightProperties",
QMap<QString, QPair<bool, bool>>())
, highlightUserBlacklist(this->settingsItems, "highlightUserBlacklist", "")
, snapshot(nullptr)
, settings(Path::getAppdataPath() + "settings.ini", QSettings::IniFormat)
{
this->wordMaskListener.addSetting(this->showTimestamps);
@ -62,7 +70,6 @@ void SettingsManager::load()
} else {
this->settings.beginGroup("Highlights");
QStringList list = this->settings.childGroups();
qDebug() << list.join(",");
for (auto string : list) {
this->settings.beginGroup(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) {
if (item.get().getName() != "highlightProperties") {
snapshot.addItem(item, item.get().getVariant());
} else {
snapshot.mapItems = highlightProperties.get();
for (const auto &weakSetting : _settings) {
auto setting = weakSetting.lock();
if (!setting) {
continue;
}
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

View file

@ -2,7 +2,6 @@
#include "messages/word.hpp"
#include "setting.hpp"
#include "settingssnapshot.hpp"
#include <QSettings>
#include <pajlada/settings/setting.hpp>
@ -10,12 +9,76 @@
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
{
Q_OBJECT
using BoolSetting = pajlada::Settings::Setting<bool>;
using FloatSetting = pajlada::Settings::Setting<float>;
using BoolSetting = ChatterinoSetting<bool>;
using FloatSetting = ChatterinoSetting<float>;
public:
void load();
@ -24,7 +87,6 @@ public:
messages::Word::Flags getWordTypeMask();
bool isIgnoredEmote(const QString &emote);
QSettings &getQSettings();
SettingsSnapshot createSnapshot();
/// Appearance
BoolSetting showTimestamps = {"/appearance/messages/showTimestamps", true};
@ -87,10 +149,15 @@ public:
}
void updateWordTypeMask();
void saveSnapshot();
void recallSnapshot();
signals:
void wordTypeMaskChanged();
private:
std::unique_ptr<rapidjson::Document> snapshot;
SettingsManager();
QSettings settings;

View file

@ -27,7 +27,6 @@ namespace widgets {
SettingsDialog::SettingsDialog()
: BaseWidget()
, snapshot(SettingsManager::getInstance().createSnapshot())
, usernameDisplayMode(
"/appearance/messages/usernameDisplayMode",
twitch::TwitchMessageBuilder::UsernameDisplayMode::UsernameAndLocalizedName)
@ -615,6 +614,8 @@ void SettingsDialog::showDialog(PreferredTab preferredTab)
void SettingsDialog::refresh()
{
this->ui.accountSwitchWidget->refresh();
SettingsManager::getInstance().saveSnapshot();
}
void SettingsDialog::dpiMultiplierChanged(float oldDpi, float newDpi)
@ -683,7 +684,9 @@ QCheckBox *SettingsDialog::createCheckbox(const QString &title,
auto checkbox = new QCheckBox(title);
// 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) {
qDebug() << "update checkbox value";
@ -751,13 +754,11 @@ void SettingsDialog::okButtonClicked()
void SettingsDialog::cancelButtonClicked()
{
// TODO: Re-implement the snapshot feature properly
auto &instance = SettingsManager::getInstance();
auto &settings = SettingsManager::getInstance();
this->snapshot.apply();
instance.highlightProperties.set(this->snapshot.mapItems);
settings.recallSnapshot();
QStringList list = instance.highlightProperties.get().keys();
QStringList list = settings.highlightProperties.get().keys();
list.removeDuplicates();
while (globalHighlights->count() > 0) {
delete globalHighlights->takeItem(0);

View file

@ -1,7 +1,6 @@
#pragma once
#include "settingsmanager.hpp"
#include "settingssnapshot.hpp"
#include "widgets/accountswitchwidget.hpp"
#include "widgets/helper/settingsdialogtab.hpp"
@ -27,11 +26,13 @@ namespace widgets {
class SettingsDialog : public BaseWidget
{
public:
SettingsDialog();
void select(SettingsDialogTab *tab);
friend class SettingsDialogTab;
public:
enum class PreferredTab {
NoPreference,
Accounts,
@ -45,7 +46,6 @@ protected:
private:
void refresh();
SettingsSnapshot snapshot;
std::vector<SettingsDialogTab *> tabs;
pajlada::Settings::Setting<int> usernameDisplayMode;