2022-12-31 15:41:01 +01:00
|
|
|
#include "widgets/TooltipWidget.hpp"
|
2019-07-23 22:18:36 +02:00
|
|
|
|
2022-12-29 16:07:46 +01:00
|
|
|
#include "Application.hpp"
|
2022-12-31 15:41:01 +01:00
|
|
|
#include "messages/Image.hpp"
|
2019-07-23 22:18:36 +02:00
|
|
|
#include "singletons/Fonts.hpp"
|
2022-12-29 16:07:46 +01:00
|
|
|
#include "singletons/WindowManager.hpp"
|
2019-07-23 22:18:36 +02:00
|
|
|
|
2021-05-08 15:57:00 +02:00
|
|
|
#include <QPainter>
|
2022-12-29 16:07:46 +01:00
|
|
|
#include <QVBoxLayout>
|
2021-05-08 15:57:00 +02:00
|
|
|
|
2019-10-07 15:46:08 +02:00
|
|
|
namespace chatterino {
|
2019-07-23 22:18:36 +02:00
|
|
|
|
2019-10-07 22:42:34 +02:00
|
|
|
TooltipWidget *TooltipWidget::instance()
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
|
|
|
static TooltipWidget *tooltipWidget = new TooltipWidget();
|
|
|
|
return tooltipWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
TooltipWidget::TooltipWidget(BaseWidget *parent)
|
2022-12-03 11:50:22 +01:00
|
|
|
: BaseWindow({BaseWindow::TopMost, BaseWindow::DontFocus,
|
|
|
|
BaseWindow::DisableLayoutSave},
|
|
|
|
parent)
|
2022-12-29 16:07:46 +01:00
|
|
|
, displayImage_(new QLabel(this))
|
|
|
|
, displayText_(new QLabel(this))
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2019-08-21 02:37:10 +02:00
|
|
|
this->setStyleSheet("color: #fff; background: rgba(11, 11, 11, 0.8)");
|
|
|
|
this->setAttribute(Qt::WA_TranslucentBackground);
|
2022-12-29 16:07:46 +01:00
|
|
|
this->setWindowFlag(Qt::WindowStaysOnTopHint, true);
|
|
|
|
|
2019-07-23 22:18:36 +02:00
|
|
|
this->setStayInScreenRect(true);
|
|
|
|
|
|
|
|
displayImage_->setAlignment(Qt::AlignHCenter);
|
2019-08-21 02:37:10 +02:00
|
|
|
displayImage_->setStyleSheet("background: transparent");
|
2022-12-29 16:07:46 +01:00
|
|
|
|
2019-07-23 22:18:36 +02:00
|
|
|
displayText_->setAlignment(Qt::AlignHCenter);
|
2019-08-21 02:37:10 +02:00
|
|
|
displayText_->setStyleSheet("background: transparent");
|
2022-12-29 16:07:46 +01:00
|
|
|
|
|
|
|
auto *layout = new QVBoxLayout(this);
|
|
|
|
layout->setSizeConstraint(QLayout::SetFixedSize);
|
2019-07-23 22:18:36 +02:00
|
|
|
layout->setContentsMargins(10, 5, 10, 5);
|
|
|
|
layout->addWidget(displayImage_);
|
|
|
|
layout->addWidget(displayText_);
|
|
|
|
this->setLayout(layout);
|
|
|
|
|
2022-12-29 16:07:46 +01:00
|
|
|
this->connections_.managedConnect(getFonts()->fontChanged, [this] {
|
2020-11-08 12:02:19 +01:00
|
|
|
this->updateFont();
|
|
|
|
});
|
2022-12-29 16:07:46 +01:00
|
|
|
this->updateFont();
|
2019-07-23 22:18:36 +02:00
|
|
|
|
2022-12-29 16:07:46 +01:00
|
|
|
auto windows = getApp()->windows;
|
|
|
|
this->connections_.managedConnect(windows->gifRepaintRequested, [this] {
|
|
|
|
if (this->image_ && this->image_->animated())
|
|
|
|
{
|
|
|
|
this->refreshPixmap();
|
|
|
|
}
|
|
|
|
});
|
2019-07-23 22:18:36 +02:00
|
|
|
|
2022-12-29 16:07:46 +01:00
|
|
|
this->connections_.managedConnect(windows->miscUpdate, [this] {
|
|
|
|
if (this->image_ && this->attemptRefresh)
|
|
|
|
{
|
|
|
|
if (this->refreshPixmap())
|
|
|
|
{
|
|
|
|
this->attemptRefresh = false;
|
|
|
|
this->adjustSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::themeChangedEvent()
|
|
|
|
{
|
2019-08-21 02:37:10 +02:00
|
|
|
// this->setStyleSheet("color: #fff; background: #000");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
|
|
|
|
painter.fillRect(this->rect(), QColor(0, 0, 0, int(0.8 * 255)));
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::scaleChangedEvent(float)
|
|
|
|
{
|
|
|
|
this->updateFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::updateFont()
|
|
|
|
{
|
|
|
|
this->setFont(
|
|
|
|
getFonts()->getFont(FontStyle::ChatMediumSmall, this->scale()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::setText(QString text)
|
|
|
|
{
|
|
|
|
this->displayText_->setText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::setWordWrap(bool wrap)
|
|
|
|
{
|
|
|
|
this->displayText_->setWordWrap(wrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::clearImage()
|
|
|
|
{
|
|
|
|
this->displayImage_->hide();
|
2022-12-29 16:07:46 +01:00
|
|
|
this->image_ = nullptr;
|
|
|
|
this->setImageScale(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2022-12-29 16:07:46 +01:00
|
|
|
void TooltipWidget::hideEvent(QHideEvent *)
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2022-12-29 16:07:46 +01:00
|
|
|
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);
|
|
|
|
}
|
2019-07-23 22:18:36 +02:00
|
|
|
this->displayImage_->show();
|
2022-12-29 16:07:46 +01:00
|
|
|
|
|
|
|
return true;
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::changeEvent(QEvent *)
|
|
|
|
{
|
|
|
|
// clear parents event
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::leaveEvent(QEvent *)
|
|
|
|
{
|
|
|
|
// clear parents event
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:46:08 +02:00
|
|
|
} // namespace chatterino
|