2021-08-14 16:57:29 +00:00
|
|
|
/**
|
2022-03-05 16:49:35 +00:00
|
|
|
* LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions.
|
2024-01-10 23:11:09 +00:00
|
|
|
* Copyright (C) 2021-2024 Ad5001
|
2021-08-14 16:57:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-28 02:09:37 +00:00
|
|
|
import { Expression } from "../mathlib.mjs"
|
|
|
|
import * as P from "../parameters.mjs"
|
|
|
|
import Latex from "../math/latex.mjs"
|
|
|
|
import Objects from "../objects.mjs"
|
2021-08-14 16:57:29 +00:00
|
|
|
|
2024-03-28 02:09:37 +00:00
|
|
|
import { API as Common, DrawableObject } from "common.mjs"
|
2022-03-07 16:20:24 +00:00
|
|
|
|
2021-08-14 16:57:29 +00:00
|
|
|
|
2024-03-28 02:09:37 +00:00
|
|
|
export default class XCursor extends DrawableObject {
|
2021-08-14 16:57:29 +00:00
|
|
|
static type(){return 'X Cursor'}
|
2022-01-20 17:19:36 +00:00
|
|
|
static displayType(){return qsTr('X Cursor')}
|
|
|
|
static displayTypeMultiple(){return qsTr('X Cursors')}
|
2022-01-27 00:02:44 +00:00
|
|
|
static properties() {return {
|
|
|
|
[QT_TRANSLATE_NOOP('prop','x')]: 'Expression',
|
|
|
|
[QT_TRANSLATE_NOOP('prop','targetElement')]: new P.ObjectType('ExecutableObject'),
|
|
|
|
[QT_TRANSLATE_NOOP('prop','labelPosition')]: P.Enum.Position,
|
|
|
|
[QT_TRANSLATE_NOOP('prop','approximate')]: 'boolean',
|
|
|
|
[QT_TRANSLATE_NOOP('prop','rounding')]: 'number',
|
|
|
|
[QT_TRANSLATE_NOOP('prop','displayStyle')]: new P.Enum(
|
|
|
|
'— — — — — — —',
|
|
|
|
'⸺⸺⸺⸺⸺⸺',
|
|
|
|
'• • • • • • • • • •'
|
|
|
|
),
|
|
|
|
[QT_TRANSLATE_NOOP('prop','targetValuePosition')]: P.Enum.XCursorValuePosition,
|
|
|
|
}}
|
2021-08-14 16:57:29 +00:00
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
|
|
|
x = 1, targetElement = null, labelPosition = 'left', approximate = true,
|
|
|
|
rounding = 3, displayStyle = '— — — — — — —', targetValuePosition = 'Next to target') {
|
|
|
|
if(name == null) name = Common.getNewName('X')
|
|
|
|
super(name, visible, color, labelContent)
|
|
|
|
this.approximate = approximate
|
|
|
|
this.rounding = rounding
|
2024-03-28 02:09:37 +00:00
|
|
|
if(typeof x == 'number' || typeof x == 'string') x = new Expression(x.toString())
|
2021-08-14 16:57:29 +00:00
|
|
|
this.x = x
|
|
|
|
this.targetElement = targetElement
|
|
|
|
if(typeof targetElement == "string") {
|
2022-10-17 18:25:29 +00:00
|
|
|
this.targetElement = Objects.currentObjectsByName[targetElement]
|
2021-08-14 16:57:29 +00:00
|
|
|
}
|
|
|
|
this.labelPosition = labelPosition
|
|
|
|
this.displayStyle = displayStyle
|
|
|
|
this.targetValuePosition = targetValuePosition
|
|
|
|
}
|
|
|
|
|
|
|
|
export() {
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
|
|
|
this.x.toEditableString(), this.targetElement == null ? null : this.targetElement.name, this.labelPosition,
|
|
|
|
this.approximate, this.rounding, this.displayStyle, this.targetValuePosition]
|
|
|
|
}
|
|
|
|
|
|
|
|
getReadableString() {
|
|
|
|
if(this.targetElement == null) return `${this.name} = ${this.x.toString()}`
|
|
|
|
return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}`
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:20:24 +00:00
|
|
|
getLatexString() {
|
|
|
|
if(this.targetElement == null) return `${Latex.variable(this.name)} = ${this.x.latexMarkup}`
|
|
|
|
return `\\begin{array}{l}
|
|
|
|
${Latex.variable(this.name)} = ${this.x.latexMarkup} \\\\
|
2022-10-18 23:49:30 +00:00
|
|
|
${this.getTargetValueLatexLabel()}
|
|
|
|
\\end{array}`
|
2022-03-07 16:20:24 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 16:57:29 +00:00
|
|
|
getTargetValueLabel() {
|
|
|
|
var t = this.targetElement
|
|
|
|
var approx = ''
|
|
|
|
if(this.approximate) {
|
|
|
|
approx = t.execute(this.x.execute())
|
|
|
|
approx = approx.toPrecision(this.rounding + Math.round(approx).toString().length)
|
|
|
|
}
|
|
|
|
return `${t.name}(${this.name}) = ${t.simplify(this.x.toEditableString())}` +
|
|
|
|
(this.approximate ? ' ≈ ' + approx : '')
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:20:24 +00:00
|
|
|
getTargetValueLatexLabel() {
|
2024-03-29 00:55:13 +00:00
|
|
|
let t = this.targetElement
|
|
|
|
let approx = ''
|
2022-03-07 16:20:24 +00:00
|
|
|
if(this.approximate) {
|
|
|
|
approx = t.execute(this.x.execute())
|
|
|
|
approx = approx.toPrecision(this.rounding + Math.round(approx).toString().length)
|
|
|
|
}
|
|
|
|
let simpl = t.simplify(this.x.toEditableString())
|
|
|
|
return `${Latex.variable(t.name)}(${Latex.variable(this.name)}) = ${simpl.tokens ? Latex.expression(simpl.tokens) : simpl}` +
|
|
|
|
(this.approximate ? ' \\simeq ' + approx : '')
|
|
|
|
}
|
|
|
|
|
2021-08-14 16:57:29 +00:00
|
|
|
getLabel() {
|
|
|
|
switch(this.labelContent) {
|
|
|
|
case 'name':
|
|
|
|
return this.name
|
|
|
|
break;
|
|
|
|
case 'name + value':
|
|
|
|
switch(this.targetValuePosition) {
|
|
|
|
case 'Next to target':
|
|
|
|
case 'Hidden':
|
|
|
|
return `${this.name} = ${this.x.toString()}`
|
|
|
|
break;
|
|
|
|
case 'With label':
|
|
|
|
return this.getReadableString()
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'null':
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:20:24 +00:00
|
|
|
getLatexLabel() {
|
|
|
|
switch(this.labelContent) {
|
|
|
|
case 'name':
|
|
|
|
return Latex.variable(this.name)
|
|
|
|
break;
|
|
|
|
case 'name + value':
|
|
|
|
switch(this.targetValuePosition) {
|
|
|
|
case 'Next to target':
|
|
|
|
case 'Hidden':
|
|
|
|
return `${Latex.variable(this.name)} = ${this.x.latexMarkup}`
|
|
|
|
break;
|
|
|
|
case 'With label':
|
|
|
|
return this.getLatexString()
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'null':
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 00:55:13 +00:00
|
|
|
draw(canvas) {
|
2022-03-07 16:20:24 +00:00
|
|
|
let xpos = canvas.x2px(this.x.execute())
|
2021-08-14 16:57:29 +00:00
|
|
|
switch(this.displayStyle) {
|
|
|
|
case '— — — — — — —':
|
2024-03-29 00:55:13 +00:00
|
|
|
canvas.drawDashedLine(xpos, 0, xpos, canvas.height, 20)
|
2021-08-14 16:57:29 +00:00
|
|
|
break;
|
|
|
|
case '⸺⸺⸺⸺⸺⸺':
|
2024-03-29 00:55:13 +00:00
|
|
|
canvas.drawXLine(this.x.execute())
|
2021-08-14 16:57:29 +00:00
|
|
|
break;
|
|
|
|
case '• • • • • • • • • •':
|
2024-03-29 00:55:13 +00:00
|
|
|
let pointDistancePx = 10
|
|
|
|
let pointSize = 2
|
|
|
|
for(let i = 0; i < canvas.height; i += pointDistancePx)
|
|
|
|
canvas.disc(xpos, i, pointSize)
|
2021-08-14 16:57:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:20:24 +00:00
|
|
|
// Drawing label at the top of the canvas.
|
2024-03-29 00:55:13 +00:00
|
|
|
this.drawLabel(canvas, this.labelPosition, xpos, 0, false, null, null,
|
|
|
|
(x,y,ltxImg) => canvas.drawVisibleImage(ltxImg.source, x, 5, ltxImg.width, ltxImg.height),
|
|
|
|
(x,y,text,textSize) => canvas.drawVisibleText(text, x, textSize.height+5))
|
2021-08-14 16:57:29 +00:00
|
|
|
|
2022-03-07 16:20:24 +00:00
|
|
|
// Drawing label at the position of the target element.
|
2024-03-28 02:09:37 +00:00
|
|
|
if(this.targetValuePosition === 'Next to target' && this.targetElement != null) {
|
2022-03-07 16:20:24 +00:00
|
|
|
let ypos = canvas.y2px(this.targetElement.execute(this.x.execute()))
|
2024-03-29 00:55:13 +00:00
|
|
|
this.drawLabel(canvas, this.labelPosition, xpos, ypos, false,
|
2022-03-07 16:20:24 +00:00
|
|
|
this.getTargetValueLatexLabel.bind(this), this.getTargetValueLabel.bind(this),
|
2024-03-29 00:55:13 +00:00
|
|
|
(x,y,ltxImg) => canvas.drawVisibleImage(ltxImg.source, x, y, ltxImg.width, ltxImg.height),
|
|
|
|
(x,y,text,textSize) => canvas.drawVisibleText(text, x, y+textSize.height))
|
2021-08-14 16:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|