• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kurllabel.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 1998 Kurt Granroth <granroth@kde.org>
3 Copyright (C) 2000 Peter Putzer <putzer@kde.org>
4 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include <tqcolor.h>
22#include <tqtimer.h>
23#include <tqtooltip.h>
24#include <tqpixmap.h>
25#include <tqpainter.h>
26#include <tqstyle.h>
27#include <tqapplication.h>
28
29#include <kcursor.h>
30#include <tdeglobalsettings.h>
31
32#include "kurllabel.h"
33
34class KURLLabel::Private
35{
36public:
37 Private (const TQString& url, KURLLabel* label)
38 : URL (url),
39 LinkColor (TDEGlobalSettings::linkColor()),
40 HighlightedLinkColor (TQt::red),
41 Tip(url),
42 Cursor (0L),
43 Underline (true),
44 UseTips (false),
45 Glow (true),
46 Float (false),
47 RealUnderline (true),
48 MousePressed(false),
49 WasInsideRect(false),
50 MarginAltered(false),
51 Timer (new TQTimer (label))
52 {
53 connect (Timer, TQ_SIGNAL (timeout ()), label, TQ_SLOT (updateColor ()));
54 }
55
56 ~Private ()
57 {
58 delete Cursor;
59 }
60
61 TQString URL;
62 TQPixmap AltPixmap;
63
64 TQColor LinkColor;
65 TQColor HighlightedLinkColor;
66
67 TQString Tip;
68 TQCursor* Cursor;
69 bool Underline:1;
70 bool UseTips:1;
71 bool Glow:1;
72 bool Float:1;
73 bool RealUnderline:1;
74 bool MousePressed:1;
75 bool WasInsideRect:1;
76 bool MarginAltered:1;
77 TQPixmap RealPixmap;
78
79 TQTimer* Timer;
80};
81
82KURLLabel::KURLLabel (const TQString& url, const TQString& text,
83 TQWidget* parent, const char* name)
84 : TQLabel (!text.isNull() ? text : url, parent, name),
85 d (new Private (url, this))
86{
87 setFont (font());
88 setUseCursor (true);
89 setLinkColor (d->LinkColor);
90 setFocusPolicy( TQWidget::StrongFocus ); //better accessibility
91 setMouseTracking (true);
92}
93
94KURLLabel::KURLLabel (TQWidget* parent, const char* name)
95 : TQLabel (parent, name),
96 d (new Private (TQString::null, this))
97{
98 setFont (font());
99 setUseCursor (true);
100 setLinkColor (d->LinkColor);
101 setFocusPolicy( TQWidget::StrongFocus ); //better accessibility
102 setMouseTracking (true);
103}
104
105KURLLabel::~KURLLabel ()
106{
107 delete d;
108}
109
110void KURLLabel::mouseReleaseEvent (TQMouseEvent* e)
111{
112 TQLabel::mouseReleaseEvent (e);
113 if (!d->MousePressed)
114 return;
115 d->MousePressed = false;
116 TQRect r( activeRect() );
117 if (!r.contains(e->pos()))
118 return;
119
120 setLinkColor (d->HighlightedLinkColor);
121 d->Timer->start (300);
122
123 switch (e->button())
124 {
125 case TQt::LeftButton:
126 emit leftClickedURL ();
127 emit leftClickedURL (d->URL);
128 break;
129
130 case TQt::MidButton:
131 emit middleClickedURL ();
132 emit middleClickedURL (d->URL);
133 break;
134
135 case TQt::RightButton:
136 emit rightClickedURL ();
137 emit rightClickedURL (d->URL);
138 break;
139
140 default:
141 ; // nothing
142 }
143}
144
145void KURLLabel::setFont (const TQFont& f)
146{
147 TQFont newFont = f;
148 newFont.setUnderline (d->Underline);
149
150 TQLabel::setFont (newFont);
151}
152
153void KURLLabel::setUnderline (bool on)
154{
155 d->Underline = on;
156
157 setFont (font());
158}
159
160void KURLLabel::updateColor ()
161{
162 d->Timer->stop();
163
164 TQRect r( activeRect() );
165 if (!(d->Glow || d->Float) || !r.contains (mapFromGlobal(TQCursor::pos())))
166 setLinkColor (d->LinkColor);
167}
168
169void KURLLabel::setLinkColor (const TQColor& col)
170{
171 TQPalette p = palette();
172 p.setColor (TQColorGroup::Foreground, col);
173 setPalette (p);
174
175 update();
176}
177
178void KURLLabel::setURL (const TQString& url)
179{
180 if ( d->Tip == d->URL ) { // update the tip as well
181 d->Tip = url;
182 setUseTips( d->UseTips );
183 }
184
185 d->URL = url;
186}
187
188const TQString& KURLLabel::url () const
189{
190 return d->URL;
191}
192
193void KURLLabel::unsetCursor ()
194{
195 delete d->Cursor;
196 d->Cursor = 0;
197}
198
199void KURLLabel::setCursor ( const TQCursor& cursor )
200{
201 delete d->Cursor;
202 d->Cursor = new TQCursor( cursor );
203}
204
205void KURLLabel::setUseCursor (bool on, TQCursor* cursor)
206{
207 if (on)
208 {
209 if (cursor)
210 KURLLabel::setCursor (*cursor);
211 else
212 KURLLabel::setCursor (KCursor::handCursor());
213 }
214 else
215 KURLLabel::unsetCursor ();
216}
217
218bool KURLLabel::useCursor () const
219{
220 return d->Cursor;
221}
222
223void KURLLabel::setUseTips (bool on)
224{
225 d->UseTips = on;
226
227 if (on) {
228 TQToolTip::add (this, activeRect(), d->Tip);
229 } else
230 TQToolTip::remove (this);
231}
232
233void KURLLabel::setTipText (const TQString& tip)
234{
235 d->Tip = tip;
236
237 setUseTips (d->UseTips);
238}
239
240bool KURLLabel::useTips () const
241{
242 return d->UseTips;
243}
244
245const TQString& KURLLabel::tipText () const
246{
247 return d->Tip;
248}
249
250void KURLLabel::setHighlightedColor (const TQColor& highcolor)
251{
252 d->LinkColor = highcolor;
253
254 if (!d->Timer->isActive())
255 setLinkColor (highcolor);
256}
257
258void KURLLabel::setHighlightedColor (const TQString& highcolor)
259{
260 setHighlightedColor (TQColor (highcolor));
261}
262
263void KURLLabel::setSelectedColor (const TQColor& selcolor)
264{
265 d->HighlightedLinkColor = selcolor;
266
267 if (d->Timer->isActive())
268 setLinkColor (selcolor);
269}
270
271void KURLLabel::setSelectedColor (const TQString& selcolor)
272{
273 setSelectedColor (TQColor (selcolor));
274}
275
276void KURLLabel::setGlow (bool glow)
277{
278 d->Glow = glow;
279}
280
281void KURLLabel::setFloat (bool do_float)
282{
283 d->Float = do_float;
284}
285
286bool KURLLabel::isGlowEnabled () const
287{
288 return d->Glow;
289}
290
291bool KURLLabel::isFloatEnabled () const
292{
293 return d->Float;
294}
295
296void KURLLabel::setAltPixmap (const TQPixmap& altPix)
297{
298 d->AltPixmap = altPix;
299}
300
301const TQPixmap* KURLLabel::altPixmap () const
302{
303 return &d->AltPixmap;
304}
305
306void KURLLabel::enterEvent (TQEvent* e)
307{
308 TQLabel::enterEvent (e);
309
310 TQRect r( activeRect() );
311 if (!r.contains( static_cast<TQMouseEvent*>(e)->pos() ))
312 return;
313
314 if (!d->AltPixmap.isNull() && pixmap())
315 {
316 d->RealPixmap = *pixmap();
317 setPixmap (d->AltPixmap);
318 }
319
320 if (d->Glow || d->Float)
321 {
322 d->Timer->stop();
323
324 setLinkColor (d->HighlightedLinkColor);
325
326 d->RealUnderline = d->Underline;
327
328 if (d->Float)
329 setUnderline (true);
330 }
331
332 emit enteredURL ();
333 emit enteredURL (d->URL);
334}
335
336void KURLLabel::leaveEvent (TQEvent* e)
337{
338 TQLabel::leaveEvent (e);
339
340 if (!d->AltPixmap.isNull() && pixmap())
341 setPixmap (d->RealPixmap);
342
343 if ((d->Glow || d->Float) && !d->Timer->isActive())
344 setLinkColor (d->LinkColor);
345
346 setUnderline (d->RealUnderline);
347
348 emit leftURL ();
349 emit leftURL (d->URL);
350}
351
352bool KURLLabel::event (TQEvent *e)
353{
354 if (e && e->type() == TQEvent::ParentPaletteChange)
355 {
356 // use parentWidget() unless you are a toplevel widget, then try qAapp
357 TQPalette p = parentWidget() ? parentWidget()->palette() : tqApp->palette();
358 p.setBrush(TQColorGroup::Base, p.brush(TQPalette::Active, TQColorGroup::Background));
359 p.setColor(TQColorGroup::Foreground, palette().active().foreground());
360 setPalette(p);
361 d->LinkColor = TDEGlobalSettings::linkColor();
362 setLinkColor(d->LinkColor);
363 return true;
364 }
365 else if (e->type() == TQEvent::Paint) {
366 const bool result = TQLabel::event(e);
367 if (result && hasFocus()) {
368 TQPainter p(this);
369 TQRect r( activeRect() );
370 style().drawPrimitive( TQStyle::PE_FocusRect, &p, r, colorGroup() );
371 }
372 return result;
373 }
374 else if (e->type() == TQEvent::KeyPress) {
375 TQKeyEvent* ke = static_cast<TQKeyEvent*>(e);
376 if (ke->key() == TQt::Key_Enter || ke->key() == TQt::Key_Return) {
377 setLinkColor (d->HighlightedLinkColor);
378 d->Timer->start (300);
379 emit leftClickedURL ();
380 emit leftClickedURL (d->URL);
381 ke->accept();
382 return true;
383 }
384 }
385 else if (e->type() == TQEvent::MouseButtonPress) {
386 TQRect r( activeRect() );
387 d->MousePressed = r.contains(static_cast<TQMouseEvent*>(e)->pos());
388 }
389 else if (e->type() == TQEvent::MouseMove) {
390 if (d->Cursor) {
391 TQRect r( activeRect() );
392 bool inside = r.contains(static_cast<TQMouseEvent*>(e)->pos());
393 if (d->WasInsideRect != inside) {
394 if (inside)
395 TQLabel::setCursor(*d->Cursor);
396 else
397 TQLabel::unsetCursor();
398 d->WasInsideRect = inside;
399 }
400 }
401 }
402 return TQLabel::event(e);
403}
404
405TQRect KURLLabel::activeRect() const
406{
407 TQRect r( contentsRect() );
408 if (text().isEmpty() || (!d->MarginAltered && sizePolicy() == TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed)))
409 return r; //fixed size is sometimes used with pixmap
410 int hAlign = TQApplication::horizontalAlignment( alignment() );
411 int indentX = (hAlign && indent()>0) ? indent() : 0;
412 TQFontMetrics fm(font());
413 r.setWidth( TQMIN(fm.width(text()), r.width()));
414 if ( hAlign & AlignLeft )
415 r.moveLeft(r.left() + indentX);
416 if ( hAlign & AlignCenter )
417 r.moveLeft((contentsRect().width()-r.width())/2+margin());
418 if ( hAlign & AlignRight )
419 r.moveLeft(contentsRect().width()-r.width()-indentX+margin());
420 int add = TQMIN(3, margin());
421 r = TQRect(r.left()-add, r.top()-add, r.width()+2*add, r.height()+2*add);
422 return r;
423}
424
425void KURLLabel::setMargin( int margin )
426{
427 TQLabel::setMargin(margin);
428 d->MarginAltered = true;
429}
430
431void KURLLabel::setFocusPolicy( TQWidget::FocusPolicy policy )
432{
433 TQLabel::setFocusPolicy(policy);
434 if (!d->MarginAltered) {
435 TQLabel::setMargin(policy == TQWidget::NoFocus ? 0 : 3); //better default : better look when focused
436 }
437}
438
439void KURLLabel::setSizePolicy ( TQSizePolicy policy )
440{
441 TQLabel::setSizePolicy(policy);
442 if (!d->MarginAltered && policy.horData()==TQSizePolicy::Fixed && policy.verData()==TQSizePolicy::Fixed) {
443 TQLabel::setMargin(0); //better default : better look when fixed size
444 }
445}
446
447void KURLLabel::virtual_hook( int, void* )
448{ /*BASE::virtual_hook( id, data );*/ }
449
450#include "kurllabel.moc"
KCursor::handCursor
static TQCursor handCursor()
Returns the proper hand cursor according to the current GUI style (static function).
Definition: kcursor.cpp:43
KURLLabel
A drop-in replacement for TQLabel that displays hyperlinks.
Definition: kurllabel.h:72
KURLLabel::setCursor
virtual void setCursor(const TQCursor &cursor)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:199
KURLLabel::url
const TQString & url() const
Returns the URL.
Definition: kurllabel.cpp:188
KURLLabel::unsetCursor
virtual void unsetCursor()
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:193
KURLLabel::setSelectedColor
void setSelectedColor(const TQColor &selcolor)
Sets the selected color.
Definition: kurllabel.cpp:263
KURLLabel::enteredURL
void enteredURL()
Emitted when the mouse has passed over the label.
KURLLabel::setMargin
virtual void setMargin(int margin)
Reimplemented for internal reasons, the API is not affected.
Definition: kurllabel.cpp:425
KURLLabel::rightClickedURL
void rightClickedURL()
Emitted when the user clicked the right mouse button on this label.
KURLLabel::setFocusPolicy
virtual void setFocusPolicy(TQWidget::FocusPolicy policy)
Reimplemented for internal reasons, the API is not affected.
Definition: kurllabel.cpp:431
KURLLabel::middleClickedURL
void middleClickedURL()
Emitted when the user clicked the left mouse button on this label.
KURLLabel::KURLLabel
KURLLabel(TQWidget *parent=0L, const char *name=0L)
Default constructor.
Definition: kurllabel.cpp:94
KURLLabel::useTips
bool useTips() const
Definition: kurllabel.cpp:240
KURLLabel::setSizePolicy
virtual void setSizePolicy(TQSizePolicy)
Reimplemented for internal reasons, the API is not affected.
Definition: kurllabel.cpp:439
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:291
KURLLabel::event
virtual bool event(TQEvent *e)
Catch parent palette changes.
Definition: kurllabel.cpp:352
KURLLabel::~KURLLabel
virtual ~KURLLabel()
Destructs the label.
Definition: kurllabel.cpp:105
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:286
KURLLabel::useCursor
bool useCursor() const
Definition: kurllabel.cpp:218
KURLLabel::setGlow
void setGlow(bool glow=true)
Turns on or off the "glow" feature.
Definition: kurllabel.cpp:276
KURLLabel::setFloat
void setFloat(bool do_float=true)
Turns on or off the "float" feature.
Definition: kurllabel.cpp:281
KURLLabel::setURL
void setURL(const TQString &url)
Sets the URL for this label to url.
Definition: kurllabel.cpp:178
KURLLabel::setFont
virtual void setFont(const TQFont &)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:145
KURLLabel::enterEvent
virtual void enterEvent(TQEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:306
KURLLabel::setUnderline
void setUnderline(bool on=true)
Turns on or off the underlining.
Definition: kurllabel.cpp:153
KURLLabel::setUseTips
void setUseTips(bool on=true)
Turns on or off the tool tip feature.
Definition: kurllabel.cpp:223
KURLLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(TQMouseEvent *e)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:110
KURLLabel::setAltPixmap
void setAltPixmap(const TQPixmap &altPix)
Sets the "alt" pixmap.
Definition: kurllabel.cpp:296
KURLLabel::tipText
const TQString & tipText() const
Returns the current tooltip text.
Definition: kurllabel.cpp:245
KURLLabel::setHighlightedColor
void setHighlightedColor(const TQColor &highcolor)
Sets the highlight color.
Definition: kurllabel.cpp:250
KURLLabel::setTipText
void setTipText(const TQString &tip)
Specifies what text to display when tooltips are turned on.
Definition: kurllabel.cpp:233
KURLLabel::leftURL
void leftURL()
Emitted when the mouse is no longer over the label.
KURLLabel::altPixmap
const TQPixmap * altPixmap() const
Definition: kurllabel.cpp:301
KURLLabel::leaveEvent
virtual void leaveEvent(TQEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:336
KURLLabel::setUseCursor
void setUseCursor(bool on, TQCursor *cursor=0L)
Turns the custom cursor feature on or off.
Definition: kurllabel.cpp:205
KURLLabel::leftClickedURL
void leftClickedURL()
Emitted when the user clicked the left mouse button on this label.
TDEGlobalSettings::linkColor
static TQColor linkColor()
TDEStdAccel::label
TQString label(StdAccel id)
TDEGlobalSettings

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.