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 * SizeGroup is a utility object that makes groups of items request the same size. 19 */ 20 class SizeGroup : public QObject, public QQmlParserStatus 21 { 22 Q_OBJECT 23 Q_INTERFACES(QQmlParserStatus) 24 25 public: 26 enum Mode { 27 None = 0, /// SizeGroup does nothing 28 Width = 1, /// SizeGroup syncs item widths 29 Height = 2, /// SizeGroup syncs item heights 30 Both = 3, /// SizeGroup syncs both item widths and heights 31 }; 32 Q_ENUM(Mode) 33 Q_DECLARE_FLAGS(Modes, Mode) 34 35 private: 36 Mode m_mode = None; 37 QList<QPointer<QQuickItem>> m_items; 38 QMap<QQuickItem *, QPair<QMetaObject::Connection, QMetaObject::Connection>> m_connections; 39 40 public: 41 /** 42 * Which dimensions this SizeGroup should adjust 43 */ 44 Q_PROPERTY(Mode mode MEMBER m_mode NOTIFY modeChanged) 45 Q_SIGNAL void modeChanged(); 46 47 /** 48 * Which items this SizeGroup should adjust 49 */ 50 Q_PROPERTY(QQmlListProperty<QQuickItem> items READ items CONSTANT) 51 QQmlListProperty<QQuickItem> items(); 52 53 void adjustItems(Mode whatChanged); 54 void connectItem(QQuickItem *item); 55 56 /** 57 * Forces the SizeGroup to relayout items. 58 * 59 * Normally this is never needed as the SizeGroup automatically 60 * relayout items as they're added and their sizes change. 61 */ 62 Q_INVOKABLE void relayout(); 63 classBegin()64 void classBegin() override 65 { 66 } 67 void componentComplete() override; 68 69 static void appendItem(QQmlListProperty<QQuickItem> *prop, QQuickItem *value); 70 static int itemCount(QQmlListProperty<QQuickItem> *prop); 71 static QQuickItem *itemAt(QQmlListProperty<QQuickItem> *prop, int index); 72 static void clearItems(QQmlListProperty<QQuickItem> *prop); 73 }; 74