| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include <QQmlComponent> |
| 11 |
#include <QQmlIncubator> |
| 12 |
#include <QQmlListProperty> |
| 13 |
#include <QQuickItem> |
| 14 |
#include <QtCore/QObject> |
| 15 |
#include <QtCore/QVariant> |
| 16 |
|
| 17 |
class QQmlAdaptorModel; |
| 18 |
|
| 19 |
class DelegateChoice : public QObject |
| 20 |
{ |
| 21 |
Q_OBJECT |
| 22 |
QML_ELEMENT |
| 23 |
Q_CLASSINFO("DefaultProperty", "delegate") |
| 24 |
|
| 25 |
public: |
| 26 |
Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged) |
| 27 |
Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged) |
| 28 |
|
| 29 |
[[nodiscard]] QQmlComponent *delegate() const; |
| 30 |
void setDelegate(QQmlComponent *delegate); |
| 31 |
|
| 32 |
[[nodiscard]] QVariant roleValue() const; |
| 33 |
[[nodiscard]] const QVariant &roleValueRef() const { return roleValue_; } |
| 34 |
void setRoleValue(const QVariant &value); |
| 35 |
|
| 36 |
signals: |
| 37 |
void delegateChanged(); |
| 38 |
void roleValueChanged(); |
| 39 |
void changed(); |
| 40 |
|
| 41 |
private: |
| 42 |
QVariant roleValue_; |
| 43 |
QQmlComponent *delegate_ = nullptr; |
| 44 |
}; |
| 45 |
|
| 46 |
class DelegateChooser : public QQuickItem |
| 47 |
{ |
| 48 |
Q_OBJECT |
| 49 |
QML_ELEMENT |
| 50 |
Q_CLASSINFO("DefaultProperty", "choices") |
| 51 |
|
| 52 |
public: |
| 53 |
Q_PROPERTY(QQmlListProperty<DelegateChoice> choices READ choices CONSTANT) |
| 54 |
Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged) |
| 55 |
Q_PROPERTY(QQuickItem *child READ child NOTIFY childChanged) |
| 56 |
|
| 57 |
QQmlListProperty<DelegateChoice> choices(); |
| 58 |
|
| 59 |
[[nodiscard]] QVariant roleValue() const; |
| 60 |
void setRoleValue(const QVariant &value); |
| 61 |
|
| 62 |
[[nodiscard]] QQuickItem *child() const { return child_; } |
| 63 |
|
| 64 |
void recalcChild(); |
| 65 |
void componentComplete() override; |
| 66 |
|
| 67 |
signals: |
| 68 |
void roleChanged(); |
| 69 |
void roleValueChanged(); |
| 70 |
void childChanged(); |
| 71 |
|
| 72 |
private: |
| 73 |
struct DelegateIncubator final : public QQmlIncubator |
| 74 |
{ |
| 75 |
DelegateIncubator(DelegateChooser &parent) |
| 76 |
: QQmlIncubator(QQmlIncubator::AsynchronousIfNested) |
| 77 |
, chooser(parent) |
| 78 |
{ |
| 79 |
} |
| 80 |
void statusChanged(QQmlIncubator::Status status) override; |
| 81 |
|
| 82 |
DelegateChooser &chooser; |
| 83 |
}; |
| 84 |
|
| 85 |
QVariant roleValue_; |
| 86 |
QList<DelegateChoice *> choices_; |
| 87 |
QQuickItem *child_ = nullptr; |
| 88 |
DelegateIncubator incubator{*this}; |
| 89 |
|
| 90 |
static void appendChoice(QQmlListProperty<DelegateChoice> *, DelegateChoice *); |
| 91 |
static qsizetype choiceCount(QQmlListProperty<DelegateChoice> *); |
| 92 |
static DelegateChoice *choice(QQmlListProperty<DelegateChoice> *, qsizetype index); |
| 93 |
static void clearChoices(QQmlListProperty<DelegateChoice> *); |
| 94 |
}; |