From 15fd660e0c58a171d1715aa81de78df2e5fdaad0 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Tue, 18 Oct 2022 01:47:49 +0200 Subject: [PATCH] Adding support for latex images in the objects tab. --- .../LogarithmPlotter/ObjectLists/ObjectRow.qml | 14 +++++++++++++- LogarithmPlotter/util/latex.py | 11 ++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectRow.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectRow.qml index cf91ffc..13cb9df 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectRow.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/ObjectLists/ObjectRow.qml @@ -22,6 +22,8 @@ import QtQuick.Controls 2.12 import eu.ad5001.LogarithmPlotter.Setting 1.0 as Setting import "../js/objects.js" as Objects import "../js/historylib.js" as HistoryLib +import "../js/math/latex.js" as LatexJS + /*! \qmltype ObjectRow @@ -74,9 +76,19 @@ Item { anchors.right: deleteButton.left height: parent.height verticalAlignment: TextInput.AlignVCenter - text: obj.getReadableString() + text: LatexJS.enabled ? "" : obj.getReadableString() font.pixelSize: 14 + Image { + anchors.verticalCenter: parent.verticalCenter + anchors.left: parent.left + visible: LatexJS.enabled + property var ltxInfo: visible ? Latex.render(obj.getLatexLabel(), 2*parent.font.pixelSize, parent.color).split(",") : ["","0","0"] + source: visible ? ltxInfo[0] : "" + width: parseInt(ltxInfo[1])/2 + height: parseInt(ltxInfo[2])/2 + } + MouseArea { anchors.fill: parent onClicked: { diff --git a/LogarithmPlotter/util/latex.py b/LogarithmPlotter/util/latex.py index 0ce5b91..d9b4643 100644 --- a/LogarithmPlotter/util/latex.py +++ b/LogarithmPlotter/util/latex.py @@ -79,11 +79,11 @@ class Latex(QObject): @Property(bool) def latexSupported(self): return LATEX_PATH is not None and DVIPNG_PATH is not None - + @Slot(str, float, QColor, result=str) - def render(self, latex_markup: str, font_size: float, color: QColor = True) -> str: + def render(self, latex_markup: str, font_size: float, color: QColor) -> str: """ - Renders a latex string into a png file. + Prepares and renders a latex string into a png file. """ markup_hash = hash(latex_markup) export_path = path.join(self.tempdir.name, f'{markup_hash}_{font_size}_{color.rgb()}') @@ -100,7 +100,7 @@ class Latex(QObject): self.convert_dvi_to_png(latex_path, export_path, font_size, color) except Exception as e: # One of the processes failed. A message will be sent every time. raise e - img = QImage(export_path + ".png"); + img = QImage(export_path); # Small hack, not very optimized since we load the image twice, but you can't pass a QImage to QML and expect it to be loaded return f'{export_path}.png,{img.width()},{img.height()}' @@ -171,7 +171,7 @@ class Latex(QObject): for i in [".tex", ".aux", ".log"]: remove(export_path + i) - + """ @Slot(str, float, QColor, result=str) def render_legacy(self, latexstring, font_size, color = True): exprpath = path.join(self.tempdir.name, f'{hash(latexstring)}_{font_size}_{color.rgb()}.png') @@ -190,3 +190,4 @@ class Latex(QObject): img = QImage(exprpath); # Small hack, not very optimized since we load the image twice, but you can't pass a QImage to QML and expect it to be loaded return f'{exprpath},{img.width()},{img.height()}' + """