Debian Codegraph @

src/QML/TableEditor.qml

raw
1 //qmlTableEditor - helps to graphically create and change Csound Gen7 (straigt lines) type of tables
2 // (c) Tarmo Johannes 2015 [email protected]
3 //Licence: GPL 2
4
5 import QtQuick 2.1
6 import QtQuick.Controls 1.1
7 import QtQuick.Window 2.0
8 import QtQuick.Dialogs 1.1
9
10 Rectangle {
11 id: mainWindow
12 width: 720
13 height: 550
14 property var points: []; // array of endpoints of the segments
15 property int pointWidth: 10; // set constant
16 property real currentIndex: 0
17 property real currentValue: 0
18
19 Item {id: mainArea; anchors.fill: parent}
20
21 signal newSyntax(string syntax)
22
23 function insertPoint(x,y) { // finds right place in the array (sorted by x), insert into array and creates the object
24 var index = -1;
25 for (var i=1;i<points.length;i++) { // find right index
26 if (x>=points[i-1].x && x<=points[i].x ) {
27 index = i;
28 break;
29 }
30
31 }
32 //console.log("New index: ",index);
33 if (index!=-1) {
34 points.splice(index,0, pointComponent.createObject(drawRect, { "x" : x, "y": y , "index": x2index(x+pointWidth/2), "value" : y2value(y+pointWidth/2) })); // create new point objet
35 canvas.requestPaint();
36 }
37
38 }
39
40 function getMinimumX(index) { // to not let a point pass its neighbour
41 var minX = -1
42 if (index==0)
43 minX = 0;
44 else if (index===points.length-1)
45 minX = drawRect.width-pointWidth/2;
46 else if (index>0)
47 minX = points[index-1].x;
48 //console.log("point no, minx ",index,minX);
49 return minX;
50 }
51
52 function getMaximumX(index) { // to not let a point pass its neighbour
53 var maxX = -1
54 if (index==0)
55 maxX = 0;
56 else if (index===points.length-1)
57 maxX = drawRect.width-pointWidth/2;
58 else if (index>0)
59 maxX = points[index+1].x;
60 //console.log("point no, minx ",index,maxX);
61 return maxX;
62 }
63
64 function x2index(x) { // converts the x-coordinate to index of the table
65 var tabIndex = (x-pointWidth/2) / (drawRect.width-pointWidth/2); // * tableSizeSpinbox.value) ; // store as 0..1
66 //console.log("x, index: ",x,tabIndex);
67 return tabIndex;
68
69
70 }
71
72 function y2value(y) { // converts the y-coordinate to 0..1
73 //first 0..maxvalue
74 var value = (1 - (y) / (drawRect.height)) //* maxSpinbox.value ; // store as 0..1
75 return value;
76 }
77
78 function scaleValue(value) { // converts value 0..1 into 0..max or -max..max if bipolar graph
79 return (bipolar.checked) ? 2*value*maxSpinbox.value - maxSpinbox.value: value*maxSpinbox.value;
80 }
81
82 function graph2syntax() { // returns table syntax as string according to defined points
83 // get first parts from already existing text in Textarea (since may be changed by hand)
84 var name = syntaxField.text.split("ftgen")[0]; // table name before "ftgen"
85 var parameters = syntaxField.text.split("ftgen")[1].replace("ftgen ","").trim().split(",");
86 //console.log("name,parameters:",name, parameters)
87
88 var syntax = name + "ftgen " + parameters[0]
89 + "," + parameters[1] + "," + + tableSizeSpinbox.value.toString()+", 7, ";
90
91 var checksum = 0;
92 for (var i=0;i<points.length-1;i++) {
93
94 var value = scaleValue(points[i].value);
95 var sectionLength = (points[i+1].index - points[i].index)*tableSizeSpinbox.value; // if the tableLength is set to 1, leave decimal values, don't round
96 if (tableSizeSpinbox.value>=2) {
97 sectionLength = Math.round(sectionLength); // otherwise round to integer values;
98 }
99
100 checksum += sectionLength;
101 //console.log("value, sectionLength",value, sectionLength)
102 syntax += value.toFixed(6) + ", "+ sectionLength.toString() + ", "; // TODO function scaleValue () - returns stored value in 0..1 into 0..max or -max..max
103 }
104 syntax += (scaleValue( points[points.length-1].value)).toFixed(6); // add the last value
105 //console.log("Last point index, value: ",points[points.length-1].value, points[points.length-1].index)
106 //console.log("Checksum: ", checksum);
107 //console.log("New table definition: ", syntax);
108 syntaxField.text = syntax;
109 //mainWindow.newSyntax(syntax);
110 return syntax;
111 }
112
113 function syntax2graph(syntax, maxValue) { // if second argument not given, find max from data
114 var parameters = syntax.split("ftgen")[1].replace("ftgen ","").trim().split(","); // parameters after ftgen to array
115 var size = parseInt(parameters[2]);
116 //var segmentScale = (size + 0.0)/ (tableSizeSpinbox.value + 0.0)
117 tableSizeSpinbox.value = size;
118
119 // separate values and sizes of segments into arrays
120 var values = [];
121 var segments = [];
122 //var max = (typeof maxValue === 'undefined') ? 0 : maxValue; // 0 signals, that find max from data, otherwise use given value
123 var containsNegative = false;
124 var max = 0;
125 for (var i=4;i<parameters.length;i++) {
126 //console.log("i, value",i,parameters[i])
127 var val = parseFloat(parameters[i]);
128 if (val<0) {
129 containsNegative = true ; // to be able to scale correctly later
130 bipolar.checked = true;
131 }
132 values[values.length] = val;
133 if (Math.abs(val) > max)
134 max = Math.abs(val);
135 if (i<parameters.length-1) { // don't find it after last value
136 segments[segments.length] = parseInt(parameters[i+1]);
137 ++i;
138 }
139 }
140
141 maxValue = Math.max(max, maxValue)
142 //console.log("MAX: ",maxValue);
143 for (var i=0; i<values.length;i++) {
144 values[i] /= maxValue; // to scale 0..1
145 //console.log("SCALED VALUE , i, value", i, values[i] )
146 }
147
148
149 if (bipolar.checked )
150 for (var k=0; k<values.length;k++) {
151 values[k] = values[k]/2.0+0.5; // to scale 0..1
152 //console.log("NEGATIVE , i, value", i, values[k] )
153 }
154
155
156 //console.log("Values: ",values, "segments: ", segments );
157 maxSpinbox.value = maxValue;
158
159 // create points
160 // first delete old ones and clear array:
161 while (points.length>0) {
162 var point = points.pop();
163 point.destroy();
164 }
165 var sum = 0, index=0;
166 for (var j=0;j<values.length;j++) { // set only values and indexes, later update point positions and redraw
167 if (j==values.length-1)
168 index = 1
169 else
170 index = (sum+0.0) / (size+0.0);
171 //console.log("j,index, sum, value: ",j,index, sum ,values[j])
172 if (j<values.length-1) // the array of segments is shorter that the one with values
173 sum += segments[j];
174
175 points[j] = pointComponent.createObject(drawRect, { "x" : 0, "y":0, "index":index, "value":values[j] }); // value = 0.5 since bipolar view in the begining
176
177 }
178 drawRect.updatePointPositions();
179 //canvas.requestPaint()
180
181 }
182
183 function clear() {
184 while (points.length>0) {
185 var point = points.pop();
186 point.destroy();
187 }
188 points[0] = pointComponent.createObject(drawRect, { "x" : 0, "y":0, "index":0, "value":0.5 });
189 points[1] = pointComponent.createObject(drawRect, { "x" :0, "y":0, "index":1,"value":1});
190 maxSpinbox.value = 1;
191 tableSizeSpinbox.value = 1024;
192 bipolar.checked = true;
193 syntaxField.text = "giTable ftgen 0,0,1024, 7, 0.000000, 1024, 1.000000";
194 drawRect.updatePointPositions();
195
196
197 }
198
199
200
201
202
203
204 Component {
205 id: pointComponent // create points on the grpah dynamically
206
207 Rectangle {
208 id:pointRect
209 width: pointWidth; height: width
210 color: (pointArea.containsMouse || pointArea.drag.active )? "blue" : "red"
211 radius: width/2
212 property real value: 0 // TODO: keep as 0..1 and the scale to -max..max
213 property real index:0 //TODO: keep as 0..1 and then scale to int(0..tableSize)
214
215
216 MouseArea {
217 id: pointArea
218 anchors.fill: parent
219 acceptedButtons: Qt.LeftButton | Qt.RightButton
220 hoverEnabled: true
221 drag.target: parent
222
223 drag.minimumX: -width/2 //TODO : make it fit with getMaximumX
224 drag.maximumX: drawRect.width - width/2
225 drag.minimumY: -height/2
226 drag.maximumY: drawRect.height - height/2
227
228 onPressed:
229 if (pressedButtons & Qt.RightButton) {
230 var index = points.indexOf(parent);
231 //console.log("Deleting point with index: ", index)
232
233 points.splice(index,1) // remove from array
234 parent.destroy() // remove pointRect
235 valueRect.visible = false; // hide valueRect
236 canvas.requestPaint();
237
238 }
239
240 drag.onActiveChanged: { //to detect dragEnd and dragFihised - not fired if not in automiatic mode
241 valueRect.visible = drag.active // show the valuebox on drag
242 if (!drag.active) { // on dragEnd update index of the pointArea
243 parent.index = x2index(parent.x+pointWidth/2)
244 parent.value = y2value(parent.y+pointWidth/2)
245 graph2syntax() // update syntax
246 }
247 }
248
249
250
251 onHoveredChanged: {
252 currentValue = y2value(parent.y + pointWidth/2);
253 currentIndex = x2index(parent.x + pointWidth/2);
254 valueRect.x = parent.x-valueRect.width/2;
255 valueRect.y = parent.y-valueRect.height-10;
256 valueRect.y = Math.max(-drawRect.y +2, valueRect.y) // not to show outside of window
257 valueRect.visible = containsMouse || drag.active; // show the valuebox while hovered
258 }
259
260
261 }
262
263
264
265 onXChanged: {
266 var index = points.indexOf(this);
267 //console.log("MOVING: ",index);
268 if (index!=-1)
269 pointArea.drag.minimumX = getMinimumX(index);
270 if (index!=-1)
271 pointArea.drag.maximumX = getMaximumX(index);
272 canvas.requestPaint();
273
274 currentIndex = x2index(this.x+pointWidth/2); // pointWidth/2 since the center of the point marker is the desired point
275 valueRect.x = x-valueRect.width/2
276 }
277 onYChanged: {
278 canvas.requestPaint();
279 currentValue = y2value(y+pointWidth/2);
280 valueRect.y = y-valueRect.height-10;
281 valueRect.y = Math.max(-drawRect.y +2, valueRect.y) // not to show outside of window
282
283 }
284 }
285
286
287 }
288
289
290
291
292
293 Rectangle {
294 width: parent.width*0.75; height: parent.height*0.6
295 color: "#ffffff"
296 anchors.horizontalCenter: parent.horizontalCenter
297 y: valueRect.height + 5
298 id: drawRect
299 z: 1
300 function updatePointPositions() {
301
302 //points[0].x = 0;
303 //points[0].y= drawRect.height/2-pointWidth/2;
304 //points[points.length-1].x= drawRect.width-pointWidth/2;
305 //points[points.length-1].y= 0-pointWidth/2;
306
307 // update all points: - lisa value ja index iga punkti juurde
308 for (var i=0;i<points.length;i++) {
309
310 points[i].x = points[i].index * (drawRect.width-pointWidth/2);
311 var helper = 1-(points[i].value+maxSpinbox.value)/2; // to 0..1 inversed
312 //onsole.log("HElper 0..1",helper);
313 points[i].y= (1-points[i].value)*drawRect.height-pointWidth/2;
314 //console.log("point, inedx,value,newX, newY",i,points[i].index,points[i].value, points[i].x, points[i].y);
315
316
317 }
318
319 canvas.requestPaint();
320
321 }
322
323 Component.onCompleted: {
324 points[0] = pointComponent.createObject(drawRect, { "x" : 0, "y":mainWindow.height*0.6*0.5-pointWidth/2, "index":0, "value":0.5 }); // value = 0.5 since bipolar view in the begining
325 points[1] = pointComponent.createObject(drawRect, { "x" :mainWindow.width*0.75-pointWidth/2, "y":0-pointWidth/2, "index":1,"value":1}); // cannot use this.heigth since probably not created yet...
326 canvas.requestPaint();
327 }
328
329 onWidthChanged: updatePointPositions();
330 onHeightChanged:updatePointPositions();
331
332 Rectangle { // displays
333 id:valueRect
334 //y: (y<-drawRect.y + 2) ? -drawRect.y + 2 : y // don't let to go up from upper window border
335 width: 150
336 height: 50
337 // TODO: how to rise above labels and other objects
338 z:1
339 opacity: 0.8
340 color: "#fbfcba"
341 radius: 4
342 visible: false
343 border.width: 1
344 border.color: "black"
345
346
347 Column {
348 anchors.horizontalCenter: parent.horizontalCenter
349 anchors.verticalCenter: parent.verticalCenter
350 spacing: 4
351 Label {text: qsTr("Value: ")+(scaleValue(currentValue)).toFixed(3)}
352 Label {
353 text: qsTr("Index: ")+ ( (tableSizeSpinbox.value>=2) ? (Math.round(currentIndex*tableSizeSpinbox.value)).toString() : currentIndex.toFixed(6) ) // if table lenght set to 1, so also decimal values, don't scale
354 }
355 }
356
357 }
358
359 //TODO: set firt element 0
360 MouseArea {
361 anchors.fill: parent
362 onDoubleClicked: {
363 //console.log(mouse.x,mouse.y);
364 insertPoint(mouse.x,mouse.y);
365 graph2syntax()
366 }
367 }
368
369
370
371 Canvas {
372 id:canvas
373
374
375 //width:155; height: 118
376 anchors.fill: parent
377
378 onPaint:{
379 var context = canvas.getContext('2d');
380 var offset = points[0].width / 2 ; // to move to the centre of circle
381
382 context.beginPath();
383 context.clearRect(0, 0, width, height);
384 context.fill();
385
386 //axis and numbers
387 context.beginPath();
388 context.lindeWidth = 4;
389 context.moveTo(pointWidth/2,0) ;
390 context.strokeStyle = "black";
391 context.lineTo(pointWidth/2,drawRect.height);
392 //context.stroke();
393 var y0 = (bipolar.checked) ? drawRect.height/2 : drawRect.height-context.lindeWidth/2
394 //y0 -= 10;
395 context.moveTo(0,y0);
396 context.lineTo(drawRect.width,y0);
397 context.stroke();
398 context.font ="12px sans-serif";
399 context.strokeText(maxSpinbox.value.toString() ,10,10);
400 context.strokeText("0",10,y0-5);
401 if (bipolar.checked)
402 context.strokeText(-maxSpinbox.value.toString() ,10,drawRect.height-5);
403
404
405
406 // lines between points
407 context.beginPath();
408 context.lineWidth = 1;
409
410 //console.log("points.length: ", points.length)
411 for (var i=1;i<points.length;i++) {
412 context.moveTo(points[i-1].x + offset, points[i-1].y+ offset) ;
413 context.strokeStyle = "red"
414 context.lineTo(points[i].x + offset, points[i].y + offset);
415 context.stroke();
416 //console.log("i-1, x, y:",i-1,points[i-1].x,points[i-1].y);
417 //console.log("i, x, y:",i,points[i].x,points[i].y);
418 }
419
420 }
421 }
422
423
424
425
426 }
427
428 Label { // TODO: leia koht, võibolla messagedialog
429 visible: false
430 id: tipLAbel
431 x: 271
432 y: 74
433 text: qsTr("Double-click to add, right-click to remove point")
434 anchors.bottom: drawRect.top
435 anchors.bottomMargin: 6
436 anchors.horizontalCenter: parent.horizontalCenter
437 horizontalAlignment: Text.AlignHCenter
438 }
439
440 Label {
441 id: maxLabel
442 anchors.left: maxSpinbox.left
443 anchors.bottom: maxSpinbox.top
444 anchors.bottomMargin: 5
445 text: qsTr("Max value:")
446 }
447
448 SpinBox {
449 id: maxSpinbox
450 horizontalAlignment: 1
451 anchors.left: parent.left
452 anchors.leftMargin: 6
453 stepSize: 0.01
454 anchors.right: drawRect.left
455 anchors.rightMargin: 6
456 anchors.top: drawRect.top
457 anchors.topMargin: 0
458 value: 1
459 maximumValue: 9999999
460 decimals: 2
461 onEditingFinished: {canvas.requestPaint(); graph2syntax() }// to display new max number
462 }
463
464 CheckBox {
465 text: qsTr("Bipolar:")
466 id: bipolar
467 checked: true
468 anchors.left: maxSpinbox.left
469 anchors.top: maxSpinbox.bottom
470 anchors.topMargin: 5
471 onCheckedChanged: { canvas.requestPaint() ; graph2syntax()}
472
473
474 }
475
476
477 Button {
478 id: graph2syntaxButton
479 text: qsTr("&Insert to CsoundQt")
480 anchors.left: drawRect.left
481 anchors.top: syntaxRadioButtons.bottom
482 anchors.topMargin: 6
483
484 onClicked: {
485 mainWindow.newSyntax(graph2syntax()); // send signal to host
486 }
487 }
488
489
490 Button {
491 id: syntax2graphButton
492 text: qsTr("&Update graph")
493 anchors.left: drawRect.left
494 anchors.top: graph2syntaxButton.bottom
495 anchors.topMargin: 6
496
497
498 onClicked: syntax2graph(syntaxField.text, maxSpinbox.value)
499 }
500
501
502
503
504 TextArea {
505 id: syntaxField
506 objectName: "syntaxField" // to reach it fro C++
507 anchors.left: graph2syntaxButton.right
508 anchors.leftMargin: 6
509 anchors.right: drawRect.right
510 anchors.top: graph2syntaxButton.top
511 anchors.bottom: mainArea.bottom
512 anchors.bottomMargin: 10 // has no influence in some reason
513 //height: mainWindow.height * 0.15
514 readOnly: false
515 //wrapMode: TextInput.WordWrap
516 text: "giTable ftgen 0,0,1024, 7, 0.000000, 1024, 1.000000" // corresponds to the default position of points
517 Keys.onReturnPressed: syntax2graph(syntaxField.text, maxSpinbox.value)
518
519 }
520
521
522
523
524
525
526
527 SpinBox {
528 id: tableSizeSpinbox
529 y: 362
530 value: 1024
531 minimumValue: 1
532 maximumValue: 99999
533 anchors.left: drawRect.right
534 anchors.leftMargin: 6
535 anchors.bottom: drawRect.bottom
536 anchors.bottomMargin: 0
537 onEditingFinished: {canvas.requestPaint(); graph2syntax() }// to display new max number
538
539
540 }
541
542 Label {
543 id: tableSizeLabel
544 x: 583
545 y: 334
546 height: 22
547 text: qsTr("Table size")
548 anchors.horizontalCenterOffset: 0
549 anchors.bottom: tableSizeSpinbox.top
550 anchors.bottomMargin: 6
551 horizontalAlignment: Text.AlignHCenter
552 anchors.horizontalCenter: tableSizeSpinbox.horizontalCenter
553 }
554
555
556 Row {
557 id: syntaxRadioButtons
558 ExclusiveGroup { id: syntaxTypeGroup }
559
560 width: drawRect.width
561 //height: 40
562 anchors.left: drawRect.left
563 anchors.leftMargin: 0
564 anchors.top: drawRect.bottom
565 anchors.topMargin: 6
566 spacing: 5
567
568 Label {text: qsTr("Syntax type: ")}
569
570
571 RadioButton {
572 id: ftgenButton
573 text: qsTr("ftgen")
574 checked: true
575 exclusiveGroup: syntaxTypeGroup
576 }
577
578 RadioButton {
579 id: fbutton
580 text: qsTr("f statement")
581 enabled: false
582 exclusiveGroup: syntaxTypeGroup
583 }
584
585
586
587 }
588
589 Button {
590 id: clearButton
591 anchors.left: drawRect.left
592 anchors.top: syntax2graphButton.bottom
593 anchors.topMargin: 6
594 text: qsTr("&Clear")
595 onClicked:clear()
596 }
597
598 Button {
599 id: helpButton
600 anchors.right: mainArea.right
601 anchors.rightMargin: 10
602 anchors.top: mainArea.top
603 anchors.topMargin: 10
604 text: qsTr("&Help")
605 onClicked:helpDialog.visible = true;
606 }
607
608 MessageDialog {
609 id: helpDialog
610 title: qsTr("Help")
611 visible: false
612 text: qsTr("Double-click to add a new point.\nDrag to move, right-click to remove\nYou can edit the table definition in textarea. \nThe changes in definition are displayed when you press ENTER or click on button Update Graph\n");
613 onAccepted: visible=false;
614 }
615
616 }