2024-01-31 17:43:18 -08:00
|
|
|
#include "rootwrapper.hpp"
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2024-03-03 17:05:19 -08:00
|
|
|
#include <qdir.h>
|
2024-02-04 23:00:59 -08:00
|
|
|
#include <qfileinfo.h>
|
2024-01-31 17:43:18 -08:00
|
|
|
#include <qlogging.h>
|
|
|
|
|
#include <qobject.h>
|
|
|
|
|
#include <qqmlcomponent.h>
|
|
|
|
|
#include <qqmlengine.h>
|
2024-11-22 20:18:04 -08:00
|
|
|
#include <qquickitem.h>
|
2024-05-30 02:39:37 -07:00
|
|
|
#include <qtmetamacros.h>
|
2024-01-31 17:43:18 -08:00
|
|
|
#include <qurl.h>
|
|
|
|
|
|
2024-11-22 20:18:04 -08:00
|
|
|
#include "../window/floatingwindow.hpp"
|
2024-03-13 22:53:05 -07:00
|
|
|
#include "generation.hpp"
|
2024-03-03 17:05:19 -08:00
|
|
|
#include "qmlglobal.hpp"
|
2024-03-14 00:16:22 -07:00
|
|
|
#include "scan.hpp"
|
2024-01-31 17:43:18 -08:00
|
|
|
|
2024-08-02 02:09:55 -07:00
|
|
|
RootWrapper::RootWrapper(QString rootPath, QString shellId)
|
2024-08-30 15:23:48 -07:00
|
|
|
: QObject(nullptr)
|
|
|
|
|
, rootPath(std::move(rootPath))
|
|
|
|
|
, shellId(std::move(shellId))
|
|
|
|
|
, originalWorkingDirectory(QDir::current().absolutePath()) {
|
2024-03-04 01:31:31 -08:00
|
|
|
// clang-format off
|
|
|
|
|
QObject::connect(QuickshellSettings::instance(), &QuickshellSettings::watchFilesChanged, this, &RootWrapper::onWatchFilesChanged);
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-02-01 01:29:45 -08:00
|
|
|
this->reloadGraph(true);
|
2024-01-31 17:43:18 -08:00
|
|
|
|
2024-03-13 22:53:05 -07:00
|
|
|
if (this->generation == nullptr) {
|
2024-01-31 17:43:18 -08:00
|
|
|
qCritical() << "could not create scene graph, exiting";
|
|
|
|
|
exit(-1); // NOLINT
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-03 06:26:41 -08:00
|
|
|
RootWrapper::~RootWrapper() {
|
|
|
|
|
// event loop may no longer be running so deleteLater is not an option
|
2024-03-13 22:53:05 -07:00
|
|
|
if (this->generation != nullptr) {
|
2024-06-18 17:03:38 -07:00
|
|
|
this->generation->shutdown();
|
2024-03-13 22:53:05 -07:00
|
|
|
}
|
2024-03-03 06:26:41 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-01 01:29:45 -08:00
|
|
|
void RootWrapper::reloadGraph(bool hard) {
|
2024-05-29 15:07:10 -07:00
|
|
|
auto rootPath = QFileInfo(this->rootPath).dir();
|
|
|
|
|
auto scanner = QmlScanner(rootPath);
|
2024-03-14 00:16:22 -07:00
|
|
|
scanner.scanQmlFile(this->rootPath);
|
|
|
|
|
|
2024-05-29 15:07:10 -07:00
|
|
|
auto* generation = new EngineGeneration(rootPath, std::move(scanner));
|
2024-03-25 01:57:15 -07:00
|
|
|
generation->wrapper = this;
|
2024-03-12 14:55:51 -07:00
|
|
|
|
2024-03-13 22:53:05 -07:00
|
|
|
// todo: move into EngineGeneration
|
|
|
|
|
if (this->generation != nullptr) {
|
2024-03-04 01:31:31 -08:00
|
|
|
QuickshellSettings::reset();
|
2024-01-31 17:43:18 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-03 17:05:19 -08:00
|
|
|
QDir::setCurrent(this->originalWorkingDirectory);
|
|
|
|
|
|
2024-03-14 00:16:22 -07:00
|
|
|
auto url = QUrl::fromLocalFile(this->rootPath);
|
|
|
|
|
// unless the original file comes from the qsintercept scheme
|
|
|
|
|
url.setScheme("qsintercept");
|
2024-04-20 02:59:50 -07:00
|
|
|
auto component = QQmlComponent(generation->engine, url);
|
2024-02-01 01:29:45 -08:00
|
|
|
|
2024-11-22 19:40:39 -08:00
|
|
|
auto* newRoot = component.beginCreate(generation->engine->rootContext());
|
2024-02-01 01:29:45 -08:00
|
|
|
|
|
|
|
|
if (newRoot == nullptr) {
|
2024-11-22 19:40:39 -08:00
|
|
|
const QString error = "failed to create root component\n" + component.errorString();
|
2024-05-30 02:39:37 -07:00
|
|
|
qWarning().noquote() << error;
|
2024-05-31 04:03:00 -07:00
|
|
|
generation->destroy();
|
2024-05-30 02:39:37 -07:00
|
|
|
|
|
|
|
|
if (this->generation != nullptr && this->generation->qsgInstance != nullptr) {
|
|
|
|
|
emit this->generation->qsgInstance->reloadFailed(error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 17:43:18 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 20:18:04 -08:00
|
|
|
if (auto* item = qobject_cast<QQuickItem*>(newRoot)) {
|
|
|
|
|
auto* window = new FloatingWindowInterface();
|
|
|
|
|
item->setParent(window);
|
|
|
|
|
item->setParentItem(window->contentItem());
|
|
|
|
|
window->setWidth(static_cast<int>(item->width()));
|
|
|
|
|
window->setHeight(static_cast<int>(item->height()));
|
|
|
|
|
newRoot = window;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 22:53:05 -07:00
|
|
|
generation->root = newRoot;
|
2024-03-03 01:26:43 -08:00
|
|
|
|
2024-03-13 22:53:05 -07:00
|
|
|
component.completeCreate();
|
2024-03-03 01:26:43 -08:00
|
|
|
|
2024-08-30 15:23:48 -07:00
|
|
|
if (this->generation) {
|
|
|
|
|
QObject::disconnect(this->generation, nullptr, this, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 02:39:37 -07:00
|
|
|
auto isReload = this->generation != nullptr;
|
2024-03-13 22:53:05 -07:00
|
|
|
generation->onReload(hard ? nullptr : this->generation);
|
2024-05-31 00:26:34 -07:00
|
|
|
|
2024-08-30 15:23:48 -07:00
|
|
|
if (hard && this->generation) {
|
2024-05-31 00:26:34 -07:00
|
|
|
this->generation->destroy();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 22:53:05 -07:00
|
|
|
this->generation = generation;
|
2024-01-31 17:43:18 -08:00
|
|
|
|
2024-03-13 22:53:05 -07:00
|
|
|
qInfo() << "Configuration Loaded";
|
2024-03-12 14:55:51 -07:00
|
|
|
|
2024-08-28 15:30:02 -07:00
|
|
|
QObject::connect(this->generation, &QObject::destroyed, this, &RootWrapper::generationDestroyed);
|
2024-03-14 00:16:22 -07:00
|
|
|
QObject::connect(
|
|
|
|
|
this->generation,
|
|
|
|
|
&EngineGeneration::filesChanged,
|
|
|
|
|
this,
|
|
|
|
|
&RootWrapper::onWatchedFilesChanged
|
|
|
|
|
);
|
|
|
|
|
|
2024-03-04 01:31:31 -08:00
|
|
|
this->onWatchFilesChanged();
|
2024-05-30 02:39:37 -07:00
|
|
|
|
|
|
|
|
if (isReload && this->generation->qsgInstance != nullptr) {
|
|
|
|
|
emit this->generation->qsgInstance->reloadCompleted();
|
|
|
|
|
}
|
2024-01-31 17:43:18 -08:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 15:30:02 -07:00
|
|
|
void RootWrapper::generationDestroyed() { this->generation = nullptr; }
|
|
|
|
|
|
2024-03-04 01:31:31 -08:00
|
|
|
void RootWrapper::onWatchFilesChanged() {
|
|
|
|
|
auto watchFiles = QuickshellSettings::instance()->watchFiles();
|
2024-03-14 00:16:22 -07:00
|
|
|
if (this->generation != nullptr) {
|
|
|
|
|
this->generation->setWatchingFiles(watchFiles);
|
2024-01-31 17:43:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-04 23:00:59 -08:00
|
|
|
void RootWrapper::onWatchedFilesChanged() { this->reloadGraph(false); }
|