-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqmlpreviewer.cpp
134 lines (118 loc) · 4.44 KB
/
qmlpreviewer.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
#include "qmlpreviewer.h"
#include <QDebug>
#include <QDirIterator>
#include <QRegularExpression>
#include <QProcess>
#include <QResource>
#include <QQmlEngine>
#include <QCryptographicHash>
#include <QTimer>
QmlPreviewer::QmlPreviewer(QGuiApplication &app)
: m_view(new QQuickView())
, m_app(app)
{
Q_UNUSED(app)
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this, [&](const QString &path) {
qDebug() << "Path" << path << "updated";
if(m_reloadRequested) {
qDebug() << "Reload already requested.";
return;
}
m_reloadRequested = true;
qDebug() << "Starting reload timer.";
m_timer.singleShot(200, this, &QmlPreviewer::reload);
});
}
void QmlPreviewer::reload()
{
m_reloadRequested = false;
m_view->engine()->clearComponentCache();
qDebug() << "Reloading";
for(auto qrcPath : m_qrcPaths) {
QVariantMap map = qrcPath.toMap();
qDebug() << "Unregistering" << map["path"].toString();
QResource::unregisterResource(map["rcc"].toString(), m_prefix);
}
for(auto qrcPath : m_qrcPaths) {
QVariantMap map = qrcPath.toMap();
QProcess process;
process.start("rcc", QStringList()
<< "-binary" << map["path"].toUrl().toLocalFile()
<< "-o" << map["rcc"].toString());
process.waitForFinished();
qDebug() << "Registering" << map["path"].toString();
bool ok = QResource::registerResource(map["rcc"].toString(), m_prefix);
if(!ok) {
qWarning() << "Could not register resource for" << map["path"].toString() << map["rcc"].toString();
}
QDirIterator it(QString(":" + m_prefix), QStringList() << "*", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString next = it.next();
QString relativeFilePath = next;
QUrl qrcDirectory = map["path"].toUrl().adjusted(QUrl::RemoveFilename);
relativeFilePath = relativeFilePath.replace(":" + m_prefix + "/", "");
// qDebug() << "- Path" << qrcDirectory;
// qDebug() << "- Relative" << relativeFilePath;
QString result = qrcDirectory.resolved(QUrl(relativeFilePath)).toLocalFile();
// qDebug() << "- Result" << result;
if(QFileInfo::exists(result)) {
// qDebug() << "-- Adding path" << result;
m_watcher.addPath(result);
}
}
qDebug() << "Watching" << m_watcher.files().count() << "files";
}
qDebug() << "Requesting QML to reload";
QMetaObject::invokeMethod(m_rootItem, "reload");
}
void QmlPreviewer::setQrcPaths(QVariant qrcPaths)
{
qDebug() << "Handle dialog start";
for(auto qrcPath : m_qrcPaths) {
QVariantMap map = qrcPath.toMap();
qDebug() << "Unregistering" << map["path"].toString();
QResource::unregisterResource(map["rcc"].toString(), m_prefix);
}
QVariantList paths = qrcPaths.toList();
QUrl projectPath;
m_qrcPaths.clear();
QString proPaths = QMLPREVIEWER_RESOURCES;
for(const QString &path : proPaths.split(" ")) {
if(!path.endsWith("qmlpreviewer.qrc")) {
qDebug() << "Propath" << path;
paths.append(QUrl::fromLocalFile(path));
}
}
for(QVariant path : paths) {
QUrl pathUrl = path.toUrl();
QString hash = QCryptographicHash::hash(pathUrl.toString().toLatin1(), QCryptographicHash::Md5).toBase64().replace("=", "").replace("/", "").replace("\\", "");
QVariantMap map{
{"path", pathUrl},
{"rcc", hash + QString(".rcc")},
{"hash", hash}
};
m_qrcPaths.append(map);
qDebug() << "URL" << pathUrl;
projectPath = path.toUrl().adjusted(QUrl::RemoveFilename);
}
reload();
QMetaObject::invokeMethod(m_rootItem, "refreshFileView");
}
bool QmlPreviewer::show()
{
if(m_app.arguments().contains("--qmlpreviewer")) {
m_view->setTitle("QmlPreviewer");
m_view->setSource(QUrl("qrc:///QmlPreviewer/QmlPreviewerDialog.qml"));
m_view->setResizeMode(QQuickView::SizeRootObjectToView);
m_rootItem = m_view->rootObject();
connect(m_rootItem, SIGNAL(changeQrcPaths(QVariant)), this, SLOT(setQrcPaths(QVariant)));
QMetaObject::invokeMethod(m_rootItem, "refresh");
m_view->show();
return true;
}
return false;
}
int QmlPreviewer::exec()
{
return m_app.exec();
}