From 4a5dc80bc6cd27194f7f9e8a0c508cdc5ba106cf Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 23 Feb 2020 17:45:59 +0100 Subject: [PATCH] some more refactoring --- src/common/SignalVector.hpp | 39 ++++++++-------- src/common/SignalVectorModel.hpp | 8 ++-- .../accounts/AccountController.cpp | 2 +- .../commands/CommandController.cpp | 2 +- .../highlights/HighlightController.cpp | 46 ++++++++----------- .../highlights/HighlightController.hpp | 7 --- src/controllers/ignores/IgnoreController.cpp | 2 +- .../moderationactions/ModerationActions.cpp | 2 +- .../notifications/NotificationController.cpp | 12 ++--- src/controllers/pings/PingController.cpp | 6 +-- src/widgets/dialogs/SelectChannelDialog.cpp | 7 ++- src/widgets/dialogs/UserInfoPopup.cpp | 4 +- 12 files changed, 62 insertions(+), 75 deletions(-) diff --git a/src/common/SignalVector.hpp b/src/common/SignalVector.hpp index 95006011..bf8cbfc5 100644 --- a/src/common/SignalVector.hpp +++ b/src/common/SignalVector.hpp @@ -115,30 +115,33 @@ public: this->itemsChanged_(); } - // compatability - [[deprecated]] int insertItem(const T &item, int proposedIndex = -1, - void *caller = nullptr) - { - return this->insert(item, proposedIndex, caller); - } - - [[deprecated]] int appendItem(const T &item, void *caller = nullptr) - { - return this->append(item, caller); - } - - [[deprecated]] void removeItem(int index, void *caller = nullptr) - { - this->removeAt(index, caller); - } - - [[deprecated]] const std::vector &getVector() const + const std::vector &raw() const { assertInGuiThread(); return this->items_; } + // compatability + [[deprecated("use insert")]] int insertItem(const T &item, + int proposedIndex = -1, + void *caller = nullptr) + { + return this->insert(item, proposedIndex, caller); + } + + [[deprecated("use append")]] int appendItem(const T &item, + void *caller = nullptr) + { + return this->append(item, caller); + } + + [[deprecated("use removeAt")]] void removeItem(int index, + void *caller = nullptr) + { + this->removeAt(index, caller); + } + [[deprecated]] std::vector cloneVector() { return *this->readOnly(); diff --git a/src/common/SignalVectorModel.hpp b/src/common/SignalVectorModel.hpp index bb7cd7c0..4fe39edd 100644 --- a/src/common/SignalVectorModel.hpp +++ b/src/common/SignalVectorModel.hpp @@ -52,7 +52,7 @@ public: }; int i = 0; - for (const TVectorItem &item : vec->getVector()) + for (const TVectorItem &item : vec->raw()) { SignalVectorItemArgs args{item, i++, 0}; @@ -272,15 +272,15 @@ public: int from = data->data("chatterino_row_id").toInt(); int to = parent.row(); - if (from < 0 || from > this->vector_->getVector().size() || - to < 0 || to > this->vector_->getVector().size()) + if (from < 0 || from > this->vector_->raw().size() || + to < 0 || to > this->vector_->raw().size()) { return false; } if (from != to) { - auto item = this->vector_->getVector()[from]; + auto item = this->vector_->raw()[from]; this->vector_->removeItem(from); this->vector_->insertItem(item, to); } diff --git a/src/controllers/accounts/AccountController.cpp b/src/controllers/accounts/AccountController.cpp index bd32401a..db65e944 100644 --- a/src/controllers/accounts/AccountController.cpp +++ b/src/controllers/accounts/AccountController.cpp @@ -16,7 +16,7 @@ AccountController::AccountController() this->twitch.accounts.itemRemoved.connect([this](const auto &args) { if (args.caller != this) { - auto &accs = this->twitch.accounts.getVector(); + auto &accs = this->twitch.accounts.raw(); auto it = std::find(accs.begin(), accs.end(), args.item); assert(it != accs.end()); diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index a6150cfa..75464bbb 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -212,7 +212,7 @@ void CommandController::initialize(Settings &, Paths &paths) // Update the setting when the vector of commands has been updated (most // likely from the settings dialog) this->items_.delayedItemsChanged.connect([this] { // - this->commandsSetting_->setValue(this->items_.getVector()); + this->commandsSetting_->setValue(this->items_.raw()); }); // Load commands from commands.json diff --git a/src/controllers/highlights/HighlightController.cpp b/src/controllers/highlights/HighlightController.cpp index a59218f3..60df26ff 100644 --- a/src/controllers/highlights/HighlightController.cpp +++ b/src/controllers/highlights/HighlightController.cpp @@ -12,38 +12,30 @@ HighlightController::HighlightController() { } +template +inline void persist(SignalVector &vec, const std::string &name) +{ + auto setting = std::make_unique>>(name); + + for (auto &&item : setting->getValue()) + vec.append(item); + + vec.delayedItemsChanged.connect([setting = setting.get(), vec = &vec] { + setting->setValue(vec->raw()); + }); + + // TODO + setting.release(); +} + void HighlightController::initialize(Settings &settings, Paths &paths) { assert(!this->initialized_); this->initialized_ = true; - for (const HighlightPhrase &phrase : this->highlightsSetting_.getValue()) - { - this->phrases.appendItem(phrase); - } - - this->phrases.delayedItemsChanged.connect([this] { // - this->highlightsSetting_.setValue(this->phrases.getVector()); - }); - - for (const HighlightBlacklistUser &blacklistedUser : - this->blacklistSetting_.getValue()) - { - this->blacklistedUsers.appendItem(blacklistedUser); - } - - this->blacklistedUsers.delayedItemsChanged.connect([this] { - this->blacklistSetting_.setValue(this->blacklistedUsers.getVector()); - }); - - for (const HighlightPhrase &user : this->userSetting_.getValue()) - { - this->highlightedUsers.appendItem(user); - } - - this->highlightedUsers.delayedItemsChanged.connect([this] { // - this->userSetting_.setValue(this->highlightedUsers.getVector()); - }); + persist(this->phrases, "/highlighting/highlights"); + persist(this->blacklistedUsers, "/highlighting/blacklist"); + persist(this->highlightedUsers, "/highlighting/users"); } HighlightModel *HighlightController::createModel(QObject *parent) diff --git a/src/controllers/highlights/HighlightController.hpp b/src/controllers/highlights/HighlightController.hpp index faa4683c..2f61b1a4 100644 --- a/src/controllers/highlights/HighlightController.hpp +++ b/src/controllers/highlights/HighlightController.hpp @@ -40,13 +40,6 @@ public: private: bool initialized_ = false; - - ChatterinoSetting> highlightsSetting_ = { - "/highlighting/highlights"}; - ChatterinoSetting> blacklistSetting_ = { - "/highlighting/blacklist"}; - ChatterinoSetting> userSetting_ = { - "/highlighting/users"}; }; } // namespace chatterino diff --git a/src/controllers/ignores/IgnoreController.cpp b/src/controllers/ignores/IgnoreController.cpp index 5548ee5d..b1028be0 100644 --- a/src/controllers/ignores/IgnoreController.cpp +++ b/src/controllers/ignores/IgnoreController.cpp @@ -18,7 +18,7 @@ void IgnoreController::initialize(Settings &, Paths &) } this->phrases.delayedItemsChanged.connect([this] { // - this->ignoresSetting_.setValue(this->phrases.getVector()); + this->ignoresSetting_.setValue(this->phrases.raw()); }); } diff --git a/src/controllers/moderationactions/ModerationActions.cpp b/src/controllers/moderationactions/ModerationActions.cpp index 0104c7de..cb68174b 100644 --- a/src/controllers/moderationactions/ModerationActions.cpp +++ b/src/controllers/moderationactions/ModerationActions.cpp @@ -27,7 +27,7 @@ void ModerationActions::initialize(Settings &settings, Paths &paths) } this->items.delayedItemsChanged.connect([this] { // - this->setting_->setValue(this->items.getVector()); + this->setting_->setValue(this->items.raw()); }); } diff --git a/src/controllers/notifications/NotificationController.cpp b/src/controllers/notifications/NotificationController.cpp index b9777c80..ec2f231c 100644 --- a/src/controllers/notifications/NotificationController.cpp +++ b/src/controllers/notifications/NotificationController.cpp @@ -31,7 +31,7 @@ void NotificationController::initialize(Settings &settings, Paths &paths) this->channelMap[Platform::Twitch].delayedItemsChanged.connect([this] { // this->twitchSetting_.setValue( - this->channelMap[Platform::Twitch].getVector()); + this->channelMap[Platform::Twitch].raw()); }); /* for (const QString &channelName : this->mixerSetting_.getValue()) { @@ -88,9 +88,9 @@ void NotificationController::removeChannelNotification( const QString &channelName, Platform p) { for (std::vector::size_type i = 0; - i != channelMap[p].getVector().size(); i++) + i != channelMap[p].raw().size(); i++) { - if (channelMap[p].getVector()[i].toLower() == channelName.toLower()) + if (channelMap[p].raw()[i].toLower() == channelName.toLower()) { channelMap[p].removeItem(i); i--; @@ -128,14 +128,14 @@ NotificationModel *NotificationController::createModel(QObject *parent, void NotificationController::fetchFakeChannels() { for (std::vector::size_type i = 0; - i != channelMap[Platform::Twitch].getVector().size(); i++) + i != channelMap[Platform::Twitch].raw().size(); i++) { auto chan = getApp()->twitch.server->getChannelOrEmpty( - channelMap[Platform::Twitch].getVector()[i]); + channelMap[Platform::Twitch].raw()[i]); if (chan->isEmpty()) { getFakeTwitchChannelLiveStatus( - channelMap[Platform::Twitch].getVector()[i]); + channelMap[Platform::Twitch].raw()[i]); } } } diff --git a/src/controllers/pings/PingController.cpp b/src/controllers/pings/PingController.cpp index b61d6e79..67bd3ad6 100644 --- a/src/controllers/pings/PingController.cpp +++ b/src/controllers/pings/PingController.cpp @@ -12,7 +12,7 @@ void PingController::initialize(Settings &settings, Paths &paths) } this->channelVector.delayedItemsChanged.connect([this] { // - this->pingSetting_.setValue(this->channelVector.getVector()); + this->pingSetting_.setValue(this->channelVector.raw()); }); } @@ -43,9 +43,9 @@ void PingController::muteChannel(const QString &channelName) void PingController::unmuteChannel(const QString &channelName) { for (std::vector::size_type i = 0; - i != channelVector.getVector().size(); i++) + i != channelVector.raw().size(); i++) { - if (channelVector.getVector()[i].toLower() == channelName.toLower()) + if (channelVector.raw()[i].toLower() == channelName.toLower()) { channelVector.removeItem(i); i--; diff --git a/src/widgets/dialogs/SelectChannelDialog.cpp b/src/widgets/dialogs/SelectChannelDialog.cpp index 25d61ff0..c8e21a18 100644 --- a/src/widgets/dialogs/SelectChannelDialog.cpp +++ b/src/widgets/dialogs/SelectChannelDialog.cpp @@ -160,13 +160,12 @@ SelectChannelDialog::SelectChannelDialog(QWidget *parent) view->getTableView(), &QTableView::doubleClicked, [](const QModelIndex &index) { auto editor = new IrcConnectionEditor( - Irc::instance() - .connections.getVector()[size_t(index.row())]); + Irc::instance().connections.raw()[size_t(index.row())]); if (editor->exec() == QDialog::Accepted) { auto data = editor->data(); - auto &&conns = Irc::instance().connections.getVector(); + auto &&conns = Irc::instance().connections.raw(); int i = 0; for (auto &&conn : conns) { @@ -348,7 +347,7 @@ IndirectChannel SelectChannelDialog::getSelectedChannel() const ->currentIndex() .row(); - auto &&vector = Irc::instance().connections.getVector(); + auto &&vector = Irc::instance().connections.raw(); if (row >= 0 && row < int(vector.size())) { diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index e530a5bf..bb75a61f 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -343,7 +343,7 @@ void UserInfoPopup::installEvents() else { const auto &vector = - getApp()->highlights->blacklistedUsers.getVector(); + getApp()->highlights->blacklistedUsers.raw(); for (int i = 0; i < vector.size(); i++) { @@ -456,7 +456,7 @@ void UserInfoPopup::updateUserData() // get ignoreHighlights state bool isIgnoringHighlights = false; - const auto &vector = getApp()->highlights->blacklistedUsers.getVector(); + const auto &vector = getApp()->highlights->blacklistedUsers.raw(); for (int i = 0; i < vector.size(); i++) { if (this->userName_ == vector[i].getPattern())