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.

266 lines
6.9 KiB

10 years ago
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
10 years ago
import QtQuick.Dialogs 1.1
import QtQuick.Window 2.2
10 years ago
import QtQuick.Layouts 1.1
import org.ethereum.qml.QEther 1.0
import "js/QEtherHelper.js" as QEtherHelper
Item {
property alias model: stateListModel
property var stateList: []
function fromPlainStateItem(s) {
return {
title: s.title,
balance: QEtherHelper.createEther(s.balance.value, s.balance.unit),
transactions: s.transactions.map(fromPlainTransactionItem)
};
}
function fromPlainTransactionItem(t) {
10 years ago
var r = {
contractId: t.contractId,
10 years ago
functionId: t.functionId,
10 years ago
url: t.url,
10 years ago
value: QEtherHelper.createEther(t.value.value, t.value.unit),
gas: QEtherHelper.createBigInt(t.gas.value), //t.gas,//QEtherHelper.createEther(t.gas.value, t.gas.unit),
10 years ago
gasPrice: QEtherHelper.createEther(t.gasPrice.value, t.gasPrice.unit),
10 years ago
stdContract: t.stdContract,
parameters: {}
10 years ago
};
var qType = [];
for (var key in t.parameters)
{
r.parameters[key] = t.parameters[key].value;
var type = t.parameters[key].type;
var varComponent;
if (type.indexOf("int") !== -1)
varComponent = Qt.createComponent("qrc:/qml/QIntType.qml");
else if (type.indexOf("real") !== -1)
varComponent = Qt.createComponent("qrc:/qml/QRealType.qml");
else if (type.indexOf("string") !== -1 || type.indexOf("text") !== -1)
varComponent = Qt.createComponent("qrc:/qml/QStringType.qml");
else if (type.indexOf("hash") !== -1 || type.indexOf("address") !== -1)
varComponent = Qt.createComponent("qrc:/qml/QHashType.qml");
else if (type.indexOf("bool") !== -1)
varComponent = Qt.createComponent("qrc:/qml/QBoolType.qml");
10 years ago
else {
console.log("Unknown parameter type: " + type);
continue;
}
var param = varComponent.createObject(stateListModel);
var dec = Qt.createComponent("qrc:/qml/QVariableDeclaration.qml");
param.setDeclaration(dec.createObject(stateListModel, { "type": type }));
param.setValue(r.parameters[key]);
qType.push(param);
10 years ago
}
r.qType = qType;
10 years ago
return r;
10 years ago
}
function toPlainStateItem(s) {
return {
title: s.title,
10 years ago
balance: { value: s.balance.value, unit: s.balance.unit },
10 years ago
transactions: s.transactions.map(toPlainTransactionItem)
};
}
function getParamType(param, params)
{
for (var k in params)
{
if (params[k].declaration.name === param)
return params[k].declaration.type;
}
return '';
}
10 years ago
function toPlainTransactionItem(t) {
10 years ago
var r = {
contractId: t.contractId,
10 years ago
functionId: t.functionId,
10 years ago
url: t.url,
10 years ago
value: { value: t.value.value, unit: t.value.unit },
gas: { value: t.gas.value() },
10 years ago
gasPrice: { value: t.gasPrice.value, unit: t.gasPrice.unit },
10 years ago
stdContract: t.stdContract,
parameters: {}
10 years ago
};
10 years ago
for (var key in t.parameters)
{
var param = {
name: key,
value: t.parameters[key],
type: getParamType(key, t.qType)
}
r.parameters[key] = param;
}
10 years ago
return r;
10 years ago
}
Connections {
target: projectModel
onProjectClosed: {
stateListModel.clear();
stateList = [];
}
onProjectLoading: stateListModel.loadStatesFromProject(projectData);
10 years ago
onProjectSaving: {
projectData.states = []
for(var i = 0; i < stateListModel.count; i++) {
projectData.states.push(toPlainStateItem(stateList[i]));
}
projectData.defaultStateIndex = stateListModel.defaultStateIndex;
10 years ago
}
onNewProject: {
10 years ago
var state = toPlainStateItem(stateListModel.createDefaultState());
10 years ago
state.title = qsTr("Default");
projectData.states = [ state ];
projectData.defaultStateIndex = 0;
}
}
StateDialog {
id: stateDialog
onAccepted: {
var item = stateDialog.getItem();
if (stateDialog.stateIndex < stateListModel.count) {
if (stateDialog.isDefault)
stateListModel.defaultStateIndex = stateIndex;
10 years ago
stateList[stateDialog.stateIndex] = item;
stateListModel.set(stateDialog.stateIndex, item);
} else {
if (stateDialog.isDefault)
stateListModel.defaultStateIndex = 0;
10 years ago
stateList.push(item);
stateListModel.append(item);
}
if (stateDialog.isDefault)
stateListModel.defaultStateChanged();
10 years ago
stateListModel.save();
}
}
ContractLibrary {
id: contractLibrary;
}
ListModel {
id: stateListModel
property string defaultSecret: "cb73d9408c4720e230387d956eb0f829d8a4dd2c1055f96257167e14e7169074"
property int defaultStateIndex: 0
signal defaultStateChanged;
signal stateListModelReady;
signal stateRun(int index)
10 years ago
function defaultTransactionItem() {
return {
value: QEtherHelper.createEther("100", QEther.Wei),
gas: QEtherHelper.createBigInt("125000"),
10 years ago
gasPrice: QEtherHelper.createEther("10000000000000", QEther.Wei),
stdContract: false
};
}
function createDefaultState() {
var ether = QEtherHelper.createEther("1000000", QEther.Ether);
10 years ago
var item = {
title: "",
transactions: [],
accounts: []
10 years ago
};
item.accounts.push({
secret: defaultSecret,
balance: ether
});
10 years ago
//add all stdc contracts
for (var i = 0; i < contractLibrary.model.count; i++) {
var contractTransaction = defaultTransactionItem();
var contractItem = contractLibrary.model.get(i);
contractTransaction.url = contractItem.url;
contractTransaction.contractId = contractItem.name;
10 years ago
contractTransaction.functionId = contractItem.name;
contractTransaction.stdContract = true;
item.transactions.push(contractTransaction);
};
//add constructors, //TODO: order by dependencies
for(var c in codeModel.contracts) {
var ctorTr = defaultTransactionItem();
ctorTr.functionId = c;
ctorTr.contractId = c;
item.transactions.push(ctorTr);
}
10 years ago
return item;
}
function addState() {
var item = createDefaultState();
stateDialog.open(stateListModel.count, item, false);
10 years ago
}
function editState(index) {
stateDialog.open(index, stateList[index], defaultStateIndex === index);
10 years ago
}
10 years ago
function debugDefaultState() {
10 years ago
if (defaultStateIndex >= 0 && defaultStateIndex < stateList.length)
10 years ago
runState(defaultStateIndex);
}
10 years ago
function runState(index) {
var item = stateList[index];
10 years ago
clientModel.setupState(item);
stateRun(index);
10 years ago
}
function deleteState(index) {
stateListModel.remove(index);
stateList.splice(index, 1);
if (index === defaultStateIndex)
{
defaultStateIndex = 0;
defaultStateChanged();
}
10 years ago
else if (defaultStateIndex > index)
defaultStateIndex--;
10 years ago
save();
}
function save() {
projectModel.saveProject();
}
function defaultStateName()
{
return stateList[defaultStateIndex].title;
}
function loadStatesFromProject(projectData)
{
if (!projectData.states)
projectData.states = [];
if (projectData.defaultStateIndex !== undefined)
defaultStateIndex = projectData.defaultStateIndex;
else
defaultStateIndex = 0;
var items = projectData.states;
for(var i = 0; i < items.length; i++) {
var item = fromPlainStateItem(items[i]);
stateListModel.append(item);
stateList.push(item);
}
stateListModelReady();
}
10 years ago
}
}