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

View file

@ -78,7 +78,8 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
link = link.replace("{{id}}", id).replace("{{image}}", "1x");
auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] {
return EmoteData(new LazyLoadedImage(link, 1, code, code + "\nChannel BTTV Emote"));
return EmoteData(
new LazyLoadedImage(link, 1, code, code + "<br/>Channel BTTV Emote"));
});
this->bttvChannelEmotes.insert(code, emote);
@ -127,7 +128,7 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
auto emote =
this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code, &url1] {
return EmoteData(
new LazyLoadedImage(url1, 1, code, code + "\nGlobal FFZ Emote"));
new LazyLoadedImage(url1, 1, code, code + "<br/>Global FFZ Emote"));
});
this->ffzChannelEmotes.insert(code, emote);
@ -222,7 +223,7 @@ void EmoteManager::loadEmojis()
code + ".png";
this->emojis.insert(code, EmoteData(new LazyLoadedImage(url, 0.35, ":" + shortCode + ":",
":" + shortCode + ":")));
":" + shortCode + ":<br/>Emoji")));
// TODO(pajlada): The vectors in emojiFirstByte need to be sorted by
// emojiData.code.length()
@ -430,7 +431,7 @@ void EmoteManager::loadBTTVEmotes()
QString url = tmp.replace("{{id}}", id).replace("{{image}}", "1x");
this->bttvGlobalEmotes.insert(
code, new LazyLoadedImage(url, 1, code, code + "\nGlobal BTTV Emote"));
code, new LazyLoadedImage(url, 1, code, code + "<br/>Global BTTV Emote"));
codes.push_back(code.toStdString());
}
@ -475,7 +476,7 @@ void EmoteManager::loadFFZEmotes()
QString url1 = "http:" + urls.value("1").toString();
this->ffzGlobalEmotes.insert(
code, new LazyLoadedImage(url1, 1, code, code + "\nGlobal FFZ Emote"));
code, new LazyLoadedImage(url1, 1, code, code + "<br/>Global FFZ Emote"));
codes.push_back(code.toStdString());
}
@ -495,7 +496,7 @@ EmoteData EmoteManager::getTwitchEmoteById(long id, const QString &emoteName)
return _twitchEmoteFromCache.getOrAdd(id, [this, &emoteName, &id] {
qreal scale;
QString url = getTwitchEmoteLink(id, scale);
return new LazyLoadedImage(url, scale, emoteName, emoteName + "\nTwitch Emote");
return new LazyLoadedImage(url, scale, emoteName, emoteName + "<br/>Twitch Emote");
});
}

View file

@ -101,7 +101,7 @@ Message *Message::createSystemMessage(const QString &text)
AddCurrentTimestamp(message);
Word word(text, Word::Type::Default, MessageColor(MessageColor::Type::System), text, text);
Word word(text, Word::Type::Default, MessageColor(MessageColor::Type::System), text, QString());
message->getWords().push_back(word);
@ -142,7 +142,7 @@ Message *Message::createTimeoutMessage(const QString &username, const QString &d
}
text.append(".");
Word word(text, Word::Type::Default, MessageColor(MessageColor::Type::System), text, text);
Word word(text, Word::Type::Default, MessageColor(MessageColor::Type::System), text, QString());
message->getWords().push_back(word);

View file

@ -93,9 +93,9 @@ public:
}
explicit Word(LazyLoadedImage *_image, Type getType, const QString &copytext,
const QString &getTooltip, const Link &getLink = Link());
const QString &tooltip, const Link &getLink = Link());
explicit Word(const QString &_text, Type getType, const MessageColor &getColor,
const QString &copytext, const QString &getTooltip, const Link &getLink = Link());
const QString &copytext, const QString &tooltip, const Link &getLink = Link());
LazyLoadedImage &getImage() const;
const QString &getText() const;

View file

@ -225,12 +225,10 @@ SharedMessage TwitchMessageBuilder::parse()
this->appendWord(Word(string, Word::Text, textColor, string, QString(), link));
} else { // is emoji
static QString emojiTooltip("Emoji");
this->appendWord(Word(emoteData.image, Word::EmojiImage, emoteData.image->getName(),
emojiTooltip));
emoteData.image->getTooltip()));
Word(emoteData.image->getName(), Word::EmojiText, textColor,
emoteData.image->getName(), emojiTooltip);
emoteData.image->getName(), emoteData.image->getTooltip());
}
}

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);