kurllabel.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1998 Kurt Granroth <granroth@kde.org>
00003    Copyright (C) 2000 Peter Putzer <putzer@kde.org>
00004    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
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    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qcolor.h>
00022 #include <qtimer.h>
00023 #include <qtooltip.h>
00024 #include <qpixmap.h>
00025 #include <qpainter.h>
00026 #include <qstyle.h>
00027 #include <qapplication.h>
00028 
00029 #include <kcursor.h>
00030 #include <kglobalsettings.h>
00031 
00032 #include "kurllabel.h"
00033 
00034 class KURLLabel::Private
00035 {
00036 public:
00037   Private (const QString& url, KURLLabel* label)
00038     : URL (url),
00039       Underline (true),
00040       LinkColor (KGlobalSettings::linkColor()),
00041       HighlightedLinkColor (Qt::red),
00042       Tip(url),
00043       Cursor (0L),
00044       UseTips (false),
00045       UseCursor (false),
00046       Glow (true),
00047       Float (false),
00048       RealUnderline (true),
00049       Timer (new QTimer (label))
00050   {
00051     connect (Timer, SIGNAL (timeout ()), label, SLOT (updateColor ()));
00052   }
00053 
00054   ~Private ()
00055   {
00056   }
00057 
00058   QString URL;
00059   QPixmap AltPixmap;
00060 
00061   bool Underline;
00062   QColor LinkColor;
00063   QColor HighlightedLinkColor;
00064 
00065   QString Tip;
00066   QCursor* Cursor;
00067   bool UseTips:1;
00068   bool UseCursor:1;
00069   bool Glow:1;
00070   bool Float:1;
00071   bool RealUnderline:1;
00072   QPixmap RealPixmap;
00073 
00074   QTimer* Timer;
00075 };
00076 
00077 KURLLabel::KURLLabel (const QString& url, const QString& text,
00078                         QWidget* parent, const char* name)
00079   : QLabel (!text.isNull() ? text : url, parent, name),
00080     d (new Private (url, this))
00081 {
00082   setFont (font());
00083   setCursor (KCursor::handCursor());
00084   setLinkColor (d->LinkColor);
00085   setMargin(3); //better default : better look when focused
00086   setFocusPolicy( QWidget::StrongFocus ); //better accessibility
00087 }
00088 
00089 KURLLabel::KURLLabel (QWidget* parent, const char* name)
00090   : QLabel (parent, name),
00091     d (new Private (QString::null, this))
00092 {
00093   setFont (font());
00094   setCursor (KCursor::handCursor());
00095   setLinkColor (d->LinkColor);
00096   setMargin(3); //better default : better look when focused
00097   setFocusPolicy( QWidget::StrongFocus ); //better accessibility
00098 }
00099 
00100 KURLLabel::~KURLLabel ()
00101 {
00102   delete d;
00103 }
00104 
00105 void KURLLabel::mouseReleaseEvent (QMouseEvent* e)
00106 {
00107   QLabel::mouseReleaseEvent (e);
00108 
00109   setLinkColor (d->HighlightedLinkColor);
00110   d->Timer->start (300);
00111 
00112   switch (e->button())
00113     {
00114     case LeftButton:
00115       emit leftClickedURL ();
00116       emit leftClickedURL (d->URL);
00117       break;
00118 
00119     case MidButton:
00120       emit middleClickedURL ();
00121       emit middleClickedURL (d->URL);
00122       break;
00123 
00124     case RightButton:
00125       emit rightClickedURL ();
00126       emit rightClickedURL (d->URL);
00127       break;
00128 
00129     default:
00130       ; // nothing
00131     }
00132 }
00133 
00134 void KURLLabel::setFont (const QFont& f)
00135 {
00136   QFont newFont = f;
00137   newFont.setUnderline (d->Underline);
00138 
00139   QLabel::setFont (newFont);
00140 }
00141 
00142 void KURLLabel::setUnderline (bool on)
00143 {
00144   d->Underline = on;
00145 
00146   setFont (font());
00147 }
00148 
00149 void KURLLabel::updateColor ()
00150 {
00151   d->Timer->stop();
00152 
00153   if (!(d->Glow || d->Float) || !rect().contains (mapFromGlobal(QCursor::pos())))
00154     setLinkColor (d->LinkColor);
00155 }
00156 
00157 void KURLLabel::setLinkColor (const QColor& col)
00158 {
00159   QPalette p = palette();
00160   p.setColor (QColorGroup::Foreground, col);
00161   setPalette (p);
00162 
00163   update();
00164 }
00165 
00166 void KURLLabel::setURL (const QString& url)
00167 {
00168   if ( d->Tip == d->URL ) { // update the tip as well
00169     d->Tip = url;
00170     setUseTips( d->UseTips );
00171   }
00172 
00173   d->URL = url;
00174 }
00175 
00176 const QString& KURLLabel::url () const
00177 {
00178   return d->URL;
00179 }
00180 
00181 void KURLLabel::setUseCursor (bool on, QCursor* cursor)
00182 {
00183   d->UseCursor = on;
00184   d->Cursor = cursor;
00185 
00186   if (on)
00187     {
00188       if (cursor)
00189         setCursor (*cursor);
00190       else
00191         setCursor (KCursor::handCursor());
00192     }
00193   else
00194     unsetCursor();
00195 }
00196 
00197 bool KURLLabel::useCursor () const
00198 {
00199   return d->UseCursor;
00200 }
00201 
00202 void KURLLabel::setUseTips (bool on)
00203 {
00204   d->UseTips = on;
00205 
00206   if (on)
00207     QToolTip::add (this, d->Tip);
00208   else
00209     QToolTip::remove (this);
00210 }
00211 
00212 void KURLLabel::setTipText (const QString& tip)
00213 {
00214   d->Tip = tip;
00215 
00216   setUseTips (d->UseTips);
00217 }
00218 
00219 bool KURLLabel::useTips () const
00220 {
00221   return d->UseTips;
00222 }
00223 
00224 const QString& KURLLabel::tipText () const
00225 {
00226   return d->Tip;
00227 }
00228 
00229 void KURLLabel::setHighlightedColor (const QColor& highcolor)
00230 {
00231   d->LinkColor = highcolor;
00232 
00233   if (!d->Timer->isActive())
00234     setLinkColor (highcolor);
00235 }
00236 
00237 void KURLLabel::setHighlightedColor (const QString& highcolor)
00238 {
00239   setHighlightedColor (QColor (highcolor));
00240 }
00241 
00242 void KURLLabel::setSelectedColor (const QColor& selcolor)
00243 {
00244   d->HighlightedLinkColor = selcolor;
00245 
00246   if (d->Timer->isActive())
00247     setLinkColor (selcolor);
00248 }
00249 
00250 void KURLLabel::setSelectedColor (const QString& selcolor)
00251 {
00252   setSelectedColor (QColor (selcolor));
00253 }
00254 
00255 void KURLLabel::setGlow (bool glow)
00256 {
00257   d->Glow = glow;
00258 }
00259 
00260 void KURLLabel::setFloat (bool do_float)
00261 {
00262   d->Float = do_float;
00263 }
00264 
00265 bool KURLLabel::isGlowEnabled () const
00266 {
00267   return d->Glow;
00268 }
00269 
00270 bool KURLLabel::isFloatEnabled () const
00271 {
00272   return d->Float;
00273 }
00274 
00275 void KURLLabel::setAltPixmap (const QPixmap& altPix)
00276 {
00277   d->AltPixmap = altPix;
00278 }
00279 
00280 const QPixmap* KURLLabel::altPixmap () const
00281 {
00282   return &d->AltPixmap;
00283 }
00284 
00285 void KURLLabel::enterEvent (QEvent* e)
00286 {
00287   QLabel::enterEvent (e);
00288 
00289   if (!d->AltPixmap.isNull() && pixmap())
00290     {
00291       d->RealPixmap = *pixmap();
00292       setPixmap (d->AltPixmap);
00293     }
00294 
00295   if (d->Glow || d->Float)
00296     {
00297       d->Timer->stop();
00298 
00299       setLinkColor (d->HighlightedLinkColor);
00300 
00301       d->RealUnderline = d->Underline;
00302 
00303       if (d->Float)
00304         setUnderline (true);
00305     }
00306 
00307   emit enteredURL ();
00308   emit enteredURL (d->URL);
00309 }
00310 
00311 void KURLLabel::leaveEvent (QEvent* e)
00312 {
00313   QLabel::leaveEvent (e);
00314 
00315   if (!d->AltPixmap.isNull() && pixmap())
00316     setPixmap (d->RealPixmap);
00317 
00318   if ((d->Glow || d->Float) && !d->Timer->isActive())
00319     setLinkColor (d->LinkColor);
00320 
00321   setUnderline (d->RealUnderline);
00322 
00323   emit leftURL ();
00324   emit leftURL (d->URL);
00325 }
00326 
00327 bool KURLLabel::event (QEvent *e)
00328 {
00329   if (e && e->type() == QEvent::ParentPaletteChange)
00330   {
00331     // use parentWidget() unless you are a toplevel widget, then try qAapp
00332     QPalette p = parentWidget() ? parentWidget()->palette() : qApp->palette();
00333     p.setBrush(QColorGroup::Base, p.brush(QPalette::Normal, QColorGroup::Background));
00334     p.setColor(QColorGroup::Foreground, palette().active().foreground());
00335     setPalette(p);
00336     d->LinkColor = KGlobalSettings::linkColor();
00337     setLinkColor(d->LinkColor);
00338     return true;
00339   }
00340   else if (e->type() == QEvent::Paint) {
00341     QPaintEvent* pe = static_cast<QPaintEvent*>(e);
00342     bool result = QLabel::event(e);
00343     if (result && hasFocus()) {
00344         QPainter p(this);
00345         QRect r(contentsRect());
00346         int hAlign = QApplication::horizontalAlignment( alignment() );
00347         int indentX = (hAlign && indent()>0) ? indent() : 0;
00348         QFontMetrics fm(font());
00349         r.setWidth( QMIN(fm.width(text()), r.width()));
00350         if ( hAlign & AlignLeft )
00351             r.moveLeft(r.left() + indentX);
00352         if ( hAlign & AlignCenter )
00353             r.moveLeft((contentsRect().width()-r.width())/2+margin());
00354         if ( hAlign & AlignRight )
00355             r.moveLeft(contentsRect().width()-r.width()-indentX+margin());
00356         int add = QMIN(3, margin());
00357         r = QRect(r.left()-add, r.top()-add, r.width()+2*add, r.height()+2*add);
00358         style().drawPrimitive( QStyle::PE_FocusRect, &p, r, colorGroup() );
00359     }
00360     return result;
00361   }
00362   else if (e->type() == QEvent::KeyPress) {
00363     QKeyEvent* ke = static_cast<QKeyEvent*>(e);
00364     if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return) {
00365       setLinkColor (d->HighlightedLinkColor);
00366       d->Timer->start (300);
00367       emit leftClickedURL ();
00368       emit leftClickedURL (d->URL);
00369       ke->accept();
00370       return true;
00371     }
00372   }
00373   return QLabel::event(e);  
00374 }
00375 
00376 
00377 void KURLLabel::virtual_hook( int, void* )
00378 { /*BASE::virtual_hook( id, data );*/ }
00379 
00380 #include "kurllabel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys