summary refs log tree commit diff
path: root/src/timeline2
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2019-10-25 13:20:05 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2019-11-23 20:06:15 +0100
commit3d6f502bcc4bae477eb3f8d51aa7b90a6c9e9f46 (patch)
tree79410962404eef52737deff6d3e8b91776210513 /src/timeline2
parentSmall fixes to delegate chooser implementation (diff)
downloadnheko-3d6f502bcc4bae477eb3f8d51aa7b90a6c9e9f46.tar.xz
Incubate delegates asynchronously
Diffstat (limited to 'src/timeline2')
-rw-r--r--src/timeline2/DelegateChooser.cpp35
-rw-r--r--src/timeline2/DelegateChooser.h15
2 files changed, 41 insertions, 9 deletions
diff --git a/src/timeline2/DelegateChooser.cpp b/src/timeline2/DelegateChooser.cpp
index b86fc6cc..e558da61 100644
--- a/src/timeline2/DelegateChooser.cpp
+++ b/src/timeline2/DelegateChooser.cpp
@@ -95,17 +95,11 @@ DelegateChooser::recalcChild()
                 auto choiceValue = choice->roleValue();
                 if (!roleValue_.isValid() || !choiceValue.isValid() || choiceValue == roleValue_) {
                         if (child) {
-                                // delete child;
+                                child->setParentItem(nullptr);
                                 child = nullptr;
                         }
 
-                        child = dynamic_cast<QQuickItem *>(
-                          choice->delegate()->create(QQmlEngine::contextForObject(this)));
-                        child->setParentItem(this);
-                        connect(this->child, &QQuickItem::heightChanged, this, [this]() {
-                                this->setHeight(this->child->height());
-                        });
-                        this->setHeight(this->child->height());
+                        choice->delegate()->create(incubator, QQmlEngine::contextForObject(this));
                         return;
                 }
         }
@@ -118,3 +112,28 @@ DelegateChooser::componentComplete()
         recalcChild();
 }
 
+void
+DelegateChooser::DelegateIncubator::statusChanged(QQmlIncubator::Status status)
+{
+        if (status == QQmlIncubator::Ready) {
+                chooser.child = dynamic_cast<QQuickItem *>(object());
+                if (chooser.child == nullptr) {
+                        nhlog::ui()->error("Delegate has to be derived of Item!");
+                        delete chooser.child;
+                        return;
+                }
+
+                chooser.child->setParentItem(&chooser);
+                connect(chooser.child, &QQuickItem::heightChanged, &chooser, [this]() {
+                        chooser.setHeight(chooser.child->height());
+                });
+                chooser.setHeight(chooser.child->height());
+                QQmlEngine::setObjectOwnership(chooser.child,
+                                               QQmlEngine::ObjectOwnership::JavaScriptOwnership);
+
+        } else if (status == QQmlIncubator::Error) {
+                for (const auto &e : errors())
+                        nhlog::ui()->error("Error instantiating delegate: {}",
+                                           e.toString().toStdString());
+        }
+}
diff --git a/src/timeline2/DelegateChooser.h b/src/timeline2/DelegateChooser.h
index 7350e0d3..a20a1489 100644
--- a/src/timeline2/DelegateChooser.h
+++ b/src/timeline2/DelegateChooser.h
@@ -5,6 +5,7 @@
 #pragma once
 
 #include <QQmlComponent>
+#include <QQmlIncubator>
 #include <QQmlListProperty>
 #include <QQuickItem>
 #include <QtCore/QObject>
@@ -59,9 +60,21 @@ signals:
         void roleValueChanged();
 
 private:
+        struct DelegateIncubator : public QQmlIncubator
+        {
+                DelegateIncubator(DelegateChooser &parent)
+                  : QQmlIncubator(QQmlIncubator::AsynchronousIfNested)
+                  , chooser(parent)
+                {}
+                void statusChanged(QQmlIncubator::Status status) override;
+
+                DelegateChooser &chooser;
+        };
+
         QVariant roleValue_;
         QList<DelegateChoice *> choices_;
-        QQuickItem *child;
+        QQuickItem *child = nullptr;
+        DelegateIncubator incubator{*this};
 
         static void appendChoice(QQmlListProperty<DelegateChoice> *, DelegateChoice *);
         static int choiceCount(QQmlListProperty<DelegateChoice> *);