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 QGRAPHICSWIDGET_P_H
41 #define QGRAPHICSWIDGET_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists for the convenience
48 // of other Qt classes.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtWidgets/private/qtwidgetsglobal_p.h>
55 #include <private/qobject_p.h>
56 #include "qgraphicsitem_p.h"
57 #include "qgraphicswidget.h"
58 #include <QtGui/qfont.h>
59 #include <QtGui/qpalette.h>
60 #include <QtWidgets/qsizepolicy.h>
61 #include <QtWidgets/qstyle.h>
62 
63 #include <memory>
64 
65 QT_REQUIRE_CONFIG(graphicsview);
66 
67 QT_BEGIN_NAMESPACE
68 
69 class QGraphicsLayout;
70 class QStyleOptionTitleBar;
71 
72 class QGraphicsWidgetPrivate : public QGraphicsItemPrivate
73 {
74     Q_DECLARE_PUBLIC(QGraphicsWidget)
75 public:
76     QGraphicsWidgetPrivate();
77     virtual ~QGraphicsWidgetPrivate();
78 
79     void init(QGraphicsItem *parentItem, Qt::WindowFlags wFlags);
80     qreal titleBarHeight(const QStyleOptionTitleBar &options) const;
81 
82     // Margins
83     mutable std::unique_ptr<QMarginsF> margins;
84     void ensureMargins() const;
85 
86     void fixFocusChainBeforeReparenting(QGraphicsWidget *newParent, QGraphicsScene *oldScene, QGraphicsScene *newScene = nullptr);
87     void setLayout_helper(QGraphicsLayout *l);
88 
89     // Layouts
90     QGraphicsLayout *layout;
91     void setLayoutDirection_helper(Qt::LayoutDirection direction);
92     void resolveLayoutDirection();
93 
94     // Style
95     QPalette palette;
96     uint inheritedPaletteResolveMask;
97     void setPalette_helper(const QPalette &palette);
98     void resolvePalette(uint inheritedMask) override;
99     void updatePalette(const QPalette &palette);
100     QPalette naturalWidgetPalette() const;
101     QFont font;
102     uint inheritedFontResolveMask;
103     void setFont_helper(const QFont &font);
104     void resolveFont(uint inheritedMask) override;
105     void updateFont(const QFont &font);
106     QFont naturalWidgetFont() const;
107 
108     // Window specific
109     void initStyleOptionTitleBar(QStyleOptionTitleBar *option);
110     void adjustWindowFlags(Qt::WindowFlags *wFlags);
111     void windowFrameMouseReleaseEvent(QGraphicsSceneMouseEvent *event);
112     void windowFrameMousePressEvent(QGraphicsSceneMouseEvent *event);
113     void windowFrameMouseMoveEvent(QGraphicsSceneMouseEvent *event);
114     void windowFrameHoverMoveEvent(QGraphicsSceneHoverEvent *event);
115     void windowFrameHoverLeaveEvent(QGraphicsSceneHoverEvent *event);
116     bool hasDecoration() const;
117 
118     // Private Properties
119     qreal width() const override;
120     void setWidth(qreal) override;
121     void resetWidth() override;
122 
123     qreal height() const override;
124     void setHeight(qreal) override;
125     void resetHeight() override;
126     void setGeometryFromSetPos();
127 
128     // State
attributeToBitIndex(Qt::WidgetAttribute att)129     inline int attributeToBitIndex(Qt::WidgetAttribute att) const
130     {
131         int bit = -1;
132         switch (att) {
133         case Qt::WA_SetLayoutDirection: bit = 0; break;
134         case Qt::WA_RightToLeft: bit = 1; break;
135         case Qt::WA_SetStyle: bit = 2; break;
136         case Qt::WA_Resized: bit = 3; break;
137         case Qt::WA_DeleteOnClose: bit = 4; break;
138         case Qt::WA_NoSystemBackground: bit = 5; break;
139         case Qt::WA_OpaquePaintEvent: bit = 6; break;
140         case Qt::WA_SetPalette: bit = 7; break;
141         case Qt::WA_SetFont: bit = 8; break;
142         case Qt::WA_WindowPropagation: bit = 9; break;
143         default: break;
144         }
145         return bit;
146     }
setAttribute(Qt::WidgetAttribute att,bool value)147     inline void setAttribute(Qt::WidgetAttribute att, bool value)
148     {
149         int bit = attributeToBitIndex(att);
150         if (bit == -1) {
151             qWarning("QGraphicsWidget::setAttribute: unsupported attribute %d", int(att));
152             return;
153         }
154         if (value)
155             attributes |= (1 << bit);
156         else
157             attributes &= ~(1 << bit);
158     }
testAttribute(Qt::WidgetAttribute att)159     inline bool testAttribute(Qt::WidgetAttribute att) const
160     {
161         int bit = attributeToBitIndex(att);
162         if (bit == -1)
163             return false;
164         return (attributes & (1 << bit)) != 0;
165     }
166     quint32 attributes : 10;
167     quint32 inSetGeometry : 1;
168     quint32 polished: 1;
169     quint32 inSetPos : 1;
170     quint32 autoFillBackground : 1;
171 
172     // Focus
173     Qt::FocusPolicy focusPolicy;
174     QGraphicsWidget *focusNext;
175     QGraphicsWidget *focusPrev;
176 
177     // Windows
178     Qt::WindowFlags windowFlags;
179     struct WindowData {
180         QString windowTitle;
181         QStyle::SubControl hoveredSubControl;
182         Qt::WindowFrameSection grabbedSection;
183         uint buttonMouseOver : 1;
184         uint buttonSunken : 1;
185         QRectF startGeometry;
186         QRect buttonRect;
WindowDataWindowData187         WindowData()
188             : hoveredSubControl(QStyle::SC_None)
189             , grabbedSection(Qt::NoSection)
190             , buttonMouseOver(false)
191             , buttonSunken(false)
192         {}
193     };
194     std::unique_ptr<WindowData> windowData;
195     void ensureWindowData();
196 
197     bool setWindowFrameMargins;
198     mutable std::unique_ptr<QMarginsF> windowFrameMargins;
199     void ensureWindowFrameMargins() const;
200 
201 #ifndef QT_NO_ACTION
202     QList<QAction *> actions;
203 #endif
204 };
205 
206 QT_END_NAMESPACE
207 
208 #endif //QGRAPHICSWIDGET_P_H
209 
210