matrixion/src/timeline/DelegateChooser.cpp

141 lines
3.5 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
#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_;
}
void
DelegateChoice::setDelegate(QQmlComponent *delegate)
{
2021-09-18 01:22:33 +03:00
if (delegate != delegate_) {
delegate_ = delegate;
emit delegateChanged();
emit changed();
}
}
QVariant
DelegateChoice::roleValue() const
{
2021-09-18 01:22:33 +03:00
return roleValue_;
}
void
DelegateChoice::setRoleValue(const QVariant &value)
{
2021-09-18 01:22:33 +03:00
if (value != roleValue_) {
roleValue_ = value;
emit roleValueChanged();
emit changed();
}
}
QVariant
DelegateChooser::roleValue() const
{
2021-09-18 01:22:33 +03:00
return roleValue_;
}
void
DelegateChooser::setRoleValue(const QVariant &value)
{
2021-09-18 01:22:33 +03:00
if (value != roleValue_) {
roleValue_ = value;
if (isComponentComplete())
recalcChild();
2021-09-18 01:22:33 +03:00
emit roleValueChanged();
}
}
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);
}
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);
}
2023-06-02 01:24:26 +03:00
qsizetype
DelegateChooser::choiceCount(QQmlListProperty<DelegateChoice> *p)
{
2021-09-18 01:22:33 +03:00
return static_cast<DelegateChooser *>(p->object)->choices_.count();
}
DelegateChoice *
2023-06-02 01:24:26 +03:00
DelegateChooser::choice(QQmlListProperty<DelegateChoice> *p, qsizetype index)
{
2021-09-18 01:22:33 +03:00
return static_cast<DelegateChooser *>(p->object)->choices_.at(index);
}
void
DelegateChooser::clearChoices(QQmlListProperty<DelegateChoice> *p)
{
2021-09-18 01:22:33 +03:00
static_cast<DelegateChooser *>(p->object)->choices_.clear();
}
void
DelegateChooser::recalcChild()
{
2023-10-14 00:28:57 +03:00
for (const auto choice : std::as_const(choices_)) {
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;
}
2021-09-18 01:22:33 +03:00
}
}
void
DelegateChooser::componentComplete()
{
2021-09-18 01:22:33 +03:00
QQuickItem::componentComplete();
recalcChild();
}
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
}