• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • practice
latexrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Copyright 2010 Daniel Laidig <laidig@kde.org>
3  Copyright 2009 Alexander Rieder <alexanderrieder@gmail.com>
4  ***************************************************************************/
5 
6 /***************************************************************************
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  ***************************************************************************/
14 
15 #include "latexrenderer.h"
16 
17 #include <klocale.h>
18 #include <kdebug.h>
19 #include <ktemporaryfile.h>
20 #include <kstandarddirs.h>
21 #include <kprocess.h>
22 
23 #include <QLabel>
24 #include <QProcess>
25 #include <QFileInfo>
26 #include <QColor>
27 #include <complex>
28 
29 using namespace Practice;
30 
31 const char* texTemplate = "\\documentclass[12pt,fleqn]{article} \n "\
32  "\\usepackage{latexsym,amsfonts,amssymb,ulem} \n "\
33  "\\usepackage[dvips]{graphicx} \n "\
34  "\\setlength\\textwidth{5in} \n "\
35  "\\setlength{\\parindent}{0pt} \n "\
36  "\\usepackage{amsmath} \n "\
37  "\\usepackage{color} \n "\
38  "\\pagestyle{empty} \n "\
39  "\\begin{document} \n "\
40  "{\\definecolor{mycolor}{rgb}{%1} \n "\
41  "{\\color{mycolor} \n "\
42  "%2 } \n "\
43  "\\end{document}\n";
44 
45 LatexRenderer::LatexRenderer(QObject* parent)
46  : QObject(parent), m_label(0)
47 {
48 
49 }
50 
51 void LatexRenderer::renderLatex(QString tex)
52 {
53  if (m_label) {
54  m_label->setText(i18n("Rendering..."));
55  m_label->setToolTip(tex);
56  }
57 
58  if (tex.startsWith(QLatin1String("$$"))) {
59  tex.replace(0, 2, "\\begin{eqnarray*}").remove(-2, 2).append("\\end{eqnarray*}");
60  } else {
61  tex.remove(0, 2).remove(-2, 2);
62  }
63  kDebug() << "rendering as latex";
64 
65  QString dir = KGlobal::dirs()->saveLocation("tmp", "parley/");
66 
67  //Check if the parley subdir exists, if not, create it
68  KTemporaryFile *texFile = new KTemporaryFile();
69  texFile->setPrefix("parley/");
70  texFile->setSuffix(".tex");
71  texFile->open();
72 
73  QColor color = m_label->palette().color(QPalette::WindowText);
74  QString colorString = QString::number(color.redF()) + ','
75  + QString::number(color.greenF()) + ','
76  + QString::number(color.blueF());
77  QString expressionTex = QString(texTemplate).arg(colorString, tex.trimmed());
78 
79  texFile->write(expressionTex.toUtf8());
80  texFile->flush();
81 
82  QString fileName = texFile->fileName();
83  kDebug() << "fileName: " << fileName;
84  m_latexFilename = fileName;
85  m_latexFilename.replace(".tex", ".eps");
86  KProcess *p = new KProcess(this);
87  p->setWorkingDirectory(dir);
88 
89  (*p) << "latex" << "-interaction=batchmode" << "-halt-on-error" << fileName;
90 
91  connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(convertToPs()));
92  connect(p, SIGNAL(error(QProcess::ProcessError)), this, SLOT(latexRendered()));
93  p->start();
94 }
95 
96 bool LatexRenderer::isLatex(const QString& tex)
97 {
98  return tex.length() > 4 && tex.mid(2, tex.length() - 4).simplified().length() > 0 &&
99  ((tex.startsWith(QLatin1String("$$")) && tex.endsWith(QLatin1String("$$"))) ||
100  (tex.startsWith(QString::fromUtf8("§§")) && tex.endsWith(QString::fromUtf8("§§"))));
101 }
102 
103 void LatexRenderer::convertToPs()
104 {
105  kDebug() << "converting to ps";
106  QString dviFile = m_latexFilename;
107  dviFile.replace(".eps", ".dvi");
108  KProcess *p = new KProcess(this);
109  kDebug() << "running: " << "dvips" << "-E" << "-o" << m_latexFilename << dviFile;
110  (*p) << "dvips" << "-E" << "-o" << m_latexFilename << dviFile;
111 
112  connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(convertToImage()));
113  connect(p, SIGNAL(error(QProcess::ProcessError)), this, SLOT(latexRendered()));
114  p->start();
115 }
116 
117 void LatexRenderer::convertToImage()
118 {
119  kDebug() << "converting to ps";
120  QString pngFile = m_latexFilename;
121  pngFile.replace(".eps", ".png");
122  KProcess *p = new KProcess(this);
123  kDebug() << "running:" << "convert" << m_latexFilename << pngFile;
124  (*p) << "convert" << "-density" << "85" << m_latexFilename << pngFile;
125 
126  connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(latexRendered()));
127  connect(p, SIGNAL(error(QProcess::ProcessError)), this, SLOT(latexRendered()));
128  p->start();
129 }
130 
131 void LatexRenderer::latexRendered()
132 {
133  kDebug() << "rendered file " << m_latexFilename;
134 
135  QString pngFile = m_latexFilename;
136  pngFile.replace(".eps", ".png");
137  if (QFileInfo(pngFile).exists()) {
138  QPixmap pixmap(pngFile);
139  m_label->setPixmap(pixmap);
140  m_label->setMinimumSize(pixmap.size().boundedTo(QSize(600, 300)));
141  } else {
142  m_label->setText(i18n("LaTeX error.")); //TODO: better error handling and error messages
143  }
144 
145  //cleanup the temp directory a bit...
146  QString dir = KGlobal::dirs()->saveLocation("tmp", "parley/");
147  QStringList extensions;
148  extensions << ".log" << ".aux" << ".tex" << ".dvi" << ".eps" << ".png";
149  foreach(const QString & ext, extensions) {
150  QString s = m_latexFilename;
151  s.replace(".eps", ext);
152  QFile f(s);
153  f.remove();
154  }
155 }
156 
157 #include "latexrenderer.moc"
QString::append
QString & append(QChar ch)
QWidget::palette
palette
QColor::redF
qreal redF() const
QLabel::setPixmap
void setPixmap(const QPixmap &)
QColor::blueF
qreal blueF() const
KTemporaryFile
QString::remove
QString & remove(int position, int n)
texTemplate
const char * texTemplate
Definition: latexrenderer.cpp:31
QFile
QWidget::setMinimumSize
void setMinimumSize(const QSize &)
QString::number
QString number(int n, int base)
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QObject
QString::trimmed
QString trimmed() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QColor::greenF
qreal greenF() const
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QLabel::setText
void setText(const QString &)
QString
QColor
QStringList
QPixmap
QFileInfo
QSize
QString::replace
QString & replace(int position, int n, QChar after)
QString::mid
QString mid(int position, int n) const
QLatin1String
QString::length
int length() const
latexrenderer.h
Practice::LatexRenderer::LatexRenderer
LatexRenderer(QObject *parent=0)
Definition: latexrenderer.cpp:45
QWidget::setToolTip
void setToolTip(const QString &)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Practice::LatexRenderer::renderLatex
void renderLatex(QString tex)
Definition: latexrenderer.cpp:51
Practice::LatexRenderer::isLatex
static bool isLatex(const QString &tex)
Definition: latexrenderer.cpp:96
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal