summaryrefslogtreecommitdiffstats
path: root/kdeui/kurllabel.h
blob: b39d65d922cec2d4297cea222bb9f02b8f33eeab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* 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.
*/

#ifndef KURLLABEL_H
#define KURLLABEL_H

#include <tqlabel.h>

#include <kdelibs_export.h>

class TQColor;
class TQCursor;
class TQPixmap;

/**
 * @short A drop-in replacement for TQLabel that displays hyperlinks.
 *
 * KURLLabel is a drop-in replacement for TQLabel that handles text
 * in a fashion similar to how an HTML widget handles hyperlinks.  The
 * text can be underlined (or not) and set to different colors.  It
 * can also "glow" (cycle colors) when the mouse passes over it.
 *
 * KURLLabel also provides signals for several events, including
 * the mouse leaving and entering the text area and all forms of
 * mouse clicking.
 *
 * By default KURLLabel accepts focus. When focused, standard 
 * focus rectangle is displayed as in HTML widget. 
 * Pressing Enter key accepts the focused label.
 *
 * A typical usage would be something like so:
 *
 * \code
 * KURLLabel *address = new KURLLabel(this);
 * address->setText("My homepage");
 * address->setURL("http://www.home.com/~me");
 * connect(address, TQT_SIGNAL(leftClickedURL(const TQString&)),
 *                  TQT_SLOT(processMyURL(const TQString&)));
 * \endcode
 *
 * In this example, the text "My homepage" would be displayed
 * as blue, underlined text.  When the mouse passed over it,
 * it would "glow" red.  When the user clicks on the text, the
 * signal leftClickedURL() would be emitted with "http://www.home.com/~me"
 * as its argument.
 *
 * \image html kurllabel.png "KDE URL Label"
 *
 * @author Kurt Granroth <granroth@kde.org> (Interface)
 * @author Peter Putzer <putzer@kde.org> (Rewrite)
 * @version $Id$
 *
 */
class KDEUI_EXPORT KURLLabel : public QLabel
{
  Q_OBJECT
  Q_PROPERTY (TQString url READ url WRITE setURL)
  Q_PROPERTY (TQString tipText READ tipText WRITE setTipText )
  Q_PROPERTY (TQPixmap altPixmap READ altPixmap WRITE setAltPixmap)
  Q_PROPERTY (bool glowEnabled READ isGlowEnabled WRITE setGlow )
  Q_PROPERTY (bool floatEnabled READ isFloatEnabled WRITE setFloat )
  Q_PROPERTY (bool useTips READ useTips WRITE setUseTips )
  Q_PROPERTY (bool useCursor READ useCursor WRITE setUseCursor )

public:
  /**
   * Default constructor.
   *
   * Use setURL() and setText() or TQListView::setPixmap()
   * to set the resp. properties.
   */
  KURLLabel (TQWidget* parent = 0L, const char* name = 0L);

  /**
   * Convenience constructor.
   *
   * @param url is the URL emitted when the label is clicked.
   * @param text is the displayed string. If it's equal to TQString::null
   * the @p url will be used instead.
   * @param parent Passed to lower level constructor
   * @param name Passed to lower level constructor
   *
   * @p parent and @p name are passed to TQLabel, which in turn passes
   * them further down
   */
  KURLLabel (const TQString& url, const TQString& text = TQString::null,
              TQWidget* parent = 0L, const char* name = 0L);

  /**
   * Destructs the label.
   */
  virtual ~KURLLabel ();

  /**
   * Returns the URL.
   */
  const TQString& url () const;

  /**
   * Returns the current tooltip text.
   */
  const TQString& tipText () const;

  /**
   * @return true if a tooltip will be displayed.
   *
   * @see setTipText()
   */
  bool useTips () const;

  /**
   * @return true if the cursor will change while over the URL.
   *
   * @see setUseCursor ()
   */
  bool useCursor () const;

  /**
   * When this is on, the text will switch to the selected
   * color whenever the mouse passes over it.
   */
  bool isGlowEnabled () const;

  /**
   * This feature is very similar to the "glow" feature in that the color of the
   * label switches to the selected color when the cursor passes
   * over it. In addition, underlining is turned on for as
   * long as the mouse is overhead. Note that if "glow" and
   * underlining are both already turned on, this feature
   * will have no visible effect.
   */
  bool isFloatEnabled () const;

  /**
   * @return the alternate pixmap (may be 0L if none was set).
   */
  const TQPixmap* altPixmap () const;

  /**
   * Reimplemented for internal reasons, the API is not affected.
   */
  virtual void setMargin ( int margin );

  /**
   * Reimplemented for internal reasons, the API is not affected.
   */
  virtual void setFocusPolicy ( FocusPolicy policy );

  /**
   * Reimplemented for internal reasons, the API is not affected.
   */
  virtual void setSizePolicy ( TQSizePolicy );

public slots:
  /**
   * Turns on or off the underlining.
   *
   *  When this is on, the
   * text will be underlined.  By default, it is @p true.
   */
  void setUnderline (bool on = true);

  /**
   * Sets the URL for this label to @p url.
   *
   * @see url
   */
  void setURL (const TQString& url);

