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