KWidgetsAddons

kurllabel.cpp
1 /*
2  SPDX-FileCopyrightText: 2000 Peter Putzer <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "kurllabel.h"
8 
9 #include <QTimer>
10 #include <QApplication>
11 #include <QMouseEvent>
12 #include <QPalette>
13 
14 class Q_DECL_HIDDEN KUrlLabel::Private
15 {
16 public:
17  Private(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  connect(timer, &QTimer::timeout, parent, [this]() { updateColor(); });
33  }
34 
35  ~Private()
36  {
37  }
38 
39  void updateColor()
40  {
41  timer->stop();
42 
43  if (!(glowEnabled || floatEnabled) || !parent->rect().contains(parent->mapFromGlobal(QCursor::pos()))) {
44  setLinkColor(linkColor);
45  }
46  }
47 
48  void setLinkColor(const QColor &color)
49  {
50  QPalette palette = parent->palette();
51  palette.setColor(QPalette::WindowText, color);
52  parent->setPalette(palette);
53 
54  parent->update();
55  }
56 
57  KUrlLabel *parent;
58 
59  QString url;
60  QString tipText;
61  QColor linkColor;
62  QColor highlightedLinkColor;
63  QCursor *cursor;
64  bool textUnderlined : 1;
65  bool realUnderlined : 1;
66  bool useTips : 1;
67  bool useCursor : 1;
68  bool glowEnabled : 1;
69  bool floatEnabled : 1;
70  QPixmap alternatePixmap;
71  QPixmap realPixmap;
72  QTimer *timer;
73 };
74 
75 KUrlLabel::KUrlLabel(const QString &url, const QString &text, QWidget *parent)
76  : QLabel(!text.isNull() ? text : url, parent),
77  d(new Private(url, this))
78 {
79  setFont(font());
81  d->setLinkColor(d->linkColor);
82 }
83 
85  : QLabel(parent),
86  d(new Private(QString(), this))
87 {
88  setFont(font());
90  d->setLinkColor(d->linkColor);
91 }
92 
94 {
95  delete d;
96 }
97 
99 {
101 
102  d->setLinkColor(d->highlightedLinkColor);
103  d->timer->start(300);
104 
105  switch (event->button()) {
106  case Qt::LeftButton:
107  emit leftClickedUrl();
108 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 65)
109  emit leftClickedUrl(d->url);
110 #endif
111  break;
112 
113  case Qt::MidButton:
114  emit middleClickedUrl();
115 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 65)
116  emit middleClickedUrl(d->url);
117 #endif
118  break;
119 
120  case Qt::RightButton:
121  emit rightClickedUrl();
122 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 65)
123  emit rightClickedUrl(d->url);
124 #endif
125  break;
126 
127  default:
128  break;
129  }
130 }
131 
133 {
134  QFont newFont = font;
135  newFont.setUnderline(d->textUnderlined);
136 
137  QLabel::setFont(newFont);
138 }
139 
141 {
142  d->textUnderlined = on;
143 
144  setFont(font());
145 }
146 
148 {
149  if (d->tipText == d->url) { // update the tip as well
150  d->tipText = url;
151  setUseTips(d->useTips);
152  }
153 
154  d->url = url;
155 }
156 
157 QString KUrlLabel::url() const
158 {
159  return d->url;
160 }
161 
163 {
164  d->useCursor = on;
165  d->cursor = cursor;
166 
167  if (on) {
168  if (cursor) {
169  setCursor(*cursor);
170  } else {
172  }
173  } else {
174  unsetCursor();
175  }
176 }
177 
178 bool KUrlLabel::useCursor() const
179 {
180  return d->useCursor;
181 }
182 
184 {
185  d->useTips = on;
186 
187  if (on) {
188  setToolTip(d->tipText);
189  } else {
190  setToolTip(QString());
191  }
192 }
193 
195 {
196  d->tipText = tipText;
197 
198  setUseTips(d->useTips);
199 }
200 
201 bool KUrlLabel::useTips() const
202 {
203  return d->useTips;
204 }
205 
207 {
208  return d->tipText;
209 }
210 
212 {
213  d->linkColor = color;
214 
215  if (!d->timer->isActive()) {
216  d->setLinkColor(color);
217  }
218 }
219 
221 {
222  setHighlightedColor(QColor(color));
223 }
224 
226 {
227  d->highlightedLinkColor = color;
228 
229  if (d->timer->isActive()) {
230  d->setLinkColor(color);
231  }
232 }
233 
235 {
236  setSelectedColor(QColor(color));
237 }
238 
239 void KUrlLabel::setGlowEnabled(bool glowEnabled)
240 {
241  d->glowEnabled = glowEnabled;
242 }
243 
244 void KUrlLabel::setFloatEnabled(bool floatEnabled)
245 {
246  d->floatEnabled = floatEnabled;
247 }
248 
250 {
251  return d->glowEnabled;
252 }
253 
255 {
256  return d->floatEnabled;
257 }
258 
260 {
261  d->alternatePixmap = pixmap;
262 }
263 
264 const QPixmap *KUrlLabel::alternatePixmap() const
265 {
266  return &d->alternatePixmap;
267 }
268 
270 {
271  QLabel::enterEvent(event);
272 
273 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
274  if (!d->alternatePixmap.isNull()) {
275  const auto currentPixmap = pixmap(Qt::ReturnByValue);
276  if (!currentPixmap.isNull()) {
277  d->realPixmap = currentPixmap;
278  setPixmap(d->alternatePixmap);
279  }
280  }
281 #else
282  if (!d->alternatePixmap.isNull() && pixmap()) {
283  d->realPixmap = *pixmap();
284  setPixmap(d->alternatePixmap);
285  }
286 #endif
287 
288  if (d->glowEnabled || d->floatEnabled) {
289  d->timer->stop();
290 
291  d->setLinkColor(d->highlightedLinkColor);
292 
293  d->realUnderlined = d->textUnderlined;
294 
295  if (d->floatEnabled) {
296  setUnderline(true);
297  }
298  }
299  emit enteredUrl();
300 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 65)
301  emit enteredUrl(d->url);
302 #endif
303 }
304 
306 {
307  QLabel::leaveEvent(event);
308 
309  if (!d->alternatePixmap.isNull() &&
310 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
312 #else
313  pixmap()) {
314 #endif
315  setPixmap(d->realPixmap);
316  }
317 
318  if ((d->glowEnabled || d->floatEnabled) && !d->timer->isActive()) {
319  d->setLinkColor(d->linkColor);
320  }
321 
322  setUnderline(d->realUnderlined);
323  emit leftUrl();
324 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 65)
325  emit leftUrl(d->url);
326 #endif
327 }
328 
330 {
331  if (event->type() == QEvent::PaletteChange) {
335  QPalette palette = parentWidget() ? parentWidget()->palette() : qApp->palette();
336 
339  setPalette(palette);
340 
341  d->linkColor = palette.color(QPalette::Active, QPalette::Link);
342  d->updateColor();
343 
344  return true;
345  } else {
346  return QLabel::event(event);
347  }
348 }
349 
350 #include "moc_kurllabel.cpp"
void setBrush(QPalette::ColorRole role, const QBrush &brush)
QEvent::Type type() const const
void setUseCursor(bool on, QCursor *cursor=nullptr)
Turns the custom cursor feature on or off.
Definition: kurllabel.cpp:162
KUrlLabel(QWidget *parent=nullptr)
Default constructor.
Definition: kurllabel.cpp:84
void setCursor(const QCursor &)
void setColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color)
void setHighlightedColor(const QColor &highcolor)
Sets the highlight color.
Definition: kurllabel.cpp:211
bool isFloatEnabled() const
This feature is very similar to the "glow" feature in that the color of the label switches to the sel...
Definition: kurllabel.cpp:254
const QPixmap * pixmap() const const
ReturnByValue
bool isGlowEnabled() const
When this is on, the text will switch to the selected color whenever the mouse passes over it...
Definition: kurllabel.cpp:249
void setPixmap(const QPixmap &)
const QColor & color(QPalette::ColorGroup group, QPalette::ColorRole role) const const
void leftUrl()
Emitted when the mouse is no longer over the label.
void setUnderline(bool on=true)
Turns on or off the underlining.
Definition: kurllabel.cpp:140
void setUnderline(bool enable)
void enterEvent(QEvent *) override
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:269
LeftButton
void leaveEvent(QEvent *) override
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:305
void middleClickedUrl()
Emitted when the user clicked the left mouse button on this label.
const QPixmap * alternatePixmap() const
void rightClickedUrl()
Emitted when the user clicked the right mouse button on this label.
void timeout()
virtual bool event(QEvent *e) override
bool useTips() const
const QBrush & brush(QPalette::ColorGroup group, QPalette::ColorRole role) const const
void setUrl(const QString &url)
Sets the URL for this label to url.
Definition: kurllabel.cpp:147
QString tipText() const
Returns the current tooltip text.
QString url() const
Returns the URL.
Qt::MouseButton button() const const
PointingHandCursor
virtual void enterEvent(QEvent *event)
void setFloatEnabled(bool do_float=true)
Turns on or off the "float" feature.
Definition: kurllabel.cpp:244
bool isNull() const const
const QFont & font() const const
void setAlternatePixmap(const QPixmap &pixmap)
Sets the "alt" pixmap.
Definition: kurllabel.cpp:259
QPoint pos()
void leftClickedUrl()
Emitted when the user clicked the left mouse button on this label.
QWidget * parentWidget() const const
virtual void mouseReleaseEvent(QMouseEvent *ev) override
void mouseReleaseEvent(QMouseEvent *) override
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:98
~KUrlLabel() override
Destructs the label.
Definition: kurllabel.cpp:93
void setSelectedColor(const QColor &color)
Sets the selected color.
Definition: kurllabel.cpp:225
void enteredUrl()
Emitted when the mouse has passed over the label.
void setToolTip(const QString &)
bool useCursor() const
QObject * parent() const const
virtual void leaveEvent(QEvent *event)
void setGlowEnabled(bool glow=true)
Turns on or off the "glow" feature.
Definition: kurllabel.cpp:239
A drop-in replacement for QLabel that displays hyperlinks.
Definition: kurllabel.h:60
virtual void setFont(const QFont &font)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:132
bool event(QEvent *) override
Catch parent palette changes.
Definition: kurllabel.cpp:329
void setUseTips(bool on=true)
Turns on or off the tool tip feature.
Definition: kurllabel.cpp:183
void setTipText(const QString &tip)
Specifies what text to display when tooltips are turned on.
Definition: kurllabel.cpp:194
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 22:47:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.