2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
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
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
|
|
|
Q_CLASSINFO("DefaultProperty", "delegate")
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
public:
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_PROPERTY(QVariant roleValue READ roleValue WRITE setRoleValue NOTIFY roleValueChanged)
|
|
|
|
Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
|
2019-10-20 13:39:47 +03:00
|
|
|
|
2021-12-30 00:46:04 +03:00
|
|
|
[[nodiscard]] QQmlComponent *delegate() const;
|
2021-09-18 01:22:33 +03:00
|
|
|
void setDelegate(QQmlComponent *delegate);
|
2019-10-20 13:39:47 +03:00
|
|
|
|
2021-12-30 00:46:04 +03:00
|
|
|
[[nodiscard]] QVariant roleValue() const;
|
|
|
|
[[nodiscard]] const QVariant &roleValueRef() const { return roleValue_; }
|
2021-09-18 01:22:33 +03:00
|
|
|
void setRoleValue(const QVariant &value);
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void delegateChanged();
|
|
|
|
void roleValueChanged();
|
|
|
|
void changed();
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
private:
|
2021-09-18 01:22:33 +03:00
|
|
|
QVariant roleValue_;
|
|
|
|
QQmlComponent *delegate_ = nullptr;
|
2019-10-20 13:39:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class DelegateChooser : public QQuickItem
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
Q_OBJECT
|
|
|
|
Q_CLASSINFO("DefaultProperty", "choices")
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
public:
|
2021-09-18 01:22:33 +03:00
|
|
|
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)
|
2019-10-20 13:39:47 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
QQmlListProperty<DelegateChoice> choices();
|
2019-10-20 13:39:47 +03:00
|
|
|
|
2021-12-30 00:46:04 +03:00
|
|
|
[[nodiscard]] QVariant roleValue() const;
|
2021-09-18 01:22:33 +03:00
|
|
|
void setRoleValue(const QVariant &value);
|
2019-10-20 13:39:47 +03:00
|
|
|
|
2021-12-30 00:46:04 +03:00
|
|
|
[[nodiscard]] QQuickItem *child() const { return child_; }
|
2020-04-09 01:07:04 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
void recalcChild();
|
|
|
|
void componentComplete() override;
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
signals:
|
2021-09-18 01:22:33 +03:00
|
|
|
void roleChanged();
|
|
|
|
void roleValueChanged();
|
|
|
|
void childChanged();
|
2019-10-20 13:39:47 +03:00
|
|
|
|
|
|
|
private:
|
2021-09-18 01:22:33 +03:00
|
|
|
struct DelegateIncubator : public QQmlIncubator
|
|
|
|
{
|
|
|
|
DelegateIncubator(DelegateChooser &parent)
|
|
|
|
: QQmlIncubator(QQmlIncubator::AsynchronousIfNested)
|
|
|
|
, chooser(parent)
|
2022-05-10 04:53:35 +03:00
|
|
|
{}
|
2021-09-18 01:22:33 +03:00
|
|
|
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> *);
|
2019-10-20 13:39:47 +03:00
|
|
|
};
|