2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2019-10-20 13:39:47 +03:00
|
|
|
#include "DelegateChooser.h"
|
|
|
|
|
|
|
|
#include "Logging.h"
|
|
|
|
|
|
|
|
// uses private API, which moved between versions
|
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
QQmlComponent *
|
|
|
|
DelegateChoice::delegate() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return delegate_;
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChoice::setDelegate(QQmlComponent *delegate)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (delegate != delegate_) {
|
|
|
|
delegate_ = delegate;
|
|
|
|
emit delegateChanged();
|
|
|
|
emit changed();
|
|
|
|
}
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
DelegateChoice::roleValue() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return roleValue_;
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChoice::setRoleValue(const QVariant &value)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (value != roleValue_) {
|
|
|
|
roleValue_ = value;
|
|
|
|
emit roleValueChanged();
|
|
|
|
emit changed();
|
|
|
|
}
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
DelegateChooser::roleValue() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return roleValue_;
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::setRoleValue(const QVariant &value)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (value != roleValue_) {
|
|
|
|
roleValue_ = value;
|
|
|
|
recalcChild();
|
|
|
|
emit roleValueChanged();
|
|
|
|
}
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QQmlListProperty<DelegateChoice>
|
|
|
|
DelegateChooser::choices()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return QQmlListProperty<DelegateChoice>(this,
|
|
|
|
this,
|
|
|
|
&DelegateChooser::appendChoice,
|
|
|
|
&DelegateChooser::choiceCount,
|
|
|
|
&DelegateChooser::choice,
|
|
|
|
&DelegateChooser::clearChoices);
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::appendChoice(QQmlListProperty<DelegateChoice> *p, DelegateChoice *c)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
DelegateChooser *dc = static_cast<DelegateChooser *>(p->object);
|
|
|
|
dc->choices_.append(c);
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
DelegateChooser::choiceCount(QQmlListProperty<DelegateChoice> *p)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return static_cast<DelegateChooser *>(p->object)->choices_.count();
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
DelegateChoice *
|
|
|
|
DelegateChooser::choice(QQmlListProperty<DelegateChoice> *p, int index)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return static_cast<DelegateChooser *>(p->object)->choices_.at(index);
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
void
|
|
|
|
DelegateChooser::clearChoices(QQmlListProperty<DelegateChoice> *p)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
static_cast<DelegateChooser *>(p->object)->choices_.clear();
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::recalcChild()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto choice : qAsConst(choices_)) {
|
|
|
|
auto choiceValue = choice->roleValue();
|
|
|
|
if (!roleValue_.isValid() || !choiceValue.isValid() || choiceValue == roleValue_) {
|
|
|
|
if (child_) {
|
|
|
|
child_->setParentItem(nullptr);
|
|
|
|
child_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
choice->delegate()->create(incubator, QQmlEngine::contextForObject(this));
|
|
|
|
return;
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::componentComplete()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QQuickItem::componentComplete();
|
|
|
|
recalcChild();
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 14:20:05 +03:00
|
|
|
void
|
|
|
|
DelegateChooser::DelegateIncubator::statusChanged(QQmlIncubator::Status status)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (status == QQmlIncubator::Ready) {
|
|
|
|
chooser.child_ = dynamic_cast<QQuickItem *>(object());
|
|
|
|
if (chooser.child_ == nullptr) {
|
|
|
|
nhlog::ui()->error("Delegate has to be derived of Item!");
|
|
|
|
return;
|
2019-10-25 14:20:05 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
chooser.child_->setParentItem(&chooser);
|
|
|
|
QQmlEngine::setObjectOwnership(chooser.child_,
|
|
|
|
QQmlEngine::ObjectOwnership::JavaScriptOwnership);
|
|
|
|
emit chooser.childChanged();
|
|
|
|
|
|
|
|
} else if (status == QQmlIncubator::Error) {
|
2021-12-29 00:30:12 +03:00
|
|
|
auto errors_ = errors();
|
|
|
|
for (const auto &e : qAsConst(errors_))
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->error("Error instantiating delegate: {}", e.toString().toStdString());
|
|
|
|
}
|
2019-10-25 14:20:05 +03:00
|
|
|
}
|