File indexing completed on 2024-04-21 03:56:02

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Carson Black <uhhadd@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QMap>
0010 #include <QObject>
0011 #include <QPair>
0012 #include <QPointer>
0013 #include <QQmlListProperty>
0014 #include <QQmlParserStatus>
0015 #include <QQuickItem>
0016 
0017 /**
0018  * SizeGroup is a utility object that makes groups of items request the same size.
0019  */
0020 class SizeGroup : public QObject, public QQmlParserStatus
0021 {
0022     Q_OBJECT
0023     QML_ELEMENT
0024     Q_INTERFACES(QQmlParserStatus)
0025 
0026 public:
0027     enum Mode {
0028         None = 0, /// SizeGroup does nothing
0029         Width = 1, /// SizeGroup syncs item widths
0030         Height = 2, /// SizeGroup syncs item heights
0031         Both = 3, /// SizeGroup syncs both item widths and heights
0032     };
0033     Q_ENUM(Mode)
0034     Q_DECLARE_FLAGS(Modes, Mode)
0035 
0036 private:
0037     Mode m_mode = None;
0038     QList<QPointer<QQuickItem>> m_items;
0039     QMap<QQuickItem *, QPair<QMetaObject::Connection, QMetaObject::Connection>> m_connections;
0040 
0041 public:
0042     /**
0043      * Which dimensions this SizeGroup should adjust
0044      */
0045     Q_PROPERTY(Mode mode MEMBER m_mode NOTIFY modeChanged FINAL)
0046     Q_SIGNAL void modeChanged();
0047 
0048     /**
0049      * Which items this SizeGroup should adjust
0050      */
0051     Q_PROPERTY(QQmlListProperty<QQuickItem> items READ items CONSTANT FINAL)
0052     QQmlListProperty<QQuickItem> items();
0053 
0054     void adjustItems(Mode whatChanged);
0055     void connectItem(QQuickItem *item);
0056 
0057     /**
0058      * Forces the SizeGroup to relayout items.
0059      *
0060      * Normally this is never needed as the SizeGroup automatically
0061      * relayout items as they're added and their sizes change.
0062      */
0063     Q_INVOKABLE void relayout();
0064 
0065     void classBegin() override
0066     {
0067     }
0068     void componentComplete() override;
0069 
0070 private:
0071     static void appendItem(QQmlListProperty<QQuickItem> *prop, QQuickItem *value);
0072     static qsizetype itemCount(QQmlListProperty<QQuickItem> *prop);
0073     static QQuickItem *itemAt(QQmlListProperty<QQuickItem> *prop, qsizetype index);
0074     static void clearItems(QQmlListProperty<QQuickItem> *prop);
0075 };