1 | /* |
2 | * SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com> |
3 | * |
4 | * SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #pragma once |
8 | |
9 | #include <QMap> |
10 | #include <QObject> |
11 | #include <QPair> |
12 | #include <QPointer> |
13 | #include <QQmlListProperty> |
14 | #include <QQmlParserStatus> |
15 | #include <QQuickItem> |
16 | |
17 | /*! |
18 | * \qmltype SizeGroup |
19 | * \inqmlmodule org.kde.kirigami.layouts |
20 | * |
21 | * \brief SizeGroup is a utility object that makes groups of items request the same size. |
22 | */ |
23 | class SizeGroup : public QObject, public QQmlParserStatus |
24 | { |
25 | Q_OBJECT |
26 | QML_ELEMENT |
27 | Q_INTERFACES(QQmlParserStatus) |
28 | |
29 | public: |
30 | enum Mode { |
31 | None = 0, /// SizeGroup does nothing |
32 | Width = 1, /// SizeGroup syncs item widths |
33 | Height = 2, /// SizeGroup syncs item heights |
34 | Both = 3, /// SizeGroup syncs both item widths and heights |
35 | }; |
36 | Q_ENUM(Mode) |
37 | Q_DECLARE_FLAGS(Modes, Mode) |
38 | |
39 | private: |
40 | Mode m_mode = None; |
41 | QList<QPointer<QQuickItem>> m_items; |
42 | QMap<QQuickItem *, QPair<QMetaObject::Connection, QMetaObject::Connection>> m_connections; |
43 | |
44 | public: |
45 | /*! |
46 | * \qmlproperty enumeration SizeGroup::mode |
47 | * Which dimensions this SizeGroup should adjust. |
48 | * |
49 | * Possible values are: |
50 | * \list |
51 | * \li None: SizeGroup does nothing |
52 | * \li Width: SizeGroup syncs item widths |
53 | * \li Height: SizeGroup syncs item heights |
54 | * \li Both: SizeGroup syncs both item widths and heights |
55 | * \endlist |
56 | */ |
57 | Q_PROPERTY(Mode mode MEMBER m_mode NOTIFY modeChanged FINAL) |
58 | Q_SIGNAL void modeChanged(); |
59 | |
60 | /*! |
61 | * \qmlproperty list<Item> SizeGroup::items |
62 | * Which items this SizeGroup should adjust |
63 | */ |
64 | Q_PROPERTY(QQmlListProperty<QQuickItem> items READ items CONSTANT FINAL) |
65 | QQmlListProperty<QQuickItem> items(); |
66 | |
67 | void adjustItems(Mode whatChanged); |
68 | void connectItem(QQuickItem *item); |
69 | |
70 | /*! |
71 | * \qmlmethod void SizeGroup::relayout() |
72 | * Forces the SizeGroup to relayout items. |
73 | * |
74 | * Normally this is never needed as the SizeGroup automatically |
75 | * relayout items as they're added and their sizes change. |
76 | */ |
77 | Q_INVOKABLE void relayout(); |
78 | |
79 | void classBegin() override |
80 | { |
81 | } |
82 | void componentComplete() override; |
83 | |
84 | private: |
85 | static void appendItem(QQmlListProperty<QQuickItem> *prop, QQuickItem *value); |
86 | static qsizetype itemCount(QQmlListProperty<QQuickItem> *prop); |
87 | static QQuickItem *itemAt(QQmlListProperty<QQuickItem> *prop, qsizetype index); |
88 | static void clearItems(QQmlListProperty<QQuickItem> *prop); |
89 | }; |
90 | |