KWidgetsAddons

kurllabel.cpp
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(QPalette::Active, QPalette::Link))
22 , highlightedLinkColor(_parent->palette().color(QPalette::Active, 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(timer, &QTimer::timeout, parent, [this]() {
33 updateColor();
34 });
35 }
36
37 void updateColor()
38 {
39 timer->stop();
40
41 if (!(glowEnabled || floatEnabled) || !parent->rect().contains(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(QPalette::WindowText, 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());
79 d->setLinkColor(d->linkColor);
80}
81
83 : QLabel(parent)
84 , d(new KUrlLabelPrivate(QString(), this))
85{
86 setFont(font());
88 d->setLinkColor(d->linkColor);
89}
90
91KUrlLabel::~KUrlLabel() = default;
92
94{
96
97 d->setLinkColor(d->highlightedLinkColor);
98 d->timer->start(300);
99
100 switch (event->button()) {
101 case Qt::LeftButton:
103 break;
104
105 case Qt::MiddleButton:
107 break;
108
109 case Qt::RightButton:
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
127{
128 d->textUnderlined = on;
129
130 setFont(font());
131}
132
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) {
156 } else {
158 }
159 } else {
160 unsetCursor();
161 }
162}
163
164bool KUrlLabel::useCursor() const
165{
166 return d->useCursor;
167}
168
170{
171 d->useTips = on;
172
173 if (on) {
174 setToolTip(d->tipText);
175 } else {
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
198{
199 d->linkColor = color;
200
201 if (!d->timer->isActive()) {
202 d->setLinkColor(color);
203 }
204}
205
207{
209}
210
212{
213 d->highlightedLinkColor = color;
214
215 if (d->timer->isActive()) {
216 d->setLinkColor(color);
217 }
218}
219
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
236{
237 return d->glowEnabled;
238}
239
241{
242 return d->floatEnabled;
243}
244
246{
247 d->alternatePixmap = pixmap;
248}
249
250const QPixmap *KUrlLabel::alternatePixmap() const
251{
252 return &d->alternatePixmap;
253}
254
256{
258
259 if (!d->alternatePixmap.isNull()) {
260 const auto currentPixmap = pixmap();
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 }
279}
280
282{
284
285 if (!d->alternatePixmap.isNull() && !pixmap().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
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
306
307 d->linkColor = palette.color(QPalette::Active, QPalette::Link);
308 d->updateColor();
309
310 return true;
311 } else {
312 return QLabel::event(event);
313 }
314}
315
316#include "moc_kurllabel.cpp"
A drop-in replacement for QLabel that displays hyperlinks.
Definition kurllabel.h:61
void enterEvent(QEnterEvent *event) override
Overridden for internal reasons; the API remains unaffected.
void setUnderline(bool on=true)
Turns on or off the underlining.
void leaveEvent(QEvent *) override
Overridden for internal reasons; the API remains unaffected.
void rightClickedUrl()
Emitted when the user clicked the right mouse button on this label.
void setUrl(const QString &url)
Sets the URL for this label to url.
void setFloatEnabled(bool do_float=true)
Turns on or off the "float" feature.
void setUseCursor(bool on, QCursor *cursor=nullptr)
Turns the custom cursor feature on or off.
bool isFloatEnabled() const
This feature is very similar to the "glow" feature in that the color of the label switches to the sel...
KUrlLabel(QWidget *parent=nullptr)
Default constructor.
Definition kurllabel.cpp:82
virtual void setFont(const QFont &font)
Overridden for internal reasons; the API remains unaffected.
bool isGlowEnabled() const
When this is on, the text will switch to the selected color whenever the mouse passes over it.
void enteredUrl()
Emitted when the mouse has passed over the label.
void setUseTips(bool on=true)
Turns on or off the tool tip feature.
void setTipText(const QString &tip)
Specifies what text to display when tooltips are turned on.
void setAlternatePixmap(const QPixmap &pixmap)
Sets the "alt" pixmap.
~KUrlLabel() override
Destructs the label.
void setGlowEnabled(bool glow=true)
Turns on or off the "glow" feature.
bool event(QEvent *) override
Catch parent palette changes.
void setSelectedColor(const QColor &color)
Sets the selected color.
void middleClickedUrl()
Emitted when the user clicked the left mouse button on this label.
void leftClickedUrl()
Emitted when the user clicked the left mouse button on this label.
void mouseReleaseEvent(QMouseEvent *) override
Overridden for internal reasons; the API remains unaffected.
Definition kurllabel.cpp:93
void leftUrl()
Emitted when the mouse is no longer over the label.
void setHighlightedColor(const QColor &highcolor)
Sets the highlight color.
QPoint pos()
void setUnderline(bool enable)
virtual bool event(QEvent *e) override
virtual void mouseReleaseEvent(QMouseEvent *ev) override
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setColor(ColorGroup group, ColorRole role, const QColor &color)
PointingHandCursor
LeftButton
void stop()
void timeout()
void setCursor(const QCursor &)
virtual void enterEvent(QEnterEvent *event)
virtual void leaveEvent(QEvent *event)
QPoint mapFromGlobal(const QPoint &pos) const const
QWidget * parentWidget() const const
void setToolTip(const QString &)
void update()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:19:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.