| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "DelegateChooser.h" |
| 6 | |
| 7 | #include "Logging.h" |
| 8 | |
| 9 | // uses private API, which moved between versions |
| 10 | #include <QQmlEngine> |
| 11 | #include <QtGlobal> |
| 12 | |
| 13 | QQmlComponent * |
| 14 | DelegateChoice::delegate() const |
| 15 | { |
| 16 | return delegate_; |
| 17 | } |
| 18 | |
| 19 | void |
| 20 | DelegateChoice::setDelegate(QQmlComponent *delegate) |
| 21 | { |
| 22 | if (delegate != delegate_) { |
| 23 | delegate_ = delegate; |
| 24 | emit delegateChanged(); |
| 25 | emit changed(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | QVariant |
| 30 | DelegateChoice::roleValue() const |
| 31 | { |
| 32 | return roleValue_; |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | DelegateChoice::setRoleValue(const QVariant &value) |
| 37 | { |
| 38 | if (value != roleValue_) { |
| 39 | roleValue_ = value; |
| 40 | emit roleValueChanged(); |
| 41 | emit changed(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | QVariant |
| 46 | DelegateChooser::roleValue() const |
| 47 | { |
| 48 | return roleValue_; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | DelegateChooser::setRoleValue(const QVariant &value) |
| 53 | { |
| 54 | if (value != roleValue_) { |
| 55 | roleValue_ = value; |
| 56 | if (isComponentComplete()) |
| 57 | recalcChild(); |
| 58 | emit roleValueChanged(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | QQmlListProperty<DelegateChoice> |
| 63 | DelegateChooser::choices() |
| 64 | { |
| 65 | return QQmlListProperty<DelegateChoice>(this, |
| 66 | this, |
| 67 | &DelegateChooser::appendChoice, |
| 68 | &DelegateChooser::choiceCount, |
| 69 | &DelegateChooser::choice, |
| 70 | &DelegateChooser::clearChoices); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | DelegateChooser::appendChoice(QQmlListProperty<DelegateChoice> *p, DelegateChoice *c) |
| 75 | { |
| 76 | DelegateChooser *dc = static_cast<DelegateChooser *>(p->object); |
| 77 | dc->choices_.append(c); |
| 78 | } |
| 79 | |
| 80 | qsizetype |
| 81 | DelegateChooser::choiceCount(QQmlListProperty<DelegateChoice> *p) |
| 82 | { |
| 83 | return static_cast<DelegateChooser *>(p->object)->choices_.count(); |
| 84 | } |
| 85 | DelegateChoice * |
| 86 | DelegateChooser::choice(QQmlListProperty<DelegateChoice> *p, qsizetype index) |
| 87 | { |
| 88 | return static_cast<DelegateChooser *>(p->object)->choices_.at(index); |
| 89 | } |
| 90 | void |
| 91 | DelegateChooser::clearChoices(QQmlListProperty<DelegateChoice> *p) |
| 92 | { |
| 93 | static_cast<DelegateChooser *>(p->object)->choices_.clear(); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | DelegateChooser::recalcChild() |
| 98 | { |
| 99 | for (const auto choice : std::as_const(choices_)) { |
| 100 | const auto &choiceValue = choice->roleValueRef(); |
| 101 | if (choiceValue == roleValue_ || (!choiceValue.isValid() && !roleValue_.isValid())) { |
| 102 | if (child_) { |
| 103 | child_->setParentItem(nullptr); |
| 104 | child_ = nullptr; |
| 105 | } |
| 106 | |
| 107 | choice->delegate()->create(incubator, QQmlEngine::contextForObject(this)); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | DelegateChooser::componentComplete() |
| 115 | { |
| 116 | QQuickItem::componentComplete(); |
| 117 | recalcChild(); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | DelegateChooser::DelegateIncubator::statusChanged(QQmlIncubator::Status status) |
| 122 | { |
| 123 | if (status == QQmlIncubator::Ready) { |
| 124 | chooser.child_ = qobject_cast<QQuickItem *>(object()); |
| 125 | if (chooser.child_ == nullptr) { |
| 126 | nhlog::ui()->error("Delegate has to be derived of Item!"); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | chooser.child_->setParentItem(&chooser); |
| 131 | QQmlEngine::setObjectOwnership(chooser.child_, |
| 132 | QQmlEngine::ObjectOwnership::JavaScriptOwnership); |
| 133 | emit chooser.childChanged(); |
| 134 | |
| 135 | } else if (status == QQmlIncubator::Error) { |
| 136 | auto errors_ = errors(); |
| 137 | for (const auto &e : std::as_const(errors_)) |
| 138 | nhlog::ui()->error("Error instantiating delegate: {}", e.toString().toStdString()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #include "moc_DelegateChooser.cpp" |