2024-02-01 01:43:18 +00:00
|
|
|
#include "rootwrapper.hpp"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <qlogging.h>
|
|
|
|
#include <qobject.h>
|
|
|
|
#include <qqmlcomponent.h>
|
|
|
|
#include <qqmlengine.h>
|
|
|
|
#include <qurl.h>
|
|
|
|
|
2024-02-01 09:29:45 +00:00
|
|
|
#include "scavenge.hpp"
|
2024-02-01 01:43:18 +00:00
|
|
|
#include "shell.hpp"
|
|
|
|
|
|
|
|
RootWrapper::RootWrapper(QUrl rootUrl):
|
|
|
|
QObject(nullptr), rootUrl(std::move(rootUrl)), engine(this) {
|
2024-02-01 09:29:45 +00:00
|
|
|
this->reloadGraph(true);
|
2024-02-01 01:43:18 +00:00
|
|
|
|
|
|
|
if (this->activeRoot == nullptr) {
|
|
|
|
qCritical() << "could not create scene graph, exiting";
|
|
|
|
exit(-1); // NOLINT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:29:45 +00:00
|
|
|
void RootWrapper::reloadGraph(bool hard) {
|
2024-02-01 01:43:18 +00:00
|
|
|
if (this->activeRoot != nullptr) {
|
|
|
|
this->engine.clearComponentCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto component = QQmlComponent(&this->engine, this->rootUrl);
|
2024-02-01 09:29:45 +00:00
|
|
|
|
|
|
|
SCAVENGE_PARENT = hard ? nullptr : this;
|
2024-02-01 01:43:18 +00:00
|
|
|
auto* obj = component.beginCreate(this->engine.rootContext());
|
2024-02-01 09:29:45 +00:00
|
|
|
SCAVENGE_PARENT = nullptr;
|
|
|
|
|
2024-02-01 01:43:18 +00:00
|
|
|
if (obj == nullptr) {
|
|
|
|
qWarning() << "failed to create root component";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:29:45 +00:00
|
|
|
auto* newRoot = qobject_cast<QtShell*>(obj);
|
|
|
|
if (newRoot == nullptr) {
|
2024-02-01 01:43:18 +00:00
|
|
|
qWarning() << "root component was not a QtShell";
|
|
|
|
delete obj;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
component.completeCreate();
|
|
|
|
|
|
|
|
if (this->activeRoot != nullptr) {
|
|
|
|
this->activeRoot->deleteLater();
|
|
|
|
this->activeRoot = nullptr;
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:29:45 +00:00
|
|
|
this->activeRoot = newRoot;
|
2024-02-01 01:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:29:45 +00:00
|
|
|
QObject* RootWrapper::scavengeTargetFor(QObject* /* child */) { return this->activeRoot; }
|
|
|
|
|
2024-02-01 01:43:18 +00:00
|
|
|
void RootWrapper::destroy() { this->deleteLater(); }
|