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