System notice: In light of the Debian OpenSSL security issue we've regenerated the server keys. See this thread for instructions and the new key fingerprints.

Blob of src/sif/aerobutton.cpp (raw blob data)

1
2
3 #include "aerobutton.h"
4
5 #include <QPainter>
6 #include <QApplication>
7 #include <QStyleOptionButton>
8
9 namespace SIF {
10
11
12 struct AeroButton::Private {
13 bool hovered;
14
15 QColor color;
16 QColor highlight;
17 QColor shadow;
18
19 qreal opacity;
20
21 int roundness;
22
23 Private(AeroButton *q);
24 ~Private();
25
26 QRect calculateIconPosition(const QRect &button_rect, const QSize &icon_size);
27 };
28
29 AeroButton::Private::Private(AeroButton *q) : hovered(false),
30 color(Qt::gray),
31 highlight(Qt::lightGray),
32 shadow(Qt::black),
33 opacity(1.0),
34 roundness(0)
35 {
36 QPalette pal = q->palette();
37 pal.setBrush(QPalette::WindowText, Qt::white);
38 pal.setBrush(QPalette::Text, Qt::white);
39 pal.setBrush(QPalette::ButtonText, Qt::white);
40 q->setPalette(pal);
41 }
42
43 QRect AeroButton::Private::calculateIconPosition(const QRect &button_rect, const QSize &icon_size)
44 {
45 int x = (button_rect.width() / 2) - (icon_size.width() / 2);
46 int y = (button_rect.height() / 2) - (icon_size.height() / 2);
47 int width = icon_size.width();
48 int height = icon_size.height();
49
50 QRect icon_position(x, y, width, height);
51
52 return icon_position;
53 }
54
55 AeroButton::Private::~Private()
56 {
57 }
58
59
60 AeroButton::AeroButton(QWidget * parent)
61 : QPushButton(parent), d(new Private(this))
62 {
63 }
64
65 AeroButton::AeroButton(const QString & text, QWidget * parent)
66 : QPushButton(text, parent), d(new Private(this))
67 {
68 }
69
70 AeroButton::AeroButton(const QIcon & icon, const QString & text, QWidget * parent)
71 : QPushButton(icon, text, parent), d(new Private(this))
72 {
73 }
74
75 AeroButton::~AeroButton()
76 {
77 delete d;
78 }
79
80 void AeroButton::setColor(const QColor &color)
81 {
82 d->color = color;
83 update();
84 }
85
86 void AeroButton::setHighlight(const QColor &highlight)
87 {
88 d->highlight = highlight;
89 update();
90 }
91
92 void AeroButton::setShadow(const QColor &shadow)
93 {
94 d->shadow = shadow;
95 update();
96 }
97
98 //Range: 0.0 [invisible] - 1.0 [opaque]
99 void AeroButton::setOpacity(qreal opacity)
100 {
101 d->opacity = opacity;
102 update();
103 }
104
105 //Range: 0 [rectangle] - 99 [oval]
106 void AeroButton::setRoundness(int roundness)
107 {
108 d->roundness = roundness;
109 update();
110 }
111
112 void AeroButton::paintEvent(QPaintEvent * pe)
113 {
114 Q_UNUSED(pe);
115
116 QRect rect = this->rect();
117 QStyleOptionButton option;
118 initStyleOption(&option);
119
120 QPainter painter(this);
121 painter.setRenderHint(QPainter::Antialiasing);
122
123 painter.save();
124
125 painter.fillRect(rect, Qt::transparent);
126
127 //test for state changes
128 QColor button_color;
129 if(this->isEnabled())
130 {
131 d->hovered ? button_color = d->highlight : button_color = d->color;
132
133 if(option.state & QStyle::State_Sunken )
134 {
135 button_color = d->highlight.darker(250);
136 }
137 }
138 else
139 {
140 button_color = QColor(50, 50, 50);
141 }
142
143 QRect button_rect = this->geometry();
144
145 //outline
146 painter.setPen(QPen(QBrush(Qt::black), 2.0));
147 QPainterPath outline;
148 outline.addRoundRect(0, 0, button_rect.width(), button_rect.height(), d->roundness, d->roundness);
149 painter.setOpacity(d->opacity);
150 painter.drawPath(outline);
151
152 //gradient
153 QLinearGradient gradient(0, 0, 0, button_rect.height());
154 gradient.setSpread(QGradient::ReflectSpread);
155 gradient.setColorAt(0.0, button_color);
156 gradient.setColorAt(0.4, d->shadow);
157 gradient.setColorAt(0.6, d->shadow);
158 gradient.setColorAt(1.0, button_color);
159
160 QBrush brush(gradient);
161 painter.setBrush(brush);
162 painter.setPen(QPen(QBrush(button_color), 2.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ));
163
164 //main button
165 QPainterPath painter_path;
166 painter_path.addRoundRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, d->roundness, d->roundness);
167 painter.setClipPath(painter_path);
168
169 painter.setOpacity(d->opacity);
170 painter.drawRoundRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, d->roundness, d->roundness);
171
172 //glass highlight
173 painter.setBrush(QBrush(Qt::white));
174 painter.setPen(QPen(QBrush(Qt::white), 0.01, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
175 painter.setOpacity(0.30);
176 painter.drawRect(1, 1, button_rect.width() - 2, (button_rect.height() / 2) - 2);
177
178 painter.restore();
179
180 QIcon icon = this->icon();
181 QString text = this->text();
182 QRect icon_position;
183
184 //icon
185 if(!icon.isNull())
186 {
187 QSize icon_size = this->iconSize();
188 icon_position = d->calculateIconPosition(button_rect, icon_size);
189
190
191 if( !text.isEmpty() )
192 {
193 icon_position.setX(20);
194 icon_position.setWidth(icon_size.width());
195 }
196
197 painter.setOpacity(1.0);
198 painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size)));
199 }
200
201 //button text
202
203 if(!text.isNull())
204 {
205 QFont font = this->font();
206 font.setBold(true);
207 painter.setFont(font);
208
209 painter.setOpacity(1.0);
210 painter.drawText(icon_position.width(), 0, button_rect.width() - icon_position.width(), button_rect.height(), Qt::AlignCenter, text);
211 }
212
213 if(option.state & QStyle::State_On)
214 {
215 QPen old = painter.pen();
216
217 QColor h = d->highlight;
218 h.setAlpha(150);
219 QPen pen(h, 2);
220 painter.setPen(pen);
221
222 painter.drawLine(rect.bottomLeft()-QPoint(-6, 3), rect.bottomRight()-QPoint(6, 3));
223
224 painter.setPen(old);
225 }
226 }
227
228 void AeroButton::enterEvent(QEvent * e)
229 {
230 Q_UNUSED(e);
231
232 d->hovered = true;
233 this->update();
234 }
235
236 void AeroButton::leaveEvent(QEvent * e)
237 {
238 Q_UNUSED(e);
239
240 d->hovered = false;
241 this->update();
242 }
243
244 void AeroButton::mousePressEvent(QMouseEvent * e)
245 {
246 Q_UNUSED(e);
247
248 this->update();
249
250 QPushButton::mousePressEvent(e);
251 }
252
253 void AeroButton::mouseReleaseEvent(QMouseEvent * e)
254 {
255 Q_UNUSED(e);
256
257 this->update();
258
259 QPushButton::mouseReleaseEvent(e);
260 }
261
262
263 }