//qmlTableEditor - helps to graphically create and change Csound Gen7 (straigt lines) type of tables // (c) Tarmo Johannes 2015 tarmo@otsakool.edu.ee //Licence: GPL 2 import QtQuick 2.1 import QtQuick.Controls 1.1 import QtQuick.Window 2.0 import QtQuick.Dialogs 1.1 Rectangle { id: mainWindow width: 720 height: 550 property var points: []; // array of endpoints of the segments property int pointWidth: 10; // set constant property real currentIndex: 0 property real currentValue: 0 Item {id: mainArea; anchors.fill: parent} signal newSyntax(string syntax) function insertPoint(x,y) { // finds right place in the array (sorted by x), insert into array and creates the object var index = -1; for (var i=1;i=points[i-1].x && x<=points[i].x ) { index = i; break; } } //console.log("New index: ",index); if (index!=-1) { 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 canvas.requestPaint(); } } function getMinimumX(index) { // to not let a point pass its neighbour var minX = -1 if (index==0) minX = 0; else if (index===points.length-1) minX = drawRect.width-pointWidth/2; else if (index>0) minX = points[index-1].x; //console.log("point no, minx ",index,minX); return minX; } function getMaximumX(index) { // to not let a point pass its neighbour var maxX = -1 if (index==0) maxX = 0; else if (index===points.length-1) maxX = drawRect.width-pointWidth/2; else if (index>0) maxX = points[index+1].x; //console.log("point no, minx ",index,maxX); return maxX; } function x2index(x) { // converts the x-coordinate to index of the table var tabIndex = (x-pointWidth/2) / (drawRect.width-pointWidth/2); // * tableSizeSpinbox.value) ; // store as 0..1 //console.log("x, index: ",x,tabIndex); return tabIndex; } function y2value(y) { // converts the y-coordinate to 0..1 //first 0..maxvalue var value = (1 - (y) / (drawRect.height)) //* maxSpinbox.value ; // store as 0..1 return value; } function scaleValue(value) { // converts value 0..1 into 0..max or -max..max if bipolar graph return (bipolar.checked) ? 2*value*maxSpinbox.value - maxSpinbox.value: value*maxSpinbox.value; } function graph2syntax() { // returns table syntax as string according to defined points // get first parts from already existing text in Textarea (since may be changed by hand) var name = syntaxField.text.split("ftgen")[0]; // table name before "ftgen" var parameters = syntaxField.text.split("ftgen")[1].replace("ftgen ","").trim().split(","); //console.log("name,parameters:",name, parameters) var syntax = name + "ftgen " + parameters[0] + "," + parameters[1] + "," + + tableSizeSpinbox.value.toString()+", 7, "; var checksum = 0; for (var i=0;i=2) { sectionLength = Math.round(sectionLength); // otherwise round to integer values; } checksum += sectionLength; //console.log("value, sectionLength",value, sectionLength) syntax += value.toFixed(6) + ", "+ sectionLength.toString() + ", "; // TODO function scaleValue () - returns stored value in 0..1 into 0..max or -max..max } syntax += (scaleValue( points[points.length-1].value)).toFixed(6); // add the last value //console.log("Last point index, value: ",points[points.length-1].value, points[points.length-1].index) //console.log("Checksum: ", checksum); //console.log("New table definition: ", syntax); syntaxField.text = syntax; //mainWindow.newSyntax(syntax); return syntax; } function syntax2graph(syntax, maxValue) { // if second argument not given, find max from data var parameters = syntax.split("ftgen")[1].replace("ftgen ","").trim().split(","); // parameters after ftgen to array var size = parseInt(parameters[2]); //var segmentScale = (size + 0.0)/ (tableSizeSpinbox.value + 0.0) tableSizeSpinbox.value = size; // separate values and sizes of segments into arrays var values = []; var segments = []; //var max = (typeof maxValue === 'undefined') ? 0 : maxValue; // 0 signals, that find max from data, otherwise use given value var containsNegative = false; var max = 0; for (var i=4;i max) max = Math.abs(val); if (i0) { var point = points.pop(); point.destroy(); } var sum = 0, index=0; for (var j=0;j0) { var point = points.pop(); point.destroy(); } points[0] = pointComponent.createObject(drawRect, { "x" : 0, "y":0, "index":0, "value":0.5 }); points[1] = pointComponent.createObject(drawRect, { "x" :0, "y":0, "index":1,"value":1}); maxSpinbox.value = 1; tableSizeSpinbox.value = 1024; bipolar.checked = true; syntaxField.text = "giTable ftgen 0,0,1024, 7, 0.000000, 1024, 1.000000"; drawRect.updatePointPositions(); } Component { id: pointComponent // create points on the grpah dynamically Rectangle { id:pointRect width: pointWidth; height: width color: (pointArea.containsMouse || pointArea.drag.active )? "blue" : "red" radius: width/2 property real value: 0 // TODO: keep as 0..1 and the scale to -max..max property real index:0 //TODO: keep as 0..1 and then scale to int(0..tableSize) MouseArea { id: pointArea anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton hoverEnabled: true drag.target: parent drag.minimumX: -width/2 //TODO : make it fit with getMaximumX drag.maximumX: drawRect.width - width/2 drag.minimumY: -height/2 drag.maximumY: drawRect.height - height/2 onPressed: if (pressedButtons & Qt.RightButton) { var index = points.indexOf(parent); //console.log("Deleting point with index: ", index) points.splice(index,1) // remove from array parent.destroy() // remove pointRect valueRect.visible = false; // hide valueRect canvas.requestPaint(); } drag.onActiveChanged: { //to detect dragEnd and dragFihised - not fired if not in automiatic mode valueRect.visible = drag.active // show the valuebox on drag if (!drag.active) { // on dragEnd update index of the pointArea parent.index = x2index(parent.x+pointWidth/2) parent.value = y2value(parent.y+pointWidth/2) graph2syntax() // update syntax } } onHoveredChanged: { currentValue = y2value(parent.y + pointWidth/2); currentIndex = x2index(parent.x + pointWidth/2); valueRect.x = parent.x-valueRect.width/2; valueRect.y = parent.y-valueRect.height-10; valueRect.y = Math.max(-drawRect.y +2, valueRect.y) // not to show outside of window valueRect.visible = containsMouse || drag.active; // show the valuebox while hovered } } onXChanged: { var index = points.indexOf(this); //console.log("MOVING: ",index); if (index!=-1) pointArea.drag.minimumX = getMinimumX(index); if (index!=-1) pointArea.drag.maximumX = getMaximumX(index); canvas.requestPaint(); currentIndex = x2index(this.x+pointWidth/2); // pointWidth/2 since the center of the point marker is the desired point valueRect.x = x-valueRect.width/2 } onYChanged: { canvas.requestPaint(); currentValue = y2value(y+pointWidth/2); valueRect.y = y-valueRect.height-10; valueRect.y = Math.max(-drawRect.y +2, valueRect.y) // not to show outside of window } } } Rectangle { width: parent.width*0.75; height: parent.height*0.6 color: "#ffffff" anchors.horizontalCenter: parent.horizontalCenter y: valueRect.height + 5 id: drawRect z: 1 function updatePointPositions() { //points[0].x = 0; //points[0].y= drawRect.height/2-pointWidth/2; //points[points.length-1].x= drawRect.width-pointWidth/2; //points[points.length-1].y= 0-pointWidth/2; // update all points: - lisa value ja index iga punkti juurde for (var i=0;i=2) ? (Math.round(currentIndex*tableSizeSpinbox.value)).toString() : currentIndex.toFixed(6) ) // if table lenght set to 1, so also decimal values, don't scale } } } //TODO: set firt element 0 MouseArea { anchors.fill: parent onDoubleClicked: { //console.log(mouse.x,mouse.y); insertPoint(mouse.x,mouse.y); graph2syntax() } } Canvas { id:canvas //width:155; height: 118 anchors.fill: parent onPaint:{ var context = canvas.getContext('2d'); var offset = points[0].width / 2 ; // to move to the centre of circle context.beginPath(); context.clearRect(0, 0, width, height); context.fill(); //axis and numbers context.beginPath(); context.lindeWidth = 4; context.moveTo(pointWidth/2,0) ; context.strokeStyle = "black"; context.lineTo(pointWidth/2,drawRect.height); //context.stroke(); var y0 = (bipolar.checked) ? drawRect.height/2 : drawRect.height-context.lindeWidth/2 //y0 -= 10; context.moveTo(0,y0); context.lineTo(drawRect.width,y0); context.stroke(); context.font ="12px sans-serif"; context.strokeText(maxSpinbox.value.toString() ,10,10); context.strokeText("0",10,y0-5); if (bipolar.checked) context.strokeText(-maxSpinbox.value.toString() ,10,drawRect.height-5); // lines between points context.beginPath(); context.lineWidth = 1; //console.log("points.length: ", points.length) for (var i=1;i