Vidalia  0.2.21
VClickLabel.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file VClickLabel.cpp
13 ** \brief Custom widget to create a clickable label with both an image and text.
14 */
15 
16 #include "VClickLabel.h"
17 #include "Vidalia.h"
18 
19 #include <QPainter>
20 
21 
22 /** Default constructor. */
23 VClickLabel::VClickLabel(QWidget *parent)
24  : QWidget(parent)
25 {
26  setCursor(Qt::PointingHandCursor);
27  _flashToggle = false;
28  _isPressed = false;
29 }
30 
31 /** Returns the current size hint for this widget's current contents. */
32 QSize
34 {
35  int height = qMax(_pixmap.height(), fontMetrics().height())+2;
36  int width = _pixmap.width() + fontMetrics().width(_text)+2;
37  return QSize(width, height);
38 }
39 
40 /** Returns the minimum size hint for this widget's current contents. */
41 QSize
43 {
44  return sizeHint();
45 }
46 
47 /** Overloaded paint event to draw a pixmap and a text label. */
48 void
49 VClickLabel::paintEvent(QPaintEvent *e)
50 {
51  QPainter p(this);
52  QRect rect = this->rect();
53 
54  if (_flashToggle || _isPressed) {
55  p.setBrush(QColor(150,150,150));
56  rect.setX(rect.x()+1);
57  rect.setY(rect.y()+1);
58  rect.setWidth(rect.width());
59  rect.setHeight(rect.height());
60  p.drawRect(rect);
61  }
62 
63  rect = this->rect();
64 
65  // when label is in "pressed" state, we will translate the pixmap and text
66  // a bit, just like regular buttons do
67  const int d = _isPressed ? 2 : 0;
68 
69  if (vApp->isLeftToRight()) {
70  if (!_pixmap.isNull())
71  p.drawPixmap(d, qMax((rect.height()-_pixmap.height())/2+d, 0), _pixmap);
72  if (!_text.isEmpty())
73  p.drawText(_pixmap.width()+2+d, (rect.height()+fontInfo().pixelSize())/2+d, _text);
74  } else {
75  if (!_pixmap.isNull())
76  p.drawPixmap(qMax(rect.right()-_pixmap.width()-d, d),
77  qMax((rect.height()-_pixmap.height())/2-d, d), _pixmap);
78  if (!_text.isEmpty()) {
79  int textWidth = fontMetrics().width(_text);
80  p.drawText(qMax(rect.right()-_pixmap.width()-textWidth-2-d, d),
81  (rect.height()+fontInfo().pixelSize())/2-d, _text);
82  }
83  }
84  e->accept();
85 }
86 
87 /** Overloaded mouse event to remember click state. */
88 void
90 {
91  if (e->button() == Qt::LeftButton) {
92  _isPressed = true;
93  update();
94  }
95  e->accept();
96 }
97 
98 /** Overloaded mouse event to catch left mouse button clicks. */
99 void
101 {
102  if (e->button() == Qt::LeftButton) {
103  _isPressed = false;
104  update();
105  emit clicked();
106  }
107  e->accept();
108 }
109 
110 /** Sets the label text to <b>text</b>. */
111 void
112 VClickLabel::setText(const QString &text)
113 {
114  _text = text;
115  update();
116 }
117 
118 /** Sets the widget's image to <b>img</b>. */
119 void
120 VClickLabel::setPixmap(const QPixmap &pixmap)
121 {
122  _pixmap = pixmap;
123  update();
124 }
125 
126 void
128 {
129  _flashToggle = true;
130  repaint();
131 }
132 
133 void
135 {
136  _flashToggle = false;
137  repaint();
138 }