| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QSTYLESHEETSTYLE_P_H |
| 41 | #define QSTYLESHEETSTYLE_P_H |
| 42 | |
| 43 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 44 | #include "private/qwindowsstyle_p.h" |
| 45 | |
| 46 | #ifndef QT_NO_STYLE_STYLESHEET |
| 47 | |
| 48 | #include "QtWidgets/qapplication.h" |
| 49 | #include "QtWidgets/qstyleoption.h" |
| 50 | #include "QtCore/qhash.h" |
| 51 | #include "QtCore/qlist.h" |
| 52 | #include "QtCore/qset.h" |
| 53 | #include "QtGui/qbrush.h" |
| 54 | #include "QtGui/qevent.h" |
| 55 | #include "private/qcssparser_p.h" |
| 56 | |
| 57 | QT_BEGIN_NAMESPACE |
| 58 | |
| 59 | // |
| 60 | // W A R N I N G |
| 61 | // ------------- |
| 62 | // |
| 63 | // This file is not part of the Qt API. It exists purely as an |
| 64 | // implementation detail. This header file may change from version to |
| 65 | // version without notice, or even be removed. |
| 66 | // |
| 67 | // We mean it. |
| 68 | // |
| 69 | |
| 70 | class QRenderRule; |
| 71 | class QAbstractScrollArea; |
| 72 | class QStyleSheetStylePrivate; |
| 73 | class QStyleOptionTitleBar; |
| 74 | |
| 75 | class Q_AUTOTEST_EXPORT QStyleSheetStyle : public QWindowsStyle |
| 76 | { |
| 77 | typedef QWindowsStyle ParentStyle; |
| 78 | |
| 79 | Q_OBJECT |
| 80 | public: |
| 81 | QStyleSheetStyle(QStyle *baseStyle); |
| 82 | ~QStyleSheetStyle(); |
| 83 | |
| 84 | void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, |
| 85 | const QWidget *w = nullptr) const override; |
| 86 | void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, |
| 87 | const QWidget *w = nullptr) const override; |
| 88 | void drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const override; |
| 89 | void drawItemText(QPainter *painter, const QRect& rect, int alignment, const QPalette &pal, |
| 90 | bool enabled, const QString& text, QPalette::ColorRole textRole = QPalette::NoRole) const override; |
| 91 | void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, |
| 92 | const QWidget *w = nullptr) const override; |
| 93 | QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, |
| 94 | const QStyleOption *option) const override; |
| 95 | SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, |
| 96 | const QPoint &pt, const QWidget *w = nullptr) const override; |
| 97 | QRect itemPixmapRect(const QRect &rect, int alignment, const QPixmap &pixmap) const override; |
| 98 | QRect itemTextRect(const QFontMetrics &metrics, const QRect &rect, int alignment, bool enabled, |
| 99 | const QString &text) const override; |
| 100 | int pixelMetric(PixelMetric metric, const QStyleOption *option = nullptr, const QWidget *widget = nullptr) const override; |
| 101 | void polish(QWidget *widget) override; |
| 102 | void polish(QApplication *app) override; |
| 103 | void polish(QPalette &pal) override; |
| 104 | QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, |
| 105 | const QSize &contentsSize, const QWidget *widget = nullptr) const override; |
| 106 | QPalette standardPalette() const override; |
| 107 | QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *opt = nullptr, |
| 108 | const QWidget *widget = nullptr) const override; |
| 109 | QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *option = nullptr, |
| 110 | const QWidget *w = nullptr ) const override; |
| 111 | int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, |
| 112 | Qt::Orientation orientation, const QStyleOption *option = nullptr, |
| 113 | const QWidget *widget = nullptr) const override; |
| 114 | int styleHint(StyleHint sh, const QStyleOption *opt = nullptr, const QWidget *w = nullptr, |
| 115 | QStyleHintReturn *shret = nullptr) const override; |
| 116 | QRect subElementRect(SubElement r, const QStyleOption *opt, const QWidget *widget = nullptr) const override; |
| 117 | QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, |
| 118 | const QWidget *w = nullptr) const override; |
| 119 | |
| 120 | // These functions are called from QApplication/QWidget. Be careful. |
| 121 | QStyle *baseStyle() const; |
| 122 | void repolish(QWidget *widget); |
| 123 | void repolish(QApplication *app); |
| 124 | |
| 125 | void unpolish(QWidget *widget) override; |
| 126 | void unpolish(QApplication *app) override; |
| 127 | |
| 128 | QStyle *base; |
| 129 | void ref() { ++refcount; } |
| 130 | void deref() { Q_ASSERT(refcount > 0); if (!--refcount) delete this; } |
| 131 | |
| 132 | void updateStyleSheetFont(QWidget* w) const; |
| 133 | void saveWidgetFont(QWidget* w, const QFont& font) const; |
| 134 | void clearWidgetFont(QWidget* w) const; |
| 135 | |
| 136 | bool styleSheetPalette(const QWidget* w, const QStyleOption* opt, QPalette* pal); |
| 137 | |
| 138 | protected: |
| 139 | bool event(QEvent *e) override; |
| 140 | |
| 141 | private: |
| 142 | int refcount; |
| 143 | |
| 144 | friend class QRenderRule; |
| 145 | int nativeFrameWidth(const QWidget *); |
| 146 | QRenderRule renderRule(const QObject *, int, quint64 = 0) const; |
| 147 | QRenderRule renderRule(const QObject *, const QStyleOption *, int = 0) const; |
| 148 | QSize defaultSize(const QWidget *, QSize, const QRect&, int) const; |
| 149 | QRect positionRect(const QWidget *, const QRenderRule&, const QRenderRule&, int, |
| 150 | const QRect&, Qt::LayoutDirection) const; |
| 151 | QRect positionRect(const QWidget *w, const QRenderRule &rule2, int pe, |
| 152 | const QRect &originRect, Qt::LayoutDirection dir) const; |
| 153 | |
| 154 | mutable QCss::Parser parser; |
| 155 | |
| 156 | void setPalette(QWidget *); |
| 157 | void unsetPalette(QWidget *); |
| 158 | void setProperties(QWidget *); |
| 159 | void setGeometry(QWidget *); |
| 160 | void unsetStyleSheetFont(QWidget *) const; |
| 161 | QList<QCss::StyleRule> styleRules(const QObject *obj) const; |
| 162 | bool hasStyleRule(const QObject *obj, int part) const; |
| 163 | |
| 164 | QHash<QStyle::SubControl, QRect> titleBarLayout(const QWidget *w, const QStyleOptionTitleBar *tb) const; |
| 165 | |
| 166 | QCss::StyleSheet getDefaultStyleSheet() const; |
| 167 | |
| 168 | static Qt::Alignment resolveAlignment(Qt::LayoutDirection, Qt::Alignment); |
| 169 | static bool isNaturalChild(const QObject *obj); |
| 170 | static QPixmap loadPixmap(const QString &fileName, const QObject *context); |
| 171 | bool initObject(const QObject *obj) const; |
| 172 | public: |
| 173 | static int numinstances; |
| 174 | |
| 175 | private: |
| 176 | Q_DISABLE_COPY_MOVE(QStyleSheetStyle) |
| 177 | Q_DECLARE_PRIVATE(QStyleSheetStyle) |
| 178 | }; |
| 179 | |
| 180 | class QStyleSheetStyleCaches : public QObject |
| 181 | { |
| 182 | Q_OBJECT |
| 183 | public Q_SLOTS: |
| 184 | void objectDestroyed(QObject *); |
| 185 | void styleDestroyed(QObject *); |
| 186 | public: |
| 187 | QHash<const QObject *, QList<QCss::StyleRule>> styleRulesCache; |
| 188 | QHash<const QObject *, QHash<int, bool> > hasStyleRuleCache; |
| 189 | typedef QHash<int, QHash<quint64, QRenderRule> > QRenderRules; |
| 190 | QHash<const QObject *, QRenderRules> renderRulesCache; |
| 191 | QHash<const void *, QCss::StyleSheet> styleSheetCache; // parsed style sheets |
| 192 | QSet<const QWidget *> autoFillDisabledWidgets; |
| 193 | // widgets with whose palettes and fonts we have tampered: |
| 194 | template <typename T> |
| 195 | struct Tampered { |
| 196 | T oldWidgetValue; |
| 197 | decltype(std::declval<T>().resolveMask()) resolveMask; |
| 198 | |
| 199 | // only call this function on an rvalue *this (it mangles oldWidgetValue) |
| 200 | T reverted(T current) |
| 201 | #ifdef Q_COMPILER_REF_QUALIFIERS |
| 202 | && |
| 203 | #endif |
| 204 | { |
| 205 | oldWidgetValue.setResolveMask(oldWidgetValue.resolveMask() & resolveMask); |
| 206 | current.setResolveMask(current.resolveMask() & ~resolveMask); |
| 207 | current.resolve(oldWidgetValue); |
| 208 | current.setResolveMask(current.resolveMask() | oldWidgetValue.resolveMask()); |
| 209 | return current; |
| 210 | } |
| 211 | }; |
| 212 | QHash<const QWidget *, Tampered<QPalette>> customPaletteWidgets; |
| 213 | QHash<const QWidget *, Tampered<QFont>> customFontWidgets; |
| 214 | }; |
| 215 | template <typename T> |
| 216 | class QTypeInfo<QStyleSheetStyleCaches::Tampered<T>> |
| 217 | : public QTypeInfoMerger<QStyleSheetStyleCaches::Tampered<T>, T> {}; |
| 218 | |
| 219 | |
| 220 | // Returns a QStyleSheet from the given style. |
| 221 | inline QStyleSheetStyle* qt_styleSheet(QStyle *style) |
| 222 | { |
| 223 | return qobject_cast<QStyleSheetStyle *>(style); |
| 224 | } |
| 225 | |
| 226 | QT_END_NAMESPACE |
| 227 | #endif // QT_NO_STYLE_STYLESHEET |
| 228 | #endif // QSTYLESHEETSTYLE_P_H |
| 229 | |