00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <tqcolor.h>
00022 #include <tqtimer.h>
00023 #include <tqtooltip.h>
00024 #include <tqpixmap.h>
00025 #include <tqpainter.h>
00026 #include <tqstyle.h>
00027 #include <tqapplication.h>
00028
00029 #include <kcursor.h>
00030 #include <tdeglobalsettings.h>
00031
00032 #include "kurllabel.h"
00033
00034 class KURLLabel::Private
00035 {
00036 public:
00037 Private (const TQString& url, KURLLabel* label)
00038 : URL (url),
00039 LinkColor (TDEGlobalSettings::linkColor()),
00040 HighlightedLinkColor (Qt::red),
00041 Tip(url),
00042 Cursor (0L),
00043 Underline (true),
00044 UseTips (false),
00045 Glow (true),
00046 Float (false),
00047 RealUnderline (true),
00048 MousePressed(false),
00049 WasInsideRect(false),
00050 MarginAltered(false),
00051 Timer (new TQTimer (label))
00052 {
00053 connect (Timer, TQT_SIGNAL (timeout ()), label, TQT_SLOT (updateColor ()));
00054 }
00055
00056 ~Private ()
00057 {
00058 delete Cursor;
00059 }
00060
00061 TQString URL;
00062 TQPixmap AltPixmap;
00063
00064 TQColor LinkColor;
00065 TQColor HighlightedLinkColor;
00066
00067 TQString Tip;
00068 TQCursor* Cursor;
00069 bool Underline:1;
00070 bool UseTips:1;
00071 bool Glow:1;
00072 bool Float:1;
00073 bool RealUnderline:1;
00074 bool MousePressed:1;
00075 bool WasInsideRect:1;
00076 bool MarginAltered:1;
00077 TQPixmap RealPixmap;
00078
00079 TQTimer* Timer;
00080 };
00081
00082 KURLLabel::KURLLabel (const TQString& url, const TQString& text,
00083 TQWidget* parent, const char* name)
00084 : TQLabel (!text.isNull() ? text : url, parent, name),
00085 d (new Private (url, this))
00086 {
00087 setFont (font());
00088 setUseCursor (true);
00089 setLinkColor (d->LinkColor);
00090 setFocusPolicy( TQ_StrongFocus );
00091 setMouseTracking (true);
00092 }
00093
00094 KURLLabel::KURLLabel (TQWidget* parent, const char* name)
00095 : TQLabel (parent, name),
00096 d (new Private (TQString::null, this))
00097 {
00098 setFont (font());
00099 setUseCursor (true);
00100 setLinkColor (d->LinkColor);
00101 setFocusPolicy( TQ_StrongFocus );
00102 setMouseTracking (true);
00103 }
00104
00105 KURLLabel::~KURLLabel ()
00106 {
00107 delete d;
00108 }
00109
00110 void KURLLabel::mouseReleaseEvent (TQMouseEvent* e)
00111 {
00112 TQLabel::mouseReleaseEvent (e);
00113 if (!d->MousePressed)
00114 return;
00115 d->MousePressed = false;
00116 TQRect r( activeRect() );
00117 if (!r.contains(e->pos()))
00118 return;
00119
00120 setLinkColor (d->HighlightedLinkColor);
00121 d->Timer->start (300);
00122
00123 switch (e->button())
00124 {
00125 case Qt::LeftButton:
00126 emit leftClickedURL ();
00127 emit leftClickedURL (d->URL);
00128 break;
00129
00130 case Qt::MidButton:
00131 emit middleClickedURL ();
00132 emit middleClickedURL (d->URL);
00133 break;
00134
00135 case Qt::RightButton:
00136 emit rightClickedURL ();
00137 emit rightClickedURL (d->URL);
00138 break;
00139
00140 default:
00141 ;
00142 }
00143 }
00144
00145 void KURLLabel::setFont (const TQFont& f)
00146 {
00147 TQFont newFont = f;
00148 newFont.setUnderline (d->Underline);
00149
00150 TQLabel::setFont (newFont);
00151 }
00152
00153 void KURLLabel::setUnderline (bool on)
00154 {
00155 d->Underline = on;
00156
00157 setFont (font());
00158 }
00159
00160 void KURLLabel::updateColor ()
00161 {
00162 d->Timer->stop();
00163
00164 TQRect r( activeRect() );
00165 if (!(d->Glow || d->Float) || !r.contains (mapFromGlobal(TQCursor::pos())))
00166 setLinkColor (d->LinkColor);
00167 }
00168
00169 void KURLLabel::setLinkColor (const TQColor& col)
00170 {
00171 TQPalette p = palette();
00172 p.setColor (TQColorGroup::Foreground, col);
00173 setPalette (p);
00174
00175 update();
00176 }
00177
00178 void KURLLabel::setURL (const TQString& url)
00179 {
00180 if ( d->Tip == d->URL ) {
00181 d->Tip = url;
00182 setUseTips( d->UseTips );
00183 }
00184
00185 d->URL = url;
00186 }
00187
00188 const TQString& KURLLabel::url () const
00189 {
00190 return d->URL;
00191 }
00192
00193 void KURLLabel::unsetCursor ()
00194 {
00195 delete d->Cursor;
00196 d->Cursor = 0;
00197 }
00198
00199 void KURLLabel::setCursor ( const TQCursor& cursor )
00200 {
00201 delete d->Cursor;
00202 d->Cursor = new TQCursor( cursor );
00203 }
00204
00205 void KURLLabel::setUseCursor (bool on, TQCursor* cursor)
00206 {
00207 if (on)
00208 {
00209 if (cursor)
00210 KURLLabel::setCursor (*cursor);
00211 else
00212 KURLLabel::setCursor (KCursor::handCursor());
00213 }
00214 else
00215 KURLLabel::unsetCursor ();
00216 }
00217
00218 bool KURLLabel::useCursor () const
00219 {
00220 return d->Cursor;
00221 }
00222
00223 void KURLLabel::setUseTips (bool on)
00224 {
00225 d->UseTips = on;
00226
00227 if (on) {
00228 TQToolTip::add (this, activeRect(), d->Tip);
00229 } else
00230 TQToolTip::remove (this);
00231 }
00232
00233 void KURLLabel::setTipText (const TQString& tip)
00234 {
00235 d->Tip = tip;
00236
00237 setUseTips (d->UseTips);
00238 }
00239
00240 bool KURLLabel::useTips () const
00241 {
00242 return d->UseTips;
00243 }
00244
00245 const TQString& KURLLabel::tipText () const
00246 {
00247 return d->Tip;
00248 }
00249
00250 void KURLLabel::setHighlightedColor (const TQColor& highcolor)
00251 {
00252 d->LinkColor = highcolor;
00253
00254 if (!d->Timer->isActive())
00255 setLinkColor (highcolor);
00256 }
00257
00258 void KURLLabel::setHighlightedColor (const TQString& highcolor)
00259 {
00260 setHighlightedColor (TQColor (highcolor));
00261 }
00262
00263 void KURLLabel::setSelectedColor (const TQColor& selcolor)
00264 {
00265 d->HighlightedLinkColor = selcolor;
00266
00267 if (d->Timer->isActive())
00268 setLinkColor (selcolor);
00269 }
00270
00271 void KURLLabel::setSelectedColor (const TQString& selcolor)
00272 {
00273 setSelectedColor (TQColor (selcolor));
00274 }
00275
00276 void KURLLabel::setGlow (bool glow)
00277 {
00278 d->Glow = glow;
00279 }
00280
00281 void KURLLabel::setFloat (bool do_float)
00282 {
00283 d->Float = do_float;
00284 }
00285
00286 bool KURLLabel::isGlowEnabled () const
00287 {
00288 return d->Glow;
00289 }
00290
00291 bool KURLLabel::isFloatEnabled () const
00292 {
00293 return d->Float;
00294 }
00295
00296 void KURLLabel::setAltPixmap (const TQPixmap& altPix)
00297 {
00298 d->AltPixmap = altPix;
00299 }
00300
00301 const TQPixmap* KURLLabel::altPixmap () const
00302 {
00303 return &d->AltPixmap;
00304 }
00305
00306 void KURLLabel::enterEvent (TQEvent* e)
00307 {
00308 TQLabel::enterEvent (e);
00309
00310 TQRect r( activeRect() );
00311 if (!r.contains( TQT_TQMOUSEEVENT(e)->pos() ))
00312 return;
00313
00314 if (!d->AltPixmap.isNull() && pixmap())
00315 {
00316 d->RealPixmap = *pixmap();
00317 setPixmap (d->AltPixmap);
00318 }
00319
00320 if (d->Glow || d->Float)
00321 {
00322 d->Timer->stop();
00323
00324 setLinkColor (d->HighlightedLinkColor);
00325
00326 d->RealUnderline = d->Underline;
00327
00328 if (d->Float)
00329 setUnderline (true);
00330 }
00331
00332 emit enteredURL ();
00333 emit enteredURL (d->URL);
00334 }
00335
00336 void KURLLabel::leaveEvent (TQEvent* e)
00337 {
00338 TQLabel::leaveEvent (e);
00339
00340 if (!d->AltPixmap.isNull() && pixmap())
00341 setPixmap (d->RealPixmap);
00342
00343 if ((d->Glow || d->Float) && !d->Timer->isActive())
00344 setLinkColor (d->LinkColor);
00345
00346 setUnderline (d->RealUnderline);
00347
00348 emit leftURL ();
00349 emit leftURL (d->URL);
00350 }
00351
00352 bool KURLLabel::event (TQEvent *e)
00353 {
00354 if (e && e->type() == TQEvent::ParentPaletteChange)
00355 {
00356
00357 TQPalette p = parentWidget() ? parentWidget()->palette() : tqApp->palette();
00358 p.setBrush(TQColorGroup::Base, p.brush(TQPalette::Normal, TQColorGroup::Background));
00359 p.setColor(TQColorGroup::Foreground, palette().active().foreground());
00360 setPalette(p);
00361 d->LinkColor = TDEGlobalSettings::linkColor();
00362 setLinkColor(d->LinkColor);
00363 return true;
00364 }
00365 else if (e->type() == TQEvent::Paint) {
00366 const bool result = TQLabel::event(e);
00367 if (result && hasFocus()) {
00368 TQPainter p(this);
00369 TQRect r( activeRect() );
00370 style().tqdrawPrimitive( TQStyle::PE_FocusRect, &p, r, colorGroup() );
00371 }
00372 return result;
00373 }
00374 else if (e->type() == TQEvent::KeyPress) {
00375 TQKeyEvent* ke = TQT_TQKEYEVENT(e);
00376 if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return) {
00377 setLinkColor (d->HighlightedLinkColor);
00378 d->Timer->start (300);
00379 emit leftClickedURL ();
00380 emit leftClickedURL (d->URL);
00381 ke->accept();
00382 return true;
00383 }
00384 }
00385 else if (e->type() == TQEvent::MouseButtonPress) {
00386 TQRect r( activeRect() );
00387 d->MousePressed = r.contains(TQT_TQMOUSEEVENT(e)->pos());
00388 }
00389 else if (e->type() == TQEvent::MouseMove) {
00390 if (d->Cursor) {
00391 TQRect r( activeRect() );
00392 bool inside = r.contains(TQT_TQMOUSEEVENT(e)->pos());
00393 if (d->WasInsideRect != inside) {
00394 if (inside)
00395 TQLabel::setCursor(*d->Cursor);
00396 else
00397 TQLabel::unsetCursor();
00398 d->WasInsideRect = inside;
00399 }
00400 }
00401 }
00402 return TQLabel::event(e);
00403 }
00404
00405 TQRect KURLLabel::activeRect() const
00406 {
00407 TQRect r( contentsRect() );
00408 if (text().isEmpty() || (!d->MarginAltered && sizePolicy() == TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed)))
00409 return r;
00410 int hAlign = TQApplication::horizontalAlignment( alignment() );
00411 int indentX = (hAlign && indent()>0) ? indent() : 0;
00412 TQFontMetrics fm(font());
00413 r.setWidth( TQMIN(fm.width(text()), r.width()));
00414 if ( hAlign & AlignLeft )
00415 r.moveLeft(r.left() + indentX);
00416 if ( hAlign & AlignCenter )
00417 r.moveLeft((contentsRect().width()-r.width())/2+margin());
00418 if ( hAlign & AlignRight )
00419 r.moveLeft(contentsRect().width()-r.width()-indentX+margin());
00420 int add = TQMIN(3, margin());
00421 r = TQRect(r.left()-add, r.top()-add, r.width()+2*add, r.height()+2*add);
00422 return r;
00423 }
00424
00425 void KURLLabel::setMargin( int margin )
00426 {
00427 TQLabel::setMargin(margin);
00428 d->MarginAltered = true;
00429 }
00430
00431 void KURLLabel::setFocusPolicy( TQ_FocusPolicy policy )
00432 {
00433 TQLabel::setFocusPolicy(policy);
00434 if (!d->MarginAltered) {
00435 TQLabel::setMargin(policy == TQ_NoFocus ? 0 : 3);
00436 }
00437 }
00438
00439 void KURLLabel::setSizePolicy ( TQSizePolicy policy )
00440 {
00441 TQLabel::setSizePolicy(policy);
00442 if (!d->MarginAltered && policy.horData()==TQSizePolicy::Fixed && policy.verData()==TQSizePolicy::Fixed) {
00443 TQLabel::setMargin(0);
00444 }
00445 }
00446
00447 void KURLLabel::virtual_hook( int, void* )
00448 { }
00449
00450 #include "kurllabel.moc"