Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: merge TooltipPreviewImage and TooltipWidget #4268

Merged
merged 7 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
- Bugfix: Fixed crash that could occur when changing Tab layout and utilizing multiple windows. (#4248)
- Bugfix: Fixed text sometimes not pasting properly when image uploader was disabled. (#4246)
- Bugfix: Fixed text cursor(caret) not showing in open channel dialog. (#4196)
- Bugfix: Fixed tooltip images not appearing if mouse hovered only first pixel. (#4268)
- Dev: Remove protocol from QApplication's Organization Domain (so changed from `https://www.chatterino.com` to `chatterino.com`). (#4256)
- Dev: Ignore `WM_SHOWWINDOW` hide events, causing fewer attempted rescales. (#4198)
- Dev: Migrated to C++ 20 (#4252, #4257)
- Dev: Enable LTO for main branch builds. (#4258, #4260)
- Dev: Removed unused include directives. (#4266)
- Dev: Removed TooltipPreviewImage. (#4268)
kornes marked this conversation as resolved.
Show resolved Hide resolved
- Dev: Removed unused operators in `Image` (#4267)

## 2.4.0
Expand Down
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,6 @@ set(SOURCE_FILES
singletons/Theme.hpp
singletons/Toasts.cpp
singletons/Toasts.hpp
singletons/TooltipPreviewImage.cpp
singletons/TooltipPreviewImage.hpp
singletons/Updates.cpp
singletons/Updates.hpp
singletons/WindowManager.cpp
Expand Down
81 changes: 0 additions & 81 deletions src/singletons/TooltipPreviewImage.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions src/singletons/TooltipPreviewImage.hpp

This file was deleted.

119 changes: 89 additions & 30 deletions src/widgets/TooltipWidget.cpp
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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();
kornes marked this conversation as resolved.
Show resolved Hide resolved

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()
{
Expand Down Expand Up @@ -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(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::setImage(QPixmap image)
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;
kornes marked this conversation as resolved.
Show resolved Hide resolved
}

void TooltipWidget::changeEvent(QEvent *)
Expand Down
25 changes: 17 additions & 8 deletions src/widgets/TooltipWidget.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "messages/Image.hpp"
#include "widgets/BaseWindow.hpp"

#include <pajlada/signals/signal.hpp>
#include <pajlada/signals/signalholder.hpp>
#include <QLabel>
#include <QWidget>

Expand All @@ -16,18 +17,17 @@ class TooltipWidget : public BaseWindow
static TooltipWidget *instance();

TooltipWidget(BaseWidget *parent = nullptr);
~TooltipWidget() override;
~TooltipWidget() override = default;

void setText(QString text);
void setWordWrap(bool wrap);
void clearImage();
void setImage(QPixmap image);

#ifdef USEWINSDK
void raise();
#endif
void setImage(ImagePtr image);
void setImageScale(int w, int h);

protected:
void showEvent(QShowEvent *) override;
void hideEvent(QHideEvent *) override;
void changeEvent(QEvent *) override;
void leaveEvent(QEvent *) override;
void themeChangedEvent() override;
Expand All @@ -37,9 +37,18 @@ class TooltipWidget : public BaseWindow
private:
void updateFont();

// used by WindowManager::gifRepaintRequested signal to progress frames when tooltip image is animated
bool refreshPixmap();

// set to true when tooltip image did not finish loading yet (pixmapOrLoad returned false)
bool attemptRefresh{false};

ImagePtr image_ = nullptr;
int customImgWidth = 0;
int customImgHeight = 0;
QLabel *displayImage_;
QLabel *displayText_;
pajlada::Signals::Connection fontChangedConnection_;
pajlada::Signals::SignalHolder connections_;
};

} // namespace chatterino
Loading