added moderation buttons

This commit is contained in:
fourtf
2018-01-17 14:14:31 +01:00
parent 252f648ff8
commit 6d6b99f3ef
15 changed files with 367 additions and 76 deletions
+25 -23
View File
@@ -4,16 +4,16 @@
#include <QtGlobal>
#ifdef Q_OS_WIN32
#define DEFAULT_FONT_FAMILY "Segoe UI"
#define DEFAULT_FONT_SIZE 10
#define DEFAULT_FONT_FAMILY "Segoe UI"
#define DEFAULT_FONT_SIZE 10
#else
#ifdef Q_OS_MACOS
#define DEFAULT_FONT_FAMILY "Helvetica Neue"
#define DEFAULT_FONT_SIZE 12
#else
#define DEFAULT_FONT_FAMILY "Arial"
#define DEFAULT_FONT_SIZE 11
#endif
#ifdef Q_OS_MACOS
#define DEFAULT_FONT_FAMILY "Helvetica Neue"
#define DEFAULT_FONT_SIZE 12
#else
#define DEFAULT_FONT_FAMILY "Arial"
#define DEFAULT_FONT_SIZE 11
#endif
#endif
namespace chatterino {
@@ -27,13 +27,13 @@ FontManager::FontManager()
this->currentFontFamily.connect([this](const std::string &newValue, auto) {
this->incGeneration();
// this->currentFont.setFamily(newValue.c_str());
this->currentFontByDpi.clear();
this->currentFontByScale.clear();
this->fontChanged.invoke();
});
this->currentFontSize.connect([this](const int &newValue, auto) {
this->incGeneration();
// this->currentFont.setSize(newValue);
this->currentFontByDpi.clear();
this->currentFontByScale.clear();
this->fontChanged.invoke();
});
}
@@ -45,21 +45,23 @@ FontManager &FontManager::getInstance()
return instance;
}
QFont &FontManager::getFont(Type type, float dpi)
QFont &FontManager::getFont(Type type, float scale)
{
// return this->currentFont.getFont(type);
return this->getCurrentFont(dpi).getFont(type);
return this->getCurrentFont(scale).getFont(type);
}
QFontMetrics &FontManager::getFontMetrics(Type type, float dpi)
QFontMetrics &FontManager::getFontMetrics(Type type, float scale)
{
// return this->currentFont.getFontMetrics(type);
return this->getCurrentFont(dpi).getFontMetrics(type);
return this->getCurrentFont(scale).getFontMetrics(type);
}
FontManager::FontData &FontManager::Font::getFontData(Type type)
{
switch (type) {
case Tiny:
return this->tiny;
case Small:
return this->small;
case MediumSmall:
@@ -90,18 +92,18 @@ QFontMetrics &FontManager::Font::getFontMetrics(Type type)
return this->getFontData(type).metrics;
}
FontManager::Font &FontManager::getCurrentFont(float dpi)
FontManager::Font &FontManager::getCurrentFont(float scale)
{
for (auto it = this->currentFontByDpi.begin(); it != this->currentFontByDpi.end(); it++) {
if (it->first == dpi) {
for (auto it = this->currentFontByScale.begin(); it != this->currentFontByScale.end(); it++) {
if (it->first == scale) {
return it->second;
}
}
this->currentFontByDpi.push_back(std::make_pair(
dpi,
Font(this->currentFontFamily.getValue().c_str(), this->currentFontSize.getValue() * dpi)));
this->currentFontByScale.push_back(
std::make_pair(scale, Font(this->currentFontFamily.getValue().c_str(),
this->currentFontSize.getValue() * scale)));
return this->currentFontByDpi.back().second;
return this->currentFontByScale.back().second;
}
} // namespace singletons
} // namespace chatterino
}
+11 -6
View File
@@ -1,6 +1,7 @@
#pragma once
#include <QFont>
#include <QFontDatabase>
#include <QFontMetrics>
#include <pajlada/settings/setting.hpp>
#include <pajlada/signals/signal.hpp>
@@ -16,6 +17,7 @@ class FontManager
public:
enum Type : uint8_t {
Tiny,
Small,
MediumSmall,
Medium,
@@ -28,8 +30,8 @@ public:
// FontManager is initialized only once, on first use
static FontManager &getInstance();
QFont &getFont(Type type, float dpi);
QFontMetrics &getFontMetrics(Type type, float dpi);
QFont &getFont(Type type, float scale);
QFontMetrics &getFontMetrics(Type type, float scale);
int getGeneration() const
{
@@ -62,7 +64,8 @@ private:
Font() = delete;
explicit Font(const char *fontFamilyName, int mediumSize)
: small(QFont(fontFamilyName, mediumSize - 4))
: tiny(QFont("Monospace", 8))
, small(QFont(fontFamilyName, mediumSize - 4))
, mediumSmall(QFont(fontFamilyName, mediumSize - 2))
, medium(QFont(fontFamilyName, mediumSize))
, mediumBold(QFont(fontFamilyName, mediumSize, QFont::DemiBold))
@@ -70,6 +73,7 @@ private:
, large(QFont(fontFamilyName, mediumSize))
, veryLarge(QFont(fontFamilyName, mediumSize))
{
tiny.font.setStyleHint(QFont::TypeWriter);
}
void setFamily(const char *newFamily)
@@ -114,6 +118,7 @@ private:
QFont &getFont(Type type);
QFontMetrics &getFontMetrics(Type type);
FontData tiny;
FontData small;
FontData mediumSmall;
FontData medium;
@@ -123,16 +128,16 @@ private:
FontData veryLarge;
};
Font &getCurrentFont(float dpi);
Font &getCurrentFont(float scale);
// Future plans:
// Could have multiple fonts in here, such as "Menu font", "Application font", "Chat font"
std::list<std::pair<float, Font>> currentFontByDpi;
std::list<std::pair<float, Font>> currentFontByScale;
int generation = 0;
};
}
} // namespace singletons
typedef singletons::FontManager::Type FontStyle;
} // namespace chatterino
@@ -0,0 +1,50 @@
#include "moderationaction.hpp"
#include "singletons/resourcemanager.hpp"
namespace chatterino {
namespace singletons {
ModerationAction::ModerationAction(messages::Image *_image, const QString &_action)
: _isImage(true)
, image(_image)
, action(_action)
{
}
ModerationAction::ModerationAction(const QString &_line1, const QString &_line2,
const QString &_action)
: _isImage(false)
, image(nullptr)
, line1(_line1)
, line2(_line2)
, action(_action)
{
}
bool ModerationAction::isImage() const
{
return this->_isImage;
}
messages::Image *ModerationAction::getImage() const
{
return this->image;
}
const QString &ModerationAction::getLine1() const
{
return this->line1;
}
const QString &ModerationAction::getLine2() const
{
return this->line2;
}
const QString &ModerationAction::getAction() const
{
return this->action;
}
} // namespace singletons
} // namespace chatterino
@@ -0,0 +1,31 @@
#pragma once
#include <QString>
namespace chatterino {
namespace messages {
class Image;
}
namespace singletons {
class ModerationAction
{
public:
ModerationAction(messages::Image *image, const QString &action);
ModerationAction(const QString &line1, const QString &line2, const QString &action);
bool isImage() const;
messages::Image *getImage() const;
const QString &getLine1() const;
const QString &getLine2() const;
const QString &getAction() const;
private:
bool _isImage;
messages::Image *image;
QString line1;
QString line2;
QString action;
};
} // namespace singletons
} // namespace chatterino
+75
View File
@@ -1,6 +1,7 @@
#include "singletons/settingsmanager.hpp"
#include "debug/log.hpp"
#include "singletons/pathmanager.hpp"
#include "singletons/resourcemanager.hpp"
using namespace chatterino::messages;
@@ -26,6 +27,8 @@ SettingManager::SettingManager()
this->wordMaskListener.cb = [this](auto) {
this->updateWordTypeMask(); //
};
this->moderationActions.connect([this](auto, auto) { this->updateModerationActions(); });
}
MessageElement::Flags SettingManager::getWordTypeMask()
@@ -127,5 +130,77 @@ void SettingManager::recallSnapshot()
}
}
std::vector<ModerationAction> SettingManager::getModerationActions() const
{
return this->_moderationActions;
}
void SettingManager::updateModerationActions()
{
auto &resources = singletons::ResourceManager::getInstance();
this->_moderationActions.clear();
static QRegularExpression newLineRegex("(\r\n?|\n)+");
static QRegularExpression replaceRegex("[!/.]");
static QRegularExpression timeoutRegex("^[./]timeout.* (\\d+)");
QStringList list = this->moderationActions.getValue().split(newLineRegex);
int multipleTimeouts = 0;
for (QString &str : list) {
if (timeoutRegex.match(str).hasMatch()) {
multipleTimeouts++;
if (multipleTimeouts > 1) {
break;
}
}
}
for (int i = 0; i < list.size(); i++) {
QString &str = list[i];
if (str.isEmpty()) {
continue;
}
auto timeoutMatch = timeoutRegex.match(str);
if (timeoutMatch.hasMatch()) {
if (multipleTimeouts > 1) {
QString line1;
QString line2;
int amount = timeoutMatch.captured(1).toInt();
if (amount < 60) {
line1 = QString::number(amount);
line2 = "s";
} else if (amount < 60 * 60) {
line1 = QString::number(amount / 60);
line2 = "m";
} else if (amount < 60 * 60 * 24) {
line1 = QString::number(amount / 60 / 60);
line2 = "h";
} else {
line1 = QString::number(amount / 60 / 60 / 24);
line2 = "d";
}
this->_moderationActions.emplace_back(line1, line2, str);
} else {
this->_moderationActions.emplace_back(resources.buttonTimeout, str);
}
} else if (str.startsWith("/ban ")) {
this->_moderationActions.emplace_back(resources.buttonBan, str);
} else {
QString xD = str;
xD.replace(replaceRegex, "");
this->_moderationActions.emplace_back(xD.mid(0, 2), xD.mid(2, 2), str);
}
}
}
} // namespace singletons
} // namespace chatterino
+6
View File
@@ -3,6 +3,7 @@
#include "messages/highlightphrase.hpp"
#include "messages/messageelement.hpp"
#include "singletons/helper/chatterinosetting.hpp"
#include "singletons/helper/moderationaction.hpp"
#include <pajlada/settings/setting.hpp>
#include <pajlada/settings/settinglistener.hpp>
@@ -105,14 +106,19 @@ public:
void saveSnapshot();
void recallSnapshot();
std::vector<ModerationAction> getModerationActions() const;
signals:
void wordTypeMaskChanged();
private:
std::vector<ModerationAction> _moderationActions;
std::unique_ptr<rapidjson::Document> snapshot;
SettingManager();
void updateModerationActions();
messages::MessageElement::Flags wordTypeMask = messages::MessageElement::Default;
pajlada::Settings::SettingListener wordMaskListener;