From b54c68d74647d1e0ddf50beacbf224379ad46f8c Mon Sep 17 00:00:00 2001 From: datguy Date: Wed, 4 Jul 2018 15:38:16 +0300 Subject: [PATCH] Rework to use controllers --- chatterino.pro | 3 ++ .../highlights/HighlightController.cpp | 21 ++++++++++++ .../highlights/HighlightController.hpp | 5 +++ .../highlights/userhighlightmodel.cpp | 33 +++++++++++++++++++ src/providers/twitch/TwitchMessageBuilder.cpp | 11 +++++-- .../settingspages/HighlightingPage.cpp | 24 ++++++++++++++ 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/controllers/highlights/userhighlightmodel.cpp diff --git a/chatterino.pro b/chatterino.pro index bee2ef085..3c6d6b2d5 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -113,6 +113,7 @@ SOURCES += \ src/controllers/highlights/HighlightController.cpp \ src/controllers/highlights/HighlightModel.cpp \ src/controllers/highlights/HighlightBlacklistModel.cpp \ + src/controllers/highlights/UserHighlightModel.cpp \ src/controllers/ignores/IgnoreController.cpp \ src/controllers/ignores/IgnoreModel.cpp \ src/controllers/taggedusers/TaggedUser.cpp \ @@ -258,6 +259,8 @@ HEADERS += \ src/controllers/highlights/HighlightBlacklistModel.hpp \ src/controllers/highlights/HighlightPhrase.hpp \ src/controllers/highlights/HighlightBlacklistUser.hpp \ + src/controllers/highlights/UserHighlightModel.hpp \ + src/controllers/highlights/UserHighlight.hpp \ src/controllers/ignores/IgnoreController.hpp \ src/controllers/ignores/IgnoreModel.hpp \ src/controllers/ignores/IgnorePhrase.hpp \ diff --git a/src/controllers/highlights/HighlightController.cpp b/src/controllers/highlights/HighlightController.cpp index 2e49dd34d..d9ac63fb6 100644 --- a/src/controllers/highlights/HighlightController.cpp +++ b/src/controllers/highlights/HighlightController.cpp @@ -3,6 +3,7 @@ #include "Application.hpp" #include "controllers/highlights/HighlightBlacklistModel.hpp" #include "controllers/highlights/HighlightModel.hpp" +#include "controllers/highlights/UserHighlightModel.hpp" #include "widgets/dialogs/NotificationPopup.hpp" namespace chatterino { @@ -33,6 +34,26 @@ HighlightModel *HighlightController::createModel(QObject *parent) return model; } +UserHighlightModel *HighlightController::createUserModel(QObject *parent) +{ + auto *model = new UserHighlightModel(parent); + model->init(&this->highlightedUsers); + + return model; +} + +bool HighlightController::userContains(const QString &username) +{ + std::vector userItems = this->highlightedUsers.getVector(); + for (const auto &highlightedUser : userItems) { + if (highlightedUser.isMatch(username)) { + return true; + } + } + + return false; +} + HighlightBlacklistModel *HighlightController::createBlacklistModel(QObject *parent) { auto *model = new HighlightBlacklistModel(parent); diff --git a/src/controllers/highlights/HighlightController.hpp b/src/controllers/highlights/HighlightController.hpp index 916641455..6787ba3ba 100644 --- a/src/controllers/highlights/HighlightController.hpp +++ b/src/controllers/highlights/HighlightController.hpp @@ -3,11 +3,13 @@ #include "common/SignalVector.hpp" #include "controllers/highlights/HighlightBlacklistUser.hpp" #include "controllers/highlights/HighlightPhrase.hpp" +#include "controllers/highlights/UserHighlight.hpp" #include "messages/Message.hpp" #include "singletons/Settings.hpp" namespace chatterino { +class UserHighlightModel; class HighlightModel; class HighlightBlacklistModel; @@ -20,10 +22,13 @@ public: UnsortedSignalVector phrases; UnsortedSignalVector blacklistedUsers; + UnsortedSignalVector highlightedUsers; HighlightModel *createModel(QObject *parent); HighlightBlacklistModel *createBlacklistModel(QObject *parent); + UserHighlightModel *createUserModel(QObject *parent); + bool userContains(const QString &username); bool blacklistContains(const QString &username); void addHighlight(const MessagePtr &msg); diff --git a/src/controllers/highlights/userhighlightmodel.cpp b/src/controllers/highlights/userhighlightmodel.cpp new file mode 100644 index 000000000..50592d1c2 --- /dev/null +++ b/src/controllers/highlights/userhighlightmodel.cpp @@ -0,0 +1,33 @@ +#include "UserHighlightModel.hpp" + +#include "Application.hpp" +#include "singletons/Settings.hpp" +#include "util/StandardItemHelper.hpp" + +namespace chatterino { + +// commandmodel +UserHighlightModel::UserHighlightModel(QObject *parent) + : SignalVectorModel(2, parent) +{ +} + +// turn vector item into model row +UserHighlight UserHighlightModel::getItemFromRow(std::vector &row, + const UserHighlight &original) +{ + // key, regex + + return UserHighlight{row[0]->data(Qt::DisplayRole).toString(), + row[1]->data(Qt::CheckStateRole).toBool()}; +} + +// row into vector item +void UserHighlightModel::getRowFromItem(const UserHighlight &item, + std::vector &row) +{ + setStringItem(row[0], item.getPattern()); + setBoolItem(row[1], item.isRegex()); +} + +} // namespace chatterino diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 4c1997cfe..a4ccd06cf 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -458,9 +458,14 @@ void TwitchMessageBuilder::parseHighlights() if (!app->highlights->blacklistContains(this->ircMessage->nick())) { for (const HighlightPhrase &highlight : activeHighlights) { - if (highlight.isMatch(this->originalMessage)) { - Log("Highlight because {} matches {}", this->originalMessage, - highlight.getPattern()); + if (highlight.isMatch(this->originalMessage) || + app->highlights->userContains(this->ircMessage->nick())) { + if (app->highlights->userContains(this->ircMessage->nick())) { + Log("Highlight because user {} sent a message", this->ircMessage->nick()); + } else { + Log("Highlight because {} matches {}", this->originalMessage, + highlight.getPattern()); + } doHighlight = true; if (highlight.getAlert()) { diff --git a/src/widgets/settingspages/HighlightingPage.cpp b/src/widgets/settingspages/HighlightingPage.cpp index 7c8886531..06e1daa1f 100644 --- a/src/widgets/settingspages/HighlightingPage.cpp +++ b/src/widgets/settingspages/HighlightingPage.cpp @@ -4,6 +4,7 @@ #include "controllers/highlights/HighlightBlacklistModel.hpp" #include "controllers/highlights/HighlightController.hpp" #include "controllers/highlights/HighlightModel.hpp" +#include "controllers/highlights/UserHighlightModel.hpp" #include "debug/Log.hpp" #include "singletons/Settings.hpp" #include "util/LayoutCreator.hpp" @@ -88,6 +89,29 @@ HighlightingPage::HighlightingPage() HighlightBlacklistUser{"blacklisted user", false}); }); } + + auto pingUsers = tabs.appendTab(new QVBoxLayout, "Highlight on message"); + { + EditableModelView *view = + pingUsers.emplace(app->highlights->createUserModel(nullptr)) + .getElement(); + + view->setTitles({"Username", "Regex"}); + view->getTableView()->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed); + view->getTableView()->horizontalHeader()->setSectionResizeMode( + 0, QHeaderView::Stretch); + + // fourtf: make class extrend BaseWidget and add this to dpiChanged + QTimer::singleShot(1, [view] { + view->getTableView()->resizeColumnsToContents(); + view->getTableView()->setColumnWidth(0, 200); + }); + + view->addButtonPressed.connect([] { + getApp()->highlights->highlightedUsers.appendItem( + UserHighlight{"highlighted user", false}); + }); + } } // MISC