From d4e97f28609a558a4fdc886ae70e8dddf2bd1536 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Tue, 17 Sep 2024 02:14:47 +0200 Subject: [PATCH] Moving last methods from LogGraphCanvas to Canvas JS module. --- .../LogarithmPlotter/LogGraphCanvas.qml | 41 ------------------- .../eu/ad5001/LogarithmPlotter/Settings.qml | 8 ++-- .../eu/ad5001/LogarithmPlotter/js/canvas.mjs | 15 +++++-- 3 files changed, 15 insertions(+), 49 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml index 8d4b2f2..c2d9f60 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/LogGraphCanvas.qml @@ -168,45 +168,4 @@ Canvas { } }) } - - /*! - \qmlmethod double LogGraphCanvas::x2px(double x) - Converts an \c x coordinate to it's relative position on the canvas. - It supports both logarithmic and non logarithmic scale depending on the currently selected mode. - */ - function x2px(x) { - if(logscalex) { - var logxmin = Math.log(xmin) - return (Math.log(x)-logxmin)*xzoom - } else return (x - xmin)*xzoom - } - - /*! - \qmlmethod double LogGraphCanvas::y2px(double y) - Converts an \c y coordinate to it's relative position on the canvas. - The y axis not supporting logarithmic scale, it only support linear convertion. - */ - function y2px(y) { - return (ymax-y)*yzoom - } - - /*! - \qmlmethod double LogGraphCanvas::px2x(double px) - Converts an x \c px position on the canvas to it's corresponding coordinate on the plot. - It supports both logarithmic and non logarithmic scale depending on the currently selected mode. - */ - function px2x(px) { - if(logscalex) { - return Math.exp(px/xzoom+Math.log(xmin)) - } else return (px/xzoom+xmin) - } - - /*! - \qmlmethod double LogGraphCanvas::px2x(double px) - Converts an x \c px position on the canvas to it's corresponding coordinate on the plot. - It supports both logarithmic and non logarithmic scale depending on the currently selected mode. - */ - function px2y(px) { - return -(px/yzoom-ymax) - } } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml index 067c70d..c94bef1 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Settings.qml @@ -227,10 +227,10 @@ ScrollView { label: qsTr("Max X") icon: "settings/xmax.svg" width: settings.settingWidth - defValue: canvas.px2x(canvas.width).toFixed(2) + defValue: Modules.Canvas.px2x(canvas.width).toFixed(2) onChanged: function(xvaluemax) { if(xvaluemax > settings.xmin) { - settings.xzoom = settings.xzoom * canvas.width/(canvas.x2px(xvaluemax)) // Adjusting zoom to fit. = (end)/(px of current point) + settings.xzoom = settings.xzoom * canvas.width/(Modules.Canvas.x2px(xvaluemax)) // Adjusting zoom to fit. = (end)/(px of current point) settings.changed() } else { alert.show("Maximum x value must be superior to minimum.") @@ -246,10 +246,10 @@ ScrollView { label: qsTr("Min Y") icon: "settings/ymin.svg" width: settings.settingWidth - defValue: canvas.px2y(canvas.height).toFixed(2) + defValue: Modules.Canvas.px2y(canvas.height).toFixed(2) onChanged: function(yvaluemin) { if(yvaluemin < settings.ymax) { - settings.yzoom = settings.yzoom * canvas.height/(canvas.y2px(yvaluemin)) // Adjusting zoom to fit. = (end)/(px of current point) + settings.yzoom = settings.yzoom * canvas.height/(Modules.Canvas.y2px(yvaluemin)) // Adjusting zoom to fit. = (end)/(px of current point) settings.changed() } else { alert.show("Minimum y value must be inferior to maximum.") diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/canvas.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/canvas.mjs index 2a03677..1e85788 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/canvas.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/canvas.mjs @@ -388,7 +388,11 @@ class CanvasAPI extends Module { * @returns {number} */ x2px(x) { - return this._canvas.x2px(x) + if(this.logscalex) { + const logxmin = Math.log(this.xmin) + return (Math.log(x)-logxmin)*this.xzoom + } else + return (x - this.xmin)*this.xzoom } /** @@ -398,7 +402,7 @@ class CanvasAPI extends Module { * @returns {number} */ y2px(y) { - return this._canvas.y2px(y) + return (this.ymax - y) * this.yzoom } /** @@ -408,7 +412,10 @@ class CanvasAPI extends Module { * @returns {number} */ px2x(px) { - return this._canvas.px2x(px) + if(this.logscalex) { + return Math.exp(px/this.xzoom+Math.log(this.xmin)) + } else + return (px/this.xzoom+this.xmin) } /** @@ -418,7 +425,7 @@ class CanvasAPI extends Module { * @returns {number} */ px2y(px) { - return this._canvas.px2y(px) + return -(px/this.yzoom-this.ymax) } /**