-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveloader.cpp
157 lines (107 loc) · 4.19 KB
/
liveloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "liveloader.h"
#include <QDebug>
#include <QFileInfo>
#include <QQuickWindow>
#include <QQmlContext>
#include <QQuickItem>
#include <QUrl>
LiveLoader::LiveLoader( QQmlApplicationEngine *engine, FileWatcher *fileWatcher, QObject *parent) : QObject(parent)
{
m_fileWatcher = fileWatcher;
m_engine = engine;
m_engine->setOutputWarningsToStandardError(false); //don't display errors in logs
m_currentWindow = nullptr;
m_engine->rootContext()->setContextProperty("fileWatcher", this);
m_engine->rootContext()->setContextProperty("sourceUrl", m_filePath);
QQmlComponent component(m_engine, QUrl("qrc:/Errors.qml"));
m_errorWrapper = qobject_cast<QQuickItem*>(component.create());
QObject::connect(m_fileWatcher, &FileWatcher::qmlChanged, this, &LiveLoader::loadQml);
//QObject::connect(m_engine, &QQmlApplicationEngine::objectCreated, this, &LiveLoader::onLoadedQml);
QObject::connect(m_engine, &QQmlApplicationEngine::warnings,this, &LiveLoader::handleQmlErrors);
}
void LiveLoader::setFile(const QString &path)
{
QFileInfo fi(path);
m_errorMsg = "";
m_filePath = QUrl::fromLocalFile(fi.filePath());
qDebug() << "selected file:" << m_filePath;
//m_engine->trimComponentCache();
created = false;
useWrapper = false;
m_fileWatcher->setDirectory(fi.absolutePath()); //start watching directory
}
void LiveLoader::loadQml()
{
//m_engine->trimComponentCache();
m_errorMsg = "";
emit errorMsgChanged(m_errorMsg);
//clean existing window if not viewWrapper
if (m_currentWindow && !useWrapper) {
qDebug() << "destroy old item";
delete m_currentWindow;
}
m_engine->clearComponentCache();
QQmlComponent component(m_engine, m_filePath);
QObject *item = component.create();
if (!item) {
//something went wrong
qDebug() << "kikou error:" << component.errorString();
m_errorMsg = component.errorString();
//start wrapper to display error
if (!useWrapper) startWrapper();
}else{
//window item ?
QQuickWindow* window = qobject_cast<QQuickWindow*>(item);
if (window){
//qDebug() << "i'm a window";
//make sure that we do not have a wrapper
if (useWrapper && m_currentWindow){
delete m_currentWindow;
useWrapper = false;
//qDebug() << "destroy wrapper";
}
m_currentWindow = window;
QQuickItem *root = window->contentItem();
m_errorWrapper->setParentItem(root);
}else{
//qDebug() << "i'm an item";
QQuickItem *itemToDisplay = qobject_cast<QQuickItem*>(item);
if (!useWrapper) startWrapper();
QQuickItem *loader = m_currentWindow->findChild<QQuickItem*>("liveCodingContainer");
//any already loaded child ? -> delete them
for( QObject* ob: loader->childItems()) {
delete ob; //delete root item
}
itemToDisplay->setParentItem(loader);
//item->setParent(loader);
// for( QObject* ob: loader->childItems()) {
// qDebug()<< "existing:" << ob->metaObject()->className();
// for( QObject* obc: ob->children()) {
// qDebug()<< "existing-child:" << obc->metaObject()->className();
// }
// }
}
}
emit errorMsgChanged(m_errorMsg);
}
void LiveLoader::startWrapper()
{
qDebug() << "start view wrapper";
useWrapper = true;
QQmlComponent component(m_engine, QUrl("qrc:/ViewWrapper.qml"));
QQuickWindow *object = qobject_cast<QQuickWindow*>(component.create());
QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
m_currentWindow = object;
}
void LiveLoader::handleQmlErrors(const QList<QQmlError> &qmlErrors)
{
qDebug() << "kikou errors";
QStringList errors;
foreach (const QQmlError& error, qmlErrors) {
errors.append(error.toString());
}
if (!errors.isEmpty()){
m_errorMsg = errors.join("\n");
emit errorMsgChanged(m_errorMsg);
}
}