# kdeui Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kurllabel.cpp

    00001 // (c) 2000 Peter Putzer
    00002
    00003 #include <qcolor.h>
    00004 #include <qtimer.h>
    00005 #include <qtooltip.h>
    00006 #include <qpixmap.h>
    00007
    00008 #include <kcursor.h>
    00009 #include <kglobalsettings.h>
    00010
    00011 #include "kurllabel.h"
    00012
    00013 class KURLLabel::Private
    00014 {
    00015 public:
    00016   Private (const QString& url, KURLLabel* label)
    00017     : URL (url),
    00018       Underline (true),
    00019       LinkColor (KGlobalSettings::linkColor()),
    00020       HighlightedLinkColor (Qt::red),
    00021       Tip(url),
    00022       Cursor (0L),
    00023       UseTips (false),
    00024       UseCursor (false),
    00025       Glow (true),
    00026       Float (false),
    00027       RealUnderline (true),
    00028       Timer (new QTimer (label))
    00029   {
    00030     connect (Timer, SIGNAL (timeout ()), label, SLOT (updateColor ()));
    00031   }
    00032
    00033   ~Private ()
    00034   {
    00035   }
    00036
    00037   QString URL;
    00038   QPixmap AltPixmap;
    00039
    00040   bool Underline;
    00041   QColor LinkColor;
    00042   QColor HighlightedLinkColor;
    00043
    00044   QString Tip;
    00045   QCursor* Cursor;
    00046   bool UseTips:1;
    00047   bool UseCursor:1;
    00048   bool Glow:1;
    00049   bool Float:1;
    00050   bool RealUnderline:1;
    00051   QPixmap RealPixmap;
    00052
    00053   QTimer* Timer;
    00054 };
    00055
    00056 KURLLabel::KURLLabel (const QString& url, const QString& text,
    00057                         QWidget* parent, const char* name)
    00058   : QLabel (!text.isNull() ? text : url, parent, name),
    00059     d (new Private (url, this))
    00060 {
    00061   setFont (font());
    00062   setCursor (KCursor::handCursor());
    00063   setLinkColor (d->LinkColor);
    00064 }
    00065
    00066 KURLLabel::KURLLabel (QWidget* parent, const char* name)
    00067   : QLabel (parent, name),
    00068     d (new Private (QString::null, this))
    00069 {
    00070   setFont (font());
    00071   setCursor (KCursor::handCursor());
    00072   setLinkColor (d->LinkColor);
    00073 }
    00074
    00075 KURLLabel::~KURLLabel ()
    00076 {
    00077   delete d;
    00078 }
    00079
    00080 void KURLLabel::mouseReleaseEvent (QMouseEvent* e)
    00081 {
    00082   QLabel::mouseReleaseEvent (e);
    00083
    00084   setLinkColor (d->HighlightedLinkColor);
    00085   d->Timer->start (300);
    00086
    00087   switch (e->button())
    00088     {
    00089     case LeftButton:
    00090       emit leftClickedURL ();
    00091       emit leftClickedURL (d->URL);
    00092       break;
    00093
    00094     case MidButton:
    00095       emit middleClickedURL ();
    00096       emit middleClickedURL (d->URL);
    00097       break;
    00098
    00099     case RightButton:
    00100       emit rightClickedURL ();
    00101       emit rightClickedURL (d->URL);
    00102       break;
    00103
    00104     default:
    00105       ; // nothing
    00106     }
    00107 }
    00108
    00109 void KURLLabel::setFont (const QFont& f)
    00110 {
    00111   QFont newFont = f;
    00112   newFont.setUnderline (d->Underline);
    00113
    00114   QLabel::setFont (newFont);
    00115 }
    00116
    00117 void KURLLabel::setUnderline (bool on)
    00118 {
    00119   d->Underline = on;
    00120
    00121   setFont (font());
    00122 }
    00123
    00124 void KURLLabel::updateColor ()
    00125 {
    00126   d->Timer->stop();
    00127
    00128   if (!(d->Glow || d->Float) || !rect().contains (mapFromGlobal(QCursor::pos())))
    00129     setLinkColor (d->LinkColor);
    00130 }
    00131
    00132 void KURLLabel::setLinkColor (const QColor& col)
    00133 {
    00134   QPalette p = palette();
    00135   p.setColor (QColorGroup::Foreground, col);
    00136   setPalette (p);
    00137
    00138   update();
    00139 }
    00140
    00141 void KURLLabel::setURL (const QString& url)
    00142 {
    00143   if ( d->Tip == d->URL ) { // update the tip as well
    00144     d->Tip = url;
    00145     setUseTips( d->UseTips );
    00146   }
    00147
    00148   d->URL = url;
    00149 }
    00150
    00151 const QString& KURLLabel::url () const
    00152 {
    00153   return d->URL;
    00154 }
    00155
    00156 void KURLLabel::setUseCursor (bool on, QCursor* cursor)
    00157 {
    00158   d->UseCursor = on;
    00159   d->Cursor = cursor;
    00160
    00161   if (on)
    00162     {
    00163       if (cursor)
    00164         setCursor (*cursor);
    00165       else
    00166         setCursor (KCursor::handCursor());
    00167     }
    00168 }
    00169
    00170 bool KURLLabel::useCursor () const
    00171 {
    00172   return d->UseCursor;
    00173 }
    00174
    00175 void KURLLabel::setUseTips (bool on)
    00176 {
    00177   d->UseTips = on;
    00178
    00179   if (on)
    00180     QToolTip::add (this, d->Tip);
    00181   else
    00182     QToolTip::remove (this);
    00183 }
    00184
    00185 void KURLLabel::setTipText (const QString& tip)
    00186 {
    00187   d->Tip = tip;
    00188
    00189   setUseTips (d->UseTips);
    00190 }
    00191
    00192 bool KURLLabel::useTips () const
    00193 {
    00194   return d->UseTips;
    00195 }
    00196
    00197 const QString& KURLLabel::tipText () const
    00198 {
    00199   return d->Tip;
    00200 }
    00201
    00202 void KURLLabel::setHighlightedColor (const QColor& highcolor)
    00203 {
    00204   d->LinkColor = highcolor;
    00205
    00206   if (!d->Timer->isActive())
    00207     setLinkColor (highcolor);
    00208 }
    00209
    00210 void KURLLabel::setHighlightedColor (const QString& highcolor)
    00211 {
    00212   setHighlightedColor (QColor (highcolor));
    00213 }
    00214
    00215 void KURLLabel::setSelectedColor (const QColor& selcolor)
    00216 {
    00217   d->HighlightedLinkColor = selcolor;
    00218
    00219   if (d->Timer->isActive())
    00220     setLinkColor (selcolor);
    00221 }
    00222
    00223 void KURLLabel::setSelectedColor (const QString& selcolor)
    00224 {
    00225   setSelectedColor (QColor (selcolor));
    00226 }
    00227
    00228 void KURLLabel::setGlow (bool glow)
    00229 {
    00230   d->Glow = glow;
    00231 }
    00232
    00233 void KURLLabel::setFloat (bool do_float)
    00234 {
    00235   d->Float = do_float;
    00236 }
    00237
    00238 bool KURLLabel::isGlowEnabled () const
    00239 {
    00240   return d->Glow;
    00241 }
    00242
    00243 bool KURLLabel::isFloatEnabled () const
    00244 {
    00245   return d->Float;
    00246 }
    00247
    00248 void KURLLabel::setAltPixmap (const QPixmap& altPix)
    00249 {
    00250   d->AltPixmap = altPix;
    00251 }
    00252
    00253 const QPixmap* KURLLabel::altPixmap () const
    00254 {
    00255   return &d->AltPixmap;
    00256 }
    00257
    00258 void KURLLabel::enterEvent (QEvent* e)
    00259 {
    00260   QLabel::enterEvent (e);
    00261
    00262   if (!d->AltPixmap.isNull() && pixmap())
    00263     {
    00264       d->RealPixmap = *pixmap();
    00265       setPixmap (d->AltPixmap);
    00266     }
    00267
    00268   if (d->Glow || d->Float)
    00269     {
    00270       d->Timer->stop();
    00271
    00272       setLinkColor (d->HighlightedLinkColor);
    00273
    00274       d->RealUnderline = d->Underline;
    00275
    00276       if (d->Float)
    00277         setUnderline (true);
    00278     }
    00279
    00280   emit enteredURL ();
    00281   emit enteredURL (d->URL);
    00282 }
    00283
    00284 void KURLLabel::leaveEvent (QEvent* e)
    00285 {
    00286   QLabel::leaveEvent (e);
    00287
    00288   if (!d->AltPixmap.isNull() && pixmap())
    00289     setPixmap (d->RealPixmap);
    00290
    00291   if ((d->Glow || d->Float) && !d->Timer->isActive())
    00292     setLinkColor (d->LinkColor);
    00293
    00294   setUnderline (d->RealUnderline);
    00295
    00296   emit leftURL ();
    00297   emit leftURL (d->URL);
    00298 }
    00299
    00300 void KURLLabel::virtual_hook( int, void* )
    00301 { /*BASE::virtual_hook( id, data );*/ }
    00302
    00303 #include "kurllabel.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:57:58 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001