1/*
2 SPDX-FileCopyrightText: 2000 Peter Putzer <putzer@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "kurllabel.h"
8
9#include <QApplication>
10#include <QMouseEvent>
11#include <QPalette>
12#include <QTimer>
13
14class KUrlLabelPrivate
15{
16public:
17 KUrlLabelPrivate(const QString &_url, KUrlLabel *_parent)
18 : parent(_parent)
19 , url(_url)
20 , tipText(url)
21 , linkColor(_parent->palette().color(cg: QPalette::Active, cr: QPalette::Link))
22 , highlightedLinkColor(_parent->palette().color(cg: QPalette::Active, cr: QPalette::BrightText))
23 , cursor(nullptr)
24 , textUnderlined(true)
25 , realUnderlined(true)
26 , useTips(false)
27 , useCursor(false)
28 , glowEnabled(true)
29 , floatEnabled(false)
30 , timer(new QTimer(parent))
31 {
32 QObject::connect(sender: timer, signal: &QTimer::timeout, context: parent, slot: [this]() {
33 updateColor();
34 });
35 }
36
37 void updateColor()
38 {
39 timer->stop();
40
41 if (!(glowEnabled || floatEnabled) || !parent->rect().contains(p: parent->mapFromGlobal(QCursor::pos()))) {
42 setLinkColor(linkColor);
43 }
44 }
45
46 void setLinkColor(const QColor &color)
47 {
48 QPalette palette = parent->palette();
49 palette.setColor(acr: QPalette::WindowText, acolor: color);
50 parent->setPalette(palette);
51
52 parent->update();
53 }
54
55 KUrlLabel *parent;
56
57 QString url;
58 QString tipText;
59 QColor linkColor;
60 QColor highlightedLinkColor;
61 QCursor *cursor;
62 bool textUnderlined : 1;
63 bool realUnderlined : 1;
64 bool useTips : 1;
65 bool useCursor : 1;
66 bool glowEnabled : 1;
67 bool floatEnabled : 1;
68 QPixmap alternatePixmap;
69 QPixmap realPixmap;
70 QTimer *timer;
71};
72
73KUrlLabel::KUrlLabel(const QString &url, const QString &text, QWidget *parent)
74 : QLabel(!text.isNull() ? text : url, parent)
75 , d(new KUrlLabelPrivate(url, this))
76{
77 setFont(font());
78 setCursor(QCursor(Qt::PointingHandCursor));
79 d->setLinkColor(d->linkColor);
80}
81
82KUrlLabel::KUrlLabel(QWidget *parent)
83 : QLabel(parent)
84 , d(new KUrlLabelPrivate(QString(), this))
85{
86 setFont(font());
87 setCursor(QCursor(Qt::PointingHandCursor));
88 d->setLinkColor(d->linkColor);
89}
90
91KUrlLabel::~KUrlLabel() = default;
92
93void KUrlLabel::mouseReleaseEvent(QMouseEvent *event)
94{
95 QLabel::mouseReleaseEvent(ev: event);
96
97 d->setLinkColor(d->highlightedLinkColor);
98 d->timer->start(msec: 300);
99
100 switch (event->button()) {
101 case Qt::LeftButton:
102 Q_EMIT leftClickedUrl();
103 break;
104
105 case Qt::MiddleButton:
106 Q_EMIT middleClickedUrl();
107 break;
108
109 case Qt::RightButton:
110 Q_EMIT rightClickedUrl();
111 break;
112
113 default:
114 break;
115 }
116}
117
118void KUrlLabel::setFont(const QFont &font)
119{
120 QFont newFont = font;
121 newFont.setUnderline(d->textUnderlined);
122
123 QLabel::setFont(newFont);
124}
125
126void KUrlLabel::setUnderline(bool on)
127{
128 d->textUnderlined = on;
129
130 setFont(font());
131}
132
133void KUrlLabel::setUrl(const QString &url)
134{
135 if (d->tipText == d->url) { // update the tip as well
136 d->tipText = url;
137 setUseTips(d->useTips);
138 }
139
140 d->url = url;
141}
142
143QString KUrlLabel::url() const
144{
145 return d->url;
146}
147
148void KUrlLabel::setUseCursor(bool on, QCursor *cursor)
149{
150 d->useCursor = on;
151 d->cursor = cursor;
152
153 if (on) {
154 if (cursor) {
155 setCursor(*cursor);
156 } else {
157 setCursor(QCursor(Qt::PointingHandCursor));
158 }
159 } else {
160 unsetCursor();
161 }
162}
163
164bool KUrlLabel::useCursor() const
165{
166 return d->useCursor;
167}
168
169void KUrlLabel::setUseTips(bool on)
170{
171 d->useTips = on;
172
173 if (on) {
174 setToolTip(d->tipText);
175 } else {
176 setToolTip(QString());
177 }
178}
179
180void KUrlLabel::setTipText(const QString &tipText)
181{
182 d->tipText = tipText;
183
184 setUseTips(d->useTips);
185}
186
187bool KUrlLabel::useTips() const
188{
189 return d->useTips;
190}
191
192QString KUrlLabel::tipText() const
193{
194 return d->tipText;
195}
196
197void KUrlLabel::setHighlightedColor(const QColor &color)
198{
199 d->linkColor = color;
200
201 if (!d->timer->isActive()) {
202 d->setLinkColor(color);
203 }
204}
205
206void KUrlLabel::setHighlightedColor(const QString &color)
207{
208 setHighlightedColor(QColor(color));
209}
210
211void KUrlLabel::setSelectedColor(const QColor &color)
212{
213 d->highlightedLinkColor = color;
214
215 if (d->timer->isActive()) {
216 d->setLinkColor(color);
217 }
218}
219
220void KUrlLabel::setSelectedColor(const QString &color)
221{
222 setSelectedColor(QColor(color));
223}
224
225void KUrlLabel::setGlowEnabled(bool glowEnabled)
226{
227 d->glowEnabled = glowEnabled;
228}
229
230void KUrlLabel::setFloatEnabled(bool floatEnabled)
231{
232 d->floatEnabled = floatEnabled;
233}
234
235bool KUrlLabel::isGlowEnabled() const
236{
237 return d->glowEnabled;
238}
239
240bool KUrlLabel::isFloatEnabled() const
241{
242 return d->floatEnabled;
243}
244
245void KUrlLabel::setAlternatePixmap(const QPixmap &pixmap)
246{
247 d->alternatePixmap = pixmap;
248}
249
250const QPixmap *KUrlLabel::alternatePixmap() const
251{
252 return &d->alternatePixmap;
253}
254
255void KUrlLabel::enterEvent(QEnterEvent *event)
256{
257 QLabel::enterEvent(event);
258
259 if (!d->alternatePixmap.isNull()) {
260 const auto currentPixmap = pixmap(Qt::ReturnByValue);
261 if (!currentPixmap.isNull()) {
262 d->realPixmap = currentPixmap;
263 setPixmap(d->alternatePixmap);
264 }
265 }
266
267 if (d->glowEnabled || d->floatEnabled) {
268 d->timer->stop();
269
270 d->setLinkColor(d->highlightedLinkColor);
271
272 d->realUnderlined = d->textUnderlined;
273
274 if (d->floatEnabled) {
275 setUnderline(true);
276 }
277 }
278 Q_EMIT enteredUrl();
279}
280
281void KUrlLabel::leaveEvent(QEvent *event)
282{
283 QLabel::leaveEvent(event);
284
285 if (!d->alternatePixmap.isNull() && !pixmap(Qt::ReturnByValue).isNull()) {
286 setPixmap(d->realPixmap);
287 }
288
289 if ((d->glowEnabled || d->floatEnabled) && !d->timer->isActive()) {
290 d->setLinkColor(d->linkColor);
291 }
292
293 setUnderline(d->realUnderlined);
294 Q_EMIT leftUrl();
295}
296
297bool KUrlLabel::event(QEvent *event)
298{
299 if (event->type() == QEvent::PaletteChange) {
300 // Use parentWidget() unless you are a toplevel widget, then try qAapp
301 QPalette palette = parentWidget() ? parentWidget()->palette() : qApp->palette();
302
303 palette.setBrush(acr: QPalette::Base, abrush: palette.brush(cg: QPalette::Normal, cr: QPalette::Window));
304 palette.setColor(acr: QPalette::WindowText, acolor: this->palette().color(cg: QPalette::Active, cr: QPalette::WindowText));
305 setPalette(palette);
306
307 d->linkColor = palette.color(cg: QPalette::Active, cr: QPalette::Link);
308 d->updateColor();
309
310 return true;
311 } else {
312 return QLabel::event(e: event);
313 }
314}
315
316#include "moc_kurllabel.cpp"
317

source code of kwidgetsaddons/src/kurllabel.cpp