205 lines
5.2 KiB

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.0
import QtQuick.Controls.Styles 1.1
import QtWebEngine 1.0
import QtWebEngine.experimental 1.0
import org.ethereum.qml.Clipboard 1.0
10 years ago
import "js/ErrorLocationFormater.js" as ErrorLocationFormater
Item {
signal breakpointsChanged
signal editorTextChanged
signal loadComplete
property bool isClean: true
property string currentText: ""
property string currentMode: ""
property bool initialized: false
property bool unloaded: false
10 years ago
property var currentBreakpoints: []
property string sourceName
property var document
property int fontSize: 0
function setText(text, mode) {
currentText = text;
if (mode !== undefined)
currentMode = mode;
10 years ago
if (initialized && editorBrowser) {
editorBrowser.runJavaScript("setTextBase64(\"" + Qt.btoa(text) + "\")");
editorBrowser.runJavaScript("setMode(\"" + currentMode + "\")");
}
setFocus();
}
function setFocus() {
10 years ago
if (editorBrowser)
editorBrowser.forceActiveFocus();
}
function getText() {
return currentText;
}
function syncClipboard() {
10 years ago
if (Qt.platform.os == "osx" && editorBrowser) {
var text = clipboard.text;
editorBrowser.runJavaScript("setClipboardBase64(\"" + Qt.btoa(text) + "\")");
}
}
function highlightExecution(location) {
10 years ago
if (initialized && editorBrowser)
editorBrowser.runJavaScript("highlightExecution(" + location.start + "," + location.end + ")");
}
function showWarning(content) {
10 years ago
if (initialized && editorBrowser)
editorBrowser.runJavaScript("showWarning('" + content + "')");
}
10 years ago
function getBreakpoints() {
return currentBreakpoints;
}
function toggleBreakpoint() {
10 years ago
if (initialized && editorBrowser)
editorBrowser.runJavaScript("toggleBreakpoint()");
10 years ago
}
function changeGeneration() {
10 years ago
if (initialized && editorBrowser)
editorBrowser.runJavaScript("changeGeneration()", function(result) {});
}
10 years ago
function goToCompilationError() {
if (initialized && editorBrowser)
editorBrowser.runJavaScript("goToCompilationError()", function(result) {});
}
function setFontSize(size) {
fontSize = size;
if (initialized && editorBrowser)
editorBrowser.runJavaScript("setFontSize(" + size + ")", function(result) {});
}
function setGasCosts(gasCosts) {
if (initialized && editorBrowser)
editorBrowser.runJavaScript("setGasCosts('" + JSON.stringify(gasCosts) + "')", function(result) {});
}
10 years ago
function displayGasEstimation(show) {
10 years ago
if (initialized && editorBrowser)
editorBrowser.runJavaScript("displayGasEstimation('" + show + "')", function(result) {});
10 years ago
}
Clipboard
{
id: clipboard
}
Connections {
target: clipboard
onClipboardChanged: syncClipboard()
}
anchors.top: parent.top
id: codeEditorView
anchors.fill: parent
WebEngineView {
id: editorBrowser
url: "qrc:///qml/html/codeeditor.html"
anchors.fill: parent
experimental.settings.javascriptCanAccessClipboard: true
onJavaScriptConsoleMessage: {
console.log("editor: " + sourceID + ":" + lineNumber + ":" + message);
}
10 years ago
Component.onDestruction:
{
codeModel.onCompilationComplete.disconnect(compilationComplete);
codeModel.onCompilationError.disconnect(compilationError);
}
onLoadingChanged:
{
10 years ago
if (!loading && editorBrowser) {
initialized = true;
setFontSize(fontSize);
setText(currentText, currentMode);
runJavaScript("getTextChanged()", function(result) { });
pollTimer.running = true;
syncClipboard();
10 years ago
if (currentMode === "solidity")
{
10 years ago
codeModel.onCompilationComplete.connect(compilationComplete);
codeModel.onCompilationError.connect(compilationError);
10 years ago
}
parent.changeGeneration();
loadComplete();
}
}
10 years ago
function compilationComplete()
{
if (editorBrowser)
10 years ago
{
10 years ago
editorBrowser.runJavaScript("compilationComplete()", function(result) { });
10 years ago
parent.displayGasEstimation(gasEstimationAction.checked);
}
10 years ago
}
function compilationError(error, firstLocation, secondLocations)
10 years ago
{
if (!editorBrowser || !error)
return;
var detail = error.split('\n')[0];
var reg = detail.match(/:\d+:\d+:/g);
if (reg !== null)
detail = detail.replace(reg[0], "");
10 years ago
displayErrorAnnotations(detail, firstLocation, secondLocations);
}
10 years ago
function displayErrorAnnotations(detail, location, secondaryErrors)
{
10 years ago
editorBrowser.runJavaScript("compilationError('" + sourceName + "', '" + JSON.stringify(location) + "', '" + detail + "', '" + JSON.stringify(secondaryErrors) + "')", function(result){});
10 years ago
}
Timer
{
id: pollTimer
interval: 30
running: false
repeat: true
onTriggered: {
10 years ago
if (!editorBrowser)
return;
editorBrowser.runJavaScript("getTextChanged()", function(result) {
if (result === true && editorBrowser) {
editorBrowser.runJavaScript("getText()" , function(textValue) {
currentText = textValue;
editorTextChanged();
});
}
});
10 years ago
editorBrowser.runJavaScript("getBreakpointsChanged()", function(result) {
if (result === true && editorBrowser) {
10 years ago
editorBrowser.runJavaScript("getBreakpoints()" , function(bp) {
if (currentBreakpoints !== bp) {
currentBreakpoints = bp;
breakpointsChanged();
}
});
}
});
editorBrowser.runJavaScript("isClean()", function(result) {
isClean = result;
});
}
}
}
}