| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <optional> |
| 7 |
|
| 8 |
#include <QString> |
| 9 |
|
| 10 |
#include "DolphinQt/Config/ToolTipControls/BalloonTip.h" |
| 11 |
|
| 12 |
class QEnterEvent; |
| 13 |
class QEvent; |
| 14 |
class QHideEvent; |
| 15 |
class QTimerEvent; |
| 16 |
|
| 17 |
constexpr int TOOLTIP_DELAY = 300; |
| 18 |
|
| 19 |
template <class Derived> |
| 20 |
class ToolTipWidget : public Derived |
| 21 |
{ |
| 22 |
public: |
| 23 |
using Derived::Derived; |
| 24 |
|
| 25 |
void SetTitle(QString title) { m_title = std::move(title); } |
| 26 |
|
| 27 |
void SetDescription(QString description) { m_description = std::move(description); } |
| 28 |
|
| 29 |
private: |
| 30 |
void enterEvent(QEnterEvent* const event) override |
| 31 |
{ |
| 32 |
|
| 33 |
|
| 34 |
if (!m_timer_id && !BalloonTip::IsWidgetBalloonTipActive(*this)) |
| 35 |
m_timer_id = this->startTimer(TOOLTIP_DELAY); |
| 36 |
|
| 37 |
Derived::enterEvent(event); |
| 38 |
} |
| 39 |
|
| 40 |
void leaveEvent(QEvent* const event) override |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
if (!BalloonTip::IsCursorInsideWidgetBoundingBox(*this) || !BalloonTip::IsCursorOnBalloonTip()) |
| 46 |
KillTimerAndHideBalloon(); |
| 47 |
|
| 48 |
Derived::leaveEvent(event); |
| 49 |
} |
| 50 |
|
| 51 |
void hideEvent(QHideEvent* const event) override |
| 52 |
{ |
| 53 |
KillTimerAndHideBalloon(); |
| 54 |
|
| 55 |
Derived::hideEvent(event); |
| 56 |
} |
| 57 |
|
| 58 |
void timerEvent(QTimerEvent* const event) override |
| 59 |
{ |
| 60 |
this->killTimer(*m_timer_id); |
| 61 |
m_timer_id.reset(); |
| 62 |
|
| 63 |
BalloonTip::ShowBalloon(m_title, m_description, |
| 64 |
this->parentWidget()->mapToGlobal(GetToolTipPosition()), this); |
| 65 |
|
| 66 |
Derived::timerEvent(event); |
| 67 |
} |
| 68 |
|
| 69 |
virtual QPoint GetToolTipPosition() const = 0; |
| 70 |
|
| 71 |
void KillTimerAndHideBalloon() |
| 72 |
{ |
| 73 |
if (m_timer_id) |
| 74 |
{ |
| 75 |
this->killTimer(*m_timer_id); |
| 76 |
m_timer_id.reset(); |
| 77 |
} |
| 78 |
BalloonTip::HideBalloon(); |
| 79 |
} |
| 80 |
|
| 81 |
std::optional<int> m_timer_id; |
| 82 |
QString m_title; |
| 83 |
QString m_description; |
| 84 |
}; |