From 4eb5dc559342677d2e45a98c4e4db9c1c85c6757 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 21 Mar 2024 05:32:55 -0700 Subject: [PATCH] core/variants: expose instances list as a property --- src/core/variants.cpp | 26 ++++++++++++++++++++------ src/core/variants.hpp | 11 ++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/core/variants.cpp b/src/core/variants.cpp index 8f251c15..aa3bddef 100644 --- a/src/core/variants.cpp +++ b/src/core/variants.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "reload.hpp" @@ -15,10 +16,10 @@ void Variants::onReload(QObject* oldInstance) { auto* old = qobject_cast(oldInstance); - for (auto& [variant, instanceObj]: this->instances.values) { + for (auto& [variant, instanceObj]: this->mInstances.values) { QObject* oldInstance = nullptr; if (old != nullptr) { - auto& values = old->instances.values; + auto& values = old->mInstances.values; if (variant.canConvert()) { auto variantMap = variant.value(); @@ -96,6 +97,19 @@ void Variants::setModel(const QVariant& model) { this->updateVariants(); emit this->modelChanged(); + emit this->instancesChanged(); +} + +QQmlListProperty Variants::instances() { + return QQmlListProperty(this, nullptr, &Variants::instanceCount, &Variants::instanceAt); +} + +qsizetype Variants::instanceCount(QQmlListProperty* prop) { + return static_cast(prop->object)->mInstances.values.length(); // NOLINT +} + +QObject* Variants::instanceAt(QQmlListProperty* prop, qsizetype i) { + return static_cast(prop->object)->mInstances.values.at(i).second; // NOLINT } void Variants::componentComplete() { @@ -110,12 +124,12 @@ void Variants::updateVariants() { } // clean up removed entries - for (auto iter = this->instances.values.begin(); iter < this->instances.values.end();) { + for (auto iter = this->mInstances.values.begin(); iter < this->mInstances.values.end();) { if (this->mModel.contains(iter->first)) { iter++; } else { iter->second->deleteLater(); - iter = this->instances.values.erase(iter); + iter = this->mInstances.values.erase(iter); } } @@ -130,7 +144,7 @@ void Variants::updateVariants() { } { - if (this->instances.contains(variant)) { + if (this->mInstances.contains(variant)) { continue; // we dont need to recreate this one } @@ -151,7 +165,7 @@ void Variants::updateVariants() { QQmlEngine::setObjectOwnership(instance, QQmlEngine::CppOwnership); instance->setParent(this); - this->instances.insert(variant, instance); + this->mInstances.insert(variant, instance); if (this->loaded) { if (auto* reloadable = qobject_cast(instance)) reloadable->onReload(nullptr); diff --git a/src/core/variants.hpp b/src/core/variants.hpp index cdfea7ea..a9071cf8 100644 --- a/src/core/variants.hpp +++ b/src/core/variants.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,8 @@ class Variants: public Reloadable { /// Each set creates an instance of the component, which are updated when the input sets update. QSDOC_PROPERTY_OVERRIDE(QList model READ model WRITE setModel NOTIFY modelChanged); QSDOC_HIDE Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged); + /// Current instances of the delegate. + Q_PROPERTY(QQmlListProperty instances READ instances NOTIFY instancesChanged); Q_CLASSINFO("DefaultProperty", "delegate"); QML_ELEMENT; @@ -64,14 +67,20 @@ public: [[nodiscard]] QVariant model() const; void setModel(const QVariant& model); + QQmlListProperty instances(); + signals: void modelChanged(); + void instancesChanged(); private: + static qsizetype instanceCount(QQmlListProperty* prop); + static QObject* instanceAt(QQmlListProperty* prop, qsizetype i); + void updateVariants(); QQmlComponent* mDelegate = nullptr; QVariantList mModel; - AwfulMap instances; + AwfulMap mInstances; bool loaded = false; };