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