2019-10-25 15:20:43 +03:00
|
|
|
// 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
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QQmlComponent>
|
2019-10-25 14:20:05 +03:00
|
|
|
#include <QQmlIncubator>
|
2019-10-20 13:39:47 +03:00
|
|
|
#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
|
|
|
|
{
|
2019-10-25 15:20:43 +03:00
|
|
|
Q_OBJECT
|
|
|
|
Q_CLASSINFO("DefaultProperty", "choices")
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
Q_PROPERTY(QQmlListProperty<DelegateChoice> choices READ choices CONSTANT)
|
|
|
|
Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged)
|
|
|
|
|
|
|
|
QQmlListProperty<DelegateChoice> choices();
|
|
|
|
|
|
|
|
QVariant roleValue() const;
|
|
|
|
void setRoleValue(const QVariant &value);
|
|
|
|
|
|
|
|
void recalcChild();
|
|
|
|
void componentComplete() override;
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void roleChanged();
|
|
|
|
void roleValueChanged();
|
|
|
|
|
|
|
|
private:
|
2019-10-25 14:20:05 +03:00
|
|
|
struct DelegateIncubator : public QQmlIncubator
|
|
|
|
{
|
|
|
|
DelegateIncubator(DelegateChooser &parent)
|
|
|
|
: QQmlIncubator(QQmlIncubator::AsynchronousIfNested)
|
|
|
|
, chooser(parent)
|
|
|
|
{}
|
|
|
|
void statusChanged(QQmlIncubator::Status status) override;
|
|
|
|
|
|
|
|
DelegateChooser &chooser;
|
|
|
|
};
|
|
|
|
|
2019-10-20 13:39:47 +03:00
|
|
|
QVariant roleValue_;
|
|
|
|
QList<DelegateChoice *> choices_;
|
2019-10-25 14:20:05 +03:00
|
|
|
QQuickItem *child = nullptr;
|
|
|
|
DelegateIncubator incubator{*this};
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
static void appendChoice(QQmlListProperty<DelegateChoice> *, DelegateChoice *);
|
|
|
|
static int choiceCount(QQmlListProperty<DelegateChoice> *);
|
|
|
|
static DelegateChoice *choice(QQmlListProperty<DelegateChoice> *, int index);
|
|
|
|
static void clearChoices(QQmlListProperty<DelegateChoice> *);
|
|
|
|
};
|