|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qstylehelper_p.h" |
|
43 |
|
44 #include <qstyleoption.h> |
|
45 #include <qpainter.h> |
|
46 #include <qpixmapcache.h> |
|
47 #include <private/qmath_p.h> |
|
48 #include <private/qstyle_p.h> |
|
49 #include <qmath.h> |
|
50 |
|
51 #if defined(Q_WS_WIN) |
|
52 #include "qt_windows.h" |
|
53 #elif defined(Q_WS_MAC) |
|
54 #include <private/qt_cocoa_helpers_mac_p.h> |
|
55 #endif |
|
56 |
|
57 QT_BEGIN_NAMESPACE |
|
58 |
|
59 namespace QStyleHelper { |
|
60 |
|
61 QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size) |
|
62 { |
|
63 const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(option); |
|
64 QString tmp = QString::fromLatin1("%1-%2-%3-%4-%5-%6x%7").arg(key).arg(uint(option->state)).arg(option->direction) |
|
65 .arg(complexOption ? uint(complexOption->activeSubControls) : uint(0)) |
|
66 .arg(option->palette.cacheKey()).arg(size.width()).arg(size.height()); |
|
67 #ifndef QT_NO_SPINBOX |
|
68 if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) { |
|
69 tmp.append(QLatin1Char('-')); |
|
70 tmp.append(QString::number(spinBox->buttonSymbols)); |
|
71 tmp.append(QLatin1Char('-')); |
|
72 tmp.append(QString::number(spinBox->stepEnabled)); |
|
73 tmp.append(QLatin1Char('-')); |
|
74 tmp.append(QLatin1Char(spinBox->frame ? '1' : '0')); |
|
75 } |
|
76 #endif // QT_NO_SPINBOX |
|
77 return tmp; |
|
78 } |
|
79 |
|
80 qreal dpiScaled(qreal value) |
|
81 { |
|
82 static qreal scale = -1; |
|
83 if (scale < 0) { |
|
84 scale = 1.0; |
|
85 #if defined(Q_WS_WIN) |
|
86 { |
|
87 HDC hdcScreen = GetDC(0); |
|
88 int dpi = GetDeviceCaps(hdcScreen, LOGPIXELSX); |
|
89 ReleaseDC(0, hdcScreen); |
|
90 scale = dpi/96.0; |
|
91 } |
|
92 #elif defined(Q_WS_MAC) |
|
93 scale = qt_mac_get_scalefactor(); |
|
94 #endif |
|
95 } |
|
96 return value * scale; |
|
97 } |
|
98 |
|
99 |
|
100 #ifndef QT_NO_DIAL |
|
101 |
|
102 int calcBigLineSize(int radius) |
|
103 { |
|
104 int bigLineSize = radius / 6; |
|
105 if (bigLineSize < 4) |
|
106 bigLineSize = 4; |
|
107 if (bigLineSize > radius / 2) |
|
108 bigLineSize = radius / 2; |
|
109 return bigLineSize; |
|
110 } |
|
111 |
|
112 static QPointF calcRadialPos(const QStyleOptionSlider *dial, qreal offset) |
|
113 { |
|
114 const int width = dial->rect.width(); |
|
115 const int height = dial->rect.height(); |
|
116 const int r = qMin(width, height) / 2; |
|
117 const int currentSliderPosition = dial->upsideDown ? dial->sliderPosition : (dial->maximum - dial->sliderPosition); |
|
118 qreal a = 0; |
|
119 if (dial->maximum == dial->minimum) |
|
120 a = Q_PI / 2; |
|
121 else if (dial->dialWrapping) |
|
122 a = Q_PI * 3 / 2 - (currentSliderPosition - dial->minimum) * 2 * Q_PI |
|
123 / (dial->maximum - dial->minimum); |
|
124 else |
|
125 a = (Q_PI * 8 - (currentSliderPosition - dial->minimum) * 10 * Q_PI |
|
126 / (dial->maximum - dial->minimum)) / 6; |
|
127 qreal xc = width / 2.0; |
|
128 qreal yc = height / 2.0; |
|
129 qreal len = r - QStyleHelper::calcBigLineSize(r) - 3; |
|
130 qreal back = offset * len; |
|
131 QPointF pos(QPointF(xc + back * qCos(a), yc - back * qSin(a))); |
|
132 return pos; |
|
133 } |
|
134 |
|
135 qreal angle(const QPointF &p1, const QPointF &p2) |
|
136 { |
|
137 static const qreal rad_factor = 180 / Q_PI; |
|
138 qreal _angle = 0; |
|
139 |
|
140 if (p1.x() == p2.x()) { |
|
141 if (p1.y() < p2.y()) |
|
142 _angle = 270; |
|
143 else |
|
144 _angle = 90; |
|
145 } else { |
|
146 qreal x1, x2, y1, y2; |
|
147 |
|
148 if (p1.x() <= p2.x()) { |
|
149 x1 = p1.x(); y1 = p1.y(); |
|
150 x2 = p2.x(); y2 = p2.y(); |
|
151 } else { |
|
152 x2 = p1.x(); y2 = p1.y(); |
|
153 x1 = p2.x(); y1 = p2.y(); |
|
154 } |
|
155 |
|
156 qreal m = -(y2 - y1) / (x2 - x1); |
|
157 _angle = atan(m) * rad_factor; |
|
158 |
|
159 if (p1.x() < p2.x()) |
|
160 _angle = 180 - _angle; |
|
161 else |
|
162 _angle = -_angle; |
|
163 } |
|
164 return _angle; |
|
165 } |
|
166 |
|
167 QPolygonF calcLines(const QStyleOptionSlider *dial) |
|
168 { |
|
169 QPolygonF poly; |
|
170 int width = dial->rect.width(); |
|
171 int height = dial->rect.height(); |
|
172 qreal r = qMin(width, height) / 2; |
|
173 int bigLineSize = calcBigLineSize(int(r)); |
|
174 |
|
175 qreal xc = width / 2 + 0.5; |
|
176 qreal yc = height / 2 + 0.5; |
|
177 int ns = dial->tickInterval; |
|
178 int notches = (dial->maximum + ns - 1 - dial->minimum) / ns; |
|
179 if (notches <= 0) |
|
180 return poly; |
|
181 if (dial->maximum < dial->minimum || dial->maximum - dial->minimum > 1000) { |
|
182 int maximum = dial->minimum + 1000; |
|
183 notches = (maximum + ns - 1 - dial->minimum) / ns; |
|
184 } |
|
185 |
|
186 poly.resize(2 + 2 * notches); |
|
187 int smallLineSize = bigLineSize / 2; |
|
188 for (int i = 0; i <= notches; ++i) { |
|
189 qreal angle = dial->dialWrapping ? Q_PI * 3 / 2 - i * 2 * Q_PI / notches |
|
190 : (Q_PI * 8 - i * 10 * Q_PI / notches) / 6; |
|
191 qreal s = qSin(angle); |
|
192 qreal c = qCos(angle); |
|
193 if (i == 0 || (((ns * i) % (dial->pageStep ? dial->pageStep : 1)) == 0)) { |
|
194 poly[2 * i] = QPointF(xc + (r - bigLineSize) * c, |
|
195 yc - (r - bigLineSize) * s); |
|
196 poly[2 * i + 1] = QPointF(xc + r * c, yc - r * s); |
|
197 } else { |
|
198 poly[2 * i] = QPointF(xc + (r - 1 - smallLineSize) * c, |
|
199 yc - (r - 1 - smallLineSize) * s); |
|
200 poly[2 * i + 1] = QPointF(xc + (r - 1) * c, yc -(r - 1) * s); |
|
201 } |
|
202 } |
|
203 return poly; |
|
204 } |
|
205 |
|
206 // This will draw a nice and shiny QDial for us. We don't want |
|
207 // all the shinyness in QWindowsStyle, hence we place it here |
|
208 |
|
209 void drawDial(const QStyleOptionSlider *option, QPainter *painter) |
|
210 { |
|
211 QPalette pal = option->palette; |
|
212 QColor buttonColor = pal.button().color(); |
|
213 const int width = option->rect.width(); |
|
214 const int height = option->rect.height(); |
|
215 const bool enabled = option->state & QStyle::State_Enabled; |
|
216 qreal r = qMin(width, height) / 2; |
|
217 r -= r/50; |
|
218 const qreal penSize = r/20.0; |
|
219 |
|
220 painter->save(); |
|
221 painter->setRenderHint(QPainter::Antialiasing); |
|
222 |
|
223 // Draw notches |
|
224 if (option->subControls & QStyle::SC_DialTickmarks) { |
|
225 painter->setPen(option->palette.dark().color().darker(120)); |
|
226 painter->drawLines(QStyleHelper::calcLines(option)); |
|
227 } |
|
228 |
|
229 // Cache dial background |
|
230 BEGIN_STYLE_PIXMAPCACHE(QString::fromLatin1("qdial")); |
|
231 p->setRenderHint(QPainter::Antialiasing); |
|
232 |
|
233 const qreal d_ = r / 6; |
|
234 const qreal dx = option->rect.x() + d_ + (width - 2 * r) / 2 + 1; |
|
235 const qreal dy = option->rect.y() + d_ + (height - 2 * r) / 2 + 1; |
|
236 |
|
237 QRectF br = QRectF(dx + 0.5, dy + 0.5, |
|
238 int(r * 2 - 2 * d_ - 2), |
|
239 int(r * 2 - 2 * d_ - 2)); |
|
240 buttonColor.setHsv(buttonColor .hue(), |
|
241 qMin(140, buttonColor .saturation()), |
|
242 qMax(180, buttonColor.value())); |
|
243 QColor shadowColor(0, 0, 0, 20); |
|
244 |
|
245 if (enabled) { |
|
246 // Drop shadow |
|
247 qreal shadowSize = qMax(1.0, penSize/2.0); |
|
248 QRectF shadowRect= br.adjusted(-2*shadowSize, -2*shadowSize, |
|
249 2*shadowSize, 2*shadowSize); |
|
250 QRadialGradient shadowGradient(shadowRect.center().x(), |
|
251 shadowRect.center().y(), shadowRect.width()/2.0, |
|
252 shadowRect.center().x(), shadowRect.center().y()); |
|
253 shadowGradient.setColorAt(qreal(0.91), QColor(0, 0, 0, 40)); |
|
254 shadowGradient.setColorAt(qreal(1.0), Qt::transparent); |
|
255 p->setBrush(shadowGradient); |
|
256 p->setPen(Qt::NoPen); |
|
257 p->translate(shadowSize, shadowSize); |
|
258 p->drawEllipse(shadowRect); |
|
259 p->translate(-shadowSize, -shadowSize); |
|
260 |
|
261 // Main gradient |
|
262 QRadialGradient gradient(br.center().x() - br.width()/3, dy, |
|
263 br.width()*1.3, br.center().x(), |
|
264 br.center().y() - br.height()/2); |
|
265 gradient.setColorAt(0, buttonColor.lighter(110)); |
|
266 gradient.setColorAt(qreal(0.5), buttonColor); |
|
267 gradient.setColorAt(qreal(0.501), buttonColor.darker(102)); |
|
268 gradient.setColorAt(1, buttonColor.darker(115)); |
|
269 p->setBrush(gradient); |
|
270 } else { |
|
271 p->setBrush(Qt::NoBrush); |
|
272 } |
|
273 |
|
274 p->setPen(QPen(buttonColor.darker(280))); |
|
275 p->drawEllipse(br); |
|
276 p->setBrush(Qt::NoBrush); |
|
277 p->setPen(buttonColor.lighter(110)); |
|
278 p->drawEllipse(br.adjusted(1, 1, -1, -1)); |
|
279 |
|
280 if (option->state & QStyle::State_HasFocus) { |
|
281 QColor highlight = pal.highlight().color(); |
|
282 highlight.setHsv(highlight.hue(), |
|
283 qMin(160, highlight.saturation()), |
|
284 qMax(230, highlight.value())); |
|
285 highlight.setAlpha(127); |
|
286 p->setPen(QPen(highlight, 2.0)); |
|
287 p->setBrush(Qt::NoBrush); |
|
288 p->drawEllipse(br.adjusted(-1, -1, 1, 1)); |
|
289 } |
|
290 |
|
291 END_STYLE_PIXMAPCACHE |
|
292 |
|
293 QPointF dp = calcRadialPos(option, qreal(0.70)); |
|
294 buttonColor = buttonColor.lighter(104); |
|
295 buttonColor.setAlphaF(qreal(0.8)); |
|
296 const qreal ds = r/qreal(7.0); |
|
297 QRectF dialRect(dp.x() - ds, dp.y() - ds, 2*ds, 2*ds); |
|
298 QRadialGradient dialGradient(dialRect.center().x() + dialRect.width()/2, |
|
299 dialRect.center().y() + dialRect.width(), |
|
300 dialRect.width()*2, |
|
301 dialRect.center().x(), dialRect.center().y()); |
|
302 dialGradient.setColorAt(1, buttonColor.darker(140)); |
|
303 dialGradient.setColorAt(qreal(0.4), buttonColor.darker(120)); |
|
304 dialGradient.setColorAt(0, buttonColor.darker(110)); |
|
305 if (penSize > 3.0) { |
|
306 painter->setPen(QPen(QColor(0, 0, 0, 25), penSize)); |
|
307 painter->drawLine(calcRadialPos(option, qreal(0.90)), calcRadialPos(option, qreal(0.96))); |
|
308 } |
|
309 |
|
310 painter->setBrush(dialGradient); |
|
311 painter->setPen(QColor(255, 255, 255, 150)); |
|
312 painter->drawEllipse(dialRect.adjusted(-1, -1, 1, 1)); |
|
313 painter->setPen(QColor(0, 0, 0, 80)); |
|
314 painter->drawEllipse(dialRect); |
|
315 painter->restore(); |
|
316 } |
|
317 #endif //QT_NO_DIAL |
|
318 |
|
319 void drawBorderPixmap(const QPixmap &pixmap, QPainter *painter, const QRect &rect, |
|
320 int left, int top, int right, |
|
321 int bottom) |
|
322 { |
|
323 QSize size = pixmap.size(); |
|
324 //painter->setRenderHint(QPainter::SmoothPixmapTransform); |
|
325 |
|
326 //top |
|
327 if (top > 0) { |
|
328 painter->drawPixmap(QRect(rect.left() + left, rect.top(), rect.width() -right - left, top), pixmap, |
|
329 QRect(left, 0, size.width() -right - left, top)); |
|
330 |
|
331 //top-left |
|
332 if(left > 0) |
|
333 painter->drawPixmap(QRect(rect.left(), rect.top(), left, top), pixmap, |
|
334 QRect(0, 0, left, top)); |
|
335 |
|
336 //top-right |
|
337 if (right > 0) |
|
338 painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top(), right, top), pixmap, |
|
339 QRect(size.width() - right, 0, right, top)); |
|
340 } |
|
341 |
|
342 //left |
|
343 if (left > 0) |
|
344 painter->drawPixmap(QRect(rect.left(), rect.top()+top, left, rect.height() - top - bottom), pixmap, |
|
345 QRect(0, top, left, size.height() - bottom - top)); |
|
346 |
|
347 //center |
|
348 painter->drawPixmap(QRect(rect.left() + left, rect.top()+top, rect.width() -right - left, |
|
349 rect.height() - bottom - top), pixmap, |
|
350 QRect(left, top, size.width() -right -left, |
|
351 size.height() - bottom - top)); |
|
352 //right |
|
353 if (right > 0) |
|
354 painter->drawPixmap(QRect(rect.left() +rect.width() - right, rect.top()+top, right, rect.height() - top - bottom), pixmap, |
|
355 QRect(size.width() - right, top, right, size.height() - bottom - top)); |
|
356 |
|
357 //bottom |
|
358 if (bottom > 0) { |
|
359 painter->drawPixmap(QRect(rect.left() +left, rect.top() + rect.height() - bottom, |
|
360 rect.width() - right - left, bottom), pixmap, |
|
361 QRect(left, size.height() - bottom, |
|
362 size.width() - right - left, bottom)); |
|
363 //bottom-left |
|
364 if (left > 0) |
|
365 painter->drawPixmap(QRect(rect.left(), rect.top() + rect.height() - bottom, left, bottom), pixmap, |
|
366 QRect(0, size.height() - bottom, left, bottom)); |
|
367 |
|
368 //bottom-right |
|
369 if (right > 0) |
|
370 painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top() + rect.height() - bottom, right, bottom), pixmap, |
|
371 QRect(size.width() - right, size.height() - bottom, right, bottom)); |
|
372 |
|
373 } |
|
374 } |
|
375 } |
|
376 QT_END_NAMESPACE |