refactored TaggedUsers and Ping

This commit is contained in:
fourtf
2020-02-23 20:34:00 +01:00
parent df91ea44c4
commit d0a81f3fe7
14 changed files with 131 additions and 161 deletions
@@ -0,0 +1,57 @@
#include "controllers/pings/MutedChannelController.hpp"
#include "controllers/pings/MutedChannelModel.hpp"
#include "util/PersistSignalVector.hpp"
namespace chatterino {
void MutedChannelController::initialize(Settings &settings, Paths &paths)
{
this->initialized_ = true;
persist(this->channels, "/pings/muted");
}
bool MutedChannelController::isMuted(const QString &channelName)
{
for (const auto &channel : this->channels)
{
if (channelName.toLower() == channel.toLower())
{
return true;
}
}
return false;
}
void MutedChannelController::mute(const QString &channelName)
{
channels.append(channelName);
}
void MutedChannelController::unmute(const QString &channelName)
{
for (std::vector<int>::size_type i = 0; i != channels.raw().size(); i++)
{
if (channels.raw()[i].toLower() == channelName.toLower())
{
channels.removeAt(i);
i--;
}
}
}
bool MutedChannelController::toggleMuted(const QString &channelName)
{
if (this->isMuted(channelName))
{
unmute(channelName);
return false;
}
else
{
mute(channelName);
return true;
}
}
} // namespace chatterino
@@ -0,0 +1,32 @@
#pragma once
#include <QObject>
#include "common/SignalVector.hpp"
#include "common/Singleton.hpp"
#include "singletons/Settings.hpp"
namespace chatterino {
class Settings;
class Paths;
class MutedChannelModel;
class MutedChannelController final : public Singleton, private QObject
{
public:
virtual void initialize(Settings &settings, Paths &paths) override;
bool isMuted(const QString &channelName);
bool toggleMuted(const QString &channelName);
private:
void mute(const QString &channelName);
void unmute(const QString &channelName);
bool initialized_ = false;
SignalVector<QString> channels;
};
} // namespace chatterino
@@ -0,0 +1,28 @@
#include "MutedChannelModel.hpp"
#include "Application.hpp"
#include "singletons/Settings.hpp"
#include "util/StandardItemHelper.hpp"
namespace chatterino {
MutedChannelModel::MutedChannelModel(QObject *parent)
: SignalVectorModel<QString>(1, parent)
{
}
// turn a vector item into a model row
QString MutedChannelModel::getItemFromRow(std::vector<QStandardItem *> &row,
const QString &original)
{
return QString(row[0]->data(Qt::DisplayRole).toString());
}
// turn a model
void MutedChannelModel::getRowFromItem(const QString &item,
std::vector<QStandardItem *> &row)
{
setStringItem(row[0], item);
}
} // namespace chatterino
@@ -7,11 +7,11 @@
namespace chatterino {
class PingController;
class MutedChannelController;
class PingModel : public SignalVectorModel<QString>
class MutedChannelModel : public SignalVectorModel<QString>
{
explicit PingModel(QObject *parent);
explicit MutedChannelModel(QObject *parent);
protected:
// turn a vector item into a model row
@@ -21,8 +21,6 @@ protected:
// turns a row in the model into a vector item
virtual void getRowFromItem(const QString &item,
std::vector<QStandardItem *> &row) override;
friend class PingController;
};
} // namespace chatterino
-70
View File
@@ -1,70 +0,0 @@
#include "controllers/pings/PingController.hpp"
#include "controllers/pings/PingModel.hpp"
namespace chatterino {
void PingController::initialize(Settings &settings, Paths &paths)
{
this->initialized_ = true;
for (const QString &channelName : this->pingSetting_.getValue())
{
this->channelVector.append(channelName);
}
this->channelVector.delayedItemsChanged.connect([this] { //
this->pingSetting_.setValue(this->channelVector.raw());
});
}
PingModel *PingController::createModel(QObject *parent)
{
PingModel *model = new PingModel(parent);
model->initialize(&this->channelVector);
return model;
}
bool PingController::isMuted(const QString &channelName)
{
for (const auto &channel : this->channelVector)
{
if (channelName.toLower() == channel.toLower())
{
return true;
}
}
return false;
}
void PingController::muteChannel(const QString &channelName)
{
channelVector.append(channelName);
}
void PingController::unmuteChannel(const QString &channelName)
{
for (std::vector<int>::size_type i = 0; i != channelVector.raw().size();
i++)
{
if (channelVector.raw()[i].toLower() == channelName.toLower())
{
channelVector.removeAt(i);
i--;
}
}
}
bool PingController::toggleMuteChannel(const QString &channelName)
{
if (this->isMuted(channelName))
{
unmuteChannel(channelName);
return false;
}
else
{
muteChannel(channelName);
return true;
}
}
} // namespace chatterino
-36
View File
@@ -1,36 +0,0 @@
#pragma once
#include <QObject>
#include "common/SignalVector.hpp"
#include "common/Singleton.hpp"
#include "singletons/Settings.hpp"
namespace chatterino {
class Settings;
class Paths;
class PingModel;
class PingController final : public Singleton, private QObject
{
public:
virtual void initialize(Settings &settings, Paths &paths) override;
bool isMuted(const QString &channelName);
void muteChannel(const QString &channelName);
void unmuteChannel(const QString &channelName);
bool toggleMuteChannel(const QString &channelName);
PingModel *createModel(QObject *parent);
private:
bool initialized_ = false;
SignalVector<QString> channelVector;
ChatterinoSetting<std::vector<QString>> pingSetting_ = {"/pings/muted"};
};
} // namespace chatterino
-28
View File
@@ -1,28 +0,0 @@
#include "PingModel.hpp"
#include "Application.hpp"
#include "singletons/Settings.hpp"
#include "util/StandardItemHelper.hpp"
namespace chatterino {
PingModel::PingModel(QObject *parent)
: SignalVectorModel<QString>(1, parent)
{
}
// turn a vector item into a model row
QString PingModel::getItemFromRow(std::vector<QStandardItem *> &row,
const QString &original)
{
return QString(row[0]->data(Qt::DisplayRole).toString());
}
// turn a model
void PingModel::getRowFromItem(const QString &item,
std::vector<QStandardItem *> &row)
{
setStringItem(row[0], item);
}
} // namespace chatterino
@@ -9,12 +9,4 @@ TaggedUsersController::TaggedUsersController()
{
}
TaggedUsersModel *TaggedUsersController::createModel(QObject *parent)
{
TaggedUsersModel *model = new TaggedUsersModel(parent);
model->initialize(&this->users);
return model;
}
} // namespace chatterino
@@ -15,8 +15,6 @@ public:
TaggedUsersController();
SignalVector<TaggedUser> users;
TaggedUsersModel *createModel(QObject *parent = nullptr);
};
} // namespace chatterino