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
qstylesheetstyle.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:critical reason:data-parser
4
5#include <qglobal.h>
7
8#if QT_CONFIG(style_stylesheet)
9
10#include "private/qcssutil_p.h"
11#include <qdebug.h>
12#include <qdir.h>
13#include <qapplication.h>
14#if QT_CONFIG(menu)
15#include <qmenu.h>
16#endif
17#if QT_CONFIG(menubar)
18#include <qmenubar.h>
19#endif
20#include <qpainter.h>
21#include <qstyleoption.h>
22#if QT_CONFIG(lineedit)
23#include <qlineedit.h>
24#endif
25#if QT_CONFIG(textedit)
26#include <qtextedit.h>
27#include <qplaintextedit.h>
28#endif
29#include <private/qwindowsstyle_p.h>
30#if QT_CONFIG(combobox)
31#include <qcombobox.h>
32#endif
33#include "private/qcssparser_p.h"
34#include "private/qmath_p.h"
35#include <qabstractscrollarea.h>
36#include "private/qabstractscrollarea_p.h"
37#if QT_CONFIG(tooltip)
38#include <qtooltip.h>
39#endif
40#include <qshareddata.h>
41#if QT_CONFIG(toolbutton)
42#include <qtoolbutton.h>
43#endif
44#if QT_CONFIG(scrollbar)
45#include <qscrollbar.h>
46#endif
47#if QT_CONFIG(abstractslider)
48#include <qabstractslider.h>
49#endif
50#include <qstring.h>
51#include <qfile.h>
52#if QT_CONFIG(checkbox)
53#include <qcheckbox.h>
54#endif
55#if QT_CONFIG(itemviews)
56#include <qheaderview.h>
57#endif
58#include <private/qwindowsstyle_p_p.h>
59#if QT_CONFIG(animation)
60#include <private/qstyleanimation_p.h>
61#endif
62#if QT_CONFIG(tabbar)
63#include <private/qtabbar_p.h>
64#endif
65#include <QMetaProperty>
66#if QT_CONFIG(mainwindow)
67#include <qmainwindow.h>
68#endif
69#if QT_CONFIG(dockwidget)
70#include <qdockwidget.h>
71#endif
72#if QT_CONFIG(mdiarea)
73#include <qmdisubwindow.h>
74#endif
75#if QT_CONFIG(dialog)
76#include <qdialog.h>
77#endif
78#include <private/qwidget_p.h>
79#if QT_CONFIG(spinbox)
80#include <QAbstractSpinBox>
81#endif
82#if QT_CONFIG(label)
83#include <QLabel>
84#endif
85#include "qdrawutil.h"
86#include "qstylehelper_p.h"
87
88#include <limits.h>
89#if QT_CONFIG(toolbar)
90#include <QtWidgets/qtoolbar.h>
91#endif
92#if QT_CONFIG(pushbutton)
93#include <QtWidgets/qpushbutton.h>
94#endif
95
96#include <QtGui/qpainterpath.h>
97#include <QtGui/qscreen.h>
98
99#include <QtCore/private/qduplicatetracker_p.h>
100
101QT_BEGIN_NAMESPACE
102
103using namespace Qt::StringLiterals;
104
105using namespace QCss;
106
107
108class QStyleSheetStylePrivate : public QWindowsStylePrivate
109{
110 Q_DECLARE_PUBLIC(QStyleSheetStyle)
111public:
112 QStyleSheetStylePrivate() { }
113};
114
115class QStyleSheetProxySaver
116{
117public:
118 QStyleSheetProxySaver(const QStyleSheetStyle *that)
119 : m_that(const_cast<QStyleSheetStyle *>(that))
120 , m_oldProxy(const_cast<QStyle *>(that->baseStyle()->proxy()))
121 {
122 m_that->baseStyle()->setProxy(m_that);
123 }
124 ~QStyleSheetProxySaver()
125 {
126 m_that->baseStyle()->setProxy(m_oldProxy);
127 }
128
129private:
130 QStyleSheetStyle *m_that;
131 QStyle *m_oldProxy;
132};
133
134
135static QStyleSheetStyleCaches *styleSheetCaches = nullptr;
136
137/* RECURSION_GUARD:
138 * the QStyleSheetStyle is a proxy. If used with others proxy style, we may end up with something like:
139 * QStyleSheetStyle -> ProxyStyle -> QStyleSheetStyle -> OriginalStyle
140 * Recursion may happen if the style call the widget()->style() again.
141 * Not to mention the performance penalty of having two lookup of rules.
142 *
143 * The first instance of QStyleSheetStyle will set globalStyleSheetStyle to itself. The second one
144 * will notice the globalStyleSheetStyle is not istelf and call its base style directly.
145 */
146static const QStyleSheetStyle *globalStyleSheetStyle = nullptr;
147class QStyleSheetStyleRecursionGuard
148{
149 public:
150 QStyleSheetStyleRecursionGuard(const QStyleSheetStyle *that)
151 : guarded(globalStyleSheetStyle == nullptr)
152 {
153 if (guarded) globalStyleSheetStyle = that;
154 }
155 ~QStyleSheetStyleRecursionGuard() { if (guarded) globalStyleSheetStyle = nullptr; }
156 bool guarded;
157};
158#define RECURSION_GUARD(RETURN)
159 if (globalStyleSheetStyle != 0 && globalStyleSheetStyle != this) { RETURN; }
160 QStyleSheetStyleRecursionGuard recursion_guard(this);
161
162enum PseudoElement {
163 PseudoElement_None,
164 PseudoElement_DownArrow,
165 PseudoElement_UpArrow,
166 PseudoElement_LeftArrow,
167 PseudoElement_RightArrow,
168 PseudoElement_Indicator,
169 PseudoElement_ExclusiveIndicator,
170 PseudoElement_PushButtonMenuIndicator,
171 PseudoElement_ComboBoxDropDown,
172 PseudoElement_ComboBoxArrow,
173 PseudoElement_Item,
174 PseudoElement_SpinBoxUpButton,
175 PseudoElement_SpinBoxUpArrow,
176 PseudoElement_SpinBoxDownButton,
177 PseudoElement_SpinBoxDownArrow,
178 PseudoElement_GroupBoxTitle,
179 PseudoElement_GroupBoxIndicator,
180 PseudoElement_ToolButtonMenu,
181 PseudoElement_ToolButtonMenuArrow,
182 PseudoElement_ToolButtonMenuIndicator,
183 PseudoElement_ToolBoxTab,
184 PseudoElement_ScrollBarSlider,
185 PseudoElement_ScrollBarAddPage,
186 PseudoElement_ScrollBarSubPage,
187 PseudoElement_ScrollBarAddLine,
188 PseudoElement_ScrollBarSubLine,
189 PseudoElement_ScrollBarFirst,
190 PseudoElement_ScrollBarLast,
191 PseudoElement_ScrollBarUpArrow,
192 PseudoElement_ScrollBarDownArrow,
193 PseudoElement_ScrollBarLeftArrow,
194 PseudoElement_ScrollBarRightArrow,
195 PseudoElement_SplitterHandle,
196 PseudoElement_ToolBarHandle,
197 PseudoElement_ToolBarSeparator,
198 PseudoElement_MenuScroller,
199 PseudoElement_MenuTearoff,
200 PseudoElement_MenuCheckMark,
201 PseudoElement_MenuSeparator,
202 PseudoElement_MenuIcon,
203 PseudoElement_MenuRightArrow,
204 PseudoElement_TreeViewBranch,
205 PseudoElement_HeaderViewSection,
206 PseudoElement_HeaderViewUpArrow,
207 PseudoElement_HeaderViewDownArrow,
208 PseudoElement_ProgressBarChunk,
209 PseudoElement_TabBarTab,
210 PseudoElement_TabBarScroller,
211 PseudoElement_TabBarTear,
212 PseudoElement_SliderGroove,
213 PseudoElement_SliderHandle,
214 PseudoElement_SliderAddPage,
215 PseudoElement_SliderSubPage,
216 PseudoElement_SliderTickmark,
217 PseudoElement_TabWidgetPane,
218 PseudoElement_TabWidgetTabBar,
219 PseudoElement_TabWidgetLeftCorner,
220 PseudoElement_TabWidgetRightCorner,
221 PseudoElement_DockWidgetTitle,
222 PseudoElement_DockWidgetCloseButton,
223 PseudoElement_DockWidgetFloatButton,
224 PseudoElement_DockWidgetSeparator,
225 PseudoElement_MdiCloseButton,
226 PseudoElement_MdiMinButton,
227 PseudoElement_MdiNormalButton,
228 PseudoElement_TitleBar,
229 PseudoElement_TitleBarCloseButton,
230 PseudoElement_TitleBarMinButton,
231 PseudoElement_TitleBarMaxButton,
232 PseudoElement_TitleBarShadeButton,
233 PseudoElement_TitleBarUnshadeButton,
234 PseudoElement_TitleBarNormalButton,
235 PseudoElement_TitleBarContextHelpButton,
236 PseudoElement_TitleBarSysMenu,
237 PseudoElement_ViewItem,
238 PseudoElement_ViewItemIcon,
239 PseudoElement_ViewItemText,
240 PseudoElement_ViewItemIndicator,
241 PseudoElement_ScrollAreaCorner,
242 PseudoElement_TabBarTabCloseButton,
243 NumPseudoElements
244};
245
246struct PseudoElementInfo {
247 QStyle::SubControl subControl;
248 const char name[19];
249};
250
251static constexpr PseudoElementInfo knownPseudoElements[NumPseudoElements] = {
252 { QStyle::SC_None, "" },
253 { QStyle::SC_None, "down-arrow" },
254 { QStyle::SC_None, "up-arrow" },
255 { QStyle::SC_None, "left-arrow" },
256 { QStyle::SC_None, "right-arrow" },
257 { QStyle::SC_None, "indicator" },
258 { QStyle::SC_None, "indicator" },
259 { QStyle::SC_None, "menu-indicator" },
260 { QStyle::SC_ComboBoxArrow, "drop-down" },
261 { QStyle::SC_ComboBoxArrow, "down-arrow" },
262 { QStyle::SC_None, "item" },
263 { QStyle::SC_SpinBoxUp, "up-button" },
264 { QStyle::SC_SpinBoxUp, "up-arrow" },
265 { QStyle::SC_SpinBoxDown, "down-button" },
266 { QStyle::SC_SpinBoxDown, "down-arrow" },
267 { QStyle::SC_GroupBoxLabel, "title" },
268 { QStyle::SC_GroupBoxCheckBox, "indicator" },
269 { QStyle::SC_ToolButtonMenu, "menu-button" },
270 { QStyle::SC_ToolButtonMenu, "menu-arrow" },
271 { QStyle::SC_None, "menu-indicator" },
272 { QStyle::SC_None, "tab" },
273 { QStyle::SC_ScrollBarSlider, "handle" },
274 { QStyle::SC_ScrollBarAddPage, "add-page" },
275 { QStyle::SC_ScrollBarSubPage, "sub-page" },
276 { QStyle::SC_ScrollBarAddLine, "add-line" },
277 { QStyle::SC_ScrollBarSubLine, "sub-line" },
278 { QStyle::SC_ScrollBarFirst, "first" },
279 { QStyle::SC_ScrollBarLast, "last" },
280 { QStyle::SC_ScrollBarSubLine, "up-arrow" },
281 { QStyle::SC_ScrollBarAddLine, "down-arrow" },
282 { QStyle::SC_ScrollBarSubLine, "left-arrow" },
283 { QStyle::SC_ScrollBarAddLine, "right-arrow" },
284 { QStyle::SC_None, "handle" },
285 { QStyle::SC_None, "handle" },
286 { QStyle::SC_None, "separator" },
287 { QStyle::SC_None, "scroller" },
288 { QStyle::SC_None, "tearoff" },
289 { QStyle::SC_None, "indicator" },
290 { QStyle::SC_None, "separator" },
291 { QStyle::SC_None, "icon" },
292 { QStyle::SC_None, "right-arrow" },
293 { QStyle::SC_None, "branch" },
294 { QStyle::SC_None, "section" },
295 { QStyle::SC_None, "down-arrow" },
296 { QStyle::SC_None, "up-arrow" },
297 { QStyle::SC_None, "chunk" },
298 { QStyle::SC_None, "tab" },
299 { QStyle::SC_None, "scroller" },
300 { QStyle::SC_None, "tear" },
301 { QStyle::SC_SliderGroove, "groove" },
302 { QStyle::SC_SliderHandle, "handle" },
303 { QStyle::SC_None, "add-page" },
304 { QStyle::SC_None, "sub-page" },
305 { QStyle::SC_SliderTickmarks, "tick-mark" },
306 { QStyle::SC_None, "pane" },
307 { QStyle::SC_None, "tab-bar" },
308 { QStyle::SC_None, "left-corner" },
309 { QStyle::SC_None, "right-corner" },
310 { QStyle::SC_None, "title" },
311 { QStyle::SC_None, "close-button" },
312 { QStyle::SC_None, "float-button" },
313 { QStyle::SC_None, "separator" },
314 { QStyle::SC_MdiCloseButton, "close-button" },
315 { QStyle::SC_MdiMinButton, "minimize-button" },
316 { QStyle::SC_MdiNormalButton, "normal-button" },
317 { QStyle::SC_TitleBarLabel, "title" },
318 { QStyle::SC_TitleBarCloseButton, "close-button" },
319 { QStyle::SC_TitleBarMinButton, "minimize-button" },
320 { QStyle::SC_TitleBarMaxButton, "maximize-button" },
321 { QStyle::SC_TitleBarShadeButton, "shade-button" },
322 { QStyle::SC_TitleBarUnshadeButton, "unshade-button" },
323 { QStyle::SC_TitleBarNormalButton, "normal-button" },
324 { QStyle::SC_TitleBarContextHelpButton, "contexthelp-button" },
325 { QStyle::SC_TitleBarSysMenu, "sys-menu" },
326 { QStyle::SC_None, "item" },
327 { QStyle::SC_None, "icon" },
328 { QStyle::SC_None, "text" },
329 { QStyle::SC_None, "indicator" },
330 { QStyle::SC_None, "corner" },
331 { QStyle::SC_None, "close-button" },
332};
333
334
335struct QStyleSheetBorderImageData : public QSharedData
336{
337 std::array<int, 4> cuts = {-1, -1, -1, -1};
338 QCss::TileMode horizStretch = QCss::TileMode_Unknown;
339 QCss::TileMode vertStretch = QCss::TileMode_Unknown;
340 QPixmap pixmap;
341};
342
343struct QStyleSheetBackgroundData : public QSharedData
344{
345 QStyleSheetBackgroundData(const QBrush& b, const QPixmap& p, QCss::Repeat r,
346 Qt::Alignment a, QCss::Origin o, Attachment t, QCss::Origin c)
347 : brush(b), pixmap(p), repeat(r), position(a), origin(o), attachment(t), clip(c) { }
348
349 bool isTransparent() const {
350 if (brush.style() != Qt::NoBrush)
351 return !brush.isOpaque();
352 return pixmap.isNull() ? false : pixmap.hasAlpha();
353 }
354 QBrush brush;
355 QPixmap pixmap;
356 QCss::Repeat repeat;
357 Qt::Alignment position;
358 QCss::Origin origin;
359 QCss::Attachment attachment;
360 QCss::Origin clip;
361};
362
363struct QStyleSheetBorderData : public QSharedData
364{
365 QStyleSheetBorderData() = default;
366
367 std::array<int, 4> borders = {0, 0, 0, 0};
368 std::array<QBrush, 4> colors;
369 std::array<QCss::BorderStyle, 4> styles = {QCss::BorderStyle_None,
370 QCss::BorderStyle_None,
371 QCss::BorderStyle_None,
372 QCss::BorderStyle_None};
373 std::array<QSize, 4> radii; // topleft, topright, bottomleft, bottomright
374 QSharedDataPointer<QStyleSheetBorderImageData> bi;
375
376 const QStyleSheetBorderImageData *borderImage() const { return bi; }
377 bool hasBorderImage() const { return bi!=nullptr; }
378 bool isOpaque() const
379 {
380 for (size_t i = 0; i < 4; i++) {
381 if (styles[i] == QCss::BorderStyle_Native || styles[i] == QCss::BorderStyle_None)
382 continue;
383 if (styles[i] >= QCss::BorderStyle_Dotted && styles[i] <= QCss::BorderStyle_DotDotDash
384 && styles[i] != BorderStyle_Solid)
385 return false;
386 if (!colors[i].isOpaque())
387 return false;
388 if (!radii[i].isEmpty())
389 return false;
390 }
391 if (bi != nullptr && bi->pixmap.hasAlpha())
392 return false;
393 return true;
394 }
395};
396
397
398struct QStyleSheetOutlineData : public QStyleSheetBorderData
399{
400 QStyleSheetOutlineData() = default;
401
402 std::array<int, 4> offsets = {0, 0, 0, 0};
403};
404
405struct QStyleSheetBoxData : public QSharedData
406{
407 std::array<int, 4> margins = {0, 0, 0, 0};
408 std::array<int, 4> paddings = {0, 0, 0, 0};
409 int spacing = -1;
410};
411
412struct QStyleSheetPaletteData : public QSharedData
413{
414 QStyleSheetPaletteData(const QBrush &foreground,
415 const QBrush &selectedForeground,
416 const QBrush &selectedBackground,
417 const QBrush &alternateBackground,
418 const QBrush &placeHolderTextForeground,
419 const QBrush &accent)
420 : foreground(foreground)
421 , selectionForeground(selectedForeground)
422 , selectionBackground(selectedBackground)
423 , alternateBackground(alternateBackground)
424 , placeholderForeground(placeHolderTextForeground)
425 , accent(accent)
426 { }
427
428 QBrush foreground;
429 QBrush selectionForeground;
430 QBrush selectionBackground;
431 QBrush alternateBackground;
432 QBrush placeholderForeground;
433 QBrush accent;
434};
435
436struct QStyleSheetGeometryData : public QSharedData
437{
438 QStyleSheetGeometryData(int w, int h, int minw, int minh, int maxw, int maxh)
439 : minWidth(minw), minHeight(minh), width(w), height(h), maxWidth(maxw), maxHeight(maxh) { }
440
441 int minWidth, minHeight, width, height, maxWidth, maxHeight;
442};
443
444struct QStyleSheetPositionData : public QSharedData
445{
446 QStyleSheetPositionData(int l, int t, int r, int b, Origin o, Qt::Alignment p, QCss::PositionMode m, Qt::Alignment a = { })
447 : left(l), top(t), bottom(b), right(r), origin(o), position(p), mode(m), textAlignment(a) { }
448
449 int left, top, bottom, right;
450 Origin origin;
451 Qt::Alignment position;
452 QCss::PositionMode mode;
453 Qt::Alignment textAlignment;
454};
455
456struct QStyleSheetImageData : public QSharedData
457{
458 QStyleSheetImageData(const QIcon &i, Qt::Alignment a, const QSize &sz)
459 : icon(i), alignment(a), size(sz) { }
460
461 QIcon icon;
462 Qt::Alignment alignment;
463 QSize size;
464};
465
466class QRenderRule
467{
468public:
469 QRenderRule() : features(0), hasFont(false), pal(nullptr), b(nullptr), bg(nullptr), bd(nullptr), ou(nullptr), geo(nullptr), p(nullptr), img(nullptr), clipset(0) { }
470 QRenderRule(const QList<QCss::Declaration> &, const QObject *);
471
472 QRect borderRect(const QRect &r) const;
473 QRect outlineRect(const QRect &r) const;
474 QRect paddingRect(const QRect &r) const;
475 QRect contentsRect(const QRect &r) const;
476
477 enum { Margin = 1, Border = 2, Padding = 4, All=Margin|Border|Padding };
478 QRect boxRect(const QRect &r, int flags = All) const;
479 QSize boxSize(const QSize &s, int flags = All) const;
480 QRect originRect(const QRect &rect, Origin origin) const;
481
482 QPainterPath borderClip(QRect rect);
483 void drawBorder(QPainter *, const QRect&);
484 void drawOutline(QPainter *, const QRect&);
485 void drawBorderImage(QPainter *, const QRect&);
486 void drawBackground(QPainter *, const QRect&, const QPoint& = QPoint(0, 0));
487 void drawBackgroundImage(QPainter *, const QRect&, QPoint = QPoint(0, 0));
488 void drawFrame(QPainter *, const QRect&);
489 void drawImage(QPainter *p, const QRect &rect);
490 void drawRule(QPainter *, const QRect&);
491 void configurePalette(QPalette *, QPalette::ColorGroup, const QWidget *, bool);
492 void configurePalette(QPalette *p, QPalette::ColorRole fr, QPalette::ColorRole br);
493
494 const QStyleSheetPaletteData *palette() const { return pal; }
495 const QStyleSheetBoxData *box() const { return b; }
496 const QStyleSheetBackgroundData *background() const { return bg; }
497 const QStyleSheetBorderData *border() const { return bd; }
498 const QStyleSheetOutlineData *outline() const { return ou; }
499 const QStyleSheetGeometryData *geometry() const { return geo; }
500 const QStyleSheetPositionData *position() const { return p; }
501 const QStyleSheetImageData *icon() const { return iconPtr; }
502
503 bool hasModification() const;
504
505 bool hasPalette() const { return pal != nullptr; }
506 bool hasBackground() const { return bg != nullptr && (!bg->pixmap.isNull() || bg->brush.style() != Qt::NoBrush); }
507 bool hasGradientBackground() const { return bg && bg->brush.style() >= Qt::LinearGradientPattern
508 && bg->brush.style() <= Qt::ConicalGradientPattern; }
509
510 bool hasNativeBorder() const {
511 return bd == nullptr
512 || (!bd->hasBorderImage() && bd->styles[0] == BorderStyle_Native);
513 }
514
515 bool hasNativeOutline() const {
516 return (ou == nullptr
517 || (!ou->hasBorderImage() && ou->styles[0] == BorderStyle_Native));
518 }
519
520 bool baseStyleCanDraw() const {
521 if (!hasBackground() || (background()->brush.style() == Qt::NoBrush && bg->pixmap.isNull()))
522 return true;
523 if (bg && !bg->pixmap.isNull())
524 return false;
525 if (hasGradientBackground())
526 return features & StyleFeature_BackgroundGradient;
527 return features & StyleFeature_BackgroundColor;
528 }
529
530 bool hasBox() const { return b != nullptr; }
531 bool hasBorder() const { return bd != nullptr; }
532 bool hasOutline() const { return ou != nullptr; }
533 bool hasPosition() const { return p != nullptr; }
534 bool hasGeometry() const { return geo != nullptr; }
535 bool hasDrawable() const { return !hasNativeBorder() || hasBackground() || hasImage(); }
536 bool hasImage() const { return img != nullptr; }
537 bool hasIcon() const { return iconPtr != nullptr; }
538
539 QSize minimumContentsSize() const
540 { return geo ? QSize(geo->minWidth, geo->minHeight) : QSize(0, 0); }
541 QSize minimumSize() const
542 { return boxSize(minimumContentsSize()); }
543
544 QSize contentsSize() const
545 { return geo ? QSize(geo->width, geo->height)
546 : ((img && img->size.isValid()) ? img->size : QSize()); }
547 QSize contentsSize(const QSize &sz) const
548 {
549 QSize csz = contentsSize();
550 if (csz.width() == -1) csz.setWidth(sz.width());
551 if (csz.height() == -1) csz.setHeight(sz.height());
552 return csz;
553 }
554 bool hasContentsSize() const
555 { return (geo && (geo->width != -1 || geo->height != -1)) || (img && img->size.isValid()); }
556
557 QSize size() const { return boxSize(contentsSize()); }
558 QSize size(const QSize &sz) const { return boxSize(contentsSize(sz)); }
559 QSize adjustSize(const QSize &sz)
560 {
561 if (!geo)
562 return sz;
563 QSize csz = contentsSize();
564 if (csz.width() == -1) csz.setWidth(sz.width());
565 if (csz.height() == -1) csz.setHeight(sz.height());
566 if (geo->maxWidth != -1 && csz.width() > geo->maxWidth) csz.setWidth(geo->maxWidth);
567 if (geo->maxHeight != -1 && csz.height() > geo->maxHeight) csz.setHeight(geo->maxHeight);
568 csz=csz.expandedTo(QSize(geo->minWidth, geo->minHeight));
569 return csz;
570 }
571
572 QVariant styleHint(QLatin1StringView sh) const { return styleHints.value(sh); }
573
574 void fixupBorder(int);
575
576 // Shouldn't be here
577 void setClip(QPainter *p, const QRect &rect);
578 void unsetClip(QPainter *);
579
580public:
581 int features;
582 QBrush defaultBackground;
583 QFont font; // Be careful using this font directly. Prefer using font.resolve( )
584 bool hasFont;
585
586 QHash<QString, QVariant> styleHints;
587
588 QSharedDataPointer<QStyleSheetPaletteData> pal;
589 QSharedDataPointer<QStyleSheetBoxData> b;
590 QSharedDataPointer<QStyleSheetBackgroundData> bg;
591 QSharedDataPointer<QStyleSheetBorderData> bd;
592 QSharedDataPointer<QStyleSheetOutlineData> ou;
593 QSharedDataPointer<QStyleSheetGeometryData> geo;
594 QSharedDataPointer<QStyleSheetPositionData> p;
595 QSharedDataPointer<QStyleSheetImageData> img;
596 QSharedDataPointer<QStyleSheetImageData> iconPtr;
597
598 int clipset;
599 QPainterPath clipPath;
600};
601Q_DECLARE_TYPEINFO(QRenderRule, Q_RELOCATABLE_TYPE);
602
603///////////////////////////////////////////////////////////////////////////////////////////
604static constexpr std::array<const char*, 90> knownStyleHints = {
605 "activate-on-singleclick",
606 "alignment",
607 "arrow-keys-navigate-into-children",
608 "backward-icon",
609 "button-layout",
610 "cd-icon",
611 "combobox-list-mousetracking",
612 "combobox-popup",
613 "computer-icon",
614 "desktop-icon",
615 "dialog-apply-icon",
616 "dialog-cancel-icon",
617 "dialog-close-icon",
618 "dialog-discard-icon",
619 "dialog-help-icon",
620 "dialog-no-icon",
621 "dialog-ok-icon",
622 "dialog-open-icon",
623 "dialog-reset-icon",
624 "dialog-save-icon",
625 "dialog-yes-icon",
626 "dialogbuttonbox-buttons-have-icons",
627 "directory-closed-icon",
628 "directory-icon",
629 "directory-link-icon",
630 "directory-open-icon",
631 "dither-disable-text",
632 "dockwidget-close-icon",
633 "downarrow-icon",
634 "dvd-icon",
635 "etch-disabled-text",
636 "file-icon",
637 "file-link-icon",
638 "filedialog-backward-icon", // unused
639 "filedialog-contentsview-icon",
640 "filedialog-detailedview-icon",
641 "filedialog-end-icon",
642 "filedialog-infoview-icon",
643 "filedialog-listview-icon",
644 "filedialog-new-directory-icon",
645 "filedialog-parent-directory-icon",
646 "filedialog-start-icon",
647 "floppy-icon",
648 "forward-icon",
649 "gridline-color",
650 "harddisk-icon",
651 "home-icon",
652 "lineedit-clear-button-icon",
653 "icon-size",
654 "leftarrow-icon",
655 "lineedit-password-character",
656 "lineedit-password-mask-delay",
657 "mdi-fill-space-on-maximize",
658 "menu-scrollable",
659 "menubar-altkey-navigation",
660 "menubar-separator",
661 "messagebox-critical-icon",
662 "messagebox-information-icon",
663 "messagebox-question-icon",
664 "messagebox-text-interaction-flags",
665 "messagebox-warning-icon",
666 "mouse-tracking",
667 "network-icon",
668 "opacity",
669 "paint-alternating-row-colors-for-empty-area",
670 "rightarrow-icon",
671 "scrollbar-contextmenu",
672 "scrollbar-leftclick-absolute-position",
673 "scrollbar-middleclick-absolute-position",
674 "scrollbar-roll-between-buttons",
675 "scrollbar-scroll-when-pointer-leaves-control",
676 "scrollview-frame-around-contents",
677 "show-decoration-selected",
678 "spinbox-click-autorepeat-rate",
679 "spincontrol-disable-on-bounds",
680 "tabbar-elide-mode",
681 "tabbar-prefer-no-arrows",
682 "titlebar-close-icon",
683 "titlebar-contexthelp-icon",
684 "titlebar-maximize-icon",
685 "titlebar-menu-icon",
686 "titlebar-minimize-icon",
687 "titlebar-normal-icon",
688 "titlebar-shade-icon",
689 "titlebar-show-tooltips-on-buttons",
690 "titlebar-unshade-icon",
691 "toolbutton-popup-delay",
692 "trash-icon",
693 "uparrow-icon",
694 "widget-animation-duration"
695};
696
697static QList<QVariant> subControlLayout(QByteArrayView layout)
698{
699 QList<QVariant> buttons;
700 for (int button : layout) {
701 switch (button) {
702 case 'm':
703 buttons.append(PseudoElement_MdiMinButton);
704 buttons.append(PseudoElement_TitleBarMinButton);
705 break;
706 case 'M':
707 buttons.append(PseudoElement_TitleBarMaxButton);
708 break;
709 case 'X':
710 buttons.append(PseudoElement_MdiCloseButton);
711 buttons.append(PseudoElement_TitleBarCloseButton);
712 break;
713 case 'N':
714 buttons.append(PseudoElement_MdiNormalButton);
715 buttons.append(PseudoElement_TitleBarNormalButton);
716 break;
717 case 'I':
718 buttons.append(PseudoElement_TitleBarSysMenu);
719 break;
720 case 'T':
721 buttons.append(PseudoElement_TitleBar);
722 break;
723 case 'H':
724 buttons.append(PseudoElement_TitleBarContextHelpButton);
725 break;
726 case 'S':
727 buttons.append(PseudoElement_TitleBarShadeButton);
728 break;
729 default:
730 buttons.append(button);
731 break;
732 }
733 }
734 return buttons;
735}
736
737namespace {
738 struct ButtonInfo {
739 QRenderRule rule;
740 int element;
741 int offset;
742 int where;
743 int width;
744 };
745}
746template <> class QTypeInfo<ButtonInfo> : public QTypeInfoMerger<ButtonInfo, QRenderRule, int> {};
747
748QHash<QStyle::SubControl, QRect> QStyleSheetStyle::titleBarLayout(const QWidget *w, const QStyleOptionTitleBar *tb) const
749{
750 QHash<QStyle::SubControl, QRect> layoutRects;
751 const bool isMinimized = tb->titleBarState & Qt::WindowMinimized;
752 const bool isMaximized = tb->titleBarState & Qt::WindowMaximized;
753 QRenderRule subRule = renderRule(w, tb);
754 QRect cr = subRule.contentsRect(tb->rect);
755 QList<QVariant> layout = subRule.styleHint("button-layout"_L1).toList();
756 if (layout.isEmpty())
757 layout = subControlLayout("I(T)HSmMX"_L1);
758
759 int offsets[3] = { 0, 0, 0 };
760 enum Where { Left, Right, Center, NoWhere } where = Left;
761 QList<ButtonInfo> infos;
762 infos.reserve(layout.size());
763 for (const QVariant &val : std::as_const(layout)) {
764 const int element = val.toInt();
765 if (element == '(') {
766 where = Center;
767 } else if (element == ')') {
768 where = Right;
769 } else {
770 ButtonInfo info;
771 info.element = element;
772 switch (element) {
773 case PseudoElement_TitleBar:
774 if (!(tb->titleBarFlags & (Qt::WindowTitleHint | Qt::WindowSystemMenuHint)))
775 continue;
776 break;
777 case PseudoElement_TitleBarContextHelpButton:
778 if (!(tb->titleBarFlags & Qt::WindowContextHelpButtonHint))
779 continue;
780 break;
781 case PseudoElement_TitleBarMinButton:
782 if (!(tb->titleBarFlags & Qt::WindowMinimizeButtonHint))
783 continue;
784 if (isMinimized)
785 info.element = PseudoElement_TitleBarNormalButton;
786 break;
787 case PseudoElement_TitleBarMaxButton:
788 if (!(tb->titleBarFlags & Qt::WindowMaximizeButtonHint))
789 continue;
790 if (isMaximized)
791 info.element = PseudoElement_TitleBarNormalButton;
792 break;
793 case PseudoElement_TitleBarShadeButton:
794 if (!(tb->titleBarFlags & Qt::WindowShadeButtonHint))
795 continue;
796 if (isMinimized)
797 info.element = PseudoElement_TitleBarUnshadeButton;
798 break;
799 case PseudoElement_TitleBarCloseButton:
800 case PseudoElement_TitleBarSysMenu:
801 if (!(tb->titleBarFlags & Qt::WindowSystemMenuHint))
802 continue;
803 break;
804 default:
805 continue;
806 }
807 if (info.element == PseudoElement_TitleBar) {
808 info.width = tb->fontMetrics.horizontalAdvance(tb->text) + 6;
809 subRule.geo = new QStyleSheetGeometryData(info.width, tb->fontMetrics.height(), -1, -1, -1, -1);
810 } else {
811 subRule = renderRule(w, tb, info.element);
812 info.width = subRule.size().width();
813 }
814 info.rule = subRule;
815 info.offset = offsets[where];
816 info.where = where;
817 offsets[where] += info.width;
818
819 infos.append(std::move(info));
820 }
821 }
822
823 for (const ButtonInfo &info : std::as_const(infos)) {
824 QRect lr = cr;
825 switch (info.where) {
826 case Center: {
827 lr.setLeft(cr.left() + offsets[Left]);
828 lr.setRight(cr.right() - offsets[Right]);
829 QRect r(0, 0, offsets[Center], lr.height());
830 r.moveCenter(lr.center());
831 r.setLeft(r.left()+info.offset);
832 r.setWidth(info.width);
833 lr = r;
834 break; }
835 case Left:
836 lr.translate(info.offset, 0);
837 lr.setWidth(info.width);
838 break;
839 case Right:
840 lr.moveLeft(cr.right() + 1 - offsets[Right] + info.offset);
841 lr.setWidth(info.width);
842 break;
843 default:
844 break;
845 }
846 QStyle::SubControl control = knownPseudoElements[info.element].subControl;
847 layoutRects[control] = positionRect(w, info.rule, info.element, lr, tb->direction);
848 }
849
850 return layoutRects;
851}
852
853static QStyle::StandardPixmap subControlIcon(int pe)
854{
855 switch (pe) {
856 case PseudoElement_MdiCloseButton: return QStyle::SP_TitleBarCloseButton;
857 case PseudoElement_MdiMinButton: return QStyle::SP_TitleBarMinButton;
858 case PseudoElement_MdiNormalButton: return QStyle::SP_TitleBarNormalButton;
859 case PseudoElement_TitleBarCloseButton: return QStyle::SP_TitleBarCloseButton;
860 case PseudoElement_TitleBarMinButton: return QStyle::SP_TitleBarMinButton;
861 case PseudoElement_TitleBarMaxButton: return QStyle::SP_TitleBarMaxButton;
862 case PseudoElement_TitleBarShadeButton: return QStyle::SP_TitleBarShadeButton;
863 case PseudoElement_TitleBarUnshadeButton: return QStyle::SP_TitleBarUnshadeButton;
864 case PseudoElement_TitleBarNormalButton: return QStyle::SP_TitleBarNormalButton;
865 case PseudoElement_TitleBarContextHelpButton: return QStyle::SP_TitleBarContextHelpButton;
866 default: break;
867 }
868 return QStyle::SP_CustomBase;
869}
870
871QRenderRule::QRenderRule(const QList<Declaration> &declarations, const QObject *object)
872 : features(0),
873 hasFont(false),
874 pal(nullptr),
875 b(nullptr),
876 bg(nullptr),
877 bd(nullptr),
878 ou(nullptr),
879 geo(nullptr),
880 p(nullptr),
881 img(nullptr),
882 clipset(0)
883{
884 QPalette palette = QGuiApplication::palette(); // ###: ideally widget's palette
885 ValueExtractor v(declarations, palette);
886 features = v.extractStyleFeatures();
887
888 int w = -1, h = -1, minw = -1, minh = -1, maxw = -1, maxh = -1;
889 if (v.extractGeometry(&w, &h, &minw, &minh, &maxw, &maxh))
890 geo = new QStyleSheetGeometryData(w, h, minw, minh, maxw, maxh);
891
892 int left = 0, top = 0, right = 0, bottom = 0;
893 Origin origin = Origin_Unknown;
894 Qt::Alignment position;
895 QCss::PositionMode mode = PositionMode_Unknown;
896 Qt::Alignment textAlignment;
897 if (v.extractPosition(&left, &top, &right, &bottom, &origin, &position, &mode, &textAlignment))
898 p = new QStyleSheetPositionData(left, top, right, bottom, origin, position, mode, textAlignment);
899
900 b = new QStyleSheetBoxData;
901 if (!v.extractBox(b->margins.data(), b->paddings.data(), &b->spacing))
902 b = nullptr;
903
904 bd = new QStyleSheetBorderData;
905 if (!v.extractBorder(bd->borders.data(), bd->colors.data(), bd->styles.data(), bd->radii.data()))
906 bd = nullptr;
907
908 ou = new QStyleSheetOutlineData;
909 if (!v.extractOutline(ou->borders.data(), ou->colors.data(), ou->styles.data(), ou->radii.data(), ou->offsets.data()))
910 ou = nullptr;
911
912 QBrush brush;
913 QString uri;
914 Repeat repeat = Repeat_XY;
915 Qt::Alignment alignment = Qt::AlignTop | Qt::AlignLeft;
916 Attachment attachment = Attachment_Scroll;
917 origin = Origin_Padding;
918 Origin clip = Origin_Border;
919 if (v.extractBackground(&brush, &uri, &repeat, &alignment, &origin, &attachment, &clip)) {
920 QPixmap pixmap = QStyleSheetStyle::loadPixmap(uri, object);
921 if (!uri.isEmpty() && pixmap.isNull())
922 qWarning("Could not create pixmap from %s", qPrintable(QDir::toNativeSeparators(uri)));
923 bg = new QStyleSheetBackgroundData(brush, pixmap, repeat, alignment, origin, attachment, clip);
924 }
925
926 QBrush foreground;
927 QBrush selectedForeground;
928 QBrush selectedBackground;
929 QBrush alternateBackground;
930 QBrush placeHolderTextForeground;
931 QBrush accent;
932 if (v.extractPalette(&foreground, &selectedForeground, &selectedBackground,
933 &alternateBackground, &placeHolderTextForeground, &accent)) {
934 pal = new QStyleSheetPaletteData(foreground, selectedForeground, selectedBackground,
935 alternateBackground, placeHolderTextForeground, accent);
936 }
937
938 QIcon imgIcon;
939 alignment = Qt::AlignCenter;
940 QSize imgSize;
941 if (v.extractImage(&imgIcon, &alignment, &imgSize))
942 img = new QStyleSheetImageData(imgIcon, alignment, imgSize);
943
944 QIcon icon;
945 QSize size;
946 if (v.extractIcon(&icon, &size))
947 iconPtr = new QStyleSheetImageData(icon, Qt::AlignCenter, size);
948
949 int adj = -255;
950 hasFont = v.extractFont(&font, &adj);
951
952#if QT_CONFIG(tooltip)
953 if (object && qstrcmp(object->metaObject()->className(), "QTipLabel") == 0)
954 palette = QToolTip::palette();
955#endif
956
957 for (const Declaration &decl : declarations) {
958 if (decl.d->propertyId == BorderImage) {
959 QString uri;
960 QCss::TileMode horizStretch, vertStretch;
961 std::array<int, 4> cuts;
962
963 decl.borderImageValue(&uri, cuts.data(), &horizStretch, &vertStretch);
964 if (uri.isEmpty() || uri == "none"_L1) {
965 if (bd && bd->bi)
966 bd->bi->pixmap = QPixmap();
967 } else {
968 if (!bd)
969 bd = new QStyleSheetBorderData;
970 if (!bd->bi)
971 bd->bi = new QStyleSheetBorderImageData;
972
973 QStyleSheetBorderImageData *bi = bd->bi;
974 bi->pixmap = QStyleSheetStyle::loadPixmap(uri, object);
975 bi->cuts = cuts;
976 bi->horizStretch = horizStretch;
977 bi->vertStretch = vertStretch;
978 }
979 } else if (decl.d->propertyId == QtBackgroundRole) {
980 if (bg && bg->brush.style() != Qt::NoBrush)
981 continue;
982 int role = decl.d->values.at(0).variant.toInt();
983 if (role >= Value_FirstColorRole && role <= Value_LastColorRole)
984 defaultBackground = palette.color((QPalette::ColorRole)(role-Value_FirstColorRole));
985 } else if (decl.d->property.startsWith("qproperty-"_L1, Qt::CaseInsensitive)) {
986 // intentionally left blank...
987 } else if (decl.d->propertyId == UnknownProperty) {
988 bool knownStyleHint = false;
989 for (const auto sh : knownStyleHints) {
990 QLatin1StringView styleHint(sh);
991 if (decl.d->property == styleHint) {
992 QVariant hintValue;
993 if (styleHint.endsWith("alignment"_L1)) {
994 hintValue = (int) decl.alignmentValue();
995 } else if (styleHint.endsWith("color"_L1)) {
996 hintValue = (int) decl.colorValue().rgba();
997 } else if (styleHint.endsWith("size"_L1)) {
998 // Check only for the 'em' case
999 const QString valueString = decl.d->values.at(0).variant.toString();
1000 const bool isEmSize = valueString.endsWith(u"em", Qt::CaseInsensitive);
1001 if (isEmSize || valueString.endsWith(u"ex", Qt::CaseInsensitive)) {
1002 // 1em == size of font; 1ex == xHeight of font
1003 // See lengthValueFromData helper in qcssparser.cpp
1004 QFont fontForSize(font);
1005 // if no font is specified, then use the widget font if possible
1006 if (const QWidget *widget; !hasFont && (widget = qobject_cast<const QWidget*>(object)))
1007 fontForSize = widget->font();
1008
1009 const QFontMetrics fontMetrics(fontForSize);
1010 qreal pixelSize = isEmSize ? fontMetrics.height() : fontMetrics.xHeight();
1011
1012 // Transform size according to the 'em'/'ex' value
1013 qreal emexSize = {};
1014 if (decl.realValue(&emexSize, isEmSize ? "em" : "ex") && emexSize > 0) {
1015 pixelSize *= emexSize;
1016 const QSizeF newSize(pixelSize, pixelSize);
1017 decl.d->parsed = QVariant::fromValue<QSizeF>(newSize);
1018 hintValue = newSize;
1019 } else {
1020 qWarning("Invalid '%s' size for %s. Skipping.",
1021 isEmSize ? "em" : "ex", qPrintable(valueString));
1022 }
1023 } else {
1024 // Normal case where we receive a 'px' or 'pt' unit
1025 hintValue = decl.sizeValue();
1026 }
1027 } else if (styleHint.endsWith("icon"_L1)) {
1028 hintValue = decl.iconValue();
1029 } else if (styleHint == "button-layout"_L1 && decl.d->values.size() != 0
1030 && decl.d->values.at(0).type == QCss::Value::String) {
1031 hintValue = subControlLayout(decl.d->values.at(0).variant.toString().toLatin1());
1032 } else {
1033 int integer;
1034 decl.intValue(&integer);
1035 hintValue = integer;
1036 }
1037 styleHints[decl.d->property] = hintValue;
1038 knownStyleHint = true;
1039 break;
1040 }
1041 }
1042 if (!knownStyleHint)
1043 qWarning("Unknown property %s", qPrintable(decl.d->property));
1044 }
1045 }
1046
1047 if (hasBorder()) {
1048 if (const QWidget *widget = qobject_cast<const QWidget *>(object)) {
1049 QStyleSheetStyle *style = const_cast<QStyleSheetStyle *>(globalStyleSheetStyle);
1050 if (!style)
1051 style = qt_styleSheet(widget->style());
1052 if (style)
1053 fixupBorder(style->nativeFrameWidth(widget));
1054 }
1055 if (border()->hasBorderImage())
1056 defaultBackground = QBrush();
1057 }
1058}
1059
1060QRect QRenderRule::borderRect(const QRect& r) const
1061{
1062 if (!hasBox())
1063 return r;
1064 const auto &m = box()->margins;
1065 return r.adjusted(m[LeftEdge], m[TopEdge], -m[RightEdge], -m[BottomEdge]);
1066}
1067
1068QRect QRenderRule::outlineRect(const QRect& r) const
1069{
1070 QRect br = borderRect(r);
1071 if (!hasOutline())
1072 return br;
1073 const auto &b = outline()->borders;
1074 return r.adjusted(b[LeftEdge], b[TopEdge], -b[RightEdge], -b[BottomEdge]);
1075}
1076
1077QRect QRenderRule::paddingRect(const QRect& r) const
1078{
1079 QRect br = borderRect(r);
1080 if (!hasBorder())
1081 return br;
1082 const auto &b = border()->borders;
1083 return br.adjusted(b[LeftEdge], b[TopEdge], -b[RightEdge], -b[BottomEdge]);
1084}
1085
1086QRect QRenderRule::contentsRect(const QRect& r) const
1087{
1088 QRect pr = paddingRect(r);
1089 if (!hasBox())
1090 return pr;
1091 const auto &p = box()->paddings;
1092 return pr.adjusted(p[LeftEdge], p[TopEdge], -p[RightEdge], -p[BottomEdge]);
1093}
1094
1095QRect QRenderRule::boxRect(const QRect& cr, int flags) const
1096{
1097 QRect r = cr;
1098 if (hasBox()) {
1099 if (flags & Margin) {
1100 const auto &m = box()->margins;
1101 r.adjust(-m[LeftEdge], -m[TopEdge], m[RightEdge], m[BottomEdge]);
1102 }
1103 if (flags & Padding) {
1104 const auto &p = box()->paddings;
1105 r.adjust(-p[LeftEdge], -p[TopEdge], p[RightEdge], p[BottomEdge]);
1106 }
1107 }
1108 if (hasBorder() && (flags & Border)) {
1109 const auto &b = border()->borders;
1110 r.adjust(-b[LeftEdge], -b[TopEdge], b[RightEdge], b[BottomEdge]);
1111 }
1112 return r;
1113}
1114
1115QSize QRenderRule::boxSize(const QSize &cs, int flags) const
1116{
1117 QSize bs = boxRect(QRect(QPoint(0, 0), cs), flags).size();
1118 if (cs.width() < 0) bs.setWidth(-1);
1119 if (cs.height() < 0) bs.setHeight(-1);
1120 return bs;
1121}
1122
1123void QRenderRule::fixupBorder(int nativeWidth)
1124{
1125 if (bd == nullptr)
1126 return;
1127
1128 if (!bd->hasBorderImage() || bd->bi->pixmap.isNull()) {
1129 bd->bi = nullptr;
1130 // ignore the color, border of edges that have none border-style
1131 QBrush color = pal ? pal->foreground : QBrush();
1132 const bool hasRadius = bd->radii[0].isValid() || bd->radii[1].isValid()
1133 || bd->radii[2].isValid() || bd->radii[3].isValid();
1134 for (int i = 0; i < 4; i++) {
1135 if ((bd->styles[i] == BorderStyle_Native) && hasRadius)
1136 bd->styles[i] = BorderStyle_None;
1137
1138 switch (bd->styles[i]) {
1139 case BorderStyle_None:
1140 // border-style: none forces width to be 0
1141 bd->colors[i] = QBrush();
1142 bd->borders[i] = 0;
1143 break;
1144 case BorderStyle_Native:
1145 if (bd->borders[i] == 0)
1146 bd->borders[i] = nativeWidth;
1147 Q_FALLTHROUGH();
1148 default:
1149 if (bd->colors[i].style() == Qt::NoBrush) // auto-acquire 'color'
1150 bd->colors[i] = color;
1151 break;
1152 }
1153 }
1154
1155 return;
1156 }
1157
1158 // inspect the border image
1159 QStyleSheetBorderImageData *bi = bd->bi;
1160 if (bi->cuts[0] == -1) {
1161 for (int i = 0; i < 4; i++) // assume, cut = border
1162 bi->cuts[i] = int(border()->borders[i]);
1163 }
1164}
1165
1166void QRenderRule::drawBorderImage(QPainter *p, const QRect& rect)
1167{
1168 setClip(p, rect);
1169 static const Qt::TileRule tileMode2TileRule[] = {
1170 Qt::StretchTile, Qt::RoundTile, Qt::StretchTile, Qt::RepeatTile, Qt::StretchTile };
1171
1172 const QStyleSheetBorderImageData *borderImageData = border()->borderImage();
1173 const auto &targetBorders = border()->borders;
1174 const auto sourceBorders = borderImageData->cuts;
1175 QMargins sourceMargins(sourceBorders[LeftEdge], sourceBorders[TopEdge],
1176 sourceBorders[RightEdge], sourceBorders[BottomEdge]);
1177 QMargins targetMargins(targetBorders[LeftEdge], targetBorders[TopEdge],
1178 targetBorders[RightEdge], targetBorders[BottomEdge]);
1179
1180 bool wasSmoothPixmapTransform = p->renderHints() & QPainter::SmoothPixmapTransform;
1181 p->setRenderHint(QPainter::SmoothPixmapTransform);
1182 qDrawBorderPixmap(p, rect, targetMargins, borderImageData->pixmap,
1183 QRect(QPoint(), borderImageData->pixmap.size()), sourceMargins,
1184 QTileRules(tileMode2TileRule[borderImageData->horizStretch], tileMode2TileRule[borderImageData->vertStretch]));
1185 p->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothPixmapTransform);
1186 unsetClip(p);
1187}
1188
1189QRect QRenderRule::originRect(const QRect &rect, Origin origin) const
1190{
1191 switch (origin) {
1192 case Origin_Padding:
1193 return paddingRect(rect);
1194 case Origin_Border:
1195 return borderRect(rect);
1196 case Origin_Content:
1197 return contentsRect(rect);
1198 case Origin_Margin:
1199 default:
1200 return rect;
1201 }
1202}
1203
1204void QRenderRule::drawBackgroundImage(QPainter *p, const QRect &rect, QPoint off)
1205{
1206 if (!hasBackground())
1207 return;
1208
1209 const QPixmap& bgp = background()->pixmap;
1210 if (bgp.isNull())
1211 return;
1212
1213 setClip(p, borderRect(rect));
1214
1215 if (background()->origin != background()->clip) {
1216 p->save();
1217 p->setClipRect(originRect(rect, background()->clip), Qt::IntersectClip);
1218 }
1219
1220 if (background()->attachment == Attachment_Fixed)
1221 off = QPoint(0, 0);
1222
1223 QSize bgpSize = bgp.size() / bgp.devicePixelRatio();
1224 int bgpHeight = bgpSize.height();
1225 int bgpWidth = bgpSize.width();
1226 QRect r = originRect(rect, background()->origin);
1227 QRect aligned = QStyle::alignedRect(Qt::LeftToRight, background()->position, bgpSize, r);
1228 QRect inter = aligned.translated(-off).intersected(r);
1229
1230 switch (background()->repeat) {
1231 case Repeat_Y:
1232 p->drawTiledPixmap(inter.x(), r.y(), inter.width(), r.height(), bgp,
1233 inter.x() - aligned.x() + off.x(),
1234 bgpHeight - int(aligned.y() - r.y()) % bgpHeight + off.y());
1235 break;
1236 case Repeat_X:
1237 p->drawTiledPixmap(r.x(), inter.y(), r.width(), inter.height(), bgp,
1238 bgpWidth - int(aligned.x() - r.x())%bgpWidth + off.x(),
1239 inter.y() - aligned.y() + off.y());
1240 break;
1241 case Repeat_XY:
1242 p->drawTiledPixmap(r, bgp,
1243 QPoint(bgpWidth - int(aligned.x() - r.x())% bgpWidth + off.x(),
1244 bgpHeight - int(aligned.y() - r.y())%bgpHeight + off.y()));
1245 break;
1246 case Repeat_None:
1247 default:
1248 p->drawPixmap(inter.x(), inter.y(), bgp, inter.x() - aligned.x() + off.x(),
1249 inter.y() - aligned.y() + off.y(), bgp.width() , bgp.height());
1250 break;
1251 }
1252
1253
1254 if (background()->origin != background()->clip)
1255 p->restore();
1256
1257 unsetClip(p);
1258}
1259
1260void QRenderRule::drawOutline(QPainter *p, const QRect &rect)
1261{
1262 if (!hasOutline())
1263 return;
1264
1265 bool wasAntialiased = p->renderHints() & QPainter::Antialiasing;
1266 p->setRenderHint(QPainter::Antialiasing);
1267 qDrawBorder(p, rect, ou->styles.data(), ou->borders.data(), ou->colors.data(), ou->radii.data());
1268 p->setRenderHint(QPainter::Antialiasing, wasAntialiased);
1269}
1270
1271void QRenderRule::drawBorder(QPainter *p, const QRect& rect)
1272{
1273 if (!hasBorder())
1274 return;
1275
1276 if (border()->hasBorderImage()) {
1277 drawBorderImage(p, rect);
1278 return;
1279 }
1280
1281 bool wasAntialiased = p->renderHints() & QPainter::Antialiasing;
1282 p->setRenderHint(QPainter::Antialiasing);
1283 qDrawBorder(p, rect, bd->styles.data(), bd->borders.data(), bd->colors.data(), bd->radii.data());
1284 p->setRenderHint(QPainter::Antialiasing, wasAntialiased);
1285}
1286
1287QPainterPath QRenderRule::borderClip(QRect r)
1288{
1289 if (!hasBorder())
1290 return QPainterPath();
1291
1292 QSize tlr, trr, blr, brr;
1293 qNormalizeRadii(r, bd->radii.data(), &tlr, &trr, &blr, &brr);
1294 if (tlr.isNull() && trr.isNull() && blr.isNull() && brr.isNull())
1295 return QPainterPath();
1296
1297 const QRectF rect(r);
1298 const auto &borders = border()->borders;
1299 QPainterPath path;
1300 qreal curY = rect.y() + borders[TopEdge]/2.0;
1301 path.moveTo(rect.x() + tlr.width(), curY);
1302 path.lineTo(rect.right() - trr.width(), curY);
1303 qreal curX = rect.right() - borders[RightEdge]/2.0;
1304 path.arcTo(curX - 2*trr.width() + borders[RightEdge], curY,
1305 trr.width()*2 - borders[RightEdge], trr.height()*2 - borders[TopEdge], 90, -90);
1306
1307 path.lineTo(curX, rect.bottom() - brr.height());
1308 curY = rect.bottom() - borders[BottomEdge]/2.0;
1309 path.arcTo(curX - 2*brr.width() + borders[RightEdge], curY - 2*brr.height() + borders[BottomEdge],
1310 brr.width()*2 - borders[RightEdge], brr.height()*2 - borders[BottomEdge], 0, -90);
1311
1312 path.lineTo(rect.x() + blr.width(), curY);
1313 curX = rect.left() + borders[LeftEdge]/2.0;
1314 path.arcTo(curX, rect.bottom() - 2*blr.height() + borders[BottomEdge]/2.0,
1315 blr.width()*2 - borders[LeftEdge], blr.height()*2 - borders[BottomEdge], 270, -90);
1316
1317 path.lineTo(curX, rect.top() + tlr.height());
1318 path.arcTo(curX, rect.top() + borders[TopEdge]/2.0,
1319 tlr.width()*2 - borders[LeftEdge], tlr.height()*2 - borders[TopEdge], 180, -90);
1320
1321 path.closeSubpath();
1322 return path;
1323}
1324
1325/*! \internal
1326 Clip the painter to the border (in case we are using radius border)
1327 */
1328void QRenderRule::setClip(QPainter *p, const QRect &rect)
1329{
1330 if (clipset++)
1331 return;
1332 clipPath = borderClip(rect);
1333 if (!clipPath.isEmpty()) {
1334 p->save();
1335 p->setClipPath(clipPath, Qt::IntersectClip);
1336 }
1337}
1338
1339void QRenderRule::unsetClip(QPainter *p)
1340{
1341 if (--clipset)
1342 return;
1343 if (!clipPath.isEmpty())
1344 p->restore();
1345}
1346
1347void QRenderRule::drawBackground(QPainter *p, const QRect& rect, const QPoint& off)
1348{
1349 QBrush brush = hasBackground() ? background()->brush : QBrush();
1350 if (brush.style() == Qt::NoBrush)
1351 brush = defaultBackground;
1352
1353 if (brush.style() != Qt::NoBrush) {
1354 Origin origin = hasBackground() ? background()->clip : Origin_Border;
1355 // ### fix for gradients
1356 const QPainterPath &borderPath = borderClip(originRect(rect, origin));
1357 if (!borderPath.isEmpty()) {
1358 // Drawn instead of being used as clipping path for better visual quality
1359 bool wasAntialiased = p->renderHints() & QPainter::Antialiasing;
1360 p->setRenderHint(QPainter::Antialiasing);
1361 p->fillPath(borderPath, brush);
1362 p->setRenderHint(QPainter::Antialiasing, wasAntialiased);
1363 } else {
1364 p->fillRect(originRect(rect, origin), brush);
1365 }
1366 }
1367
1368 drawBackgroundImage(p, rect, off);
1369}
1370
1371void QRenderRule::drawFrame(QPainter *p, const QRect& rect)
1372{
1373 drawBackground(p, rect);
1374 if (hasBorder())
1375 drawBorder(p, borderRect(rect));
1376}
1377
1378void QRenderRule::drawImage(QPainter *p, const QRect &rect)
1379{
1380 if (!hasImage())
1381 return;
1382 img->icon.paint(p, rect, img->alignment);
1383}
1384
1385void QRenderRule::drawRule(QPainter *p, const QRect& rect)
1386{
1387 drawFrame(p, rect);
1388 drawImage(p, contentsRect(rect));
1389}
1390
1391// *shudder* , *horror*, *whoa* <-- what you might feel when you see the functions below
1392void QRenderRule::configurePalette(QPalette *p, QPalette::ColorRole fr, QPalette::ColorRole br)
1393{
1394 if (bg && bg->brush.style() != Qt::NoBrush) {
1395 if (br != QPalette::NoRole)
1396 p->setBrush(br, bg->brush);
1397 p->setBrush(QPalette::Window, bg->brush);
1398 if (bg->brush.style() == Qt::SolidPattern) {
1399 p->setBrush(QPalette::Light, bg->brush.color().lighter(115));
1400 p->setBrush(QPalette::Midlight, bg->brush.color().lighter(107));
1401 p->setBrush(QPalette::Dark, bg->brush.color().darker(150));
1402 p->setBrush(QPalette::Shadow, bg->brush.color().darker(300));
1403 }
1404 }
1405
1406 if (!hasPalette())
1407 return;
1408
1409 if (pal->foreground.style() != Qt::NoBrush) {
1410 if (fr != QPalette::NoRole)
1411 p->setBrush(fr, pal->foreground);
1412 p->setBrush(QPalette::WindowText, pal->foreground);
1413 p->setBrush(QPalette::Text, pal->foreground);
1414 }
1415 if (pal->selectionBackground.style() != Qt::NoBrush)
1416 p->setBrush(QPalette::Highlight, pal->selectionBackground);
1417 if (pal->selectionForeground.style() != Qt::NoBrush)
1418 p->setBrush(QPalette::HighlightedText, pal->selectionForeground);
1419 if (pal->alternateBackground.style() != Qt::NoBrush)
1420 p->setBrush(QPalette::AlternateBase, pal->alternateBackground);
1421}
1422
1423void setDefault(QPalette *palette, QPalette::ColorGroup group, QPalette::ColorRole role,
1424 const QBrush &defaultBrush, const QWidget *widget)
1425{
1426 const QPalette &widgetPalette = widget->palette();
1427 if (defaultBrush != Qt::NoBrush)
1428 palette->setBrush(group, role, defaultBrush);
1429 else
1430 palette->setBrush(group, role, widgetPalette.brush(group, role));
1431}
1432
1433void QRenderRule::configurePalette(QPalette *p, QPalette::ColorGroup cg, const QWidget *w, bool embedded)
1434{
1435 if (bg && bg->brush.style() != Qt::NoBrush) {
1436 p->setBrush(cg, QPalette::Base, bg->brush); // for windows, windowxp
1437 p->setBrush(cg, QPalette::Button, bg->brush); // for plastique
1438 p->setBrush(cg, w->backgroundRole(), bg->brush);
1439 p->setBrush(cg, QPalette::Window, bg->brush);
1440 }
1441
1442 if (embedded) {
1443 /* For embedded widgets (ComboBox, SpinBox and ScrollArea) we want the embedded widget
1444 * to be transparent when we have a transparent background or border image */
1445 if ((hasBackground() && background()->isTransparent())
1446 || (hasBorder() && border()->hasBorderImage() && !border()->borderImage()->pixmap.isNull()))
1447 p->setBrush(cg, w->backgroundRole(), Qt::NoBrush);
1448 }
1449
1450 if (!hasPalette())
1451 return;
1452
1453 if (pal->foreground.style() != Qt::NoBrush) {
1454 setDefault(p, cg, QPalette::ButtonText, pal->foreground, w);
1455 setDefault(p, cg, w->foregroundRole(), pal->foreground, w);
1456 setDefault(p, cg, QPalette::WindowText, pal->foreground, w);
1457 setDefault(p, cg, QPalette::Text, pal->foreground, w);
1458 QColor phColor(pal->foreground.color());
1459 phColor.setAlpha((phColor.alpha() + 1) / 2);
1460 QBrush placeholder = pal->foreground;
1461 placeholder.setColor(phColor);
1462 setDefault(p, cg, QPalette::PlaceholderText, placeholder, w);
1463 }
1464 if (pal->selectionBackground.style() != Qt::NoBrush)
1465 p->setBrush(cg, QPalette::Highlight, pal->selectionBackground);
1466 if (pal->selectionForeground.style() != Qt::NoBrush)
1467 p->setBrush(cg, QPalette::HighlightedText, pal->selectionForeground);
1468 if (pal->alternateBackground.style() != Qt::NoBrush)
1469 p->setBrush(cg, QPalette::AlternateBase, pal->alternateBackground);
1470 if (pal->placeholderForeground.style() != Qt::NoBrush)
1471 p->setBrush(cg, QPalette::PlaceholderText, pal->placeholderForeground);
1472 if (pal->accent.style() != Qt::NoBrush)
1473 p->setBrush(cg, QPalette::Accent, pal->accent);
1474}
1475
1476bool QRenderRule::hasModification() const
1477{
1478 return hasPalette() ||
1479 hasBackground() ||
1480 hasGradientBackground() ||
1481 !hasNativeBorder() ||
1482 !hasNativeOutline() ||
1483 hasBox() ||
1484 hasPosition() ||
1485 hasGeometry() ||
1486 hasImage() ||
1487 hasFont ||
1488 !styleHints.isEmpty();
1489}
1490
1491///////////////////////////////////////////////////////////////////////////////
1492// Style rules
1493#define OBJECT_PTR(x) (static_cast<QObject *>(x.ptr))
1494
1495static inline QObject *parentObject(const QObject *obj)
1496{
1497#if QT_CONFIG(tooltip)
1498 if (qobject_cast<const QLabel *>(obj) && qstrcmp(obj->metaObject()->className(), "QTipLabel") == 0) {
1499 QObject *p = qvariant_cast<QObject *>(obj->property("_q_stylesheet_parent"));
1500 if (p)
1501 return p;
1502 }
1503#endif
1504 return obj->parent();
1505}
1506
1507class QStyleSheetStyleSelector : public StyleSelector
1508{
1509public:
1510 QStyleSheetStyleSelector() { }
1511
1512 QStringList nodeNames(NodePtr node) const override
1513 {
1514 if (isNullNode(node))
1515 return QStringList();
1516 const QMetaObject *metaObject = OBJECT_PTR(node)->metaObject();
1517#if QT_CONFIG(tooltip)
1518 if (qstrcmp(metaObject->className(), "QTipLabel") == 0)
1519 return QStringList("QToolTip"_L1);
1520#endif
1521 QStringList result;
1522 do {
1523 result += QString::fromLatin1(metaObject->className()).replace(u':', u'-');
1524 metaObject = metaObject->superClass();
1525 } while (metaObject != nullptr);
1526 return result;
1527 }
1528 QString attributeValue(NodePtr node, const QCss::AttributeSelector& aSelector) const override
1529 {
1530 if (isNullNode(node))
1531 return QString();
1532
1533 const QString &name = aSelector.name;
1534 QHash<QString, QString> &cache = m_attributeCache[OBJECT_PTR(node)];
1535 QHash<QString, QString>::const_iterator cacheIt = cache.constFind(name);
1536 if (cacheIt != cache.constEnd())
1537 return cacheIt.value();
1538
1539 QVariant value;
1540 QString valueStr;
1541 QObject *obj = OBJECT_PTR(node);
1542 const int propertyIndex = obj->metaObject()->indexOfProperty(name.toLatin1());
1543 if (propertyIndex == -1) {
1544 value = obj->property(name.toLatin1()); // might be a dynamic property
1545 if (!value.isValid()) {
1546 if (name == "class"_L1) {
1547 QString className = QString::fromLatin1(obj->metaObject()->className());
1548 if (className.contains(u':'))
1549 className.replace(u':', u'-');
1550 valueStr = className;
1551 } else if (name == "style"_L1) {
1552 QWidget *w = qobject_cast<QWidget *>(obj);
1553 QStyleSheetStyle *proxy = w ? qt_styleSheet(w->style()) : nullptr;
1554 if (proxy)
1555 valueStr = QString::fromLatin1(proxy->baseStyle()->metaObject()->className());
1556 }
1557 }
1558 } else {
1559 const QMetaProperty property = obj->metaObject()->property(propertyIndex);
1560 value = property.read(obj);
1561 // support Qt 5 selector syntax, which required the integer enum value
1562 if (property.isEnumType()) {
1563 bool isNumber;
1564 aSelector.value.toInt(&isNumber);
1565 if (isNumber)
1566 value.convert(QMetaType::fromType<int>());
1567 }
1568 }
1569 if (value.isValid()) {
1570 valueStr = (value.userType() == QMetaType::QStringList
1571 || value.userType() == QMetaType::QVariantList)
1572 ? value.toStringList().join(u' ')
1573 : value.toString();
1574 }
1575 cache[name] = valueStr;
1576 return valueStr;
1577 }
1578 bool nodeNameEquals(NodePtr node, const QString& nodeName) const override
1579 {
1580 if (isNullNode(node))
1581 return false;
1582 const QMetaObject *metaObject = OBJECT_PTR(node)->metaObject();
1583#if QT_CONFIG(tooltip)
1584 if (qstrcmp(metaObject->className(), "QTipLabel") == 0)
1585 return nodeName == "QToolTip"_L1;
1586#endif
1587 do {
1588 const auto *uc = reinterpret_cast<const char16_t *>(nodeName.constData());
1589 const auto *e = uc + nodeName.size();
1590 const uchar *c = (const uchar *)metaObject->className();
1591 while (*c && uc != e && (*uc == *c || (*c == ':' && *uc == '-'))) {
1592 ++uc;
1593 ++c;
1594 }
1595 if (uc == e && !*c)
1596 return true;
1597 metaObject = metaObject->superClass();
1598 } while (metaObject != nullptr);
1599 return false;
1600 }
1601 bool hasAttributes(NodePtr) const override
1602 { return true; }
1603 QStringList nodeIds(NodePtr node) const override
1604 { return isNullNode(node) ? QStringList() : QStringList(OBJECT_PTR(node)->objectName()); }
1605 bool isNullNode(NodePtr node) const override
1606 { return node.ptr == nullptr; }
1607 NodePtr parentNode(NodePtr node) const override
1608 { NodePtr n; n.ptr = isNullNode(node) ? nullptr : parentObject(OBJECT_PTR(node)); return n; }
1609 NodePtr previousSiblingNode(NodePtr) const override
1610 { NodePtr n; n.ptr = nullptr; return n; }
1611 NodePtr duplicateNode(NodePtr node) const override
1612 { return node; }
1613 void freeNode(NodePtr) const override
1614 { }
1615
1616private:
1617 mutable QHash<const QObject *, QHash<QString, QString> > m_attributeCache;
1618};
1619
1620QList<QCss::StyleRule> QStyleSheetStyle::styleRules(const QObject *obj) const
1621{
1622 QHash<const QObject *, QList<StyleRule>>::const_iterator cacheIt =
1623 styleSheetCaches->styleRulesCache.constFind(obj);
1624 if (cacheIt != styleSheetCaches->styleRulesCache.constEnd())
1625 return cacheIt.value();
1626
1627 if (!initObject(obj)) {
1628 return QList<StyleRule>();
1629 }
1630
1631 QStyleSheetStyleSelector styleSelector;
1632
1633 StyleSheet defaultSs;
1634 QHash<const void *, StyleSheet>::const_iterator defaultCacheIt = styleSheetCaches->styleSheetCache.constFind(baseStyle());
1635 if (defaultCacheIt == styleSheetCaches->styleSheetCache.constEnd()) {
1636 defaultSs = getDefaultStyleSheet();
1637 QStyle *bs = baseStyle();
1638 styleSheetCaches->styleSheetCache.insert(bs, defaultSs);
1639 QObject::connect(bs, &QStyle::destroyed, styleSheetCaches,
1640 &QStyleSheetStyleCaches::styleDestroyed);
1641 } else {
1642 defaultSs = defaultCacheIt.value();
1643 }
1644 styleSelector.styleSheets += defaultSs;
1645
1646 if (!qApp->styleSheet().isEmpty()) {
1647 StyleSheet appSs;
1648 QHash<const void *, StyleSheet>::const_iterator appCacheIt = styleSheetCaches->styleSheetCache.constFind(qApp);
1649 if (appCacheIt == styleSheetCaches->styleSheetCache.constEnd()) {
1650 QString ss = qApp->styleSheet();
1651 if (ss.startsWith("file:///"_L1))
1652 ss.remove(0, 8);
1653 parser.init(ss, qApp->styleSheet() != ss);
1654 if (Q_UNLIKELY(!parser.parse(&appSs)))
1655 qWarning("Could not parse application stylesheet");
1656 appSs.origin = StyleSheetOrigin_Inline;
1657 appSs.depth = 1;
1658 styleSheetCaches->styleSheetCache.insert(qApp, appSs);
1659 } else {
1660 appSs = appCacheIt.value();
1661 }
1662 styleSelector.styleSheets += appSs;
1663 }
1664
1665 QList<QCss::StyleSheet> objectSs;
1666 for (const QObject *o = obj; o; o = parentObject(o)) {
1667 QString styleSheet = o->property("styleSheet").toString();
1668 if (styleSheet.isEmpty())
1669 continue;
1670 StyleSheet ss;
1671 QHash<const void *, StyleSheet>::const_iterator objCacheIt = styleSheetCaches->styleSheetCache.constFind(o);
1672 if (objCacheIt == styleSheetCaches->styleSheetCache.constEnd()) {
1673 parser.init(styleSheet);
1674 if (!parser.parse(&ss)) {
1675 parser.init("* {"_L1 + styleSheet + u'}');
1676 if (Q_UNLIKELY(!parser.parse(&ss)))
1677 qWarning() << "Could not parse stylesheet of object" << o;
1678 }
1679 ss.origin = StyleSheetOrigin_Inline;
1680 styleSheetCaches->styleSheetCache.insert(o, ss);
1681 } else {
1682 ss = objCacheIt.value();
1683 }
1684 objectSs.append(ss);
1685 }
1686
1687 for (int i = 0; i < objectSs.size(); i++)
1688 objectSs[i].depth = objectSs.size() - i + 2;
1689
1690 styleSelector.styleSheets += objectSs;
1691
1692 StyleSelector::NodePtr n;
1693 n.ptr = const_cast<QObject *>(obj);
1694 QList<QCss::StyleRule> rules = styleSelector.styleRulesForNode(n);
1695 styleSheetCaches->styleRulesCache.insert(obj, rules);
1696 return rules;
1697}
1698
1699/////////////////////////////////////////////////////////////////////////////////////////
1700// Rendering rules
1701static QList<Declaration> declarations(const QList<StyleRule> &styleRules, QLatin1StringView part,
1702 quint64 pseudoClass = PseudoClass_Unspecified)
1703{
1704 QList<Declaration> decls;
1705 for (const auto &rule : styleRules) {
1706 const Selector &selector = rule.selectors.at(0);
1707 // Rules with pseudo elements don't cascade. This is an intentional
1708 // diversion for CSS
1709 if (part.compare(selector.pseudoElement(), Qt::CaseInsensitive) != 0)
1710 continue;
1711 quint64 negated = 0;
1712 quint64 cssClass = selector.pseudoClass(&negated);
1713 if ((pseudoClass == PseudoClass_Any) || (cssClass == PseudoClass_Unspecified)
1714 || ((((cssClass & pseudoClass) == cssClass)) && ((negated & pseudoClass) == 0)))
1715 decls += rule.declarations;
1716 }
1717 return decls;
1718}
1719
1720int QStyleSheetStyle::nativeFrameWidth(const QWidget *w)
1721{
1722 QStyle *base = baseStyle();
1723
1724#if QT_CONFIG(spinbox)
1725 if (qobject_cast<const QAbstractSpinBox *>(w))
1726 return base->pixelMetric(QStyle::PM_SpinBoxFrameWidth, nullptr, w);
1727#endif
1728
1729#if QT_CONFIG(combobox)
1730 if (qobject_cast<const QComboBox *>(w))
1731 return base->pixelMetric(QStyle::PM_ComboBoxFrameWidth, nullptr, w);
1732#endif
1733
1734#if QT_CONFIG(menu)
1735 if (qobject_cast<const QMenu *>(w))
1736 return base->pixelMetric(QStyle::PM_MenuPanelWidth, nullptr, w);
1737#endif
1738
1739#if QT_CONFIG(menubar)
1740 if (qobject_cast<const QMenuBar *>(w))
1741 return base->pixelMetric(QStyle::PM_MenuBarPanelWidth, nullptr, w);
1742#endif
1743#ifndef QT_NO_FRAME
1744 if (const QFrame *frame = qobject_cast<const QFrame *>(w)) {
1745 if (frame->frameShape() == QFrame::NoFrame)
1746 return 0;
1747 }
1748#endif
1749
1750 if (qstrcmp(w->metaObject()->className(), "QTipLabel") == 0)
1751 return base->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, nullptr, w);
1752
1753 return base->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, w);
1754}
1755
1756static quint64 pseudoClass(QStyle::State state)
1757{
1758 quint64 pc = 0;
1759 if (state & QStyle::State_Enabled) {
1760 pc |= PseudoClass_Enabled;
1761 if (state & QStyle::State_MouseOver)
1762 pc |= PseudoClass_Hover;
1763 } else {
1764 pc |= PseudoClass_Disabled;
1765 }
1766 if (state & QStyle::State_Active)
1767 pc |= PseudoClass_Active;
1768 if (state & QStyle::State_Window)
1769 pc |= PseudoClass_Window;
1770 if (state & QStyle::State_Sunken)
1771 pc |= PseudoClass_Pressed;
1772 if (state & QStyle::State_HasFocus)
1773 pc |= PseudoClass_Focus;
1774 if (state & QStyle::State_On)
1775 pc |= (PseudoClass_On | PseudoClass_Checked);
1776 if (state & QStyle::State_Off)
1777 pc |= (PseudoClass_Off | PseudoClass_Unchecked);
1778 if (state & QStyle::State_NoChange)
1779 pc |= PseudoClass_Indeterminate;
1780 if (state & QStyle::State_Selected)
1781 pc |= PseudoClass_Selected;
1782 if (state & QStyle::State_Horizontal)
1783 pc |= PseudoClass_Horizontal;
1784 else
1785 pc |= PseudoClass_Vertical;
1786 if (state & (QStyle::State_Open | QStyle::State_On | QStyle::State_Sunken))
1787 pc |= PseudoClass_Open;
1788 else
1789 pc |= PseudoClass_Closed;
1790 if (state & QStyle::State_Children)
1791 pc |= PseudoClass_Children;
1792 if (state & QStyle::State_Sibling)
1793 pc |= PseudoClass_Sibling;
1794 if (state & QStyle::State_ReadOnly)
1795 pc |= PseudoClass_ReadOnly;
1796 if (state & QStyle::State_Item)
1797 pc |= PseudoClass_Item;
1798#ifdef QT_KEYPAD_NAVIGATION
1799 if (state & QStyle::State_HasEditFocus)
1800 pc |= PseudoClass_EditFocus;
1801#endif
1802 return pc;
1803}
1804
1805static void qt_check_if_internal_object(const QObject **obj, int *element)
1806{
1807#if !QT_CONFIG(dockwidget)
1808 Q_UNUSED(obj);
1809 Q_UNUSED(element);
1810#else
1811 if (*obj && qstrcmp((*obj)->metaObject()->className(), "QDockWidgetTitleButton") == 0) {
1812 if ((*obj)->objectName() == "qt_dockwidget_closebutton"_L1) {
1813 *element = PseudoElement_DockWidgetCloseButton;
1814 } else if ((*obj)->objectName() == "qt_dockwidget_floatbutton"_L1) {
1815 *element = PseudoElement_DockWidgetFloatButton;
1816 }
1817 *obj = (*obj)->parent();
1818 }
1819#endif
1820}
1821
1822QRenderRule QStyleSheetStyle::renderRule(const QObject *obj, int element, quint64 state) const
1823{
1824 qt_check_if_internal_object(&obj, &element);
1825 QHash<quint64, QRenderRule> &cache = styleSheetCaches->renderRulesCache[obj][element];
1826 QHash<quint64, QRenderRule>::const_iterator cacheIt = cache.constFind(state);
1827 if (cacheIt != cache.constEnd())
1828 return cacheIt.value();
1829
1830 if (!initObject(obj))
1831 return QRenderRule();
1832
1833 quint64 stateMask = 0;
1834 const QList<StyleRule> rules = styleRules(obj);
1835 for (const auto &rule : rules) {
1836 const Selector &selector = rule.selectors.at(0);
1837 quint64 negated = 0;
1838 stateMask |= selector.pseudoClass(&negated);
1839 stateMask |= negated;
1840 }
1841
1842 cacheIt = cache.constFind(state & stateMask);
1843 if (cacheIt != cache.constEnd()) {
1844 QRenderRule newRule = cacheIt.value();
1845 cache[state] = newRule;
1846 return newRule;
1847 }
1848
1849
1850 const auto part = QLatin1StringView(knownPseudoElements[element].name);
1851 QList<Declaration> decls = declarations(rules, part, state);
1852 QRenderRule newRule(decls, obj);
1853 cache[state] = newRule;
1854 if ((state & stateMask) != state)
1855 cache[state&stateMask] = newRule;
1856 return newRule;
1857}
1858
1859QRenderRule QStyleSheetStyle::renderRule(const QObject *obj, const QStyleOption *opt, int pseudoElement) const
1860{
1861 quint64 extraClass = 0;
1862 QStyle::State state = opt ? opt->state : QStyle::State(QStyle::State_None);
1863
1864 if (const QStyleOptionComplex *complex = qstyleoption_cast<const QStyleOptionComplex *>(opt)) {
1865 if (pseudoElement != PseudoElement_None) {
1866 // if not an active subcontrol, just pass enabled/disabled
1867 QStyle::SubControl subControl = knownPseudoElements[pseudoElement].subControl;
1868
1869 if (!(complex->activeSubControls & subControl))
1870 state &= (QStyle::State_Enabled | QStyle::State_Horizontal | QStyle::State_HasFocus);
1871 }
1872
1873 switch (pseudoElement) {
1874 case PseudoElement_ComboBoxDropDown:
1875 case PseudoElement_ComboBoxArrow:
1876 state |= (complex->state & (QStyle::State_On|QStyle::State_ReadOnly));
1877 break;
1878 case PseudoElement_SpinBoxUpButton:
1879 case PseudoElement_SpinBoxDownButton:
1880 case PseudoElement_SpinBoxUpArrow:
1881 case PseudoElement_SpinBoxDownArrow:
1882#if QT_CONFIG(spinbox)
1883 if (const QStyleOptionSpinBox *sb = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
1884 bool on = false;
1885 bool up = pseudoElement == PseudoElement_SpinBoxUpButton
1886 || pseudoElement == PseudoElement_SpinBoxUpArrow;
1887 if ((sb->stepEnabled & QAbstractSpinBox::StepUpEnabled) && up)
1888 on = true;
1889 else if ((sb->stepEnabled & QAbstractSpinBox::StepDownEnabled) && !up)
1890 on = true;
1891 state |= (on ? QStyle::State_On : QStyle::State_Off);
1892 }
1893#endif // QT_CONFIG(spinbox)
1894 break;
1895 case PseudoElement_GroupBoxTitle:
1896 state |= (complex->state & (QStyle::State_MouseOver | QStyle::State_Sunken));
1897 break;
1898 case PseudoElement_ToolButtonMenu:
1899 case PseudoElement_ToolButtonMenuArrow:
1900 case PseudoElement_ToolButtonMenuIndicator:
1901 state |= complex->state & QStyle::State_MouseOver;
1902 if (complex->state & QStyle::State_Sunken ||
1903 complex->activeSubControls & QStyle::SC_ToolButtonMenu)
1904 state |= QStyle::State_Sunken;
1905 break;
1906 case PseudoElement_SliderGroove:
1907 state |= complex->state & QStyle::State_MouseOver;
1908 break;
1909 default:
1910 break;
1911 }
1912
1913 if (const QStyleOptionComboBox *combo = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
1914 // QStyle::State_On is set when the popup is being shown
1915 // Propagate EditField Pressed state
1916 if (pseudoElement == PseudoElement_None
1917 && (complex->activeSubControls & QStyle::SC_ComboBoxEditField)
1918 && (!(state & QStyle::State_MouseOver))) {
1919 state |= QStyle::State_Sunken;
1920 }
1921
1922 if (!combo->frame)
1923 extraClass |= PseudoClass_Frameless;
1924 if (!combo->editable)
1925 extraClass |= PseudoClass_ReadOnly;
1926 else
1927 extraClass |= PseudoClass_Editable;
1928#if QT_CONFIG(spinbox)
1929 } else if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
1930 if (!spin->frame)
1931 extraClass |= PseudoClass_Frameless;
1932#endif // QT_CONFIG(spinbox)
1933 } else if (const QStyleOptionGroupBox *gb = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
1934 if (gb->features & QStyleOptionFrame::Flat)
1935 extraClass |= PseudoClass_Flat;
1936 if (gb->lineWidth == 0)
1937 extraClass |= PseudoClass_Frameless;
1938 } else if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
1939 if (tb->titleBarState & Qt::WindowMinimized) {
1940 extraClass |= PseudoClass_Minimized;
1941 }
1942 else if (tb->titleBarState & Qt::WindowMaximized)
1943 extraClass |= PseudoClass_Maximized;
1944 }
1945 } else {
1946 // handle simple style options
1947 if (const QStyleOptionMenuItem *mi = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
1948 if (mi->menuItemType == QStyleOptionMenuItem::DefaultItem)
1949 extraClass |= PseudoClass_Default;
1950 if (mi->checkType == QStyleOptionMenuItem::Exclusive)
1951 extraClass |= PseudoClass_Exclusive;
1952 else if (mi->checkType == QStyleOptionMenuItem::NonExclusive)
1953 extraClass |= PseudoClass_NonExclusive;
1954 if (mi->checkType != QStyleOptionMenuItem::NotCheckable)
1955 extraClass |= (mi->checked) ? (PseudoClass_On|PseudoClass_Checked)
1956 : (PseudoClass_Off|PseudoClass_Unchecked);
1957 } else if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
1958 if (hdr->position == QStyleOptionHeader::OnlyOneSection)
1959 extraClass |= PseudoClass_OnlyOne;
1960 else if (hdr->position == QStyleOptionHeader::Beginning)
1961 extraClass |= PseudoClass_First;
1962 else if (hdr->position == QStyleOptionHeader::End)
1963 extraClass |= PseudoClass_Last;
1964 else if (hdr->position == QStyleOptionHeader::Middle)
1965 extraClass |= PseudoClass_Middle;
1966
1967 if (hdr->selectedPosition == QStyleOptionHeader::NextAndPreviousAreSelected)
1968 extraClass |= (PseudoClass_NextSelected | PseudoClass_PreviousSelected);
1969 else if (hdr->selectedPosition == QStyleOptionHeader::NextIsSelected)
1970 extraClass |= PseudoClass_NextSelected;
1971 else if (hdr->selectedPosition == QStyleOptionHeader::PreviousIsSelected)
1972 extraClass |= PseudoClass_PreviousSelected;
1973#if QT_CONFIG(tabwidget)
1974 } else if (const QStyleOptionTabWidgetFrame *tab = qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt)) {
1975 switch (tab->shape) {
1976 case QTabBar::RoundedNorth:
1977 case QTabBar::TriangularNorth:
1978 extraClass |= PseudoClass_Top;
1979 break;
1980 case QTabBar::RoundedSouth:
1981 case QTabBar::TriangularSouth:
1982 extraClass |= PseudoClass_Bottom;
1983 break;
1984 case QTabBar::RoundedEast:
1985 case QTabBar::TriangularEast:
1986 extraClass |= PseudoClass_Right;
1987 break;
1988 case QTabBar::RoundedWest:
1989 case QTabBar::TriangularWest:
1990 extraClass |= PseudoClass_Left;
1991 break;
1992 default:
1993 break;
1994 }
1995#endif
1996#if QT_CONFIG(tabbar)
1997 } else if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
1998 if (tab->position == QStyleOptionTab::OnlyOneTab)
1999 extraClass |= PseudoClass_OnlyOne;
2000 else if (tab->position == QStyleOptionTab::Beginning)
2001 extraClass |= PseudoClass_First;
2002 else if (tab->position == QStyleOptionTab::End)
2003 extraClass |= PseudoClass_Last;
2004 else if (tab->position == QStyleOptionTab::Middle)
2005 extraClass |= PseudoClass_Middle;
2006
2007 if (tab->selectedPosition == QStyleOptionTab::NextIsSelected)
2008 extraClass |= PseudoClass_NextSelected;
2009 else if (tab->selectedPosition == QStyleOptionTab::PreviousIsSelected)
2010 extraClass |= PseudoClass_PreviousSelected;
2011
2012 switch (tab->shape) {
2013 case QTabBar::RoundedNorth:
2014 case QTabBar::TriangularNorth:
2015 extraClass |= PseudoClass_Top;
2016 break;
2017 case QTabBar::RoundedSouth:
2018 case QTabBar::TriangularSouth:
2019 extraClass |= PseudoClass_Bottom;
2020 break;
2021 case QTabBar::RoundedEast:
2022 case QTabBar::TriangularEast:
2023 extraClass |= PseudoClass_Right;
2024 break;
2025 case QTabBar::RoundedWest:
2026 case QTabBar::TriangularWest:
2027 extraClass |= PseudoClass_Left;
2028 break;
2029 default:
2030 break;
2031 }
2032#endif // QT_CONFIG(tabbar)
2033 } else if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
2034 if (btn->features & QStyleOptionButton::Flat)
2035 extraClass |= PseudoClass_Flat;
2036 if (btn->features & QStyleOptionButton::DefaultButton)
2037 extraClass |= PseudoClass_Default;
2038 } else if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
2039 if (frm->lineWidth == 0)
2040 extraClass |= PseudoClass_Frameless;
2041 if (frm->features & QStyleOptionFrame::Flat)
2042 extraClass |= PseudoClass_Flat;
2043 }
2044#if QT_CONFIG(toolbar)
2045 else if (const QStyleOptionToolBar *tb = qstyleoption_cast<const QStyleOptionToolBar *>(opt)) {
2046 if (tb->toolBarArea == Qt::LeftToolBarArea)
2047 extraClass |= PseudoClass_Left;
2048 else if (tb->toolBarArea == Qt::RightToolBarArea)
2049 extraClass |= PseudoClass_Right;
2050 else if (tb->toolBarArea == Qt::TopToolBarArea)
2051 extraClass |= PseudoClass_Top;
2052 else if (tb->toolBarArea == Qt::BottomToolBarArea)
2053 extraClass |= PseudoClass_Bottom;
2054
2055 if (tb->positionWithinLine == QStyleOptionToolBar::Beginning)
2056 extraClass |= PseudoClass_First;
2057 else if (tb->positionWithinLine == QStyleOptionToolBar::Middle)
2058 extraClass |= PseudoClass_Middle;
2059 else if (tb->positionWithinLine == QStyleOptionToolBar::End)
2060 extraClass |= PseudoClass_Last;
2061 else if (tb->positionWithinLine == QStyleOptionToolBar::OnlyOne)
2062 extraClass |= PseudoClass_OnlyOne;
2063 }
2064#endif // QT_CONFIG(toolbar)
2065#if QT_CONFIG(toolbox)
2066 else if (const QStyleOptionToolBox *tb = qstyleoption_cast<const QStyleOptionToolBox *>(opt)) {
2067 if (tb->position == QStyleOptionToolBox::OnlyOneTab)
2068 extraClass |= PseudoClass_OnlyOne;
2069 else if (tb->position == QStyleOptionToolBox::Beginning)
2070 extraClass |= PseudoClass_First;
2071 else if (tb->position == QStyleOptionToolBox::End)
2072 extraClass |= PseudoClass_Last;
2073 else if (tb->position == QStyleOptionToolBox::Middle)
2074 extraClass |= PseudoClass_Middle;
2075
2076 if (tb->selectedPosition == QStyleOptionToolBox::NextIsSelected)
2077 extraClass |= PseudoClass_NextSelected;
2078 else if (tb->selectedPosition == QStyleOptionToolBox::PreviousIsSelected)
2079 extraClass |= PseudoClass_PreviousSelected;
2080 }
2081#endif // QT_CONFIG(toolbox)
2082#if QT_CONFIG(dockwidget)
2083 else if (const QStyleOptionDockWidget *dw = qstyleoption_cast<const QStyleOptionDockWidget *>(opt)) {
2084 if (dw->verticalTitleBar)
2085 extraClass |= PseudoClass_Vertical;
2086 else
2087 extraClass |= PseudoClass_Horizontal;
2088 if (dw->closable)
2089 extraClass |= PseudoClass_Closable;
2090 if (dw->floatable)
2091 extraClass |= PseudoClass_Floatable;
2092 if (dw->movable)
2093 extraClass |= PseudoClass_Movable;
2094 }
2095#endif // QT_CONFIG(dockwidget)
2096#if QT_CONFIG(itemviews)
2097 else if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
2098 if (vopt->features & QStyleOptionViewItem::Alternate)
2099 extraClass |= PseudoClass_Alternate;
2100 if (vopt->viewItemPosition == QStyleOptionViewItem::OnlyOne)
2101 extraClass |= PseudoClass_OnlyOne;
2102 else if (vopt->viewItemPosition == QStyleOptionViewItem::Beginning)
2103 extraClass |= PseudoClass_First;
2104 else if (vopt->viewItemPosition == QStyleOptionViewItem::End)
2105 extraClass |= PseudoClass_Last;
2106 else if (vopt->viewItemPosition == QStyleOptionViewItem::Middle)
2107 extraClass |= PseudoClass_Middle;
2108
2109 }
2110#endif
2111#if QT_CONFIG(textedit)
2112 else if (const QPlainTextEdit *edit = qobject_cast<const QPlainTextEdit *>(obj)) {
2113 extraClass |= (edit->isReadOnly() ? PseudoClass_ReadOnly : PseudoClass_Editable);
2114 }
2115 else if (const QTextEdit *edit = qobject_cast<const QTextEdit *>(obj)) {
2116 extraClass |= (edit->isReadOnly() ? PseudoClass_ReadOnly : PseudoClass_Editable);
2117 }
2118#endif
2119#if QT_CONFIG(lineedit)
2120 // LineEdit sets Sunken flag to indicate Sunken frame (argh)
2121 if (const QLineEdit *lineEdit = qobject_cast<const QLineEdit *>(obj)) {
2122 state &= ~QStyle::State_Sunken;
2123 if (lineEdit->hasFrame()) {
2124 extraClass &= ~PseudoClass_Frameless;
2125 } else {
2126 extraClass |= PseudoClass_Frameless;
2127 }
2128 } else
2129#endif
2130 if (const QFrame *frm = qobject_cast<const QFrame *>(obj)) {
2131 if (frm->lineWidth() == 0)
2132 extraClass |= PseudoClass_Frameless;
2133 }
2134 }
2135
2136 return renderRule(obj, pseudoElement, pseudoClass(state) | extraClass);
2137}
2138
2139bool QStyleSheetStyle::hasStyleRule(const QObject *obj, int part) const
2140{
2141 QHash<int, bool> &cache = styleSheetCaches->hasStyleRuleCache[obj];
2142 QHash<int, bool>::const_iterator cacheIt = cache.constFind(part);
2143 if (cacheIt != cache.constEnd())
2144 return cacheIt.value();
2145
2146 if (!initObject(obj))
2147 return false;
2148
2149 const QList<StyleRule> &rules = styleRules(obj);
2150 if (part == PseudoElement_None) {
2151 bool result = obj && !rules.isEmpty();
2152 cache[part] = result;
2153 return result;
2154 }
2155
2156 const auto pseudoElement = QLatin1StringView(knownPseudoElements[part].name);
2157 for (const auto &rule : rules) {
2158 const Selector &selector = rule.selectors.at(0);
2159 if (pseudoElement.compare(selector.pseudoElement(), Qt::CaseInsensitive) == 0) {
2160 cache[part] = true;
2161 return true;
2162 }
2163 }
2164
2165 cache[part] = false;
2166 return false;
2167}
2168
2169static Origin defaultOrigin(int pe)
2170{
2171 switch (pe) {
2172 case PseudoElement_ScrollBarAddPage:
2173 case PseudoElement_ScrollBarSubPage:
2174 case PseudoElement_ScrollBarAddLine:
2175 case PseudoElement_ScrollBarSubLine:
2176 case PseudoElement_ScrollBarFirst:
2177 case PseudoElement_ScrollBarLast:
2178 case PseudoElement_GroupBoxTitle:
2179 case PseudoElement_GroupBoxIndicator: // never used
2180 case PseudoElement_ToolButtonMenu:
2181 case PseudoElement_SliderAddPage:
2182 case PseudoElement_SliderSubPage:
2183 return Origin_Border;
2184
2185 case PseudoElement_SpinBoxUpButton:
2186 case PseudoElement_SpinBoxDownButton:
2187 case PseudoElement_PushButtonMenuIndicator:
2188 case PseudoElement_ComboBoxDropDown:
2189 case PseudoElement_ToolButtonMenuIndicator:
2190 case PseudoElement_MenuCheckMark:
2191 case PseudoElement_MenuIcon:
2192 case PseudoElement_MenuRightArrow:
2193 return Origin_Padding;
2194
2195 case PseudoElement_Indicator:
2196 case PseudoElement_ExclusiveIndicator:
2197 case PseudoElement_ComboBoxArrow:
2198 case PseudoElement_ScrollBarSlider:
2199 case PseudoElement_ScrollBarUpArrow:
2200 case PseudoElement_ScrollBarDownArrow:
2201 case PseudoElement_ScrollBarLeftArrow:
2202 case PseudoElement_ScrollBarRightArrow:
2203 case PseudoElement_SpinBoxUpArrow:
2204 case PseudoElement_SpinBoxDownArrow:
2205 case PseudoElement_ToolButtonMenuArrow:
2206 case PseudoElement_HeaderViewUpArrow:
2207 case PseudoElement_HeaderViewDownArrow:
2208 case PseudoElement_SliderGroove:
2209 case PseudoElement_SliderHandle:
2210 return Origin_Content;
2211
2212 default:
2213 return Origin_Margin;
2214 }
2215}
2216
2217static Qt::Alignment defaultPosition(int pe)
2218{
2219 switch (pe) {
2220 case PseudoElement_Indicator:
2221 case PseudoElement_ExclusiveIndicator:
2222 case PseudoElement_MenuCheckMark:
2223 case PseudoElement_MenuIcon:
2224 return Qt::AlignLeft | Qt::AlignVCenter;
2225
2226 case PseudoElement_ScrollBarAddLine:
2227 case PseudoElement_ScrollBarLast:
2228 case PseudoElement_SpinBoxDownButton:
2229 case PseudoElement_PushButtonMenuIndicator:
2230 case PseudoElement_ToolButtonMenuIndicator:
2231 return Qt::AlignRight | Qt::AlignBottom;
2232
2233 case PseudoElement_ScrollBarSubLine:
2234 case PseudoElement_ScrollBarFirst:
2235 case PseudoElement_SpinBoxUpButton:
2236 case PseudoElement_ComboBoxDropDown:
2237 case PseudoElement_ToolButtonMenu:
2238 case PseudoElement_DockWidgetCloseButton:
2239 case PseudoElement_DockWidgetFloatButton:
2240 return Qt::AlignRight | Qt::AlignTop;
2241
2242 case PseudoElement_ScrollBarUpArrow:
2243 case PseudoElement_ScrollBarDownArrow:
2244 case PseudoElement_ScrollBarLeftArrow:
2245 case PseudoElement_ScrollBarRightArrow:
2246 case PseudoElement_SpinBoxUpArrow:
2247 case PseudoElement_SpinBoxDownArrow:
2248 case PseudoElement_ComboBoxArrow:
2249 case PseudoElement_DownArrow:
2250 case PseudoElement_UpArrow:
2251 case PseudoElement_LeftArrow:
2252 case PseudoElement_RightArrow:
2253 case PseudoElement_ToolButtonMenuArrow:
2254 case PseudoElement_SliderGroove:
2255 return Qt::AlignCenter;
2256
2257 case PseudoElement_GroupBoxTitle:
2258 case PseudoElement_GroupBoxIndicator: // never used
2259 return Qt::AlignLeft | Qt::AlignTop;
2260
2261 case PseudoElement_HeaderViewUpArrow:
2262 case PseudoElement_HeaderViewDownArrow:
2263 case PseudoElement_MenuRightArrow:
2264 return Qt::AlignRight | Qt::AlignVCenter;
2265
2266 default:
2267 return { };
2268 }
2269}
2270
2271QSize QStyleSheetStyle::defaultSize(const QWidget *w, QSize sz, const QRect& rect, int pe) const
2272{
2273 QStyle *base = baseStyle();
2274
2275 switch (pe) {
2276 case PseudoElement_Indicator:
2277 case PseudoElement_MenuCheckMark:
2278 if (sz.width() == -1)
2279 sz.setWidth(base->pixelMetric(PM_IndicatorWidth, nullptr, w));
2280 if (sz.height() == -1)
2281 sz.setHeight(base->pixelMetric(PM_IndicatorHeight, nullptr, w));
2282 break;
2283
2284 case PseudoElement_ExclusiveIndicator:
2285 case PseudoElement_GroupBoxIndicator:
2286 if (sz.width() == -1)
2287 sz.setWidth(base->pixelMetric(PM_ExclusiveIndicatorWidth, nullptr, w));
2288 if (sz.height() == -1)
2289 sz.setHeight(base->pixelMetric(PM_ExclusiveIndicatorHeight, nullptr, w));
2290 break;
2291
2292 case PseudoElement_PushButtonMenuIndicator: {
2293 int pm = base->pixelMetric(PM_MenuButtonIndicator, nullptr, w);
2294 if (sz.width() == -1)
2295 sz.setWidth(pm);
2296 if (sz.height() == -1)
2297 sz.setHeight(pm);
2298 }
2299 break;
2300
2301 case PseudoElement_ComboBoxDropDown:
2302 if (sz.width() == -1)
2303 sz.setWidth(16);
2304 break;
2305
2306 case PseudoElement_ComboBoxArrow:
2307 case PseudoElement_DownArrow:
2308 case PseudoElement_UpArrow:
2309 case PseudoElement_LeftArrow:
2310 case PseudoElement_RightArrow:
2311 case PseudoElement_ToolButtonMenuArrow:
2312 case PseudoElement_ToolButtonMenuIndicator:
2313 case PseudoElement_MenuRightArrow:
2314 if (sz.width() == -1)
2315 sz.setWidth(13);
2316 if (sz.height() == -1)
2317 sz.setHeight(13);
2318 break;
2319
2320 case PseudoElement_SpinBoxUpButton:
2321 case PseudoElement_SpinBoxDownButton:
2322 if (sz.width() == -1)
2323 sz.setWidth(16);
2324 if (sz.height() == -1)
2325 sz.setHeight(rect.height()/2);
2326 break;
2327
2328 case PseudoElement_ToolButtonMenu:
2329 if (sz.width() == -1)
2330 sz.setWidth(base->pixelMetric(PM_MenuButtonIndicator, nullptr, w));
2331 break;
2332
2333 case PseudoElement_HeaderViewUpArrow:
2334 case PseudoElement_HeaderViewDownArrow: {
2335 int pm = base->pixelMetric(PM_HeaderMargin, nullptr, w);
2336 if (sz.width() == -1)
2337 sz.setWidth(pm);
2338 if (sz.height() == 1)
2339 sz.setHeight(pm);
2340 break;
2341 }
2342
2343 case PseudoElement_ScrollBarFirst:
2344 case PseudoElement_ScrollBarLast:
2345 case PseudoElement_ScrollBarAddLine:
2346 case PseudoElement_ScrollBarSubLine:
2347 case PseudoElement_ScrollBarSlider: {
2348 int pm = pixelMetric(QStyle::PM_ScrollBarExtent, nullptr, w);
2349 if (sz.width() == -1)
2350 sz.setWidth(pm);
2351 if (sz.height() == -1)
2352 sz.setHeight(pm);
2353 break;
2354 }
2355
2356 case PseudoElement_DockWidgetCloseButton:
2357 case PseudoElement_DockWidgetFloatButton: {
2358 int iconSize = pixelMetric(PM_SmallIconSize, nullptr, w);
2359 return QSize(iconSize, iconSize);
2360 }
2361
2362 default:
2363 break;
2364 }
2365
2366 // expand to rectangle
2367 if (sz.height() == -1)
2368 sz.setHeight(rect.height());
2369 if (sz.width() == -1)
2370 sz.setWidth(rect.width());
2371
2372 return sz;
2373}
2374
2375static PositionMode defaultPositionMode(int pe)
2376{
2377 switch (pe) {
2378 case PseudoElement_ScrollBarFirst:
2379 case PseudoElement_ScrollBarLast:
2380 case PseudoElement_ScrollBarAddLine:
2381 case PseudoElement_ScrollBarSubLine:
2382 case PseudoElement_ScrollBarAddPage:
2383 case PseudoElement_ScrollBarSubPage:
2384 case PseudoElement_ScrollBarSlider:
2385 case PseudoElement_SliderGroove:
2386 case PseudoElement_SliderHandle:
2387 case PseudoElement_TabWidgetPane:
2388 return PositionMode_Absolute;
2389 default:
2390 return PositionMode_Static;
2391 }
2392}
2393
2394QRect QStyleSheetStyle::positionRect(const QWidget *w, const QRenderRule &rule2, int pe,
2395 const QRect &originRect, Qt::LayoutDirection dir) const
2396{
2397 const QStyleSheetPositionData *p = rule2.position();
2398 PositionMode mode = (p && p->mode != PositionMode_Unknown) ? p->mode : defaultPositionMode(pe);
2399 Qt::Alignment position = (p && p->position != 0) ? p->position : defaultPosition(pe);
2400 QRect r;
2401
2402 if (mode != PositionMode_Absolute) {
2403 QSize sz = defaultSize(w, rule2.size(), originRect, pe);
2404 sz = sz.expandedTo(rule2.minimumContentsSize());
2405 r = QStyle::alignedRect(dir, position, sz, originRect);
2406 if (p) {
2407 int left = p->left ? p->left : -p->right;
2408 int top = p->top ? p->top : -p->bottom;
2409 r.translate(dir == Qt::LeftToRight ? left : -left, top);
2410 }
2411 } else {
2412 r = p ? originRect.adjusted(dir == Qt::LeftToRight ? p->left : p->right, p->top,
2413 dir == Qt::LeftToRight ? -p->right : -p->left, -p->bottom)
2414 : originRect;
2415 if (rule2.hasContentsSize()) {
2416 QSize sz = rule2.size().expandedTo(rule2.minimumContentsSize());
2417 if (sz.width() == -1) sz.setWidth(r.width());
2418 if (sz.height() == -1) sz.setHeight(r.height());
2419 r = QStyle::alignedRect(dir, position, sz, r);
2420 }
2421 }
2422 return r;
2423}
2424
2425QRect QStyleSheetStyle::positionRect(const QWidget *w, const QRenderRule& rule1, const QRenderRule& rule2, int pe,
2426 const QRect& rect, Qt::LayoutDirection dir) const
2427{
2428 const QStyleSheetPositionData *p = rule2.position();
2429 Origin origin = (p && p->origin != Origin_Unknown) ? p->origin : defaultOrigin(pe);
2430 QRect originRect = rule1.originRect(rect, origin);
2431 return positionRect(w, rule2, pe, originRect, dir);
2432}
2433
2434
2435/** \internal
2436 For widget that have an embedded widget (such as combobox) return that embedded widget.
2437 otherwise return the widget itself
2438 */
2439static QWidget *embeddedWidget(QWidget *w)
2440{
2441#if QT_CONFIG(combobox)
2442 if (QComboBox *cmb = qobject_cast<QComboBox *>(w)) {
2443 if (cmb->isEditable())
2444 return cmb->lineEdit();
2445 else
2446 return cmb;
2447 }
2448#endif
2449
2450#if QT_CONFIG(spinbox)
2451 if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox *>(w))
2452 return sb->findChild<QLineEdit *>();
2453#endif
2454
2455#if QT_CONFIG(scrollarea)
2456 if (QAbstractScrollArea *sa = qobject_cast<QAbstractScrollArea *>(w))
2457 return sa->viewport();
2458#endif
2459
2460 return w;
2461}
2462
2463/** \internal
2464 Returns the widget whose style rules apply to \a w.
2465
2466 When \a w is an embedded widget, this is the container widget.
2467 For example, if w is a line edit embedded in a combobox, this returns the combobox.
2468 When \a w is not embedded, this function return \a w itself.
2469
2470*/
2471static QWidget *containerWidget(const QWidget *w)
2472{
2473#if QT_CONFIG(lineedit)
2474 if (qobject_cast<const QLineEdit *>(w)) {
2475 //if the QLineEdit is an embeddedWidget, we need the rule of the real widget
2476#if QT_CONFIG(combobox)
2477 if (qobject_cast<const QComboBox *>(w->parentWidget()))
2478 return w->parentWidget();
2479#endif
2480#if QT_CONFIG(spinbox)
2481 if (qobject_cast<const QAbstractSpinBox *>(w->parentWidget()))
2482 return w->parentWidget();
2483#endif
2484 }
2485#endif // QT_CONFIG(lineedit)
2486
2487#if QT_CONFIG(scrollarea)
2488 if (const QAbstractScrollArea *sa = qobject_cast<const QAbstractScrollArea *>(w->parentWidget())) {
2489 if (sa->viewport() == w)
2490 return w->parentWidget();
2491 }
2492#endif
2493
2494 return const_cast<QWidget *>(w);
2495}
2496
2497/** \internal
2498 returns \c true if the widget can NOT be styled directly
2499 */
2500static bool unstylable(const QWidget *w)
2501{
2502 if (!w->styleSheet().isEmpty())
2503 return false;
2504
2505 if (containerWidget(w) != w)
2506 return true;
2507
2508#ifndef QT_NO_FRAME
2509 // detect QComboBoxPrivateContainer
2510 else if (qobject_cast<const QFrame *>(w)) {
2511 if (0
2512#if QT_CONFIG(combobox)
2513 || qobject_cast<const QComboBox *>(w->parentWidget())
2514#endif
2515 )
2516 return true;
2517 }
2518#endif
2519
2520#if QT_CONFIG(tabbar)
2521 if (w->metaObject() == &QWidget::staticMetaObject
2522 && qobject_cast<const QTabBar*>(w->parentWidget()))
2523 return true; // The moving tab of a QTabBar
2524#endif
2525
2526 return false;
2527}
2528
2529static quint64 extendedPseudoClass(const QWidget *w)
2530{
2531 quint64 pc = w->isWindow() ? quint64(PseudoClass_Window) : 0;
2532#if QT_CONFIG(abstractslider)
2533 if (const QAbstractSlider *slider = qobject_cast<const QAbstractSlider *>(w)) {
2534 pc |= ((slider->orientation() == Qt::Vertical) ? PseudoClass_Vertical : PseudoClass_Horizontal);
2535 } else
2536#endif
2537#if QT_CONFIG(combobox)
2538 if (const QComboBox *combo = qobject_cast<const QComboBox *>(w)) {
2539 if (combo->isEditable())
2540 pc |= (combo->isEditable() ? PseudoClass_Editable : PseudoClass_ReadOnly);
2541 } else
2542#endif
2543#if QT_CONFIG(lineedit)
2544 if (const QLineEdit *edit = qobject_cast<const QLineEdit *>(w)) {
2545 pc |= (edit->isReadOnly() ? PseudoClass_ReadOnly : PseudoClass_Editable);
2546 } else
2547#endif
2548#if QT_CONFIG(textedit)
2549 if (const QTextEdit *edit = qobject_cast<const QTextEdit *>(w)) {
2550 pc |= (edit->isReadOnly() ? PseudoClass_ReadOnly : PseudoClass_Editable);
2551 } else
2552 if (const QPlainTextEdit *edit = qobject_cast<const QPlainTextEdit *>(w)) {
2553 pc |= (edit->isReadOnly() ? PseudoClass_ReadOnly : PseudoClass_Editable);
2554 } else
2555#endif
2556 {}
2557 return pc;
2558}
2559
2560// sets up the geometry of the widget. We set a dynamic property when
2561// we modify the min/max size of the widget. The min/max size is restored
2562// to their original value when a new stylesheet that does not contain
2563// the CSS properties is set and when the widget has this dynamic property set.
2564// This way we don't trample on users who had setup a min/max size in code and
2565// don't use stylesheets at all.
2566void QStyleSheetStyle::setGeometry(QWidget *w)
2567{
2568 QRenderRule rule = renderRule(w, PseudoElement_None, PseudoClass_Enabled | extendedPseudoClass(w));
2569 const QStyleSheetGeometryData *geo = rule.geometry();
2570 if (w->property("_q_stylesheet_minw").toBool()
2571 && ((!rule.hasGeometry() || geo->minWidth == -1))) {
2572 w->setMinimumWidth(0);
2573 w->setProperty("_q_stylesheet_minw", QVariant());
2574 }
2575 if (w->property("_q_stylesheet_minh").toBool()
2576 && ((!rule.hasGeometry() || geo->minHeight == -1))) {
2577 w->setMinimumHeight(0);
2578 w->setProperty("_q_stylesheet_minh", QVariant());
2579 }
2580 if (w->property("_q_stylesheet_maxw").toBool()
2581 && ((!rule.hasGeometry() || geo->maxWidth == -1))) {
2582 w->setMaximumWidth(QWIDGETSIZE_MAX);
2583 w->setProperty("_q_stylesheet_maxw", QVariant());
2584 }
2585 if (w->property("_q_stylesheet_maxh").toBool()
2586 && ((!rule.hasGeometry() || geo->maxHeight == -1))) {
2587 w->setMaximumHeight(QWIDGETSIZE_MAX);
2588 w->setProperty("_q_stylesheet_maxh", QVariant());
2589 }
2590
2591
2592 if (rule.hasGeometry()) {
2593 if (geo->minWidth != -1) {
2594 w->setProperty("_q_stylesheet_minw", true);
2595 w->setMinimumWidth(rule.boxSize(QSize(qMax(geo->width, geo->minWidth), 0)).width());
2596 }
2597 if (geo->minHeight != -1) {
2598 w->setProperty("_q_stylesheet_minh", true);
2599 w->setMinimumHeight(rule.boxSize(QSize(0, qMax(geo->height, geo->minHeight))).height());
2600 }
2601 if (geo->maxWidth != -1) {
2602 w->setProperty("_q_stylesheet_maxw", true);
2603 w->setMaximumWidth(rule.boxSize(QSize(qMin(geo->width == -1 ? QWIDGETSIZE_MAX : geo->width,
2604 geo->maxWidth == -1 ? QWIDGETSIZE_MAX : geo->maxWidth), 0)).width());
2605 }
2606 if (geo->maxHeight != -1) {
2607 w->setProperty("_q_stylesheet_maxh", true);
2608 w->setMaximumHeight(rule.boxSize(QSize(0, qMin(geo->height == -1 ? QWIDGETSIZE_MAX : geo->height,
2609 geo->maxHeight == -1 ? QWIDGETSIZE_MAX : geo->maxHeight))).height());
2610 }
2611 }
2612}
2613
2614void QStyleSheetStyle::setProperties(QWidget *w)
2615{
2616 // The final occurrence of each property is authoritative.
2617 // Set value for each property in the order of property final occurrence
2618 // since properties interact.
2619
2620 const QList<Declaration> decls = declarations(styleRules(w), {});
2621 QList<int> finals; // indices in reverse order of each property's final occurrence
2622
2623 {
2624 // scan decls for final occurrence of each "qproperty"
2625 QDuplicateTracker<QString> propertySet(decls.size());
2626 for (int i = decls.size() - 1; i >= 0; --i) {
2627 const QString property = decls.at(i).d->property;
2628 if (!property.startsWith("qproperty-"_L1, Qt::CaseInsensitive))
2629 continue;
2630 if (!propertySet.hasSeen(property))
2631 finals.append(i);
2632 }
2633 }
2634
2635 for (int i = finals.size() - 1; i >= 0; --i) {
2636 const Declaration &decl = decls.at(finals[i]);
2637 QStringView property = decl.d->property;
2638 property = property.mid(10); // strip "qproperty-"
2639 const auto propertyL1 = property.toLatin1();
2640
2641 const QMetaObject *metaObject = w->metaObject();
2642 int index = metaObject->indexOfProperty(propertyL1);
2643 if (Q_UNLIKELY(index == -1)) {
2644 qWarning() << w << " does not have a property named " << property;
2645 continue;
2646 }
2647 const QMetaProperty metaProperty = metaObject->property(index);
2648 if (Q_UNLIKELY(!metaProperty.isWritable() || !metaProperty.isDesignable())) {
2649 qWarning() << w << " cannot design property named " << property;
2650 continue;
2651 }
2652
2653 QVariant v;
2654 const QVariant value = w->property(propertyL1);
2655 switch (value.userType()) {
2656 case QMetaType::QIcon: v = decl.iconValue(); break;
2657 case QMetaType::QImage: v = QImage(decl.uriValue()); break;
2658 case QMetaType::QPixmap: v = QPixmap(decl.uriValue()); break;
2659 case QMetaType::QRect: v = decl.rectValue(); break;
2660 case QMetaType::QSize: v = decl.sizeValue(); break;
2661 case QMetaType::QColor: v = decl.colorValue(); break;
2662 case QMetaType::QBrush: v = decl.brushValue(); break;
2663#ifndef QT_NO_SHORTCUT
2664 case QMetaType::QKeySequence: v = QKeySequence(decl.d->values.at(0).variant.toString()); break;
2665#endif
2666 default: v = decl.d->values.at(0).variant; break;
2667 }
2668
2669 if (propertyL1 == QByteArrayView("styleSheet") && value == v)
2670 continue;
2671
2672 w->setProperty(propertyL1, v);
2673 }
2674}
2675
2676void QStyleSheetStyle::setPalette(QWidget *w)
2677{
2678 struct RuleRoleMap {
2679 int state;
2680 QPalette::ColorGroup group;
2681 } map[3] = {
2682 { int(PseudoClass_Active | PseudoClass_Enabled), QPalette::Active },
2683 { PseudoClass_Disabled, QPalette::Disabled },
2684 { PseudoClass_Enabled, QPalette::Inactive }
2685 };
2686
2687 const bool useStyleSheetPropagationInWidgetStyles =
2688 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
2689
2690 QPalette p;
2691 if (!useStyleSheetPropagationInWidgetStyles)
2692 p = w->palette();
2693
2694 QWidget *ew = embeddedWidget(w);
2695
2696 for (int i = 0; i < 3; i++) {
2697 QRenderRule rule = renderRule(w, PseudoElement_None, map[i].state | extendedPseudoClass(w));
2698 if (i == 0) {
2699 if (!w->property("_q_styleSheetWidgetFont").isValid()) {
2700 saveWidgetFont(w, w->d_func()->localFont());
2701 }
2702 updateStyleSheetFont(w);
2703 if (ew != w)
2704 updateStyleSheetFont(ew);
2705 }
2706
2707 rule.configurePalette(&p, map[i].group, ew, ew != w);
2708 }
2709
2710 if (!useStyleSheetPropagationInWidgetStyles || p.resolveMask() != 0) {
2711 QPalette wp = w->palette();
2712 styleSheetCaches->customPaletteWidgets.insert(w, {wp, p.resolveMask()});
2713
2714 if (useStyleSheetPropagationInWidgetStyles) {
2715 p = p.resolve(wp);
2716 p.setResolveMask(p.resolveMask() | wp.resolveMask());
2717 }
2718
2719 w->setPalette(p);
2720 if (ew != w)
2721 ew->setPalette(p);
2722 }
2723}
2724
2725void QStyleSheetStyle::unsetPalette(QWidget *w)
2726{
2727 const bool useStyleSheetPropagationInWidgetStyles =
2728 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
2729
2730 const auto it = styleSheetCaches->customPaletteWidgets.find(w);
2731 if (it != styleSheetCaches->customPaletteWidgets.end()) {
2732 auto customizedPalette = std::move(*it);
2733 styleSheetCaches->customPaletteWidgets.erase(it);
2734
2735 QPalette original;
2736 if (useStyleSheetPropagationInWidgetStyles)
2737 original = std::move(customizedPalette).reverted(w->palette());
2738 else
2739 original = customizedPalette.oldWidgetValue;
2740
2741 w->setPalette(original);
2742 QWidget *ew = embeddedWidget(w);
2743 if (ew != w)
2744 ew->setPalette(original);
2745 }
2746
2747 if (useStyleSheetPropagationInWidgetStyles) {
2748 unsetStyleSheetFont(w);
2749 QWidget *ew = embeddedWidget(w);
2750 if (ew != w)
2751 unsetStyleSheetFont(ew);
2752 } else {
2753 QVariant oldFont = w->property("_q_styleSheetWidgetFont");
2754 if (oldFont.isValid()) {
2755 w->setFont(qvariant_cast<QFont>(oldFont));
2756 }
2757 }
2758
2759 if (styleSheetCaches->autoFillDisabledWidgets.contains(w)) {
2760 embeddedWidget(w)->setAutoFillBackground(true);
2761 styleSheetCaches->autoFillDisabledWidgets.remove(w);
2762 }
2763}
2764
2765void QStyleSheetStyle::unsetStyleSheetFont(QWidget *w) const
2766{
2767 const auto it = styleSheetCaches->customFontWidgets.find(w);
2768 if (it != styleSheetCaches->customFontWidgets.end()) {
2769 auto customizedFont = std::move(*it);
2770 styleSheetCaches->customFontWidgets.erase(it);
2771 w->setFont(std::move(customizedFont).reverted(w->font()));
2772 }
2773}
2774
2775static void updateObjects(const QList<const QObject *>& objects)
2776{
2777 if (!styleSheetCaches->styleRulesCache.isEmpty() || !styleSheetCaches->hasStyleRuleCache.isEmpty() || !styleSheetCaches->renderRulesCache.isEmpty()) {
2778 for (const QObject *object : objects) {
2779 styleSheetCaches->styleRulesCache.remove(object);
2780 styleSheetCaches->hasStyleRuleCache.remove(object);
2781 styleSheetCaches->renderRulesCache.remove(object);
2782 }
2783 }
2784
2785 QEvent event(QEvent::StyleChange);
2786 for (const QObject *object : objects) {
2787 if (auto widget = qobject_cast<QWidget*>(const_cast<QObject*>(object))) {
2788 widget->style()->polish(widget);
2789 QCoreApplication::sendEvent(widget, &event);
2790 QList<const QObject *> children;
2791 children.reserve(widget->children().size() + 1);
2792 for (auto child: std::as_const(widget->children()))
2793 children.append(child);
2794 updateObjects(children);
2795 }
2796 }
2797}
2798
2799/////////////////////////////////////////////////////////////////////////////////////////
2800// The stylesheet style
2801int QStyleSheetStyle::numinstances = 0;
2802
2803QStyleSheetStyle::QStyleSheetStyle(QStyle *base)
2804 : QWindowsStyle(*new QStyleSheetStylePrivate), base(base), refcount(1)
2805{
2806 ++numinstances;
2807 if (numinstances == 1) {
2808 styleSheetCaches = new QStyleSheetStyleCaches;
2809 }
2810}
2811
2812QStyleSheetStyle::~QStyleSheetStyle()
2813{
2814 --numinstances;
2815 if (numinstances == 0) {
2816 delete styleSheetCaches;
2817 }
2818}
2819QStyle *QStyleSheetStyle::baseStyle() const
2820{
2821 if (base)
2822 return base;
2823 if (QStyleSheetStyle *me = qt_styleSheet(QApplication::style()))
2824 return me->base;
2825 return QApplication::style();
2826}
2827
2828void QStyleSheetStyleCaches::objectDestroyed(QObject *o)
2829{
2830 styleRulesCache.remove(o);
2831 hasStyleRuleCache.remove(o);
2832 renderRulesCache.remove(o);
2833 customPaletteWidgets.remove((const QWidget *)o);
2834 customFontWidgets.remove(static_cast<QWidget *>(o));
2835 styleSheetCache.remove(o);
2836 autoFillDisabledWidgets.remove((const QWidget *)o);
2837}
2838
2839void QStyleSheetStyleCaches::styleDestroyed(QObject *o)
2840{
2841 styleSheetCache.remove(o);
2842}
2843
2844/*!
2845 * Make sure that the cache will be clean by connecting destroyed if needed.
2846 * return false if the widget is not stylable;
2847 */
2848bool QStyleSheetStyle::initObject(const QObject *obj) const
2849{
2850 if (!obj)
2851 return false;
2852 if (const QWidget *w = qobject_cast<const QWidget*>(obj)) {
2853 if (w->testAttribute(Qt::WA_StyleSheet))
2854 return true;
2855 if (unstylable(w))
2856 return false;
2857 const_cast<QWidget *>(w)->setAttribute(Qt::WA_StyleSheet, true);
2858 }
2859
2860 connect(obj, &QObject::destroyed,
2861 styleSheetCaches, &QStyleSheetStyleCaches::objectDestroyed,
2862 Qt::UniqueConnection);
2863 return true;
2864}
2865
2866void QStyleSheetStyle::polish(QWidget *w)
2867{
2868 baseStyle()->polish(w);
2869 RECURSION_GUARD(return)
2870
2871 if (!initObject(w))
2872 return;
2873
2874 if (styleSheetCaches->styleRulesCache.contains(w)) {
2875 // the widget accessed its style pointer before polish (or repolish)
2876 // (example: the QAbstractSpinBox constructor ask for the stylehint)
2877 styleSheetCaches->styleRulesCache.remove(w);
2878 styleSheetCaches->hasStyleRuleCache.remove(w);
2879 styleSheetCaches->renderRulesCache.remove(w);
2880 styleSheetCaches->styleSheetCache.remove(w);
2881 }
2882 setGeometry(w);
2883 setProperties(w);
2884 unsetPalette(w);
2885 setPalette(w);
2886
2887 //set the WA_Hover attribute if one of the selector depends of the hover state
2888 const QList<StyleRule> rules = styleRules(w);
2889 for (const auto &rule : rules) {
2890 const Selector &selector = rule.selectors.at(0);
2891 quint64 negated = 0;
2892 quint64 cssClass = selector.pseudoClass(&negated);
2893 if ( cssClass & PseudoClass_Hover || negated & PseudoClass_Hover) {
2894 w->setAttribute(Qt::WA_Hover);
2895 embeddedWidget(w)->setAttribute(Qt::WA_Hover);
2896 embeddedWidget(w)->setMouseTracking(true);
2897 }
2898 }
2899
2900
2901#if QT_CONFIG(scrollarea)
2902 if (QAbstractScrollArea *sa = qobject_cast<QAbstractScrollArea *>(w)) {
2903 QRenderRule rule = renderRule(sa, PseudoElement_None, PseudoClass_Enabled);
2904 if ((rule.hasBorder() && rule.border()->hasBorderImage())
2905 || (rule.hasBackground() && !rule.background()->pixmap.isNull())) {
2906 connect(sa->horizontalScrollBar(), &QScrollBar::valueChanged,
2907 sa, QOverload<>::of(&QAbstractScrollArea::update), Qt::UniqueConnection);
2908 connect(sa->verticalScrollBar(), &QScrollBar::valueChanged,
2909 sa, QOverload<>::of(&QAbstractScrollArea::update), Qt::UniqueConnection);
2910 }
2911 }
2912#endif
2913
2914 QRenderRule rule = renderRule(w, PseudoElement_None, PseudoClass_Any);
2915
2916 w->setAttribute(Qt::WA_StyleSheetTarget, rule.hasModification());
2917
2918 if (rule.hasDrawable() || rule.hasBox()) {
2919 if (w->metaObject() == &QWidget::staticMetaObject
2920#if QT_CONFIG(itemviews)
2921 || qobject_cast<QHeaderView *>(w)
2922#endif
2923#if QT_CONFIG(tabbar)
2924 || qobject_cast<QTabBar *>(w)
2925#endif
2926#ifndef QT_NO_FRAME
2927 || qobject_cast<QFrame *>(w)
2928#endif
2929#if QT_CONFIG(mainwindow)
2930 || qobject_cast<QMainWindow *>(w)
2931#endif
2932#if QT_CONFIG(mdiarea)
2933 || qobject_cast<QMdiSubWindow *>(w)
2934#endif
2935#if QT_CONFIG(menubar)
2936 || qobject_cast<QMenuBar *>(w)
2937#endif
2938#if QT_CONFIG(dialog)
2939 || qobject_cast<QDialog *>(w)
2940#endif
2941 ) {
2942 w->setAttribute(Qt::WA_StyledBackground, true);
2943 }
2944 QWidget *ew = embeddedWidget(w);
2945 if (ew->autoFillBackground()) {
2946 ew->setAutoFillBackground(false);
2947 styleSheetCaches->autoFillDisabledWidgets.insert(w);
2948 if (ew != w) { //eg. viewport of a scrollarea
2949 //(in order to draw the background anyway in case we don't.)
2950 ew->setAttribute(Qt::WA_StyledBackground, true);
2951 }
2952 }
2953 if (!rule.hasBackground() || rule.background()->isTransparent() || rule.hasBox()
2954 || (!rule.hasNativeBorder() && !rule.border()->isOpaque()))
2955 w->setAttribute(Qt::WA_OpaquePaintEvent, false);
2956 if (rule.hasBox() || !rule.hasNativeBorder()
2957#if QT_CONFIG(pushbutton)
2958 || (qobject_cast<QPushButton *>(w))
2959#endif
2960 )
2961 w->setAttribute(Qt::WA_MacShowFocusRect, false);
2962 }
2963}
2964
2965void QStyleSheetStyle::polish(QApplication *app)
2966{
2967 baseStyle()->polish(app);
2968}
2969
2970void QStyleSheetStyle::polish(QPalette &pal)
2971{
2972 baseStyle()->polish(pal);
2973}
2974
2975void QStyleSheetStyle::repolish(QWidget *w)
2976{
2977 QList<const QObject *> children;
2978 children.reserve(w->children().size() + 1);
2979 for (auto child: std::as_const(w->children()))
2980 children.append(child);
2981 children.append(w);
2982 styleSheetCaches->styleSheetCache.remove(w);
2983 updateObjects(children);
2984}
2985
2986void QStyleSheetStyle::repolish(QApplication *app)
2987{
2988 Q_UNUSED(app);
2989 const QList<const QObject*> allObjects = styleSheetCaches->styleRulesCache.keys();
2990 styleSheetCaches->styleSheetCache.remove(qApp);
2991 styleSheetCaches->styleRulesCache.clear();
2992 styleSheetCaches->hasStyleRuleCache.clear();
2993 styleSheetCaches->renderRulesCache.clear();
2994 updateObjects(allObjects);
2995}
2996
2997void QStyleSheetStyle::unpolish(QWidget *w)
2998{
2999 if (!w || !w->testAttribute(Qt::WA_StyleSheet)) {
3000 baseStyle()->unpolish(w);
3001 return;
3002 }
3003
3004 styleSheetCaches->styleRulesCache.remove(w);
3005 styleSheetCaches->hasStyleRuleCache.remove(w);
3006 styleSheetCaches->renderRulesCache.remove(w);
3007 styleSheetCaches->styleSheetCache.remove(w);
3008 unsetPalette(w);
3009 setGeometry(w);
3010 w->setAttribute(Qt::WA_StyleSheetTarget, false);
3011 w->setAttribute(Qt::WA_StyleSheet, false);
3012 w->disconnect(this);
3013#if QT_CONFIG(scrollarea)
3014 if (QAbstractScrollArea *sa = qobject_cast<QAbstractScrollArea *>(w)) {
3015 disconnect(sa->horizontalScrollBar(), &QScrollBar::valueChanged,
3016 sa, QOverload<>::of(&QAbstractScrollArea::update));
3017 disconnect(sa->verticalScrollBar(), &QScrollBar::valueChanged,
3018 sa, QOverload<>::of(&QAbstractScrollArea::update));
3019 }
3020#endif
3021 baseStyle()->unpolish(w);
3022}
3023
3024void QStyleSheetStyle::unpolish(QApplication *app)
3025{
3026 baseStyle()->unpolish(app);
3027 RECURSION_GUARD(return)
3028 styleSheetCaches->styleRulesCache.clear();
3029 styleSheetCaches->hasStyleRuleCache.clear();
3030 styleSheetCaches->renderRulesCache.clear();
3031 styleSheetCaches->styleSheetCache.remove(qApp);
3032}
3033
3034void QStyleSheetStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p,
3035 const QWidget *w) const
3036{
3037 RECURSION_GUARD(baseStyle()->drawComplexControl(cc, opt, p, w); return)
3038
3039 QRenderRule rule = renderRule(w, opt);
3040
3041 switch (cc) {
3042 case CC_ComboBox:
3043 if (const QStyleOptionComboBox *cmb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
3044 QStyleOptionComboBox cmbOpt(*cmb);
3045 cmbOpt.rect = rule.borderRect(opt->rect);
3046 if (rule.hasNativeBorder()) {
3047 rule.drawBackgroundImage(p, cmbOpt.rect);
3048 rule.configurePalette(&cmbOpt.palette, QPalette::ButtonText, QPalette::Button);
3049 bool customDropDown = (opt->subControls & QStyle::SC_ComboBoxArrow)
3050 && (hasStyleRule(w, PseudoElement_ComboBoxDropDown) || hasStyleRule(w, PseudoElement_ComboBoxArrow));
3051 if (customDropDown)
3052 cmbOpt.subControls &= ~QStyle::SC_ComboBoxArrow;
3053 if (rule.baseStyleCanDraw()) {
3054 baseStyle()->drawComplexControl(cc, &cmbOpt, p, w);
3055 } else {
3056 QWindowsStyle::drawComplexControl(cc, &cmbOpt, p, w);
3057 }
3058 if (!customDropDown)
3059 return;
3060 } else {
3061 rule.drawRule(p, opt->rect);
3062 }
3063
3064 if (opt->subControls & QStyle::SC_ComboBoxArrow) {
3065 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
3066 if (subRule.hasDrawable()) {
3067 QRect r = subControlRect(CC_ComboBox, opt, SC_ComboBoxArrow, w);
3068 subRule.drawRule(p, r);
3069 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_ComboBoxArrow);
3070 r = positionRect(w, subRule, subRule2, PseudoElement_ComboBoxArrow, r, opt->direction);
3071 subRule2.drawRule(p, r);
3072 } else {
3073 rule.configurePalette(&cmbOpt.palette, QPalette::ButtonText, QPalette::Button);
3074 cmbOpt.subControls = QStyle::SC_ComboBoxArrow;
3075 QWindowsStyle::drawComplexControl(cc, &cmbOpt, p, w);
3076 }
3077 }
3078
3079 return;
3080 }
3081 break;
3082
3083#if QT_CONFIG(spinbox)
3084 case CC_SpinBox:
3085 if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
3086 QStyleOptionSpinBox spinOpt(*spin);
3087 rule.configurePalette(&spinOpt.palette, QPalette::ButtonText, QPalette::Button);
3088 rule.configurePalette(&spinOpt.palette, QPalette::Text, QPalette::Base);
3089 spinOpt.rect = rule.borderRect(opt->rect);
3090 bool customUp = true, customDown = true;
3091 QRenderRule upRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
3092 QRenderRule downRule = renderRule(w, opt, PseudoElement_SpinBoxDownButton);
3093 bool upRuleMatch = upRule.hasGeometry() || upRule.hasPosition();
3094 bool downRuleMatch = downRule.hasGeometry() || downRule.hasPosition();
3095 if (rule.hasNativeBorder() && !upRuleMatch && !downRuleMatch) {
3096 rule.drawBackgroundImage(p, spinOpt.rect);
3097 customUp = (opt->subControls & QStyle::SC_SpinBoxUp)
3098 && (hasStyleRule(w, PseudoElement_SpinBoxUpButton) || hasStyleRule(w, PseudoElement_UpArrow));
3099 if (customUp)
3100 spinOpt.subControls &= ~QStyle::SC_SpinBoxUp;
3101 customDown = (opt->subControls & QStyle::SC_SpinBoxDown)
3102 && (hasStyleRule(w, PseudoElement_SpinBoxDownButton) || hasStyleRule(w, PseudoElement_DownArrow));
3103 if (customDown)
3104 spinOpt.subControls &= ~QStyle::SC_SpinBoxDown;
3105 if (rule.baseStyleCanDraw()) {
3106 baseStyle()->drawComplexControl(cc, &spinOpt, p, w);
3107 } else {
3108 QWindowsStyle::drawComplexControl(cc, &spinOpt, p, w);
3109 }
3110 if (!customUp && !customDown)
3111 return;
3112 } else {
3113 rule.drawRule(p, opt->rect);
3114 }
3115
3116 if ((opt->subControls & QStyle::SC_SpinBoxUp) && customUp) {
3117 QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
3118 if (subRule.hasDrawable()) {
3119 QRect r = subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w);
3120 subRule.drawRule(p, r);
3121 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SpinBoxUpArrow);
3122 r = positionRect(w, subRule, subRule2, PseudoElement_SpinBoxUpArrow, r, opt->direction);
3123 subRule2.drawRule(p, r);
3124 } else {
3125 spinOpt.subControls = QStyle::SC_SpinBoxUp;
3126 QWindowsStyle::drawComplexControl(cc, &spinOpt, p, w);
3127 }
3128 }
3129
3130 if ((opt->subControls & QStyle::SC_SpinBoxDown) && customDown) {
3131 QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxDownButton);
3132 if (subRule.hasDrawable()) {
3133 QRect r = subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w);
3134 subRule.drawRule(p, r);
3135 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SpinBoxDownArrow);
3136 r = positionRect(w, subRule, subRule2, PseudoElement_SpinBoxDownArrow, r, opt->direction);
3137 subRule2.drawRule(p, r);
3138 } else {
3139 spinOpt.subControls = QStyle::SC_SpinBoxDown;
3140 QWindowsStyle::drawComplexControl(cc, &spinOpt, p, w);
3141 }
3142 }
3143 return;
3144 }
3145 break;
3146#endif // QT_CONFIG(spinbox)
3147
3148 case CC_GroupBox:
3149 if (const QStyleOptionGroupBox *gb = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
3150
3151 QRect labelRect, checkBoxRect, titleRect, frameRect;
3152 bool hasTitle = (gb->subControls & QStyle::SC_GroupBoxCheckBox) || !gb->text.isEmpty();
3153
3154 if (!rule.hasDrawable() && (!hasTitle || !hasStyleRule(w, PseudoElement_GroupBoxTitle))
3155 && !hasStyleRule(w, PseudoElement_Indicator) && !rule.hasBox() && !rule.hasFont && !rule.hasPalette()) {
3156 // let the native style draw the combobox if there is no style for it.
3157 break;
3158 }
3159 rule.drawBackground(p, opt->rect);
3160
3161 QRenderRule titleRule = renderRule(w, opt, PseudoElement_GroupBoxTitle);
3162 bool clipSet = false;
3163
3164 if (hasTitle) {
3165 labelRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, w);
3166 //Some native style (such as mac) may return a too small rectangle (because they use smaller fonts), so we may need to expand it a little bit.
3167 labelRect.setSize(labelRect.size().expandedTo(ParentStyle::subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, w).size()));
3168 if (gb->subControls & QStyle::SC_GroupBoxCheckBox) {
3169 checkBoxRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, w);
3170 titleRect = titleRule.boxRect(checkBoxRect.united(labelRect));
3171 } else {
3172 titleRect = titleRule.boxRect(labelRect);
3173 }
3174 if (!titleRule.hasBackground() || !titleRule.background()->isTransparent()) {
3175 clipSet = true;
3176 p->save();
3177 p->setClipRegion(QRegion(opt->rect) - titleRect);
3178 }
3179 }
3180
3181 frameRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, w);
3182 QStyleOptionFrame frame;
3183 frame.QStyleOption::operator=(*gb);
3184 frame.features = gb->features;
3185 frame.lineWidth = gb->lineWidth;
3186 frame.midLineWidth = gb->midLineWidth;
3187 frame.rect = frameRect;
3188 drawPrimitive(PE_FrameGroupBox, &frame, p, w);
3189
3190 if (clipSet)
3191 p->restore();
3192
3193 // draw background and frame of the title
3194 if (hasTitle)
3195 titleRule.drawRule(p, titleRect);
3196
3197 // draw the indicator
3198 if (gb->subControls & QStyle::SC_GroupBoxCheckBox) {
3199 QStyleOptionButton box;
3200 box.QStyleOption::operator=(*gb);
3201 box.rect = checkBoxRect;
3202 drawPrimitive(PE_IndicatorCheckBox, &box, p, w);
3203 }
3204
3205 // draw the text
3206 if (!gb->text.isEmpty()) {
3207 int alignment = int(Qt::AlignCenter | Qt::TextShowMnemonic);
3208 if (!styleHint(QStyle::SH_UnderlineShortcut, opt, w)) {
3209 alignment |= Qt::TextHideMnemonic;
3210 }
3211
3212 QPalette pal = gb->palette;
3213 if (gb->textColor.isValid())
3214 pal.setColor(QPalette::WindowText, gb->textColor);
3215 titleRule.configurePalette(&pal, QPalette::WindowText, QPalette::Window);
3216 drawItemText(p, labelRect, alignment, pal, gb->state & State_Enabled,
3217 gb->text, QPalette::WindowText);
3218
3219 if (gb->state & State_HasFocus) {
3220 QStyleOptionFocusRect fropt;
3221 fropt.QStyleOption::operator=(*gb);
3222 fropt.rect = labelRect;
3223 drawPrimitive(PE_FrameFocusRect, &fropt, p, w);
3224 }
3225 }
3226
3227 return;
3228 }
3229 break;
3230
3231 case CC_ToolButton:
3232#if QT_CONFIG(toolbutton)
3233 if (const QStyleOptionToolButton *tool = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
3234 QStyleOptionToolButton toolOpt(*tool);
3235 rule.configurePalette(&toolOpt.palette, QPalette::ButtonText, QPalette::Button);
3236 toolOpt.font = rule.font.resolve(toolOpt.font);
3237 toolOpt.rect = rule.borderRect(opt->rect);
3238 const auto customArrowElement = [tool]{
3239 switch (tool->arrowType) {
3240 case Qt::DownArrow: return PseudoElement_DownArrow;
3241 case Qt::UpArrow: return PseudoElement_UpArrow;
3242 case Qt::LeftArrow: return PseudoElement_LeftArrow;
3243 case Qt::RightArrow: return PseudoElement_RightArrow;
3244 default: break;
3245 }
3246 return PseudoElement_None;
3247 };
3248 // if arrow/menu/indicators are requested, either draw them using the available rule,
3249 // or let the base style draw them; but not both
3250 const bool drawArrow = tool->features & QStyleOptionToolButton::Arrow;
3251 bool customArrow = drawArrow && hasStyleRule(w, customArrowElement());
3252 if (customArrow) {
3253 toolOpt.features &= ~QStyleOptionToolButton::Arrow;
3254 toolOpt.text = QString(); // we need to draw the arrow and the text ourselves
3255 }
3256 bool drawDropDown = tool->features & QStyleOptionToolButton::MenuButtonPopup;
3257 bool customDropDown = drawDropDown && hasStyleRule(w, PseudoElement_ToolButtonMenu);
3258 bool customDropDownArrow = false;
3259 bool drawMenuIndicator = tool->features & QStyleOptionToolButton::HasMenu;
3260 if (customDropDown) {
3261 toolOpt.subControls &= ~QStyle::SC_ToolButtonMenu;
3262 customDropDownArrow = hasStyleRule(w, PseudoElement_ToolButtonMenuArrow);
3263 if (customDropDownArrow)
3264 toolOpt.features &= ~(QStyleOptionToolButton::Menu | QStyleOptionToolButton::HasMenu);
3265 }
3266 const bool customMenuIndicator = (!drawDropDown && drawMenuIndicator)
3267 && hasStyleRule(w, PseudoElement_ToolButtonMenuIndicator);
3268 if (customMenuIndicator)
3269 toolOpt.features &= ~QStyleOptionToolButton::HasMenu;
3270
3271 if (rule.hasNativeBorder()) {
3272 if (tool->subControls & SC_ToolButton) {
3273 //in some case (eg. the button is "auto raised") the style doesn't draw the background
3274 //so we need to draw the background.
3275 // use the same condition as in QCommonStyle
3276 State bflags = tool->state & ~State_Sunken;
3277 if (bflags & State_AutoRaise && (!(bflags & State_MouseOver) || !(bflags & State_Enabled)))
3278 bflags &= ~State_Raised;
3279 if (tool->state & State_Sunken && tool->activeSubControls & SC_ToolButton)
3280 bflags |= State_Sunken;
3281 if (!(bflags & (State_Sunken | State_On | State_Raised)))
3282 rule.drawBackground(p, toolOpt.rect);
3283 }
3284
3285 QStyleOptionToolButton nativeToolOpt(toolOpt);
3286 // don't draw natively if we have a custom rule for menu indicators and/or buttons
3287 if (customMenuIndicator)
3288 nativeToolOpt.features &= ~(QStyleOptionToolButton::Menu | QStyleOptionToolButton::HasMenu);
3289 if (customDropDown || customDropDownArrow)
3290 nativeToolOpt.features &= ~(QStyleOptionToolButton::Menu | QStyleOptionToolButton::HasMenu | QStyleOptionToolButton::MenuButtonPopup);
3291 // Let base or windows style draw the button, which will include the menu-button
3292 if (rule.baseStyleCanDraw() && !(tool->features & QStyleOptionToolButton::Arrow))
3293 baseStyle()->drawComplexControl(cc, &nativeToolOpt, p, w);
3294 else
3295 QWindowsStyle::drawComplexControl(cc, &nativeToolOpt, p, w);
3296 // if we did draw natively, don't draw custom
3297 if (nativeToolOpt.features & (QStyleOptionToolButton::Menu | QStyleOptionToolButton::HasMenu))
3298 drawMenuIndicator = false;
3299 if (nativeToolOpt.features & QStyleOptionToolButton::MenuButtonPopup && !customDropDownArrow)
3300 drawDropDown = false;
3301 } else {
3302 rule.drawRule(p, opt->rect);
3303 toolOpt.rect = rule.contentsRect(opt->rect);
3304 if (rule.hasFont)
3305 toolOpt.font = rule.font.resolve(toolOpt.font);
3306 drawControl(CE_ToolButtonLabel, &toolOpt, p, w);
3307 }
3308
3309 const QRect cr = toolOpt.rect;
3310 // Draw DropDownButton unless drawn before
3311 if (drawDropDown) {
3312 if (opt->subControls & QStyle::SC_ToolButtonMenu) {
3313 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
3314 QRect menuButtonRect = subControlRect(CC_ToolButton, opt, QStyle::SC_ToolButtonMenu, w);
3315 if (subRule.hasDrawable()) {
3316 subRule.drawRule(p, menuButtonRect);
3317 } else {
3318 toolOpt.rect = menuButtonRect;
3319 baseStyle()->drawPrimitive(PE_IndicatorButtonDropDown, &toolOpt, p, w);
3320 }
3321
3322 if (customDropDownArrow || drawMenuIndicator) {
3323 QRenderRule arrowRule = renderRule(w, opt, PseudoElement_ToolButtonMenuArrow);
3324 QRect arrowRect = arrowRule.hasGeometry()
3325 ? positionRect(w, arrowRule, PseudoElement_ToolButtonMenuArrow, menuButtonRect, toolOpt.direction)
3326 : arrowRule.contentsRect(menuButtonRect);
3327 if (arrowRule.hasDrawable()) {
3328 arrowRule.drawRule(p, arrowRect);
3329 } else {
3330 toolOpt.rect = arrowRect;
3331 baseStyle()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &toolOpt, p, w);
3332 }
3333 }
3334 }
3335 } else if (drawMenuIndicator) {
3336 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenuIndicator);
3337
3338 // content padding does not impact the indicator, so use the original rect to
3339 // calculate position of the sub element within the toplevel rule
3340 QRect r = positionRect(w, rule, subRule, PseudoElement_ToolButtonMenuIndicator, opt->rect, toolOpt.direction);
3341 if (subRule.hasDrawable()) {
3342 subRule.drawRule(p, r);
3343 } else {
3344 toolOpt.rect = r;
3345 baseStyle()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &toolOpt, p, w);
3346 }
3347 }
3348 toolOpt.rect = cr;
3349
3350 // If we don't have a custom arrow, then the arrow will have been rendered
3351 // already by the base style when drawing the label.
3352 if (customArrow) {
3353 const auto arrowElement = customArrowElement();
3354 QRenderRule subRule = renderRule(w, opt, arrowElement);
3355 QRect arrowRect = subRule.hasGeometry() ? positionRect(w, subRule, arrowElement, toolOpt.rect, toolOpt.direction)
3356 : subRule.contentsRect(toolOpt.rect);
3357
3358 switch (toolOpt.toolButtonStyle) {
3359 case Qt::ToolButtonIconOnly:
3360 break;
3361 case Qt::ToolButtonTextOnly:
3362 case Qt::ToolButtonTextBesideIcon:
3363 case Qt::ToolButtonTextUnderIcon: {
3364 // The base style needs to lay out the contents and will render the styled
3365 // arrow icons, unless the geometry is defined in the style sheet.
3366 toolOpt.text = tool->text;
3367 if (!subRule.hasGeometry())
3368 toolOpt.features |= QStyleOptionToolButton::Arrow;
3369 drawControl(CE_ToolButtonLabel, &toolOpt, p, w);
3370 if (!subRule.hasGeometry())
3371 return;
3372 break;
3373 }
3374 case Qt::ToolButtonFollowStyle:
3375 // QToolButton handles this, so must never happen
3376 Q_ASSERT(false);
3377 break;
3378 }
3379 subRule.drawRule(p, arrowRect);
3380 }
3381 return;
3382 }
3383#endif // QT_CONFIG(toolbutton)
3384 break;
3385
3386#if QT_CONFIG(scrollbar)
3387 case CC_ScrollBar:
3388 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
3389 if (!rule.hasDrawable()) {
3390 QStyleOptionSlider sbOpt(*sb);
3391 sbOpt.rect = rule.borderRect(opt->rect);
3392 rule.drawBackgroundImage(p, opt->rect);
3393 baseStyle()->drawComplexControl(cc, &sbOpt, p, w);
3394 } else {
3395 rule.drawRule(p, opt->rect);
3396 QWindowsStyle::drawComplexControl(cc, opt, p, w);
3397 }
3398 return;
3399 }
3400 break;
3401#endif // QT_CONFIG(scrollbar)
3402
3403#if QT_CONFIG(slider)
3404 case CC_Slider:
3405 if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
3406 rule.drawRule(p, opt->rect);
3407
3408 QRenderRule grooveSubRule = renderRule(w, opt, PseudoElement_SliderGroove);
3409 QRenderRule handleSubRule = renderRule(w, opt, PseudoElement_SliderHandle);
3410 if (!grooveSubRule.hasDrawable()) {
3411 QStyleOptionSlider slOpt(*slider);
3412 bool handleHasRule = handleSubRule.hasDrawable();
3413 // If the style specifies a different handler rule, draw the groove without the handler.
3414 if (handleHasRule)
3415 slOpt.subControls &= ~SC_SliderHandle;
3416 baseStyle()->drawComplexControl(cc, &slOpt, p, w);
3417 if (!handleHasRule)
3418 return;
3419 }
3420
3421 QRect gr = subControlRect(cc, opt, SC_SliderGroove, w);
3422 if (slider->subControls & SC_SliderGroove)
3423 grooveSubRule.drawRule(p, gr);
3424
3425 if (slider->subControls & SC_SliderHandle) {
3426 QRect hr = subControlRect(cc, opt, SC_SliderHandle, w);
3427
3428 if (slider->subControls & SC_SliderGroove) {
3429 const bool isHor = slider->orientation == Qt::Horizontal;
3430 QRenderRule subRule1 = renderRule(w, opt, PseudoElement_SliderSubPage);
3431 if (subRule1.hasDrawable()) {
3432 QRect r(gr.topLeft(),
3433 isHor ? QPoint(hr.x() + hr.width() / 2, gr.y() + gr.height() - 1)
3434 : QPoint(gr.x() + gr.width() - 1, hr.y() + hr.height() / 2));
3435 subRule1.drawRule(p, r);
3436 }
3437 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SliderAddPage);
3438 if (subRule2.hasDrawable()) {
3439 QRect r(isHor ? QPoint(hr.x() + hr.width() / 2 + 1, gr.y())
3440 : QPoint(gr.x(), hr.y() + hr.height() / 2 + 1),
3441 gr.bottomRight());
3442 subRule2.drawRule(p, r);
3443 }
3444 }
3445
3446 handleSubRule.drawRule(p, handleSubRule.boxRect(hr, Margin));
3447 }
3448
3449 if (slider->subControls & SC_SliderTickmarks) {
3450 // TODO...
3451 }
3452
3453 return;
3454 }
3455 break;
3456#endif // QT_CONFIG(slider)
3457
3458 case CC_MdiControls:
3459 if (hasStyleRule(w, PseudoElement_MdiCloseButton)
3460 || hasStyleRule(w, PseudoElement_MdiNormalButton)
3461 || hasStyleRule(w, PseudoElement_MdiMinButton)) {
3462 QList<QVariant> layout = rule.styleHint("button-layout"_L1).toList();
3463 if (layout.isEmpty())
3464 layout = subControlLayout("mNX");
3465
3466 QStyleOptionComplex optCopy(*opt);
3467 optCopy.subControls = { };
3468 for (const QVariant &val : std::as_const(layout)) {
3469 int layoutButton = val.toInt();
3470 if (layoutButton < PseudoElement_MdiCloseButton
3471 || layoutButton > PseudoElement_MdiNormalButton)
3472 continue;
3473 QStyle::SubControl control = knownPseudoElements[layoutButton].subControl;
3474 if (!(opt->subControls & control))
3475 continue;
3476 QRenderRule subRule = renderRule(w, opt, layoutButton);
3477 if (subRule.hasDrawable()) {
3478 QRect rect = subRule.boxRect(subControlRect(CC_MdiControls, opt, control, w), Margin);
3479 subRule.drawRule(p, rect);
3480 QIcon icon = standardIcon(subControlIcon(layoutButton), opt);
3481 icon.paint(p, subRule.contentsRect(rect), Qt::AlignCenter);
3482 } else {
3483 optCopy.subControls |= control;
3484 }
3485 }
3486
3487 if (optCopy.subControls)
3488 baseStyle()->drawComplexControl(CC_MdiControls, &optCopy, p, w);
3489 return;
3490 }
3491 break;
3492
3493 case CC_TitleBar:
3494 if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
3495 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
3496 if (!subRule.hasDrawable() && !subRule.hasBox() && !subRule.hasBorder())
3497 break;
3498 subRule.drawRule(p, opt->rect);
3499 QHash<QStyle::SubControl, QRect> layout = titleBarLayout(w, tb);
3500 const auto paintDeviceDpr = QStyleHelper::getDpr(p);
3501
3502 QRect ir;
3503 ir = layout[SC_TitleBarLabel];
3504 if (ir.isValid()) {
3505 if (subRule.hasPalette())
3506 p->setPen(subRule.palette()->foreground.color());
3507 p->fillRect(ir, Qt::white);
3508 p->drawText(ir.x(), ir.y(), ir.width(), ir.height(), Qt::AlignLeft | Qt::AlignVCenter | Qt::TextSingleLine, tb->text);
3509 }
3510
3511 ir = layout[SC_TitleBarSysMenu];
3512 if (ir.isValid()) {
3513 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_TitleBarSysMenu);
3514 subSubRule.drawRule(p, ir);
3515 ir = subSubRule.contentsRect(ir);
3516 if (!tb->icon.isNull()) {
3517 tb->icon.paint(p, ir);
3518 } else {
3519 int iconSize = pixelMetric(PM_SmallIconSize, tb, w);
3520 const QSize sz(iconSize, iconSize);
3521 const auto pm = standardIcon(SP_TitleBarMenuButton, nullptr, w)
3522 .pixmap(sz, paintDeviceDpr);
3523 drawItemPixmap(p, ir, Qt::AlignCenter, pm);
3524 }
3525 }
3526
3527 ir = layout[SC_TitleBarCloseButton];
3528 if (ir.isValid()) {
3529 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_TitleBarCloseButton);
3530 subSubRule.drawRule(p, ir);
3531
3532 const QSize sz = subSubRule.contentsRect(ir).size();
3533 const auto type = ((tb->titleBarFlags & Qt::WindowType_Mask) == Qt::Tool)
3534 ? SP_DockWidgetCloseButton : SP_TitleBarCloseButton;
3535 const auto pm = standardIcon(type, nullptr, w).pixmap(sz, paintDeviceDpr);
3536 drawItemPixmap(p, ir, Qt::AlignCenter, pm);
3537 }
3538
3539 constexpr std::array<int, 6> pes = {
3540 PseudoElement_TitleBarMaxButton,
3541 PseudoElement_TitleBarMinButton,
3542 PseudoElement_TitleBarNormalButton,
3543 PseudoElement_TitleBarShadeButton,
3544 PseudoElement_TitleBarUnshadeButton,
3545 PseudoElement_TitleBarContextHelpButton
3546 };
3547
3548 for (int pe : pes) {
3549 QStyle::SubControl sc = knownPseudoElements[pe].subControl;
3550 ir = layout[sc];
3551 if (!ir.isValid())
3552 continue;
3553 QRenderRule subSubRule = renderRule(w, opt, pe);
3554 subSubRule.drawRule(p, ir);
3555 const QSize sz = subSubRule.contentsRect(ir).size();
3556 const auto pm = standardIcon(subControlIcon(pe), nullptr, w).pixmap(sz, paintDeviceDpr);
3557 drawItemPixmap(p, ir, Qt::AlignCenter, pm);
3558 }
3559
3560 return;
3561 }
3562 break;
3563
3564
3565 default:
3566 break;
3567 }
3568
3569 baseStyle()->drawComplexControl(cc, opt, p, w);
3570}
3571
3572void QStyleSheetStyle::renderMenuItemIcon(const QStyleOptionMenuItem *mi, QPainter *p, const QWidget *w,
3573 const QRect &rect, QRenderRule &subRule) const
3574{
3575 const QIcon::Mode mode = mi->state & QStyle::State_Enabled
3576 ? (mi->state & QStyle::State_Selected ? QIcon::Active : QIcon::Normal)
3577 : QIcon::Disabled;
3578 const bool checked = mi->checkType != QStyleOptionMenuItem::NotCheckable && mi->checked;
3579 const auto iconSize = pixelMetric(PM_SmallIconSize, mi, w);
3580 const QSize sz(iconSize, iconSize);
3581 const QPixmap pixmap(mi->icon.pixmap(sz, QStyleHelper::getDpr(p), mode,
3582 checked ? QIcon::On : QIcon::Off));
3583 const int pixw = pixmap.width() / pixmap.devicePixelRatio();
3584 const int pixh = pixmap.height() / pixmap.devicePixelRatio();
3585 QRenderRule iconRule = renderRule(w, mi, PseudoElement_MenuIcon);
3586 if (!iconRule.hasGeometry()) {
3587 iconRule.geo = new QStyleSheetGeometryData(pixw, pixh, pixw, pixh, -1, -1);
3588 } else {
3589 iconRule.geo->width = pixw;
3590 iconRule.geo->height = pixh;
3591 }
3592 QRect iconRect = positionRect(w, subRule, iconRule, PseudoElement_MenuIcon, rect, mi->direction);
3593 if (mi->direction == Qt::LeftToRight)
3594 iconRect.moveLeft(iconRect.left());
3595 else
3596 iconRect.moveRight(iconRect.right());
3597 iconRule.drawRule(p, iconRect);
3598 QRect pmr(0, 0, pixw, pixh);
3599 pmr.moveCenter(iconRect.center());
3600 p->drawPixmap(pmr.topLeft(), pixmap);
3601}
3602
3603void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter *p,
3604 const QWidget *w) const
3605{
3606 RECURSION_GUARD(baseStyle()->drawControl(ce, opt, p, w); return)
3607
3608 QRenderRule rule = renderRule(w, opt);
3609 int pe1 = PseudoElement_None, pe2 = PseudoElement_None;
3610 bool fallback = false;
3611
3612 switch (ce) {
3613 case CE_ToolButtonLabel:
3614#if QT_CONFIG(toolbutton)
3615 if (const QStyleOptionToolButton *btn = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
3616 if (rule.hasBox() || btn->features & QStyleOptionToolButton::Arrow) {
3617 QWindowsStyle::drawControl(ce, opt, p, w);
3618 } else {
3619 QStyleOptionToolButton butOpt(*btn);
3620 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3621 baseStyle()->drawControl(ce, &butOpt, p, w);
3622 }
3623 return;
3624 }
3625#endif // QT_CONFIG(toolbutton)
3626 break;
3627
3628 case CE_FocusFrame:
3629 if (!rule.hasNativeBorder()) {
3630 rule.drawBorder(p, opt->rect);
3631 return;
3632 }
3633 break;
3634
3635 case CE_PushButton:
3636 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3637 if (rule.hasDrawable() || rule.hasBox() || rule.hasPosition() || rule.hasPalette() ||
3638 ((btn->features & QStyleOptionButton::HasMenu) && hasStyleRule(w, PseudoElement_PushButtonMenuIndicator))) {
3639 ParentStyle::drawControl(ce, opt, p, w);
3640 return;
3641 }
3642 }
3643 break;
3644 case CE_PushButtonBevel:
3645 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3646 QStyleOptionButton btnOpt(*btn);
3647 btnOpt.rect = rule.borderRect(opt->rect);
3648 if (rule.hasNativeBorder()) {
3649 rule.drawBackgroundImage(p, btnOpt.rect);
3650 rule.configurePalette(&btnOpt.palette, QPalette::ButtonText, QPalette::Button);
3651 bool customMenu = (btn->features & QStyleOptionButton::HasMenu
3652 && hasStyleRule(w, PseudoElement_PushButtonMenuIndicator));
3653 if (customMenu)
3654 btnOpt.features &= ~QStyleOptionButton::HasMenu;
3655 if (rule.baseStyleCanDraw()) {
3656 baseStyle()->drawControl(ce, &btnOpt, p, w);
3657 } else {
3658 QWindowsStyle::drawControl(ce, &btnOpt, p, w);
3659 }
3660 rule.drawImage(p, rule.contentsRect(opt->rect));
3661 if (!customMenu)
3662 return;
3663 } else {
3664 rule.drawRule(p, opt->rect);
3665 }
3666
3667 if (btn->features & QStyleOptionButton::HasMenu) {
3668 QRenderRule subRule = renderRule(w, opt, PseudoElement_PushButtonMenuIndicator);
3669 QRect ir = positionRect(w, rule, subRule, PseudoElement_PushButtonMenuIndicator,
3670 baseStyle()->subElementRect(SE_PushButtonBevel, btn, w), opt->direction);
3671 if (subRule.hasDrawable()) {
3672 subRule.drawRule(p, ir);
3673 } else {
3674 btnOpt.rect = ir;
3675 baseStyle()->drawPrimitive(PE_IndicatorArrowDown, &btnOpt, p, w);
3676 }
3677 }
3678 }
3679 return;
3680
3681 case CE_PushButtonLabel:
3682 if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3683 QStyleOptionButton butOpt(*button);
3684 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3685
3686 const QFont oldFont = p->font();
3687 if (rule.hasFont)
3688 p->setFont(rule.font.resolve(p->font()));
3689
3690 if (rule.hasPosition() || rule.hasIcon()) {
3691 uint tf = Qt::TextShowMnemonic;
3692 QRect textRect = button->rect;
3693
3694 const uint horizontalAlignMask = Qt::AlignHCenter | Qt::AlignLeft | Qt::AlignRight;
3695 const uint verticalAlignMask = Qt::AlignVCenter | Qt::AlignTop | Qt::AlignBottom;
3696
3697 if (rule.hasPosition() && rule.position()->textAlignment != 0) {
3698 Qt::Alignment textAlignment = rule.position()->textAlignment;
3699 tf |= (textAlignment & verticalAlignMask) ? (textAlignment & verticalAlignMask) : Qt::AlignVCenter;
3700 tf |= (textAlignment & horizontalAlignMask) ? (textAlignment & horizontalAlignMask) : Qt::AlignHCenter;
3701 if (!styleHint(SH_UnderlineShortcut, button, w))
3702 tf |= Qt::TextHideMnemonic;
3703 } else {
3704 tf |= Qt::AlignVCenter | Qt::AlignHCenter;
3705 }
3706
3707 QIcon icon = rule.hasIcon() ? rule.icon()->icon : button->icon;
3708 if (!icon.isNull()) {
3709 //Group both icon and text
3710 QRect iconRect;
3711 QIcon::Mode mode = button->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
3712 if (mode == QIcon::Normal && button->state & State_HasFocus)
3713 mode = QIcon::Active;
3714 QIcon::State state = QIcon::Off;
3715 if (button->state & State_On)
3716 state = QIcon::On;
3717
3718 const auto paintDeviceDpr = QStyleHelper::getDpr(p);
3719 QPixmap pixmap = icon.pixmap(button->iconSize, paintDeviceDpr, mode, state);
3720 int pixmapWidth = pixmap.width() / pixmap.devicePixelRatio();
3721 int pixmapHeight = pixmap.height() / pixmap.devicePixelRatio();
3722 int labelWidth = pixmapWidth;
3723 int labelHeight = pixmapHeight;
3724 int iconSpacing = 4;//### 4 is currently hardcoded in QPushButton::sizeHint()
3725 int textWidth = button->fontMetrics.boundingRect(opt->rect, tf, button->text).width();
3726 if (!button->text.isEmpty())
3727 labelWidth += (textWidth + iconSpacing);
3728
3729 //Determine label alignment:
3730 if (tf & Qt::AlignLeft) { /*left*/
3731 iconRect = QRect(textRect.x(), textRect.y() + (textRect.height() - labelHeight) / 2,
3732 pixmapWidth, pixmapHeight);
3733 } else if (tf & Qt::AlignHCenter) { /* center */
3734 iconRect = QRect(textRect.x() + (textRect.width() - labelWidth) / 2,
3735 textRect.y() + (textRect.height() - labelHeight) / 2,
3736 pixmapWidth, pixmapHeight);
3737 } else { /*right*/
3738 iconRect = QRect(textRect.x() + textRect.width() - labelWidth,
3739 textRect.y() + (textRect.height() - labelHeight) / 2,
3740 pixmapWidth, pixmapHeight);
3741 }
3742
3743 iconRect = visualRect(button->direction, textRect, iconRect);
3744
3745 // Left align, adjust the text-rect according to the icon instead
3746 tf &= ~horizontalAlignMask;
3747 tf |= Qt::AlignLeft;
3748
3749 if (button->direction == Qt::RightToLeft)
3750 textRect.setRight(iconRect.left() - iconSpacing);
3751 else
3752 textRect.setLeft(iconRect.left() + iconRect.width() + iconSpacing);
3753
3754 if (button->state & (State_On | State_Sunken))
3755 iconRect.translate(pixelMetric(PM_ButtonShiftHorizontal, opt, w),
3756 pixelMetric(PM_ButtonShiftVertical, opt, w));
3757 p->drawPixmap(iconRect, pixmap);
3758 }
3759
3760 if (button->state & (State_On | State_Sunken))
3761 textRect.translate(pixelMetric(PM_ButtonShiftHorizontal, opt, w),
3762 pixelMetric(PM_ButtonShiftVertical, opt, w));
3763
3764 if (button->features & QStyleOptionButton::HasMenu) {
3765 int indicatorSize = pixelMetric(PM_MenuButtonIndicator, button, w);
3766 if (button->direction == Qt::LeftToRight)
3767 textRect = textRect.adjusted(0, 0, -indicatorSize, 0);
3768 else
3769 textRect = textRect.adjusted(indicatorSize, 0, 0, 0);
3770 }
3771 drawItemText(p, textRect, tf, butOpt.palette, (button->state & State_Enabled),
3772 button->text, QPalette::ButtonText);
3773 } else {
3774 ParentStyle::drawControl(ce, &butOpt, p, w);
3775 }
3776
3777 if (rule.hasFont)
3778 p->setFont(oldFont);
3779 }
3780 return;
3781
3782 case CE_RadioButton:
3783 case CE_CheckBox:
3784 if (rule.hasBox() || !rule.hasNativeBorder() || rule.hasDrawable() || hasStyleRule(w, PseudoElement_Indicator)) {
3785 rule.drawRule(p, opt->rect);
3786 ParentStyle::drawControl(ce, opt, p, w);
3787 return;
3788 } else if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3789 QStyleOptionButton butOpt(*btn);
3790 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3791 baseStyle()->drawControl(ce, &butOpt, p, w);
3792 return;
3793 }
3794 break;
3795 case CE_RadioButtonLabel:
3796 case CE_CheckBoxLabel:
3797 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
3798 QStyleOptionButton butOpt(*btn);
3799 rule.configurePalette(&butOpt.palette, QPalette::ButtonText, QPalette::Button);
3800 ParentStyle::drawControl(ce, &butOpt, p, w);
3801 }
3802 return;
3803
3804 case CE_Splitter:
3805 pe1 = PseudoElement_SplitterHandle;
3806 break;
3807
3808 case CE_ToolBar:
3809 if (rule.hasBackground()) {
3810 rule.drawBackground(p, opt->rect);
3811 }
3812 if (rule.hasBorder()) {
3813 rule.drawBorder(p, rule.borderRect(opt->rect));
3814 } else {
3815#if QT_CONFIG(toolbar)
3816 if (const QStyleOptionToolBar *tb = qstyleoption_cast<const QStyleOptionToolBar *>(opt)) {
3817 QStyleOptionToolBar newTb(*tb);
3818 newTb.rect = rule.borderRect(opt->rect);
3819 baseStyle()->drawControl(ce, &newTb, p, w);
3820 }
3821#endif // QT_CONFIG(toolbar)
3822 }
3823 return;
3824
3825 case CE_MenuEmptyArea:
3826 case CE_MenuBarEmptyArea:
3827 if (rule.hasDrawable()) {
3828 // Drawn by PE_Widget
3829 return;
3830 }
3831 break;
3832
3833 case CE_MenuTearoff:
3834 case CE_MenuScroller:
3835 if (const QStyleOptionMenuItem *m = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
3836 QStyleOptionMenuItem mi(*m);
3837 int pe = ce == CE_MenuTearoff ? PseudoElement_MenuTearoff : PseudoElement_MenuScroller;
3838 QRenderRule subRule = renderRule(w, opt, pe);
3839 mi.rect = subRule.contentsRect(opt->rect);
3840 rule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3841 subRule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3842
3843 if (subRule.hasDrawable()) {
3844 subRule.drawRule(p, opt->rect);
3845 } else {
3846 baseStyle()->drawControl(ce, &mi, p, w);
3847 }
3848 }
3849 return;
3850
3851 case CE_MenuItem:
3852 if (const QStyleOptionMenuItem *m = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
3853 QStyleOptionMenuItem mi(*m);
3854
3855 int pseudo = (mi.menuItemType == QStyleOptionMenuItem::Separator) ? PseudoElement_MenuSeparator : PseudoElement_Item;
3856 QRenderRule subRule = renderRule(w, opt, pseudo);
3857 mi.rect = subRule.contentsRect(opt->rect);
3858 rule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3859 rule.configurePalette(&mi.palette, QPalette::HighlightedText, QPalette::Highlight);
3860 subRule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3861 subRule.configurePalette(&mi.palette, QPalette::HighlightedText, QPalette::Highlight);
3862 QFont oldFont = p->font();
3863 if (subRule.hasFont)
3864 p->setFont(subRule.font.resolve(mi.font));
3865 else
3866 p->setFont(mi.font);
3867
3868 // We fall back to drawing with the style sheet code whenever at least one of the
3869 // items are styled in an incompatible way, such as having a background image.
3870 QRenderRule allRules = renderRule(w, PseudoElement_Item, PseudoClass_Any);
3871
3872 if ((pseudo == PseudoElement_MenuSeparator) && subRule.hasDrawable()) {
3873 subRule.drawRule(p, opt->rect);
3874 } else if ((pseudo == PseudoElement_Item)
3875 && (allRules.hasBox() || allRules.hasBorder() || subRule.hasFont
3876 || (allRules.background() && !allRules.background()->pixmap.isNull()))) {
3877 subRule.drawRule(p, opt->rect);
3878 if (subRule.hasBackground()) {
3879 mi.palette.setBrush(QPalette::Highlight, Qt::NoBrush);
3880 mi.palette.setBrush(QPalette::Button, Qt::NoBrush);
3881 } else {
3882 mi.palette.setBrush(QPalette::Highlight, mi.palette.brush(QPalette::Button));
3883 }
3884 mi.palette.setBrush(QPalette::HighlightedText, mi.palette.brush(QPalette::ButtonText));
3885
3886 bool drawCheckMark = mi.menuHasCheckableItems;
3887#if QT_CONFIG(combobox)
3888 if (qobject_cast<const QComboBox *>(w))
3889 drawCheckMark = false; // ignore the checkmarks provided by the QComboMenuDelegate
3890#endif
3891 int textRectOffset = m->maxIconWidth;
3892 if (!mi.icon.isNull()) {
3893 renderMenuItemIcon(&mi, p, w, opt->rect, subRule);
3894 } else if (drawCheckMark) {
3895 const bool checkable = mi.checkType != QStyleOptionMenuItem::NotCheckable;
3896 const bool checked = checkable ? mi.checked : false;
3897
3898 const QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
3899 const QRect cmRect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
3900 if (checkable && (subSubRule.hasDrawable() || checked)) {
3901 QStyleOptionMenuItem newMi = mi;
3902 if (opt->state & QStyle::State_Enabled)
3903 newMi.state |= State_Enabled;
3904 if (mi.checked)
3905 newMi.state |= State_On;
3906 newMi.rect = cmRect;
3907 drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w);
3908 }
3909 textRectOffset = std::max(m->maxIconWidth, cmRect.width());
3910 }
3911
3912 QRect textRect = subRule.contentsRect(opt->rect);
3913 textRect.setLeft(textRect.left() + textRectOffset);
3914 textRect.setWidth(textRect.width() - mi.reservedShortcutWidth);
3915 const QRect vTextRect = visualRect(opt->direction, m->rect, textRect);
3916
3917 QStringView s(mi.text);
3918 p->setPen(mi.palette.buttonText().color());
3919 if (!s.isEmpty()) {
3920 int text_flags = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextShowMnemonic | Qt::TextDontClip | Qt::TextSingleLine;
3921 if (!styleHint(SH_UnderlineShortcut, &mi, w))
3922 text_flags |= Qt::TextHideMnemonic;
3923 qsizetype t = s.indexOf(u'\t');
3924 if (t >= 0) {
3925 QRect vShortcutRect = visualRect(opt->direction, mi.rect,
3926 QRect(textRect.topRight(), QPoint(mi.rect.right(), textRect.bottom())));
3927 p->drawText(vShortcutRect, text_flags, s.mid(t + 1).toString());
3928 s = s.left(t);
3929 }
3930 p->drawText(vTextRect, text_flags, s.left(t).toString());
3931 }
3932
3933 if (mi.menuItemType == QStyleOptionMenuItem::SubMenu) {// draw sub menu arrow
3934 PrimitiveElement arrow = (opt->direction == Qt::RightToLeft) ? PE_IndicatorArrowLeft : PE_IndicatorArrowRight;
3935 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_MenuRightArrow);
3936 mi.rect = positionRect(w, subRule, subRule2, PseudoElement_MenuRightArrow, opt->rect, mi.direction);
3937 drawPrimitive(arrow, &mi, p, w);
3938 }
3939 } else if (!mi.icon.isNull() && hasStyleRule(w, PseudoElement_MenuIcon)) {
3940 // we wouldn't be here if the item itself would be styled, so now we only want
3941 // the text from the default style, and then draw the icon ourselves.
3942 QStyleOptionMenuItem newMi = mi;
3943 newMi.icon = {};
3944 newMi.checkType = QStyleOptionMenuItem::NotCheckable;
3945 if (rule.baseStyleCanDraw() && subRule.baseStyleCanDraw())
3946 baseStyle()->drawControl(ce, &newMi, p, w);
3947 else
3948 ParentStyle::drawControl(ce, &newMi, p, w);
3949 renderMenuItemIcon(&mi, p, w, opt->rect, subRule);
3950 } else if (hasStyleRule(w, PseudoElement_MenuCheckMark) || hasStyleRule(w, PseudoElement_MenuRightArrow)) {
3951 QWindowsStyle::drawControl(ce, &mi, p, w);
3952 if (mi.checkType != QStyleOptionMenuItem::NotCheckable && !mi.checked) {
3953 // We have a style defined, but QWindowsStyle won't draw anything if not checked.
3954 // So we mimic what QWindowsStyle would do.
3955 int checkcol = qMax<int>(mi.maxIconWidth, QWindowsStylePrivate::windowsCheckMarkWidth);
3956 QRect vCheckRect = visualRect(opt->direction, mi.rect, QRect(mi.rect.x(), mi.rect.y(), checkcol, mi.rect.height()));
3957 if (mi.state.testFlag(State_Enabled) && mi.state.testFlag(State_Selected)) {
3958 qDrawShadePanel(p, vCheckRect, mi.palette, true, 1, &mi.palette.brush(QPalette::Button));
3959 } else {
3960 QBrush fill(mi.palette.light().color(), Qt::Dense4Pattern);
3961 qDrawShadePanel(p, vCheckRect, mi.palette, true, 1, &fill);
3962 }
3963 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
3964 if (subSubRule.hasDrawable()) {
3965 QStyleOptionMenuItem newMi(mi);
3966 newMi.rect = visualRect(opt->direction, mi.rect, QRect(mi.rect.x() + QWindowsStylePrivate::windowsItemFrame,
3967 mi.rect.y() + QWindowsStylePrivate::windowsItemFrame,
3968 checkcol - 2 * QWindowsStylePrivate::windowsItemFrame,
3969 mi.rect.height() - 2 * QWindowsStylePrivate::windowsItemFrame));
3970 drawPrimitive(PE_IndicatorMenuCheckMark, &newMi, p, w);
3971 }
3972 }
3973 } else {
3974 if (rule.hasDrawable() && !subRule.hasDrawable() && !(opt->state & QStyle::State_Selected)) {
3975 mi.palette.setColor(QPalette::Window, Qt::transparent);
3976 mi.palette.setColor(QPalette::Button, Qt::transparent);
3977 }
3978 if (rule.baseStyleCanDraw() && subRule.baseStyleCanDraw()) {
3979 baseStyle()->drawControl(ce, &mi, p, w);
3980 } else {
3981 ParentStyle::drawControl(ce, &mi, p, w);
3982 }
3983 }
3984
3985 p->setFont(oldFont);
3986
3987 return;
3988 }
3989 return;
3990
3991 case CE_MenuBarItem:
3992 if (const QStyleOptionMenuItem *m = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
3993 QStyleOptionMenuItem mi(*m);
3994 QRenderRule subRule = renderRule(w, opt, PseudoElement_Item);
3995 mi.rect = subRule.contentsRect(opt->rect);
3996 rule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3997 subRule.configurePalette(&mi.palette, QPalette::ButtonText, QPalette::Button);
3998
3999 if (subRule.hasDrawable()) {
4000 subRule.drawRule(p, opt->rect);
4001 QCommonStyle::drawControl(ce, &mi, p, w); // deliberate bypass of the base
4002 } else {
4003 if (rule.hasDrawable() && !(opt->state & QStyle::State_Selected)) {
4004 // So that the menu bar background is not hidden by the items
4005 mi.palette.setColor(QPalette::Window, Qt::transparent);
4006 mi.palette.setColor(QPalette::Button, Qt::transparent);
4007 }
4008 baseStyle()->drawControl(ce, &mi, p, w);
4009 }
4010 }
4011 return;
4012
4013#if QT_CONFIG(combobox)
4014 case CE_ComboBoxLabel:
4015 if (!rule.hasDrawable())
4016 break;
4017 if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
4018 QRect editRect = subControlRect(CC_ComboBox, cb, SC_ComboBoxEditField, w);
4019 p->save();
4020 p->setClipRect(editRect);
4021 if (!cb->currentIcon.isNull()) {
4022 int spacing = rule.hasBox() ? rule.box()->spacing : -1;
4023 if (spacing == -1)
4024 spacing = 6;
4025 QIcon::Mode mode = cb->state & State_Enabled ? QIcon::Normal : QIcon::Disabled;
4026 const auto paintDeviceDpr = QStyleHelper::getDpr(p);
4027 QPixmap pixmap = cb->currentIcon.pixmap(cb->iconSize, paintDeviceDpr, mode);
4028 QRect iconRect(editRect);
4029 iconRect.setWidth(cb->iconSize.width());
4030 iconRect = alignedRect(cb->direction,
4031 Qt::AlignLeft | Qt::AlignVCenter,
4032 iconRect.size(), editRect);
4033 drawItemPixmap(p, iconRect, Qt::AlignCenter, pixmap);
4034
4035 if (cb->direction == Qt::RightToLeft)
4036 editRect.translate(-spacing - cb->iconSize.width(), 0);
4037 else
4038 editRect.translate(cb->iconSize.width() + spacing, 0);
4039 }
4040 if (!cb->currentText.isEmpty() && !cb->editable) {
4041 QPalette styledPalette(cb->palette);
4042 rule.configurePalette(&styledPalette, QPalette::Text, QPalette::Base);
4043 drawItemText(p, editRect.adjusted(0, 0, 0, 0), cb->textAlignment, styledPalette,
4044 cb->state & State_Enabled, cb->currentText, QPalette::Text);
4045 }
4046 p->restore();
4047 return;
4048 }
4049 break;
4050#endif // QT_CONFIG(combobox)
4051
4052 case CE_Header:
4053 if (hasStyleRule(w, PseudoElement_HeaderViewUpArrow)
4054 || hasStyleRule(w, PseudoElement_HeaderViewDownArrow)) {
4055 ParentStyle::drawControl(ce, opt, p, w);
4056 return;
4057 }
4058 if (hasStyleRule(w, PseudoElement_HeaderViewSection)) {
4059 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
4060 if (!subRule.hasNativeBorder() || !subRule.baseStyleCanDraw()
4061 || subRule.hasBackground() || subRule.hasPalette() || subRule.hasFont || subRule.hasBorder()) {
4062 ParentStyle::drawControl(ce, opt, p, w);
4063 return;
4064 }
4065 }
4066 break;
4067 case CE_HeaderSection:
4068 if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
4069 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
4070 if (subRule.hasNativeBorder()) {
4071 QStyleOptionHeader hdr(*header);
4072 subRule.configurePalette(&hdr.palette, QPalette::ButtonText, QPalette::Button);
4073
4074 if (subRule.baseStyleCanDraw()) {
4075 baseStyle()->drawControl(CE_HeaderSection, &hdr, p, w);
4076 } else {
4077 QWindowsStyle::drawControl(CE_HeaderSection, &hdr, p, w);
4078 }
4079 } else {
4080 subRule.drawRule(p, opt->rect);
4081 }
4082 return;
4083 }
4084 break;
4085
4086 case CE_HeaderLabel:
4087 if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
4088 QStyleOptionHeaderV2 hdr;
4089 QStyleOptionHeader &v1Copy = hdr;
4090 if (auto v2 = qstyleoption_cast<const QStyleOptionHeaderV2 *>(opt))
4091 hdr = *v2;
4092 else
4093 v1Copy = *header;
4094 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
4095 if (hasStyleRule(w, PseudoElement_HeaderViewUpArrow)
4096 || hasStyleRule(w, PseudoElement_HeaderViewDownArrow)) {
4097 if (hdr.sortIndicator != QStyleOptionHeader::None) {
4098 const QRect arrowRect = subElementRect(SE_HeaderArrow, opt, w);
4099 if (hdr.orientation == Qt::Horizontal)
4100 hdr.rect.setWidth(hdr.rect.width() - arrowRect.width());
4101 else
4102 hdr.rect.setHeight(hdr.rect.height() - arrowRect.height());
4103 }
4104 }
4105 subRule.configurePalette(&hdr.palette, QPalette::ButtonText, QPalette::Button);
4106 if (subRule.hasFont) {
4107 QFont oldFont = p->font();
4108 p->setFont(subRule.font.resolve(p->font()));
4109 ParentStyle::drawControl(ce, &hdr, p, w);
4110 p->setFont(oldFont);
4111 } else {
4112 baseStyle()->drawControl(ce, &hdr, p, w);
4113 }
4114 return;
4115 }
4116 break;
4117
4118 case CE_HeaderEmptyArea:
4119 if (rule.hasDrawable()) {
4120 return;
4121 }
4122 break;
4123
4124 case CE_ProgressBar:
4125 QWindowsStyle::drawControl(ce, opt, p, w);
4126 return;
4127
4128 case CE_ProgressBarGroove:
4129 if (!rule.hasNativeBorder()) {
4130 rule.drawRule(p, rule.boxRect(opt->rect, Margin));
4131 return;
4132 }
4133 break;
4134
4135 case CE_ProgressBarContents: {
4136 QRenderRule subRule = renderRule(w, opt, PseudoElement_ProgressBarChunk);
4137 if (subRule.hasDrawable()) {
4138 if (const QStyleOptionProgressBar *pb = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
4139 p->save();
4140 p->setClipRect(pb->rect);
4141
4142 qint64 minimum = qint64(pb->minimum);
4143 qint64 maximum = qint64(pb->maximum);
4144 qint64 progress = qint64(pb->progress);
4145 bool vertical = !(pb->state & QStyle::State_Horizontal);
4146 bool inverted = pb->invertedAppearance;
4147
4148 QTransform m;
4149 QRect rect = pb->rect;
4150 if (vertical) {
4151 rect = QRect(rect.y(), rect.x(), rect.height(), rect.width());
4152 m.rotate(90);
4153 m.translate(0, -(rect.height() + rect.y()*2));
4154 }
4155
4156 bool reverse = ((!vertical && (pb->direction == Qt::RightToLeft)) || vertical);
4157 if (inverted)
4158 reverse = !reverse;
4159 const bool indeterminate = pb->minimum == pb->maximum;
4160 const auto fillRatio = indeterminate ? 0.50 : double(progress - minimum) / (maximum - minimum);
4161 const auto fillWidth = static_cast<int>(rect.width() * fillRatio);
4162 int chunkWidth = fillWidth;
4163 if (subRule.hasContentsSize()) {
4164 QSize sz = subRule.size();
4165 chunkWidth = (opt->state & QStyle::State_Horizontal) ? sz.width() : sz.height();
4166 }
4167
4168 QRect r = rect;
4169#if QT_CONFIG(animation)
4170 Q_D(const QWindowsStyle);
4171#endif
4172 if (pb->minimum == 0 && pb->maximum == 0) {
4173 int chunkCount = fillWidth/chunkWidth;
4174 int offset = 0;
4175#if QT_CONFIG(animation)
4176 if (QProgressStyleAnimation *animation = qobject_cast<QProgressStyleAnimation*>(d->animation(opt->styleObject)))
4177 offset = animation->animationStep() * 8 % rect.width();
4178 else
4179 d->startAnimation(new QProgressStyleAnimation(d->animationFps, opt->styleObject));
4180#endif
4181 int x = reverse ? r.left() + r.width() - offset - chunkWidth : r.x() + offset;
4182 while (chunkCount > 0) {
4183 r.setRect(x, rect.y(), chunkWidth, rect.height());
4184 r = m.mapRect(QRectF(r)).toRect();
4185 subRule.drawRule(p, r);
4186 x += reverse ? -chunkWidth : chunkWidth;
4187 if (reverse ? x < rect.left() : x > rect.right())
4188 break;
4189 --chunkCount;
4190 }
4191
4192 r = rect;
4193 x = reverse ? r.right() - (r.left() - x - chunkWidth)
4194 : r.left() + (x - r.right() - chunkWidth);
4195 while (chunkCount > 0) {
4196 r.setRect(x, rect.y(), chunkWidth, rect.height());
4197 r = m.mapRect(QRectF(r)).toRect();
4198 subRule.drawRule(p, r);
4199 x += reverse ? -chunkWidth : chunkWidth;
4200 --chunkCount;
4201 };
4202 } else if (chunkWidth > 0) {
4203 const auto ceil = [](qreal x) { return int(x) + (x > 0 && x != int(x)); };
4204 const int chunkCount = ceil(qreal(fillWidth)/chunkWidth);
4205 int x = reverse ? r.left() + r.width() - chunkWidth : r.x();
4206
4207 for (int i = 0; i < chunkCount; ++i) {
4208 r.setRect(x, rect.y(), chunkWidth, rect.height());
4209 r = m.mapRect(QRectF(r)).toRect();
4210 subRule.drawRule(p, r);
4211 x += reverse ? -chunkWidth : chunkWidth;
4212 }
4213#if QT_CONFIG(animation)
4214 d->stopAnimation(opt->styleObject);
4215#endif
4216 }
4217
4218 p->restore();
4219 return;
4220 }
4221 }
4222 }
4223 break;
4224
4225 case CE_ProgressBarLabel:
4226 if (const QStyleOptionProgressBar *pb = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
4227 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_ProgressBarChunk)) {
4228 drawItemText(p, pb->rect, pb->textAlignment | Qt::TextSingleLine, pb->palette,
4229 pb->state & State_Enabled, pb->text, QPalette::Text);
4230 } else {
4231 QStyleOptionProgressBar pbCopy(*pb);
4232 rule.configurePalette(&pbCopy.palette, QPalette::HighlightedText, QPalette::Highlight);
4233 baseStyle()->drawControl(ce, &pbCopy, p, w);
4234 }
4235 return;
4236 }
4237 break;
4238
4239 case CE_SizeGrip:
4240 if (const QStyleOptionSizeGrip *sgOpt = qstyleoption_cast<const QStyleOptionSizeGrip *>(opt)) {
4241 if (rule.hasDrawable()) {
4242 rule.drawFrame(p, opt->rect);
4243 p->save();
4244 static constexpr int rotation[] = { 180, 270, 90, 0 };
4245 if (rotation[sgOpt->corner]) {
4246 p->translate(opt->rect.center());
4247 p->rotate(rotation[sgOpt->corner]);
4248 p->translate(-opt->rect.center());
4249 }
4250 rule.drawImage(p, opt->rect);
4251 p->restore();
4252 } else {
4253 QStyleOptionSizeGrip sg(*sgOpt);
4254 sg.rect = rule.contentsRect(opt->rect);
4255 baseStyle()->drawControl(CE_SizeGrip, &sg, p, w);
4256 }
4257 return;
4258 }
4259 break;
4260
4261 case CE_ToolBoxTab:
4262 QWindowsStyle::drawControl(ce, opt, p, w);
4263 return;
4264
4265 case CE_ToolBoxTabShape: {
4266 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolBoxTab);
4267 if (subRule.hasDrawable()) {
4268 subRule.drawRule(p, opt->rect);
4269 return;
4270 }
4271 }
4272 break;
4273
4274 case CE_ToolBoxTabLabel:
4275 if (const QStyleOptionToolBox *box = qstyleoption_cast<const QStyleOptionToolBox *>(opt)) {
4276 QStyleOptionToolBox boxCopy(*box);
4277 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolBoxTab);
4278 subRule.configurePalette(&boxCopy.palette, QPalette::ButtonText, QPalette::Button);
4279 QFont oldFont = p->font();
4280 if (subRule.hasFont)
4281 p->setFont(subRule.font.resolve(p->font()));
4282 boxCopy.rect = subRule.contentsRect(opt->rect);
4283 if (subRule.hasImage()) {
4284 // the image is already drawn with CE_ToolBoxTabShape, adjust rect here
4285 const int iconExtent = proxy()->pixelMetric(QStyle::PM_SmallIconSize, box, w);
4286 boxCopy.rect.setLeft(boxCopy.rect.left() + iconExtent);
4287 }
4288 QWindowsStyle::drawControl(ce, &boxCopy, p , w);
4289 if (subRule.hasFont)
4290 p->setFont(oldFont);
4291 return;
4292 }
4293 break;
4294
4295 case CE_ScrollBarAddPage:
4296 pe1 = PseudoElement_ScrollBarAddPage;
4297 break;
4298
4299 case CE_ScrollBarSubPage:
4300 pe1 = PseudoElement_ScrollBarSubPage;
4301 break;
4302
4303 case CE_ScrollBarAddLine:
4304 pe1 = PseudoElement_ScrollBarAddLine;
4305 pe2 = (opt->state & QStyle::State_Horizontal) ? PseudoElement_ScrollBarRightArrow : PseudoElement_ScrollBarDownArrow;
4306 fallback = true;
4307 break;
4308
4309 case CE_ScrollBarSubLine:
4310 pe1 = PseudoElement_ScrollBarSubLine;
4311 pe2 = (opt->state & QStyle::State_Horizontal) ? PseudoElement_ScrollBarLeftArrow : PseudoElement_ScrollBarUpArrow;
4312 fallback = true;
4313 break;
4314
4315 case CE_ScrollBarFirst:
4316 pe1 = PseudoElement_ScrollBarFirst;
4317 break;
4318
4319 case CE_ScrollBarLast:
4320 pe1 = PseudoElement_ScrollBarLast;
4321 break;
4322
4323 case CE_ScrollBarSlider:
4324 pe1 = PseudoElement_ScrollBarSlider;
4325 fallback = true;
4326 break;
4327
4328#if QT_CONFIG(itemviews)
4329 case CE_ItemViewItem:
4330 if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
4331 QRenderRule subRule = renderRule(w, opt, PseudoElement_ViewItem);
4332 QStyleOptionViewItem optCopy(*vopt);
4333 if (subRule.hasDrawable()) {
4334 subRule.configurePalette(&optCopy.palette, vopt->state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text,
4335 vopt->state & QStyle::State_Selected ? QPalette::Highlight : QPalette::Base);
4336 QWindowsStyle::drawControl(ce, &optCopy, p, w);
4337 } else {
4338 p->save();
4339 if (hasStyleRule(w, PseudoElement_Indicator)) {
4340 // there is a rule for the indicator, but no rule for the item itself (otherwise
4341 // the previous path would have been taken); only draw the indicator using the
4342 // rule (via QWindows/QCommonStyle), then let the base style handle the rest.
4343 QStyleOptionViewItem optIndicator(*vopt);
4344 subRule.configurePalette(&optIndicator.palette,
4345 vopt->state & QStyle::State_Selected
4346 ? QPalette::HighlightedText
4347 : QPalette::Text,
4348 vopt->state & QStyle::State_Selected
4349 ? QPalette::Highlight
4350 : QPalette::Base);
4351 // only draw the indicator; no text, icon or background
4352 optIndicator.backgroundBrush = Qt::NoBrush; // no background
4353 optIndicator.text.clear();
4354 optIndicator.icon = QIcon();
4355 QWindowsStyle::drawControl(ce, &optIndicator, p, w);
4356
4357 // Now draw text, background,icon, and highlight, but not the indicator with
4358 // the base style. Since we can't turn off HasCheckIndicator to prevent the base
4359 // style from drawing the check indicator again (it would change how the item
4360 // gets laid out) we have to clip the indicator that's already been painted.
4361 const QRect crStyle = subElementRect(QStyle::SE_ItemViewItemCheckIndicator,
4362 &optIndicator, w);
4363 const QRect crBase = baseStyle()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator,
4364 &optIndicator, w);
4365 const QRegion clipRegion = QRegion(p->hasClipping() ? p->clipRegion()
4366 : QRegion(optIndicator.rect))
4367 - crStyle.united(crBase);
4368 p->setClipRegion(clipRegion);
4369 }
4370 subRule.configurePalette(&optCopy.palette, QPalette::Text, QPalette::NoRole);
4371 QStyleSheetProxySaver proxySaver(this);
4372 baseStyle()->drawControl(ce, &optCopy, p, w);
4373 p->restore();
4374 }
4375 return;
4376 }
4377 break;
4378#endif // QT_CONFIG(itemviews)
4379
4380#if QT_CONFIG(tabbar)
4381 case CE_TabBarTab:
4382 if (hasStyleRule(w, PseudoElement_TabBarTab)) {
4383 QWindowsStyle::drawControl(ce, opt, p, w);
4384 return;
4385 }
4386 break;
4387
4388 case CE_TabBarTabLabel:
4389 case CE_TabBarTabShape:
4390 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
4391 const auto foregroundRole = w ? w->foregroundRole() : QPalette::WindowText;
4392 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
4393 QRect r = positionRect(w, subRule, PseudoElement_TabBarTab, opt->rect, opt->direction);
4394 if (ce == CE_TabBarTabShape && subRule.hasDrawable() && tab->shape < QTabBar::TriangularNorth) {
4395 subRule.drawRule(p, r);
4396 return;
4397 }
4398 QStyleOptionTab tabCopy(*tab);
4399 subRule.configurePalette(&tabCopy.palette, foregroundRole, QPalette::Base);
4400 QFont oldFont = p->font();
4401 if (subRule.hasFont)
4402 p->setFont(subRule.font.resolve(p->font()));
4403 if (subRule.hasBox() || !subRule.hasNativeBorder()) {
4404 tabCopy.rect = ce == CE_TabBarTabShape ? subRule.borderRect(r)
4405 : subRule.contentsRect(r);
4406 QWindowsStyle::drawControl(ce, &tabCopy, p, w);
4407 } else {
4408 baseStyle()->drawControl(ce, &tabCopy, p, w);
4409 }
4410 if (subRule.hasFont)
4411 p->setFont(oldFont);
4412
4413 return;
4414 }
4415 break;
4416#endif // QT_CONFIG(tabbar)
4417
4418 case CE_ColumnViewGrip:
4419 if (rule.hasDrawable()) {
4420 rule.drawRule(p, opt->rect);
4421 return;
4422 }
4423 break;
4424
4425 case CE_DockWidgetTitle:
4426 if (const QStyleOptionDockWidget *dwOpt = qstyleoption_cast<const QStyleOptionDockWidget *>(opt)) {
4427 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetTitle);
4428 if (!subRule.hasDrawable() && !subRule.hasPosition())
4429 break;
4430 if (subRule.hasDrawable()) {
4431 subRule.drawRule(p, opt->rect);
4432 } else {
4433 QStyleOptionDockWidget dwCopy(*dwOpt);
4434 dwCopy.title = QString();
4435 baseStyle()->drawControl(ce, &dwCopy, p, w);
4436 }
4437
4438 if (!dwOpt->title.isEmpty()) {
4439 QRect r = subElementRect(SE_DockWidgetTitleBarText, opt, w);
4440 if (dwOpt->verticalTitleBar) {
4441 r = r.transposed();
4442 p->save();
4443 p->translate(r.left(), r.top() + r.width());
4444 p->rotate(-90);
4445 p->translate(-r.left(), -r.top());
4446 }
4447 r = subRule.contentsRect(r);
4448
4449 Qt::Alignment alignment;
4450 if (subRule.hasPosition())
4451 alignment = subRule.position()->textAlignment;
4452 if (alignment == 0)
4453 alignment = Qt::AlignLeft;
4454
4455 QString titleText = p->fontMetrics().elidedText(dwOpt->title, Qt::ElideRight, r.width());
4456 drawItemText(p, r,
4457 alignment | Qt::TextHideMnemonic, dwOpt->palette,
4458 dwOpt->state & State_Enabled, titleText,
4459 QPalette::WindowText);
4460
4461 if (dwOpt->verticalTitleBar)
4462 p->restore();
4463 }
4464
4465 return;
4466 }
4467 break;
4468 case CE_ShapedFrame:
4469 if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4470 if (rule.hasNativeBorder()) {
4471 QStyleOptionFrame frmOpt(*frm);
4472 rule.configurePalette(&frmOpt.palette, QPalette::Text, QPalette::Base);
4473 frmOpt.rect = rule.borderRect(frmOpt.rect);
4474 baseStyle()->drawControl(ce, &frmOpt, p, w);
4475 }
4476 // else, borders are already drawn in PE_Widget
4477 }
4478 return;
4479
4480
4481 default:
4482 break;
4483 }
4484
4485 if (pe1 != PseudoElement_None) {
4486 QRenderRule subRule = renderRule(w, opt, pe1);
4487 if (subRule.bg != nullptr || subRule.hasDrawable()) {
4488 //We test subRule.bg directly because hasBackground() would return false for background:none.
4489 //But we still don't want the default drawning in that case (example for QScrollBar::add-page) (task 198926)
4490 subRule.drawRule(p, opt->rect);
4491 } else if (fallback) {
4492 QWindowsStyle::drawControl(ce, opt, p, w);
4493 pe2 = PseudoElement_None;
4494 } else {
4495 baseStyle()->drawControl(ce, opt, p, w);
4496 }
4497 if (pe2 != PseudoElement_None) {
4498 QRenderRule subSubRule = renderRule(w, opt, pe2);
4499 QRect r = positionRect(w, subRule, subSubRule, pe2, opt->rect, opt->direction);
4500 subSubRule.drawRule(p, r);
4501 }
4502 return;
4503 }
4504
4505 baseStyle()->drawControl(ce, opt, p, w);
4506}
4507
4508void QStyleSheetStyle::drawItemPixmap(QPainter *p, const QRect &rect, int alignment, const
4509 QPixmap &pixmap) const
4510{
4511 baseStyle()->drawItemPixmap(p, rect, alignment, pixmap);
4512}
4513
4514void QStyleSheetStyle::drawItemText(QPainter *painter, const QRect& rect, int alignment, const QPalette &pal,
4515 bool enabled, const QString& text, QPalette::ColorRole textRole) const
4516{
4517 baseStyle()->drawItemText(painter, rect, alignment, pal, enabled, text, textRole);
4518}
4519
4520void QStyleSheetStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
4521 const QWidget *w) const
4522{
4523 RECURSION_GUARD(baseStyle()->drawPrimitive(pe, opt, p, w); return)
4524
4525 int pseudoElement = PseudoElement_None;
4526 QRenderRule rule = renderRule(w, opt);
4527 QRect rect = opt->rect;
4528
4529 switch (pe) {
4530
4531 case PE_FrameStatusBarItem: {
4532 QRenderRule subRule = renderRule(w ? w->parentWidget() : nullptr, opt, PseudoElement_Item);
4533 if (subRule.hasDrawable()) {
4534 subRule.drawRule(p, opt->rect);
4535 return;
4536 }
4537 break;
4538 }
4539
4540 case PE_IndicatorArrowDown:
4541 pseudoElement = PseudoElement_DownArrow;
4542 break;
4543
4544 case PE_IndicatorArrowUp:
4545 pseudoElement = PseudoElement_UpArrow;
4546 break;
4547
4548 case PE_IndicatorRadioButton:
4549 pseudoElement = PseudoElement_ExclusiveIndicator;
4550 break;
4551
4552 case PE_IndicatorItemViewItemCheck:
4553 pseudoElement = PseudoElement_ViewItemIndicator;
4554 break;
4555
4556 case PE_IndicatorCheckBox:
4557 pseudoElement = PseudoElement_Indicator;
4558 break;
4559
4560 case PE_IndicatorHeaderArrow:
4561 if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
4562 pseudoElement = hdr->sortIndicator == QStyleOptionHeader::SortUp
4563 ? PseudoElement_HeaderViewUpArrow
4564 : PseudoElement_HeaderViewDownArrow;
4565 }
4566 break;
4567
4568 case PE_PanelButtonTool:
4569 case PE_PanelButtonCommand:
4570#if QT_CONFIG(abstractbutton)
4571 if (qobject_cast<const QAbstractButton *>(w) && rule.hasBackground() && rule.hasNativeBorder()) {
4572 //the window style will draw the borders
4573 ParentStyle::drawPrimitive(pe, opt, p, w);
4574 if (!rule.background()->pixmap.isNull() || rule.hasImage()) {
4575 rule.drawRule(p, rule.boxRect(opt->rect, QRenderRule::Margin).adjusted(1,1,-1,-1));
4576 }
4577 return;
4578 }
4579#endif
4580 if (!rule.hasNativeBorder()) {
4581 rule.drawRule(p, rule.boxRect(opt->rect, QRenderRule::Margin));
4582 return;
4583 }
4584 break;
4585
4586 case PE_IndicatorButtonDropDown: {
4587 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
4588 if (!subRule.hasNativeBorder()) {
4589 rule.drawBorder(p, opt->rect);
4590 return;
4591 }
4592 break;
4593 }
4594
4595 case PE_FrameDefaultButton:
4596 if (rule.hasNativeBorder()) {
4597 if (rule.baseStyleCanDraw())
4598 break;
4599 QWindowsStyle::drawPrimitive(pe, opt, p, w);
4600 }
4601 return;
4602
4603 case PE_FrameWindow:
4604 case PE_FrameDockWidget:
4605 case PE_Frame:
4606 if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4607 if (rule.hasNativeBorder()) {
4608 QStyleOptionFrame frmOpt(*frm);
4609 rule.configurePalette(&frmOpt.palette, QPalette::Text, QPalette::Base);
4610 baseStyle()->drawPrimitive(pe, &frmOpt, p, w);
4611 } else {
4612 rule.drawBorder(p, rule.borderRect(opt->rect));
4613 }
4614 }
4615 return;
4616
4617 case PE_PanelLineEdit:
4618 if (const QStyleOptionFrame *frm = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4619 // Fall back to container widget's render rule
4620 if (w) {
4621 if (QWidget *container = containerWidget(w); container != w) {
4622 QRenderRule containerRule = renderRule(container, opt);
4623 if (!containerRule.hasNativeBorder() || !containerRule.baseStyleCanDraw())
4624 return;
4625 rule = containerRule;
4626 }
4627 }
4628
4629 if (rule.hasNativeBorder()) {
4630 QStyleOptionFrame frmOpt(*frm);
4631 rule.configurePalette(&frmOpt.palette, QPalette::Text, QPalette::Base);
4632 frmOpt.rect = rule.borderRect(frmOpt.rect);
4633 if (rule.baseStyleCanDraw()) {
4634 rule.drawBackgroundImage(p, opt->rect);
4635 baseStyle()->drawPrimitive(pe, &frmOpt, p, w);
4636 } else {
4637 rule.drawBackground(p, opt->rect);
4638 if (frmOpt.lineWidth > 0)
4639 baseStyle()->drawPrimitive(PE_FrameLineEdit, &frmOpt, p, w);
4640 }
4641 } else {
4642 rule.drawRule(p, opt->rect);
4643 }
4644 }
4645 return;
4646
4647 case PE_Widget:
4648 if (w && !rule.hasDrawable()) {
4649 QWidget *container = containerWidget(w);
4650 if (styleSheetCaches->autoFillDisabledWidgets.contains(container)
4651 && (container == w || !renderRule(container, opt).hasBackground())) {
4652 //we do not have a background, but we disabled the autofillbackground anyway. so fill the background now.
4653 // (this may happen if we have rules like :focus)
4654 p->fillRect(opt->rect, opt->palette.brush(w->backgroundRole()));
4655 }
4656 break;
4657 }
4658#if QT_CONFIG(scrollarea)
4659 if (const QAbstractScrollArea *sa = qobject_cast<const QAbstractScrollArea *>(w)) {
4660 const QAbstractScrollAreaPrivate *sap = sa->d_func();
4661 rule.drawBackground(p, opt->rect, sap->contentsOffset());
4662 if (rule.hasBorder()) {
4663 QRect brect = rule.borderRect(opt->rect);
4664 if (styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, opt, w)) {
4665 QRect r = brect.adjusted(0, 0, sa->verticalScrollBar()->isVisible() ? -sa->verticalScrollBar()->width() : 0,
4666 sa->horizontalScrollBar()->isVisible() ? -sa->horizontalScrollBar()->height() : 0);
4667 brect = QStyle::visualRect(opt->direction, brect, r);
4668 }
4669 rule.drawBorder(p, brect);
4670 }
4671 break;
4672 }
4673#endif
4674 Q_FALLTHROUGH();
4675 case PE_PanelMenu:
4676 case PE_PanelStatusBar:
4677 if (rule.hasDrawable()) {
4678 rule.drawRule(p, opt->rect);
4679 return;
4680 }
4681 break;
4682
4683 case PE_FrameMenu:
4684 if (rule.hasDrawable()) {
4685 // Drawn by PE_PanelMenu
4686 return;
4687 }
4688 break;
4689
4690 case PE_PanelMenuBar:
4691 if (rule.hasDrawable()) {
4692 // Drawn by PE_Widget
4693 return;
4694 }
4695 break;
4696
4697 case PE_IndicatorToolBarSeparator:
4698 case PE_IndicatorToolBarHandle: {
4699 PseudoElement ps = pe == PE_IndicatorToolBarHandle ? PseudoElement_ToolBarHandle : PseudoElement_ToolBarSeparator;
4700 QRenderRule subRule = renderRule(w, opt, ps);
4701 if (subRule.hasDrawable()) {
4702 subRule.drawRule(p, opt->rect);
4703 return;
4704 }
4705 }
4706 break;
4707
4708 case PE_IndicatorMenuCheckMark:
4709 pseudoElement = PseudoElement_MenuCheckMark;
4710 break;
4711
4712 case PE_IndicatorArrowLeft:
4713 pseudoElement = PseudoElement_LeftArrow;
4714 break;
4715
4716 case PE_IndicatorArrowRight:
4717 pseudoElement = PseudoElement_RightArrow;
4718 break;
4719
4720 case PE_IndicatorColumnViewArrow:
4721#if QT_CONFIG(itemviews)
4722 if (const QStyleOptionViewItem *viewOpt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
4723 bool reverse = (viewOpt->direction == Qt::RightToLeft);
4724 pseudoElement = reverse ? PseudoElement_LeftArrow : PseudoElement_RightArrow;
4725 } else
4726#endif
4727 {
4728 pseudoElement = PseudoElement_RightArrow;
4729 }
4730 break;
4731
4732#if QT_CONFIG(itemviews)
4733 case PE_IndicatorBranch:
4734 if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
4735 QRenderRule subRule = renderRule(w, opt, PseudoElement_TreeViewBranch);
4736 if (subRule.hasDrawable()) {
4737 proxy()->drawPrimitive(PE_PanelItemViewRow, vopt, p, w);
4738 subRule.drawRule(p, opt->rect);
4739 } else {
4740 baseStyle()->drawPrimitive(pe, vopt, p, w);
4741 }
4742 }
4743 return;
4744#endif // QT_CONFIG(itemviews)
4745
4746 case PE_PanelTipLabel:
4747 if (!rule.hasDrawable())
4748 break;
4749
4750 if (const QStyleOptionFrame *frmOpt = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
4751 if (rule.hasNativeBorder()) {
4752 rule.drawBackground(p, opt->rect);
4753 QStyleOptionFrame optCopy(*frmOpt);
4754 optCopy.rect = rule.borderRect(opt->rect);
4755 optCopy.palette.setBrush(QPalette::Window, Qt::NoBrush); // oh dear
4756 baseStyle()->drawPrimitive(pe, &optCopy, p, w);
4757 } else {
4758 rule.drawRule(p, opt->rect);
4759 }
4760 }
4761 return;
4762
4763 case PE_FrameGroupBox:
4764 if (rule.hasNativeBorder())
4765 break;
4766 rule.drawBorder(p, opt->rect);
4767 return;
4768
4769#if QT_CONFIG(tabwidget)
4770 case PE_FrameTabWidget:
4771 if (const QStyleOptionTabWidgetFrame *frm = qstyleoption_cast<const QStyleOptionTabWidgetFrame *>(opt)) {
4772 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabWidgetPane);
4773 if (subRule.hasNativeBorder()) {
4774 subRule.drawBackground(p, opt->rect);
4775 QStyleOptionTabWidgetFrame frmCopy(*frm);
4776 subRule.configurePalette(&frmCopy.palette, QPalette::WindowText, QPalette::Window);
4777 baseStyle()->drawPrimitive(pe, &frmCopy, p, w);
4778 } else {
4779 subRule.drawRule(p, opt->rect);
4780 }
4781 return;
4782 }
4783 break;
4784#endif // QT_CONFIG(tabwidget)
4785
4786 case PE_IndicatorProgressChunk:
4787 pseudoElement = PseudoElement_ProgressBarChunk;
4788 break;
4789
4790 case PE_IndicatorTabTear:
4791 pseudoElement = PseudoElement_TabBarTear;
4792 break;
4793
4794 case PE_FrameFocusRect:
4795 if (!rule.hasNativeOutline()) {
4796 rule.drawOutline(p, opt->rect);
4797 return;
4798 }
4799 break;
4800
4801 case PE_IndicatorDockWidgetResizeHandle:
4802 pseudoElement = PseudoElement_DockWidgetSeparator;
4803 break;
4804
4805 case PE_PanelItemViewRow:
4806 // For compatibility reasons, QTreeView draws different parts of
4807 // the background of an item row separately, before calling the
4808 // delegate to draw the item. The row background of an item is
4809 // however not separately styleable through a style sheet, but
4810 // only indirectly through the background of the item. To get the
4811 // same background for all parts drawn by QTreeView, we have to
4812 // use the background rule for the item here.
4813 if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
4814 // default handling for drawing empty space
4815 if (vopt->viewItemPosition == QStyleOptionViewItem::Invalid)
4816 break;
4817 if (QRenderRule rule = renderRule(w, opt, PseudoElement_ViewItem); rule.hasBackground()) {
4818 // if the item background is not fully opaque, then we have to paint the row
4819 if (rule.background()->brush.color().alpha() != 1.0)
4820 baseStyle()->drawPrimitive(pe, opt, p, w);
4821 // Skip border for the branch and draw only the brackground
4822 if (vopt->features & QStyleOptionViewItem::IsDecorationForRootColumn &&
4823 (vopt->viewItemPosition == QStyleOptionViewItem::Beginning ||
4824 vopt->viewItemPosition == QStyleOptionViewItem::OnlyOne) && rule.hasBorder()) {
4825 if (rule.hasDrawable()) {
4826 rule.drawBackground(p, rect);
4827 rule.drawImage(p, rule.contentsRect(rect));
4828 } else {
4829 baseStyle()->drawPrimitive(pe, opt, p, w);
4830 }
4831 return;
4832 }
4833 pseudoElement = PseudoElement_ViewItem;
4834 }
4835 }
4836 break;
4837 case PE_PanelItemViewItem:
4838 pseudoElement = PseudoElement_ViewItem;
4839 break;
4840
4841 case PE_PanelScrollAreaCorner:
4842 pseudoElement = PseudoElement_ScrollAreaCorner;
4843 break;
4844
4845 case PE_IndicatorSpinDown:
4846 case PE_IndicatorSpinMinus:
4847 pseudoElement = PseudoElement_SpinBoxDownArrow;
4848 break;
4849
4850 case PE_IndicatorSpinUp:
4851 case PE_IndicatorSpinPlus:
4852 pseudoElement = PseudoElement_SpinBoxUpArrow;
4853 break;
4854#if QT_CONFIG(tabbar)
4855 case PE_IndicatorTabClose:
4856 if (w) {
4857 // QMacStyle needs a real widget, not its parent - to implement
4858 // 'document mode' properly, drawing nothing if a tab is not hovered.
4859 baseStyle()->setProperty("_q_styleSheetRealCloseButton", QVariant::fromValue((void *)w));
4860 w = w->parentWidget(); //match on the QTabBar instead of the CloseButton
4861 }
4862 pseudoElement = PseudoElement_TabBarTabCloseButton;
4863 break;
4864#endif
4865
4866 default:
4867 break;
4868 }
4869
4870 if (pseudoElement != PseudoElement_None) {
4871 QRenderRule subRule = renderRule(w, opt, pseudoElement);
4872 if (subRule.hasDrawable()) {
4873 subRule.drawRule(p, rect);
4874 } else {
4875 baseStyle()->drawPrimitive(pe, opt, p, w);
4876 }
4877 } else {
4878 baseStyle()->drawPrimitive(pe, opt, p, w);
4879 }
4880
4881 if (baseStyle()->property("_q_styleSheetRealCloseButton").toBool())
4882 baseStyle()->setProperty("_q_styleSheetRealCloseButton", QVariant());
4883}
4884
4885QPixmap QStyleSheetStyle::generatedIconPixmap(QIcon::Mode iconMode, const QPixmap& pixmap,
4886 const QStyleOption *option) const
4887{
4888 return baseStyle()->generatedIconPixmap(iconMode, pixmap, option);
4889}
4890
4891QStyle::SubControl QStyleSheetStyle::hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt,
4892 const QPoint &pt, const QWidget *w) const
4893{
4894 RECURSION_GUARD(return baseStyle()->hitTestComplexControl(cc, opt, pt, w))
4895 switch (cc) {
4896 case CC_TitleBar:
4897 if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
4898 QRenderRule rule = renderRule(w, opt, PseudoElement_TitleBar);
4899 if (rule.hasDrawable() || rule.hasBox() || rule.hasBorder()) {
4900 QHash<QStyle::SubControl, QRect> layout = titleBarLayout(w, tb);
4901 QRect r;
4902 QStyle::SubControl sc = QStyle::SC_None;
4903 uint ctrl = SC_TitleBarSysMenu;
4904 while (ctrl <= SC_TitleBarLabel) {
4905 r = layout[QStyle::SubControl(ctrl)];
4906 if (r.isValid() && r.contains(pt)) {
4907 sc = QStyle::SubControl(ctrl);
4908 break;
4909 }
4910 ctrl <<= 1;
4911 }
4912 return sc;
4913 }
4914 }
4915 break;
4916
4917 case CC_MdiControls:
4918 if (hasStyleRule(w, PseudoElement_MdiCloseButton)
4919 || hasStyleRule(w, PseudoElement_MdiNormalButton)
4920 || hasStyleRule(w, PseudoElement_MdiMinButton))
4921 return QWindowsStyle::hitTestComplexControl(cc, opt, pt, w);
4922 break;
4923
4924 case CC_ScrollBar: {
4925 QRenderRule rule = renderRule(w, opt);
4926 if (!rule.hasDrawable() && !rule.hasBox())
4927 break;
4928 }
4929 Q_FALLTHROUGH();
4930 case CC_SpinBox:
4931 case CC_GroupBox:
4932 case CC_ComboBox:
4933 case CC_Slider:
4934 case CC_ToolButton:
4935 return QWindowsStyle::hitTestComplexControl(cc, opt, pt, w);
4936 default:
4937 break;
4938 }
4939
4940 return baseStyle()->hitTestComplexControl(cc, opt, pt, w);
4941}
4942
4943QRect QStyleSheetStyle::itemPixmapRect(const QRect &rect, int alignment, const QPixmap &pixmap) const
4944{
4945 return baseStyle()->itemPixmapRect(rect, alignment, pixmap);
4946}
4947
4948QRect QStyleSheetStyle::itemTextRect(const QFontMetrics &metrics, const QRect& rect, int alignment,
4949 bool enabled, const QString& text) const
4950{
4951 return baseStyle()->itemTextRect(metrics, rect, alignment, enabled, text);
4952}
4953
4954int QStyleSheetStyle::pixelMetric(PixelMetric m, const QStyleOption *opt, const QWidget *w) const
4955{
4956 RECURSION_GUARD(return baseStyle()->pixelMetric(m, opt, w))
4957
4958 QRenderRule rule = renderRule(w, opt);
4959 QRenderRule subRule;
4960
4961 switch (m) {
4962 case PM_MenuButtonIndicator:
4963#if QT_CONFIG(toolbutton)
4964 // QToolButton adds this directly to the width
4965 if (qobject_cast<const QToolButton *>(w)) {
4966 if (rule.hasBox() || !rule.hasNativeBorder())
4967 return 0;
4968 if (const auto *tbOpt = qstyleoption_cast<const QStyleOptionToolButton*>(opt)) {
4969 if (tbOpt->features & QStyleOptionToolButton::MenuButtonPopup)
4970 subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
4971 else
4972 subRule = renderRule(w, opt, PseudoElement_ToolButtonMenuIndicator);
4973 if (subRule.hasContentsSize())
4974 return subRule.size().width();
4975 }
4976 break;
4977 }
4978#endif
4979 subRule = renderRule(w, opt, PseudoElement_PushButtonMenuIndicator);
4980 if (subRule.hasContentsSize())
4981 return subRule.size().width();
4982 break;
4983
4984 case PM_ButtonShiftHorizontal:
4985 case PM_ButtonShiftVertical:
4986 case PM_ButtonMargin:
4987 case PM_ButtonDefaultIndicator:
4988 if (rule.hasBox())
4989 return 0;
4990 break;
4991
4992 case PM_DefaultFrameWidth:
4993 if (!rule.hasNativeBorder())
4994 return rule.border()->borders[LeftEdge];
4995 break;
4996
4997 case PM_ExclusiveIndicatorWidth:
4998 case PM_IndicatorWidth:
4999 case PM_ExclusiveIndicatorHeight:
5000 case PM_IndicatorHeight:
5001 subRule = renderRule(w, opt, PseudoElement_Indicator);
5002 if (subRule.hasContentsSize()) {
5003 return (m == PM_ExclusiveIndicatorWidth) || (m == PM_IndicatorWidth)
5004 ? subRule.size().width() : subRule.size().height();
5005 }
5006 break;
5007
5008 case PM_DockWidgetFrameWidth:
5009 case PM_ToolTipLabelFrameWidth: // border + margin + padding (support only one width)
5010 if (!rule.hasDrawable())
5011 break;
5012
5013 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
5014 + (rule.hasBox() ? rule.box()->margins[LeftEdge] + rule.box()->paddings[LeftEdge]: 0);
5015
5016 case PM_ToolBarFrameWidth:
5017 if (rule.hasBorder() || rule.hasBox())
5018 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
5019 + (rule.hasBox() ? rule.box()->paddings[LeftEdge]: 0);
5020 break;
5021
5022 case PM_MenuPanelWidth:
5023 case PM_MenuBarPanelWidth:
5024 if (rule.hasBorder() || rule.hasBox())
5025 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
5026 + (rule.hasBox() ? rule.box()->margins[LeftEdge]: 0);
5027 break;
5028
5029
5030 case PM_MenuHMargin:
5031 case PM_MenuBarHMargin:
5032 if (rule.hasBox())
5033 return rule.box()->paddings[LeftEdge];
5034 break;
5035
5036 case PM_MenuVMargin:
5037 case PM_MenuBarVMargin:
5038 if (rule.hasBox())
5039 return rule.box()->paddings[TopEdge];
5040 break;
5041
5042 case PM_DockWidgetTitleBarButtonMargin:
5043 case PM_ToolBarItemMargin:
5044 if (rule.hasBox())
5045 return rule.box()->margins[TopEdge];
5046 break;
5047
5048 case PM_ToolBarItemSpacing:
5049 case PM_MenuBarItemSpacing:
5050 if (rule.hasBox() && rule.box()->spacing != -1)
5051 return rule.box()->spacing;
5052 break;
5053
5054 case PM_MenuTearoffHeight:
5055 case PM_MenuScrollerHeight: {
5056 PseudoElement ps = m == PM_MenuTearoffHeight ? PseudoElement_MenuTearoff : PseudoElement_MenuScroller;
5057 subRule = renderRule(w, opt, ps);
5058 if (subRule.hasContentsSize())
5059 return subRule.size().height();
5060 break;
5061 }
5062
5063 case PM_ToolBarExtensionExtent:
5064 break;
5065
5066 case PM_SplitterWidth:
5067 case PM_ToolBarSeparatorExtent:
5068 case PM_ToolBarHandleExtent: {
5069 PseudoElement ps;
5070 if (m == PM_ToolBarHandleExtent) ps = PseudoElement_ToolBarHandle;
5071 else if (m == PM_SplitterWidth) ps = PseudoElement_SplitterHandle;
5072 else ps = PseudoElement_ToolBarSeparator;
5073 subRule = renderRule(w, opt, ps);
5074 if (subRule.hasContentsSize()) {
5075 QSize sz = subRule.size();
5076 return (opt && opt->state & QStyle::State_Horizontal) ? sz.width() : sz.height();
5077 }
5078 break;
5079 }
5080
5081 case PM_RadioButtonLabelSpacing:
5082 if (rule.hasBox() && rule.box()->spacing != -1)
5083 return rule.box()->spacing;
5084 break;
5085 case PM_CheckBoxLabelSpacing:
5086#if QT_CONFIG(checkbox)
5087 if (qobject_cast<const QCheckBox *>(w)) {
5088 if (rule.hasBox() && rule.box()->spacing != -1)
5089 return rule.box()->spacing;
5090 }
5091#endif
5092 // assume group box
5093 subRule = renderRule(w, opt, PseudoElement_GroupBoxTitle);
5094 if (subRule.hasBox() && subRule.box()->spacing != -1)
5095 return subRule.box()->spacing;
5096 break;
5097
5098#if QT_CONFIG(scrollbar)
5099 case PM_ScrollBarExtent:
5100 if (rule.hasContentsSize()) {
5101 QSize sz = rule.size();
5102 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt))
5103 return sb->orientation == Qt::Horizontal ? sz.height() : sz.width();
5104 return sz.width() == -1 ? sz.height() : sz.width();
5105 }
5106 break;
5107
5108 case PM_ScrollBarSliderMin:
5109 if (hasStyleRule(w, PseudoElement_ScrollBarSlider)) {
5110 subRule = renderRule(w, opt, PseudoElement_ScrollBarSlider);
5111 QSize msz = subRule.minimumSize();
5112 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt))
5113 return sb->orientation == Qt::Horizontal ? msz.width() : msz.height();
5114 return msz.width() == -1 ? msz.height() : msz.width();
5115 }
5116 break;
5117
5118 case PM_ScrollView_ScrollBarSpacing:
5119 if (!rule.hasNativeBorder() || rule.hasBox())
5120 return 0;
5121 break;
5122
5123 case PM_ScrollView_ScrollBarOverlap:
5124 if (!proxy()->styleHint(SH_ScrollBar_Transient, opt, w))
5125 return 0;
5126 break;
5127#endif // QT_CONFIG(scrollbar)
5128
5129
5130 case PM_ProgressBarChunkWidth:
5131 subRule = renderRule(w, opt, PseudoElement_ProgressBarChunk);
5132 if (subRule.hasContentsSize()) {
5133 QSize sz = subRule.size();
5134 return (opt->state & QStyle::State_Horizontal)
5135 ? sz.width() : sz.height();
5136 }
5137 break;
5138
5139#if QT_CONFIG(tabwidget)
5140 case PM_TabBarTabHSpace:
5141 case PM_TabBarTabVSpace:
5142 subRule = renderRule(w, opt, PseudoElement_TabBarTab);
5143 if (subRule.hasBox() || subRule.hasBorder())
5144 return 0;
5145 break;
5146
5147 case PM_TabBarScrollButtonWidth:
5148 subRule = renderRule(w, opt, PseudoElement_TabBarScroller);
5149 if (subRule.hasContentsSize()) {
5150 QSize sz = subRule.size();
5151 return (sz.width() != -1 ? sz.width() : sz.height()) / 2;
5152 }
5153 break;
5154
5155 case PM_TabBarTabShiftHorizontal:
5156 case PM_TabBarTabShiftVertical:
5157 subRule = renderRule(w, opt, PseudoElement_TabBarTab);
5158 if (subRule.hasBox())
5159 return 0;
5160 break;
5161
5162 case PM_TabBarBaseOverlap: {
5163 const QWidget *tabWidget = qobject_cast<const QTabWidget *>(w);
5164 if (!tabWidget && w)
5165 tabWidget = w->parentWidget();
5166 if (hasStyleRule(tabWidget, PseudoElement_TabWidgetPane)) {
5167 return 0;
5168 }
5169 break;
5170 }
5171#endif // QT_CONFIG(tabwidget)
5172
5173 case PM_SliderThickness: // horizontal slider's height (sizeHint)
5174 case PM_SliderLength: // minimum length of slider
5175 if (rule.hasContentsSize()) {
5176 bool horizontal = opt->state & QStyle::State_Horizontal;
5177 if (m == PM_SliderThickness) {
5178 QSize sz = rule.size();
5179 return horizontal ? sz.height() : sz.width();
5180 } else {
5181 QSize msz = rule.minimumContentsSize();
5182 return horizontal ? msz.width() : msz.height();
5183 }
5184 }
5185 break;
5186
5187 case PM_SliderControlThickness: {
5188 QRenderRule subRule = renderRule(w, opt, PseudoElement_SliderHandle);
5189 if (!subRule.hasContentsSize())
5190 break;
5191 QSize size = subRule.size();
5192 return (opt->state & QStyle::State_Horizontal) ? size.height() : size.width();
5193 }
5194
5195 case PM_ToolBarIconSize:
5196 case PM_ListViewIconSize:
5197 case PM_IconViewIconSize:
5198 case PM_TabBarIconSize:
5199 case PM_MessageBoxIconSize:
5200 case PM_ButtonIconSize:
5201 case PM_SmallIconSize: {
5202 const auto styleHint = rule.styleHint("icon-size"_L1);
5203 if (styleHint.isValid() && styleHint.canConvert<QSize>())
5204 return styleHint.toSize().width();
5205 break;
5206 }
5207 case PM_DockWidgetTitleMargin: {
5208 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetTitle);
5209 if (!subRule.hasBox())
5210 break;
5211 return (subRule.border() ? subRule.border()->borders[TopEdge] : 0)
5212 + (subRule.hasBox() ? subRule.box()->margins[TopEdge] + subRule.box()->paddings[TopEdge]: 0);
5213 }
5214
5215 case PM_DockWidgetSeparatorExtent: {
5216 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetSeparator);
5217 if (!subRule.hasContentsSize())
5218 break;
5219 QSize sz = subRule.size();
5220 return qMax(sz.width(), sz.height());
5221 }
5222
5223 case PM_TitleBarHeight: {
5224 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
5225 if (subRule.hasContentsSize())
5226 return subRule.size().height();
5227 else if (subRule.hasBox() || subRule.hasBorder()) {
5228 QFontMetrics fm = opt ? opt->fontMetrics : w->fontMetrics();
5229 return subRule.size(QSize(0, fm.height())).height();
5230 }
5231 break;
5232 }
5233
5234 case PM_MdiSubWindowFrameWidth:
5235 if (rule.hasBox() || rule.hasBorder()) {
5236 return (rule.border() ? rule.border()->borders[LeftEdge] : 0)
5237 + (rule.hasBox() ? rule.box()->paddings[LeftEdge]+rule.box()->margins[LeftEdge]: 0);
5238 }
5239 break;
5240
5241 case PM_MdiSubWindowMinimizedWidth: {
5242 QRenderRule subRule = renderRule(w, PseudoElement_None, PseudoClass_Minimized);
5243 int width = subRule.size().width();
5244 if (width != -1)
5245 return width;
5246 break;
5247 }
5248 default:
5249 break;
5250 }
5251
5252 return baseStyle()->pixelMetric(m, opt, w);
5253}
5254
5255QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
5256 const QSize &csz, const QWidget *w) const
5257{
5258 RECURSION_GUARD(return baseStyle()->sizeFromContents(ct, opt, csz, w))
5259
5260 QRenderRule rule = renderRule(w, opt);
5261 QSize sz = rule.adjustSize(csz);
5262
5263 switch (ct) {
5264#if QT_CONFIG(spinbox)
5265 case CT_SpinBox:
5266 if (const QStyleOptionSpinBox *spinbox = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
5267 if (rule.baseStyleCanDraw()) {
5268 sz = baseStyle()->sizeFromContents(ct, opt, sz, w);
5269 if (rule.hasBox() || !rule.hasNativeBorder())
5270 sz = rule.boxSize(sz);
5271 return sz;
5272 }
5273 if (spinbox->buttonSymbols != QAbstractSpinBox::NoButtons) {
5274 // Add some space for the up/down buttons
5275 QRenderRule subRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
5276 if (subRule.hasDrawable()) {
5277 QRect r = positionRect(w, rule, subRule, PseudoElement_SpinBoxUpButton,
5278 opt->rect, opt->direction);
5279 sz.rwidth() += r.width();
5280 } else {
5281 QSize defaultUpSize = defaultSize(w, subRule.size(), spinbox->rect, PseudoElement_SpinBoxUpButton);
5282 sz.rwidth() += defaultUpSize.width();
5283 }
5284 }
5285 if (rule.hasBox() || rule.hasBorder() || !rule.hasNativeBorder())
5286 sz = rule.boxSize(sz);
5287 return sz;
5288 }
5289 break;
5290#endif // QT_CONFIG(spinbox)
5291 case CT_ToolButton:
5292 if (rule.hasBox() || !rule.hasNativeBorder() || !rule.baseStyleCanDraw())
5293 sz += QSize(3, 3); // ### broken QToolButton
5294 Q_FALLTHROUGH();
5295 case CT_ComboBox:
5296 case CT_PushButton:
5297 if (rule.hasBox() || !rule.hasNativeBorder()) {
5298 if (ct == CT_ComboBox) {
5299 //add some space for the drop down.
5300 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
5301 QRect comboRect = positionRect(w, rule, subRule, PseudoElement_ComboBoxDropDown, opt->rect, opt->direction);
5302 //+2 because there is hardcoded margins in QCommonStyle::drawControl(CE_ComboBoxLabel)
5303 sz += QSize(comboRect.width() + 2, 0);
5304 }
5305 return rule.boxSize(sz);
5306 }
5307 sz = rule.baseStyleCanDraw() ? baseStyle()->sizeFromContents(ct, opt, sz, w)
5308 : QWindowsStyle::sizeFromContents(ct, opt, sz, w);
5309 return rule.boxSize(sz, Margin);
5310
5311 case CT_HeaderSection: {
5312 if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
5313 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
5314 if (subRule.hasGeometry() || subRule.hasBox() || !subRule.hasNativeBorder() || subRule.hasFont) {
5315 sz = subRule.adjustSize(csz);
5316 if (!sz.isValid()) {
5317 // Try to set the missing values based on the base style.
5318 const auto baseSize = baseStyle()->sizeFromContents(ct, opt, sz, w);
5319 if (sz.width() < 0)
5320 sz.setWidth(baseSize.width());
5321 if (sz.height() < 0)
5322 sz.setHeight(baseSize.height());
5323 }
5324 if (!subRule.hasGeometry()) {
5325 QSize nativeContentsSize;
5326 bool nullIcon = hdr->icon.isNull();
5327 const int margin = pixelMetric(QStyle::PM_HeaderMargin, hdr, w);
5328 int iconSize = nullIcon ? 0 : pixelMetric(QStyle::PM_SmallIconSize, hdr, w);
5329 QFontMetrics fm = hdr->fontMetrics;
5330 if (subRule.hasFont) {
5331 QFont styleFont = w ? subRule.font.resolve(w->font()) : subRule.font;
5332 fm = QFontMetrics(styleFont);
5333 }
5334 const QSize txt = fm.size(0, hdr->text);
5335 nativeContentsSize.setHeight(margin + qMax(iconSize, txt.height()) + margin);
5336 nativeContentsSize.setWidth((nullIcon ? 0 : margin) + iconSize
5337 + (hdr->text.isNull() ? 0 : margin) + txt.width() + margin);
5338 sz = sz.expandedTo(nativeContentsSize);
5339 }
5340 return subRule.size(sz);
5341 }
5342 sz = subRule.baseStyleCanDraw() ? baseStyle()->sizeFromContents(ct, opt, sz, w)
5343 : QWindowsStyle::sizeFromContents(ct, opt, sz, w);
5344 if (hasStyleRule(w, PseudoElement_HeaderViewDownArrow)
5345 || hasStyleRule(w, PseudoElement_HeaderViewUpArrow)) {
5346 const QRect arrowRect = subElementRect(SE_HeaderArrow, opt, w);
5347 if (hdr->orientation == Qt::Horizontal)
5348 sz.rwidth() += arrowRect.width();
5349 else
5350 sz.rheight() += arrowRect.height();
5351 }
5352 return sz;
5353 }
5354 }
5355 break;
5356 case CT_GroupBox:
5357 case CT_LineEdit:
5358#if QT_CONFIG(spinbox)
5359 if (qobject_cast<QAbstractSpinBox *>(w ? w->parentWidget() : nullptr))
5360 return csz; // we only care about the size hint of the line edit
5361#endif
5362 if (rule.hasBox() || !rule.hasNativeBorder()) {
5363 return rule.boxSize(sz);
5364 }
5365 break;
5366
5367 case CT_CheckBox:
5368 case CT_RadioButton:
5369 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
5370 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
5371 bool isRadio = (ct == CT_RadioButton);
5372 int iw = pixelMetric(isRadio ? PM_ExclusiveIndicatorWidth
5373 : PM_IndicatorWidth, btn, w);
5374 int ih = pixelMetric(isRadio ? PM_ExclusiveIndicatorHeight
5375 : PM_IndicatorHeight, btn, w);
5376
5377 int spacing = pixelMetric(isRadio ? PM_RadioButtonLabelSpacing
5378 : PM_CheckBoxLabelSpacing, btn, w);
5379 sz.setWidth(sz.width() + iw + spacing);
5380 sz.setHeight(qMax(sz.height(), ih));
5381 return rule.boxSize(sz);
5382 }
5383 }
5384 break;
5385
5386 case CT_Menu:
5387 case CT_MenuBar: // already has everything!
5388 case CT_ScrollBar:
5389 if (rule.hasBox() || rule.hasBorder())
5390 return sz;
5391 break;
5392
5393 case CT_MenuItem:
5394 if (const QStyleOptionMenuItem *mi = qstyleoption_cast<const QStyleOptionMenuItem *>(opt)) {
5395 PseudoElement pe = (mi->menuItemType == QStyleOptionMenuItem::Separator)
5396 ? PseudoElement_MenuSeparator : PseudoElement_Item;
5397 QRenderRule subRule = renderRule(w, opt, pe);
5398 if ((pe == PseudoElement_MenuSeparator) && subRule.hasContentsSize()) {
5399 return QSize(sz.width(), subRule.size().height());
5400 }
5401 if ((pe == PseudoElement_Item) && (subRule.hasBox() || subRule.hasBorder() || subRule.hasFont)) {
5402 bool drawCheckMark = mi->menuHasCheckableItems;
5403#if QT_CONFIG(combobox)
5404 if (qobject_cast<const QComboBox *>(w))
5405 drawCheckMark = false; // ignore the checkmarks provided by the QComboMenuDelegate
5406#endif
5407 QSize sz(csz);
5408 if (subRule.hasFont) {
5409 QFontMetrics fm(subRule.font.resolve(mi->font));
5410 const QRect r = fm.boundingRect(QRect(), Qt::TextSingleLine | Qt::TextShowMnemonic, mi->text);
5411 sz = sz.expandedTo(r.size());
5412 }
5413 if (mi->text.contains(u'\t'))
5414 sz.rwidth() += 12; //as in QCommonStyle
5415 if (!mi->icon.isNull()) {
5416 const int pmSmall = pixelMetric(PM_SmallIconSize);
5417 const QSize pmSize = mi->icon.actualSize(QSize(pmSmall, pmSmall));
5418 sz.rwidth() += std::max(mi->maxIconWidth, pmSize.width()) + 4;
5419 } else if (drawCheckMark) {
5420 QRenderRule subSubRule = renderRule(w, opt, PseudoElement_MenuCheckMark);
5421 QRect checkmarkRect = positionRect(w, subRule, subSubRule, PseudoElement_MenuCheckMark, opt->rect, opt->direction);
5422 sz.rwidth() += std::max(mi->maxIconWidth, checkmarkRect.width()) + 4;
5423 } else {
5424 sz.rwidth() += mi->maxIconWidth;
5425 }
5426 return subRule.boxSize(subRule.adjustSize(sz));
5427 }
5428 }
5429 break;
5430
5431 case CT_Splitter:
5432 case CT_MenuBarItem: {
5433 PseudoElement pe = (ct == CT_Splitter) ? PseudoElement_SplitterHandle : PseudoElement_Item;
5434 QRenderRule subRule = renderRule(w, opt, pe);
5435 if (subRule.hasBox() || subRule.hasBorder())
5436 return subRule.boxSize(sz);
5437 break;
5438 }
5439
5440 case CT_ProgressBar:
5441 case CT_SizeGrip:
5442 return (rule.hasContentsSize())
5443 ? rule.size(sz)
5444 : rule.boxSize(baseStyle()->sizeFromContents(ct, opt, sz, w));
5445 break;
5446
5447 case CT_Slider:
5448 if (rule.hasBorder() || rule.hasBox() || rule.hasGeometry())
5449 return rule.boxSize(sz);
5450 break;
5451
5452#if QT_CONFIG(tabbar)
5453 case CT_TabBarTab: {
5454 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
5455 if (subRule.hasBox() || !subRule.hasNativeBorder() || subRule.hasFont) {
5456 int spaceForIcon = 0;
5457 bool vertical = false;
5458 QString text;
5459 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
5460 if (!tab->icon.isNull())
5461 spaceForIcon = 6 /* icon offset */ + 4 /* spacing */ + 2 /* magic */; // ###: hardcoded to match with common style
5462 vertical = verticalTabs(tab->shape);
5463 text = tab->text;
5464 }
5465 if (subRule.hasBox() || !subRule.hasNativeBorder())
5466 sz = csz + QSize(vertical ? 0 : spaceForIcon, vertical ? spaceForIcon : 0);
5467 if (subRule.hasFont) {
5468 // first we remove the space needed for the text using the default font
5469 const QSize oldTextSize = opt->fontMetrics.size(Qt::TextShowMnemonic, text);
5470 (vertical ? sz.rheight() : sz.rwidth()) -= oldTextSize.width();
5471
5472 // then we add the space needed when using the rule font to the relevant
5473 // dimension, and constraint the other dimension to the maximum to make
5474 // sure we don't grow, but also don't clip icons or buttons.
5475 const QFont ruleFont = subRule.font.resolve(w->font());
5476 const QFontMetrics fm(ruleFont);
5477 const QSize textSize = fm.size(Qt::TextShowMnemonic, text);
5478 if (vertical) {
5479 sz.rheight() += textSize.width();
5480 sz.rwidth() = qMax(textSize.height(), sz.width());
5481 } else {
5482 sz.rwidth() += textSize.width();
5483 sz.rheight() = qMax(textSize.height(), sz.height());
5484 }
5485 }
5486
5487 return subRule.boxSize(subRule.adjustSize(sz));
5488 }
5489 sz = subRule.adjustSize(csz);
5490 break;
5491 }
5492#endif // QT_CONFIG(tabbar)
5493
5494 case CT_MdiControls:
5495 if (const QStyleOptionComplex *ccOpt = qstyleoption_cast<const QStyleOptionComplex *>(opt)) {
5496 if (!hasStyleRule(w, PseudoElement_MdiCloseButton)
5497 && !hasStyleRule(w, PseudoElement_MdiNormalButton)
5498 && !hasStyleRule(w, PseudoElement_MdiMinButton))
5499 break;
5500
5501 QList<QVariant> layout = rule.styleHint("button-layout"_L1).toList();
5502 if (layout.isEmpty())
5503 layout = subControlLayout("mNX");
5504
5505 int width = 0, height = 0;
5506 for (const QVariant &val : std::as_const(layout)) {
5507 int layoutButton = val.toInt();
5508 if (layoutButton < PseudoElement_MdiCloseButton
5509 || layoutButton > PseudoElement_MdiNormalButton)
5510 continue;
5511 QStyle::SubControl sc = knownPseudoElements[layoutButton].subControl;
5512 if (!(ccOpt->subControls & sc))
5513 continue;
5514 QRenderRule subRule = renderRule(w, opt, layoutButton);
5515 QSize sz = subRule.size();
5516 width += sz.width();
5517 height = qMax(height, sz.height());
5518 }
5519
5520 return QSize(width, height);
5521 }
5522 break;
5523
5524#if QT_CONFIG(itemviews)
5525 case CT_ItemViewItem: {
5526 QRenderRule subRule = renderRule(w, opt, PseudoElement_ViewItem);
5527 sz = baseStyle()->sizeFromContents(ct, opt, csz, w);
5528 sz = subRule.adjustSize(sz);
5529 if (subRule.hasBox() || subRule.hasBorder())
5530 sz = subRule.boxSize(sz);
5531 return sz;
5532 }
5533#endif // QT_CONFIG(itemviews)
5534
5535 default:
5536 break;
5537 }
5538
5539 return baseStyle()->sizeFromContents(ct, opt, sz, w);
5540}
5541
5542/*!
5543 \internal
5544*/
5545static QLatin1StringView propertyNameForStandardPixmap(QStyle::StandardPixmap sp)
5546{
5547 switch (sp) {
5548 case QStyle::SP_TitleBarMenuButton: return "titlebar-menu-icon"_L1;
5549 case QStyle::SP_TitleBarMinButton: return "titlebar-minimize-icon"_L1;
5550 case QStyle::SP_TitleBarMaxButton: return "titlebar-maximize-icon"_L1;
5551 case QStyle::SP_TitleBarCloseButton: return "titlebar-close-icon"_L1;
5552 case QStyle::SP_TitleBarNormalButton: return "titlebar-normal-icon"_L1;
5553 case QStyle::SP_TitleBarShadeButton: return "titlebar-shade-icon"_L1;
5554 case QStyle::SP_TitleBarUnshadeButton: return "titlebar-unshade-icon"_L1;
5555 case QStyle::SP_TitleBarContextHelpButton: return "titlebar-contexthelp-icon"_L1;
5556 case QStyle::SP_DockWidgetCloseButton: return "dockwidget-close-icon"_L1;
5557 case QStyle::SP_MessageBoxInformation: return "messagebox-information-icon"_L1;
5558 case QStyle::SP_MessageBoxWarning: return "messagebox-warning-icon"_L1;
5559 case QStyle::SP_MessageBoxCritical: return "messagebox-critical-icon"_L1;
5560 case QStyle::SP_MessageBoxQuestion: return "messagebox-question-icon"_L1;
5561 case QStyle::SP_DesktopIcon: return "desktop-icon"_L1;
5562 case QStyle::SP_TrashIcon: return "trash-icon"_L1;
5563 case QStyle::SP_ComputerIcon: return "computer-icon"_L1;
5564 case QStyle::SP_DriveFDIcon: return "floppy-icon"_L1;
5565 case QStyle::SP_DriveHDIcon: return "harddisk-icon"_L1;
5566 case QStyle::SP_DriveCDIcon: return "cd-icon"_L1;
5567 case QStyle::SP_DriveDVDIcon: return "dvd-icon"_L1;
5568 case QStyle::SP_DriveNetIcon: return "network-icon"_L1;
5569 case QStyle::SP_DirOpenIcon: return "directory-open-icon"_L1;
5570 case QStyle::SP_DirClosedIcon: return "directory-closed-icon"_L1;
5571 case QStyle::SP_DirLinkIcon: return "directory-link-icon"_L1;
5572 case QStyle::SP_FileIcon: return "file-icon"_L1;
5573 case QStyle::SP_FileLinkIcon: return "file-link-icon"_L1;
5574 case QStyle::SP_FileDialogStart: return "filedialog-start-icon"_L1;
5575 case QStyle::SP_FileDialogEnd: return "filedialog-end-icon"_L1;
5576 case QStyle::SP_FileDialogToParent: return "filedialog-parent-directory-icon"_L1;
5577 case QStyle::SP_FileDialogNewFolder: return "filedialog-new-directory-icon"_L1;
5578 case QStyle::SP_FileDialogDetailedView: return "filedialog-detailedview-icon"_L1;
5579 case QStyle::SP_FileDialogInfoView: return "filedialog-infoview-icon"_L1;
5580 case QStyle::SP_FileDialogContentsView: return "filedialog-contentsview-icon"_L1;
5581 case QStyle::SP_FileDialogListView: return "filedialog-listview-icon"_L1;
5582 case QStyle::SP_FileDialogBack: return "filedialog-backward-icon"_L1;
5583 case QStyle::SP_DirIcon: return "directory-icon"_L1;
5584 case QStyle::SP_DialogOkButton: return "dialog-ok-icon"_L1;
5585 case QStyle::SP_DialogCancelButton: return "dialog-cancel-icon"_L1;
5586 case QStyle::SP_DialogHelpButton: return "dialog-help-icon"_L1;
5587 case QStyle::SP_DialogOpenButton: return "dialog-open-icon"_L1;
5588 case QStyle::SP_DialogSaveButton: return "dialog-save-icon"_L1;
5589 case QStyle::SP_DialogCloseButton: return "dialog-close-icon"_L1;
5590 case QStyle::SP_DialogApplyButton: return "dialog-apply-icon"_L1;
5591 case QStyle::SP_DialogResetButton: return "dialog-reset-icon"_L1;
5592 case QStyle::SP_DialogDiscardButton: return "dialog-discard-icon"_L1;
5593 case QStyle::SP_DialogYesButton: return "dialog-yes-icon"_L1;
5594 case QStyle::SP_DialogNoButton: return "dialog-no-icon"_L1;
5595 case QStyle::SP_ArrowUp: return "uparrow-icon"_L1;
5596 case QStyle::SP_ArrowDown: return "downarrow-icon"_L1;
5597 case QStyle::SP_ArrowLeft: return "leftarrow-icon"_L1;
5598 case QStyle::SP_ArrowRight: return "rightarrow-icon"_L1;
5599 case QStyle::SP_ArrowBack: return "backward-icon"_L1;
5600 case QStyle::SP_ArrowForward: return "forward-icon"_L1;
5601 case QStyle::SP_DirHomeIcon: return "home-icon"_L1;
5602 case QStyle::SP_LineEditClearButton: return "lineedit-clear-button-icon"_L1;
5603 default: return ""_L1;
5604 }
5605}
5606
5607QIcon QStyleSheetStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption *opt,
5608 const QWidget *w) const
5609{
5610 RECURSION_GUARD(return baseStyle()->standardIcon(standardIcon, opt, w))
5611 const auto s = propertyNameForStandardPixmap(standardIcon);
5612 if (!s.isEmpty()) {
5613 const auto styleHint = renderRule(w, opt).styleHint(s);
5614 if (styleHint.isValid() && styleHint.canConvert<QIcon>())
5615 return qvariant_cast<QIcon>(styleHint);
5616 }
5617 return baseStyle()->standardIcon(standardIcon, opt, w);
5618}
5619
5620QPalette QStyleSheetStyle::standardPalette() const
5621{
5622 return baseStyle()->standardPalette();
5623}
5624
5625QPixmap QStyleSheetStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt,
5626 const QWidget *w) const
5627{
5628 RECURSION_GUARD(return baseStyle()->standardPixmap(standardPixmap, opt, w))
5629 const auto s = propertyNameForStandardPixmap(standardPixmap);
5630 if (!s.isEmpty()) {
5631 const auto styleHint = renderRule(w, opt).styleHint(s);
5632 if (styleHint.isValid() && styleHint.canConvert<QIcon>()) {
5633 QIcon icon = qvariant_cast<QIcon>(styleHint);
5634 return icon.pixmap(QSize(16, 16), QStyleHelper::getDpr(w));
5635 }
5636 }
5637 return baseStyle()->standardPixmap(standardPixmap, opt, w);
5638}
5639
5640int QStyleSheetStyle::layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2,
5641 Qt::Orientation orientation, const QStyleOption *option,
5642 const QWidget *widget) const
5643{
5644 return baseStyle()->layoutSpacing(control1, control2, orientation, option, widget);
5645}
5646
5647int QStyleSheetStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w,
5648 QStyleHintReturn *shret) const
5649{
5650 RECURSION_GUARD(return baseStyle()->styleHint(sh, opt, w, shret))
5651 // Prevent endless loop if somebody use isActiveWindow property as selector.
5652 // QWidget::isActiveWindow uses this styleHint to determine if the window is active or not
5653 if (sh == SH_Widget_ShareActivation)
5654 return baseStyle()->styleHint(sh, opt, w, shret);
5655
5656 QRenderRule rule = renderRule(w, opt);
5657 QLatin1StringView s;
5658 switch (sh) {
5659 case SH_LineEdit_PasswordCharacter: s = "lineedit-password-character"_L1; break;
5660 case SH_LineEdit_PasswordMaskDelay: s = "lineedit-password-mask-delay"_L1; break;
5661 case SH_DitherDisabledText: s = "dither-disabled-text"_L1; break;
5662 case SH_EtchDisabledText: s = "etch-disabled-text"_L1; break;
5663 case SH_ItemView_ActivateItemOnSingleClick: s = "activate-on-singleclick"_L1; break;
5664 case SH_ItemView_ShowDecorationSelected: s = "show-decoration-selected"_L1; break;
5665 case SH_Table_GridLineColor: s = "gridline-color"_L1; break;
5666 case SH_DialogButtonLayout: s = "button-layout"_L1; break;
5667 case SH_ToolTipLabel_Opacity: s = "opacity"_L1; break;
5668 case SH_ComboBox_Popup: s = "combobox-popup"_L1; break;
5669 case SH_ComboBox_ListMouseTracking: s = "combobox-list-mousetracking"_L1; break;
5670 case SH_MenuBar_AltKeyNavigation: s = "menubar-altkey-navigation"_L1; break;
5671 case SH_Menu_Scrollable: s = "menu-scrollable"_L1; break;
5672 case SH_DrawMenuBarSeparator: s = "menubar-separator"_L1; break;
5673 case SH_MenuBar_MouseTracking: s = "mouse-tracking"_L1; break;
5674 case SH_SpinBox_ClickAutoRepeatRate: s = "spinbox-click-autorepeat-rate"_L1; break;
5675 case SH_SpinControls_DisableOnBounds: s = "spincontrol-disable-on-bounds"_L1; break;
5676 case SH_MessageBox_TextInteractionFlags: s = "messagebox-text-interaction-flags"_L1; break;
5677 case SH_ToolButton_PopupDelay: s = "toolbutton-popup-delay"_L1; break;
5678 case SH_ToolBox_SelectedPageTitleBold:
5679 if (renderRule(w, opt, PseudoElement_ToolBoxTab).hasFont)
5680 return 0;
5681 break;
5682 case SH_GroupBox_TextLabelColor:
5683 if (rule.hasPalette() && rule.palette()->foreground.style() != Qt::NoBrush)
5684 return rule.palette()->foreground.color().rgba();
5685 break;
5686 case SH_ScrollView_FrameOnlyAroundContents: s = "scrollview-frame-around-contents"_L1; break;
5687 case SH_ScrollBar_ContextMenu: s = "scrollbar-contextmenu"_L1; break;
5688 case SH_ScrollBar_LeftClickAbsolutePosition: s = "scrollbar-leftclick-absolute-position"_L1; break;
5689 case SH_ScrollBar_MiddleClickAbsolutePosition: s = "scrollbar-middleclick-absolute-position"_L1; break;
5690 case SH_ScrollBar_RollBetweenButtons: s = "scrollbar-roll-between-buttons"_L1; break;
5691 case SH_ScrollBar_ScrollWhenPointerLeavesControl: s = "scrollbar-scroll-when-pointer-leaves-control"_L1; break;
5692 case SH_TabBar_Alignment:
5693#if QT_CONFIG(tabwidget)
5694 if (qobject_cast<const QTabWidget *>(w)) {
5695 rule = renderRule(w, opt, PseudoElement_TabWidgetTabBar);
5696 if (rule.hasPosition())
5697 return rule.position()->position;
5698 }
5699#endif // QT_CONFIG(tabwidget)
5700 s = "alignment"_L1;
5701 break;
5702#if QT_CONFIG(tabbar)
5703 case SH_TabBar_CloseButtonPosition:
5704 rule = renderRule(w, opt, PseudoElement_TabBarTabCloseButton);
5705 if (rule.hasPosition()) {
5706 Qt::Alignment align = rule.position()->position;
5707 if (align & Qt::AlignLeft || align & Qt::AlignTop)
5708 return QTabBar::LeftSide;
5709 if (align & Qt::AlignRight || align & Qt::AlignBottom)
5710 return QTabBar::RightSide;
5711 }
5712 break;
5713#endif
5714 case SH_TabBar_ElideMode: s = "tabbar-elide-mode"_L1; break;
5715 case SH_TabBar_PreferNoArrows: s = "tabbar-prefer-no-arrows"_L1; break;
5716 case SH_ComboBox_PopupFrameStyle:
5717#if QT_CONFIG(combobox)
5718 if (qobject_cast<const QComboBox *>(w)) {
5719 QAbstractItemView *view = w->findChild<QAbstractItemView *>();
5720 if (view) {
5721 view->ensurePolished();
5722 QRenderRule subRule = renderRule(view, PseudoElement_None);
5723 if (subRule.hasBox() || !subRule.hasNativeBorder())
5724 return QFrame::NoFrame;
5725 }
5726 }
5727#endif // QT_CONFIG(combobox)
5728 break;
5729 case SH_DialogButtonBox_ButtonsHaveIcons: s = "dialogbuttonbox-buttons-have-icons"_L1; break;
5730 case SH_Workspace_FillSpaceOnMaximize: s = "mdi-fill-space-on-maximize"_L1; break;
5731 case SH_TitleBar_NoBorder:
5732 if (rule.hasBorder())
5733 return !rule.border()->borders[LeftEdge];
5734 break;
5735 case SH_TitleBar_AutoRaise: { // plain absurd
5736 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
5737 if (subRule.hasDrawable())
5738 return 1;
5739 break;
5740 }
5741 case SH_ItemView_ArrowKeysNavigateIntoChildren: s = "arrow-keys-navigate-into-children"_L1; break;
5742 case SH_ItemView_PaintAlternatingRowColorsForEmptyArea: s = "paint-alternating-row-colors-for-empty-area"_L1; break;
5743 case SH_TitleBar_ShowToolTipsOnButtons: s = "titlebar-show-tooltips-on-buttons"_L1; break;
5744 case SH_Widget_Animation_Duration: s = "widget-animation-duration"_L1; break;
5745 case SH_ScrollBar_Transient:
5746 if (!rule.hasNativeBorder() || rule.hasBox() || rule.hasDrawable())
5747 return 0;
5748 break;
5749 default: break;
5750 }
5751 if (!s.isEmpty()) {
5752 const auto styleHint = rule.styleHint(s);
5753 if (styleHint.isValid() && styleHint.canConvert<int>())
5754 return styleHint.toInt();
5755 }
5756
5757 return baseStyle()->styleHint(sh, opt, w, shret);
5758}
5759
5760QRect QStyleSheetStyle::subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc,
5761 const QWidget *w) const
5762{
5763 RECURSION_GUARD(return baseStyle()->subControlRect(cc, opt, sc, w))
5764
5765 QRenderRule rule = renderRule(w, opt);
5766 switch (cc) {
5767 case CC_ComboBox:
5768 if (const QStyleOptionComboBox *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
5769 if (rule.hasBox() || !rule.hasNativeBorder()) {
5770 switch (sc) {
5771 case SC_ComboBoxFrame: return rule.borderRect(opt->rect);
5772 case SC_ComboBoxEditField:
5773 {
5774 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
5775 QRect r = rule.contentsRect(opt->rect);
5776 QRect r2 = positionRect(w, rule, subRule, PseudoElement_ComboBoxDropDown,
5777 opt->rect, opt->direction);
5778 if (subRule.hasPosition() && subRule.position()->position & Qt::AlignLeft) {
5779 return visualRect(opt->direction, r, r.adjusted(r2.width(),0,0,0));
5780 } else {
5781 return visualRect(opt->direction, r, r.adjusted(0,0,-r2.width(),0));
5782 }
5783 }
5784 case SC_ComboBoxArrow: {
5785 QRenderRule subRule = renderRule(w, opt, PseudoElement_ComboBoxDropDown);
5786 return positionRect(w, rule, subRule, PseudoElement_ComboBoxDropDown, opt->rect, opt->direction);
5787 }
5788 case SC_ComboBoxListBoxPopup:
5789 default:
5790 return baseStyle()->subControlRect(cc, opt, sc, w);
5791 }
5792 }
5793
5794 QStyleOptionComboBox comboBox(*cb);
5795 comboBox.rect = rule.borderRect(opt->rect);
5796 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &comboBox, sc, w)
5797 : QWindowsStyle::subControlRect(cc, &comboBox, sc, w);
5798 }
5799 break;
5800
5801#if QT_CONFIG(spinbox)
5802 case CC_SpinBox:
5803 if (const QStyleOptionSpinBox *spin = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) {
5804 QRenderRule upRule = renderRule(w, opt, PseudoElement_SpinBoxUpButton);
5805 QRenderRule downRule = renderRule(w, opt, PseudoElement_SpinBoxDownButton);
5806 bool ruleMatch = rule.hasBox() || !rule.hasNativeBorder();
5807 bool upRuleMatch = upRule.hasGeometry() || upRule.hasPosition();
5808 bool downRuleMatch = downRule.hasGeometry() || downRule.hasPosition();
5809 if (ruleMatch || upRuleMatch || downRuleMatch) {
5810 switch (sc) {
5811 case SC_SpinBoxFrame:
5812 return rule.borderRect(opt->rect);
5813 case SC_SpinBoxEditField:
5814 {
5815 QRect r = rule.contentsRect(opt->rect);
5816 // Use the widest button on each side to determine edit field size.
5817 Qt::Alignment upAlign, downAlign;
5818
5819 upAlign = upRule.hasPosition() ? upRule.position()->position
5820 : Qt::Alignment(Qt::AlignRight);
5821 upAlign = resolveAlignment(opt->direction, upAlign);
5822
5823 downAlign = downRule.hasPosition() ? downRule.position()->position
5824 : Qt::Alignment(Qt::AlignRight);
5825 downAlign = resolveAlignment(opt->direction, downAlign);
5826
5827 const bool hasButtons = (spin->buttonSymbols != QAbstractSpinBox::NoButtons);
5828 const int upSize = hasButtons
5829 ? subControlRect(CC_SpinBox, opt, SC_SpinBoxUp, w).width() : 0;
5830 const int downSize = hasButtons
5831 ? subControlRect(CC_SpinBox, opt, SC_SpinBoxDown, w).width() : 0;
5832
5833 int widestL = qMax((upAlign & Qt::AlignLeft) ? upSize : 0,
5834 (downAlign & Qt::AlignLeft) ? downSize : 0);
5835 int widestR = qMax((upAlign & Qt::AlignRight) ? upSize : 0,
5836 (downAlign & Qt::AlignRight) ? downSize : 0);
5837 r.setRight(r.right() - widestR);
5838 r.setLeft(r.left() + widestL);
5839 return r;
5840 }
5841 case SC_SpinBoxDown:
5842 if (downRuleMatch)
5843 return positionRect(w, rule, downRule, PseudoElement_SpinBoxDownButton,
5844 opt->rect, opt->direction);
5845 break;
5846 case SC_SpinBoxUp:
5847 if (upRuleMatch)
5848 return positionRect(w, rule, upRule, PseudoElement_SpinBoxUpButton,
5849 opt->rect, opt->direction);
5850 break;
5851 default:
5852 break;
5853 }
5854
5855 return baseStyle()->subControlRect(cc, opt, sc, w);
5856 }
5857
5858 QStyleOptionSpinBox spinBox(*spin);
5859 spinBox.rect = rule.borderRect(opt->rect);
5860 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &spinBox, sc, w)
5861 : QWindowsStyle::subControlRect(cc, &spinBox, sc, w);
5862 }
5863 break;
5864#endif // QT_CONFIG(spinbox)
5865
5866 case CC_GroupBox:
5867 if (const QStyleOptionGroupBox *gb = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
5868 switch (sc) {
5869 case SC_GroupBoxFrame:
5870 case SC_GroupBoxContents: {
5871 if (rule.hasBox() || !rule.hasNativeBorder()) {
5872 return sc == SC_GroupBoxFrame ? rule.borderRect(opt->rect)
5873 : rule.contentsRect(opt->rect);
5874 }
5875 QStyleOptionGroupBox groupBox(*gb);
5876 groupBox.rect = rule.borderRect(opt->rect);
5877 return baseStyle()->subControlRect(cc, &groupBox, sc, w);
5878 }
5879 default:
5880 case SC_GroupBoxLabel:
5881 case SC_GroupBoxCheckBox: {
5882 QRenderRule indRule = renderRule(w, opt, PseudoElement_GroupBoxIndicator);
5883 QRenderRule labelRule = renderRule(w, opt, PseudoElement_GroupBoxTitle);
5884 if (!labelRule.hasPosition() && !labelRule.hasGeometry() && !labelRule.hasBox()
5885 && !labelRule.hasBorder() && !indRule.hasContentsSize()) {
5886 QStyleOptionGroupBox groupBox(*gb);
5887 groupBox.rect = rule.borderRect(opt->rect);
5888 return baseStyle()->subControlRect(cc, &groupBox, sc, w);
5889 }
5890 int tw = opt->fontMetrics.horizontalAdvance(gb->text);
5891 int th = opt->fontMetrics.height();
5892 int spacing = pixelMetric(QStyle::PM_CheckBoxLabelSpacing, opt, w);
5893 int iw = pixelMetric(QStyle::PM_IndicatorWidth, opt, w);
5894 int ih = pixelMetric(QStyle::PM_IndicatorHeight, opt, w);
5895
5896 if (gb->subControls & QStyle::SC_GroupBoxCheckBox) {
5897 tw = tw + iw + spacing;
5898 th = qMax(th, ih);
5899 }
5900 if (!labelRule.hasGeometry()) {
5901 labelRule.geo = new QStyleSheetGeometryData(tw, th, tw, th, -1, -1);
5902 } else {
5903 labelRule.geo->width = tw;
5904 labelRule.geo->height = th;
5905 }
5906 if (!labelRule.hasPosition()) {
5907 labelRule.p = new QStyleSheetPositionData(0, 0, 0, 0, defaultOrigin(PseudoElement_GroupBoxTitle),
5908 gb->textAlignment, PositionMode_Static);
5909 }
5910 QRect r = positionRect(w, rule, labelRule, PseudoElement_GroupBoxTitle,
5911 opt->rect, opt->direction);
5912 if (gb->subControls & SC_GroupBoxCheckBox) {
5913 r = labelRule.contentsRect(r);
5914 if (sc == SC_GroupBoxLabel) {
5915 r.setLeft(r.left() + iw + spacing);
5916 r.setTop(r.center().y() - th/2);
5917 } else {
5918 r = QRect(r.left(), r.center().y() - ih/2, iw, ih);
5919 }
5920 return r;
5921 } else {
5922 return labelRule.contentsRect(r);
5923 }
5924 }
5925 } // switch
5926 }
5927 break;
5928
5929 case CC_ToolButton:
5930#if QT_CONFIG(toolbutton)
5931 if (const QStyleOptionToolButton *tb = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
5932 if (rule.hasBox() || !rule.hasNativeBorder()) {
5933 switch (sc) {
5934 case SC_ToolButton: return rule.borderRect(opt->rect);
5935 case SC_ToolButtonMenu: {
5936 QRenderRule subRule = renderRule(w, opt, PseudoElement_ToolButtonMenu);
5937 return positionRect(w, rule, subRule, PseudoElement_ToolButtonMenu, opt->rect, opt->direction);
5938 }
5939 default:
5940 break;
5941 }
5942 }
5943
5944 QStyleOptionToolButton tool(*tb);
5945 tool.rect = rule.borderRect(opt->rect);
5946 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &tool, sc, w)
5947 : QWindowsStyle::subControlRect(cc, &tool, sc, w);
5948 }
5949#endif // QT_CONFIG(toolbutton)
5950 break;
5951
5952#if QT_CONFIG(scrollbar)
5953 case CC_ScrollBar:
5954 if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
5955 QStyleOptionSlider styleOptionSlider(*sb);
5956 styleOptionSlider.rect = rule.borderRect(opt->rect);
5957 if (rule.hasDrawable() || rule.hasBox()) {
5958 QRect grooveRect;
5959 if (!rule.hasBox()) {
5960 grooveRect = rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, sb, SC_ScrollBarGroove, w)
5961 : QWindowsStyle::subControlRect(cc, sb, SC_ScrollBarGroove, w);
5962 } else {
5963 grooveRect = rule.contentsRect(opt->rect);
5964 }
5965
5966 PseudoElement pe = PseudoElement_None;
5967
5968 switch (sc) {
5969 case SC_ScrollBarGroove:
5970 return grooveRect;
5971 case SC_ScrollBarAddPage:
5972 case SC_ScrollBarSubPage:
5973 case SC_ScrollBarSlider: {
5974 QRect contentRect = grooveRect;
5975 if (hasStyleRule(w, PseudoElement_ScrollBarSlider)) {
5976 QRenderRule sliderRule = renderRule(w, opt, PseudoElement_ScrollBarSlider);
5977 Origin origin = sliderRule.hasPosition() ? sliderRule.position()->origin : defaultOrigin(PseudoElement_ScrollBarSlider);
5978 contentRect = rule.originRect(opt->rect, origin);
5979 }
5980 int maxlen = (styleOptionSlider.orientation == Qt::Horizontal) ? contentRect.width() : contentRect.height();
5981 int sliderlen;
5982 if (sb->maximum != sb->minimum) {
5983 uint range = sb->maximum - sb->minimum;
5984 sliderlen = (qint64(sb->pageStep) * maxlen) / (range + sb->pageStep);
5985
5986 int slidermin = pixelMetric(PM_ScrollBarSliderMin, sb, w);
5987 if (sliderlen < slidermin || range > INT_MAX / 2)
5988 sliderlen = slidermin;
5989 if (sliderlen > maxlen)
5990 sliderlen = maxlen;
5991 } else {
5992 sliderlen = maxlen;
5993 }
5994 int sliderstart = (styleOptionSlider.orientation == Qt::Horizontal ? contentRect.left() : contentRect.top())
5995 + sliderPositionFromValue(sb->minimum, sb->maximum, sb->sliderPosition,
5996 maxlen - sliderlen, sb->upsideDown);
5997
5998 QRect sr = (sb->orientation == Qt::Horizontal)
5999 ? QRect(sliderstart, contentRect.top(), sliderlen, contentRect.height())
6000 : QRect(contentRect.left(), sliderstart, contentRect.width(), sliderlen);
6001 if (sc == SC_ScrollBarSubPage)
6002 sr = QRect(contentRect.topLeft(), sb->orientation == Qt::Horizontal ? sr.bottomLeft() : sr.topRight());
6003 else if (sc == SC_ScrollBarAddPage)
6004 sr = QRect(sb->orientation == Qt::Horizontal ? sr.topRight() : sr.bottomLeft(), contentRect.bottomRight());
6005 return visualRect(styleOptionSlider.direction, grooveRect, sr);
6006 }
6007 case SC_ScrollBarAddLine: pe = PseudoElement_ScrollBarAddLine; break;
6008 case SC_ScrollBarSubLine: pe = PseudoElement_ScrollBarSubLine; break;
6009 case SC_ScrollBarFirst: pe = PseudoElement_ScrollBarFirst; break;
6010 case SC_ScrollBarLast: pe = PseudoElement_ScrollBarLast; break;
6011 default: break;
6012 }
6013 if (hasStyleRule(w,pe)) {
6014 QRenderRule subRule = renderRule(w, opt, pe);
6015 if (subRule.hasPosition() || subRule.hasGeometry() || subRule.hasBox()) {
6016 const QStyleSheetPositionData *pos = subRule.position();
6017 QRect originRect = grooveRect;
6018 if (rule.hasBox()) {
6019 Origin origin = (pos && pos->origin != Origin_Unknown) ? pos->origin : defaultOrigin(pe);
6020 originRect = rule.originRect(opt->rect, origin);
6021 }
6022 return positionRect(w, subRule, pe, originRect, styleOptionSlider.direction);
6023 }
6024 }
6025 }
6026 return rule.baseStyleCanDraw() ? baseStyle()->subControlRect(cc, &styleOptionSlider, sc, w)
6027 : QWindowsStyle::subControlRect(cc, &styleOptionSlider, sc, w);
6028 }
6029 break;
6030#endif // QT_CONFIG(scrollbar)
6031
6032#if QT_CONFIG(slider)
6033 case CC_Slider:
6034 if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
6035 QRenderRule subRule = renderRule(w, opt, PseudoElement_SliderGroove);
6036 if (!subRule.hasDrawable())
6037 break;
6038 subRule.img = nullptr;
6039 QRect gr = positionRect(w, rule, subRule, PseudoElement_SliderGroove, opt->rect, opt->direction);
6040 switch (sc) {
6041 case SC_SliderGroove:
6042 return gr;
6043 case SC_SliderHandle: {
6044 bool horizontal = slider->orientation & Qt::Horizontal;
6045 QRect cr = subRule.contentsRect(gr);
6046 QRenderRule subRule2 = renderRule(w, opt, PseudoElement_SliderHandle);
6047 int len = horizontal ? subRule2.size().width() : subRule2.size().height();
6048 subRule2.img = nullptr;
6049 subRule2.geo = nullptr;
6050 cr = positionRect(w, subRule2, PseudoElement_SliderHandle, cr, opt->direction);
6051 int thickness = horizontal ? cr.height() : cr.width();
6052 int sliderPos = sliderPositionFromValue(slider->minimum, slider->maximum, slider->sliderPosition,
6053 (horizontal ? cr.width() : cr.height()) - len, slider->upsideDown);
6054 cr = horizontal ? QRect(cr.x() + sliderPos, cr.y(), len, thickness)
6055 : QRect(cr.x(), cr.y() + sliderPos, thickness, len);
6056 return subRule2.borderRect(cr);
6057 break; }
6058 case SC_SliderTickmarks:
6059 // TODO...
6060 default:
6061 break;
6062 }
6063 }
6064 break;
6065#endif // QT_CONFIG(slider)
6066
6067 case CC_MdiControls:
6068 if (hasStyleRule(w, PseudoElement_MdiCloseButton)
6069 || hasStyleRule(w, PseudoElement_MdiNormalButton)
6070 || hasStyleRule(w, PseudoElement_MdiMinButton)) {
6071 QList<QVariant> layout = rule.styleHint("button-layout"_L1).toList();
6072 if (layout.isEmpty())
6073 layout = subControlLayout("mNX");
6074
6075 int x = 0, width = 0;
6076 QRenderRule subRule;
6077 for (const QVariant &val : std::as_const(layout)) {
6078 int layoutButton = val.toInt();
6079 if (layoutButton < PseudoElement_MdiCloseButton
6080 || layoutButton > PseudoElement_MdiNormalButton)
6081 continue;
6082 QStyle::SubControl control = knownPseudoElements[layoutButton].subControl;
6083 if (!(opt->subControls & control))
6084 continue;
6085 subRule = renderRule(w, opt, layoutButton);
6086 width = subRule.size().width();
6087 if (sc == control)
6088 break;
6089 x += width;
6090 }
6091
6092 return subRule.borderRect(QRect(x, opt->rect.top(), width, opt->rect.height()));
6093 }
6094 break;
6095
6096 case CC_TitleBar:
6097 if (const QStyleOptionTitleBar *tb = qstyleoption_cast<const QStyleOptionTitleBar *>(opt)) {
6098 QRenderRule subRule = renderRule(w, opt, PseudoElement_TitleBar);
6099 if (!subRule.hasDrawable() && !subRule.hasBox() && !subRule.hasBorder())
6100 break;
6101 QHash<QStyle::SubControl, QRect> layoutRects = titleBarLayout(w, tb);
6102 return layoutRects.value(sc);
6103 }
6104 break;
6105
6106 default:
6107 break;
6108 }
6109
6110 return baseStyle()->subControlRect(cc, opt, sc, w);
6111}
6112
6113QRect QStyleSheetStyle::subElementRect(SubElement se, const QStyleOption *opt, const QWidget *w) const
6114{
6115 RECURSION_GUARD(return baseStyle()->subElementRect(se, opt, w))
6116
6117 QRenderRule rule = renderRule(w, opt);
6118#if QT_CONFIG(tabbar)
6119 int pe = PseudoElement_None;
6120#endif
6121
6122 switch (se) {
6123 case SE_PushButtonContents:
6124 case SE_PushButtonBevel:
6125 case SE_PushButtonFocusRect:
6126 if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt)) {
6127 if (btn->features & QStyleOptionButton::HasMenu
6128 && hasStyleRule(w, PseudoElement_PushButtonMenuIndicator)) {
6129 QStyleOptionButton btnOpt(*btn);
6130 btnOpt.features &= ~QStyleOptionButton::HasMenu;
6131 return rule.baseStyleCanDraw() ? baseStyle()->subElementRect(se, &btnOpt, w)
6132 : QWindowsStyle::subElementRect(se, &btnOpt, w);
6133 }
6134 if (rule.hasBox() || !rule.hasNativeBorder()) {
6135 return visualRect(opt->direction, opt->rect, se == SE_PushButtonBevel
6136 ? rule.borderRect(opt->rect)
6137 : rule.contentsRect(opt->rect));
6138 }
6139 return rule.baseStyleCanDraw() ? baseStyle()->subElementRect(se, btn, w)
6140 : QWindowsStyle::subElementRect(se, btn, w);
6141 }
6142 break;
6143
6144 case SE_LineEditContents:
6145 case SE_FrameContents:
6146 case SE_ShapedFrameContents:
6147 if (rule.hasBox() || !rule.hasNativeBorder()) {
6148 return visualRect(opt->direction, opt->rect, rule.contentsRect(opt->rect));
6149 }
6150 break;
6151
6152 case SE_CheckBoxIndicator:
6153 case SE_RadioButtonIndicator:
6154 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
6155 PseudoElement pe = se == SE_CheckBoxIndicator ? PseudoElement_Indicator : PseudoElement_ExclusiveIndicator;
6156 QRenderRule subRule = renderRule(w, opt, pe);
6157 return positionRect(w, rule, subRule, pe, opt->rect, opt->direction);
6158 }
6159 break;
6160
6161 case SE_CheckBoxContents:
6162 case SE_RadioButtonContents:
6163 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
6164 bool isRadio = se == SE_RadioButtonContents;
6165 QRect ir = subElementRect(isRadio ? SE_RadioButtonIndicator : SE_CheckBoxIndicator,
6166 opt, w);
6167 ir = visualRect(opt->direction, opt->rect, ir);
6168 int spacing = pixelMetric(isRadio ? PM_RadioButtonLabelSpacing : PM_CheckBoxLabelSpacing, nullptr, w);
6169 QRect cr = rule.contentsRect(opt->rect);
6170 ir.setRect(ir.left() + ir.width() + spacing, cr.y(),
6171 cr.width() - ir.width() - spacing, cr.height());
6172 return visualRect(opt->direction, opt->rect, ir);
6173 }
6174 break;
6175
6176 case SE_ToolBoxTabContents:
6177 if (w && hasStyleRule(w->parentWidget(), PseudoElement_ToolBoxTab)) {
6178 QRenderRule subRule = renderRule(w->parentWidget(), opt, PseudoElement_ToolBoxTab);
6179 return visualRect(opt->direction, opt->rect, subRule.contentsRect(opt->rect));
6180 }
6181 break;
6182
6183 case SE_RadioButtonFocusRect:
6184 case SE_RadioButtonClickRect: // focusrect | indicator
6185 if (rule.hasBox() || rule.hasBorder() || hasStyleRule(w, PseudoElement_Indicator)) {
6186 return opt->rect;
6187 }
6188 break;
6189
6190 case SE_CheckBoxFocusRect:
6191 case SE_CheckBoxClickRect: // relies on indicator and contents
6192 return ParentStyle::subElementRect(se, opt, w);
6193
6194#if QT_CONFIG(itemviews)
6195 case SE_ItemViewItemCheckIndicator:
6196 if (!qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
6197 return subElementRect(SE_CheckBoxIndicator, opt, w);
6198 }
6199 Q_FALLTHROUGH();
6200 case SE_ItemViewItemText:
6201 case SE_ItemViewItemDecoration:
6202 case SE_ItemViewItemFocusRect:
6203 if (const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)) {
6204 QRenderRule subRule = renderRule(w, opt, PseudoElement_ViewItem);
6205 PseudoElement pe = PseudoElement_None;
6206 if (se == SE_ItemViewItemText || se == SE_ItemViewItemFocusRect)
6207 pe = PseudoElement_ViewItemText;
6208 else if (se == SE_ItemViewItemDecoration && vopt->features & QStyleOptionViewItem::HasDecoration)
6209 pe = PseudoElement_ViewItemIcon;
6210 else if (se == SE_ItemViewItemCheckIndicator && vopt->features & QStyleOptionViewItem::HasCheckIndicator)
6211 pe = PseudoElement_ViewItemIndicator;
6212 else
6213 break;
6214 if (subRule.hasGeometry() || subRule.hasBox() || !subRule.hasNativeBorder() || hasStyleRule(w, pe)) {
6215 QRenderRule subRule2 = renderRule(w, opt, pe);
6216 QStyleOptionViewItem optCopy(*vopt);
6217 optCopy.rect = subRule.contentsRect(vopt->rect);
6218 QRect rect = ParentStyle::subElementRect(se, &optCopy, w);
6219 return positionRect(w, subRule2, pe, rect, opt->direction);
6220 }
6221 }
6222 break;
6223#endif // QT_CONFIG(itemviews)
6224
6225 case SE_HeaderArrow: {
6226 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewUpArrow);
6227 if (subRule.hasPosition() || subRule.hasGeometry())
6228 return positionRect(w, rule, subRule, PseudoElement_HeaderViewUpArrow, opt->rect, opt->direction);
6229 }
6230 break;
6231
6232 case SE_HeaderLabel: {
6233 QRenderRule subRule = renderRule(w, opt, PseudoElement_HeaderViewSection);
6234 if (subRule.hasBox() || !subRule.hasNativeBorder()) {
6235 auto r = subRule.contentsRect(opt->rect);
6236 if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
6237 // Subtract width needed for arrow, if there is one
6238 if (header->sortIndicator != QStyleOptionHeader::None) {
6239 const auto arrowRect = subElementRect(SE_HeaderArrow, opt, w);
6240 if (arrowRect.isValid()) {
6241 if (opt->state & State_Horizontal)
6242 r.setWidth(r.width() - arrowRect.width());
6243 else
6244 r.setHeight(r.height() - arrowRect.height());
6245 }
6246 }
6247 }
6248 return r;
6249 }
6250 }
6251 break;
6252
6253 case SE_ProgressBarGroove:
6254 case SE_ProgressBarContents:
6255 case SE_ProgressBarLabel:
6256 if (const QStyleOptionProgressBar *pb = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
6257 if (rule.hasBox() || !rule.hasNativeBorder() || rule.hasPosition() || hasStyleRule(w, PseudoElement_ProgressBarChunk)) {
6258 if (se == SE_ProgressBarGroove)
6259 return rule.borderRect(pb->rect);
6260 else if (se == SE_ProgressBarContents)
6261 return rule.contentsRect(pb->rect);
6262
6263 QSize sz = pb->fontMetrics.size(0, pb->text);
6264 return QStyle::alignedRect(Qt::LeftToRight, rule.hasPosition() ? rule.position()->textAlignment : pb->textAlignment,
6265 sz, pb->rect);
6266 }
6267 }
6268 break;
6269
6270#if QT_CONFIG(tabbar)
6271 case SE_TabWidgetLeftCorner:
6272 pe = PseudoElement_TabWidgetLeftCorner;
6273 Q_FALLTHROUGH();
6274 case SE_TabWidgetRightCorner:
6275 if (pe == PseudoElement_None)
6276 pe = PseudoElement_TabWidgetRightCorner;
6277 Q_FALLTHROUGH();
6278 case SE_TabWidgetTabBar:
6279 if (pe == PseudoElement_None)
6280 pe = PseudoElement_TabWidgetTabBar;
6281 Q_FALLTHROUGH();
6282 case SE_TabWidgetTabPane:
6283 case SE_TabWidgetTabContents:
6284 if (pe == PseudoElement_None)
6285 pe = PseudoElement_TabWidgetPane;
6286
6287 if (hasStyleRule(w, pe)) {
6288 QRect r = QWindowsStyle::subElementRect(pe == PseudoElement_TabWidgetPane ? SE_TabWidgetTabPane : se, opt, w);
6289 QRenderRule subRule = renderRule(w, opt, pe);
6290 r = positionRect(w, subRule, pe, r, opt->direction);
6291 if (pe == PseudoElement_TabWidgetTabBar) {
6292 Q_ASSERT(opt);
6293 r = opt->rect.intersected(r);
6294 }
6295 if (se == SE_TabWidgetTabContents)
6296 r = subRule.contentsRect(r);
6297 return r;
6298 }
6299 break;
6300
6301 case SE_TabBarScrollLeftButton:
6302 case SE_TabBarScrollRightButton:
6303 if (hasStyleRule(w, PseudoElement_TabBarScroller)) {
6304 QStyleSheetProxySaver proxySaver(this);
6305 return baseStyle()->subElementRect(se, opt, w);
6306 }
6307 break;
6308
6309 case SE_TabBarTearIndicator: {
6310 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTear);
6311 if (subRule.hasContentsSize()) {
6312 QRect r;
6313 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
6314 switch (tab->shape) {
6315 case QTabBar::RoundedNorth:
6316 case QTabBar::TriangularNorth:
6317 case QTabBar::RoundedSouth:
6318 case QTabBar::TriangularSouth:
6319 r.setRect(tab->rect.left(), tab->rect.top(), subRule.size().width(), opt->rect.height());
6320 break;
6321 case QTabBar::RoundedWest:
6322 case QTabBar::TriangularWest:
6323 case QTabBar::RoundedEast:
6324 case QTabBar::TriangularEast:
6325 r.setRect(tab->rect.left(), tab->rect.top(), opt->rect.width(), subRule.size().height());
6326 break;
6327 default:
6328 break;
6329 }
6330 r = visualRect(opt->direction, opt->rect, r);
6331 }
6332 return r;
6333 }
6334 break;
6335 }
6336 case SE_TabBarTabText:
6337 case SE_TabBarTabLeftButton:
6338 case SE_TabBarTabRightButton: {
6339 QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab);
6340 if (subRule.hasBox() || !subRule.hasNativeBorder() || subRule.hasFont) {
6341 if (se == SE_TabBarTabText) {
6342 if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) {
6343 const QTabBar *bar = qobject_cast<const QTabBar *>(w);
6344 const QRect optRect = bar && tab->tabIndex != -1 ? bar->tabRect(tab->tabIndex) : opt->rect;
6345 const QRect r = positionRect(w, subRule, PseudoElement_TabBarTab, optRect, opt->direction);
6346 QStyleOptionTab tabCopy(*tab);
6347 if (subRule.hasFont) {
6348 const QFont ruleFont = w ? subRule.font.resolve(w->font()) : subRule.font;
6349 tabCopy.fontMetrics = QFontMetrics(ruleFont);
6350 }
6351 tabCopy.rect = subRule.contentsRect(r);
6352 return ParentStyle::subElementRect(se, &tabCopy, w);
6353 }
6354 }
6355 return ParentStyle::subElementRect(se, opt, w);
6356 }
6357 break;
6358 }
6359#endif // QT_CONFIG(tabbar)
6360
6361 case SE_DockWidgetCloseButton:
6362 case SE_DockWidgetFloatButton: {
6363 PseudoElement pe = (se == SE_DockWidgetCloseButton) ? PseudoElement_DockWidgetCloseButton : PseudoElement_DockWidgetFloatButton;
6364 QRenderRule subRule2 = renderRule(w, opt, pe);
6365 if (!subRule2.hasPosition())
6366 break;
6367 QRenderRule subRule = renderRule(w, opt, PseudoElement_DockWidgetTitle);
6368 return positionRect(w, subRule, subRule2, pe, opt->rect, opt->direction);
6369 }
6370
6371#if QT_CONFIG(toolbar)
6372 case SE_ToolBarHandle:
6373 if (hasStyleRule(w, PseudoElement_ToolBarHandle))
6374 return ParentStyle::subElementRect(se, opt, w);
6375 break;
6376#endif // QT_CONFIG(toolbar)
6377
6378 // On mac we make pixel adjustments to layouts which are not
6379 // desirable when you have custom style sheets on them
6380 case SE_CheckBoxLayoutItem:
6381 case SE_ComboBoxLayoutItem:
6382 case SE_DateTimeEditLayoutItem:
6383 case SE_LabelLayoutItem:
6384 case SE_ProgressBarLayoutItem:
6385 case SE_PushButtonLayoutItem:
6386 case SE_RadioButtonLayoutItem:
6387 case SE_SliderLayoutItem:
6388 case SE_SpinBoxLayoutItem:
6389 case SE_ToolButtonLayoutItem:
6390 case SE_FrameLayoutItem:
6391 case SE_GroupBoxLayoutItem:
6392 case SE_TabWidgetLayoutItem:
6393 if (!rule.hasNativeBorder())
6394 return opt->rect;
6395 break;
6396
6397 default:
6398 break;
6399 }
6400
6401 return baseStyle()->subElementRect(se, opt, w);
6402}
6403
6404bool QStyleSheetStyle::event(QEvent *e)
6405{
6406 return (baseStyle()->event(e) && e->isAccepted()) || ParentStyle::event(e);
6407}
6408
6409void QStyleSheetStyle::updateStyleSheetFont(QWidget* w) const
6410{
6411 // Qt's fontDialog relies on the font of the sample edit for its selection,
6412 // we should never override it.
6413 if (w->objectName() == "qt_fontDialog_sampleEdit"_L1)
6414 return;
6415
6416 QWidget *container = containerWidget(w);
6417 QRenderRule rule = renderRule(container, PseudoElement_None,
6418 PseudoClass_Active | PseudoClass_Enabled | extendedPseudoClass(container));
6419
6420 const bool useStyleSheetPropagationInWidgetStyles =
6421 QCoreApplication::testAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles);
6422
6423 if (useStyleSheetPropagationInWidgetStyles) {
6424 unsetStyleSheetFont(w);
6425
6426 if (rule.font.resolveMask()) {
6427 QFont wf = w->d_func()->localFont();
6428 styleSheetCaches->customFontWidgets.insert(w, {wf, rule.font.resolveMask()});
6429
6430 QFont font = rule.font.resolve(wf);
6431 font.setResolveMask(wf.resolveMask() | rule.font.resolveMask());
6432 w->setFont(font);
6433 }
6434 } else {
6435 QFont wf = w->d_func()->localFont();
6436 QFont font = rule.font.resolve(wf);
6437 font.setResolveMask(wf.resolveMask() | rule.font.resolveMask());
6438
6439 if ((!w->isWindow() || w->testAttribute(Qt::WA_WindowPropagation))
6440 && isNaturalChild(w) && w->parentWidget()) {
6441 const auto parentFont = w->parentWidget()->font();
6442 font = rule.hasFont ? font.resolve(parentFont) : parentFont;
6443 }
6444
6445 if (wf.resolveMask() == font.resolveMask() && wf == font)
6446 return;
6447
6448 w->data->fnt = font;
6449 w->d_func()->directFontResolveMask = font.resolveMask();
6450
6451 QEvent e(QEvent::FontChange);
6452 QCoreApplication::sendEvent(w, &e);
6453 }
6454}
6455
6456void QStyleSheetStyle::saveWidgetFont(QWidget* w, const QFont& font) const
6457{
6458 w->setProperty("_q_styleSheetWidgetFont", font);
6459}
6460
6461void QStyleSheetStyle::clearWidgetFont(QWidget* w) const
6462{
6463 w->setProperty("_q_styleSheetWidgetFont", QVariant());
6464}
6465
6466// Polish palette that should be used for a particular widget, with particular states
6467// (eg. :focus, :hover, ...)
6468// this is called by widgets that paint themself in their paint event
6469// Returns \c true if there is a new palette in pal.
6470bool QStyleSheetStyle::styleSheetPalette(const QWidget* w, const QStyleOption* opt, QPalette* pal)
6471{
6472 if (!w || !opt || !pal)
6473 return false;
6474
6475 RECURSION_GUARD(return false)
6476
6477 w = containerWidget(w);
6478
6479 QRenderRule rule = renderRule(w, PseudoElement_None, pseudoClass(opt->state) | extendedPseudoClass(w));
6480 if (!rule.hasPalette())
6481 return false;
6482
6483 rule.configurePalette(pal, QPalette::NoRole, QPalette::NoRole);
6484 return true;
6485}
6486
6487Qt::Alignment QStyleSheetStyle::resolveAlignment(Qt::LayoutDirection layDir, Qt::Alignment src)
6488{
6489 if (layDir == Qt::LeftToRight || src & Qt::AlignAbsolute)
6490 return src;
6491
6492 if (src & Qt::AlignLeft) {
6493 src &= ~Qt::AlignLeft;
6494 src |= Qt::AlignRight;
6495 } else if (src & Qt::AlignRight) {
6496 src &= ~Qt::AlignRight;
6497 src |= Qt::AlignLeft;
6498 }
6499 src |= Qt::AlignAbsolute;
6500 return src;
6501}
6502
6503// Returns whether the given QWidget has a "natural" parent, meaning that
6504// the parent contains this child as part of its normal operation.
6505// An example is the QTabBar inside a QTabWidget.
6506// This does not mean that any QTabBar which is a child of QTabWidget will
6507// match, only the one that was created by the QTabWidget initialization
6508// (and hence has the correct object name).
6509bool QStyleSheetStyle::isNaturalChild(const QObject *obj)
6510{
6511 if (obj->objectName().startsWith("qt_"_L1))
6512 return true;
6513
6514 return false;
6515}
6516
6517QPixmap QStyleSheetStyle::loadPixmap(const QString &fileName, const QObject *context)
6518{
6519 if (fileName.isEmpty())
6520 return {};
6521
6522 qreal ratio = -1.0;
6523 if (const QWidget *widget = qobject_cast<const QWidget *>(context)) {
6524 if (QScreen *screen = QApplication::screenAt(widget->mapToGlobal(QPoint(0, 0))))
6525 ratio = screen->devicePixelRatio();
6526 }
6527
6528 if (ratio < 0) {
6529 if (const QApplication *app = qApp)
6530 ratio = app->devicePixelRatio();
6531 else
6532 ratio = 1.0;
6533 }
6534
6535 qreal sourceDevicePixelRatio = 1.0;
6536 QString resolvedFileName = qt_findAtNxFile(fileName, ratio, &sourceDevicePixelRatio);
6537 QPixmap pixmap(resolvedFileName);
6538 pixmap.setDevicePixelRatio(sourceDevicePixelRatio);
6539 return pixmap;
6540}
6541
6542QT_END_NAMESPACE
6543
6544#include "moc_qstylesheetstyle_p.cpp"
6545
6546#endif // QT_CONFIG(style_stylesheet)