refactored HighlightController

This commit is contained in:
fourtf 2020-02-23 19:31:43 +01:00
parent 4a5dc80bc6
commit 843e2ad994
17 changed files with 34 additions and 100 deletions

View file

@ -25,7 +25,7 @@ public:
} }
} }
void init(BaseSignalVector<TVectorItem> *vec) void initialize(BaseSignalVector<TVectorItem> *vec)
{ {
this->vector_ = vec; this->vector_ = vec;
@ -89,6 +89,13 @@ public:
this->afterInit(); this->afterInit();
} }
SignalVectorModel<TVectorItem> *initialized(
BaseSignalVector<TVectorItem> *vec)
{
this->initialize(vec);
return this;
}
virtual ~SignalVectorModel() virtual ~SignalVectorModel()
{ {
for (Row &row : this->rows_) for (Row &row : this->rows_)
@ -272,8 +279,8 @@ public:
int from = data->data("chatterino_row_id").toInt(); int from = data->data("chatterino_row_id").toInt();
int to = parent.row(); int to = parent.row();
if (from < 0 || from > this->vector_->raw().size() || if (from < 0 || from > this->vector_->raw().size() || to < 0 ||
to < 0 || to > this->vector_->raw().size()) to > this->vector_->raw().size())
{ {
return false; return false;
} }

View file

@ -50,7 +50,7 @@ AccountModel *AccountController::createModel(QObject *parent)
{ {
AccountModel *model = new AccountModel(parent); AccountModel *model = new AccountModel(parent);
model->init(&this->accounts_); model->initialize(&this->accounts_);
return model; return model;
} }

View file

@ -234,7 +234,7 @@ void CommandController::save()
CommandModel *CommandController::createModel(QObject *parent) CommandModel *CommandController::createModel(QObject *parent)
{ {
CommandModel *model = new CommandModel(parent); CommandModel *model = new CommandModel(parent);
model->init(&this->items_); model->initialize(&this->items_);
return model; return model;
} }
@ -245,8 +245,6 @@ QString CommandController::execCommand(const QString &textNoEmoji,
QString text = getApp()->emotes->emojis.replaceShortCodes(textNoEmoji); QString text = getApp()->emotes->emojis.replaceShortCodes(textNoEmoji);
QStringList words = text.split(' ', QString::SkipEmptyParts); QStringList words = text.split(' ', QString::SkipEmptyParts);
std::lock_guard<std::mutex> lock(this->mutex_);
if (words.length() == 0) if (words.length() == 0)
{ {
return text; return text;

View file

@ -39,8 +39,6 @@ private:
QMap<QString, Command> commandsMap_; QMap<QString, Command> commandsMap_;
int maxSpaces_ = 0; int maxSpaces_ = 0;
std::mutex mutex_;
std::shared_ptr<pajlada::Settings::SettingManager> sm_; std::shared_ptr<pajlada::Settings::SettingManager> sm_;
// Because the setting manager is not initialized until the initialize // Because the setting manager is not initialized until the initialize
// function is called (and not in the constructor), we have to // function is called (and not in the constructor), we have to

View file

@ -11,9 +11,9 @@ class HighlightController;
class HighlightBlacklistModel : public SignalVectorModel<HighlightBlacklistUser> class HighlightBlacklistModel : public SignalVectorModel<HighlightBlacklistUser>
{ {
public:
explicit HighlightBlacklistModel(QObject *parent); explicit HighlightBlacklistModel(QObject *parent);
public:
enum Column { enum Column {
Pattern = 0, Pattern = 0,
UseRegex = 1, UseRegex = 1,
@ -28,8 +28,6 @@ protected:
// turns a row in the model into a vector item // turns a row in the model into a vector item
virtual void getRowFromItem(const HighlightBlacklistUser &item, virtual void getRowFromItem(const HighlightBlacklistUser &item,
std::vector<QStandardItem *> &row) override; std::vector<QStandardItem *> &row) override;
friend class HighlightController;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -1,17 +1,10 @@
#include "HighlightController.hpp" #include "HighlightController.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "controllers/highlights/HighlightBlacklistModel.hpp" #include "common/ChatterinoSetting.hpp"
#include "controllers/highlights/HighlightModel.hpp"
#include "controllers/highlights/UserHighlightModel.hpp"
#include "widgets/dialogs/NotificationPopup.hpp"
namespace chatterino { namespace chatterino {
HighlightController::HighlightController()
{
}
template <typename T> template <typename T>
inline void persist(SignalVector<T> &vec, const std::string &name) inline void persist(SignalVector<T> &vec, const std::string &name)
{ {
@ -24,7 +17,7 @@ inline void persist(SignalVector<T> &vec, const std::string &name)
setting->setValue(vec->raw()); setting->setValue(vec->raw());
}); });
// TODO // TODO: Delete when appropriate.
setting.release(); setting.release();
} }
@ -38,65 +31,26 @@ void HighlightController::initialize(Settings &settings, Paths &paths)
persist(this->highlightedUsers, "/highlighting/users"); persist(this->highlightedUsers, "/highlighting/users");
} }
HighlightModel *HighlightController::createModel(QObject *parent)
{
HighlightModel *model = new HighlightModel(parent);
model->init(&this->phrases);
return model;
}
UserHighlightModel *HighlightController::createUserModel(QObject *parent)
{
auto *model = new UserHighlightModel(parent);
model->init(&this->highlightedUsers);
return model;
}
bool HighlightController::isHighlightedUser(const QString &username) bool HighlightController::isHighlightedUser(const QString &username)
{ {
const auto &userItems = this->highlightedUsers; for (const auto &highlightedUser : this->highlightedUsers)
for (const auto &highlightedUser : userItems)
{ {
if (highlightedUser.isMatch(username)) if (highlightedUser.isMatch(username))
{
return true; return true;
} }
}
return false; return false;
} }
HighlightBlacklistModel *HighlightController::createBlacklistModel(
QObject *parent)
{
auto *model = new HighlightBlacklistModel(parent);
model->init(&this->blacklistedUsers);
return model;
}
bool HighlightController::blacklistContains(const QString &username) bool HighlightController::blacklistContains(const QString &username)
{ {
for (const auto &blacklistedUser : *this->blacklistedUsers.readOnly()) for (const auto &blacklistedUser : *this->blacklistedUsers.readOnly())
{ {
if (blacklistedUser.isMatch(username)) if (blacklistedUser.isMatch(username))
{
return true; return true;
} }
}
return false; return false;
} }
void HighlightController::addHighlight(const MessagePtr &msg)
{
// static NotificationPopup popup;
// popup.updatePosition();
// popup.addMessage(msg);
// popup.show();
}
} // namespace chatterino } // namespace chatterino

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
#include "common/ChatterinoSetting.hpp"
#include "common/SignalVector.hpp" #include "common/SignalVector.hpp"
#include "common/Singleton.hpp" #include "common/Singleton.hpp"
#include "controllers/highlights/HighlightBlacklistUser.hpp" #include "controllers/highlights/HighlightBlacklistUser.hpp"
@ -8,36 +7,18 @@
namespace chatterino { namespace chatterino {
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
class Settings;
class Paths;
class UserHighlightModel;
class HighlightModel;
class HighlightBlacklistModel;
class HighlightController final : public Singleton class HighlightController final : public Singleton
{ {
public: public:
HighlightController();
virtual void initialize(Settings &settings, Paths &paths) override; virtual void initialize(Settings &settings, Paths &paths) override;
UnsortedSignalVector<HighlightPhrase> phrases; UnsortedSignalVector<HighlightPhrase> phrases;
UnsortedSignalVector<HighlightBlacklistUser> blacklistedUsers; UnsortedSignalVector<HighlightBlacklistUser> blacklistedUsers;
UnsortedSignalVector<HighlightPhrase> highlightedUsers; UnsortedSignalVector<HighlightPhrase> highlightedUsers;
HighlightModel *createModel(QObject *parent);
HighlightBlacklistModel *createBlacklistModel(QObject *parent);
UserHighlightModel *createUserModel(QObject *parent);
bool isHighlightedUser(const QString &username); bool isHighlightedUser(const QString &username);
bool blacklistContains(const QString &username); bool blacklistContains(const QString &username);
void addHighlight(const MessagePtr &msg);
private: private:
bool initialized_ = false; bool initialized_ = false;
}; };

View file

@ -7,13 +7,11 @@
namespace chatterino { namespace chatterino {
class HighlightController;
class HighlightModel : public SignalVectorModel<HighlightPhrase> class HighlightModel : public SignalVectorModel<HighlightPhrase>
{ {
public:
explicit HighlightModel(QObject *parent); explicit HighlightModel(QObject *parent);
public:
// Used here, in HighlightingPage and in UserHighlightModel // Used here, in HighlightingPage and in UserHighlightModel
enum Column { enum Column {
Pattern = 0, Pattern = 0,
@ -40,8 +38,6 @@ protected:
virtual void customRowSetData(const std::vector<QStandardItem *> &row, virtual void customRowSetData(const std::vector<QStandardItem *> &row,
int column, const QVariant &value, int role, int column, const QVariant &value, int role,
int rowIndex) override; int rowIndex) override;
friend class HighlightController;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -11,6 +11,7 @@ class HighlightController;
class UserHighlightModel : public SignalVectorModel<HighlightPhrase> class UserHighlightModel : public SignalVectorModel<HighlightPhrase>
{ {
public:
explicit UserHighlightModel(QObject *parent); explicit UserHighlightModel(QObject *parent);
protected: protected:
@ -21,8 +22,6 @@ protected:
virtual void getRowFromItem(const HighlightPhrase &item, virtual void getRowFromItem(const HighlightPhrase &item,
std::vector<QStandardItem *> &row) override; std::vector<QStandardItem *> &row) override;
friend class HighlightController;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -25,7 +25,7 @@ void IgnoreController::initialize(Settings &, Paths &)
IgnoreModel *IgnoreController::createModel(QObject *parent) IgnoreModel *IgnoreController::createModel(QObject *parent)
{ {
IgnoreModel *model = new IgnoreModel(parent); IgnoreModel *model = new IgnoreModel(parent);
model->init(&this->phrases); model->initialize(&this->phrases);
return model; return model;
} }

View file

@ -34,7 +34,7 @@ void ModerationActions::initialize(Settings &settings, Paths &paths)
ModerationActionModel *ModerationActions::createModel(QObject *parent) ModerationActionModel *ModerationActions::createModel(QObject *parent)
{ {
ModerationActionModel *model = new ModerationActionModel(parent); ModerationActionModel *model = new ModerationActionModel(parent);
model->init(&this->items); model->initialize(&this->items);
return model; return model;
} }

View file

@ -121,7 +121,7 @@ NotificationModel *NotificationController::createModel(QObject *parent,
Platform p) Platform p)
{ {
NotificationModel *model = new NotificationModel(parent); NotificationModel *model = new NotificationModel(parent);
model->init(&this->channelMap[p]); model->initialize(&this->channelMap[p]);
return model; return model;
} }

View file

@ -19,7 +19,7 @@ void PingController::initialize(Settings &settings, Paths &paths)
PingModel *PingController::createModel(QObject *parent) PingModel *PingController::createModel(QObject *parent)
{ {
PingModel *model = new PingModel(parent); PingModel *model = new PingModel(parent);
model->init(&this->channelVector); model->initialize(&this->channelVector);
return model; return model;
} }

View file

@ -11,7 +11,7 @@ TaggedUsersController::TaggedUsersController()
TaggedUsersModel *TaggedUsersController::createModel(QObject *parent) TaggedUsersModel *TaggedUsersController::createModel(QObject *parent)
{ {
TaggedUsersModel *model = new TaggedUsersModel(parent); TaggedUsersModel *model = new TaggedUsersModel(parent);
model->init(&this->users); model->initialize(&this->users);
return model; return model;
} }

View file

@ -143,7 +143,7 @@ Irc::Irc()
QAbstractTableModel *Irc::newConnectionModel(QObject *parent) QAbstractTableModel *Irc::newConnectionModel(QObject *parent)
{ {
auto model = new Model(parent); auto model = new Model(parent);
model->init(&this->connections); model->initialize(&this->connections);
return model; return model;
} }

View file

@ -241,7 +241,6 @@ void IrcMessageHandler::addMessage(Communi::IrcMessage *_message,
if (highlighted) if (highlighted)
{ {
server.mentionsChannel->addMessage(msg); server.mentionsChannel->addMessage(msg);
getApp()->highlights->addHighlight(msg);
} }
} }

View file

@ -49,12 +49,12 @@ HighlightingPage::HighlightingPage()
"Play notification sounds and highlight messages based on " "Play notification sounds and highlight messages based on "
"certain patterns."); "certain patterns.");
EditableModelView *view = auto view =
highlights highlights
.emplace<EditableModelView>( .emplace<EditableModelView>(
app->highlights->createModel(nullptr)) (new HighlightModel(nullptr))
->initialized(&app->highlights->phrases))
.getElement(); .getElement();
view->addRegexHelpLink(); view->addRegexHelpLink();
view->setTitles({"Pattern", "Flash\ntaskbar", "Play\nsound", view->setTitles({"Pattern", "Flash\ntaskbar", "Play\nsound",
"Enable\nregex", "Case-\nsensitive", "Enable\nregex", "Case-\nsensitive",
@ -94,7 +94,9 @@ HighlightingPage::HighlightingPage()
EditableModelView *view = EditableModelView *view =
pingUsers pingUsers
.emplace<EditableModelView>( .emplace<EditableModelView>(
app->highlights->createUserModel(nullptr)) (new UserHighlightModel(nullptr))
->initialized(
&app->highlights->highlightedUsers))
.getElement(); .getElement();
view->addRegexHelpLink(); view->addRegexHelpLink();
@ -140,7 +142,9 @@ HighlightingPage::HighlightingPage()
EditableModelView *view = EditableModelView *view =
disabledUsers disabledUsers
.emplace<EditableModelView>( .emplace<EditableModelView>(
app->highlights->createBlacklistModel(nullptr)) (new HighlightBlacklistModel(nullptr))
->initialized(
&app->highlights->blacklistedUsers))
.getElement(); .getElement();
view->addRegexHelpLink(); view->addRegexHelpLink();
@ -205,7 +209,7 @@ HighlightingPage::HighlightingPage()
// ---- misc // ---- misc
this->disabledUsersChangedTimer_.setSingleShot(true); this->disabledUsersChangedTimer_.setSingleShot(true);
} } // namespace chatterino
void HighlightingPage::tableCellClicked(const QModelIndex &clicked, void HighlightingPage::tableCellClicked(const QModelIndex &clicked,
EditableModelView *view) EditableModelView *view)