summaryrefslogtreecommitdiffstats
path: root/kdeui/kurllabel.cpp
blob: 71f90abd90ebdda23dac254f105d7109c2802a13 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* 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 <tqcolor.h>
#include <tqtimer.h>
#include <tqtooltip.h>
#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqstyle.h>
#include <tqapplication.h>

#include <kcursor.h>
#include <kglobalsettings.h>

#include "kurllabel.h"

class KURLLabel::Private
{
public:
  Private (const TQString& 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 TQTimer (label))
  {
    connect (Timer, TQT_SIGNAL (timeout ()), label, TQT_SLOT (updateColor ()));
  }

  ~Private ()
  {
      delete Cursor;
  }

  TQString URL;
  TQPixmap AltPixmap;

  TQColor LinkColor;
  TQColor HighlightedLinkColor;

  TQString Tip;
  TQCursor* 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;
  TQPixmap RealPixmap;

  TQTimer* Timer;
};

KURLLabel::KURLLabel (const TQString& url, const TQString& text,
                        TQWidget* parent, const char* name)
  : TQLabel (!text.isNull() ? text : url, parent, name),
    d (new Private (url, this))
{
  setFont (font());
  setUseCursor (true);
  setLinkColor (d->LinkColor);
  setFocusPolicy( TQ_StrongFocus ); //better accessibility
  setMouseTracking (true);
}

KURLLabel::KURLLabel (TQWidget* parent, const char* name)
  : TQLabel (parent, name),
    d (new Private (TQString::null, this))
{
  setFont (font());
  setUseCursor (true);
  setLinkColor (d->LinkColor);
  setFocusPolicy( TQ_StrongFocus ); //better accessibility
  setMouseTracking (true);
}

KURLLabel::~KURLLabel ()
{
  delete d;
}

void KURLLabel::mouseReleaseEvent (TQMouseEvent* e)
{
  TQLabel::mouseReleaseEvent (e);
  if (!d->MousePressed)
    return;
  d->MousePressed = false;
  TQRect r( activeRect() );
  if (!r.contains(e->pos()))
    return;

  setLinkColor (d->HighlightedLinkColor);
  d->Timer->start (300);

  switch (e->button())
    {
    case Qt::LeftButton:
      emit leftClickedURL ();
      emit leftClickedURL (d->URL);
      break;

    case Qt::MidButton:
      emit middleClickedURL ();
      emit middleClickedURL (d->URL);
      break;

    case Qt::RightButton:
      emit rightClickedURL ();
      emit rightClickedURL (d->URL);
      break;

    default:
      ; // nothing
    }
}

void KURLLabel::setFont (const TQFont& f)
{
  TQFont newFont = f;
  newFont.setUnderline (d->Underline);

  TQLabel::setFont (newFont);
}

void KURLLabel::setUnderline (bool on)
{
  d->Underline = on;

  setFont (font());
}

void KURLLabel::updateColor ()
{
  d->Timer->stop();

  TQRect r( activeRect() );
  if (!(d->Glow || d->Float) || !r.contains (mapFromGlobal(TQCursor::pos())))
    setLinkColor (d->LinkColor);
}

void KURLLabel::setLinkColor (const TQColor& col)
{
  TQPalette p = palette();
  p.setColor (TQColorGroup::Foreground, col);
  setPalette (p);

  update();
}

void KURLLabel::setURL (const TQString& url)
{
  if ( d->Tip == d->URL ) { // update the tip as well
    d->Tip = url;
    setUseTips( d->UseTips );
  }

  d->URL = url;
}

const TQString& KURLLabel::url () const
{
  return d->URL;
}

void KURLLabel::unsetCursor ()
{
	delete d->Cursor;
	d->Cursor = 0;
}

void KURLLabel::setCursor ( const TQCursor& cursor )
{
	delete d->Cursor;
	d->Cursor = new TQCursor( cursor );
}

void KURLLabel::setUseCursor (bool on, TQCursor* 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) {
    TQToolTip::add (this, activeRect(), d->Tip);
  } else
    TQToolTip::remove (this);
}

void KURLLabel::setTipText (const TQString& tip)
{
  d->Tip = tip;

  setUseTips (d->UseTips);
}

bool KURLLabel::useTips () const
{
  return d->UseTips;
}

