• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • widgets
kurllabel.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000 Peter Putzer
2 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301 USA
17 
18 #include "kurllabel.h"
19 
20 #include <QtCore/QTimer>
21 #include <QtGui/QApplication>
22 #include <QtGui/QMouseEvent>
23 
24 #include <kcursor.h>
25 #include <kcolorscheme.h>
26 
27 class KUrlLabel::Private
28 {
29  public:
30  Private( const QString& _url, KUrlLabel* _parent )
31  : parent( _parent ),
32  url( _url ),
33  tipText( url ),
34  linkColor( KColorScheme(QPalette::Active, KColorScheme::Window).foreground(KColorScheme::LinkText).color() ),
35  highlightedLinkColor( KColorScheme(QPalette::Active, KColorScheme::Window).foreground(KColorScheme::ActiveText).color() ),
36  cursor( 0 ),
37  textUnderlined( true ),
38  realUnderlined( true ),
39  useTips( false ),
40  useCursor( false ),
41  glowEnabled( true ),
42  floatEnabled( false ),
43  timer( new QTimer( parent ) )
44  {
45  connect( timer, SIGNAL(timeout()), parent, SLOT(updateColor()) );
46  }
47 
48  ~Private ()
49  {
50  }
51 
52  void updateColor()
53  {
54  timer->stop();
55 
56  if ( !(glowEnabled || floatEnabled) || !parent->rect().contains( parent->mapFromGlobal( QCursor::pos() ) ) )
57  setLinkColor( linkColor );
58  }
59 
60  void setLinkColor( const QColor& color )
61  {
62  QPalette palette = parent->palette();
63  palette.setColor( QPalette::WindowText, color );
64  parent->setPalette( palette );
65 
66  parent->update();
67  }
68 
69 
70  KUrlLabel *parent;
71 
72  QString url;
73  QString tipText;
74  QColor linkColor;
75  QColor highlightedLinkColor;
76  QCursor* cursor;
77  bool textUnderlined : 1;
78  bool realUnderlined : 1;
79  bool useTips : 1;
80  bool useCursor : 1;
81  bool glowEnabled : 1;
82  bool floatEnabled : 1;
83  QPixmap alternatePixmap;
84  QPixmap realPixmap;
85  QTimer* timer;
86 };
87 
88 KUrlLabel::KUrlLabel( const QString& url, const QString& text, QWidget* parent )
89  : QLabel( !text.isNull() ? text : url, parent ),
90  d( new Private( url, this ) )
91 {
92  setFont( font() );
93  setCursor( QCursor( Qt::PointingHandCursor ) );
94  d->setLinkColor( d->linkColor );
95 }
96 
97 KUrlLabel::KUrlLabel( QWidget* parent )
98  : QLabel( parent ),
99  d( new Private( QString(), this ) )
100 {
101  setFont( font() );
102  setCursor( QCursor( Qt::PointingHandCursor ) );
103  d->setLinkColor( d->linkColor );
104 }
105 
106 KUrlLabel::~KUrlLabel()
107 {
108  delete d;
109 }
110 
111 void KUrlLabel::mouseReleaseEvent( QMouseEvent* event )
112 {
113  QLabel::mouseReleaseEvent( event );
114 
115  d->setLinkColor( d->highlightedLinkColor );
116  d->timer->start( 300 );
117 
118  switch ( event->button() ) {
119  case Qt::LeftButton:
120  emit leftClickedUrl();
121  emit leftClickedUrl( d->url );
122  break;
123 
124  case Qt::MidButton:
125  emit middleClickedUrl();
126  emit middleClickedUrl( d->url );
127  break;
128 
129  case Qt::RightButton:
130  emit rightClickedUrl();
131  emit rightClickedUrl( d->url );
132  break;
133 
134  default:
135  break;
136  }
137 }
138 
139 void KUrlLabel::setFont( const QFont& font )
140 {
141  QFont newFont = font;
142  newFont.setUnderline( d->textUnderlined );
143 
144  QLabel::setFont( newFont );
145 }
146 
147 void KUrlLabel::setUnderline( bool on )
148 {
149  d->textUnderlined = on;
150 
151  setFont( font() );
152 }
153 
154 void KUrlLabel::setUrl( const QString& url )
155 {
156  if ( d->tipText == d->url ) { // update the tip as well
157  d->tipText = url;
158  setUseTips( d->useTips );
159  }
160 
161  d->url = url;
162 }
163 
164 QString KUrlLabel::url() const
165 {
166  return d->url;
167 }
168 
169 void KUrlLabel::setUseCursor( bool on, QCursor* cursor )
170 {
171  d->useCursor = on;
172  d->cursor = cursor;
173 
174  if ( on ) {
175  if ( cursor ) {
176  setCursor( *cursor );
177  } else {
178  setCursor( QCursor( Qt::PointingHandCursor ) );
179  }
180  } else
181  unsetCursor();
182 }
183 
184 bool KUrlLabel::useCursor() const
185 {
186  return d->useCursor;
187 }
188 
189 void KUrlLabel::setUseTips( bool on )
190 {
191  d->useTips = on;
192 
193  if ( on )
194  setToolTip( d->tipText );
195  else
196  setToolTip( QString() );
197 }
198 
199 void KUrlLabel::setTipText( const QString& tipText )
200 {
201  d->tipText = tipText;
202 
203  setUseTips( d->useTips );
204 }
205 
206 bool KUrlLabel::useTips() const
207 {
208  return d->useTips;
209 }
210 
211 QString KUrlLabel::tipText() const
212 {
213  return d->tipText;
214 }
215 
216 void KUrlLabel::setHighlightedColor( const QColor& color )
217 {
218  d->linkColor = color;
219 
220  if ( !d->timer->isActive() )
221  d->setLinkColor( color );
222 }
223 
224 void KUrlLabel::setHighlightedColor( const QString& color )
225 {
226  setHighlightedColor( QColor( color ) );
227 }
228 
229 void KUrlLabel::setSelectedColor( const QColor& color )
230 {
231  d->highlightedLinkColor = color;
232 
233  if ( d->timer->isActive() )
234  d->setLinkColor( color );
235 }
236 
237 void KUrlLabel::setSelectedColor( const QString& color )
238 {
239  setSelectedColor( QColor( color ) );
240 }
241 
242 void KUrlLabel::setGlowEnabled( bool glowEnabled )
243 {
244  d->glowEnabled = glowEnabled;
245 }
246 
247 void KUrlLabel::setFloatEnabled( bool floatEnabled )
248 {
249  d->floatEnabled = floatEnabled;
250 }
251 
252 bool KUrlLabel::isGlowEnabled() const
253 {
254  return d->glowEnabled;
255 }
256 
257 bool KUrlLabel::isFloatEnabled() const
258 {
259  return d->floatEnabled;
260 }
261 
262 void KUrlLabel::setAlternatePixmap( const QPixmap& pixmap )
263 {
264  d->alternatePixmap = pixmap;
265 }
266 
267 const QPixmap* KUrlLabel::alternatePixmap() const
268 {
269  return &d->alternatePixmap;
270 }
271 
272 void KUrlLabel::enterEvent( QEvent* event )
273 {
274  QLabel::enterEvent( event );
275 
276  if ( !d->alternatePixmap.isNull() && pixmap() ) {
277  d->realPixmap = *pixmap();
278  setPixmap( d->alternatePixmap );
279  }
280 
281  if ( d->glowEnabled || d->floatEnabled ) {
282  d->timer->stop();
283 
284  d->setLinkColor( d->highlightedLinkColor );
285 
286  d->realUnderlined = d->textUnderlined;
287 
288  if ( d->floatEnabled )
289  setUnderline( true );
290  }
291 
292  emit enteredUrl();
293  emit enteredUrl( d->url );
294 }
295 
296 void KUrlLabel::leaveEvent( QEvent* event )
297 {
298  QLabel::leaveEvent( event );
299 
300  if ( !d->alternatePixmap.isNull() && pixmap() )
301  setPixmap( d->realPixmap );
302 
303  if ( (d->glowEnabled || d->floatEnabled) && !d->timer->isActive() )
304  d->setLinkColor( d->linkColor );
305 
306  setUnderline( d->realUnderlined );
307 
308  emit leftUrl();
309  emit leftUrl( d->url );
310 }
311 
312 bool KUrlLabel::event( QEvent *event )
313 {
314  if ( event->type() == QEvent::PaletteChange ) {
318  QPalette palette = parentWidget() ? parentWidget()->palette() : qApp->palette();
319 
320  palette.setBrush( QPalette::Base, palette.brush( QPalette::Normal, QPalette::Background ) );
321  palette.setColor( QPalette::Foreground, this->palette().color( QPalette::Active, QPalette::Foreground ) );
322  setPalette( palette );
323 
324  d->linkColor = KColorScheme(QPalette::Active, KColorScheme::Window).foreground(KColorScheme::LinkText).color();
325  d->updateColor();
326 
327  return true;
328  } else
329  return QLabel::event( event );
330 }
331 
332 #include "kurllabel.moc"
QPalette::setBrush
void setBrush(ColorRole role, const QBrush &brush)
QEvent
QWidget
QEvent::type
Type type() const
QWidget::palette
const QPalette & palette() const
QWidget::cursor
QCursor cursor() const
QPalette::setColor
void setColor(ColorGroup group, ColorRole role, const QColor &color)
KUrlLabel::setHighlightedColor
void setHighlightedColor(const QColor &highcolor)
Sets the highlight color.
Definition: kurllabel.cpp:216
KUrlLabel::isFloatEnabled
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:257
KUrlLabel::event
virtual bool event(QEvent *)
Catch parent palette changes.
Definition: kurllabel.cpp:312
KUrlLabel::leaveEvent
virtual void leaveEvent(QEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:296
timeout
int timeout
QFont
KUrlLabel::isGlowEnabled
bool isGlowEnabled() const
When this is on, the text will switch to the selected color whenever the mouse passes over it...
Definition: kurllabel.cpp:252
QLabel::pixmap
const QPixmap * pixmap() const
KUrlLabel::leftUrl
void leftUrl()
Emitted when the mouse is no longer over the label.
KUrlLabel::setUnderline
void setUnderline(bool on=true)
Turns on or off the underlining.
Definition: kurllabel.cpp:147
QFont::setUnderline
void setUnderline(bool enable)
QMouseEvent
KUrlLabel::middleClickedUrl
void middleClickedUrl()
Emitted when the user clicked the left mouse button on this label.
KColorScheme::Window
Non-editable window elements; for example, menus.
Definition: kcolorscheme.h:93
KUrlLabel::alternatePixmap
const QPixmap * alternatePixmap() const
KUrlLabel::rightClickedUrl
void rightClickedUrl()
Emitted when the user clicked the right mouse button on this label.
QBrush::color
const QColor & color() const
kcursor.h
QLabel::event
virtual bool event(QEvent *e)
KUrlLabel::useTips
bool useTips() const
KColorScheme::foreground
QBrush foreground(ForegroundRole=NormalText) const
Retrieve the requested foreground brush.
Definition: kcolorscheme.cpp:459
QPalette::brush
const QBrush & brush(ColorGroup group, ColorRole role) const
QTimer
KUrlLabel::setUrl
void setUrl(const QString &url)
Sets the URL for this label to url.
Definition: kurllabel.cpp:154
KUrlLabel::tipText
QString tipText() const
Returns the current tooltip text.
KUrlLabel::url
QString url() const
Returns the URL.
QMouseEvent::button
Qt::MouseButton button() const
KColorScheme::LinkText
Fourth color; use for (unvisited) links.
Definition: kcolorscheme.h:221
KUrlLabel::glowEnabled
bool glowEnabled
Definition: kurllabel.h:75
KUrlLabel::floatEnabled
bool floatEnabled
Definition: kurllabel.h:76
QString
QColor
QWidget::enterEvent
virtual void enterEvent(QEvent *event)
KUrlLabel::setFloatEnabled
void setFloatEnabled(bool do_float=true)
Turns on or off the "float" feature.
Definition: kurllabel.cpp:247
QPixmap
QWidget::font
const QFont & font() const
KUrlLabel::~KUrlLabel
virtual ~KUrlLabel()
Destructs the label.
Definition: kurllabel.cpp:106
KUrlLabel::setAlternatePixmap
void setAlternatePixmap(const QPixmap &pixmap)
Sets the "alt" pixmap.
Definition: kurllabel.cpp:262
KUrlLabel::KUrlLabel
KUrlLabel(QWidget *parent=0L)
Default constructor.
Definition: kurllabel.cpp:97
QCursor::pos
QPoint pos()
Window
KColorScheme
A set of methods used to work with colors.
Definition: kcolorscheme.h:71
KUrlLabel::leftClickedUrl
void leftClickedUrl()
Emitted when the user clicked the left mouse button on this label.
QWidget::parentWidget
QWidget * parentWidget() const
kurllabel.h
QLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *ev)
KUrlLabel::enterEvent
virtual void enterEvent(QEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:272
KUrlLabel::setUseCursor
void setUseCursor(bool on, QCursor *cursor=0L)
Turns the custom cursor feature on or off.
Definition: kurllabel.cpp:169
KUrlLabel::setSelectedColor
void setSelectedColor(const QColor &color)
Sets the selected color.
Definition: kurllabel.cpp:229
KUrlLabel::enteredUrl
void enteredUrl()
Emitted when the mouse has passed over the label.
QWidget::setToolTip
void setToolTip(const QString &)
KUrlLabel::useCursor
bool useCursor() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
QObject::parent
QObject * parent() const
QWidget::leaveEvent
virtual void leaveEvent(QEvent *event)
QCursor
KUrlLabel::setGlowEnabled
void setGlowEnabled(bool glow=true)
Turns on or off the "glow" feature.
Definition: kurllabel.cpp:242
KUrlLabel
A drop-in replacement for QLabel that displays hyperlinks.
Definition: kurllabel.h:69
kcolorscheme.h
KUrlLabel::setFont
virtual void setFont(const QFont &font)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:139
QPalette
KUrlLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:111
KUrlLabel::setUseTips
void setUseTips(bool on=true)
Turns on or off the tool tip feature.
Definition: kurllabel.cpp:189
KUrlLabel::setTipText
void setTipText(const QString &tip)
Specifies what text to display when tooltips are turned on.
Definition: kurllabel.cpp:199
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal