Implemented a new, better looking tooltip. (#158)

* Implemented a new, better looking tooltip.

* Pajlada fix.

* Fixed dragging behaving incorrectly.

* Fixed out of focus hovering getting stuck.
This commit is contained in:
Cranken
2017-12-23 22:17:38 +01:00
committed by pajlada
parent fc81b118c7
commit a617873f6a
11 changed files with 172 additions and 37 deletions
+19 -25
View File
@@ -10,13 +10,13 @@
#include "util/benchmark.hpp"
#include "util/distancebetweenpoints.hpp"
#include "widgets/split.hpp"
#include "widgets/tooltipwidget.hpp"
#include "windowmanager.hpp"
#include <QDebug>
#include <QDesktopServices>
#include <QGraphicsBlurEffect>
#include <QPainter>
#include <QToolTip>
#include <math.h>
#include <algorithm>
@@ -78,6 +78,16 @@ ChannelView::ChannelView(BaseWidget *parent)
this->updateTimer.start();
});
auto _split = this->parent();
auto _splitContainer = _split->parent();
auto _notebook = _splitContainer->parent();
auto _window = qobject_cast<Window*>(_notebook->parent());
assert(_window);
_window->lostFocus.connect(
[this] { TooltipWidget::getInstance()->hide(); });
}
ChannelView::~ChannelView()
@@ -728,6 +738,7 @@ void ChannelView::wheelEvent(QWheelEvent *event)
void ChannelView::mouseMoveEvent(QMouseEvent *event)
{
auto tooltipWidget = TooltipWidget::getInstance();
std::shared_ptr<messages::MessageRef> message;
QPoint relativePos;
int messageIndex;
@@ -735,12 +746,14 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
// no message under cursor
if (!tryGetMessageAt(event->pos(), message, relativePos, messageIndex)) {
this->setCursor(Qt::ArrowCursor);
tooltipWidget->hide();
return;
}
// message under cursor is collapsed
if (message->isCollapsed()) {
this->setCursor(Qt::PointingHandCursor);
tooltipWidget->hide();
return;
}
@@ -753,38 +766,19 @@ 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();
tooltipWidget->hide();
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();
if (hoverWord->isImage()) {
tooltipWidget->moveTo(event->globalPos());
tooltipWidget->setText(tooltip);
tooltipWidget->show();
}
// check if word has a link
-1
View File
@@ -164,7 +164,6 @@ private:
boost::signals2::connection layoutConnection;
std::vector<pajlada::Signals::ScopedConnection> managedConnections;
private slots:
void wordTypeMaskChanged()
{
+32 -10
View File
@@ -4,6 +4,7 @@
#include "util/urlfetch.hpp"
#include "widgets/split.hpp"
#include "widgets/splitcontainer.hpp"
#include "widgets/tooltipwidget.hpp"
#include <QByteArray>
#include <QDrag>
@@ -21,6 +22,11 @@ SplitHeader::SplitHeader(Split *_chatWidget)
, rightLabel(this)
, rightMenu(this)
{
this->setMouseTracking(true);
this->leftLabel.setMouseTracking(true);
this->channelNameLabel.setMouseTracking(true);
this->rightLabel.setMouseTracking(true);
this->refreshTheme();
this->updateChannelText();
@@ -106,19 +112,21 @@ void SplitHeader::updateChannelText()
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
if (twitchChannel != nullptr && twitchChannel->isLive) {
this->isLive = true;
this->tooltip = "<style>.center { text-align: center; }</style>"
"<p class = \"center\">" +
twitchChannel->streamStatus + "<br><br>" + twitchChannel->streamGame +
"<br>"
"Live for " +
twitchChannel->streamUptime + " with " +
twitchChannel->streamViewerCount +
" viewers"
"</p>";
this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)");
this->setToolTip("<style>.center { text-align: center; }</style>"
"<p class = \"center\">" +
twitchChannel->streamStatus + "<br><br>" + twitchChannel->streamGame +
"<br>"
"Live for " +
twitchChannel->streamUptime + " with " +
twitchChannel->streamViewerCount +
" viewers"
"</p>");
} else {
this->isLive = false;
this->channelNameLabel.setText(QString::fromStdString(channelName));
this->setToolTip("");
this->tooltip = "";
}
}
}
@@ -141,6 +149,13 @@ void SplitHeader::mousePressEvent(QMouseEvent *event)
void SplitHeader::mouseMoveEvent(QMouseEvent *event)
{
if (!this->dragging && this->isLive) {
auto tooltipWidget = TooltipWidget::getInstance();
tooltipWidget->moveTo(event->globalPos());
tooltipWidget->setText(tooltip);
tooltipWidget->show();
}
if (this->dragging) {
if (std::abs(this->dragStart.x() - event->pos().x()) > 12 ||
std::abs(this->dragStart.y() - event->pos().y()) > 12) {
@@ -168,11 +183,18 @@ void SplitHeader::mouseMoveEvent(QMouseEvent *event)
}
SplitContainer::isDraggingSplit = false;
this->dragging = false;
}
}
}
}
void SplitHeader::leaveEvent(QEvent *event)
{
TooltipWidget::getInstance()->hide();
BaseWidget::leaveEvent(event);
}
void SplitHeader::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
+4
View File
@@ -35,6 +35,7 @@ protected:
virtual void paintEvent(QPaintEvent *) override;
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void leaveEvent(QEvent *event) override;
virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
virtual void resizeEvent(QResizeEvent *event) override;
@@ -66,6 +67,9 @@ private:
void initializeChannelSignals();
QString tooltip;
bool isLive;
public slots:
void menuMoveSplit();
void menuReloadChannelEmotes();