-
Notifications
You must be signed in to change notification settings - Fork 0
/
topiclabel.cpp
72 lines (61 loc) · 2.18 KB
/
topiclabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
This file is part of NFK Lobby.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#include "Pages/basicpage.hpp"
#include "topiclabel.hpp"
#include <QEvent>
#include <QRegularExpression>
namespace Chat {
TopicLabel::TopicLabel(QWidget* parent) : QLabel(parent)
{
setWordWrap(true);
setAttribute(Qt::WA_Hover);
setTextFormat(Qt::RichText);
setTextInteractionFlags(textInteractionFlags() | Qt::LinksAccessibleByMouse);
setOpenExternalLinks(true);
setMinimumHeight(1);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
bool TopicLabel::event(QEvent* event)
{
if (event->type() == QEvent::HoverEnter) {
/*FIXME: isn't very precise since UrlText, if it does contain URLs, has bigger width than planText does*/
int hiddenLines = fontMetrics().width(plainText)/width();
if (hiddenLines > 0) {
setFixedHeight((hiddenLines+1)*fontMetrics().height());
}
return true;
}
if (event->type() == QEvent::HoverLeave) {
setFixedHeight(fontMetrics().height());
return true;
}
if (event->type() == QEvent::Resize) {
QLabel::setText(UrlText);
}
return QLabel::event(event);
}
void TopicLabel::setText(const QString &newText)
{
setFixedHeight(fontMetrics().height());
plainText = newText;
BasicPage::eraseControlCharacters(plainText);
UrlText = plainText.toHtmlEscaped();
UrlText.prepend("<style>a {text-decoration:none; color:#2266ff;}</style>");
UrlText.replace(QRegularExpression("((?:https?|ftp)://\\S+)"), "<a href=\"\\1\">\\1</a>");
QLabel::setText(UrlText);
}
QSize TopicLabel::sizeHint() const
{
return QSize(1, 1);
}
} // namespace Chat