QmlMaterial 0.1.0
Loading...
Searching...
No Matches
control.hpp
1#pragma once
2#include <QProperty>
3
4#include <QFont>
5#include <QLocale>
6#include <QPointer>
7#include <QQuickItem>
8#include <qqmlregistration.h>
9
10#include <optional>
11
12#include "qml_material/export.hpp"
13#include "qml_material/util/control_environment.hpp"
14
15class QFocusEvent;
16class QHoverEvent;
17
18namespace qml_material
19{
20
21class QML_MATERIAL_API Control : public QQuickItem, public ControlEnvironment {
22 Q_OBJECT
23 QML_NAMED_ELEMENT(Control)
24
25 Q_PROPERTY(QFont font READ font WRITE setFont RESET resetFont NOTIFY fontChanged FINAL)
26 Q_PROPERTY(
27 QLocale locale READ locale WRITE setLocale RESET resetLocale NOTIFY localeChanged FINAL)
28 Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection
29 RESET resetLayoutDirection NOTIFY layoutDirectionChanged FINAL)
30 Q_PROPERTY(bool mirrored READ mirrored NOTIFY mirroredChanged FINAL)
31
32 Q_PROPERTY(qreal availableWidth READ availableWidth NOTIFY availableWidthChanged FINAL)
33 Q_PROPERTY(qreal availableHeight READ availableHeight NOTIFY availableHeightChanged FINAL)
34 Q_PROPERTY(
35 qreal padding READ padding WRITE setPadding RESET resetPadding NOTIFY paddingChanged FINAL)
36 Q_PROPERTY(qreal horizontalPadding READ horizontalPadding WRITE setHorizontalPadding RESET
37 resetHorizontalPadding NOTIFY horizontalPaddingChanged FINAL)
38 Q_PROPERTY(qreal verticalPadding READ verticalPadding WRITE setVerticalPadding RESET
39 resetVerticalPadding NOTIFY verticalPaddingChanged FINAL)
40 Q_PROPERTY(qreal topPadding READ topPadding WRITE setTopPadding RESET resetTopPadding NOTIFY
41 topPaddingChanged FINAL)
42 Q_PROPERTY(qreal leftPadding READ leftPadding WRITE setLeftPadding RESET resetLeftPadding NOTIFY
43 leftPaddingChanged FINAL)
44 Q_PROPERTY(qreal rightPadding READ rightPadding WRITE setRightPadding RESET resetRightPadding
45 NOTIFY rightPaddingChanged FINAL)
46 Q_PROPERTY(qreal bottomPadding READ bottomPadding WRITE setBottomPadding RESET
47 resetBottomPadding NOTIFY bottomPaddingChanged FINAL)
48 Q_PROPERTY(
49 qreal spacing READ spacing WRITE setSpacing RESET resetSpacing NOTIFY spacingChanged FINAL)
50
51 Q_PROPERTY(qreal topInset READ topInset WRITE setTopInset RESET resetTopInset NOTIFY
52 topInsetChanged FINAL)
53 Q_PROPERTY(qreal leftInset READ leftInset WRITE setLeftInset RESET resetLeftInset NOTIFY
54 leftInsetChanged FINAL)
55 Q_PROPERTY(qreal rightInset READ rightInset WRITE setRightInset RESET resetRightInset NOTIFY
56 rightInsetChanged FINAL)
57 Q_PROPERTY(qreal bottomInset READ bottomInset WRITE setBottomInset RESET resetBottomInset NOTIFY
58 bottomInsetChanged FINAL)
59
60 Q_PROPERTY(QQuickItem* background READ background WRITE setBackground NOTIFY backgroundChanged
61 BINDABLE bindableBackground FINAL)
62 Q_PROPERTY(QQuickItem* contentItem READ contentItem WRITE setContentItem NOTIFY
63 contentItemChanged FINAL)
64 Q_PROPERTY(LayoutFlags backgroundLayout READ backgroundLayout WRITE setBackgroundLayout RESET
65 resetBackgroundLayout NOTIFY backgroundLayoutChanged FINAL)
66 Q_PROPERTY(LayoutFlags contentItemLayout READ contentItemLayout WRITE setContentItemLayout RESET
67 resetContentItemLayout NOTIFY contentItemLayoutChanged FINAL)
68
69 Q_PROPERTY(qreal implicitBackgroundWidth READ implicitBackgroundWidth NOTIFY
70 implicitBackgroundWidthChanged FINAL)
71 Q_PROPERTY(qreal implicitBackgroundHeight READ implicitBackgroundHeight NOTIFY
72 implicitBackgroundHeightChanged FINAL)
73 Q_PROPERTY(qreal implicitContentWidth READ implicitContentWidth NOTIFY
74 implicitContentWidthChanged FINAL)
75 Q_PROPERTY(qreal implicitContentHeight READ implicitContentHeight NOTIFY
76 implicitContentHeightChanged FINAL)
77 Q_PROPERTY(
78 qreal implicitLayoutWidth READ implicitLayoutWidth NOTIFY implicitLayoutWidthChanged FINAL)
79 Q_PROPERTY(qreal implicitLayoutHeight READ implicitLayoutHeight NOTIFY
80 implicitLayoutHeightChanged FINAL)
81 Q_PROPERTY(qreal extraImplicitWidth READ extraImplicitWidth WRITE setExtraImplicitWidth RESET
82 resetExtraImplicitWidth NOTIFY extraImplicitWidthChanged FINAL)
83 Q_PROPERTY(qreal extraImplicitHeight READ extraImplicitHeight WRITE setExtraImplicitHeight RESET
84 resetExtraImplicitHeight NOTIFY extraImplicitHeightChanged FINAL)
85
86 Q_PROPERTY(qreal baselineOffset READ baselineOffset WRITE setBaselineOffset RESET
87 resetBaselineOffset NOTIFY baselineOffsetChanged FINAL)
88 Q_PROPERTY(bool hoverEnabled READ hoverEnabled WRITE setHoverEnabled RESET resetHoverEnabled
89 NOTIFY hoverEnabledChanged FINAL)
90
91public:
92 enum LayoutFlag
93 {
94 LayoutNone = 0x0,
95 LayoutX = 0x1,
96 LayoutY = 0x2,
97 LayoutWidth = 0x4,
98 LayoutHeight = 0x8,
99 LayoutPosition = LayoutX | LayoutY,
100 LayoutSize = LayoutWidth | LayoutHeight,
101 LayoutAll = LayoutPosition | LayoutSize
102 };
103 Q_DECLARE_FLAGS(LayoutFlags, LayoutFlag)
104 Q_FLAG(LayoutFlags)
105
106 explicit Control(QQuickItem* parent = nullptr);
107 ~Control() override;
108
109 QFont font() const;
110 QFont effectiveFont() const override { return m_font; }
111 void inheritFont(const QFont&) override;
112 void inheritLocale(const QLocale&) override;
113 void inheritLayoutDirection(Qt::LayoutDirection) override;
114 void inheritHoverEnabled(bool) override;
115 std::optional<QLocale> effectiveLocale() const override { return m_locale; }
116 std::optional<Qt::LayoutDirection> effectiveLayoutDirection() const override {
117 return m_layout_direction;
118 }
119 void setFont(const QFont& value);
120 void resetFont();
121
122 QLocale locale() const;
123 void setLocale(const QLocale& value);
124 void resetLocale();
125
126 Qt::LayoutDirection layoutDirection() const;
127 void setLayoutDirection(Qt::LayoutDirection value);
128 void resetLayoutDirection();
129 bool mirrored() const;
130
131 qreal availableWidth() const;
132 qreal availableHeight() const;
133
134 qreal padding() const;
135 void setPadding(qreal value);
136 void resetPadding();
137
138 qreal horizontalPadding() const;
139 void setHorizontalPadding(qreal value);
140 void resetHorizontalPadding();
141
142 qreal verticalPadding() const;
143 void setVerticalPadding(qreal value);
144 void resetVerticalPadding();
145
146 qreal topPadding() const;
147 void setTopPadding(qreal value);
148 void resetTopPadding();
149
150 qreal leftPadding() const;
151 void setLeftPadding(qreal value);
152 void resetLeftPadding();
153
154 qreal rightPadding() const;
155 void setRightPadding(qreal value);
156 void resetRightPadding();
157
158 qreal bottomPadding() const;
159 void setBottomPadding(qreal value);
160 void resetBottomPadding();
161
162 qreal spacing() const;
163 void setSpacing(qreal value);
164 void resetSpacing();
165
166 qreal topInset() const;
167 void setTopInset(qreal value);
168 void resetTopInset();
169
170 qreal leftInset() const;
171 void setLeftInset(qreal value);
172 void resetLeftInset();
173
174 qreal rightInset() const;
175 void setRightInset(qreal value);
176 void resetRightInset();
177
178 qreal bottomInset() const;
179 void setBottomInset(qreal value);
180 void resetBottomInset();
181
182 QQuickItem* background() const;
183 QBindable<QQuickItem*> bindableBackground() { return QBindable<QQuickItem*>(&m_background); }
184 void setBackground(QQuickItem* item);
185
186 QQuickItem* contentItem() const;
187 void setContentItem(QQuickItem* item);
188
189 LayoutFlags backgroundLayout() const;
190 void setBackgroundLayout(LayoutFlags value);
191 void resetBackgroundLayout();
192
193 LayoutFlags contentItemLayout() const;
194 void setContentItemLayout(LayoutFlags value);
195 void resetContentItemLayout();
196
197 qreal implicitBackgroundWidth() const;
198 qreal implicitBackgroundHeight() const;
199 qreal implicitContentWidth() const;
200 qreal implicitContentHeight() const;
201 qreal implicitLayoutWidth() const;
202 qreal implicitLayoutHeight() const;
203
204 qreal extraImplicitWidth() const;
205 void setExtraImplicitWidth(qreal value);
206 void resetExtraImplicitWidth();
207
208 qreal extraImplicitHeight() const;
209 void setExtraImplicitHeight(qreal value);
210 void resetExtraImplicitHeight();
211
212 qreal baselineOffset() const;
213 void setBaselineOffset(qreal value);
214 void resetBaselineOffset();
215
216 bool hoverEnabled() const;
217 std::optional<bool> effectiveHoverEnabled() const override { return hoverEnabled(); }
218 void setHoverEnabled(bool value);
219 void resetHoverEnabled();
220
221 Q_SIGNAL void fontChanged();
222 Q_SIGNAL void localeChanged();
223 Q_SIGNAL void layoutDirectionChanged();
224 Q_SIGNAL void mirroredChanged();
225 Q_SIGNAL void availableWidthChanged();
226 Q_SIGNAL void availableHeightChanged();
227 Q_SIGNAL void paddingChanged();
228 Q_SIGNAL void horizontalPaddingChanged();
229 Q_SIGNAL void verticalPaddingChanged();
230 Q_SIGNAL void topPaddingChanged();
231 Q_SIGNAL void leftPaddingChanged();
232 Q_SIGNAL void rightPaddingChanged();
233 Q_SIGNAL void bottomPaddingChanged();
234 Q_SIGNAL void spacingChanged();
235 Q_SIGNAL void topInsetChanged();
236 Q_SIGNAL void leftInsetChanged();
237 Q_SIGNAL void rightInsetChanged();
238 Q_SIGNAL void bottomInsetChanged();
239 Q_SIGNAL void backgroundChanged();
240 Q_SIGNAL void contentItemChanged();
241 Q_SIGNAL void backgroundLayoutChanged();
242 Q_SIGNAL void contentItemLayoutChanged();
243 Q_SIGNAL void implicitBackgroundWidthChanged();
244 Q_SIGNAL void implicitBackgroundHeightChanged();
245 Q_SIGNAL void implicitContentWidthChanged();
246 Q_SIGNAL void implicitContentHeightChanged();
247 Q_SIGNAL void implicitLayoutWidthChanged();
248 Q_SIGNAL void implicitLayoutHeightChanged();
249 Q_SIGNAL void extraImplicitWidthChanged();
250 Q_SIGNAL void extraImplicitHeightChanged();
251 Q_SIGNAL void baselineOffsetChanged();
252 Q_SIGNAL void hoverEnabledChanged();
253
254protected:
255 virtual qreal defaultSpacing() const { return 0; }
256 virtual QRectF contentRect() const;
257 void layoutContentItem();
258 virtual QSizeF measureImplicitContent() const;
259 void updateImplicitMetrics();
260 void classBegin() override;
261 void componentComplete() override;
262 void geometryChange(const QRectF& newGeometry, const QRectF& oldGeometry) override;
263 void itemChange(ItemChange change, const ItemChangeData& value) override;
264
265private:
266 struct PaddingState {
267 qreal top;
268 qreal left;
269 qreal right;
270 qreal bottom;
271 qreal horizontal;
272 qreal vertical;
273 qreal availableWidth;
274 qreal availableHeight;
275 qreal implicitWidth;
276 qreal implicitHeight;
277 };
278
279 PaddingState paddingState() const;
280 void finishPaddingChange(const PaddingState& oldState);
281 void finishInsetChange(qreal oldImplicitWidth, qreal oldImplicitHeight);
282 void updateBaselineOffset();
283 void resolveEnvironment();
284 void resolveFont();
285 void resolveLocale();
286 void resolveLayoutDirection();
287 void resolveHoverEnabled();
288 void updateLocale(const QLocale&);
289 void updateLayoutDirection(Qt::LayoutDirection);
290 void updateHoverEnabled(bool);
291 void layoutItems();
292 void layoutBackground();
293 void orderManagedItems();
294 void disconnectBackground();
295 void backgroundChange();
296 void disconnectContentItem();
297
298 qreal m_padding { 0 };
299 std::optional<qreal> m_horizontal_padding;
300 std::optional<qreal> m_vertical_padding;
301 std::optional<qreal> m_top_padding;
302 std::optional<qreal> m_left_padding;
303 std::optional<qreal> m_right_padding;
304 std::optional<qreal> m_bottom_padding;
305 std::optional<qreal> m_spacing;
306 qreal m_top_inset { 0 };
307 qreal m_left_inset { 0 };
308 qreal m_right_inset { 0 };
309 qreal m_bottom_inset { 0 };
310
311 Q_OBJECT_BINDABLE_PROPERTY(Control, QQuickItem*, m_background, &Control::backgroundChange)
312 QPointer<QQuickItem> m_managed_background;
313 QPointer<QQuickItem> m_content_item;
314 QList<QMetaObject::Connection> m_control_connections;
315 QList<QMetaObject::Connection> m_background_connections;
316 QList<QMetaObject::Connection> m_content_connections;
317 LayoutFlags m_background_layout { LayoutAll };
318 LayoutFlags m_content_item_layout { LayoutAll };
319
320 qreal m_implicit_background_width { 0 };
321 qreal m_implicit_background_height { 0 };
322 qreal m_implicit_content_width { 0 };
323 qreal m_implicit_content_height { 0 };
324 qreal m_extra_implicit_width { 0 };
325 qreal m_extra_implicit_height { 0 };
326
327 QFont m_requested_font;
328 QFont m_font;
329 QLocale m_requested_locale;
330 QLocale m_locale;
331 Qt::LayoutDirection m_requested_layout_direction { Qt::LeftToRight };
332 Qt::LayoutDirection m_layout_direction { Qt::LeftToRight };
333 std::optional<bool> m_requested_hover_enabled;
334 bool m_font_explicit { false };
335 bool m_locale_explicit { false };
336 bool m_layout_direction_explicit { false };
337 bool m_hover_enabled { false };
338 bool m_baseline_explicit { false };
339};
340
341} // namespace qml_material
342
343Q_DECLARE_OPERATORS_FOR_FLAGS(qml_material::Control::LayoutFlags)