kurllabel.cpp
/* This file is part of the KDE libraries
Copyright (C) 1998 Kurt Granroth <granroth@kde.org>
Copyright (C) 2000 Peter Putzer <putzer@kde.org>
Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <qcolor.h>
#include <qtimer.h>
#include <qtooltip.h>
#include <qpixmap.h>
#include <qpainter.h>
#include <qstyle.h>
#include <qapplication.h>
#include <kcursor.h>
#include <kglobalsettings.h>
#include "kurllabel.h"
class KURLLabel::Private
{
public:
Private (const QString& url, KURLLabel* label)
: URL (url),
LinkColor (KGlobalSettings::linkColor()),
HighlightedLinkColor (Qt::red),
Tip(url),
Cursor (0L),
Underline (true),
UseTips (false),
Glow (true),
Float (false),
RealUnderline (true),
MousePressed(false),
WasInsideRect(false),
MarginAltered(false),
Timer (new QTimer (label))
{
connect (Timer, SIGNAL (timeout ()), label, SLOT (updateColor ()));
}
~Private ()
{
delete Cursor;
}
QString URL;
QPixmap AltPixmap;
QColor LinkColor;
QColor HighlightedLinkColor;
QString Tip;
QCursor* Cursor;
bool Underline:1;
bool UseTips:1;
bool Glow:1;
bool Float:1;
bool RealUnderline:1;
bool MousePressed:1;
bool WasInsideRect:1;
bool MarginAltered:1;
QPixmap RealPixmap;
QTimer* Timer;
};
KURLLabel::KURLLabel (const QString& url, const QString& text,
QWidget* parent, const char* name)
: QLabel (!text.isNull() ? text : url, parent, name),
d (new Private (url, this))
{
setFont (font());
setUseCursor (true);
setLinkColor (d->LinkColor);
setFocusPolicy( QWidget::StrongFocus ); //better accessibility
setMouseTracking (true);
}
KURLLabel::KURLLabel (QWidget* parent, const char* name)
: QLabel (parent, name),
d (new Private (QString::null, this))
{
setFont (font());
setUseCursor (true);
setLinkColor (d->LinkColor);
setFocusPolicy( QWidget::StrongFocus ); //better accessibility
setMouseTracking (true);
}
KURLLabel::~KURLLabel ()
{
delete d;
}
void KURLLabel::mouseReleaseEvent (QMouseEvent* e)
{
QLabel::mouseReleaseEvent (e);
if (!d->MousePressed)
return;
d->MousePressed = false;
QRect r( activeRect() );
if (!r.contains(e->pos()))
return;
setLinkColor (d->HighlightedLinkColor);
d->Timer->start (300);
switch (e->button())
{
case LeftButton:
emit leftClickedURL ();
emit leftClickedURL (d->URL);
break;
case MidButton:
emit middleClickedURL ();
emit middleClickedURL (d->URL);
break;
case RightButton:
emit rightClickedURL ();
emit rightClickedURL (d->URL);
break;
default:
; // nothing
}
}
void KURLLabel::setFont (const QFont& f)
{
QFont newFont = f;
newFont.setUnderline (d->Underline);
QLabel::setFont (newFont);
}
void KURLLabel::setUnderline (bool on)
{
d->Underline = on;
setFont (font());
}
void KURLLabel::updateColor ()
{
d->Timer->stop();
QRect r( activeRect() );
if (!(d->Glow || d->Float) || !r.contains (mapFromGlobal(QCursor::pos())))
setLinkColor (d->LinkColor);
}
void KURLLabel::setLinkColor (const QColor& col)
{
QPalette p = palette();
p.setColor (QColorGroup::Foreground, col);
setPalette (p);
update();
}
void KURLLabel::setURL (const QString& url)
{
if ( d->Tip == d->URL ) { // update the tip as well
d->Tip = url;
setUseTips( d->UseTips );
}
d->URL = url;
}
const QString& KURLLabel::url () const
{
return d->URL;
}
void KURLLabel::unsetCursor ()
{
delete d->Cursor;
d->Cursor = 0;
}
void KURLLabel::setCursor ( const QCursor& cursor )
{
delete d->Cursor;
d->Cursor = new QCursor( cursor );
}
void KURLLabel::setUseCursor (bool on, QCursor* cursor)
{
if (on)
{
if (cursor)
KURLLabel::setCursor (*cursor);
else
KURLLabel::setCursor (KCursor::handCursor());
}
else
KURLLabel::unsetCursor ();
}
bool KURLLabel::useCursor () const
{
return d->Cursor;
}
void KURLLabel::setUseTips (bool on)
{
d->UseTips = on;
if (on) {
QToolTip::add (this, activeRect(), d->Tip);
} else
QToolTip::remove (this);
}
void KURLLabel::setTipText (const QString& tip)
{
d->Tip = tip;
setUseTips (d->UseTips);
}
bool KURLLabel::useTips () const
{
return d->UseTips;
}
const QString& KURLLabel::tipText () const
{
return d->Tip;
}
void KURLLabel::setHighlightedColor (const QColor& highcolor)
{
d->LinkColor = highcolor;
if (!d->Timer->isActive())
setLinkColor (highcolor);
}
void KURLLabel::setHighlightedColor (const QString& highcolor)
{
setHighlightedColor (QColor (highcolor));
}
void KURLLabel::setSelectedColor (const QColor& selcolor)
{
d->HighlightedLinkColor = selcolor;
if (d->Timer->isActive())
setLinkColor (selcolor);
}
void KURLLabel::setSelectedColor (const QString& selcolor)
{
setSelectedColor (QColor (selcolor));
}
void KURLLabel::setGlow (bool glow)
{
d->Glow = glow;
}
void KURLLabel::setFloat (bool do_float)
{
d->Float = do_float;
}
bool KURLLabel::isGlowEnabled () const
{
return d->Glow;
}
bool KURLLabel::isFloatEnabled () const
{
return d->Float;
}
void KURLLabel::setAltPixmap (const QPixmap& altPix)
{
d->AltPixmap = altPix;
}
const QPixmap* KURLLabel::altPixmap () const
{
return &d->AltPixmap;
}
void KURLLabel::enterEvent (QEvent* e)
{
QLabel::enterEvent (e);
QRect r( activeRect() );
if (!r.contains( static_cast<QMouseEvent*>(e)->pos() ))
return;
if (!d->AltPixmap.isNull() && pixmap())
{
d->RealPixmap = *pixmap();
setPixmap (d->AltPixmap);
}
if (d->Glow || d->Float)
{
d->Timer->stop();
setLinkColor (d->HighlightedLinkColor);
d->RealUnderline = d->Underline;
if (d->Float)
setUnderline (true);
}
emit enteredURL ();
emit enteredURL (d->URL);
}
void KURLLabel::leaveEvent (QEvent* e)
{
QLabel::leaveEvent (e);
if (!d->AltPixmap.isNull() && pixmap())
setPixmap (d->RealPixmap);
if ((d->Glow || d->Float) && !d->Timer->isActive())
setLinkColor (d->LinkColor);
setUnderline (d->RealUnderline);
emit leftURL ();
emit leftURL (d->URL);
}
bool KURLLabel::event (QEvent *e)
{
if (e && e->type() == QEvent::ParentPaletteChange)
{
// use parentWidget() unless you are a toplevel widget, then try qAapp
QPalette p = parentWidget() ? parentWidget()->palette() : qApp->palette();
p.setBrush(QColorGroup::Base, p.brush(QPalette::Normal, QColorGroup::Background));
p.setColor(QColorGroup::Foreground, palette().active().foreground());
setPalette(p);
d->LinkColor = KGlobalSettings::linkColor();
setLinkColor(d->LinkColor);
return true;
}
else if (e->type() == QEvent::Paint) {
const bool result = QLabel::event(e);
if (result && hasFocus()) {
QPainter p(this);
QRect r( activeRect() );
style().drawPrimitive( QStyle::PE_FocusRect, &p, r, colorGroup() );
}
return result;
}
else if (e->type() == QEvent::KeyPress) {
QKeyEvent* ke = static_cast<QKeyEvent*>(e);
if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return) {
setLinkColor (d->HighlightedLinkColor);
d->Timer->start (300);
emit leftClickedURL ();
emit leftClickedURL (d->URL);
ke->accept();
return true;
}
}
else if (e->type() == QEvent::MouseButtonPress) {
QRect r( activeRect() );
d->MousePressed = r.contains(static_cast<QMouseEvent*>(e)->pos());
}
else if (e->type() == QEvent::MouseMove) {
if (d->Cursor) {
QRect r( activeRect() );
bool inside = r.contains(static_cast<QMouseEvent*>(e)->pos());
if (d->WasInsideRect != inside) {
if (inside)
QLabel::setCursor(*d->Cursor);
else
QLabel::unsetCursor();
d->WasInsideRect = inside;
}
}
}
return QLabel::event(e);
}
QRect KURLLabel::activeRect() const
{
QRect r( contentsRect() );
if (text().isEmpty() || (!d->MarginAltered && sizePolicy() == QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)))
return r; //fixed size is sometimes used with pixmap
int hAlign = QApplication::horizontalAlignment( alignment() );
int indentX = (hAlign && indent()>0) ? indent() : 0;
QFontMetrics fm(font());
r.setWidth( QMIN(fm.width(text()), r.width()));
if ( hAlign & AlignLeft )
r.moveLeft(r.left() + indentX);
if ( hAlign & AlignCenter )
r.moveLeft((contentsRect().width()-r.width())/2+margin());
if ( hAlign & AlignRight )
r.moveLeft(contentsRect().width()-r.width()-indentX+margin());
int add = QMIN(3, margin());
r = QRect(r.left()-add, r.top()-add, r.width()+2*add, r.height()+2*add);
return r;
}
void KURLLabel::setMargin( int margin )
{
QLabel::setMargin(margin);
d->MarginAltered = true;
}
void KURLLabel::setFocusPolicy( FocusPolicy policy )
{
QLabel::setFocusPolicy(policy);
if (!d->MarginAltered) {
QLabel::setMargin(policy == NoFocus ? 0 : 3); //better default : better look when focused
}
}
void KURLLabel::setSizePolicy ( QSizePolicy policy )
{
QLabel::setSizePolicy(policy);
if (!d->MarginAltered && policy.horData()==QSizePolicy::Fixed && policy.verData()==QSizePolicy::Fixed) {
QLabel::setMargin(0); //better default : better look when fixed size
}
}
void KURLLabel::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }
#include "kurllabel.moc"
KURLLabel::setAltPixmap
void setAltPixmap(const QPixmap &altPix)
Sets the "alt" pixmap.
Definition: kurllabel.cpp:296
KURLLabel::middleClickedURL
void middleClickedURL()
Emitted when the user clicked the left mouse button on this label.
KURLLabel::event
virtual bool event(QEvent *e)
Catch parent palette changes.
Definition: kurllabel.cpp:352
KURLLabel::setUseTips
void setUseTips(bool on=true)
Turns on or off the tool tip feature.
Definition: kurllabel.cpp:223
KURLLabel
A drop-in replacement for QLabel that displays hyperlinks.
Definition: kurllabel.h:71
KURLLabel::setTipText
void setTipText(const QString &tip)
Specifies what text to display when tooltips are turned on.
Definition: kurllabel.cpp:233
KURLLabel::~KURLLabel
virtual ~KURLLabel()
Destructs the label.
Definition: kurllabel.cpp:105
KURLLabel::useCursor
bool useCursor() const
KURLLabel::useTips
bool useTips() const
KURLLabel::setMargin
virtual void setMargin(int margin)
Reimplemented for internal reasons, the API is not affected.
Definition: kurllabel.cpp:425
KURLLabel::KURLLabel
KURLLabel(QWidget *parent=0L, const char *name=0L)
Default constructor.
Definition: kurllabel.cpp:94
KURLLabel::rightClickedURL
void rightClickedURL()
Emitted when the user clicked the right mouse button on this label.
KURLLabel::setFocusPolicy
virtual void setFocusPolicy(FocusPolicy policy)
Reimplemented for internal reasons, the API is not affected.
Definition: kurllabel.cpp:431
KURLLabel::tipText
const QString & tipText() const
Returns the current tooltip text.
KURLLabel::unsetCursor
virtual void unsetCursor()
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:193
KURLLabel::isFloatEnabled
bool isFloatEnabled() const
This feature is very similar to the "glow" feature in that the color of the label switches to the sel...
Definition: kurllabel.cpp:291
KURLLabel::setHighlightedColor
void setHighlightedColor(const QColor &highcolor)
Sets the highlight color.
Definition: kurllabel.cpp:250
KURLLabel::leftClickedURL
void leftClickedURL()
Emitted when the user clicked the left mouse button on this label.
KURLLabel::setSelectedColor
void setSelectedColor(const QColor &selcolor)
Sets the selected color.
Definition: kurllabel.cpp:263
KURLLabel::enterEvent
virtual void enterEvent(QEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:306
KURLLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *e)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:110
KURLLabel::leftURL
void leftURL()
Emitted when the mouse is no longer over the label.
KURLLabel::setFloat
void setFloat(bool do_float=true)
Turns on or off the "float" feature.
Definition: kurllabel.cpp:281
KURLLabel::url
const QString & url() const
Returns the URL.
KURLLabel::setGlow
void setGlow(bool glow=true)
Turns on or off the "glow" feature.
Definition: kurllabel.cpp:276
KGlobalSettings
KURLLabel::enteredURL
void enteredURL()
Emitted when the mouse has passed over the label.
KURLLabel::setUseCursor
void setUseCursor(bool on, QCursor *cursor=0L)
Turns the custom cursor feature on or off.
Definition: kurllabel.cpp:205
KCursor::handCursor
static QCursor handCursor()
Returns the proper hand cursor according to the current GUI style (static function).
Definition: kcursor.cpp:43
KURLLabel::isGlowEnabled
bool isGlowEnabled() const
When this is on, the text will switch to the selected color whenever the mouse passes over it...
Definition: kurllabel.cpp:286
KURLLabel::setUnderline
void setUnderline(bool on=true)
Turns on or off the underlining.
Definition: kurllabel.cpp:153
KURLLabel::altPixmap
const QPixmap * altPixmap() const
KURLLabel::setSizePolicy
virtual void setSizePolicy(QSizePolicy)
Reimplemented for internal reasons, the API is not affected.
Definition: kurllabel.cpp:439
KURLLabel::leaveEvent
virtual void leaveEvent(QEvent *)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:336
KURLLabel::setURL
void setURL(const QString &url)
Sets the URL for this label to url.
Definition: kurllabel.cpp:178
KURLLabel::setCursor
virtual void setCursor(const QCursor &cursor)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:199
KGlobalSettings::linkColor
static QColor linkColor()
KURLLabel::setFont
virtual void setFont(const QFont &)
Overridden for internal reasons; the API remains unaffected.
Definition: kurllabel.cpp:145

KDE Home | KDE Accessibility Home | Description of Access Keys