Kirigami2

sizegroup.cpp
1 /*
2  * SPDX-FileCopyrightText: 2020 Carson Black <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include <QQmlProperty>
8 
9 #include "sizegroup.h"
10 
11 #define pThis (static_cast<SizeGroup *>(prop->object))
12 
13 void SizeGroup::appendItem(QQmlListProperty<QQuickItem> *prop, QQuickItem *value)
14 {
15  pThis->m_items << value;
16  pThis->connectItem(value);
17 }
18 
19 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
20 int SizeGroup::itemCount(QQmlListProperty<QQuickItem> *prop)
21 #else
22 qsizetype SizeGroup::itemCount(QQmlListProperty<QQuickItem> *prop)
23 #endif
24 {
25  return pThis->m_items.count();
26 }
27 
28 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
29 QQuickItem *SizeGroup::itemAt(QQmlListProperty<QQuickItem> *prop, int index)
30 #else
31 QQuickItem *SizeGroup::itemAt(QQmlListProperty<QQuickItem> *prop, qsizetype index)
32 #endif
33 {
34  return pThis->m_items[index];
35 }
36 
37 void SizeGroup::clearItems(QQmlListProperty<QQuickItem> *prop)
38 {
39  for (const auto &item : std::as_const(pThis->m_items)) {
40  QObject::disconnect(pThis->m_connections[item].first);
41  QObject::disconnect(pThis->m_connections[item].second);
42  }
43  pThis->m_items.clear();
44 }
45 
46 void SizeGroup::connectItem(QQuickItem *item)
47 {
48  auto conn1 = connect(item, &QQuickItem::implicitWidthChanged, this, [this]() {
49  adjustItems(Mode::Width);
50  });
51  auto conn2 = connect(item, &QQuickItem::implicitHeightChanged, this, [this]() {
52  adjustItems(Mode::Height);
53  });
54  m_connections[item] = qMakePair(conn1, conn2);
55  adjustItems(m_mode);
56 }
57 
59 {
60  return QQmlListProperty<QQuickItem>(this, //
61  nullptr,
62  appendItem,
63  itemCount,
64  itemAt,
65  clearItems);
66 }
67 
69 {
70  adjustItems(Mode::Both);
71 }
72 
73 void SizeGroup::componentComplete()
74 {
75  adjustItems(Mode::Both);
76 }
77 
78 void SizeGroup::adjustItems(Mode whatChanged)
79 {
80  if (m_mode == Mode::Width && whatChanged == Mode::Height) {
81  return;
82  }
83  if (m_mode == Mode::Height && whatChanged == Mode::Width) {
84  return;
85  }
86 
87  qreal maxHeight = 0.0;
88  qreal maxWidth = 0.0;
89 
90  for (const auto &item : std::as_const(m_items)) {
91  if (item == nullptr) {
92  continue;
93  }
94 
95  switch (m_mode) {
96  case Mode::Width:
97  maxWidth = qMax(maxWidth, item->implicitWidth());
98  break;
99  case Mode::Height:
100  maxHeight = qMax(maxHeight, item->implicitHeight());
101  break;
102  case Mode::Both:
103  maxWidth = qMax(maxWidth, item->implicitWidth());
104  maxHeight = qMax(maxHeight, item->implicitHeight());
105  break;
106  case Mode::None:
107  break;
108  }
109  }
110 
111  for (const auto &item : std::as_const(m_items)) {
112  if (item == nullptr) {
113  continue;
114  }
115 
116  if (!qmlEngine(item) || !qmlContext(item)) {
117  continue;
118  }
119 
120  switch (m_mode) {
121  case Mode::Width:
122  QQmlProperty(item, QStringLiteral("Layout.preferredWidth"), qmlContext(item)).write(maxWidth);
123  break;
124  case Mode::Height:
125  QQmlProperty(item, QStringLiteral("Layout.preferredHeight"), qmlContext(item)).write(maxHeight);
126  break;
127  case Mode::Both:
128  QQmlProperty(item, QStringLiteral("Layout.preferredWidth"), qmlContext(item)).write(maxWidth);
129  QQmlProperty(item, QStringLiteral("Layout.preferredHeight"), qmlContext(item)).write(maxHeight);
130  break;
131  case Mode::None:
132  break;
133  }
134  }
135 }
Q_INVOKABLE void relayout()
Forces the SizeGroup to relayout items.
Definition: sizegroup.cpp:68
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QQmlListProperty< QQuickItem > items
Which items this SizeGroup should adjust.
Definition: sizegroup.h:50
void implicitHeightChanged()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool write(const QVariant &value) const const
void implicitWidthChanged()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.