From 9044700ec6b85032ae200f34fe16aafc73cfffcf Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Thu, 4 Jan 2018 02:19:35 +0100 Subject: [PATCH] Convert remaining settings to the new settings system --- chatterino.pro | 2 - src/messages/word.cpp | 2 +- src/messages/word.hpp | 6 +- src/setting.hpp | 85 ----------------------------- src/settingssnapshot.hpp | 38 ------------- src/singletons/settingsmanager.cpp | 13 +---- src/singletons/settingsmanager.hpp | 19 +++---- src/twitch/twitchmessagebuilder.cpp | 4 +- src/widgets/accountpopup.cpp | 11 ++-- src/widgets/accountpopup.hpp | 2 +- src/widgets/settingsdialog.cpp | 25 ++------- src/widgets/settingsdialog.hpp | 1 - 12 files changed, 27 insertions(+), 181 deletions(-) delete mode 100644 src/setting.hpp delete mode 100644 src/settingssnapshot.hpp diff --git a/chatterino.pro b/chatterino.pro index 694a3a390..588370ad6 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -126,7 +126,6 @@ HEADERS += \ src/messages/message.hpp \ src/messages/word.hpp \ src/messages/wordpart.hpp \ - src/setting.hpp \ src/twitch/emotevalue.hpp \ src/widgets/notebook.hpp \ src/widgets/helper/notebookbutton.hpp \ @@ -138,7 +137,6 @@ HEADERS += \ src/widgets/helper/signallabel.hpp \ src/widgets/textinputdialog.hpp \ src/widgets/helper/resizingtextedit.hpp \ - src/settingssnapshot.hpp \ src/messages/limitedqueue.hpp \ src/messages/limitedqueuesnapshot.hpp \ src/messages/messageref.hpp \ diff --git a/src/messages/word.cpp b/src/messages/word.cpp index bc38adc8c..e5f0f5ee9 100644 --- a/src/messages/word.cpp +++ b/src/messages/word.cpp @@ -67,7 +67,7 @@ QSize Word::getSize(float scale) const const int mediumTextLineHeight = singletons::FontManager::getInstance().getFontMetrics(this->font, scale).height(); const qreal emoteScale = - singletons::SettingManager::getInstance().emoteScale.get() * scale; + singletons::SettingManager::getInstance().emoteScale.getValue() * scale; const bool scaleEmotesByLineHeight = singletons::SettingManager::getInstance().scaleEmotesByLineHeight; diff --git a/src/messages/word.hpp b/src/messages/word.hpp index 0aa796a6f..592e0896a 100644 --- a/src/messages/word.hpp +++ b/src/messages/word.hpp @@ -1,9 +1,9 @@ #pragma once -#include "singletons/fontmanager.hpp" #include "messages/lazyloadedimage.hpp" #include "messages/link.hpp" #include "messages/messagecolor.hpp" +#include "singletons/fontmanager.hpp" //#include "wordflags.hpp" #include @@ -96,8 +96,8 @@ public: explicit Word(LazyLoadedImage *_image, Flags getFlags, const QString ©text, const QString &tooltip, const Link &getLink = Link()); explicit Word(const QString &_text, Flags getFlags, const MessageColor &textColor, - singletons::FontManager::Type font, const QString ©text, const QString &tooltip, - const Link &getLink = Link()); + singletons::FontManager::Type font, const QString ©text, + const QString &tooltip, const Link &getLink = Link()); bool isImage() const; bool isText() const; diff --git a/src/setting.hpp b/src/setting.hpp deleted file mode 100644 index 781367a19..000000000 --- a/src/setting.hpp +++ /dev/null @@ -1,85 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace chatterino { - -class BaseSetting -{ -public: - BaseSetting(const QString &_name) - : name(_name) - { - } - - virtual QVariant getVariant() = 0; - virtual void setVariant(QVariant value) = 0; - - const QString &getName() const - { - return this->name; - } - -private: - QString name; -}; - -template -class Setting : public BaseSetting -{ -public: - Setting(std::vector> &settingItems, const QString &_name, - const T &defaultValue) - : BaseSetting(_name) - , value(defaultValue) - { - settingItems.push_back(*this); - } - - const T &get() const - { - return this->value; - } - - T &getnonConst() - { - return this->value; - } - - void set(const T &newValue) - { - if (this->value != newValue) { - this->value = newValue; - - valueChanged(newValue); - } - } - - virtual QVariant getVariant() final - { - return QVariant::fromValue(this->value); - } - - virtual void setVariant(QVariant newValue) final - { - if (newValue.isValid()) { - assert(newValue.canConvert()); - set(newValue.value()); - } - } - - void insertMap(QString id, bool sound, bool task) - { - QPair pair(sound, task); - this->value.insert(id, pair); - } - - boost::signals2::signal valueChanged; - -private: - T value; -}; - -} // namespace chatterino diff --git a/src/settingssnapshot.hpp b/src/settingssnapshot.hpp deleted file mode 100644 index 96a058ba7..000000000 --- a/src/settingssnapshot.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include "setting.hpp" - -namespace chatterino { - -struct SettingsSnapshot { -public: - SettingsSnapshot() - { - } - - void addItem(std::reference_wrapper setting, const QVariant &value) - { - this->items.push_back( - std::pair, QVariant>(setting.get(), value)); - } - - void addMapItem(QString string, QPair pair) - { - QMap> map; - this->mapItems.insert(string, pair); - } - - void apply() - { - for (auto &item : this->items) { - item.first.get().setVariant(item.second); - } - } - - QMap> mapItems; - -private: - std::vector, QVariant>> items; -}; - -} // namespace chatterino diff --git a/src/singletons/settingsmanager.cpp b/src/singletons/settingsmanager.cpp index 75cab0012..b5f939e7b 100644 --- a/src/singletons/settingsmanager.cpp +++ b/src/singletons/settingsmanager.cpp @@ -18,13 +18,7 @@ void _registerSetting(std::weak_ptr setting) } SettingManager::SettingManager() - : streamlinkPath("/behaviour/streamlink/path", "") - , preferredQuality("/behaviour/streamlink/quality", "Choose") - , emoteScale(this->settingsItems, "emoteScale", 1.0) - , pathHighlightSound(this->settingsItems, "pathHighlightSound", "qrc:/sounds/ping2.wav") - , highlightUserBlacklist(this->settingsItems, "highlightUserBlacklist", "") - , snapshot(nullptr) - , settings(Path::getAppdataPath() + "settings.ini", QSettings::IniFormat) + : snapshot(nullptr) { this->wordMaskListener.addSetting(this->showTimestamps); this->wordMaskListener.addSetting(this->showTimestampSeconds); @@ -48,11 +42,6 @@ bool SettingManager::isIgnoredEmote(const QString &) return false; } -QSettings &SettingManager::getQSettings() -{ - return this->settings; -} - void SettingManager::load() { // Just to make sure the singleton is initialized diff --git a/src/singletons/settingsmanager.hpp b/src/singletons/settingsmanager.hpp index f381f4bf0..7120469cb 100644 --- a/src/singletons/settingsmanager.hpp +++ b/src/singletons/settingsmanager.hpp @@ -2,10 +2,8 @@ #include "messages/highlightphrase.hpp" #include "messages/word.hpp" -#include "setting.hpp" #include "singletons/helper/chatterinosetting.hpp" -#include #include #include @@ -20,11 +18,12 @@ class SettingManager : public QObject using BoolSetting = ChatterinoSetting; using FloatSetting = ChatterinoSetting; + using StringSetting = ChatterinoSetting; + using QStringSetting = ChatterinoSetting; public: messages::Word::Flags getWordTypeMask(); bool isIgnoredEmote(const QString &emote); - QSettings &getQSettings(); void load(); @@ -47,6 +46,8 @@ public: BoolSetting allowDuplicateMessages = {"/behaviour/allowDuplicateMessages", true}; BoolSetting mentionUsersWithAt = {"/behaviour/mentionUsersWithAt", false}; FloatSetting mouseScrollMultiplier = {"/behaviour/mouseScrollMultiplier", 1.0}; + StringSetting streamlinkPath = {"/behaviour/streamlink/path", ""}; + StringSetting preferredQuality = {"/behaviour/streamlink/quality", "Choose"}; /// Commands BoolSetting allowCommandsAtEnd = {"/commands/allowCommandsAtEnd", false}; @@ -58,6 +59,7 @@ public: BoolSetting enableFfzEmotes = {"/emotes/enableFFZEmotes", true}; BoolSetting enableEmojis = {"/emotes/enableEmojis", true}; BoolSetting enableGifAnimations = {"/emotes/enableGifAnimations", true}; + FloatSetting emoteScale = {"/emotes/scale", 1.f}; /// Links BoolSetting linksDoubleClickOnly = {"/links/doubleClickToOpen", false}; @@ -69,15 +71,12 @@ public: BoolSetting enableHighlightTaskbar = {"/highlighting/enableTaskbarFlashing", true}; BoolSetting customHighlightSound = {"/highlighting/useCustomSound", false}; - pajlada::Settings::Setting streamlinkPath; - pajlada::Settings::Setting preferredQuality; - - Setting emoteScale; ChatterinoSetting> highlightProperties = { "/highlighting/highlights"}; - Setting pathHighlightSound; - Setting highlightUserBlacklist; + QStringSetting pathHighlightSound = {"/highlighting/highlightSoundPath", + "qrc:/sounds/ping2.wav"}; + QStringSetting highlightUserBlacklist = {"/highlighting/blacklistedUsers", ""}; BoolSetting highlightAlwaysPlaySound = {"/highlighting/alwaysPlaySound", false}; @@ -101,8 +100,6 @@ private: SettingManager(); - QSettings settings; - std::vector> settingsItems; messages::Word::Flags wordTypeMask = messages::Word::Default; pajlada::Settings::SettingListener wordMaskListener; diff --git a/src/twitch/twitchmessagebuilder.cpp b/src/twitch/twitchmessagebuilder.cpp index 852aeba1a..5ea5793a7 100644 --- a/src/twitch/twitchmessagebuilder.cpp +++ b/src/twitch/twitchmessagebuilder.cpp @@ -396,7 +396,7 @@ void TwitchMessageBuilder::parseHighlights() // update the media player url if necessary QUrl highlightSoundUrl; if (settings.customHighlightSound) { - highlightSoundUrl = QUrl(settings.pathHighlightSound.get()); + highlightSoundUrl = QUrl(settings.pathHighlightSound.getValue()); } else { highlightSoundUrl = QUrl("qrc:/sounds/ping2.wav"); } @@ -408,7 +408,7 @@ void TwitchMessageBuilder::parseHighlights() } QStringList blackList = - settings.highlightUserBlacklist.get().split("\n", QString::SkipEmptyParts); + settings.highlightUserBlacklist.getValue().split("\n", QString::SkipEmptyParts); // TODO: This vector should only be rebuilt upon highlights being changed auto activeHighlights = settings.highlightProperties.getValue(); diff --git a/src/widgets/accountpopup.cpp b/src/widgets/accountpopup.cpp index 171c8b6b8..9c87f06ca 100644 --- a/src/widgets/accountpopup.cpp +++ b/src/widgets/accountpopup.cpp @@ -95,18 +95,18 @@ AccountPopupWidget::AccountPopupWidget(std::shared_ptr _channel) }); QObject::connect(this->ui->disableHighlights, &QPushButton::clicked, this, [=, &settings]() { - QString str = settings.highlightUserBlacklist.getnonConst(); + QString str = settings.highlightUserBlacklist; str.append(this->ui->lblUsername->text() + "\n"); - settings.highlightUserBlacklist.set(str); + settings.highlightUserBlacklist = str; this->ui->disableHighlights->hide(); this->ui->enableHighlights->show(); }); QObject::connect(this->ui->enableHighlights, &QPushButton::clicked, this, [=, &settings]() { - QString str = settings.highlightUserBlacklist.getnonConst(); + QString str = settings.highlightUserBlacklist; QStringList list = str.split("\n"); list.removeAll(this->ui->lblUsername->text()); - settings.highlightUserBlacklist.set(list.join("\n")); + settings.highlightUserBlacklist = list.join("\n"); this->ui->enableHighlights->hide(); this->ui->disableHighlights->show(); }); @@ -267,8 +267,7 @@ void AccountPopupWidget::showEvent(QShowEvent *) this->updateButtons(this->ui->ownerLayout, false); } - QString blacklisted = - singletons::SettingManager::getInstance().highlightUserBlacklist.getnonConst(); + QString blacklisted = singletons::SettingManager::getInstance().highlightUserBlacklist; QStringList list = blacklisted.split("\n", QString::SkipEmptyParts); if (list.contains(this->ui->lblUsername->text(), Qt::CaseInsensitive)) { this->ui->disableHighlights->hide(); diff --git a/src/widgets/accountpopup.hpp b/src/widgets/accountpopup.hpp index 7e65979df..ee34da7f5 100644 --- a/src/widgets/accountpopup.hpp +++ b/src/widgets/accountpopup.hpp @@ -1,8 +1,8 @@ #pragma once #include "basewidget.hpp" -#include "util/concurrentmap.hpp" #include "twitch/twitchchannel.hpp" +#include "util/concurrentmap.hpp" #include #include diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index fc4aa2b72..5032ed5bf 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -422,7 +422,7 @@ QVBoxLayout *SettingsDialog::createHighlightingTab() for (const auto &highlightProperty : highlightProperties) { highlights->addItem(highlightProperty.key); } - highlightUserBlacklist->setText(settings.highlightUserBlacklist.getnonConst()); + highlightUserBlacklist->setText(settings.highlightUserBlacklist); auto highlightTab = new QTabWidget(); auto customSound = new QHBoxLayout(); auto soundForm = new QFormLayout(); @@ -439,7 +439,7 @@ QVBoxLayout *SettingsDialog::createHighlightingTab() QObject::connect(selectBtn, &QPushButton::clicked, this, [&settings, this] { auto fileName = QFileDialog::getOpenFileName(this, tr("Open Sound"), "", tr("Audio Files (*.mp3 *.wav)")); - settings.pathHighlightSound.set(fileName); + settings.pathHighlightSound = fileName; }); customSound->addWidget(selectBtn); } @@ -617,11 +617,12 @@ QVBoxLayout *SettingsDialog::createHighlightingTab() QStringList list = highlightUserBlacklist->toPlainText().split("\n", QString::SkipEmptyParts); list.removeDuplicates(); - settings.highlightUserBlacklist.set(list.join("\n") + "\n"); + settings.highlightUserBlacklist = list.join("\n") + "\n"; }); - settings.highlightUserBlacklist.valueChanged.connect( - [=](const QString &str) { highlightUserBlacklist->setPlainText(str); }); + settings.highlightUserBlacklist.connect([=](const QString &str, auto) { + highlightUserBlacklist->setPlainText(str); // + }); return layout; } @@ -739,20 +740,6 @@ QVBoxLayout *SettingsDialog::createTabLayout() return layout; } -QCheckBox *SettingsDialog::createCheckbox(const QString &title, Setting &setting) -{ - auto checkbox = new QCheckBox(title); - - // Set checkbox initial state - checkbox->setChecked(setting.get()); - - QObject::connect(checkbox, &QCheckBox::toggled, this, [&setting](bool state) { - setting.set(state); // - }); - - return checkbox; -} - QCheckBox *SettingsDialog::createCheckbox(const QString &title, pajlada::Settings::Setting &setting) { diff --git a/src/widgets/settingsdialog.hpp b/src/widgets/settingsdialog.hpp index 2c1654798..2e1092f02 100644 --- a/src/widgets/settingsdialog.hpp +++ b/src/widgets/settingsdialog.hpp @@ -88,7 +88,6 @@ private: /// Widget creation helpers QVBoxLayout *createTabLayout(); - QCheckBox *createCheckbox(const QString &title, Setting &setting); QCheckBox *createCheckbox(const QString &title, pajlada::Settings::Setting &setting); QHBoxLayout *createCombobox(const QString &title, pajlada::Settings::Setting &setting, QStringList items,