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>
|
2023-03-18 17:30:08 +01:00
|
|
|
|
|
|
|
// number of columns in grid mode
|
|
|
|
#define GRID_NUM_COLS 3
|
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)
|
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);
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
// Default to using vertical layout
|
|
|
|
this->initializeVLayout();
|
|
|
|
this->setLayout(this->vLayout_);
|
|
|
|
this->currentStyle_ = TooltipStyle::Vertical;
|
2019-07-23 22:18:36 +02:00
|
|
|
|
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] {
|
2023-03-18 17:30:08 +01:00
|
|
|
for (int i = 0; i < this->visibleEntries_; ++i)
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
auto entry = this->entryAt(i);
|
|
|
|
if (entry && entry->animated())
|
|
|
|
{
|
|
|
|
entry->refreshPixmap();
|
|
|
|
}
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
});
|
2019-07-23 22:18:36 +02:00
|
|
|
|
2022-12-29 16:07:46 +01:00
|
|
|
this->connections_.managedConnect(windows->miscUpdate, [this] {
|
2023-03-18 17:30:08 +01:00
|
|
|
bool needSizeAdjustment = false;
|
|
|
|
for (int i = 0; i < this->visibleEntries_; ++i)
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
auto entry = this->entryAt(i);
|
|
|
|
if (entry->hasImage() && entry->attemptRefresh())
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
bool successfullyUpdated = entry->refreshPixmap();
|
|
|
|
needSizeAdjustment |= successfullyUpdated;
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-18 17:30:08 +01:00
|
|
|
|
|
|
|
if (needSizeAdjustment)
|
|
|
|
{
|
|
|
|
this->adjustSize();
|
|
|
|
}
|
2022-12-29 16:07:46 +01:00
|
|
|
});
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::setOne(const TooltipEntry &entry, TooltipStyle style)
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
this->set({entry}, style);
|
2019-08-21 02:37:10 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::set(const std::vector<TooltipEntry> &entries,
|
|
|
|
TooltipStyle style)
|
2019-08-21 02:37:10 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
this->setCurrentStyle(style);
|
2019-08-21 02:37:10 +02:00
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
int delta = entries.size() - this->currentLayoutCount();
|
|
|
|
if (delta > 0)
|
|
|
|
{
|
|
|
|
// Need to add more TooltipEntry instances
|
|
|
|
int base = this->currentLayoutCount();
|
|
|
|
for (int i = 0; i < delta; ++i)
|
|
|
|
{
|
|
|
|
this->addNewEntry(base + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setVisibleEntries(entries.size());
|
|
|
|
|
|
|
|
for (int i = 0; i < entries.size(); ++i)
|
|
|
|
{
|
|
|
|
if (auto entryWidget = this->entryAt(i))
|
|
|
|
{
|
|
|
|
auto &entry = entries[i];
|
|
|
|
entryWidget->setImage(entry.image);
|
|
|
|
entryWidget->setText(entry.text);
|
|
|
|
entryWidget->setImageScale(entry.customWidth, entry.customHeight);
|
|
|
|
}
|
|
|
|
}
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::setVisibleEntries(int n)
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
for (int i = 0; i < this->currentLayoutCount(); ++i)
|
|
|
|
{
|
|
|
|
auto *entry = this->entryAt(i);
|
|
|
|
if (entry == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= n)
|
|
|
|
{
|
|
|
|
entry->hide();
|
|
|
|
entry->clearImage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entry->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->visibleEntries_ = n;
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::addNewEntry(int absoluteIndex)
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
switch (this->currentStyle_)
|
|
|
|
{
|
|
|
|
case TooltipStyle::Vertical:
|
|
|
|
this->vLayout_->addWidget(new TooltipEntryWidget(),
|
|
|
|
Qt::AlignHCenter);
|
|
|
|
return;
|
|
|
|
case TooltipStyle::Grid:
|
|
|
|
if (absoluteIndex == 0)
|
|
|
|
{
|
|
|
|
// Top row spans all columns
|
|
|
|
this->gLayout_->addWidget(new TooltipEntryWidget(), 0, 0, 1,
|
|
|
|
GRID_NUM_COLS, Qt::AlignCenter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int row = ((absoluteIndex - 1) / GRID_NUM_COLS) + 1;
|
|
|
|
int col = (absoluteIndex - 1) % GRID_NUM_COLS;
|
|
|
|
this->gLayout_->addWidget(new TooltipEntryWidget(), row, col,
|
|
|
|
Qt::AlignHCenter | Qt::AlignBottom);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
// May be nullptr
|
|
|
|
QLayout *TooltipWidget::currentLayout() const
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
switch (this->currentStyle_)
|
|
|
|
{
|
|
|
|
case TooltipStyle::Vertical:
|
|
|
|
return this->vLayout_;
|
|
|
|
case TooltipStyle::Grid:
|
|
|
|
return this->gLayout_;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
int TooltipWidget::currentLayoutCount() const
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
if (auto *layout = this->currentLayout())
|
|
|
|
{
|
|
|
|
return layout->count();
|
|
|
|
}
|
|
|
|
return 0;
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
// May be nullptr
|
|
|
|
TooltipEntryWidget *TooltipWidget::entryAt(int n)
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
if (auto *layout = this->currentLayout())
|
|
|
|
{
|
|
|
|
return dynamic_cast<TooltipEntryWidget *>(layout->itemAt(n)->widget());
|
|
|
|
}
|
|
|
|
return nullptr;
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::setCurrentStyle(TooltipStyle style)
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
if (this->currentStyle_ == style)
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
// Nothing to update
|
2022-12-29 16:07:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-03-18 17:30:08 +01:00
|
|
|
|
|
|
|
this->clearEntries();
|
|
|
|
this->deleteCurrentLayout();
|
|
|
|
|
|
|
|
switch (style)
|
|
|
|
{
|
|
|
|
case TooltipStyle::Vertical:
|
|
|
|
this->initializeVLayout();
|
|
|
|
this->setLayout(this->vLayout_);
|
|
|
|
break;
|
|
|
|
case TooltipStyle::Grid:
|
|
|
|
this->initializeGLayout();
|
|
|
|
this->setLayout(this->gLayout_);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->currentStyle_ = style;
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::deleteCurrentLayout()
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
auto *currentLayout = this->layout();
|
|
|
|
delete currentLayout;
|
|
|
|
|
|
|
|
switch (this->currentStyle_)
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
case TooltipStyle::Vertical:
|
|
|
|
this->vLayout_ = nullptr;
|
|
|
|
break;
|
|
|
|
case TooltipStyle::Grid:
|
|
|
|
this->gLayout_ = nullptr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
2019-07-23 22:18:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::initializeVLayout()
|
2019-07-23 22:18:36 +02:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
auto *vLayout = new QVBoxLayout(this);
|
|
|
|
vLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
|
|
vLayout->setContentsMargins(10, 5, 10, 5);
|
|
|
|
vLayout->setSpacing(10);
|
|
|
|
this->vLayout_ = vLayout;
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::initializeGLayout()
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
auto *gLayout = new QGridLayout(this);
|
|
|
|
gLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
|
|
gLayout->setContentsMargins(10, 5, 10, 5);
|
|
|
|
gLayout->setHorizontalSpacing(8);
|
|
|
|
gLayout->setVerticalSpacing(10);
|
|
|
|
this->gLayout_ = gLayout;
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::themeChangedEvent()
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
// this->setStyleSheet("color: #fff; background: #000");
|
|
|
|
}
|
2022-12-29 16:07:46 +01:00
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
2022-12-29 16:07:46 +01:00
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
painter.fillRect(this->rect(), QColor(0, 0, 0, int(0.8 * 255)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::scaleChangedEvent(float)
|
|
|
|
{
|
|
|
|
this->updateFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::updateFont()
|
|
|
|
{
|
|
|
|
this->setFont(
|
|
|
|
getFonts()->getFont(FontStyle::ChatMediumSmall, this->scale()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::setWordWrap(bool wrap)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < this->visibleEntries_; ++i)
|
2022-12-29 16:07:46 +01:00
|
|
|
{
|
2023-03-18 17:30:08 +01:00
|
|
|
auto entry = this->entryAt(i);
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
entry->setWordWrap(wrap);
|
|
|
|
}
|
2022-12-29 16:07:46 +01:00
|
|
|
}
|
2023-03-18 17:30:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::clearEntries()
|
|
|
|
{
|
|
|
|
this->setVisibleEntries(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TooltipWidget::hideEvent(QHideEvent *)
|
|
|
|
{
|
|
|
|
this->clearEntries();
|
|
|
|
}
|
2022-12-29 16:07:46 +01:00
|
|
|
|
2023-03-18 17:30:08 +01:00
|
|
|
void TooltipWidget::showEvent(QShowEvent *)
|
|
|
|
{
|
|
|
|
this->adjustSize();
|
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
|