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