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/qhighdpiscaling_p.h>
44#include <private/qguiapplication_p.h>
45#include <private/qmath_p.h>
46#include <private/qstyle_p.h>
47#include <qmath.h>
48#if QT_CONFIG(scrollbar)
49#include <qscrollbar.h>
50#endif
51#include <qabstractscrollarea.h>
52#include <qwindow.h>
53
54#include <qmetaobject.h>
55#include "qstylehelper_p.h"
56#include <qstringbuilder.h>
57
58QT_BEGIN_NAMESPACE
59
60Q_GUI_EXPORT int qt_defaultDpiX();
61
62namespace QStyleHelper {
63
64QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size)
65{
66 const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(opt: option);
67 QString tmp = key % HexString<uint>(option->state)
68 % HexString<uint>(option->direction)
69 % HexString<uint>(complexOption ? uint(complexOption->activeSubControls) : 0u)
70 % HexString<quint64>(option->palette.cacheKey())
71 % HexString<uint>(size.width())
72 % HexString<uint>(size.height());
73
74#if QT_CONFIG(spinbox)
75 if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(opt: option)) {
76 tmp = tmp % HexString<uint>(spinBox->buttonSymbols)
77 % HexString<uint>(spinBox->stepEnabled)
78 % QLatin1Char(spinBox->frame ? '1' : '0'); ;
79 }
80#endif // QT_CONFIG(spinbox)
81
82 return tmp;
83}
84
85#ifdef Q_OS_DARWIN
86static const qreal qstyleBaseDpi = 72;
87#else
88static const qreal qstyleBaseDpi = 96;
89#endif
90
91Q_WIDGETS_EXPORT qreal dpi(const QStyleOption *option)
92{
93#ifndef Q_OS_DARWIN
94 // Prioritize the application override, except for on macOS where
95 // we have historically not supported the AA_Use96Dpi flag.
96 if (QCoreApplication::testAttribute(attribute: Qt::AA_Use96Dpi))
97 return 96;
98#endif
99
100 // Expect that QStyleOption::QFontMetrics::QFont has the correct DPI set
101 if (option)
102 return option->fontMetrics.fontDpi();
103
104 // Fall back to historical Qt behavior: hardocded 72 DPI on mac,
105 // primary screen DPI on other platforms.
106#ifdef Q_OS_DARWIN
107 return qstyleBaseDpi;
108#else
109 return qt_defaultDpiX();
110#endif
111}
112
113Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, qreal dpi)
114{
115 return value * dpi / qstyleBaseDpi;
116}
117
118Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QPaintDevice *device)
119{
120 return dpiScaled(value, dpi: device->logicalDpiX());
121}
122
123Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QStyleOption *option)
124{
125 return dpiScaled(value, dpi: dpi(option));
126}
127
128#ifndef QT_NO_ACCESSIBILITY
129bool isInstanceOf(QObject *obj, QAccessible::Role role)
130{
131 bool match = false;
132 QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(obj);
133 match = iface && iface->role() == role;
134 return match;
135}
136
137// Searches for an ancestor of a particular accessible role
138bool hasAncestor(QObject *obj, QAccessible::Role role)
139{
140 bool found = false;
141 QObject *parent = obj ? obj->parent() : nullptr;
142 while (parent && !found) {
143 if (isInstanceOf(obj: parent, role))
144 found = true;
145 parent = parent->parent();
146 }
147 return found;
148}
149#endif // QT_NO_ACCESSIBILITY
150
151
152#if QT_CONFIG(dial)
153
154int calcBigLineSize(int radius)
155{
156 int bigLineSize = radius / 6;
157 if (bigLineSize < 4)
158 bigLineSize = 4;
159 if (bigLineSize > radius / 2)
160 bigLineSize = radius / 2;
161 return bigLineSize;
162}
163
164static QPointF calcRadialPos(const QStyleOptionSlider *dial, qreal offset)
165{
166 const int width = dial->rect.width();
167 const int height = dial->rect.height();
168 const int r = qMin(a: width, b: height) / 2;
169 const int currentSliderPosition = dial->upsideDown ? dial->sliderPosition : (dial->maximum - dial->sliderPosition);
170 qreal a = 0;
171 if (dial->maximum == dial->minimum)
172 a = Q_PI / 2;
173 else if (dial->dialWrapping)
174 a = Q_PI * 3 / 2 - (currentSliderPosition - dial->minimum) * 2 * Q_PI
175 / (dial->maximum - dial->minimum);
176 else
177 a = (Q_PI * 8 - (currentSliderPosition - dial->minimum) * 10 * Q_PI
178 / (dial->maximum - dial->minimum)) / 6;
179 qreal xc = width / 2.0;
180 qreal yc = height / 2.0;
181 qreal len = r - QStyleHelper::calcBigLineSize(radius: r) - 3;
182 qreal back = offset * len;
183 QPointF pos(QPointF(xc + back * qCos(v: a), yc - back * qSin(v: a)));
184 return pos;
185}
186
187qreal angle(const QPointF &p1, const QPointF &p2)
188{
189 static const qreal rad_factor = 180 / Q_PI;
190 qreal _angle = 0;
191
192 if (p1.x() == p2.x()) {
193 if (p1.y() < p2.y())
194 _angle = 270;
195 else
196 _angle = 90;
197 } else {
198 qreal x1, x2, y1, y2;
199
200 if (p1.x() <= p2.x()) {
201 x1 = p1.x(); y1 = p1.y();
202 x2 = p2.x(); y2 = p2.y();
203 } else {
204 x2 = p1.x(); y2 = p1.y();
205 x1 = p2.x(); y1 = p2.y();
206 }
207
208 qreal m = -(y2 - y1) / (x2 - x1);
209 _angle = qAtan(v: m) * rad_factor;
210
211 if (p1.x() < p2.x())
212 _angle = 180 - _angle;
213 else
214 _angle = -_angle;
215 }
216 return _angle;
217}
218
219QPolygonF calcLines(const QStyleOptionSlider *dial)
220{
221 QPolygonF poly;
222 int width = dial->rect.width();
223 int height = dial->rect.height();
224 qreal r = qMin(a: width, b: height) / 2;
225 int bigLineSize = calcBigLineSize(radius: int(r));
226
227 qreal xc = width / 2 + 0.5;
228 qreal yc = height / 2 + 0.5;
229 const int ns = dial->tickInterval;
230 if (!ns) // Invalid values may be set by Qt Designer.
231 return poly;
232 int notches = (dial->maximum + ns - 1 - dial->minimum) / ns;
233 if (notches <= 0)
234 return poly;
235 if (dial->maximum < dial->minimum || dial->maximum - dial->minimum > 1000) {
236 int maximum = dial->minimum + 1000;
237 notches = (maximum + ns - 1 - dial->minimum) / ns;
238 }
239
240 poly.resize(asize: 2 + 2 * notches);
241 int smallLineSize = bigLineSize / 2;
242 for (int i = 0; i <= notches; ++i) {
243 qreal angle = dial->dialWrapping ? Q_PI * 3 / 2 - i * 2 * Q_PI / notches
244 : (Q_PI * 8 - i * 10 * Q_PI / notches) / 6;
245 qreal s = qSin(v: angle);
246 qreal c = qCos(v: angle);
247 if (i == 0 || (((ns * i) % (dial->pageStep ? dial->pageStep : 1)) == 0)) {
248 poly[2 * i] = QPointF(xc + (r - bigLineSize) * c,
249 yc - (r - bigLineSize) * s);
250 poly[2 * i + 1] = QPointF(xc + r * c, yc - r * s);
251 } else {
252 poly[2 * i] = QPointF(xc + (r - 1 - smallLineSize) * c,
253 yc - (r - 1 - smallLineSize) * s);
254 poly[2 * i + 1] = QPointF(xc + (r - 1) * c, yc -(r - 1) * s);
255 }
256 }
257 return poly;
258}
259
260// This will draw a nice and shiny QDial for us. We don't want
261// all the shinyness in QWindowsStyle, hence we place it here
262
263void drawDial(const QStyleOptionSlider *option, QPainter *painter)
264{
265 QPalette pal = option->palette;
266 QColor buttonColor = pal.button().color();
267 const int width = option->rect.width();
268 const int height = option->rect.height();
269 const bool enabled = option->state & QStyle::State_Enabled;
270 qreal r = qMin(a: width, b: height) / 2;
271 r -= r/50;
272 const qreal penSize = r/20.0;
273
274 painter->save();
275 painter->setRenderHint(hint: QPainter::Antialiasing);
276
277 // Draw notches
278 if (option->subControls & QStyle::SC_DialTickmarks) {
279 painter->setPen(option->palette.dark().color().darker(f: 120));
280 painter->drawLines(pointPairs: QStyleHelper::calcLines(dial: option));
281 }
282
283 // setting color before BEGIN_STYLE_PIXMAPCACHE since
284 // otherwise it is not set when the image is in the cache
285 buttonColor.setHsv(h: buttonColor .hue(),
286 s: qMin(a: 140, b: buttonColor .saturation()),
287 v: qMax(a: 180, b: buttonColor.value()));
288
289 // Cache dial background
290 BEGIN_STYLE_PIXMAPCACHE(QString::fromLatin1("qdial"));
291 p->setRenderHint(hint: QPainter::Antialiasing);
292
293 const qreal d_ = r / 6;
294 const qreal dx = option->rect.x() + d_ + (width - 2 * r) / 2 + 1;
295 const qreal dy = option->rect.y() + d_ + (height - 2 * r) / 2 + 1;
296
297 QRectF br = QRectF(dx + 0.5, dy + 0.5,
298 int(r * 2 - 2 * d_ - 2),
299 int(r * 2 - 2 * d_ - 2));
300
301 if (enabled) {
302 // Drop shadow
303 qreal shadowSize = qMax(a: 1.0, b: penSize/2.0);
304 QRectF shadowRect= br.adjusted(xp1: -2*shadowSize, yp1: -2*shadowSize,
305 xp2: 2*shadowSize, yp2: 2*shadowSize);
306 QRadialGradient shadowGradient(shadowRect.center().x(),
307 shadowRect.center().y(), shadowRect.width()/2.0,
308 shadowRect.center().x(), shadowRect.center().y());
309 shadowGradient.setColorAt(pos: qreal(0.91), color: QColor(0, 0, 0, 40));
310 shadowGradient.setColorAt(pos: qreal(1.0), color: Qt::transparent);
311 p->setBrush(shadowGradient);
312 p->setPen(Qt::NoPen);
313 p->translate(dx: shadowSize, dy: shadowSize);
314 p->drawEllipse(r: shadowRect);
315 p->translate(dx: -shadowSize, dy: -shadowSize);
316
317 // Main gradient
318 QRadialGradient gradient(br.center().x() - br.width()/3, dy,
319 br.width()*1.3, br.center().x(),
320 br.center().y() - br.height()/2);
321 gradient.setColorAt(pos: 0, color: buttonColor.lighter(f: 110));
322 gradient.setColorAt(pos: qreal(0.5), color: buttonColor);
323 gradient.setColorAt(pos: qreal(0.501), color: buttonColor.darker(f: 102));
324 gradient.setColorAt(pos: 1, color: buttonColor.darker(f: 115));
325 p->setBrush(gradient);
326 } else {
327 p->setBrush(Qt::NoBrush);
328 }
329
330 p->setPen(QPen(buttonColor.darker(f: 280)));
331 p->drawEllipse(r: br);
332 p->setBrush(Qt::NoBrush);
333 p->setPen(buttonColor.lighter(f: 110));
334 p->drawEllipse(r: br.adjusted(xp1: 1, yp1: 1, xp2: -1, yp2: -1));
335
336 if (option->state & QStyle::State_HasFocus) {
337 QColor highlight = pal.highlight().color();
338 highlight.setHsv(h: highlight.hue(),
339 s: qMin(a: 160, b: highlight.saturation()),
340 v: qMax(a: 230, b: highlight.value()));
341 highlight.setAlpha(127);
342 p->setPen(QPen(highlight, 2.0));
343 p->setBrush(Qt::NoBrush);
344 p->drawEllipse(r: br.adjusted(xp1: -1, yp1: -1, xp2: 1, yp2: 1));
345 }
346
347 END_STYLE_PIXMAPCACHE
348
349 QPointF dp = calcRadialPos(dial: option, offset: qreal(0.70));
350 buttonColor = buttonColor.lighter(f: 104);
351 buttonColor.setAlphaF(qreal(0.8));
352 const qreal ds = r/qreal(7.0);
353 QRectF dialRect(dp.x() - ds, dp.y() - ds, 2*ds, 2*ds);
354 QRadialGradient dialGradient(dialRect.center().x() + dialRect.width()/2,
355 dialRect.center().y() + dialRect.width(),
356 dialRect.width()*2,
357 dialRect.center().x(), dialRect.center().y());
358 dialGradient.setColorAt(pos: 1, color: buttonColor.darker(f: 140));
359 dialGradient.setColorAt(pos: qreal(0.4), color: buttonColor.darker(f: 120));
360 dialGradient.setColorAt(pos: 0, color: buttonColor.darker(f: 110));
361 if (penSize > 3.0) {
362 painter->setPen(QPen(QColor(0, 0, 0, 25), penSize));
363 painter->drawLine(p1: calcRadialPos(dial: option, offset: qreal(0.90)), p2: calcRadialPos(dial: option, offset: qreal(0.96)));
364 }
365
366 painter->setBrush(dialGradient);
367 painter->setPen(QColor(255, 255, 255, 150));
368 painter->drawEllipse(r: dialRect.adjusted(xp1: -1, yp1: -1, xp2: 1, yp2: 1));
369 painter->setPen(QColor(0, 0, 0, 80));
370 painter->drawEllipse(r: dialRect);
371 painter->restore();
372}
373#endif //QT_CONFIG(dial)
374
375void drawBorderPixmap(const QPixmap &pixmap, QPainter *painter, const QRect &rect,
376 int left, int top, int right,
377 int bottom)
378{
379 QSize size = pixmap.size();
380 //painter->setRenderHint(QPainter::SmoothPixmapTransform);
381
382 //top
383 if (top > 0) {
384 painter->drawPixmap(targetRect: QRect(rect.left() + left, rect.top(), rect.width() -right - left, top), pixmap,
385 sourceRect: QRect(left, 0, size.width() -right - left, top));
386
387 //top-left
388 if(left > 0)
389 painter->drawPixmap(targetRect: QRect(rect.left(), rect.top(), left, top), pixmap,
390 sourceRect: QRect(0, 0, left, top));
391
392 //top-right
393 if (right > 0)
394 painter->drawPixmap(targetRect: QRect(rect.left() + rect.width() - right, rect.top(), right, top), pixmap,
395 sourceRect: QRect(size.width() - right, 0, right, top));
396 }
397
398 //left
399 if (left > 0)
400 painter->drawPixmap(targetRect: QRect(rect.left(), rect.top()+top, left, rect.height() - top - bottom), pixmap,
401 sourceRect: QRect(0, top, left, size.height() - bottom - top));
402
403 //center
404 painter->drawPixmap(targetRect: QRect(rect.left() + left, rect.top()+top, rect.width() -right - left,
405 rect.height() - bottom - top), pixmap,
406 sourceRect: QRect(left, top, size.width() -right -left,
407 size.height() - bottom - top));
408 //right
409 if (right > 0)
410 painter->drawPixmap(targetRect: QRect(rect.left() +rect.width() - right, rect.top()+top, right, rect.height() - top - bottom), pixmap,
411 sourceRect: QRect(size.width() - right, top, right, size.height() - bottom - top));
412
413 //bottom
414 if (bottom > 0) {
415 painter->drawPixmap(targetRect: QRect(rect.left() +left, rect.top() + rect.height() - bottom,
416 rect.width() - right - left, bottom), pixmap,
417 sourceRect: QRect(left, size.height() - bottom,
418 size.width() - right - left, bottom));
419 //bottom-left
420 if (left > 0)
421 painter->drawPixmap(targetRect: QRect(rect.left(), rect.top() + rect.height() - bottom, left, bottom), pixmap,
422 sourceRect: QRect(0, size.height() - bottom, left, bottom));
423
424 //bottom-right
425 if (right > 0)
426 painter->drawPixmap(targetRect: QRect(rect.left() + rect.width() - right, rect.top() + rect.height() - bottom, right, bottom), pixmap,
427 sourceRect: QRect(size.width() - right, size.height() - bottom, right, bottom));
428
429 }
430}
431
432QColor backgroundColor(const QPalette &pal, const QWidget* widget)
433{
434#if QT_CONFIG(scrollarea)
435 if (qobject_cast<const QScrollBar *>(object: widget) && widget->parent() &&
436 qobject_cast<const QAbstractScrollArea *>(object: widget->parent()->parent()))
437 return widget->parentWidget()->parentWidget()->palette().color(cr: QPalette::Base);
438#else
439 Q_UNUSED(widget);
440#endif
441 return pal.color(cr: QPalette::Base);
442}
443
444WidgetSizePolicy widgetSizePolicy(const QWidget *widget, const QStyleOption *opt)
445{
446 while (widget) {
447 if (widget->testAttribute(attribute: Qt::WA_MacMiniSize)) {
448 return SizeMini;
449 } else if (widget->testAttribute(attribute: Qt::WA_MacSmallSize)) {
450 return SizeSmall;
451 } else if (widget->testAttribute(attribute: Qt::WA_MacNormalSize)) {
452 return SizeLarge;
453 }
454 widget = widget->parentWidget();
455 }
456
457 if (opt && opt->state & QStyle::State_Mini)
458 return SizeMini;
459 else if (opt && opt->state & QStyle::State_Small)
460 return SizeSmall;
461
462 return SizeDefault;
463}
464
465}
466QT_END_NAMESPACE
467

source code of qtbase/src/widgets/styles/qstylehelper.cpp