-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebscript.hh
190 lines (158 loc) · 6.04 KB
/
webscript.hh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef WEBSCRIPT_HH
#define WEBSCRIPT_HH
#include <QJsonDocument>
#include <QJsonObject>
// #include <QWebEngineView>
// #include <QWebChannel>
#include <QJsonArray>
#include <QFileInfo>
#include <iostream>
#include "execution.hh"
#include <QWebFrame>
#include <QWebPage>
#include <QWebView>
#include <QWebInspector>
class WebPage : public QWebPage {
Q_OBJECT
public:
explicit WebPage(QObject* parent = 0)
: QWebPage(parent) {}
protected:
void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID) {
qDebug() << sourceID << lineNumber << message;
// std::cerr << message.toStdString() << "\n";
}
};
// class WebMessenger : public QObject {
// Q_OBJECT
// public:
// WebMessenger(Execution* _execution, std::string _dataString)
// : execution(_execution),
// // treeCanvas(_treeCanvas),
// dataString(_dataString)
// {}
// Q_INVOKABLE void message(int gid) {
// announceSelectNode(gid);
// }
// Q_INVOKABLE void messageMany(QList<QVariant> gidsV) {
// announceSelectManyNodes(gidsV);
// }
// Q_INVOKABLE QString getCSV(void) {
// return QString::fromStdString(dataString);
// }
// Q_SIGNALS:
// void announceSelectNode(int);
// void announceSelectManyNodes(QList<QVariant>);
// private:
// Execution* execution;
// std::string dataString;
// };
// class WebscriptView : public QWebEngineView {
class WebscriptView : public QWebView {
Q_OBJECT
public:
WebscriptView(QWidget* parent, QString htmlPath, Execution* _execution, std::string _dataString)
// : QWebEngineView(parent),
: QWebView(parent),
execution(_execution),
dataString(_dataString)
// messenger(_execution, _dataString)
{
connect(this, &WebscriptView::loadFinished, this, &WebscriptView::onload);
WebPage* p = new WebPage(this);
setPage(p);
connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(jsCleared()));
p->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
// Uncomment this to get JavaScript/HTML debugging
// QWebInspector* inspector = new QWebInspector;
// inspector->setPage(p);
// inspector->show();
// connect(&messenger, &WebMessenger::announceSelectNode, this, &WebscriptView::announceSelectNode);
// connect(&messenger, &WebMessenger::announceSelectManyNodes,
// this, &WebscriptView::announceSelectManyNodes);
// We want the web engine view to expand to fill the dialog
// window it inhabits. When the dialog window is resized, so
// is the web engine view.
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// In "development mode", we want to load the HTML/JavaScript
// directly from the file system, not from an embedded
// resource. This way we can modify it and reload while the
// profiler is running.
#ifdef CP_PROFILER_DEVELOPMENT
QFileInfo fileInfo("../" + htmlPath);
QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
#else
QUrl url("qrc:///" + htmlPath);
#endif
load(url);
// channel = new QWebChannel;
// channel->registerObject("profiler", &messenger);
}
void select(int nodeid) {
QString js;
QTextStream(&js) << "select(" << nodeid << ")";
// page()->runJavaScript(js);
page()->mainFrame()->evaluateJavaScript(js);
}
void selectMany(QList<QVariant> nodeids) {
QString js;
QTextStream(&js) << "selectMany(" << QJsonDocument(QJsonArray::fromVariantList(nodeids)).toJson() << ")";
// page()->runJavaScript(js);
page()->mainFrame()->evaluateJavaScript(js);
}
Q_INVOKABLE QString getCSV(void) {
return QString::fromStdString(dataString);
}
Q_INVOKABLE QString getVariableListString(void) {
return QString::fromStdString(execution->getVariableListString());
}
Q_INVOKABLE void message(int gid) {
announceSelectNode(gid);
}
Q_INVOKABLE void messageMany(QList<QVariant> gidsV) {
announceSelectManyNodes(gidsV);
}
Q_SIGNALS:
void announceSelectNode(int);
void announceSelectManyNodes(QList<QVariant>);
private Q_SLOTS:
void jsCleared(void) {
page()->mainFrame()->addToJavaScriptWindowObject("profiler", this);
}
private:
Execution* execution;
std::string dataString;
// QWebChannel* channel;
// WebMessenger messenger;
void onload(void) {
page()->mainFrame()->evaluateJavaScript("window.onprofilerload()");
// Load qwebchannel javascript
// QFile qwebchanneljs(":/qtwebchannel/qwebchannel.js");
// if (!qwebchanneljs.open(QIODevice::ReadOnly | QIODevice::Text)) {
// qDebug() << "can't open qrc:///qtwebchannel/qwebchannel.js";
// return;
// }
// QTextStream qwebchanneljs_in(&qwebchanneljs);
// QString qwebchanneljs_text = qwebchanneljs_in.readAll();
// QWebEnginePage* p = page();
// p->runJavaScript(qwebchanneljs_text);
// p->setWebChannel(channel);
// QString setup_object("new QWebChannel(qt.webChannelTransport, function (channel) {"
// "window.profiler = channel.objects.profiler;"
// "});"
// );
// p->runJavaScript(setup_object, [p](const QVariant&) { p->runJavaScript("window.onprofilerload()"); });
// // We send the CSV file as a big JSON string. Since a string
// // alone is not a valid JSON document, we wrap it in an object
// // and send that.
// QJsonValue val = QJsonValue(QString::fromStdString(dataString));
// QJsonObject obj({{"s", val}});
// QJsonDocument doc(obj);
// QString msg;
// msg.append("initialise(");
// msg.append(doc.toJson());
// msg.append(")");
// p->runJavaScript(msg);
}
};
#endif