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"
|
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 02:10:51 +00:00
|
|
|
|
2021-08-14 16:57:29 +00:00
|
|
|
|
2024-03-28 02:09:37 +00:00
|
|
|
export default class Text extends DrawableObject {
|
2021-08-14 16:57:29 +00:00
|
|
|
static type(){return 'Text'}
|
2022-01-20 17:19:36 +00:00
|
|
|
static displayType(){return qsTr('Text')}
|
|
|
|
static displayTypeMultiple(){return qsTr('Texts')}
|
2022-01-27 00:02:44 +00:00
|
|
|
static properties() {return {
|
2022-10-19 23:14:09 +00:00
|
|
|
[QT_TRANSLATE_NOOP('prop','x')]: new P.Expression(),
|
|
|
|
[QT_TRANSLATE_NOOP('prop','y')]: new P.Expression(),
|
2022-01-28 10:50:57 +00:00
|
|
|
[QT_TRANSLATE_NOOP('prop','labelPosition')]: P.Enum.Positioning,
|
2022-03-07 01:46:38 +00:00
|
|
|
[QT_TRANSLATE_NOOP('prop','text')]: 'string',
|
2024-04-01 21:48:57 +00:00
|
|
|
'comment1': QT_TRANSLATE_NOOP(
|
2022-03-07 02:10:51 +00:00
|
|
|
'comment',
|
|
|
|
'If you have latex enabled, you can use use latex markup in between $$ to create equations.'
|
|
|
|
),
|
|
|
|
[QT_TRANSLATE_NOOP('prop','disableLatex')]: 'boolean'
|
2021-08-14 16:57:29 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'null',
|
2022-03-07 01:46:38 +00:00
|
|
|
x = 1, y = 0, labelPosition = 'center', text = 'New text', disableLatex = false) {
|
2021-08-14 16:57:29 +00:00
|
|
|
if(name == null) name = Common.getNewName('t')
|
|
|
|
super(name, visible, color, labelContent)
|
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
|
2024-03-28 02:09:37 +00:00
|
|
|
if(typeof y == 'number' || typeof y == 'string') y = new Expression(y.toString())
|
2021-08-14 16:57:29 +00:00
|
|
|
this.y = y
|
|
|
|
this.labelPosition = labelPosition
|
|
|
|
this.text = text
|
2022-03-07 01:46:38 +00:00
|
|
|
this.disableLatex = disableLatex
|
2021-08-14 16:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getReadableString() {
|
|
|
|
return `${this.name} = "${this.text}"`
|
|
|
|
}
|
|
|
|
|
2022-03-07 01:46:38 +00:00
|
|
|
latexMarkupText() {
|
2023-05-22 09:02:34 +00:00
|
|
|
// Check whether the text contains latex escaped elements.
|
|
|
|
let txt = []
|
|
|
|
this.text.split('$$').forEach(function(t) { txt = txt.concat(Latex.variable(t, true).replace(/\$\$/g, '').split('$')) })
|
|
|
|
let newTxt = txt[0]
|
2022-03-07 01:46:38 +00:00
|
|
|
let i
|
2023-05-22 09:02:34 +00:00
|
|
|
// Split between normal text and latex escaped.
|
|
|
|
for(i = 0; i < txt.length-1; i++)
|
2022-03-07 01:46:38 +00:00
|
|
|
if(i & 0x01) // Every odd number
|
2023-05-22 09:02:34 +00:00
|
|
|
newTxt += '\\textsf{'+Latex.variable(txt[i+1])
|
2022-03-07 01:46:38 +00:00
|
|
|
else
|
2023-05-22 09:02:34 +00:00
|
|
|
newTxt += '}'+txt[i+1]
|
|
|
|
// Finished by a }
|
|
|
|
if(i & 0x01)
|
|
|
|
newTxt += "{"
|
|
|
|
return newTxt
|
2022-03-07 01:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getLatexString() {
|
2022-03-07 14:06:40 +00:00
|
|
|
return `${Latex.variable(this.name)} = "\\textsf{${this.latexMarkupText()}}"`
|
2022-03-07 01:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getLabel() {
|
|
|
|
return this.text
|
|
|
|
}
|
|
|
|
|
|
|
|
getLatexLabel() {
|
2022-03-07 14:06:40 +00:00
|
|
|
return `\\textsf{${this.latexMarkupText()}}`
|
2022-03-07 01:46:38 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 00:55:13 +00:00
|
|
|
draw(canvas) {
|
2022-03-07 01:46:38 +00:00
|
|
|
let yOffset = this.disableLatex ? canvas.textsize-4 : 0
|
2024-03-29 00:55:13 +00:00
|
|
|
this.drawLabel(canvas, this.labelPosition, canvas.x2px(this.x.execute()), canvas.y2px(this.y.execute())+yOffset, this.disableLatex)
|
2021-08-14 16:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|