You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.8 KiB

import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Layouts 1.0
import QtQuick.Controls 1.0
import QtQuick.Controls.Styles 1.1
10 years ago
import QtWebEngine 1.0
import QtWebEngine.experimental 1.0
import HttpServer 1.0
Item {
id: webPreview
10 years ago
property string pendingPageUrl: ""
property bool initialized: false
function setPreviewUrl(url) {
if (!initialized)
pendingPageUrl = url;
else {
pendingPageUrl = "";
updateContract();
10 years ago
webView.runJavaScript("loadPage(\"" + url + "\")");
}
}
function reload() {
updateContract();
10 years ago
webView.runJavaScript("reloadPage()");
}
function updateContract() {
webView.runJavaScript("updateContract(\"" + clientModel.contractAddress + "\", " + codeModel.code.contractInterface + ")");
}
function reloadOnSave() {
if (autoReloadOnSave.checked)
reload();
}
function updateDocument(documentId, action) {
for (var i = 0; i < pageListModel.count; i++)
if (pageListModel.get(i).documentId === i)
action(i);
}
function changePage() {
if (pageCombo.currentIndex >=0 && pageCombo.currentIndex < pageListModel.count) {
10 years ago
setPreviewUrl(pageListModel.get(pageCombo.currentIndex).path);
} else {
10 years ago
setPreviewUrl("");
}
}
Connections {
target: appContext
onAppLoaded: {
//We need to load the container using file scheme so that web security would allow loading local files in iframe
var containerPage = fileIo.readFile("qrc:///qml/html/WebContainer.html");
webView.loadHtml(containerPage, "file:///WebContainer.html")
}
}
Connections {
target: clientModel
onContractAddressChanged: reload();
onRunComplete: reload();
}
Connections {
target: codeModel
onContractInterfaceChanged: reload();
}
Connections {
10 years ago
target: projectModel
onProjectSaved : reloadOnSave();
onDocumentSaved: reloadOnSave();
onDocumentAdded: {
10 years ago
var document = projectModel.getDocument(documentId)
if (document.isHtml)
pageListModel.append(document);
}
onDocumentRemoved: {
updateDocument(documentId, function(i) { pageListModel.remove(i) } )
}
onDocumentUpdated: {
10 years ago
updateDocument(documentId, function(i) { pageListModel.set(i, projectModel.getDocument(documentId)) } )
}
10 years ago
onProjectLoading: {
for (var i = 0; i < target.listModel.count; i++) {
var document = target.listModel.get(i);
if (document.isHtml) {
pageListModel.append(document);
if (pageListModel.count === 1) //first page added
changePage();
}
}
}
onProjectClosed: {
pageListModel.clear();
}
}
ListModel {
id: pageListModel
}
HttpServer {
id: httpServer
listen: true
accept: true
port: 8893
onClientConnected: {
10 years ago
//filter polling spam
//TODO: do it properly
//var log = _request.content.indexOf("eth_changed") < 0;
var log = true;
10 years ago
if (log)
console.log(_request.content);
var response = clientModel.apiCall(_request.content);
10 years ago
if (log)
console.log(response);
_request.setResponse(response);
}
}
ColumnLayout {
anchors.fill: parent
10 years ago
RowLayout {
10 years ago
anchors.top: parent.top
Layout.fillWidth: true;
Text {
10 years ago
text: qsTr("Page")
}
ComboBox {
id: pageCombo
model: pageListModel
textRole: "name"
currentIndex: -1
onCurrentIndexChanged: changePage()
}
Button {
10 years ago
text: qsTr("Reload")
onClicked: reload()
}
CheckBox {
id: autoReloadOnSave
checked: true
10 years ago
text: qsTr("Auto reload on save")
}
}
10 years ago
WebEngineView {
Layout.fillWidth: true
Layout.fillHeight: true
id: webView
experimental.settings.localContentCanAccessRemoteUrls: true
10 years ago
onJavaScriptConsoleMessage: {
console.log(sourceID + ":" + lineNumber + ":" + message);
}
10 years ago
onLoadingChanged: {
if (!loading) {
initialized = true;
webView.runJavaScript("init(\"" + httpServer.url + "\")");
10 years ago
if (pendingPageUrl)
setPreviewUrl(pendingPageUrl);
}
}
}
}
}