2021-05-04 18:31:15 +00:00
|
|
|
#include "sandboxapp.h"
|
|
|
|
|
|
|
|
#include <QQmlContext>
|
2021-05-12 08:11:25 +00:00
|
|
|
#include <QWindow>
|
2021-05-04 18:31:15 +00:00
|
|
|
#include <QDebug>
|
2022-08-04 10:54:13 +00:00
|
|
|
#include <QDirIterator>
|
2021-05-04 18:31:15 +00:00
|
|
|
|
|
|
|
SandboxApp::SandboxApp(int &argc, char **argv)
|
2022-08-04 10:54:13 +00:00
|
|
|
: QGuiApplication(argc, argv)
|
2021-05-04 18:31:15 +00:00
|
|
|
{
|
2022-08-04 10:54:13 +00:00
|
|
|
#ifdef QT_DEBUG
|
2023-05-05 16:05:44 +00:00
|
|
|
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, [this](const QString& path) {
|
|
|
|
qDebug().noquote() << QString("File change detected in '%1'").arg(path);
|
2022-08-04 10:54:13 +00:00
|
|
|
restartEngine();
|
|
|
|
});
|
|
|
|
#endif
|
2021-05-04 18:31:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 16:05:44 +00:00
|
|
|
void SandboxApp::watchDirectoryChanges(const QString& path)
|
2021-05-04 18:31:15 +00:00
|
|
|
{
|
2023-05-05 16:05:44 +00:00
|
|
|
qDebug() << "Iterating to watch over:" << path;
|
|
|
|
|
|
|
|
const auto dirFilters = QDir::Files | QDir::NoDotAndDotDot;
|
|
|
|
const auto fileNameFilters = QStringList { "*.qml" };
|
|
|
|
|
|
|
|
for (QDirIterator it(path, fileNameFilters, dirFilters, QDirIterator::Subdirectories); it.hasNext(); it.next()) {
|
2022-08-04 10:54:13 +00:00
|
|
|
if (!it.filePath().isEmpty()) {
|
|
|
|
m_watcher.addPath(it.filePath());
|
|
|
|
}
|
|
|
|
}
|
2023-05-05 16:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SandboxApp::startEngine()
|
|
|
|
{
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
watchDirectoryChanges(SANDBOX_SRC_DIR);
|
|
|
|
watchDirectoryChanges(STATUSQ_MODULE_PATH);
|
2021-06-15 09:16:22 +00:00
|
|
|
#endif
|
2023-05-05 16:05:44 +00:00
|
|
|
restartEngine();
|
|
|
|
}
|
2021-05-04 18:31:15 +00:00
|
|
|
|
2023-05-05 16:05:44 +00:00
|
|
|
void SandboxApp::restartEngine()
|
|
|
|
{
|
|
|
|
const bool firstRun { !m_engine };
|
|
|
|
|
|
|
|
if (!firstRun)
|
|
|
|
qDebug() << "Restarting QML engine";
|
|
|
|
|
|
|
|
m_engine = std::make_unique<QQmlApplicationEngine>();
|
|
|
|
m_engine->addImportPath(STATUSQ_MODULE_IMPORT_PATH);
|
|
|
|
|
|
|
|
if (firstRun)
|
|
|
|
qDebug() << "QQmlEngine import paths: " << m_engine->importPathList();
|
2023-04-14 08:18:56 +00:00
|
|
|
|
2023-05-05 16:05:44 +00:00
|
|
|
QObject::connect(m_engine.get(), &QQmlApplicationEngine::objectCreated,
|
2023-04-14 08:18:56 +00:00
|
|
|
this, [this](QObject *obj, const QUrl &objUrl) {
|
|
|
|
if (!obj && m_url == objUrl)
|
2021-05-04 18:31:15 +00:00
|
|
|
QCoreApplication::exit(-1);
|
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
|
2023-05-05 16:05:44 +00:00
|
|
|
m_engine->load(m_url);
|
2021-05-04 18:31:15 +00:00
|
|
|
}
|