From 2b85bbdf472797b4e5b7daf0f962d90bc00df2c2 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Sat, 21 Aug 2021 22:30:50 +0200 Subject: [PATCH] Snapping to grid! --- .../LogarithmPlotter/LogGraphCanvas.qml | 2 +- .../LogarithmPlotter/PickLocationOverlay.qml | 46 +++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml index 3cef6e1..93c94eb 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml @@ -37,7 +37,7 @@ Canvas { property string yaxisstep: "4" property string xlabel: "" property string ylabel: "" - property int maxgradx: 8 + property int maxgradx: 10 property double linewidth: 1 property double textsize: 14 property bool logscalex: false diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml index 42545cb..44465e2 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/PickLocationOverlay.qml @@ -50,12 +50,12 @@ Item { onClicked: { if(mouse.button == Qt.LeftButton) { // Validate if(parent.pickX) { - var newValue = canvas.px2x(picker.mouseX).toFixed(parent.precision) + let newValue = picked.mouseX.toString() newValue = { 'Expression': () => new MathLib.Expression(newValue), 'number': () => parseFloat(newValue) }[Objects.types[objType].properties()[propertyX]]() - var obj = Objects.getObjectByName(objName, objType) + let obj = Objects.getObjectByName(objName, objType) history.addToHistory(new HistoryLib.EditedProperty( objName, objType, propertyX, obj[propertyX], newValue )) @@ -64,12 +64,12 @@ Item { objectLists.update() } if(parent.pickY) { - var newValue = canvas.px2y(picker.mouseY).toFixed(parent.precision) + let newValue = picked.mouseY.toString() newValue = { 'Expression': () => new MathLib.Expression(newValue), 'number': () => parseFloat(newValue) }[Objects.types[objType].properties()[propertyY]]() - var obj = Objects.getObjectByName(objName, objType) + let obj = Objects.getObjectByName(objName, objType) history.addToHistory(new HistoryLib.EditedProperty( objName, objType, propertyY, obj[propertyY], newValue )) @@ -102,6 +102,12 @@ Item { text: precisionSlider.value.toFixed(0) } } + + CheckBox { + id: snapToGridCheckbox + text: "Snap to grid" + checked: false + } } Rectangle { @@ -111,7 +117,7 @@ Item { color: 'black' anchors.top: parent.top anchors.left: parent.left - anchors.leftMargin: picker.mouseX + anchors.leftMargin: canvas.x2px(picked.mouseX) visible: parent.pickX } @@ -122,15 +128,37 @@ Item { color: 'black' anchors.top: parent.top anchors.left: parent.left - anchors.topMargin: picker.mouseY + anchors.topMargin: canvas.y2px(picked.mouseY) visible: parent.pickY } Text { + id: picked x: picker.mouseX - width - 5 y: picker.mouseY - height - 5 - property double mouseX: canvas.px2x(picker.mouseX).toFixed(parent.precision) - property double mouseY: canvas.px2y(picker.mouseY).toFixed(parent.precision) + property double axisX: canvas.xaxisstep1 + property double mouseX: { + let xpos = canvas.px2x(picker.mouseX) + if(snapToGridCheckbox.checked) { + if(canvas.logscalex) { + // Calculate the logged power + let pow = Math.pow(10, Math.floor(Math.log10(xpos))) + return pow*Math.round(xpos/pow) + } else { + return canvas.xaxisstep1*Math.round(xpos/canvas.xaxisstep1) + } + } else { + return xpos.toFixed(parent.precision) + } + } + property double mouseY: { + let ypos = canvas.px2y(picker.mouseY) + if(snapToGridCheckbox.checked) { + return canvas.yaxisstep1*Math.round(ypos/canvas.yaxisstep1) + } else { + return ypos.toFixed(parent.precision) + } + } color: 'black' text: { if(parent.pickX && parent.pickY) @@ -141,4 +169,6 @@ Item { return `Y = ${mouseY}` } } + + }