  /**
   * Overridden for internal reasons; the API remains unaffected.
   */
  virtual void setFont (const TQFont&);

  /**
   * Turns on or off the tool tip feature.
   *
   * When this is on, the URL will be displayed as a
   * tooltip whenever the mouse passes passes over it.
   * By default, it is @p false.
   */
  void setUseTips (bool on = true);

  /**
   * Specifies what text to display when tooltips are turned on.
   *
   * If this is not used, the tip will default to the URL.
   *
   * @see setUseTips()
   */
  void setTipText (const TQString& tip);

  /**
   * Sets the highlight color.
   *
   * This is the default foreground
   * color (non-selected).  By default, it is @p blue.
   */
  void setHighlightedColor(const TQColor& highcolor);

  /**
   * This is an overloaded version for convenience.
   *
   * @see setHighlightedColor()
   */
  void setHighlightedColor(const TQString& highcolor);

  /**
   * Sets the selected color.
   *
   * This is the color the text will change
   * to when either a mouse passes over it and "glow" mode is on or
   * when it is selected (clicked).  By default, it is @p red.
   */
  void setSelectedColor(const TQColor& selcolor);

  /**
   * This is an overloaded version for convenience.
   *
   * @see setSelectedColor()
   */
  void setSelectedColor(const TQString& selcolor);

  /**
   * Overridden for internal reasons; the API remains unaffected.
   */
  virtual void setCursor ( const TQCursor& cursor );

  /**
   * Overridden for internal reasons; the API remains unaffected.
   */
  virtual void unsetCursor ();

  /**
   * Turns the custom cursor feature on or off.
   *
   * When this is on, the cursor will change to a custom cursor
   * (default is a "pointing hand") whenever the cursor passes
   * over the label. By default, it is on.
   *
   * @param on whether a custom cursor should be displayed.
   * @param cursor is the custom cursor. @p 0L indicates the default "hand cursor".
   */
  void setUseCursor (bool on, TQCursor* cursor = 0L);

  /**
   * Turns on or off the "glow" feature.
   *
   * When this is on, the text will switch to the
   * selected color whenever the mouse
   * passes over it. By default, it is @p true.
   */
  void setGlow (bool glow = true);

  /**
   * Turns on or off the "float" feature.
   *
   * This feature is very similar to the "glow" feature in
   * that the color of the label switches to the selected
   * color when the cursor passes over it. In addition,
   * underlining is turned on for as long as the mouse is overhead.
   * Note that if "glow" and underlining are both already turned
   * on, this feature will have no visible effect.
   * By default, it is @p false.
   */
  void setFloat (bool do_float = true);

  /**
   * Sets the "alt" pixmap.
   *
   * This pixmap will be displayed when the
   * cursor passes over the label.  The effect is similar to the
   * trick done with 'onMouseOver' in javascript.
   *
   * @see altPixmap()
   */
  void setAltPixmap (const TQPixmap& altPix);

signals:

  /**
   * Emitted when the mouse has passed over the label.
   *
   * @param url The URL for this label.
   */
  void enteredURL (const TQString& url);

  /**
   * Emitted when the mouse has passed over the label.
   */
  void enteredURL ();

  /**
   * Emitted when the mouse is no longer over the label.
   *
   * @param url The URL for this label.
   */
  void leftURL (const TQString& url);

  /**
   * Emitted when the mouse is no longer over the label.
   */
  void leftURL ();

  /**
   * Emitted when the user clicked the left mouse button on this label.
   *
   * @param url The URL for this label.
   */
  void leftClickedURL(const TQString& url);

  /**
   * Emitted when the user clicked the left mouse button on this label.
   */
  void leftClickedURL();

  /**
   * Emitted when the user clicked the right mouse button on this label.
   *
   * @param url The URL for this label.
   */
  void rightClickedURL(const TQString& url);

  /**
   * Emitted when the user clicked the right mouse button on this label.
   */
  void rightClickedURL();

  /**
   * Emitted when the user clicked the middle mouse button on this label.
   *
   * @param url The URL for this label.
   */
  void middleClickedURL(const TQString& url);

  /**
   * Emitted when the user clicked the left mouse button on this label.
   */
  void middleClickedURL();

protected:

  /**
   * Overridden for internal reasons; the API remains unaffected.
   */
  virtual void mouseReleaseEvent ( TQMouseEvent * e );

  /**
   * Overridden for internal reasons; the API remains unaffected.
   */
  virtual void enterEvent (TQEvent*);

  /**
   * Overridden for internal reasons; the API remains unaffected.
   */
  virtual void leaveEvent (TQEvent*);

  /**
   * Catch parent palette changes
   */
  virtual bool event (TQEvent *e);

  /**
   * 
   */
  TQRect activeRect() const;


private slots:
  /**
   * @internal
   * Slot used to reset the link-color to normal (timer-driven).
   */
  void updateColor ();

private:
  /**
   * @internal
   * A private helper function to set the link-color to @p col.
   */
  void setLinkColor (const TQColor& col);

protected:
  virtual void virtual_hook( int id, void* data );
private:
  class Private;
  Private* d;
};

#endif // KURLLABEL_H