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
|
|
|
|
{
|
2019-10-25 15:20:43 +03:00
|
|
|
return delegate_;
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChoice::setDelegate(QQmlComponent *delegate)
|
|
|
|
{
|
|
|
|
if (delegate != delegate_) {
|
|
|
|
delegate_ = delegate;
|
|
|
|
emit delegateChanged();
|
|
|
|
emit changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
DelegateChoice::roleValue() const
|
|
|
|
{
|
|
|
|
return roleValue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChoice::setRoleValue(const QVariant &value)
|
|
|
|
{
|
|
|
|
if (value != roleValue_) {
|
|
|
|
roleValue_ = value;
|
|
|
|
emit roleValueChanged();
|
|
|
|
emit changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
DelegateChooser::roleValue() const
|
|
|
|
{
|
|
|
|
return roleValue_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::setRoleValue(const QVariant &value)
|
|
|
|
{
|
|
|
|
if (value != roleValue_) {
|
|
|
|
roleValue_ = value;
|
|
|
|
recalcChild();
|
|
|
|
emit roleValueChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QQmlListProperty<DelegateChoice>
|
|
|
|
DelegateChooser::choices()
|
|
|
|
{
|
|
|
|
return QQmlListProperty<DelegateChoice>(this,
|
|
|
|
this,
|
|
|
|
&DelegateChooser::appendChoice,
|
|
|
|
&DelegateChooser::choiceCount,
|
|
|
|
&DelegateChooser::choice,
|
|
|
|
&DelegateChooser::clearChoices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::appendChoice(QQmlListProperty<DelegateChoice> *p, DelegateChoice *c)
|
|
|
|
{
|
|
|
|
DelegateChooser *dc = static_cast<DelegateChooser *>(p->object);
|
|
|
|
dc->choices_.append(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
DelegateChooser::choiceCount(QQmlListProperty<DelegateChoice> *p)
|
|
|
|
{
|
|
|
|
return static_cast<DelegateChooser *>(p->object)->choices_.count();
|
|
|
|
}
|
|
|
|
DelegateChoice *
|
|
|
|
DelegateChooser::choice(QQmlListProperty<DelegateChoice> *p, int index)
|
|
|
|
{
|
|
|
|
return static_cast<DelegateChooser *>(p->object)->choices_.at(index);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
DelegateChooser::clearChoices(QQmlListProperty<DelegateChoice> *p)
|
|
|
|
{
|
|
|
|
static_cast<DelegateChooser *>(p->object)->choices_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::recalcChild()
|
|
|
|
{
|
2020-02-07 04:52:18 +03:00
|
|
|
for (const auto choice : qAsConst(choices_)) {
|
2019-10-20 13:39:47 +03:00
|
|
|
auto choiceValue = choice->roleValue();
|
|
|
|
if (!roleValue_.isValid() || !choiceValue.isValid() || choiceValue == roleValue_) {
|
2020-04-09 01:07:04 +03:00
|
|
|
if (child_) {
|
|
|
|
child_->setParentItem(nullptr);
|
|
|
|
child_ = nullptr;
|
2019-10-20 13:39:47 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 14:20:05 +03:00
|
|
|
choice->delegate()->create(incubator, QQmlEngine::contextForObject(this));
|
2019-10-20 13:39:47 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DelegateChooser::componentComplete()
|
|
|
|
{
|
|
|
|
QQuickItem::componentComplete();
|
|
|
|
recalcChild();
|
|
|
|
}
|
|
|
|
|
2019-10-25 14:20:05 +03:00
|
|
|
void
|
|
|
|
DelegateChooser::DelegateIncubator::statusChanged(QQmlIncubator::Status status)
|
|
|
|
{
|
|
|
|
if (status == QQmlIncubator::Ready) {
|
2020-04-09 01:07:04 +03:00
|
|
|
chooser.child_ = dynamic_cast<QQuickItem *>(object());
|
|
|
|
if (chooser.child_ == nullptr) {
|
2019-10-25 14:20:05 +03:00
|
|
|
nhlog::ui()->error("Delegate has to be derived of Item!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:07:04 +03:00
|
|
|
chooser.child_->setParentItem(&chooser);
|
|
|
|
QQmlEngine::setObjectOwnership(chooser.child_,
|
2019-10-25 14:20:05 +03:00
|
|
|
QQmlEngine::ObjectOwnership::JavaScriptOwnership);
|
2020-04-09 01:07:04 +03:00
|
|
|
emit chooser.childChanged();
|
2019-10-25 14:20:05 +03:00
|
|
|
|
|
|
|
} else if (status == QQmlIncubator::Error) {
|
|
|
|
for (const auto &e : errors())
|
|
|
|
nhlog::ui()->error("Error instantiating delegate: {}",
|
|
|
|
e.toString().toStdString());
|
|
|
|
}
|
|
|
|
}
|