feat: add hard reload signal and wrap root object

This commit is contained in:
outfoxxed 2024-01-31 17:43:18 -08:00
parent d14258df8e
commit 9a5ad44aa9
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
6 changed files with 116 additions and 22 deletions

64
src/cpp/rootwrapper.cpp Normal file
View file

@ -0,0 +1,64 @@
#include "rootwrapper.hpp"
#include <cstdlib>
#include <utility>
#include <qlogging.h>
#include <qobject.h>
#include <qqmlcomponent.h>
#include <qqmlengine.h>
#include <qurl.h>
#include "shell.hpp"
RootWrapper::RootWrapper(QUrl rootUrl):
QObject(nullptr), rootUrl(std::move(rootUrl)), engine(this) {
this->reloadGraph();
if (this->activeRoot == nullptr) {
qCritical() << "could not create scene graph, exiting";
exit(-1); // NOLINT
}
}
void RootWrapper::reloadGraph() {
if (this->activeRoot != nullptr) {
this->engine.clearComponentCache();
}
auto component = QQmlComponent(&this->engine, this->rootUrl);
auto* obj = component.beginCreate(this->engine.rootContext());
if (obj == nullptr) {
qWarning() << "failed to create root component";
return;
}
auto* qtsobj = qobject_cast<QtShell*>(obj);
if (qtsobj == nullptr) {
qWarning() << "root component was not a QtShell";
delete obj;
return;
}
component.completeCreate();
if (this->activeRoot != nullptr) {
this->activeRoot->deleteLater();
this->activeRoot = nullptr;
}
this->activeRoot = qtsobj;
}
void RootWrapper::changeRoot(QtShell* newRoot) {
if (this->activeRoot != nullptr) {
QObject::disconnect(this->destroyConnection);
this->activeRoot->deleteLater();
}
if (newRoot != nullptr) {
this->activeRoot = newRoot;
QObject::connect(this->activeRoot, &QtShell::destroyed, this, &RootWrapper::destroy);
}
}
void RootWrapper::destroy() { this->deleteLater(); }