-
Notifications
You must be signed in to change notification settings - Fork 130
/
deepBachMuseScore.qml
218 lines (214 loc) · 7.57 KB
/
deepBachMuseScore.qml
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import QtQuick 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.1
import MuseScore 3.0
import FileIO 3.0
MuseScore {
id: mainMuseScoreObj
menuPath: "Plugins.deepBachMuseScore"
description: qsTr("This plugin calls deepBach project.")
pluginType: "dock"
dockArea: "left"
property variant serverCalled: false
property variant loading: false
property variant linesLogged: 0
property variant serverAddress: "http://localhost:5000/"
FileIO {
id: myFile
source: "/tmp/deepbach.mxl"
onError: console.log(msg)
}
FileIO {
id: myFileXml
source: "/tmp/deepbach.xml"
onError: console.log(msg)
}
onRun: {
console.log('on run called')
if (mainMuseScoreObj.serverCalled !== serverAddress || modelSelector.model.length === 0) {
var requestModels = getRequestObj('GET', 'models')
if (call(
requestModels,
false,
function(responseText){
mainMuseScoreObj.serverCalled = serverAddress;
try {
modelSelector.model = JSON.parse(responseText)
logStatus('Models list loaded')
var requestLoadModel = getRequestObj("GET", 'current_model')
call(
requestLoadModel,
false,
function(response) {
logStatus('currently loaded model is ' + response)
for (var i in modelSelector.model) {
if (modelSelector.model[i] === response) {
console.log('set selected at ' + i)
modelSelector.currentIndex = i
}
}
}
)
} catch(error) {
console.log(error)
logStatus('No models found')
}
}
)) {
logStatus('Retrieving models list at ' + serverAddress)
}
}
}
Rectangle {
id: wrapperPanel
color: "white"
Text {
id: title
text: "DeepBach"
font.family: "Helvetica"
font.pointSize: 20
color: "black"
anchors.top: wrapperPanel.top
anchors.topMargin: 10
font.underline: false
}
Button {
id : loadModel
anchors.top: title.bottom
anchors.topMargin: 15
text: qsTr("Load")
onClicked: {
if (modelSelector.model[modelSelector.currentIndex]) {
var requestLoadModel = getRequestObj("POST", 'current_model')
if (call(
requestLoadModel,
{
model_name: modelSelector.model[modelSelector.currentIndex]
},
function(response) {
logStatus(response)
}
)) {
logStatus('Loading model ' + modelSelector.model[modelSelector.currentIndex])
}
}
}
}
ComboBox {
id: modelSelector
anchors.top: modelSelector.top
anchors.left: modelSelector.right
anchors.leftMargin: 10
model: []
width: 100
visible: false
}
Button {
id : buttonOpenFile
text: qsTr("Compose")
anchors.top: loadModel.top
anchors.topMargin: 30
anchors.bottomMargin: 10
onClicked: {
var cursor = curScore.newCursor();
cursor.rewind(1);
var startStaff = cursor.staffIdx;
var startTick = cursor.tick;
cursor.rewind(2);
var endStaff = cursor.staffIdx;
var endTick = cursor.tick;
var extension = 'mxl'
// var targetFile = tempPath() + "/my_file." + extension
myFile.remove();
logStatus(myFile.source);
var res = writeScore(mainMuseScoreObj.curScore, myFile.source, extension)
logStatus(res);
var request = getRequestObj("POST", 'compose')
if (call(
request,
{
start_staff: startStaff,
end_staff: endStaff,
start_tick: startTick,
end_tick: endTick,
// xml_string: myFile.read(),
file_path: myFile.source
},
function(response) {
if (response) {
logStatus('Done composing')
// myFileXml.write(response)
readScore(myFileXml.source)
} else {
logStatus('Got Empty Response when composing')
}
}
)) {
logStatus('Composing...')
}
}
}
Label {
id: statusLabel
wrapMode: Text.WordWrap
text: ''
color: 'grey'
font.pointSize:12
anchors.left: wrapperPanel.left
anchors.top: buttonOpenFile.top
anchors.leftMargin: 10
anchors.topMargin: 30
visible: false
}
}
function logStatus(text) {
mainMuseScoreObj.linesLogged++;
if (mainMuseScoreObj.linesLogged > 15) {
// break the textblock into an array of lines
var lines = statusLabel.text.split("\r\n");
// remove one line, starting at the first position
lines.splice(0,1);
// join the array back into a single string
statusLabel.text = lines.join("\r\n");
}
statusLabel.text += '- ' + text + "\r\n"
}
function getRequestObj(method, endpoint) {
console.debug('calling endpoint ' + endpoint)
var request = new XMLHttpRequest()
endpoint = endpoint || ''
request.open(method, serverAddress + endpoint, true)
return request
}
function call(request, params, cb) {
if (mainMuseScoreObj.loading) {
logStatus('refusing to call server')
return false
}
request.onreadystatechange = function() {
if (request.readyState == XMLHttpRequest.DONE) {
mainMuseScoreObj.loading = false;
cb(request.responseText);
}
}
if (params) {
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
var pairs = [];
for (var prop in params) {
if (params.hasOwnProperty(prop)) {
var k = encodeURIComponent(prop),
v = encodeURIComponent(params[prop]);
pairs.push( k + "=" + v);
}
}
const content = pairs.join('&')
console.debug('params ' + content)
mainMuseScoreObj.loading = true;
request.send(content)
} else {
mainMuseScoreObj.loading = true;
request.send()
}
return true
}
}