-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQmlExporter.js
117 lines (85 loc) · 3.28 KB
/
QmlExporter.js
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
function typeName(obj) {
return obj.toString().split("(")[0].split("_")[0];
}
function save(url, stateMachineItem) {
console.log("save qml to " + url);
var textList = ["import FFaniStateMachine 1.0"];
textList = textList.concat(writeState(stateMachineItem));
var text = textList.join("\n");
//console.log(text);
fileIo.write(url, text);
}
function getTransitionList(fromState) {
var result = [];
for (var i = 0; i < mainView.transitionLayer.children.length; i++) {
var transition = mainView.transitionLayer.children[i];
if (transition.from === fromState) {
result.push(transition);
}
}
return result;
}
function writeState(stateItem, indent) {
var indent = indent || 0;
var type = stateItem.type;
console.log("writeState: " + type);
var indentString = Array(indent * 4).join(" ");
var result = [indentString + type + " {"];
var propertyIndentString = Array((indent + 1) * 4).join(" ");
var properties = [];
properties.push(propertyIndentString + "id: " + stateItem.label);
properties.push(propertyIndentString + "objectName: \"" + stateItem.label + "\"");
if (stateItem.content.children.length > 0) {
properties.push(propertyIndentString + "initialState: " + stateItem.content.children[0].label);
}
result = result.concat(properties);
// add signal when StateMachine
if (type === "StateMachine") {
var signals = [];
for (var i = 0; i < mainView.signals.count; i++) {
signals.push(propertyIndentString + 'signal ' + mainView.signals.get(i).name);
}
result = result.concat(signals);
}
//write states
for (var i = 0; i < stateItem.content.children.length; i++) {
var childStateItem = stateItem.content.children[i];
var childText = writeState(childStateItem, indent + 1);
result = result.concat(childText);
}
//write transitions
var transitionList = getTransitionList(stateItem);
if (transitionList.length > 0) {
result = result.concat(writeTransitionList(transitionList, indent + 1));
}
result.push(indentString + "}");
return result;
}
function writeTransitionList(transitionList, indent) {
var result = [];
for (var i = 0; i < transitionList.length; i++) {
var transition = transitionList[i];
result = result.concat(writeTransition(transition, indent));
}
return result;
}
function writeTransition(transition, indent) {
var type = transition.type;
var indentString = Array(indent * 4).join(" ");
var result = [indentString + type + " {"];
var properties = [];
var propertyIndentString = Array((indent + 1) * 4).join(" ");
if (transition.objectName) {
properties.push(propertyIndentString + "id: " + transition.objectName);
properties.push(propertyIndentString + "objectName: \"" + transition.objectName + "\"");
}
if (transition.to && transition.to.label) {
properties.push(propertyIndentString + "targetState: " + transition.to.label);
}
if (transition.signalModel) {
properties.push(propertyIndentString + "signalName: \"" + transition.signalModel.name + "\"");
}
result = result.concat(properties);
result.push(indentString + "}");
return result;
}