Kirigami2

delegaterecycler.h
1 /*
2  * SPDX-FileCopyrightText: 2018 Marco Martin <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #ifndef DELEGATERECYCLER_H
8 #define DELEGATERECYCLER_H
9 
10 #include <QPointer>
11 #include <QQuickItem>
12 #include <QVariant>
13 
14 class DelegateRecyclerAttached : public QObject
15 {
16  Q_OBJECT
17 
18 public:
19  DelegateRecyclerAttached(QObject *parent = nullptr);
20  ~DelegateRecyclerAttached() override;
21 
22 Q_SIGNALS:
23  void pooled();
24  void reused();
25 };
26 
27 /**
28  * This class may be used as a delegate of a ListView or a GridView in the case
29  * the intended delegate is a bit heavy, with many objects inside.
30  * This will ensure the delegate instances will be put back in a common pool after
31  * destruction, so when scrolling a big list, the delegates from old delete items will
32  * be taken from the pool and reused, minimizing the need of instantiating new objects
33  * and deleting old ones. It ensures scrolling of lists with heavy delegates is
34  * smoother and helps with memory fragmentations as well.
35  *
36  * @note CardListView and CardGridView are already using this recycler, so do NOT use it
37  * as a delegate for those 2 views. Also, do NOT use this with a Repeater.
38  * @since 2.4
39  */
41 {
42  Q_OBJECT
43 
44  /**
45  * The Component the actual delegates will be built from.
46  *
47  * @note the component may not be a child of this object, therefore it can't be
48  * declared inside the DelegateRecycler declaration.
49  *
50  * The DelegateRecycler will not take ownership of the delegate Component, so it's up
51  * to the caller to delete it (usually with the normal child/parent relationship)
52  */
53  Q_PROPERTY(QQmlComponent *sourceComponent READ sourceComponent WRITE setSourceComponent RESET resetSourceComponent NOTIFY sourceComponentChanged)
54 
55 public:
57  ~DelegateRecycler() override;
58 
60  void setSourceComponent(QQmlComponent *component);
61  void resetSourceComponent();
62 
63  static DelegateRecyclerAttached *qmlAttachedProperties(QObject *object);
64 
65 protected:
66 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
67  void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
68 #else
69  void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
70 #endif
71  void focusInEvent(QFocusEvent *event) override;
72 
73  void updateHints();
74  void updateSize(bool parentResized);
75 
76 Q_SIGNALS:
77  void sourceComponentChanged();
78 
79 private Q_SLOTS:
80  void syncIndex();
81  void syncModel();
82  void syncModelProperties();
83  void syncModelData();
84 
85 private:
86  QPointer<QQmlComponent> m_sourceComponent;
87  QPointer<QQuickItem> m_item;
88  QObject *m_propertiesTracker = nullptr;
89  bool m_updatingSize = false;
90  bool m_widthFromItem = false;
91  bool m_heightFromItem = false;
92 };
93 
94 QML_DECLARE_TYPEINFO(DelegateRecycler, QML_HAS_ATTACHED_PROPERTIES)
95 
96 #endif
Q_OBJECTQ_OBJECT
QQmlComponent sourceComponent
The Component the actual delegates will be built from.
Q_PROPERTY(...)
virtual bool event(QEvent *ev) override
Q_SLOTSQ_SLOTS
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
Q_SIGNALSQ_SIGNALS
This class may be used as a delegate of a ListView or a GridView in the case the intended delegate is...
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:14:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.