mirror-chatterino2/src/singletons/TooltipPreviewImage.cpp

82 lines
1.9 KiB
C++
Raw Normal View History

#include "TooltipPreviewImage.hpp"
#include "Application.hpp"
#include "singletons/WindowManager.hpp"
#include "widgets/TooltipWidget.hpp"
namespace chatterino {
TooltipPreviewImage &TooltipPreviewImage::instance()
2019-06-12 00:31:02 +02:00
{
static TooltipPreviewImage *instance = new TooltipPreviewImage();
return *instance;
}
2019-06-12 00:31:02 +02:00
TooltipPreviewImage::TooltipPreviewImage()
{
auto windows = getApp()->windows;
this->connections_.managedConnect(windows->gifRepaintRequested, [&] {
if (this->image_ && this->image_->animated())
2019-06-12 00:31:02 +02:00
{
this->refreshTooltipWidgetPixmap();
2019-06-12 00:31:02 +02:00
}
});
this->connections_.managedConnect(windows->miscUpdate, [&] {
if (this->attemptRefresh)
2019-06-12 00:31:02 +02:00
{
this->refreshTooltipWidgetPixmap();
2019-06-12 00:31:02 +02:00
}
});
2019-06-12 00:31:02 +02:00
}
2019-06-12 00:31:02 +02:00
void TooltipPreviewImage::setImage(ImagePtr image)
{
this->image_ = std::move(image);
this->refreshTooltipWidgetPixmap();
}
void TooltipPreviewImage::setImageScale(int w, int h)
{
this->imageWidth_ = w;
this->imageHeight_ = h;
this->refreshTooltipWidgetPixmap();
}
void TooltipPreviewImage::refreshTooltipWidgetPixmap()
{
auto tooltipWidget = TooltipWidget::instance();
if (this->image_ && !tooltipWidget->isHidden())
{
if (auto pixmap = this->image_->pixmapOrLoad())
{
if (this->imageWidth_ != 0 && this->imageHeight_)
{
tooltipWidget->setImage(pixmap->scaled(this->imageWidth_,
this->imageHeight_,
Qt::KeepAspectRatio));
}
else
{
tooltipWidget->setImage(*pixmap);
}
this->attemptRefresh = false;
}
else
{
this->attemptRefresh = true;
}
}
else
{
tooltipWidget->clearImage();
}
2019-06-12 00:31:02 +02:00
}
} // namespace chatterino