Remove "custom on success" logic from Image.

Create a new message element and message layout element type for mod badges.
This commit is contained in:
Rasmus Karlsson
2019-09-08 11:30:06 +02:00
committed by fourtf
parent 256a65a12e
commit dbce128cc3
8 changed files with 84 additions and 53 deletions
-10
View File
@@ -349,9 +349,6 @@ void Image::actuallyLoad()
if (!shared)
return Failure;
if (shared->customOnSuccess_)
return shared->customOnSuccess_(shared, result);
auto data = result.getData();
// const cast since we are only reading from it
@@ -396,11 +393,4 @@ bool Image::operator!=(const Image &other) const
return !this->operator==(other);
}
ImagePtr Image::setCustomOnSuccess(
std::function<Outcome(ImagePtr, NetworkResult)> customOnSuccess)
{
this->customOnSuccess_ = customOnSuccess;
return shared_from_this();
}
} // namespace chatterino
-6
View File
@@ -68,18 +68,13 @@ public:
bool operator==(const Image &image) const;
bool operator!=(const Image &image) const;
ImagePtr setCustomOnSuccess(
std::function<Outcome(ImagePtr, NetworkResult)> onSuccess);
private:
Image();
Image(const Url &url, qreal scale);
Image(qreal scale);
public:
void setPixmap(const QPixmap &pixmap);
private:
void actuallyLoad();
Url url_{};
@@ -88,6 +83,5 @@ private:
bool shouldLoad_{false};
std::unique_ptr<detail::Frames> frames_{};
QObject object_{};
std::function<Outcome(ImagePtr, NetworkResult)> customOnSuccess_{};
};
} // namespace chatterino
+22 -1
View File
@@ -145,7 +145,7 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container,
QSize(int(container.getScale() * image->width() * emoteScale),
int(container.getScale() * image->height() * emoteScale));
container.addElement((new ImageLayoutElement(*this, image, size))
container.addElement(this->makeImageLayoutElement(image, size)
->setLink(this->getLink()));
}
else
@@ -159,6 +159,27 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container,
}
}
MessageLayoutElement *EmoteElement::makeImageLayoutElement(
const ImagePtr &image, const QSize &size)
{
return new ImageLayoutElement(*this, image, size);
}
// MOD BADGE
ModBadgeElement::ModBadgeElement(const EmotePtr &data,
MessageElementFlags flags_)
: EmoteElement(data, flags_)
{
}
MessageLayoutElement *ModBadgeElement::makeImageLayoutElement(
const ImagePtr &image, const QSize &size)
{
static const QColor modBadgeBackgroundColor("#34AE0A");
return new ImageWithBackgroundLayoutElement(*this, image, size,
modBadgeBackgroundColor);
}
// BADGE
BadgeElement::BadgeElement(const EmotePtr &emote, MessageElementFlags flags)
: MessageElement(flags)
+16
View File
@@ -17,6 +17,7 @@
namespace chatterino {
class Channel;
struct MessageLayoutContainer;
class MessageLayoutElement;
class Image;
using ImagePtr = std::shared_ptr<Image>;
@@ -209,11 +210,26 @@ public:
MessageElementFlags flags_) override;
EmotePtr getEmote() const;
protected:
virtual MessageLayoutElement *makeImageLayoutElement(const ImagePtr &image,
const QSize &size);
private:
std::unique_ptr<TextElement> textElement_;
EmotePtr emote_;
};
// Behaves like an emote element, except it creates a different image layout element that draws the mod badge background
class ModBadgeElement : public EmoteElement
{
public:
ModBadgeElement(const EmotePtr &data, MessageElementFlags flags_);
protected:
MessageLayoutElement *makeImageLayoutElement(const ImagePtr &image,
const QSize &size) override;
};
class BadgeElement : public MessageElement
{
public:
@@ -168,6 +168,33 @@ int ImageLayoutElement::getXFromIndex(int index)
}
}
//
// IMAGE WITH BACKGROUND
//
ImageWithBackgroundLayoutElement::ImageWithBackgroundLayoutElement(
MessageElement &creator, ImagePtr image, const QSize &size, QColor color)
: ImageLayoutElement(creator, image, size)
, color_(color)
{
}
void ImageWithBackgroundLayoutElement::paint(QPainter &painter)
{
if (this->image_ == nullptr)
{
return;
}
auto pixmap = this->image_->pixmapOrLoad();
if (pixmap && !this->image_->animated())
{
painter.fillRect(QRectF(this->getRect()), this->color_);
// fourtf: make it use qreal values
painter.drawPixmap(QRectF(this->getRect()), *pixmap, QRectF());
}
}
//
// TEXT
//
+13 -1
View File
@@ -72,10 +72,22 @@ protected:
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(int index) override;
private:
ImagePtr image_;
};
class ImageWithBackgroundLayoutElement : public ImageLayoutElement
{
public:
ImageWithBackgroundLayoutElement(MessageElement &creator, ImagePtr image,
const QSize &size, QColor color);
protected:
void paint(QPainter &painter) override;
private:
QColor color_;
};
// TEXT
class TextLayoutElement : public MessageLayoutElement
{