Skip to content

Commit

Permalink
Implement element and type name query for FactoryComponent.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Feb 6, 2025
1 parent 9c59d93 commit 111d8b9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 40 deletions.
3 changes: 2 additions & 1 deletion app/editor/Main.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import QtQuick
import QtQuick.Controls
// import QtQuick.Controls.Style
import QtQuick.Layouts
import QtQuick.Models
import aspire

Window {
Expand Down
1 change: 1 addition & 0 deletions app/editor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
54 changes: 42 additions & 12 deletions module/aspire/FactoryComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
47 changes: 20 additions & 27 deletions module/aspire/FactoryComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Key, std::unique_ptr<QQmlComponent>, KeyCompare> components;
// Contains a map between QObject type names and their qml names. (i.e. "QQuickRectangle" vs "Rectangle")
std::map<std::string, std::string> qmlTypeMap;

// Contains a map between the qml name and component.
std::map<std::string, std::unique_ptr<QQmlComponent>> components;
QQmlEngine* engine{};
};
}
3 changes: 3 additions & 0 deletions module/aspire/ItemView.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <aspire/ItemView.h>

#include <aspire/FactoryComponent.h>
#include <QCursor>
#include <algorithm>

Expand All @@ -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
Expand Down

0 comments on commit 111d8b9

Please sign in to comment.