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

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

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