Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
Variants.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <PhosphorShell/phosphorshell_export.h>
7
8#include <QList>
9#include <QPointer>
10#include <QQuickItem>
11
12#include <memory>
13
14QT_BEGIN_NAMESPACE
15class QAbstractListModel;
16class QModelIndex;
17class QQmlComponent;
18QT_END_NAMESPACE
19
20namespace PhosphorShell {
21
22class PHOSPHORSHELL_EXPORT Variants : public QQuickItem
23{
24 Q_OBJECT
25 Q_CLASSINFO("DefaultProperty", "delegate")
26
27 Q_PROPERTY(QAbstractListModel* model READ model WRITE setModel NOTIFY modelChanged)
28 Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
29
30public:
31 explicit Variants(QQuickItem* parent = nullptr);
32 ~Variants() override;
33
34 [[nodiscard]] QAbstractListModel* model() const;
35 void setModel(QAbstractListModel* model);
36
37 [[nodiscard]] QQmlComponent* delegate() const;
38 void setDelegate(QQmlComponent* delegate);
39
40Q_SIGNALS:
43
44private Q_SLOTS:
45 void onRowsInserted(const QModelIndex& parent, int first, int last);
46 void onRowsRemoved(const QModelIndex& parent, int first, int last);
47 void onRowsMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd, const QModelIndex& destParent,
48 int destRow);
49 void onModelReset();
50 void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QList<int>& roles);
51
52private:
53 void rebuild();
54 void clear();
55 void refreshInstanceData(int row);
56 QVariantMap buildModelData(int row) const;
57
58 // QPointer so external destruction (model swapped out, delegate
59 // component reloaded) doesn't leave us with dangling pointers.
60 QPointer<QAbstractListModel> m_model;
61 QPointer<QQmlComponent> m_delegate;
62 // Each entry holds the delegate-instantiated object. We own them and
63 // delete via deleteLater() in clear() to be safe inside QML callstacks.
64 QList<QObject*> m_instances;
65};
66
67} // namespace PhosphorShell
Definition Variants.h:23
void setModel(QAbstractListModel *model)
void setDelegate(QQmlComponent *delegate)
Variants(QQuickItem *parent=nullptr)
QAbstractListModel * model() const
QQmlComponent * delegate() const
Definition Environment.h:11