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