ref: merge TooltipPreviewImage and TooltipWidget (#4268)

* merge TooltipPreviewImage and TooltipWidget

* changelog

* add empty line before return

* fix signalholder include

* add changelog for bugfix

* fix custom scaling issue

Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
kornes
2022-12-29 15:07:46 +00:00
committed by GitHub
parent 448c8622e7
commit 11fdd7ed74
8 changed files with 115 additions and 174 deletions
+89 -30
View File
@@ -1,17 +1,11 @@
#include "TooltipWidget.hpp"
#include "Application.hpp"
#include "singletons/Fonts.hpp"
#include <QDebug>
#include <QDesktopWidget>
#include <QStyle>
#include <QVBoxLayout>
#ifdef USEWINSDK
# include <Windows.h>
#endif
#include "singletons/WindowManager.hpp"
#include <QPainter>
#include <QVBoxLayout>
namespace chatterino {
@@ -25,44 +19,52 @@ TooltipWidget::TooltipWidget(BaseWidget *parent)
: BaseWindow({BaseWindow::TopMost, BaseWindow::DontFocus,
BaseWindow::DisableLayoutSave},
parent)
, displayImage_(new QLabel())
, displayText_(new QLabel())
, displayImage_(new QLabel(this))
, displayText_(new QLabel(this))
{
this->setStyleSheet("color: #fff; background: rgba(11, 11, 11, 0.8)");
this->setAttribute(Qt::WA_TranslucentBackground);
//this->setWindowOpacity(0.8);
this->updateFont();
this->setWindowFlag(Qt::WindowStaysOnTopHint, true);
this->setStayInScreenRect(true);
displayImage_->hide();
displayImage_->setAlignment(Qt::AlignHCenter);
displayImage_->setStyleSheet("background: transparent");
displayText_->setAlignment(Qt::AlignHCenter);
displayText_->setText("tooltip text");
displayText_->setStyleSheet("background: transparent");
auto layout = new QVBoxLayout();
auto *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetFixedSize);
layout->setContentsMargins(10, 5, 10, 5);
layout->addWidget(displayImage_);
layout->addWidget(displayText_);
this->setLayout(layout);
this->fontChangedConnection_ = getFonts()->fontChanged.connect([this] {
this->connections_.managedConnect(getFonts()->fontChanged, [this] {
this->updateFont();
});
}
this->updateFont();
TooltipWidget::~TooltipWidget()
{
this->fontChangedConnection_.disconnect();
}
auto windows = getApp()->windows;
this->connections_.managedConnect(windows->gifRepaintRequested, [this] {
if (this->image_ && this->image_->animated())
{
this->refreshPixmap();
}
});
#ifdef USEWINSDK
void TooltipWidget::raise()
{
::SetWindowPos(HWND(this->winId()), HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
this->connections_.managedConnect(windows->miscUpdate, [this] {
if (this->image_ && this->attemptRefresh)
{
if (this->refreshPixmap())
{
this->attemptRefresh = false;
this->adjustSize();
}
}
});
}
#endif
void TooltipWidget::themeChangedEvent()
{
@@ -100,12 +102,69 @@ void TooltipWidget::setWordWrap(bool wrap)
void TooltipWidget::clearImage()
{
this->displayImage_->hide();
this->image_ = nullptr;
this->setImageScale(0, 0);
}
void TooltipWidget::setImage(QPixmap image)
void TooltipWidget::setImage(ImagePtr image)
{
if (this->image_ == image)
{
return;
}
// hide image until loaded and reset scale
this->clearImage();
this->image_ = std::move(image);
this->refreshPixmap();
}
void TooltipWidget::setImageScale(int w, int h)
{
if (this->customImgWidth == w && this->customImgHeight == h)
{
return;
}
this->customImgWidth = w;
this->customImgHeight = h;
this->refreshPixmap();
}
void TooltipWidget::hideEvent(QHideEvent *)
{
this->clearImage();
}
void TooltipWidget::showEvent(QShowEvent *)
{
this->adjustSize();
}
bool TooltipWidget::refreshPixmap()
{
if (!this->image_)
{
return false;
}
auto pixmap = this->image_->pixmapOrLoad();
if (!pixmap)
{
this->attemptRefresh = true;
return false;
}
if (this->customImgWidth > 0 || this->customImgHeight > 0)
{
this->displayImage_->setPixmap(pixmap->scaled(
this->customImgWidth, this->customImgHeight, Qt::KeepAspectRatio));
}
else
{
this->displayImage_->setPixmap(*pixmap);
}
this->displayImage_->show();
this->displayImage_->setPixmap(image);
return true;
}
void TooltipWidget::changeEvent(QEvent *)