const TQString& KURLLabel::tipText () const
{
  return d->Tip;
}

void KURLLabel::setHighlightedColor (const TQColor& highcolor)
{
  d->LinkColor = highcolor;

  if (!d->Timer->isActive())
    setLinkColor (highcolor);
}

void KURLLabel::setHighlightedColor (const TQString& highcolor)
{
  setHighlightedColor (TQColor (highcolor));
}

void KURLLabel::setSelectedColor (const TQColor& selcolor)
{
  d->HighlightedLinkColor = selcolor;

  if (d->Timer->isActive())
    setLinkColor (selcolor);
}

void KURLLabel::setSelectedColor (const TQString& selcolor)
{
  setSelectedColor (TQColor (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 TQPixmap& altPix)
{
  d->AltPixmap = altPix;
}

const TQPixmap* KURLLabel::altPixmap () const
{
  return &d->AltPixmap;
}

void KURLLabel::enterEvent (TQEvent* e)
{
  TQLabel::enterEvent (e);

  TQRect r( activeRect() );
  if (!r.contains( TQT_TQMOUSEEVENT(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 (TQEvent* e)
{
  TQLabel::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 (TQEvent *e)
{
  if (e && e->type() == TQEvent::ParentPaletteChange)
  {
    // use parentWidget() unless you are a toplevel widget, then try qAapp
    TQPalette p = parentWidget() ? parentWidget()->palette() : tqApp->palette();
    p.setBrush(TQColorGroup::Base, p.brush(TQPalette::Normal, TQColorGroup::Background));
    p.setColor(TQColorGroup::Foreground, palette().active().foreground());
    setPalette(p);
    d->LinkColor = KGlobalSettings::linkColor();
    setLinkColor(d->LinkColor);
    return true;
  }
  else if (e->type() == TQEvent::Paint) {
    const bool result = TQLabel::event(e);
    if (result && hasFocus()) {
        TQPainter p(this);
        TQRect r( activeRect() );
        style().tqdrawPrimitive( TQStyle::PE_FocusRect, &p, r, colorGroup() );
    }
    return result;
  }
  else if (e->type() == TQEvent::KeyPress) {
    TQKeyEvent* ke = TQT_TQKEYEVENT(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() == TQEvent::MouseButtonPress) {
    TQRect r( activeRect() );
    d->MousePressed = r.contains(TQT_TQMOUSEEVENT(e)->pos());
  }
  else if (e->type() == TQEvent::MouseMove) {
    if (d->Cursor) {
      TQRect r( activeRect() );
      bool inside = r.contains(TQT_TQMOUSEEVENT(e)->pos());
      if (d->WasInsideRect != inside) {
        if (inside)
          TQLabel::setCursor(*d->Cursor);
        else
          TQLabel::unsetCursor();
        d->WasInsideRect = inside;
      }
    }
  }
  return TQLabel::event(e);
}

TQRect KURLLabel::activeRect() const
{
  TQRect r( contentsRect() );
  if (text().isEmpty() || (!d->MarginAltered && sizePolicy() == TQSizePolicy(TQSizePolicy::Fixed, TQSizePolicy::Fixed)))
      return r; //fixed size is sometimes used with pixmap
  int hAlign = TQApplication::horizontalAlignment( alignment() );
  int indentX = (hAlign && indent()>0) ? indent() : 0;
  TQFontMetrics 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 = TQRect(r.left()-add, r.top()-add, r.width()+2*add, r.height()+2*add);
  return r;
}

void KURLLabel::setMargin( int margin )
{
  TQLabel::setMargin(margin);
  d->MarginAltered = true;
}

void KURLLabel::setFocusPolicy( TQ_FocusPolicy policy )
{
  TQLabel::setFocusPolicy(policy);
  if (!d->MarginAltered) {
      TQLabel::setMargin(policy == TQ_NoFocus ? 0 : 3); //better default : better look when focused
  }
}

void KURLLabel::setSizePolicy ( TQSizePolicy policy )
{
  TQLabel::setSizePolicy(policy);
  if (!d->MarginAltered && policy.horData()==TQSizePolicy::Fixed && policy.verData()==TQSizePolicy::Fixed) {
      TQLabel::setMargin(0); //better default : better look when fixed size
  }
}

void KURLLabel::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "kurllabel.moc"