2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// 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;
|
2021-12-30 00:46:04 +03:00
|
|
|
if (isComponentComplete())
|
|
|
|
recalcChild();
|
2021-09-18 01:22:33 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-02 01:24:26 +03:00
|
|
|
qsizetype
|
2019-10-20 13:39:47 +03:00
|
|
|
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 *
|
2023-06-02 01:24:26 +03:00
|
|
|
DelegateChooser::choice(QQmlListProperty<DelegateChoice> *p, qsizetype index)
|
2019-10-20 13:39:47 +03:00
|
|
|
{
|
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()
|
|
|
|
{
|
2023-10-14 00:28:57 +03:00
|
|
|
for (const auto choice : std::as_const(choices_)) {
|
2021-12-30 00:46:04 +03:00
|
|
|
const auto &choiceValue = choice->roleValueRef();
|
|
|
|
if (choiceValue == roleValue_ || (!choiceValue.isValid() && !roleValue_.isValid())) {
|
2021-09-18 01:22:33 +03:00
|
|
|
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) {
|
2021-12-29 08:10:08 +03:00
|
|
|
chooser.child_ = qobject_cast<QQuickItem *>(object());
|
2021-09-18 01:22:33 +03:00
|
|
|
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();
|
2023-10-14 00:28:57 +03:00
|
|
|
for (const auto &e : std::as_const(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
|
|
|
}
|