23 #include <tqtooltip.h>
25 #include <tqpainter.h>
27 #include <tqapplication.h>
30 #include <kglobalsettings.h>
32 #include "kurllabel.h"
34 class KURLLabel::Private
40 HighlightedLinkColor (Qt::red),
51 Timer (new TQTimer (label))
53 connect (Timer, TQT_SIGNAL (timeout ()), label, TQT_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);
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);
135 case Qt::RightButton:
148 newFont.setUnderline (d->Underline);
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;
310 TQRect r( activeRect() );
311 if (!r.contains( TQT_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;
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::Normal, TQColorGroup::Background));
359 p.setColor(TQColorGroup::Foreground, palette().active().foreground());
362 setLinkColor(d->LinkColor);
365 else if (e->type() == TQEvent::Paint) {
367 if (result && hasFocus()) {
369 TQRect r( activeRect() );
370 style().tqdrawPrimitive( TQStyle::PE_FocusRect, &p, r, colorGroup() );
374 else if (e->type() == TQEvent::KeyPress) {
375 TQKeyEvent* ke = TQT_TQKEYEVENT(e);
376 if (ke->key() == Qt::Key_Enter || ke->key() == Qt::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(TQT_TQMOUSEEVENT(e)->pos());
389 else if (e->type() == TQEvent::MouseMove) {
391 TQRect r( activeRect() );
392 bool inside = r.contains(TQT_TQMOUSEEVENT(e)->pos());
393 if (d->WasInsideRect != inside) {
398 d->WasInsideRect = inside;
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( QMIN(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 = QMIN(3, margin());
421 r = TQRect(r.left()-add, r.top()-add, r.width()+2*add, r.height()+2*add);
428 d->MarginAltered =
true;
434 if (!d->MarginAltered) {
442 if (!d->MarginAltered && policy.horData()==TQSizePolicy::Fixed && policy.verData()==TQSizePolicy::Fixed) {
447 void KURLLabel::virtual_hook(
int,
void* )
450 #include "kurllabel.moc"