created base for all the list based settings

This commit is contained in:
fourtf
2018-05-06 00:32:45 +02:00
parent 4c3f0921e2
commit ba4173822e
21 changed files with 646 additions and 317 deletions
+4 -4
View File
@@ -14,13 +14,13 @@ Command::Command(const QString &_text)
return;
}
this->name = _text.mid(0, index);
this->func = _text.mid(index + 1);
this->name = _text.mid(0, index).trimmed();
this->func = _text.mid(index + 1).trimmed();
}
Command::Command(const QString &_name, const QString &_func)
: name(_name)
, func(_func)
: name(_name.trimmed())
, func(_func.trimmed())
{
}
-12
View File
@@ -25,18 +25,6 @@ void CommandModel::getRowFromItem(const Command &item, std::vector<QStandardItem
row[1]->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
}
// returns the related index of the SignalVector
int CommandModel::getVectorIndexFromModelIndex(int index)
{
return index;
}
// returns the related index of the model
int CommandModel::getModelIndexFromVectorIndex(int index)
{
return index;
}
} // namespace commands
} // namespace controllers
} // namespace chatterino
@@ -22,12 +22,6 @@ protected:
// turns a row in the model into a vector item
virtual void getRowFromItem(const Command &item, std::vector<QStandardItem *> &row) override;
// returns the related index of the SignalVector
virtual int getVectorIndexFromModelIndex(int index) override;
// returns the related index of the model
virtual int getModelIndexFromVectorIndex(int index) override;
friend class CommandController;
};
@@ -0,0 +1,39 @@
#include "highlightcontroller.hpp"
#include "application.hpp"
#include "controllers/highlights/highlightmodel.hpp"
namespace chatterino {
namespace controllers {
namespace highlights {
HighlightController::HighlightController()
{
}
void HighlightController::initialize()
{
assert(!this->initialized);
this->initialized = true;
for (const HighlightPhrase &phrase : this->highlightsSetting.getValue()) {
this->phrases.appendItem(phrase);
}
this->phrases.delayedItemsChanged.connect([this] { //
int xd = this->phrases.getVector().size();
this->highlightsSetting.setValue(this->phrases.getVector());
});
}
HighlightModel *HighlightController::createModel(QObject *parent)
{
HighlightModel *model = new HighlightModel(parent);
model->init(&this->phrases);
return model;
}
} // namespace highlights
} // namespace controllers
} // namespace chatterino
@@ -0,0 +1,33 @@
#pragma once
#include "controllers/highlights/highlightphrase.hpp"
#include "singletons/settingsmanager.hpp"
#include "util/signalvector2.hpp"
namespace chatterino {
namespace controllers {
namespace highlights {
class HighlightModel;
class HighlightController
{
public:
HighlightController();
void initialize();
util::UnsortedSignalVector<HighlightPhrase> phrases;
HighlightModel *createModel(QObject *parent);
private:
bool initialized = false;
singletons::ChatterinoSetting<std::vector<highlights::HighlightPhrase>> highlightsSetting = {
"/highlighting/highlights"};
};
} // namespace highlights
} // namespace controllers
} // namespace chatterino
@@ -0,0 +1,91 @@
#include "highlightmodel.hpp"
#include "application.hpp"
#include "singletons/settingsmanager.hpp"
#include "util/standarditemhelper.hpp"
namespace chatterino {
namespace controllers {
namespace highlights {
// commandmodel
HighlightModel::HighlightModel(QObject *parent)
: util::SignalVectorModel<HighlightPhrase>(4, parent)
{
// auto app = getApp();
// std::vector<QStandardItem *> row = this->createRow();
// util::setBoolItem(row[0], app->settings->enableHighlightsSelf.getValue(), true, false);
// util::setBoolItem(row[1], app->settings->enableHighlightsSelf.getValue(), true, false);
// util::setBoolItem(row[2], app->settings->enableHighlightsSelf.getValue(), true, false);
// row[0]->setData("Your name (automatic)", Qt::DisplayRole);
// this->insertCustomRow(row, 0);
}
// app->settings->highlightProperties.setValue(phrases);
// app->settings->enableHighlightsSelf.setValue(
// model->item(0, 0)->data(Qt::CheckStateRole).toBool());
// app->settings->enableHighlightTaskbar.setValue(
// model->item(0, 1)->data(Qt::CheckStateRole).toBool());
// app->settings->enableHighlightSound.setValue(
// model->item(0, 2)->data(Qt::CheckStateRole).toBool());
// turn a vector item into a model row
HighlightPhrase HighlightModel::getItemFromRow(std::vector<QStandardItem *> &row)
{
// key, alert, sound, regex
return HighlightPhrase{
row[0]->data(Qt::DisplayRole).toString(), row[1]->data(Qt::CheckStateRole).toBool(),
row[2]->data(Qt::CheckStateRole).toBool(), row[3]->data(Qt::CheckStateRole).toBool()};
}
// 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);
}
void HighlightModel::afterInit()
{
std::vector<QStandardItem *> row = this->createRow();
util::setBoolItem(row[0], getApp()->settings->enableHighlightsSelf.getValue(), true, false);
row[0]->setData("Your username (automatic)", Qt::DisplayRole);
util::setBoolItem(row[1], getApp()->settings->enableHighlightTaskbar.getValue(), true, false);
util::setBoolItem(row[2], getApp()->settings->enableHighlightSound.getValue(), true, false);
this->insertCustomRow(row, 0);
}
void HighlightModel::customRowSetData(const std::vector<QStandardItem *> &row, int column,
const QVariant &value, int role)
{
switch (column) {
case 0: {
if (role == Qt::CheckStateRole) {
getApp()->settings->enableHighlightsSelf.setValue(value.toBool());
}
} break;
case 1: {
if (role == Qt::CheckStateRole) {
getApp()->settings->enableHighlightTaskbar.setValue(value.toBool());
}
} break;
case 2: {
if (role == Qt::CheckStateRole) {
getApp()->settings->enableHighlightSound.setValue(value.toBool());
}
} break;
case 3: {
// empty element
} break;
}
}
} // namespace highlights
} // namespace controllers
} // namespace chatterino
@@ -0,0 +1,36 @@
#pragma once
#include <QObject>
#include "controllers/highlights/highlightphrase.hpp"
#include "util/signalvectormodel.hpp"
namespace chatterino {
namespace controllers {
namespace highlights {
class HighlightController;
class HighlightModel : public util::SignalVectorModel<HighlightPhrase>
{
explicit HighlightModel(QObject *parent);
protected:
// turn a vector item into a model row
virtual HighlightPhrase getItemFromRow(std::vector<QStandardItem *> &row) override;
// turns a row in the model into a vector item
virtual void getRowFromItem(const HighlightPhrase &item,
std::vector<QStandardItem *> &row) override;
virtual void afterInit() override;
virtual void customRowSetData(const std::vector<QStandardItem *> &row, int column,
const QVariant &value, int role) override;
friend class HighlightController;
};
} // namespace highlights
} // namespace controllers
} // namespace chatterino
@@ -0,0 +1,89 @@
#pragma once
#include "util/serialize-custom.hpp"
#include <QString>
#include <pajlada/settings/serialize.hpp>
namespace chatterino {
namespace controllers {
namespace highlights {
struct HighlightPhrase {
QString key;
bool alert;
bool sound;
bool regex;
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);
}
};
} // namespace highlights
} // namespace controllers
} // namespace chatterino
namespace pajlada {
namespace Settings {
template <>
struct Serialize<chatterino::controllers::highlights::HighlightPhrase> {
static rapidjson::Value get(const chatterino::controllers::highlights::HighlightPhrase &value,
rapidjson::Document::AllocatorType &a)
{
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);
return ret;
}
};
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;
}
if (value.HasMember("key")) {
const rapidjson::Value &key = value["key"];
if (key.IsString()) {
ret.key = key.GetString();
}
}
if (value.HasMember("alert")) {
const rapidjson::Value &alert = value["alert"];
if (alert.IsBool()) {
ret.alert = alert.GetBool();
}
}
if (value.HasMember("sound")) {
const rapidjson::Value &sound = value["sound"];
if (sound.IsBool()) {
ret.sound = sound.GetBool();
}
}
if (value.HasMember("regex")) {
const rapidjson::Value &regex = value["regex"];
if (regex.IsBool()) {
ret.regex = regex.GetBool();
}
}
return ret;
}
};
} // namespace Settings
} // namespace pajlada