Toggle for latex setting
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
Adsooi 2022-03-07 20:28:50 +01:00
parent 7120e3a781
commit 27759362cb
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
7 changed files with 51 additions and 12 deletions

View file

@ -22,6 +22,8 @@ import QtQuick.Dialogs 1.3
import eu.ad5001.MixedMenu 1.1 import eu.ad5001.MixedMenu 1.1
import "js/objects.js" as Objects import "js/objects.js" as Objects
import "js/historylib.js" as HistoryLib import "js/historylib.js" as HistoryLib
import "js/math/latex.js" as Latex
/*! /*!
\qmltype AppMenuBar \qmltype AppMenuBar
@ -139,6 +141,21 @@ MenuBar {
onTriggered: Helper.setSettingBool("reset_redo_stack", checked) onTriggered: Helper.setSettingBool("reset_redo_stack", checked)
icon.name: 'timeline' icon.name: 'timeline'
} }
Action {
id: enableLatexSetting
text: qsTr("Enable LaTeX rendering")
checkable: true
checked: Helper.getSettingBool("enable_latex")
onTriggered: {
Helper.setSettingBool("enable_latex", checked)
Latex.enabled = checked
drawCanvas.requestPaint()
}
icon.name: 'Expression'
Component.onCompleted: Latex.enabled = checked
}
} }
Menu { Menu {

View file

@ -355,10 +355,11 @@ Canvas {
Return format: dictionary {"width": width, "height": height} Return format: dictionary {"width": width, "height": height}
*/ */
function measureText(ctx, text) { function measureText(ctx, text) {
var theight = 0 let theight = 0
var twidth = 0 let twidth = 0
let defaultHeight = ctx.measureText("M").width // Approximate but good enough!
text.split("\n").forEach(function(txt, i){ text.split("\n").forEach(function(txt, i){
theight += canvas.textsize theight += defaultHeight
if(ctx.measureText(txt).width > twidth) twidth = ctx.measureText(txt).width if(ctx.measureText(txt).width > twidth) twidth = ctx.measureText(txt).width
}) })
return {'width': twidth, 'height': theight} return {'width': twidth, 'height': theight}

View file

@ -18,6 +18,7 @@
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Controls 2.12 import QtQuick.Controls 2.12
import "../js/math/latex.js" as Latex
/*! /*!
\qmltype GreetScreen \qmltype GreetScreen
@ -101,7 +102,7 @@ Popup {
text: qsTr('Check for updates on startup (requires online connectivity)') text: qsTr('Check for updates on startup (requires online connectivity)')
onClicked: { onClicked: {
Helper.setSettingBool("check_for_updates", checked) Helper.setSettingBool("check_for_updates", checked)
checkForUpdatesMenuSetting.checked = checked //checkForUpdatesMenuSetting.checked = checked
} }
} }
@ -113,7 +114,20 @@ Popup {
text: qsTr('Reset redo stack when a new action is added to history') text: qsTr('Reset redo stack when a new action is added to history')
onClicked: { onClicked: {
Helper.setSettingBool("reset_redo_stack", checked) Helper.setSettingBool("reset_redo_stack", checked)
resetRedoStackMenuSetting.checked = checked //resetRedoStackMenuSetting.checked = checked
}
}
CheckBox {
id: enableLatexSetting
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: resetRedoStackSetting.bottom
checked: Helper.getSettingBool("enable_latex")
text: qsTr('Enable LaTeX rendering')
onClicked: {
Helper.setSettingBool("enable_latex", checked)
Latex.enabled = checked
drawCanvas.requestPaint()
} }
} }

View file

@ -21,6 +21,11 @@
.import "../expr-eval.js" as ExprEval .import "../expr-eval.js" as ExprEval
/**
* true if latex has been enabled by the user, false otherwise.
*/
var enabled = false
/** /**
* Puts element within parenthesis. * Puts element within parenthesis.
* *

View file

@ -279,7 +279,7 @@ class DrawableObject {
* @param {function|null} getLatexFunction - Function (no argument) to get the latex markup to be displayed * @param {function|null} getLatexFunction - Function (no argument) to get the latex markup to be displayed
* @param {function|null} getTextFunction - Function (no argument) to get the text to be displayed * @param {function|null} getTextFunction - Function (no argument) to get the text to be displayed
* @param {function|null} drawFunctionLatex - Function (x,y,imageData) to display the latex image * @param {function|null} drawFunctionLatex - Function (x,y,imageData) to display the latex image
* @param {function|null} drawFunctionText - Function (x,y,text) to display the text * @param {function|null} drawFunctionText - Function (x,y,text,textSize) to display the text
*/ */
drawLabel(canvas, ctx, labelPosition, posX, posY, forceText = false, drawLabel(canvas, ctx, labelPosition, posX, posY, forceText = false,
getLatexFunction = null, getTextFunction = null, drawFunctionLatex = null, drawFunctionText = null) { getLatexFunction = null, getTextFunction = null, drawFunctionLatex = null, drawFunctionText = null) {
@ -291,13 +291,13 @@ class DrawableObject {
if(drawFunctionLatex == null) if(drawFunctionLatex == null)
drawFunctionLatex = (x,y,ltxImg) => canvas.drawVisibleImage(ctx, ltxImg.source, x, y, ltxImg.width, ltxImg.height) drawFunctionLatex = (x,y,ltxImg) => canvas.drawVisibleImage(ctx, ltxImg.source, x, y, ltxImg.width, ltxImg.height)
if(drawFunctionText == null) if(drawFunctionText == null)
drawFunctionText = (x,y,text) => canvas.drawVisibleText(ctx, text, x, textSize.height+5) drawFunctionText = (x,y,text,textSize) => canvas.drawVisibleText(ctx, text, x, y+textSize.height) // Positioned from left bottom
// Drawing the label // Drawing the label
let offset let offset
if(!forceText && true) { // TODO: Check for user setting with Latex. if(!forceText && Latex.enabled) { // TODO: Check for user setting with Latex.
// With latex // With latex
let drawLblCb = function(canvas, ctx, ltxImg) { let drawLblCb = function(canvas, ctx, ltxImg) {
this.drawPositionDivergence(labelPosition, 8, ltxImg, posX, posY, (x,y) => drawFunctionLatex(x,y,ltxImg)) this.drawPositionDivergence(labelPosition, 8+ctx.lineWidth/2, ltxImg, posX, posY, (x,y) => drawFunctionLatex(x,y,ltxImg))
} }
let ltxLabel = getLatexFunction(); let ltxLabel = getLatexFunction();
if(ltxLabel != "") if(ltxLabel != "")
@ -306,7 +306,8 @@ class DrawableObject {
// Without latex // Without latex
let text = getTextFunction() let text = getTextFunction()
ctx.font = `${canvas.textsize}px sans-serif` ctx.font = `${canvas.textsize}px sans-serif`
this.drawPositionDivergence(labelPosition, 4, canvas.measureText(ctx, text), posX, posY, (x,y) => drawFunctionText(x,y,text)) let textSize = canvas.measureText(ctx, text)
this.drawPositionDivergence(labelPosition, 8+ctx.lineWidth/2, textSize, posX, posY, (x,y) => drawFunctionText(x,y,text,textSize))
} }
} }

View file

@ -168,7 +168,7 @@ class XCursor extends Common.DrawableObject {
// Drawing label at the top of the canvas. // Drawing label at the top of the canvas.
this.drawLabel(canvas, ctx, this.labelPosition, xpos, 0, false, null, null, this.drawLabel(canvas, ctx, this.labelPosition, xpos, 0, false, null, null,
(x,y,ltxImg) => canvas.drawVisibleImage(ctx, ltxImg.source, x, 5, ltxImg.width, ltxImg.height), (x,y,ltxImg) => canvas.drawVisibleImage(ctx, ltxImg.source, x, 5, ltxImg.width, ltxImg.height),
(x,y,text) => canvas.drawVisibleText(ctx, text, x, textSize.height+5)) (x,y,text,textSize) => canvas.drawVisibleText(ctx, text, x, textSize.height+5))
// Drawing label at the position of the target element. // Drawing label at the position of the target element.
if(this.targetValuePosition == 'Next to target' && this.targetElement != null) { if(this.targetValuePosition == 'Next to target' && this.targetElement != null) {
@ -176,7 +176,7 @@ class XCursor extends Common.DrawableObject {
this.drawLabel(canvas, ctx, this.labelPosition, xpos, ypos, false, this.drawLabel(canvas, ctx, this.labelPosition, xpos, ypos, false,
this.getTargetValueLatexLabel.bind(this), this.getTargetValueLabel.bind(this), this.getTargetValueLatexLabel.bind(this), this.getTargetValueLabel.bind(this),
(x,y,ltxImg) => canvas.drawVisibleImage(ctx, ltxImg.source, x, y, ltxImg.width, ltxImg.height), (x,y,ltxImg) => canvas.drawVisibleImage(ctx, ltxImg.source, x, y, ltxImg.width, ltxImg.height),
(x,y,text) => canvas.drawVisibleText(ctx, text, x, y)) (x,y,text,textSize) => canvas.drawVisibleText(ctx, text, x, y+textSize.height))
} }
} }
} }

View file

@ -26,6 +26,7 @@ DEFAULT_SETTINGS = {
"check_for_updates": True, "check_for_updates": True,
"reset_redo_stack": True, "reset_redo_stack": True,
"last_install_greet": "0", "last_install_greet": "0",
"enable_latex": True
} }
# Create config directory # Create config directory