File indexing completed on 2024-05-12 16:07:26

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "PdfGenerator.h"
0021 
0022 #include <QProcess>
0023 #include <QTemporaryFile>
0024 #include <QFileInfo>
0025 #include <QTextStream>
0026 
0027 #include <QDebug>
0028 
0029 namespace tex {
0030 
0031 class PdfGeneratorPrivate
0032 {
0033     public:
0034         QProcess * process;
0035         QTemporaryFile * tempFile;
0036 };
0037 
0038 PdfGenerator::PdfGenerator(QObject * parent)
0039 : QObject(parent)
0040     , d(new PdfGeneratorPrivate())
0041 {
0042     d->process = nullptr;
0043     d->tempFile = nullptr;
0044 }
0045 
0046 PdfGenerator::~PdfGenerator()
0047 {
0048     delete d;
0049 }
0050 
0051 QString PdfGenerator::pdfFile()
0052 {
0053     if (d->tempFile) {
0054         QFileInfo fi(*d->tempFile);
0055         return fi.path() + '/' + fi.baseName() + ".pdf";
0056     }
0057     return QString();
0058 }
0059 
0060 void PdfGenerator::generatePdf(const QString& texCode)
0061 {
0062     if (!d->process) {
0063         d->process = new QProcess(this);
0064 
0065         connect(d->process, SIGNAL(readyReadStandardError()),
0066                 this, SLOT(outputReady()));
0067         connect(d->process, SIGNAL(finished(int, QProcess::ExitStatus)),
0068                 this, SLOT(processFinished(int, QProcess::ExitStatus)));
0069         connect(d->process, SIGNAL(error(QProcess::ProcessError)),
0070                 this, SLOT(processError(QProcess::ProcessError)));
0071     } else {
0072         if (d->process->state() == QProcess::Running) {
0073             d->process->terminate();
0074         }
0075     }
0076 
0077     if (d->tempFile) {
0078         delete d->tempFile;
0079     }
0080 
0081     d->tempFile = new QTemporaryFile("XXXXXX.tex", this);
0082     if (!d->tempFile->open()) {
0083         qWarning() << "Could not create temporary tex file";
0084         return;
0085     } else {
0086         QTextStream ts(d->tempFile);
0087         ts << "\\documentclass[crop]{standalone}\n"
0088         "\\renewcommand*{\\familydefault}{\\sfdefault}\n"
0089 //         "\\usepackage[pdftex,active,tightpage]{preview}\n"
0090         "\\usepackage{amsmath}\n"
0091         "\\usepackage{amssymb}\n"
0092         "\\usepackage{amsfonts}\n"
0093         "\\usepackage{tikz}\n"
0094         "\\usetikzlibrary{shapes}\n"
0095         "\\begin{document}\n"
0096 //         "\\begin{preview}\n"
0097         "\\small\n"
0098         + texCode + 
0099 //         "\\end{preview}\n"
0100         "\\end{document}";
0101         d->tempFile->close();
0102     }
0103 
0104     QStringList args;
0105     args << "-halt-on-error" << d->tempFile->fileName();
0106 
0107 //    qDebug() << "launching process: 'pdflatex'" << args;
0108     d->process->start("pdflatex", args);
0109 }
0110 
0111 void PdfGenerator::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
0112 {
0113     qDebug() << "pdflatex: finished with exitCode" << exitCode;
0114     if (exitCode != 0) {
0115         return;
0116     }
0117 
0118     Q_EMIT finished(pdfFile());
0119 }
0120 
0121 void PdfGenerator::processError(QProcess::ProcessError error)
0122 {
0123 //     qDebug() << "process error" << error;
0124 }
0125 
0126 void PdfGenerator::outputReady()
0127 {
0128 //     qDebug() << "output ready" << d->process->readAllStandardError();
0129 }
0130 
0131 }
0132 
0133 // kate: indent-width 4; replace-tabs on;