summary refs log tree commit diff
path: root/src/timeline/DelegateChooser.h
blob: 2524b0689f78971b149a3b0a868a21c210c46ae5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// A DelegateChooser like the one, that was added to Qt5.12 (in labs), but compatible with older Qt
// versions see KDE/kquickitemviews see qtdeclarative/qqmldelagatecomponent

#pragma once

#include <QQmlComponent>
#include <QQmlIncubator>
#include <QQmlListProperty>
#include <QQuickItem>
#include <QtCore/QObject>
#include <QtCore/QVariant>

class QQmlAdaptorModel;

class DelegateChoice : public QObject
{
        Q_OBJECT
        Q_CLASSINFO("DefaultProperty", "delegate")

public:
        Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged)
        Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)

        QQmlComponent *delegate() const;
        void setDelegate(QQmlComponent *delegate);

        QVariant roleValue() const;
        void setRoleValue(const QVariant &value);

signals:
        void delegateChanged();
        void roleValueChanged();
        void changed();

private:
        QVariant roleValue_;
        QQmlComponent *delegate_ = nullptr;
};

class DelegateChooser : public QQuickItem
{
        Q_OBJECT
        Q_CLASSINFO("DefaultProperty", "choices")

public:
        Q_PROPERTY(QQmlListProperty<DelegateChoice> choices READ choices CONSTANT)
        Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged)
        Q_PROPERTY(QQuickItem *child READ child NOTIFY childChanged)

        QQmlListProperty<DelegateChoice> choices();

        QVariant roleValue() const;
        void setRoleValue(const QVariant &value);

        QQuickItem *child() const { return child_; }

        void recalcChild();
        void componentComplete() override;

signals:
        void roleChanged();
        void roleValueChanged();
        void childChanged();

private:
        struct DelegateIncubator : public QQmlIncubator
        {
                DelegateIncubator(DelegateChooser &parent)
                  : QQmlIncubator(QQmlIncubator::AsynchronousIfNested)
                  , chooser(parent)
                {}
                void statusChanged(QQmlIncubator::Status status) override;

                DelegateChooser &chooser;
        };

        QVariant roleValue_;
        QList<DelegateChoice *> choices_;
        QQuickItem *child_ = nullptr;
        DelegateIncubator incubator{*this};

        static void appendChoice(QQmlListProperty<DelegateChoice> *, DelegateChoice *);
        static int choiceCount(QQmlListProperty<DelegateChoice> *);
        static DelegateChoice *choice(QQmlListProperty<DelegateChoice> *, int index);
        static void clearChoices(QQmlListProperty<DelegateChoice> *);
};