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
qstylehelper.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#include <qstyleoption.h>
6#include <qpainter.h>
7#include <qpixmapcache.h>
8#include <private/qhighdpiscaling_p.h>
9#include <private/qguiapplication_p.h>
10#include <private/qmath_p.h>
11#include <private/qstyle_p.h>
12#include <qmath.h>
13#if QT_CONFIG(scrollbar)
14#include <qscrollbar.h>
15#endif
16#include <qabstractscrollarea.h>
17#include <qwindow.h>
18
19#include <qmetaobject.h>
20#include "qstylehelper_p.h"
21#include <qstringbuilder.h>
22
24
25Q_GUI_EXPORT int qt_defaultDpiX();
26
27namespace QStyleHelper {
28
29static inline bool usePixmapCache(const QStyleOption *opt)
30{
31 if (QWidget *widget = qobject_cast<QWidget *>(opt->styleObject))
32 return !widget->testAttribute(Qt::WA_StyleSheetTarget);
33 return true;
34}
35
36QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size, qreal dpr)
37{
38 if (!usePixmapCache(option))
39 return {};
40
41 const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(option);
42 QString tmp = key % HexString<uint>(option->state)
43 % HexString<uint>(option->direction)
44 % HexString<uint>(complexOption ? uint(complexOption->activeSubControls) : 0u)
45 % HexString<quint64>(option->palette.cacheKey())
46 % HexString<uint>(size.width())
47 % HexString<uint>(size.height())
48 % HexString<qreal>(dpr);
49
50#if QT_CONFIG(spinbox)
51 if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) {
52 tmp = tmp % HexString<uint>(spinBox->buttonSymbols)
53 % HexString<uint>(spinBox->stepEnabled)
54 % QChar(spinBox->frame ? u'1' : u'0');
55 }
56#endif // QT_CONFIG(spinbox)
57
58 return tmp;
59}
60
61#ifdef Q_OS_DARWIN
62static const qreal qstyleBaseDpi = 72;
63#else
64static const qreal qstyleBaseDpi = 96;
65#endif
66
67Q_WIDGETS_EXPORT qreal dpi(const QStyleOption *option)
68{
69#ifndef Q_OS_DARWIN
70 // Prioritize the application override, except for on macOS where
71 // we have historically not supported the AA_Use96Dpi flag.
73 return 96;
74#endif
75
76 // Expect that QStyleOption::QFontMetrics::QFont has the correct DPI set
77 if (option)
78 return option->fontMetrics.fontDpi();
79
80 // Fall back to historical Qt behavior: hardocded 72 DPI on mac,
81 // primary screen DPI on other platforms.
82#ifdef Q_OS_DARWIN
83 return qstyleBaseDpi;
84#else
85 return qt_defaultDpiX();
86#endif
87}
88
89Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, qreal dpi)
90{
91 return value * dpi / qstyleBaseDpi;
92}
93
94Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QPaintDevice *device)
95{
97}
98
99Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QStyleOption *option)
100{
101 return dpiScaled(value, dpi(option));
102}
103
104#if QT_CONFIG(accessibility)
106{
107 bool match = false;
109 match = iface && iface->role() == role;
110 return match;
111}
112
113// Searches for an ancestor of a particular accessible role
115{
116 bool found = false;
117 QObject *parent = obj ? obj->parent() : nullptr;
118 while (parent && !found) {
120 found = true;
121 parent = parent->parent();
122 }
123 return found;
124}
125#endif // QT_CONFIG(accessibility)
126
127
128#if QT_CONFIG(dial)
129
130int calcBigLineSize(int radius)
131{
132 int bigLineSize = radius / 6;
133 if (bigLineSize < 4)
134 bigLineSize = 4;
135 if (bigLineSize > radius / 2)
136 bigLineSize = radius / 2;
137 return bigLineSize;
138}
139
141{
142 const int width = dial->rect.width();
143 const int height = dial->rect.height();
144 const int r = qMin(width, height) / 2;
146 qreal a = 0;
147 if (dial->maximum == dial->minimum)
148 a = Q_PI / 2;
149 else if (dial->dialWrapping)
150 a = Q_PI * 3 / 2 - (currentSliderPosition - dial->minimum) * 2 * Q_PI
151 / (dial->maximum - dial->minimum);
152 else
153 a = (Q_PI * 8 - (currentSliderPosition - dial->minimum) * 10 * Q_PI
154 / (dial->maximum - dial->minimum)) / 6;
155 qreal xc = width / 2.0;
156 qreal yc = height / 2.0;
158 qreal back = offset * len;
159 QPointF pos(QPointF(xc + back * qCos(a), yc - back * qSin(a)));
160 return pos + dial->rect.topLeft();
161}
162
163qreal angle(const QPointF &p1, const QPointF &p2)
164{
165 static const qreal rad_factor = 180 / Q_PI;
166 qreal _angle = 0;
167
168 if (p1.x() == p2.x()) {
169 if (p1.y() < p2.y())
170 _angle = 270;
171 else
172 _angle = 90;
173 } else {
174 qreal x1, x2, y1, y2;
175
176 if (p1.x() <= p2.x()) {
177 x1 = p1.x(); y1 = p1.y();
178 x2 = p2.x(); y2 = p2.y();
179 } else {
180 x2 = p1.x(); y2 = p1.y();
181 x1 = p2.x(); y1 = p2.y();
182 }
183
184 qreal m = -(y2 - y1) / (x2 - x1);
186
187 if (p1.x() < p2.x())
188 _angle = 180 - _angle;
189 else
190 _angle = -_angle;
191 }
192 return _angle;
193}
194
196{
198 int width = dial->rect.width();
199 int height = dial->rect.height();
200 qreal r = qMin(width, height) / 2;
201 int bigLineSize = calcBigLineSize(int(r));
202
203 qreal xc = width / 2 + 0.5;
204 qreal yc = height / 2 + 0.5;
205 const int ns = dial->tickInterval;
206 if (!ns) // Invalid values may be set by Qt Widgets Designer.
207 return poly;
208 int notches = (dial->maximum + ns - 1 - dial->minimum) / ns;
209 if (notches <= 0)
210 return poly;
211 if (dial->maximum < dial->minimum || dial->maximum - dial->minimum > 1000) {
212 int maximum = dial->minimum + 1000;
213 notches = (maximum + ns - 1 - dial->minimum) / ns;
214 }
215
216 poly.resize(2 + 2 * notches);
217 int smallLineSize = bigLineSize / 2;
218 for (int i = 0; i <= notches; ++i) {
219 qreal angle = dial->dialWrapping ? Q_PI * 3 / 2 - i * 2 * Q_PI / notches
220 : (Q_PI * 8 - i * 10 * Q_PI / notches) / 6;
221 qreal s = qSin(angle);
222 qreal c = qCos(angle);
223 if (i == 0 || (((ns * i) % (dial->pageStep ? dial->pageStep : 1)) == 0)) {
224 poly[2 * i] = QPointF(xc + (r - bigLineSize) * c,
225 yc - (r - bigLineSize) * s);
226 poly[2 * i + 1] = QPointF(xc + r * c, yc - r * s);
227 } else {
228 poly[2 * i] = QPointF(xc + (r - 1 - smallLineSize) * c,
229 yc - (r - 1 - smallLineSize) * s);
230 poly[2 * i + 1] = QPointF(xc + (r - 1) * c, yc -(r - 1) * s);
231 }
232 }
233 return poly.translated(dial->rect.topLeft());
234}
235
236// This will draw a nice and shiny QDial for us. We don't want
237// all the shinyness in QWindowsStyle, hence we place it here
238
240{
241 const QPalette pal = option->palette;
243 const int width = option->rect.width();
244 const int height = option->rect.height();
245 const bool enabled = option->state & QStyle::State_Enabled;
246 qreal r = qMin(width, height) / 2;
247 r -= r/50;
248 const qreal penSize = r/20.0;
249
250 painter->save();
252
253 // Draw notches
255 const bool inverted = pal.window().color().lightness() < pal.text().color().lightness()
256 && pal.light().color().lightness() > pal.dark().color().lightness();
257 const QColor notchColor = inverted ? pal.light().color().lighter(120)
258 : pal.dark().color().darker(120);
261 }
262
263 // setting color before QCachedPainter since
264 // otherwise it is not set when the image is in the cache
266 qMin(140, buttonColor .saturation()),
267 qMax(180, buttonColor.value()));
268
269 // Cache dial background
271 if (p.needsPainting()) {
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(1.0, penSize/2.0);
288 shadowGradient.setColorAt(qreal(0.91), QColor(0, 0, 0, 40));
291 p->setPen(Qt::NoPen);
295
296 // Main gradient
298 br.width()*1.3, br.center().x(),
299 br.center().y() - br.height()/2);
305 } else {
306 p->setBrush(Qt::NoBrush);
307 }
308
309 p->setPen(buttonColor.darker(280));
310 p->drawEllipse(br);
311 p->setBrush(Qt::NoBrush);
313 p->drawEllipse(br.adjusted(1, 1, -1, -1));
314
318 qMin(160, highlight.saturation()),
319 qMax(230, highlight.value()));
320 highlight.setAlpha(127);
321 p->setPen(QPen(highlight, 2.0));
322 p->setBrush(Qt::NoBrush);
323 p->drawEllipse(br.adjusted(-1, -1, 1, 1));
324 }
325 }
326 p.finish();
327
331 const qreal ds = r/qreal(7.0);
332 QRectF dialRect(dp.x() - ds, dp.y() - ds, 2*ds, 2*ds);
335 dialRect.width()*2,
336 dialRect.center().x(), dialRect.center().y());
340 if (penSize > 3.0) {
341 painter->setPen(QPen(QColor(0, 0, 0, 25), penSize));
343 }
344
346 painter->setPen(QColor(255, 255, 255, 150));
347 painter->drawEllipse(dialRect.adjusted(-1, -1, 1, 1));
348 painter->setPen(QColor(0, 0, 0, 80));
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(QRect(rect.left() + left, rect.top(), rect.width() -right - left, top), pixmap,
364 QRect(left, 0, size.width() -right - left, top));
365
366 //top-left
367 if (left > 0)
368 painter->drawPixmap(QRect(rect.left(), rect.top(), left, top), pixmap,
369 QRect(0, 0, left, top));
370
371 //top-right
372 if (right > 0)
373 painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top(), right, top), pixmap,
374 QRect(size.width() - right, 0, right, top));
375 }
376
377 //left
378 if (left > 0)
379 painter->drawPixmap(QRect(rect.left(), rect.top()+top, left, rect.height() - top - bottom), pixmap,
380 QRect(0, top, left, size.height() - bottom - top));
381
382 //center
383 painter->drawPixmap(QRect(rect.left() + left, rect.top()+top, rect.width() -right - left,
384 rect.height() - bottom - top), pixmap,
385 QRect(left, top, size.width() -right -left,
386 size.height() - bottom - top));
387 //right
388 if (right > 0)
389 painter->drawPixmap(QRect(rect.left() +rect.width() - right, rect.top()+top, right, rect.height() - top - bottom), pixmap,
390 QRect(size.width() - right, top, right, size.height() - bottom - top));
391
392 //bottom
393 if (bottom > 0) {
394 painter->drawPixmap(QRect(rect.left() +left, rect.top() + rect.height() - bottom,
395 rect.width() - right - left, bottom), pixmap,
396 QRect(left, size.height() - bottom,
397 size.width() - right - left, bottom));
398 //bottom-left
399 if (left > 0)
400 painter->drawPixmap(QRect(rect.left(), rect.top() + rect.height() - bottom, left, bottom), pixmap,
401 QRect(0, size.height() - bottom, left, bottom));
402
403 //bottom-right
404 if (right > 0)
405 painter->drawPixmap(QRect(rect.left() + rect.width() - right, rect.top() + rect.height() - bottom, right, bottom), pixmap,
406 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 *>(widget) && widget->parent() &&
415 qobject_cast<const QAbstractScrollArea *>(widget->parent()->parent()))
416 return widget->parentWidget()->parentWidget()->palette().color(QPalette::Base);
417#else
418 Q_UNUSED(widget);
419#endif
420 return pal.color(QPalette::Base);
421}
422
423WidgetSizePolicy widgetSizePolicy(const QWidget *widget, const QStyleOption *opt)
424{
425 while (widget) {
426 if (widget->testAttribute(Qt::WA_MacMiniSize)) {
427 return SizeMini;
428 } else if (widget->testAttribute(Qt::WA_MacSmallSize)) {
429 return SizeSmall;
430 } else if (widget->testAttribute(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
friend class QWidget
Definition qpainter.h:431
static bool usePixmapCache(const QStyleOption *opt)
static const qreal qstyleBaseDpi
Q_GUI_EXPORT int qt_defaultDpiX()
Definition qfont.cpp:113