1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <qstyleoption.h>
41#include <qpainter.h>
42#include <qpixmapcache.h>
43#include <private/qmath_p.h>
44#include <private/qstyle_p.h>
45#include <qmath.h>
46#if QT_CONFIG(scrollbar)
47#include <qscrollbar.h>
48#endif
49#include <qabstractscrollarea.h>
50#include <qwindow.h>
51
52#include "qstylehelper_p.h"
53#include <qstringbuilder.h>
54
55QT_BEGIN_NAMESPACE
56
57Q_GUI_EXPORT int qt_defaultDpiX();
58
59namespace QStyleHelper {
60
61QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size)
62{
63 const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(option);
64 QString tmp = key % HexString<uint>(option->state)
65 % HexString<uint>(option->direction)
66 % HexString<uint>(complexOption ? uint(complexOption->activeSubControls) : 0u)
67 % HexString<quint64>(option->palette.cacheKey())
68 % HexString<uint>(size.width())
69 % HexString<uint>(size.height());
70
71#if QT_CONFIG(spinbox)
72 if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) {
73 tmp = tmp % HexString<uint>(spinBox->buttonSymbols)
74 % HexString<uint>(spinBox->stepEnabled)
75 % QLatin1Char(spinBox->frame ? '1' : '0'); ;
76 }
77#endif // QT_CONFIG(spinbox)
78
79 return tmp;
80}
81
82qreal dpiScaled(qreal value)
83{
84#ifdef Q_OS_MAC
85 // On mac the DPI is always 72 so we should not scale it
86 return value;
87#else
88 static const qreal scale = qreal(qt_defaultDpiX()) / 96.0;
89 return value * scale;
90#endif
91}
92
93#ifndef QT_NO_ACCESSIBILITY
94bool isInstanceOf(QObject *obj, QAccessible::Role role)
95{
96 bool match = false;
97 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(obj);
98 match = iface && iface->role() == role;
99 return match;
100}
101
102// Searches for an ancestor of a particular accessible role
103bool hasAncestor(QObject *obj, QAccessible::Role role)
104{
105 bool found = false;
106 QObject *parent = obj ? obj->parent() : 0;
107 while (parent && !found) {
108 if (isInstanceOf(parent, role))
109 found = true;
110 parent = parent->parent();
111 }
112 return found;
113}
114#endif // QT_NO_ACCESSIBILITY
115
116
117#if QT_CONFIG(dial)
118
119int calcBigLineSize(int radius)
120{
121 int bigLineSize = radius / 6;
122 if (bigLineSize < 4)
123 bigLineSize = 4;
124 if (bigLineSize > radius / 2)
125 bigLineSize = radius / 2;
126 return bigLineSize;
127}
128
129static QPointF calcRadialPos(const QStyleOptionSlider *dial, qreal offset)
130{
131 const int width = dial->rect.width();
132 const int height = dial->rect.height();
133 const int r = qMin(width, height) / 2;
134 const int currentSliderPosition = dial->upsideDown ? dial->sliderPosition : (dial->maximum - dial->sliderPosition);
135 qreal a = 0;
136 if (dial->maximum == dial->minimum)
137 a = Q_PI / 2;
138 else if (dial->dialWrapping)
139 a = Q_PI * 3 / 2 - (currentSliderPosition - dial->minimum) * 2 * Q_PI
140 / (dial->maximum - dial->minimum);
141 else
142 a = (Q_PI * 8 - (currentSliderPosition - dial->minimum) * 10 * Q_PI
143 / (dial->maximum - dial->minimum)) / 6;
144 qreal xc = width / 2.0;
145 qreal yc = height / 2.0;
146 qreal len = r - QStyleHelper::calcBigLineSize(r) - 3;
147 qreal back = offset * len;
148 QPointF pos(QPointF(xc + back * qCos(a), yc - back * qSin(a)));
149 return pos;
150}
151
152qreal angle(const QPointF &p1, const QPointF &p2)
153{
154 static const qreal rad_factor = 180 / Q_PI;
155 qreal _angle = 0;
156
157 if (p1.x() == p2.x()) {
158 if (p1.y() < p2.y())
159 _angle = 270;
160 else
161 _angle = 90;
162 } else {
163 qreal x1, x2, y1, y2;
164
165 if (p1.x() <= p2.x()) {
166 x1 = p1.x(); y1 = p1.y();
167 x2 = p2.x(); y2 = p2.y();
168 } else {
169 x2 = p1.x(); y2 = p1.y();
170 x1 = p2.x(); y1 = p2.y();
171 }
172
173 qreal m = -(y2 - y1) / (x2 - x1);
174 _angle = qAtan(m) * rad_factor;
175
176 if (p1.x() < p2.x())
177 _angle = 180 - _angle;
178 else
179 _angle = -_angle;
180 }
181 return _angle;
182}
183
184QPolygonF calcLines(const QStyleOptionSlider *dial)
185{
186 QPolygonF poly;
187 int width = dial->rect.width();
188 int height = dial->rect.height();
189 qreal r = qMin(width, height) / 2;
190 int bigLineSize = calcBigLineSize(int(r));
191
192 qreal xc = width / 2 + 0.5;
193 qreal yc = height / 2 + 0.5;
194 const int ns = dial->tickInterval;
195 if (!ns) // Invalid values may be set by Qt Designer.
196 return poly;
197 int notches = (dial->maximum + ns - 1 - dial->minimum) / ns;
198 if (notches <= 0)
199 return poly;
200 if (dial->maximum < dial->minimum || dial->maximum - dial->minimum > 1000) {
201 int maximum = dial->minimum + 1000;
202 notches = (maximum + ns - 1 - dial->minimum) / ns;
203 }
204
205 poly.resize(2 + 2 * notches);
206 int smallLineSize = bigLineSize / 2;
207 for (int i = 0; i <= notches; ++i) {
208 qreal angle = dial->dialWrapping ? Q_PI * 3 / 2 - i * 2 * Q_PI / notches
209 : (Q_PI * 8 - i * 10 * Q_PI / notches) / 6;
210 qreal s = qSin(angle);
211 qreal c = qCos(angle);
212 if (i == 0 || (((ns * i) % (dial->pageStep ? dial->pageStep : 1)) == 0)) {
213 poly[2 * i] = QPointF(xc + (r - bigLineSize) * c,
214 yc - (r - bigLineSize) * s);
215 poly[2 * i + 1] = QPointF(xc + r * c, yc - r * s);
216 } else {
217 poly[2 * i] = QPointF(xc + (r - 1 - smallLineSize) * c,
218 yc - (r - 1 - smallLineSize) * s);
219 poly[2 * i + 1] = QPointF(xc + (r - 1) * c, yc -(r - 1) * s);
220 }
221 }
222 return poly;
223}
224
225// This will draw a nice and shiny QDial for us. We don't want
226// all the shinyness in QWindowsStyle, hence we place it here
227
228void drawDial(const QStyleOptionSlider *option, QPainter *painter)
229{
230 QPalette pal = option->palette;
231 QColor buttonColor = pal.button().color();
232 const int width = option->rect.width();
233 const int height = option->rect.height();
234 const bool enabled = option->state & QStyle::State_Enabled;
235 qreal r = qMin(width, height) / 2;
236 r -= r/50;
237 const qreal penSize = r/20.0;
238
239 painter->save();
240 painter->setRenderHint(QPainter::Antialiasing);
241
242 // Draw notches
243 if (option->subControls & QStyle::SC_DialTickmarks) {
244 painter->setPen(option->palette.dark().color().darker(120));
245 painter->drawLines(QStyleHelper::calcLines(option));
246 }
247
248 // Cache dial background
249 BEGIN_STYLE_PIXMAPCACHE(QString::fromLatin1("qdial"));
250 p->setRenderHint(QPainter::Antialiasing);
251
252 const qreal d_ = r / 6;
253 const qreal dx = option->rect.x() + d_ + (width - 2 * r) / 2 + 1;
254 const qreal dy = option->rect.y() + d_ + (height - 2 * r) / 2 + 1;
255
256 QRectF br = QRectF(dx + 0.5, dy + 0.5,
257 int(r * 2 - 2 * d_ - 2),
258 int(r * 2 - 2 * d_ - 2));
259 buttonColor.setHsv(buttonColor .hue(),
260 qMin(140, buttonColor .saturation()),
261 qMax(180, buttonColor.value()));
262
263 if (enabled) {
264 // Drop shadow
265 qreal shadowSize = qMax(1.0, penSize/2.0);
266 QRectF shadowRect= br.adjusted(-2*shadowSize, -2*shadowSize,
267 2*shadowSize, 2*shadowSize);
268 QRadialGradient shadowGradient(shadowRect.center().x(),
269 shadowRect.center().y(), shadowRect.width()/2.0,
270 shadowRect.center().x(), shadowRect.center().y());
271 shadowGradient.setColorAt(qreal(0.91), QColor(0, 0, 0, 40));
272 shadowGradient.setColorAt(qreal(1.0), Qt::transparent);
273 p->setBrush(shadowGradient);
274 p->setPen(Qt::NoPen);
275 p->translate(shadowSize, shadowSize);
276 p->drawEllipse(shadowRect);
277 p->translate(-shadowSize, -shadowSize);
278
279 // Main gradient
280 QRadialGradient gradient(br.center().x() - br.width()/3, dy,
281 br.width()*1.3, br.center().x(),
282 br.center().y() - br.height()/2);
283 gradient.setColorAt(0, buttonColor.lighter(110));
284 gradient.setColorAt(qreal(0.5), buttonColor);
285 gradient.setColorAt(qreal(0.501), buttonColor.darker(102));
286 gradient.setColorAt(1, buttonColor.darker(115));
287 p->setBrush(gradient);
288 } else {
289 p->setBrush(Qt::NoBrush);
290 }
291
292 p->setPen(QPen(buttonColor.darker(280)));
293 p->drawEllipse(br);
294 p->setBrush(Qt::NoBrush);
295 p->setPen(buttonColor.lighter(110));
296 p->drawEllipse(br.adjusted(1, 1, -1, -1));
297
298 if (option->state & QStyle::State_HasFocus) {
299 QColor highlight = pal.highlight().color();
300 highlight.setHsv(highlight.hue(),
301 qMin(160, highlight.saturation()),
302 qMax(230, highlight.value()));
303 highlight.setAlpha(127);
304 p->setPen(QPen(highlight, 2.0));
305 p->setBrush(Qt::NoBrush);
306 p->drawEllipse(br.adjusted(-1, -1, 1, 1));
307 }
308
309 END_STYLE_PIXMAPCACHE
310
311 QPointF dp = calcRadialPos(option, qreal(0.70));
312 buttonColor = buttonColor.lighter(104);
313 buttonColor.setAlphaF(qreal(0.8));
314 const qreal ds = r/qreal(7.0);
315 QRectF dialRect(dp.x() - ds, dp.y() - ds, 2*ds, 2*ds);
316 QRadialGradient dialGradient(dialRect.center().x() + dialRect.width()/2,
317 dialRect.center().y() + dialRect.width(),
318 dialRect.width()*2,
319 dialRect.center().x(), dialRect.center().y());
320 dialGradient.setColorAt(1, buttonColor.darker(140));
321 dialGradient.setColorAt(qreal(0.4), buttonColor.darker(120));
322 dialGradient.setColorAt(0, buttonColor.darker(110));
323 if (penSize > 3.0) {
324 painter->setPen(QPen(QColor(0, 0, 0, 25), penSize));
325 painter->drawLine(calcRadialPos(option, qreal(0.90)), calcRadialPos(option, qreal(0.96)));
326 }
327
328 painter->setBrush(dialGradient);
329 painter->setPen(QColor(255, 255, 255, 150));
330 painter->drawEllipse(dialRect.adjusted(-1, -1, 1, 1));
331 painter->setPen(QColor(0, 0, 0, 80));
332 painter->drawEllipse(dialRect);
333 painter->restore();
334}
335#endif //QT_CONFIG(dial)
336
337void drawBorderPixmap(const QPixmap &pixmap, QPainter *painter, const QRect &rect,
338 int left, int top, int right,
339 int bottom)
340{
341 QSize size = pixmap.size();
342 //painter->setRenderHint(QPainter::SmoothPixmapTransform);
343
344 //top
345 if (top > 0) {
346 painter->drawPixmap(QRect(rect.left() + left, rect.top(), rect.width() -right - left, top), pixmap,
347 QRect(left, 0, size.width() -right - left, top));
348
349 //top-left
350 if(left > 0)
351 painter->drawPixmap(QRect(rect.left(), rect.top(), left, top), pixmap,
352 QRect(0, 0, left, top));
353
354 //top-right
355 if (right > 0)
356 painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top(), right, top), pixmap,
357 QRect(size.width() - right, 0, right, top));
358 }
359
360 //left
361 if (left > 0)
362 painter->drawPixmap(QRect(rect.left(), rect.top()+top, left, rect.height() - top - bottom), pixmap,
363 QRect(0, top, left, size.height() - bottom - top));
364
365 //center
366 painter->drawPixmap(QRect(rect.left() + left, rect.top()+top, rect.width() -right - left,
367 rect.height() - bottom - top), pixmap,
368 QRect(left, top, size.width() -right -left,
369 size.height() - bottom - top));
370 //right
371 if (right > 0)
372 painter->drawPixmap(QRect(rect.left() +rect.width() - right, rect.top()+top, right, rect.height() - top - bottom), pixmap,
373 QRect(size.width() - right, top, right, size.height() - bottom - top));
374
375 //bottom
376 if (bottom > 0) {
377 painter->drawPixmap(QRect(rect.left() +left, rect.top() + rect.height() - bottom,
378 rect.width() - right - left, bottom), pixmap,
379 QRect(left, size.height() - bottom,
380 size.width() - right - left, bottom));
381 //bottom-left
382 if (left > 0)
383 painter->drawPixmap(QRect(rect.left(), rect.top() + rect.height() - bottom, left, bottom), pixmap,
384 QRect(0, size.height() - bottom, left, bottom));
385
386 //bottom-right
387 if (right > 0)
388 painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top() + rect.height() - bottom, right, bottom), pixmap,
389 QRect(size.width() - right, size.height() - bottom, right, bottom));
390
391 }
392}
393
394QColor backgroundColor(const QPalette &pal, const QWidget* widget)
395{
396#if QT_CONFIG(scrollarea)
397 if (qobject_cast<const QScrollBar *>(widget) && widget->parent() &&
398 qobject_cast<const QAbstractScrollArea *>(widget->parent()->parent()))
399 return widget->parentWidget()->parentWidget()->palette().color(QPalette::Base);
400#else
401 Q_UNUSED(widget);
402#endif
403 return pal.color(QPalette::Base);
404}
405
406WidgetSizePolicy widgetSizePolicy(const QWidget *widget, const QStyleOption *opt)
407{
408 while (widget) {
409 if (widget->testAttribute(Qt::WA_MacMiniSize)) {
410 return SizeMini;
411 } else if (widget->testAttribute(Qt::WA_MacSmallSize)) {
412 return SizeSmall;
413 } else if (widget->testAttribute(Qt::WA_MacNormalSize)) {
414 return SizeLarge;
415 }
416 widget = widget->parentWidget();
417 }
418
419 if (opt && opt->state & QStyle::State_Mini)
420 return SizeMini;
421 else if (opt && opt->state & QStyle::State_Small)
422 return SizeSmall;
423
424 return SizeDefault;
425}
426
427}
428QT_END_NAMESPACE
429