1 /* This file is part of the KDE libraries
2 Copyright (C) 1998 Kurt Granroth <granroth@kde.org>
3 Copyright (C) 2000 Peter Putzer <putzer@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
17 Boston, MA 02110-1301, USA.
25 #include <kdelibs_export.h>
32 * @short A drop-in replacement for QLabel that displays hyperlinks.
34 * KURLLabel is a drop-in replacement for QLabel that handles text
35 * in a fashion similar to how an HTML widget handles hyperlinks. The
36 * text can be underlined (or not) and set to different colors. It
37 * can also "glow" (cycle colors) when the mouse passes over it.
39 * KURLLabel also provides signals for several events, including
40 * the mouse leaving and entering the text area and all forms of
43 * A typical usage would be something like so:
46 * KURLLabel *address = new KURLLabel(this);
47 * address->setText("My homepage");
48 * address->setURL("http://www.home.com/~me");
49 * connect(address, SIGNAL(leftClickedURL(const QString&)),
50 * SLOT(processMyURL(const QString&)));
53 * In this example, the text "My homepage" would be displayed
54 * as blue, underlined text. When the mouse passed over it,
55 * it would "glow" red. When the user clicks on the text, the
56 * signal leftClickedURL() would be emitted with "http://www.home.com/~me"
59 * \image html kurllabel.png "KDE URL Label"
61 * @author Kurt Granroth <granroth@kde.org> (Interface)
62 * @author Peter Putzer <putzer@kde.org> (Rewrite)
66 class KDEUI_EXPORT KURLLabel
: public QLabel
69 Q_PROPERTY (QString url READ url WRITE setURL
)
70 Q_PROPERTY (QString tipText READ tipText WRITE setTipText
)
71 Q_PROPERTY (QPixmap altPixmap READ altPixmap WRITE setAltPixmap
)
72 Q_PROPERTY (bool glowEnabled READ isGlowEnabled WRITE setGlow
)
73 Q_PROPERTY (bool floatEnabled READ isFloatEnabled WRITE setFloat
)
74 Q_PROPERTY (bool useTips READ useTips WRITE setUseTips
)
75 Q_PROPERTY (bool useCursor READ useCursor WRITE setUseCursor
)
79 * Default constructor.
81 * Use setURL() and setText() or QListView::setPixmap()
82 * to set the resp. properties.
84 KURLLabel (QWidget
* parent
= 0L, const char* name
= 0L);
87 * Convenience constructor.
89 * @param url is the URL emitted when the label is clicked.
90 * @param text is the displayed string. If it's equal to QString::null
91 * the @p url will be used instead.
92 * @param parent Passed to lower level constructor
93 * @param name Passed to lower level constructor
95 * @p parent and @p name are passed to QLabel, which in turn passes
98 KURLLabel (const QString
& url
, const QString
& text
= QString::null
,
99 QWidget
* parent
= 0L, const char* name
= 0L);
102 * Destructs the label.
104 virtual ~KURLLabel ();
109 const QString
& url () const;
112 * Returns the current tooltip text.
114 const QString
& tipText () const;
117 * @return true if a tooltip will be displayed.
121 bool useTips () const;
124 * @return true if the cursor will change while over the URL.
126 * @see setUseCursor ()
128 bool useCursor () const;
131 * When this is on, the text will switch to the selected
132 * color whenever the mouse passes over it.
134 bool isGlowEnabled () const;
137 * This feature is very similar to the "glow" feature in that the color of the
138 * label switches to the selected color when the cursor passes
139 * over it. In addition, underlining is turned on for as
140 * long as the mouse is overhead. Note that if "glow" and
141 * underlining are both already turned on, this feature
142 * will have no visible effect.
144 bool isFloatEnabled () const;
147 * @return the alternate pixmap (may be 0L if none was set).
149 const QPixmap
* altPixmap () const;
153 * Turns on or off the underlining.
155 * When this is on, the
156 * text will be underlined. By default, it is @p true.
158 void setUnderline (bool on
= true);
161 * Sets the URL for this label to @p url.
165 void setURL (const QString
& url
);
168 * Overridden for internal reasons; the API remains unaffected.
170 virtual void setFont (const QFont
&);
173 * Turns on or off the tool tip feature.
175 * When this is on, the URL will be displayed as a
176 * tooltip whenever the mouse passes passes over it.
177 * By default, it is @p false.
179 void setUseTips (bool on
= true);
182 * Specifies what text to display when tooltips are turned on.
184 * If this is not used, the tip will default to the URL.
188 void setTipText (const QString
& tip
);
191 * Sets the highlight color.
193 * This is the default foreground
194 * color (non-selected). By default, it is @p blue.
196 void setHighlightedColor(const QColor
& highcolor
);
199 * This is an overloaded version for convenience.
201 * @see setHighlightedColor()
203 void setHighlightedColor(const QString
& highcolor
);
206 * Sets the selected color.
208 * This is the color the text will change
209 * to when either a mouse passes over it and "glow" mode is on or
210 * when it is selected (clicked). By default, it is @p red.
212 void setSelectedColor(const QColor
& selcolor
);
215 * This is an overloaded version for convenience.
217 * @see setSelectedColor()
219 void setSelectedColor(const QString
& selcolor
);
222 * Turns the custom cursor feature on or off.
224 * When this is on, the cursor will change to a custom cursor
225 * (default is a "pointing hand") whenever the cursor passes
226 * over the label. By default, it is on.
228 * @param on whether a custom cursor should be displayed.
229 * @param cursor is the custom cursor. @p 0L indicates the default "hand cursor".
231 void setUseCursor (bool on
, QCursor
* cursor
= 0L);
234 * Turns on or off the "glow" feature.
236 * When this is on, the text will switch to the
237 * selected color whenever the mouse
238 * passes over it. By default, it is @p true.
240 void setGlow (bool glow
= true);
243 * Turns on or off the "float" feature.
245 * This feature is very similar to the "glow" feature in
246 * that the color of the label switches to the selected
247 * color when the cursor passes over it. In addition,
248 * underlining is turned on for as long as the mouse is overhead.
249 * Note that if "glow" and underlining are both already turned
250 * on, this feature will have no visible effect.
251 * By default, it is @p false.
253 void setFloat (bool do_float
= true);
256 * Sets the "alt" pixmap.
258 * This pixmap will be displayed when the
259 * cursor passes over the label. The effect is similar to the
260 * trick done with 'onMouseOver' in javascript.
264 void setAltPixmap (const QPixmap
& altPix
);
269 * Emitted when the mouse has passed over the label.
271 * @param url The URL for this label.
273 void enteredURL (const QString
& url
);
276 * Emitted when the mouse has passed over the label.
281 * Emitted when the mouse is no longer over the label.
283 * @param url The URL for this label.
285 void leftURL (const QString
& url
);
288 * Emitted when the mouse is no longer over the label.
293 * Emitted when the user clicked the left mouse button on this label.
295 * @param url The URL for this label.
297 void leftClickedURL(const QString
& url
);
300 * Emitted when the user clicked the left mouse button on this label.
302 void leftClickedURL();
305 * Emitted when the user clicked the right mouse button on this label.
307 * @param url The URL for this label.
309 void rightClickedURL(const QString
& url
);
312 * Emitted when the user clicked the right mouse button on this label.
314 void rightClickedURL();
317 * Emitted when the user clicked the middle mouse button on this label.
319 * @param url The URL for this label.
321 void middleClickedURL(const QString
& url
);
324 * Emitted when the user clicked the left mouse button on this label.
326 void middleClickedURL();
331 * Overridden for internal reasons; the API remains unaffected.
333 virtual void mouseReleaseEvent (QMouseEvent
*);
336 * Overridden for internal reasons; the API remains unaffected.
338 virtual void enterEvent (QEvent
*);
341 * Overridden for internal reasons; the API remains unaffected.
343 virtual void leaveEvent (QEvent
*);
346 * Catch parent palette changes
348 virtual bool event (QEvent
*e
);
354 * Slot used to reset the link-color to normal (timer-driven).
361 * A private helper function to set the link-color to @p col.
363 void setLinkColor (const QColor
& col
);
366 virtual void virtual_hook( int id
, void* data
);
372 #endif // KURLLABEL_H