# kdeui Library API Documentation

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

# kurllabel.cpp

    00001 // kurllabel.cpp
    00002
    00003 // Copyright (C) 2000 Peter Putzer
    00004
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    00018 // 02111-1307  USA
    00019
    00020 #include <qcolor.h>
    00021 #include <qtimer.h>
    00022 #include <qtooltip.h>
    00023 #include <qpixmap.h>
    00024 #include <qapplication.h>
    00025
    00026 #include <kcursor.h>
    00027 #include <kglobalsettings.h>
    00028
    00029 #include "kurllabel.h"
    00030
    00031 class KURLLabel::Private
    00032 {
    00033 public:
    00034   Private (const QString& url, KURLLabel* label)
    00035     : URL (url),
    00036       Underline (true),
    00037       LinkColor (KGlobalSettings::linkColor()),
    00038       HighlightedLinkColor (Qt::red),
    00039       Tip(url),
    00040       Cursor (0L),
    00041       UseTips (false),
    00042       UseCursor (false),
    00043       Glow (true),
    00044       Float (false),
    00045       RealUnderline (true),
    00046       Timer (new QTimer (label))
    00047   {
    00048     connect (Timer, SIGNAL (timeout ()), label, SLOT (updateColor ()));
    00049   }
    00050
    00051   ~Private ()
    00052   {
    00053   }
    00054
    00055   QString URL;
    00056   QPixmap AltPixmap;
    00057
    00058   bool Underline;
    00059   QColor LinkColor;
    00060   QColor HighlightedLinkColor;
    00061
    00062   QString Tip;
    00063   QCursor* Cursor;
    00064   bool UseTips:1;
    00065   bool UseCursor:1;
    00066   bool Glow:1;
    00067   bool Float:1;
    00068   bool RealUnderline:1;
    00069   QPixmap RealPixmap;
    00070
    00071   QTimer* Timer;
    00072 };
    00073
    00074 KURLLabel::KURLLabel (const QString& url, const QString& text,
    00075                         QWidget* parent, const char* name)
    00076   : QLabel (!text.isNull() ? text : url, parent, name),
    00077     d (new Private (url, this))
    00078 {
    00079   setFont (font());
    00080   setCursor (KCursor::handCursor());
    00081   setLinkColor (d->LinkColor);
    00082 }
    00083
    00084 KURLLabel::KURLLabel (QWidget* parent, const char* name)
    00085   : QLabel (parent, name),
    00086     d (new Private (QString::null, this))
    00087 {
    00088   setFont (font());
    00089   setCursor (KCursor::handCursor());
    00090   setLinkColor (d->LinkColor);
    00091 }
    00092
    00093 KURLLabel::~KURLLabel ()
    00094 {
    00095   delete d;
    00096 }
    00097
    00098 void KURLLabel::mouseReleaseEvent (QMouseEvent* e)
    00099 {
    00100   QLabel::mouseReleaseEvent (e);
    00101
    00102   setLinkColor (d->HighlightedLinkColor);
    00103   d->Timer->start (300);
    00104
    00105   switch (e->button())
    00106     {
    00107     case LeftButton:
    00108       emit leftClickedURL ();
    00109       emit leftClickedURL (d->URL);
    00110       break;
    00111
    00112     case MidButton:
    00113       emit middleClickedURL ();
    00114       emit middleClickedURL (d->URL);
    00115       break;
    00116
    00117     case RightButton:
    00118       emit rightClickedURL ();
    00119       emit rightClickedURL (d->URL);
    00120       break;
    00121
    00122     default:
    00123       ; // nothing
    00124     }
    00125 }
    00126
    00127 void KURLLabel::setFont (const QFont& f)
    00128 {
    00129   QFont newFont = f;
    00130   newFont.setUnderline (d->Underline);
    00131
    00132   QLabel::setFont (newFont);
    00133 }
    00134
    00135 void KURLLabel::setUnderline (bool on)
    00136 {
    00137   d->Underline = on;
    00138
    00139   setFont (font());
    00140 }
    00141
    00142 void KURLLabel::updateColor ()
    00143 {
    00144   d->Timer->stop();
    00145
    00146   if (!(d->Glow || d->Float) || !rect().contains (mapFromGlobal(QCursor::pos())))
    00147     setLinkColor (d->LinkColor);
    00148 }
    00149
    00150 void KURLLabel::setLinkColor (const QColor& col)
    00151 {
    00152   QPalette p = palette();
    00153   p.setColor (QColorGroup::Foreground, col);
    00154   setPalette (p);
    00155
    00156   update();
    00157 }
    00158
    00159 void KURLLabel::setURL (const QString& url)
    00160 {
    00161   if ( d->Tip == d->URL ) { // update the tip as well
    00162     d->Tip = url;
    00163     setUseTips( d->UseTips );
    00164   }
    00165
    00166   d->URL = url;
    00167 }
    00168
    00169 const QString& KURLLabel::url () const
    00170 {
    00171   return d->URL;
    00172 }
    00173
    00174 void KURLLabel::setUseCursor (bool on, QCursor* cursor)
    00175 {
    00176   d->UseCursor = on;
    00177   d->Cursor = cursor;
    00178
    00179   if (on)
    00180     {
    00181       if (cursor)
    00182         setCursor (*cursor);
    00183       else
    00184         setCursor (KCursor::handCursor());
    00185     }
    00186   else
    00187     unsetCursor();
    00188 }
    00189
    00190 bool KURLLabel::useCursor () const
    00191 {
    00192   return d->UseCursor;
    00193 }
    00194
    00195 void KURLLabel::setUseTips (bool on)
    00196 {
    00197   d->UseTips = on;
    00198
    00199   if (on)
    00200     QToolTip::add (this, d->Tip);
    00201   else
    00202     QToolTip::remove (this);
    00203 }
    00204
    00205 void KURLLabel::setTipText (const QString& tip)
    00206 {
    00207   d->Tip = tip;
    00208
    00209   setUseTips (d->UseTips);
    00210 }
    00211
    00212 bool KURLLabel::useTips () const
    00213 {
    00214   return d->UseTips;
    00215 }
    00216
    00217 const QString& KURLLabel::tipText () const
    00218 {
    00219   return d->Tip;
    00220 }
    00221
    00222 void KURLLabel::setHighlightedColor (const QColor& highcolor)
    00223 {
    00224   d->LinkColor = highcolor;
    00225
    00226   if (!d->Timer->isActive())
    00227     setLinkColor (highcolor);
    00228 }
    00229
    00230 void KURLLabel::setHighlightedColor (const QString& highcolor)
    00231 {
    00232   setHighlightedColor (QColor (highcolor));
    00233 }
    00234
    00235 void KURLLabel::setSelectedColor (const QColor& selcolor)
    00236 {
    00237   d->HighlightedLinkColor = selcolor;
    00238
    00239   if (d->Timer->isActive())
    00240     setLinkColor (selcolor);
    00241 }
    00242
    00243 void KURLLabel::setSelectedColor (const QString& selcolor)
    00244 {
    00245   setSelectedColor (QColor (selcolor));
    00246 }
    00247
    00248 void KURLLabel::setGlow (bool glow)
    00249 {
    00250   d->Glow = glow;
    00251 }
    00252
    00253 void KURLLabel::setFloat (bool do_float)
    00254 {
    00255   d->Float = do_float;
    00256 }
    00257
    00258 bool KURLLabel::isGlowEnabled () const
    00259 {
    00260   return d->Glow;
    00261 }
    00262
    00263 bool KURLLabel::isFloatEnabled () const
    00264 {
    00265   return d->Float;
    00266 }
    00267
    00268 void KURLLabel::setAltPixmap (const QPixmap& altPix)
    00269 {
    00270   d->AltPixmap = altPix;
    00271 }
    00272
    00273 const QPixmap* KURLLabel::altPixmap () const
    00274 {
    00275   return &d->AltPixmap;
    00276 }
    00277
    00278 void KURLLabel::enterEvent (QEvent* e)
    00279 {
    00280   QLabel::enterEvent (e);
    00281
    00282   if (!d->AltPixmap.isNull() && pixmap())
    00283     {
    00284       d->RealPixmap = *pixmap();
    00285       setPixmap (d->AltPixmap);
    00286     }
    00287
    00288   if (d->Glow || d->Float)
    00289     {
    00290       d->Timer->stop();
    00291
    00292       setLinkColor (d->HighlightedLinkColor);
    00293
    00294       d->RealUnderline = d->Underline;
    00295
    00296       if (d->Float)
    00297         setUnderline (true);
    00298     }
    00299
    00300   emit enteredURL ();
    00301   emit enteredURL (d->URL);
    00302 }
    00303
    00304 void KURLLabel::leaveEvent (QEvent* e)
    00305 {
    00306   QLabel::leaveEvent (e);
    00307
    00308   if (!d->AltPixmap.isNull() && pixmap())
    00309     setPixmap (d->RealPixmap);
    00310
    00311   if ((d->Glow || d->Float) && !d->Timer->isActive())
    00312     setLinkColor (d->LinkColor);
    00313
    00314   setUnderline (d->RealUnderline);
    00315
    00316   emit leftURL ();
    00317   emit leftURL (d->URL);
    00318 }
    00319
    00320 bool KURLLabel::event (QEvent *e)
    00321 {
    00322   if (e && e->type() == QEvent::ParentPaletteChange)
    00323   {
    00324     // use parentWidget() unless you are a toplevel widget, then try qAapp
    00325     QPalette p = parentWidget() ? parentWidget()->palette() : qApp->palette();
    00326     p.setBrush(QColorGroup::Base, p.brush(QPalette::Normal, QColorGroup::Background));
    00327     p.setColor(QColorGroup::Foreground, palette().active().foreground());
    00328     setPalette(p);
    00329     d->LinkColor = KGlobalSettings::linkColor();
    00330     setLinkColor(d->LinkColor);
    00331     return true;
    00332   }
    00333   else
    00334     return QLabel::event(e);
    00335 }
    00336
    00337
    00338 void KURLLabel::virtual_hook( int, void* )
    00339 { /*BASE::virtual_hook( id, data );*/ }
    00340
    00341 #include "kurllabel.moc"

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:24 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003