refactored IgnoredController

This commit is contained in:
fourtf 2020-02-23 20:01:35 +01:00
parent 89389608e9
commit 8ae9abb250
7 changed files with 33 additions and 43 deletions

View file

@ -434,6 +434,7 @@ HEADERS += \
src/util/LayoutCreator.hpp \
src/util/LayoutHelper.hpp \
src/util/Overloaded.hpp \
src/util/PersistSignalVector.hpp \
src/util/PostToThread.hpp \
src/util/QObjectRef.hpp \
src/util/QStringHash.hpp \

View file

@ -1,26 +1,10 @@
#include "HighlightController.hpp"
#include "Application.hpp"
#include "common/ChatterinoSetting.hpp"
#include "util/PersistSignalVector.hpp"
namespace chatterino {
template <typename T>
inline void persist(SignalVector<T> &vec, const std::string &name)
{
auto setting = std::make_unique<ChatterinoSetting<std::vector<T>>>(name);
for (auto &&item : setting->getValue())
vec.append(item);
vec.delayedItemsChanged.connect([setting = setting.get(), vec = &vec] {
setting->setValue(vec->raw());
});
// TODO: Delete when appropriate.
setting.release();
}
void HighlightController::initialize(Settings &settings, Paths &paths)
{
assert(!this->initialized_);

View file

@ -2,6 +2,7 @@
#include "Application.hpp"
#include "controllers/ignores/IgnoreModel.hpp"
#include "util/PersistSignalVector.hpp"
#include <cassert>
@ -12,22 +13,7 @@ void IgnoreController::initialize(Settings &, Paths &)
assert(!this->initialized_);
this->initialized_ = true;
for (const IgnorePhrase &phrase : this->ignoresSetting_.getValue())
{
this->phrases.append(phrase);
}
this->phrases.delayedItemsChanged.connect([this] { //
this->ignoresSetting_.setValue(this->phrases.raw());
});
}
IgnoreModel *IgnoreController::createModel(QObject *parent)
{
IgnoreModel *model = new IgnoreModel(parent);
model->initialize(&this->phrases);
return model;
persist(this->phrases, "/ignore/phrases");
}
} // namespace chatterino

View file

@ -21,13 +21,8 @@ public:
SignalVector<IgnorePhrase> phrases;
IgnoreModel *createModel(QObject *parent);
private:
bool initialized_ = false;
ChatterinoSetting<std::vector<IgnorePhrase>> ignoresSetting_ = {
"/ignore/phrases"};
};
} // namespace chatterino

View file

@ -7,10 +7,9 @@
namespace chatterino {
class IgnoreController;
class IgnoreModel : public SignalVectorModel<IgnorePhrase>
{
public:
explicit IgnoreModel(QObject *parent);
protected:
@ -21,8 +20,6 @@ protected:
// turns a row in the model into a vector item
virtual void getRowFromItem(const IgnorePhrase &item,
std::vector<QStandardItem *> &row) override;
friend class IgnoreController;
};
} // namespace chatterino

View file

@ -0,0 +1,25 @@
#pragma once
#include <memory>
#include "common/ChatterinoSetting.hpp"
#include "common/SignalVector.hpp"
namespace chatterino {
template <typename T>
inline void persist(SignalVector<T> &vec, const std::string &name)
{
auto setting = std::make_unique<ChatterinoSetting<std::vector<T>>>(name);
for (auto &&item : setting->getValue())
vec.append(item);
vec.delayedItemsChanged.connect([setting = setting.get(), vec = &vec] {
setting->setValue(vec->raw());
});
// TODO: Delete when appropriate.
setting.release();
}
} // namespace chatterino

View file

@ -44,7 +44,9 @@ void addPhrasesTab(LayoutCreator<QVBoxLayout> layout)
layout.emplace<QLabel>("Ignore messages based certain patterns.");
EditableModelView *view =
layout
.emplace<EditableModelView>(getApp()->ignores->createModel(nullptr))
.emplace<EditableModelView>(
(new IgnoreModel(nullptr))
->initialized(&getApp()->ignores->phrases))
.getElement();
view->setTitles(
{"Pattern", "Regex", "Case Sensitive", "Block", "Replacement"});