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_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSTYLESHEETSTYLE_P_H
6#define QSTYLESHEETSTYLE_P_H
7
8#include <QtWidgets/private/qtwidgetsglobal_p.h>
9
10#if QT_CONFIG(style_windows)
11#include "private/qwindowsstyle_p.h"
12
13#if QT_CONFIG(style_stylesheet)
14
15#include "QtWidgets/qapplication.h"
16#include "QtWidgets/qstyleoption.h"
17#include "QtCore/qhash.h"
18#include "QtCore/qlist.h"
19#include "QtCore/qset.h"
20#include "QtCore/private/qflatmap_p.h"
21#include "QtGui/qbrush.h"
22#include "QtGui/qevent.h"
23#include "private/qcssparser_p.h"
24
25QT_BEGIN_NAMESPACE
26
27//
28// W A R N I N G
29// -------------
30//
31// This file is not part of the Qt API. It exists purely as an
32// implementation detail. This header file may change from version to
33// version without notice, or even be removed.
34//
35// We mean it.
36//
37
38class QRenderRule;
39class QAbstractScrollArea;
40class QStyleSheetStylePrivate;
41class QStyleOptionTitleBar;
42
43class Q_AUTOTEST_EXPORT QStyleSheetStyle : public QWindowsStyle
44{
45 typedef QWindowsStyle ParentStyle;
46
47 Q_OBJECT
48public:
49 QStyleSheetStyle(QStyle *baseStyle);
50 ~QStyleSheetStyle();
51
52 void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p,
53 const QWidget *w = nullptr) const override;
54 void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p,
55 const QWidget *w = nullptr) const override;
56 void drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const override;
57 void drawItemText(QPainter *painter, const QRect& rect, int alignment, const QPalette &pal,
58 bool enabled, const QString& text, QPalette::ColorRole textRole = QPalette::NoRole) const override;
59 void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p,
60 const QWidget *w = nullptr) const override;
61 QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap,
62 const QStyleOption *option) const override;
63 SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt,
64 const QPoint &pt, const QWidget *w = nullptr) const override;
65 QRect itemPixmapRect(const QRect &rect, int alignment, const QPixmap &pixmap) const override;
66 QRect itemTextRect(const QFontMetrics &metrics, const QRect &rect, int alignment, bool enabled,
67 const QString &text) const override;
68 int pixelMetric(PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override;
69 void polish(QWidget *widget) override;
70 void polish(QApplication *app) override;
71 void polish(QPalette &pal) override;
72 QSize sizeFromContents(ContentsType ct, const QStyleOption *opt,
73 const QSize &contentsSize, const QWidget *widget = nullptr) const override;
74 QPalette standardPalette() const override;
75 QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *opt = nullptr,
76 const QWidget *widget = nullptr) const override;
77 QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *option = nullptr,
78 const QWidget *w = nullptr ) const override;
79 int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2,
80 Qt::Orientation orientation, const QStyleOption *option = nullptr,
81 const QWidget *widget = nullptr) const override;
82 int styleHint(StyleHint sh, const QStyleOption *opt = nullptr, const QWidget *w = nullptr,
83 QStyleHintReturn *shret = nullptr) const override;
84 QRect subElementRect(SubElement r, const QStyleOption *opt, const QWidget *widget = nullptr) const override;
85 QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc,
86 const QWidget *w = nullptr) const override;
87
88 // These functions are called from QApplication/QWidget. Be careful.
89 QStyle *baseStyle() const;
90 void repolish(QWidget *widget);
91 void repolish(QApplication *app);
92
93 void unpolish(QWidget *widget) override;
94 void unpolish(QApplication *app) override;
95
96 QStyle *base;
97 void ref() { ++refcount; }
98 void deref() { Q_ASSERT(refcount > 0); if (!--refcount) delete this; }
99
100 void updateStyleSheetFont(QWidget* w) const;
101 void saveWidgetFont(QWidget* w, const QFont& font) const;
102 void clearWidgetFont(QWidget* w) const;
103
104 bool styleSheetPalette(const QWidget* w, const QStyleOption* opt, QPalette* pal);
105
106protected:
107 bool event(QEvent *e) override;
108
109private:
110 int refcount;
111
112 friend class QRenderRule;
113 int nativeFrameWidth(const QWidget *) const;
114 QRenderRule renderRule(const QWidget *, int, quint64 = 0) const;
115 QRenderRule renderRule(const QWidget *, const QStyleOption *, int = 0) const;
116 QSize defaultSize(const QWidget *, QSize, const QRect&, int) const;
117 QRect positionRect(const QWidget *, const QRenderRule&, const QRenderRule&, int,
118 const QRect&, Qt::LayoutDirection) const;
119 QRect positionRect(const QWidget *w, const QRenderRule &rule2, int pe,
120 const QRect &originRect, Qt::LayoutDirection dir) const;
121
122 mutable QCss::Parser parser;
123
124 void setPalette(QWidget *);
125 void unsetPalette(QWidget *);
126 void setProperties(QWidget *);
127 void setGeometry(QWidget *);
128 void unsetStyleSheetFont(QWidget *) const;
129 QList<QCss::StyleRule> styleRules(const QWidget *w) const;
130 bool hasStyleRule(const QWidget *w, int part) const;
131
132 QHash<QStyle::SubControl, QRect> titleBarLayout(const QWidget *w, const QStyleOptionTitleBar *tb) const;
133
134 QCss::StyleSheet getDefaultStyleSheet() const;
135
136 static Qt::Alignment resolveAlignment(Qt::LayoutDirection, Qt::Alignment);
137 static bool isNaturalChild(const QObject *obj);
138 static QPixmap loadPixmap(const QString &fileName, const QObject *context);
139 bool initWidget(const QWidget *w) const;
140 void renderMenuItemIcon(const QStyleOptionMenuItem *mi, QPainter *p, const QWidget *w,
141 const QRect &rect, QRenderRule &subRule) const;
142public:
143 static int numinstances;
144
145private:
146 Q_DISABLE_COPY_MOVE(QStyleSheetStyle)
147 Q_DECLARE_PRIVATE(QStyleSheetStyle)
148};
149
150class QStyleSheetStyleCaches : public QObject
151{
152 Q_OBJECT
153public Q_SLOTS:
154 void objectDestroyed(QObject *);
155 void styleDestroyed(QObject *);
156public:
157 QHash<const QObject *, QList<QCss::StyleRule>> styleRulesCache;
158 QHash<const QObject *, QVarLengthFlatMap<int, bool, 4>> hasStyleRuleCache;
159 typedef QVarLengthFlatMap<int, QHash<quint64, QRenderRule>, 4> QRenderRules;
160 QHash<const QObject *, QRenderRules> renderRulesCache;
161 QHash<const void *, QCss::StyleSheet> styleSheetCache; // parsed style sheets
162 QSet<const QWidget *> autoFillDisabledWidgets;
163 // widgets with whose palettes and fonts we have tampered:
164 template <typename T>
165 struct Tampered {
166 T oldWidgetValue;
167 decltype(std::declval<T>().resolveMask()) resolveMask;
168
169 // only call this function on an rvalue *this (it mangles oldWidgetValue)
170 T reverted(T current)
171#ifdef Q_COMPILER_REF_QUALIFIERS
172 &&
173#endif
174 {
175 oldWidgetValue.setResolveMask(oldWidgetValue.resolveMask() & resolveMask);
176 current.setResolveMask(current.resolveMask() & ~resolveMask);
177 current.resolve(oldWidgetValue);
178 current.setResolveMask(current.resolveMask() | oldWidgetValue.resolveMask());
179 return current;
180 }
181 };
182 QHash<const QWidget *, Tampered<QPalette>> customPaletteWidgets;
183 QHash<const QWidget *, Tampered<QFont>> customFontWidgets;
184};
185template <typename T>
186class QTypeInfo<QStyleSheetStyleCaches::Tampered<T>>
187 : public QTypeInfoMerger<QStyleSheetStyleCaches::Tampered<T>, T> {};
188
189
190// Returns a QStyleSheet from the given style.
191inline QStyleSheetStyle* qt_styleSheet(QStyle *style)
192{
193 return qobject_cast<QStyleSheetStyle *>(style);
194}
195
196QT_END_NAMESPACE
197#endif // QT_NO_STYLE_STYLESHEET
198#endif
199#endif // QSTYLESHEETSTYLE_P_H