Get rid of dynamic_cast

This commit is contained in:
rexim 2019-05-22 00:33:12 +07:00
parent cbb1ec2d7a
commit aa4e7f13e7
3 changed files with 20 additions and 9 deletions

View file

@ -296,6 +296,11 @@ int TextLayoutElement::getXFromIndex(int index)
}
}
boost::optional<QPixmap> TextLayoutElement::pixmap() const
{
return boost::none;
}
// TEXT ICON
TextIconLayoutElement::TextIconLayoutElement(MessageElement &creator,
const QString &_line1,
@ -373,4 +378,10 @@ int TextIconLayoutElement::getXFromIndex(int index)
}
}
boost::optional<QPixmap> TextIconLayoutElement::pixmap() const
{
return boost::none;
}
} // namespace chatterino

View file

@ -41,6 +41,8 @@ public:
virtual void paintAnimated(QPainter &painter, int yOffset) = 0;
virtual int getMouseOverIndex(const QPoint &abs) const = 0;
virtual int getXFromIndex(int index) = 0;
virtual boost::optional<QPixmap> pixmap() const = 0;
const Link &getLink() const;
const QString &getText() const;
FlagsEnum<MessageElementFlag> getFlags() const;
@ -62,8 +64,6 @@ public:
ImageLayoutElement(MessageElement &creator, ImagePtr image,
const QSize &size);
boost::optional<QPixmap> pixmap() const;
protected:
void addCopyTextToString(QString &str, int from = 0,
int to = INT_MAX) const override;
@ -72,6 +72,7 @@ protected:
void paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(int index) override;
boost::optional<QPixmap> pixmap() const override;
private:
ImagePtr image_;
@ -95,6 +96,7 @@ protected:
void paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(int index) override;
boost::optional<QPixmap> pixmap() const override;
private:
QColor color_;
@ -120,6 +122,7 @@ protected:
void paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(int index) override;
boost::optional<QPixmap> pixmap() const override;
private:
float scale;

View file

@ -1217,12 +1217,9 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
}
else
{
auto imageLayoutElement =
dynamic_cast<const ImageLayoutElement *>(hoverLayoutElement);
if (imageLayoutElement && imageLayoutElement->pixmap()) {
if (auto pixmap = hoverLayoutElement->pixmap()) {
QBuffer buffer;
imageLayoutElement->pixmap()->save(&buffer, "PNG");
pixmap->save(&buffer, "PNG");
// FIXME: Image inside of imageLayoutElement is not always the biggest
// FIXME: Inject image directly into tooltipWidget without base64 bs
@ -1232,8 +1229,8 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
tooltipWidget->setText(
QString("<img src='data:image/png;base64,%1' /><br/>%2x%3<br/>%4")
.arg(QString(buffer.data().toBase64()))
.arg(imageLayoutElement->pixmap()->width())
.arg(imageLayoutElement->pixmap()->height())
.arg(pixmap->width())
.arg(pixmap->height())
.arg(tooltip));
} else {
tooltipWidget->setText(tooltip);