Add simple tooltip system

This commit is contained in:
Rasmus Karlsson
2017-12-19 03:36:05 +01:00
parent 47a1911df9
commit 4b36893818
5 changed files with 41 additions and 14 deletions
+28
View File
@@ -16,6 +16,7 @@
#include <QDesktopServices>
#include <QGraphicsBlurEffect>
#include <QPainter>
#include <QToolTip>
#include <math.h>
#include <algorithm>
@@ -748,13 +749,40 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
this->repaint();
}
static QTimer *tooltipTimer = new QTimer();
// check if word underneath cursor
const messages::Word *hoverWord;
if ((hoverWord = message->tryGetWordPart(relativePos)) == nullptr) {
this->setCursor(Qt::ArrowCursor);
tooltipTimer->stop();
QToolTip::hideText();
return;
}
const auto &tooltip = hoverWord->getTooltip();
static QString targetTooltip;
static QPoint tooltipPos;
connect(tooltipTimer, &QTimer::timeout, []() {
assert(!targetTooltip.isEmpty());
QToolTip::showText(tooltipPos, targetTooltip); //
});
tooltipTimer->setSingleShot(true);
tooltipTimer->setInterval(500);
if (!tooltip.isEmpty()) {
tooltipPos = event->globalPos();
targetTooltip = "<style>.center { text-align: center; }</style>"
"<p class = \"center\">" +
tooltip + "</p>";
tooltipTimer->start();
} else {
tooltipTimer->stop();
QToolTip::hideText();
}
// check if word has a link
if (hoverWord->getLink().isValid()) {
this->setCursor(Qt::PointingHandCursor);