added regex highlights
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#include "account.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
Account::Account(const QString &category)
|
||||
{
|
||||
}
|
||||
|
||||
const QString &Account::getCategory() const
|
||||
{
|
||||
return this->category;
|
||||
}
|
||||
|
||||
bool Account::operator<(const Account &other) const
|
||||
{
|
||||
if (this->category < other.category) {
|
||||
return true;
|
||||
} else if (this->category == other.category) {
|
||||
if (this->toString() < other.toString()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace accounts
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
class Account
|
||||
{
|
||||
public:
|
||||
Account(const QString &category);
|
||||
|
||||
virtual QString toString() const = 0;
|
||||
const QString &getCategory() const;
|
||||
|
||||
bool operator<(const Account &other) const;
|
||||
|
||||
private:
|
||||
QString category;
|
||||
};
|
||||
|
||||
} // namespace accounts
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "accountcontroller.hpp"
|
||||
|
||||
#include "controllers/accounts/accountmodel.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
AccountController::AccountController()
|
||||
{
|
||||
}
|
||||
|
||||
AccountModel *AccountController::createModel(QObject *parent)
|
||||
{
|
||||
AccountModel *model = new AccountModel(parent);
|
||||
|
||||
//(util::BaseSignalVector<stdAccount> *)
|
||||
model->init(&this->accounts);
|
||||
return model;
|
||||
}
|
||||
|
||||
} // namespace accounts
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "controllers/accounts/account.hpp"
|
||||
#include "util/sharedptrelementless.hpp"
|
||||
#include "util/signalvector2.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
class AccountModel;
|
||||
|
||||
class AccountController
|
||||
{
|
||||
public:
|
||||
AccountController();
|
||||
|
||||
AccountModel *createModel(QObject *parent);
|
||||
|
||||
private:
|
||||
util::SortedSignalVector<std::shared_ptr<Account>, util::SharedPtrElementLess<Account>>
|
||||
accounts;
|
||||
};
|
||||
|
||||
} // namespace accounts
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "accountmodel.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
AccountModel::AccountModel(QObject *parent)
|
||||
: util::SignalVectorModel<std::shared_ptr<Account>>(1, parent)
|
||||
{
|
||||
}
|
||||
|
||||
// turn a vector item into a model row
|
||||
std::shared_ptr<Account> AccountModel::getItemFromRow(std::vector<QStandardItem *> &row)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// turns a row in the model into a vector item
|
||||
void AccountModel::getRowFromItem(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row)
|
||||
{
|
||||
row[0]->setData(item->toString(), Qt::DisplayRole);
|
||||
}
|
||||
|
||||
} // namespace accounts
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma
|
||||
|
||||
#include "controllers/accounts/account.hpp"
|
||||
#include "util/signalvectormodel.hpp"
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace accounts {
|
||||
|
||||
class AccountController;
|
||||
|
||||
class AccountModel : public util::SignalVectorModel<std::shared_ptr<Account>>
|
||||
{
|
||||
public:
|
||||
AccountModel(QObject *parent);
|
||||
|
||||
protected:
|
||||
// turn a vector item into a model row
|
||||
virtual std::shared_ptr<Account> getItemFromRow(std::vector<QStandardItem *> &row) override;
|
||||
|
||||
// turns a row in the model into a vector item
|
||||
virtual void getRowFromItem(const std::shared_ptr<Account> &item,
|
||||
std::vector<QStandardItem *> &row) override;
|
||||
|
||||
friend class AccountController;
|
||||
};
|
||||
|
||||
} // namespace accounts
|
||||
} // namespace controllers
|
||||
} // namespace chatterino
|
||||
@@ -45,10 +45,10 @@ HighlightPhrase HighlightModel::getItemFromRow(std::vector<QStandardItem *> &row
|
||||
// turns a row in the model into a vector item
|
||||
void HighlightModel::getRowFromItem(const HighlightPhrase &item, std::vector<QStandardItem *> &row)
|
||||
{
|
||||
util::setStringItem(row[0], item.key);
|
||||
util::setBoolItem(row[1], item.alert);
|
||||
util::setBoolItem(row[2], item.sound);
|
||||
util::setBoolItem(row[3], item.regex);
|
||||
util::setStringItem(row[0], item.getPattern());
|
||||
util::setBoolItem(row[1], item.getAlert());
|
||||
util::setBoolItem(row[2], item.getSound());
|
||||
util::setBoolItem(row[3], item.isRegex());
|
||||
}
|
||||
|
||||
void HighlightModel::afterInit()
|
||||
|
||||
@@ -2,24 +2,71 @@
|
||||
|
||||
#include "util/serialize-custom.hpp"
|
||||
|
||||
#include <QRegularExpression>
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
#include <pajlada/settings/serialize.hpp>
|
||||
|
||||
namespace chatterino {
|
||||
namespace controllers {
|
||||
namespace highlights {
|
||||
|
||||
struct HighlightPhrase {
|
||||
QString key;
|
||||
class HighlightPhrase
|
||||
{
|
||||
QString pattern;
|
||||
bool alert;
|
||||
bool sound;
|
||||
bool regex;
|
||||
bool _isRegex;
|
||||
QRegularExpression regex;
|
||||
|
||||
public:
|
||||
bool operator==(const HighlightPhrase &other) const
|
||||
{
|
||||
return std::tie(this->key, this->sound, this->alert, this->regex) ==
|
||||
std::tie(other.key, other.sound, other.alert, other.regex);
|
||||
return std::tie(this->pattern, this->sound, this->alert, this->_isRegex) ==
|
||||
std::tie(other.pattern, other.sound, other.alert, other._isRegex);
|
||||
}
|
||||
|
||||
HighlightPhrase(const QString &_pattern, bool _alert, bool _sound, bool isRegex)
|
||||
: pattern(_pattern)
|
||||
, alert(_alert)
|
||||
, sound(_sound)
|
||||
, _isRegex(isRegex)
|
||||
, regex(_isRegex ? _pattern : "\b" + QRegularExpression::escape(_pattern) + "\b",
|
||||
QRegularExpression::CaseInsensitiveOption)
|
||||
{
|
||||
}
|
||||
|
||||
const QString &getPattern() const
|
||||
{
|
||||
return this->pattern;
|
||||
}
|
||||
bool getAlert() const
|
||||
{
|
||||
return this->alert;
|
||||
}
|
||||
bool getSound() const
|
||||
{
|
||||
return this->sound;
|
||||
}
|
||||
bool isRegex() const
|
||||
{
|
||||
return this->_isRegex;
|
||||
}
|
||||
|
||||
bool isValid() const
|
||||
{
|
||||
return !this->pattern.isEmpty() && this->regex.isValid();
|
||||
}
|
||||
|
||||
bool isMatch(const QString &subject) const
|
||||
{
|
||||
return this->isValid() && this->regex.match(subject).hasMatch();
|
||||
}
|
||||
|
||||
// const QRegularExpression &getRegex() const
|
||||
// {
|
||||
// return this->regex;
|
||||
// }
|
||||
};
|
||||
} // namespace highlights
|
||||
} // namespace controllers
|
||||
@@ -35,10 +82,10 @@ struct Serialize<chatterino::controllers::highlights::HighlightPhrase> {
|
||||
{
|
||||
rapidjson::Value ret(rapidjson::kObjectType);
|
||||
|
||||
AddMember(ret, "key", value.key, a);
|
||||
AddMember(ret, "alert", value.alert, a);
|
||||
AddMember(ret, "sound", value.sound, a);
|
||||
AddMember(ret, "regex", value.regex, a);
|
||||
AddMember(ret, "pattern", value.getPattern(), a);
|
||||
AddMember(ret, "alert", value.getAlert(), a);
|
||||
AddMember(ret, "sound", value.getSound(), a);
|
||||
AddMember(ret, "regex", value.isRegex(), a);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -48,40 +95,45 @@ template <>
|
||||
struct Deserialize<chatterino::controllers::highlights::HighlightPhrase> {
|
||||
static chatterino::controllers::highlights::HighlightPhrase get(const rapidjson::Value &value)
|
||||
{
|
||||
chatterino::controllers::highlights::HighlightPhrase ret;
|
||||
if (!value.IsObject()) {
|
||||
return ret;
|
||||
return chatterino::controllers::highlights::HighlightPhrase(QString(), true, false,
|
||||
false);
|
||||
}
|
||||
|
||||
if (value.HasMember("key")) {
|
||||
const rapidjson::Value &key = value["key"];
|
||||
QString _pattern;
|
||||
if (value.HasMember("pattern")) {
|
||||
const rapidjson::Value &key = value["pattern"];
|
||||
if (key.IsString()) {
|
||||
ret.key = key.GetString();
|
||||
_pattern = key.GetString();
|
||||
}
|
||||
}
|
||||
|
||||
bool _alert = true;
|
||||
if (value.HasMember("alert")) {
|
||||
const rapidjson::Value &alert = value["alert"];
|
||||
if (alert.IsBool()) {
|
||||
ret.alert = alert.GetBool();
|
||||
_alert = alert.GetBool();
|
||||
}
|
||||
}
|
||||
|
||||
bool _sound = false;
|
||||
if (value.HasMember("sound")) {
|
||||
const rapidjson::Value &sound = value["sound"];
|
||||
if (sound.IsBool()) {
|
||||
ret.sound = sound.GetBool();
|
||||
_sound = sound.GetBool();
|
||||
}
|
||||
}
|
||||
|
||||
bool _isRegex = false;
|
||||
if (value.HasMember("regex")) {
|
||||
const rapidjson::Value ®ex = value["regex"];
|
||||
if (regex.IsBool()) {
|
||||
ret.regex = regex.GetBool();
|
||||
_isRegex = regex.GetBool();
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return chatterino::controllers::highlights::HighlightPhrase(_pattern, _alert, _sound,
|
||||
_isRegex);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user