Adding support for latex images in the objects tab.

This commit is contained in:
Ad5001 2022-10-18 01:47:49 +02:00
parent dfdd576296
commit 15fd660e0c
Signed by: Ad5001
GPG key ID: 7251B1AF90B960F9
2 changed files with 19 additions and 6 deletions

View file

@ -22,6 +22,8 @@ import QtQuick.Controls 2.12
import eu.ad5001.LogarithmPlotter.Setting 1.0 as Setting import eu.ad5001.LogarithmPlotter.Setting 1.0 as Setting
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 LatexJS
/*! /*!
\qmltype ObjectRow \qmltype ObjectRow
@ -74,9 +76,19 @@ Item {
anchors.right: deleteButton.left anchors.right: deleteButton.left
height: parent.height height: parent.height
verticalAlignment: TextInput.AlignVCenter verticalAlignment: TextInput.AlignVCenter
text: obj.getReadableString() text: LatexJS.enabled ? "" : obj.getReadableString()
font.pixelSize: 14 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 { MouseArea {
anchors.fill: parent anchors.fill: parent
onClicked: { onClicked: {

View file

@ -79,11 +79,11 @@ class Latex(QObject):
@Property(bool) @Property(bool)
def latexSupported(self): def latexSupported(self):
return LATEX_PATH is not None and DVIPNG_PATH is not None return LATEX_PATH is not None and DVIPNG_PATH is not None
@Slot(str, float, QColor, result=str) @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) markup_hash = hash(latex_markup)
export_path = path.join(self.tempdir.name, f'{markup_hash}_{font_size}_{color.rgb()}') 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) 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. except Exception as e: # One of the processes failed. A message will be sent every time.
raise e 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 # 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()}' return f'{export_path}.png,{img.width()},{img.height()}'
@ -171,7 +171,7 @@ class Latex(QObject):
for i in [".tex", ".aux", ".log"]: for i in [".tex", ".aux", ".log"]:
remove(export_path + i) remove(export_path + i)
"""
@Slot(str, float, QColor, result=str) @Slot(str, float, QColor, result=str)
def render_legacy(self, latexstring, font_size, color = True): def render_legacy(self, latexstring, font_size, color = True):
exprpath = path.join(self.tempdir.name, f'{hash(latexstring)}_{font_size}_{color.rgb()}.png') exprpath = path.join(self.tempdir.name, f'{hash(latexstring)}_{font_size}_{color.rgb()}.png')
@ -190,3 +190,4 @@ class Latex(QObject):
img = QImage(exprpath); 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 # 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()}' return f'{exprpath},{img.width()},{img.height()}'
"""