From 111d8b94ddfed77b2cde1632b0cc70df9de6b03b Mon Sep 17 00:00:00 2001 From: Aaron Shelley Date: Wed, 5 Feb 2025 22:09:45 -0500 Subject: [PATCH] Implement element and type name query for FactoryComponent. --- app/editor/Main.qml | 3 +- app/editor/main.cpp | 1 + module/aspire/FactoryComponent.cpp | 54 +++++++++++++++++++++++------- module/aspire/FactoryComponent.h | 47 +++++++++++--------------- module/aspire/ItemView.cpp | 3 ++ 5 files changed, 68 insertions(+), 40 deletions(-) diff --git a/app/editor/Main.qml b/app/editor/Main.qml index d8ce9bf..4082dd6 100644 --- a/app/editor/Main.qml +++ b/app/editor/Main.qml @@ -1,6 +1,7 @@ import QtQuick import QtQuick.Controls -// import QtQuick.Controls.Style +import QtQuick.Layouts +import QtQuick.Models import aspire Window { diff --git a/app/editor/main.cpp b/app/editor/main.cpp index 4e53174..090098a 100644 --- a/app/editor/main.cpp +++ b/app/editor/main.cpp @@ -8,6 +8,7 @@ int main(int argc, char** argv) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; + QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, [] { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("app.editor", "Main"); diff --git a/module/aspire/FactoryComponent.cpp b/module/aspire/FactoryComponent.cpp index 560f5e1..89bafba 100644 --- a/module/aspire/FactoryComponent.cpp +++ b/module/aspire/FactoryComponent.cpp @@ -38,25 +38,55 @@ FactoryComponent::FactoryComponent(QQmlEngine* engine) : engine{engine} continue; } - qInfo() << "Loaded " << type.elementName() << " (" << type.typeName() << ")"; - Key key{.elementName = type.elementName().toStdString(), .typeName = type.typeName().toStdString()}; - this->components[std::move(key)] = std::move(component); + const auto elementName = type.elementName().toStdString(); + const auto typeName = type.typeName().toStdString(); + + this->qmlTypeMap[typeName] = elementName; + qInfo() << "Loaded " << type.elementName() << " to " << type.typeName(); + this->components[elementName] = std::move(component); } +} + +FactoryComponent::~FactoryComponent() +{ + Singleton = nullptr; +} - auto foundIt = this->components.find(std::string("Rectangle")); +auto FactoryComponent::findComponent(QObject* x) const -> QQmlComponent* +{ + return this->findComponent(std::string{this->findQmlName(x)}); +} - if(foundIt != std::end(this->components)) +auto FactoryComponent::findComponent(const std::string& x) const -> QQmlComponent* +{ + if(std::empty(x) == true) { - auto& c = foundIt->second; - auto* object = c->create(); - auto type = QQmlMetaType::qmlType(object->metaObject()); - qDebug() << "Found " << type.elementName() << ", " << type.typeName(); + return nullptr; + } + + const auto foundIt = this->components.find(x); - delete object; + if(foundIt == std::end(this->components)) + { + return nullptr; } + + return foundIt->second.get(); } -FactoryComponent::~FactoryComponent() +auto FactoryComponent::findQmlName(QObject* x) const -> std::string_view { - Singleton = nullptr; + if(x == nullptr) + { + return {}; + } + + const auto foundIt = this->qmlTypeMap.find(x->metaObject()->className()); + + if(foundIt == std::end(this->qmlTypeMap)) + { + return {}; + } + + return foundIt->second; } diff --git a/module/aspire/FactoryComponent.h b/module/aspire/FactoryComponent.h index 7555e84..a82a28c 100644 --- a/module/aspire/FactoryComponent.h +++ b/module/aspire/FactoryComponent.h @@ -21,34 +21,27 @@ namespace aspire static auto Instance() -> FactoryComponent*; + /// @brief Find the QQmlComponent registered for the given object. + /// @param x The object to perform the search with. + /// @return A pointer the the associated QQmlComponent. Nullptr if the component could not be found. + auto findComponent(QObject* x) const -> QQmlComponent*; + + /// @brief Find the QQmlComponent registered for the given qml type name. + /// @param x The type name to perform the search with. + /// @return A pointer the the associated QQmlComponent. Nullptr if the component could not be found. + auto findComponent(const std::string& x) const -> QQmlComponent*; + + /// @brief Find the QML name registered for the given QObject. + /// @param x The runtime QObject to perform the search with. + /// @return The QML name of the given object. Empty string if it doesn't exist. + auto findQmlName(QObject* x) const -> std::string_view; + private: - struct Key - { - std::string elementName; - std::string typeName; - }; - - struct KeyCompare - { - using is_transparent = void; - - bool operator()(const Key& lhs, const Key& rhs) const - { - return lhs.elementName < rhs.elementName && lhs.typeName < rhs.elementName; - } - - bool operator()(const Key& lhs, const std::string& rhs) const - { - return lhs.elementName < rhs && lhs.typeName < rhs; - } - - bool operator()(const std::string& lhs, const Key& rhs) const - { - return lhs < rhs.elementName && lhs < rhs.typeName; - } - }; - - std::map, KeyCompare> components; + // Contains a map between QObject type names and their qml names. (i.e. "QQuickRectangle" vs "Rectangle") + std::map qmlTypeMap; + + // Contains a map between the qml name and component. + std::map> components; QQmlEngine* engine{}; }; } \ No newline at end of file diff --git a/module/aspire/ItemView.cpp b/module/aspire/ItemView.cpp index e924742..504ebb3 100644 --- a/module/aspire/ItemView.cpp +++ b/module/aspire/ItemView.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -22,6 +23,8 @@ ItemView::ItemView(QQuickItem* parent) : QQuickItem(parent) this->contentItem->setScale(this->zoom); }); + + // qDebug() << "Element: " << FactoryComponent::Instance()->findQmlName(this); } void ItemView::setContentItem(QQuickItem* x) noexcept