Skip to content

Commit

Permalink
Add custom image functionality for inline mod buttons. (#5369)
Browse files Browse the repository at this point in the history
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
  • Loading branch information
Mm2PL and pajlada authored May 11, 2024
1 parent 321d881 commit c3b84cb
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 88 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unversioned

- Major: Release plugins alpha. (#5288)
- Minor: Add option to customise Moderation buttons with images. (#5369)
- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Dev: Add doxygen build target. (#5377)
- Dev: Make printing of strings in tests easier. (#5379)
Expand Down
8 changes: 6 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ set(SOURCE_FILES
util/IpcQueue.hpp
util/LayoutHelper.cpp
util/LayoutHelper.hpp
util/LoadPixmap.cpp
util/LoadPixmap.hpp
util/RapidjsonHelpers.cpp
util/RapidjsonHelpers.hpp
util/RatelimitBucket.cpp
Expand Down Expand Up @@ -631,6 +633,8 @@ set(SOURCE_FILES
widgets/helper/EditableModelView.hpp
widgets/helper/EffectLabel.cpp
widgets/helper/EffectLabel.hpp
widgets/helper/IconDelegate.cpp
widgets/helper/IconDelegate.hpp
widgets/helper/InvisibleSizeGrip.cpp
widgets/helper/InvisibleSizeGrip.hpp
widgets/helper/NotebookButton.cpp
Expand All @@ -639,8 +643,6 @@ set(SOURCE_FILES
widgets/helper/NotebookTab.hpp
widgets/helper/RegExpItemDelegate.cpp
widgets/helper/RegExpItemDelegate.hpp
widgets/helper/TrimRegExpValidator.cpp
widgets/helper/TrimRegExpValidator.hpp
widgets/helper/ResizingTextEdit.cpp
widgets/helper/ResizingTextEdit.hpp
widgets/helper/ScrollbarHighlight.cpp
Expand All @@ -655,6 +657,8 @@ set(SOURCE_FILES
widgets/helper/TitlebarButton.hpp
widgets/helper/TitlebarButtons.cpp
widgets/helper/TitlebarButtons.hpp
widgets/helper/TrimRegExpValidator.cpp
widgets/helper/TrimRegExpValidator.hpp

widgets/layout/FlowLayout.cpp
widgets/layout/FlowLayout.hpp
Expand Down
77 changes: 38 additions & 39 deletions src/controllers/moderationactions/ModerationAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,11 @@
#include "singletons/Resources.hpp"

#include <QRegularExpression>
#include <QUrl>

namespace chatterino {

// ModerationAction::ModerationAction(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)
//{
//}

ModerationAction::ModerationAction(const QString &action)
ModerationAction::ModerationAction(const QString &action, const QUrl &iconPath)
: action_(action)
{
static QRegularExpression replaceRegex("[!/.]");
Expand All @@ -37,6 +20,8 @@ ModerationAction::ModerationAction(const QString &action)

if (timeoutMatch.hasMatch())
{
this->type_ = Type::Timeout;

// if (multipleTimeouts > 1) {
// QString line1;
// QString line2;
Expand Down Expand Up @@ -99,31 +84,31 @@ ModerationAction::ModerationAction(const QString &action)
}
this->line2_ = "w";
}

// line1 = this->line1_;
// line2 = this->line2_;
// } else {
// this->_moderationActions.emplace_back(getResources().buttonTimeout,
// str);
// }
}
else if (action.startsWith("/ban "))
{
this->imageToLoad_ = 1;
this->type_ = Type::Ban;
}
else if (action.startsWith("/delete "))
{
this->imageToLoad_ = 2;
this->type_ = Type::Delete;
}
else
{
this->type_ = Type::Custom;

QString xD = action;

xD.replace(replaceRegex, "");

this->line1_ = xD.mid(0, 2);
this->line2_ = xD.mid(2, 2);
}

if (iconPath.isValid())
{
this->iconPath_ = iconPath;
}
}

bool ModerationAction::operator==(const ModerationAction &other) const
Expand All @@ -139,19 +124,23 @@ bool ModerationAction::isImage() const
const std::optional<ImagePtr> &ModerationAction::getImage() const
{
assertInGuiThread();
if (this->image_.has_value())
{
return this->image_;
}

if (this->imageToLoad_ != 0)
if (this->iconPath_.isValid())
{
if (this->imageToLoad_ == 1)
{
this->image_ =
Image::fromResourcePixmap(getResources().buttons.ban);
}
else if (this->imageToLoad_ == 2)
{
this->image_ =
Image::fromResourcePixmap(getResources().buttons.trashCan);
}
this->image_ = Image::fromUrl({this->iconPath_.toString()});
}
else if (this->type_ == Type::Ban)
{
this->image_ = Image::fromResourcePixmap(getResources().buttons.ban);
}
else if (this->type_ == Type::Delete)
{
this->image_ =
Image::fromResourcePixmap(getResources().buttons.trashCan);
}

return this->image_;
Expand All @@ -172,4 +161,14 @@ const QString &ModerationAction::getAction() const
return this->action_;
}

const QUrl &ModerationAction::iconPath() const
{
return this->iconPath_;
}

ModerationAction::Type ModerationAction::getType() const
{
return this->type_;
}

} // namespace chatterino
42 changes: 38 additions & 4 deletions src/controllers/moderationactions/ModerationAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <pajlada/serialize.hpp>
#include <QString>
#include <QUrl>

#include <memory>
#include <optional>
Expand All @@ -16,7 +17,32 @@ using ImagePtr = std::shared_ptr<Image>;
class ModerationAction
{
public:
ModerationAction(const QString &action);
/**
* Type of the action, parsed from the input `action`
*/
enum class Type {
/**
* /ban <user>
*/
Ban,

/**
* /delete <msg-id>
*/
Delete,

/**
* /timeout <user> <duration>
*/
Timeout,

/**
* Anything not matching the action types above
*/
Custom,
};

ModerationAction(const QString &action, const QUrl &iconPath = {});

bool operator==(const ModerationAction &other) const;

Expand All @@ -25,13 +51,18 @@ class ModerationAction
const QString &getLine1() const;
const QString &getLine2() const;
const QString &getAction() const;
const QUrl &iconPath() const;
Type getType() const;

private:
mutable std::optional<ImagePtr> image_;
QString line1_;
QString line2_;
QString action_;
int imageToLoad_{};

Type type_{};

QUrl iconPath_;
};

} // namespace chatterino
Expand All @@ -46,6 +77,7 @@ struct Serialize<chatterino::ModerationAction> {
rapidjson::Value ret(rapidjson::kObjectType);

chatterino::rj::set(ret, "pattern", value.getAction(), a);
chatterino::rj::set(ret, "icon", value.iconPath().toString(), a);

return ret;
}
Expand All @@ -63,10 +95,12 @@ struct Deserialize<chatterino::ModerationAction> {
}

QString pattern;

chatterino::rj::getSafe(value, "pattern", pattern);

return chatterino::ModerationAction(pattern);
QString icon;
chatterino::rj::getSafe(value, "icon", icon);

return chatterino::ModerationAction(pattern, QUrl(icon));
}
};

Expand Down
29 changes: 26 additions & 3 deletions src/controllers/moderationactions/ModerationActionModel.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
#include "controllers/moderationactions/ModerationActionModel.hpp"

#include "controllers/moderationactions/ModerationAction.hpp"
#include "messages/Image.hpp"
#include "util/LoadPixmap.hpp"
#include "util/PostToThread.hpp"
#include "util/StandardItemHelper.hpp"

#include <QIcon>
#include <QPixmap>

namespace chatterino {

// commandmodel
ModerationActionModel ::ModerationActionModel(QObject *parent)
: SignalVectorModel<ModerationAction>(1, parent)
: SignalVectorModel<ModerationAction>(2, parent)
{
}

// turn a vector item into a model row
ModerationAction ModerationActionModel::getItemFromRow(
std::vector<QStandardItem *> &row, const ModerationAction &original)
{
return ModerationAction(row[0]->data(Qt::DisplayRole).toString());
return ModerationAction(
row[Column::Command]->data(Qt::DisplayRole).toString(),
row[Column::Icon]->data(Qt::UserRole).toString());
}

// turns a row in the model into a vector item
void ModerationActionModel::getRowFromItem(const ModerationAction &item,
std::vector<QStandardItem *> &row)
{
setStringItem(row[0], item.getAction());
setStringItem(row[Column::Command], item.getAction());
setFilePathItem(row[Column::Icon], item.iconPath());
if (!item.iconPath().isEmpty())
{
auto oImage = item.getImage();
assert(oImage.has_value());
if (oImage.has_value())
{
auto url = oImage->get()->url();
loadPixmapFromUrl(url, [row](const QPixmap &pixmap) {
postToThread([row, pixmap]() {
row[Column::Icon]->setData(pixmap, Qt::DecorationRole);
});
});
}
}
}

} // namespace chatterino
5 changes: 5 additions & 0 deletions src/controllers/moderationactions/ModerationActionModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class ModerationActionModel : public SignalVectorModel<ModerationAction>
public:
explicit ModerationActionModel(QObject *parent);

enum Column {
Command = 0,
Icon = 1,
};

protected:
// turn a vector item into a model row
ModerationAction getItemFromRow(std::vector<QStandardItem *> &row,
Expand Down
49 changes: 11 additions & 38 deletions src/providers/twitch/TwitchBadges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "messages/Image.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "util/DisplayBadge.hpp"
#include "util/LoadPixmap.hpp"

#include <QBuffer>
#include <QFile>
Expand Down Expand Up @@ -239,48 +240,20 @@ void TwitchBadges::getBadgeIcons(const QList<DisplayBadge> &badges,
}
}

void TwitchBadges::loadEmoteImage(const QString &name, ImagePtr image,
void TwitchBadges::loadEmoteImage(const QString &name, const ImagePtr &image,
BadgeIconCallback &&callback)
{
auto url = image->url().string;
NetworkRequest(url)
.concurrent()
.cache()
.onSuccess([this, name, callback, url](auto result) {
auto data = result.getData();

// const cast since we are only reading from it
QBuffer buffer(const_cast<QByteArray *>(&data));
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer);

if (!reader.canRead() || reader.size().isEmpty())
{
qCWarning(chatterinoTwitch)
<< "Can't read badge image at" << url << "for" << name
<< reader.errorString();
return;
}

QImage image = reader.read();
if (image.isNull())
{
qCWarning(chatterinoTwitch)
<< "Failed reading badge image at" << url << "for" << name
<< reader.errorString();
return;
}
loadPixmapFromUrl(image->url(),
[this, name, callback{std::move(callback)}](auto pixmap) {
auto icon = std::make_shared<QIcon>(pixmap);

auto icon = std::make_shared<QIcon>(QPixmap::fromImage(image));

{
std::unique_lock lock(this->badgesMutex_);
this->badgesMap_[name] = icon;
}
{
std::unique_lock lock(this->badgesMutex_);
this->badgesMap_[name] = icon;
}

callback(name, icon);
})
.execute();
callback(name, icon);
});
}

} // namespace chatterino
Loading

0 comments on commit c3b84cb

Please sign in to comment.