23 #include <tqtooltip.h>
25 #include <tqpainter.h>
27 #include <tqapplication.h>
30 #include <tdeglobalsettings.h>
32 #include "kurllabel.h"
34 class KURLLabel::Private
40 HighlightedLinkColor (TQt::red),
51 Timer (new TQTimer (
label))
53 connect (Timer, TQ_SIGNAL (timeout ()), label, TQ_SLOT (updateColor ()));
65 TQColor HighlightedLinkColor;
83 TQWidget* parent,
const char* name)
84 : TQLabel (!text.isNull() ? text : url, parent, name),
85 d (new Private (url, this))
89 setLinkColor (d->LinkColor);
91 setMouseTracking (
true);
95 : TQLabel (parent, name),
96 d (new Private (TQString::null, this))
100 setLinkColor (d->LinkColor);
102 setMouseTracking (
true);
112 TQLabel::mouseReleaseEvent (e);
113 if (!d->MousePressed)
115 d->MousePressed =
false;
116 TQRect r( activeRect() );
117 if (!r.contains(e->pos()))
120 setLinkColor (d->HighlightedLinkColor);
121 d->Timer->start (300);
125 case TQt::LeftButton:
135 case TQt::RightButton:
148 newFont.setUnderline (d->Underline);
150 TQLabel::setFont (newFont);
160 void KURLLabel::updateColor ()
164 TQRect r( activeRect() );
165 if (!(d->Glow || d->Float) || !r.contains (mapFromGlobal(TQCursor::pos())))
166 setLinkColor (d->LinkColor);
169 void KURLLabel::setLinkColor (
const TQColor& col)
171 TQPalette p = palette();
172 p.setColor (TQColorGroup::Foreground, col);
180 if ( d->Tip == d->URL ) {
202 d->Cursor =
new TQCursor( cursor );
228 TQToolTip::add (
this, activeRect(), d->Tip);
230 TQToolTip::remove (
this);
252 d->LinkColor = highcolor;
254 if (!d->Timer->isActive())
255 setLinkColor (highcolor);
265 d->HighlightedLinkColor = selcolor;
267 if (d->Timer->isActive())
268 setLinkColor (selcolor);
298 d->AltPixmap = altPix;
303 return &d->AltPixmap;
308 TQLabel::enterEvent (e);
310 TQRect r( activeRect() );
311 if (!r.contains(
static_cast<TQMouseEvent*
>(e)->pos() ))
314 if (!d->AltPixmap.isNull() && pixmap())
316 d->RealPixmap = *pixmap();
317 setPixmap (d->AltPixmap);
320 if (d->Glow || d->Float)
324 setLinkColor (d->HighlightedLinkColor);
326 d->RealUnderline = d->Underline;
338 TQLabel::leaveEvent (e);
340 if (!d->AltPixmap.isNull() && pixmap())
341 setPixmap (d->RealPixmap);
343 if ((d->Glow || d->Float) && !d->Timer->isActive())
344 setLinkColor (d->LinkColor);
354 if (e && e->type() == TQEvent::ParentPaletteChange)
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());
362 setLinkColor(d->LinkColor);
365 else if (e->type() == TQEvent::Paint) {
366 const bool result = TQLabel::event(e);
367 if (result && hasFocus()) {
369 TQRect r( activeRect() );
370 style().drawPrimitive( TQStyle::PE_FocusRect, &p, r, colorGroup() );
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);
385 else if (e->type() == TQEvent::MouseButtonPress) {
386 TQRect r( activeRect() );
387 d->MousePressed = r.contains(
static_cast<TQMouseEvent*
>(e)->pos());
389 else if (e->type() == TQEvent::MouseMove) {
391 TQRect r( activeRect() );
392 bool inside = r.contains(
static_cast<TQMouseEvent*
>(e)->pos());
393 if (d->WasInsideRect != inside) {
395 TQLabel::setCursor(*d->Cursor);
397 TQLabel::unsetCursor();
398 d->WasInsideRect = inside;
402 return TQLabel::event(e);
405 TQRect KURLLabel::activeRect()
const
407 TQRect r( contentsRect() );
408 if (text().isEmpty() || (!d->MarginAltered && sizePolicy() == TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed)))
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);
427 TQLabel::setMargin(margin);
428 d->MarginAltered =
true;
433 TQLabel::setFocusPolicy(policy);
434 if (!d->MarginAltered) {
435 TQLabel::setMargin(policy == TQWidget::NoFocus ? 0 : 3);
441 TQLabel::setSizePolicy(policy);
442 if (!d->MarginAltered && policy.horData()==TQSizePolicy::Fixed && policy.verData()==TQSizePolicy::Fixed) {
443 TQLabel::setMargin(0);
447 void KURLLabel::virtual_hook(
int,
void* )
450 #include "kurllabel.moc"
static TQCursor handCursor()
Returns the proper hand cursor according to the current GUI style (static function).
A drop-in replacement for TQLabel that displays hyperlinks.
virtual void setCursor(const TQCursor &cursor)
Overridden for internal reasons; the API remains unaffected.
const TQString & url() const
Returns the URL.
virtual void unsetCursor()
Overridden for internal reasons; the API remains unaffected.
void setSelectedColor(const TQColor &selcolor)
Sets the selected color.
void enteredURL()
Emitted when the mouse has passed over the label.
virtual void setMargin(int margin)
Reimplemented for internal reasons, the API is not affected.
void rightClickedURL()
Emitted when the user clicked the right mouse button on this label.
virtual void setFocusPolicy(TQWidget::FocusPolicy policy)
Reimplemented for internal reasons, the API is not affected.
void middleClickedURL()
Emitted when the user clicked the left mouse button on this label.
KURLLabel(TQWidget *parent=0L, const char *name=0L)
Default constructor.
virtual void setSizePolicy(TQSizePolicy)
Reimplemented for internal reasons, the API is not affected.
bool isFloatEnabled() const
This feature is very similar to the "glow" feature in that the color of the label switches to the sel...
virtual bool event(TQEvent *e)
Catch parent palette changes.
virtual ~KURLLabel()
Destructs the label.
bool isGlowEnabled() const
When this is on, the text will switch to the selected color whenever the mouse passes over it.
void setGlow(bool glow=true)
Turns on or off the "glow" feature.
void setFloat(bool do_float=true)
Turns on or off the "float" feature.
void setURL(const TQString &url)
Sets the URL for this label to url.
virtual void setFont(const TQFont &)
Overridden for internal reasons; the API remains unaffected.
virtual void enterEvent(TQEvent *)
Overridden for internal reasons; the API remains unaffected.
void setUnderline(bool on=true)
Turns on or off the underlining.
void setUseTips(bool on=true)
Turns on or off the tool tip feature.
virtual void mouseReleaseEvent(TQMouseEvent *e)
Overridden for internal reasons; the API remains unaffected.
void setAltPixmap(const TQPixmap &altPix)
Sets the "alt" pixmap.
const TQString & tipText() const
Returns the current tooltip text.
void setHighlightedColor(const TQColor &highcolor)
Sets the highlight color.
void setTipText(const TQString &tip)
Specifies what text to display when tooltips are turned on.
void leftURL()
Emitted when the mouse is no longer over the label.
const TQPixmap * altPixmap() const
virtual void leaveEvent(TQEvent *)
Overridden for internal reasons; the API remains unaffected.
void setUseCursor(bool on, TQCursor *cursor=0L)
Turns the custom cursor feature on or off.
void leftClickedURL()
Emitted when the user clicked the left mouse button on this label.
static TQColor linkColor()
TQString label(StdAccel id)