Reformat ReceiptsPrinter

remotes/origin/HEAD
markuspg 6 years ago
parent f5f32c4bcf
commit f345d88315

@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Markus Prasser
* Copyright 2014-2018 Markus Prasser, Tobias Weiss
*
* This file is part of Labcontrol.
*
@ -22,20 +22,20 @@
#include "receiptsprinter.h"
#include "settings.h"
extern std::unique_ptr< lc::Settings > settings;
extern std::unique_ptr<lc::Settings> settings;
lc::ReceiptsPrinter::ReceiptsPrinter( const QString &argDateString,
const QString &argWorkpath,
QObject *argParent ) :
QThread{ argParent },
dateString{ argDateString },
dvipsCmd{ settings->dvipsCmd },
latexCmd{ settings->latexCmd },
lprCmd{ settings->lprCmd },
postscriptViewer{ settings->postscriptViewer },
ps2pdfCmd{ settings->ps2pdfCmd },
rmCmd{ settings->rmCmd },
vncViewer{ settings->vncViewer },
workpath{ argWorkpath }
lc::ReceiptsPrinter::ReceiptsPrinter(const QString &argDateString,
const QString &argWorkpath,
QObject *argParent) :
QThread{argParent},
dateString{argDateString},
dvipsCmd{settings->dvipsCmd},
latexCmd{settings->latexCmd},
lprCmd{settings->lprCmd},
postscriptViewer{ settings->postscriptViewer},
ps2pdfCmd{settings->ps2pdfCmd},
rmCmd{settings->rmCmd},
vncViewer{settings->vncViewer},
workpath{argWorkpath}
{
}

@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Markus Prasser
* Copyright 2014-2018 Markus Prasser, Tobias Weiss
*
* This file is part of Labcontrol.
*
@ -32,22 +32,27 @@ namespace lc {
/*!
This class is used to do the actual printing of the receipts in an own thread.
*/
class ReceiptsPrinter : public QThread {
class ReceiptsPrinter : public QThread
{
Q_OBJECT
void run() Q_DECL_OVERRIDE {
void run() override
{
// Compile the TeX file to dvi
QStringList arguments;
arguments << "-interaction" << "batchmode" << QString{ dateString + ".tex" };
arguments << "-interaction" << "batchmode" << QString{dateString + ".tex"};
QProcess *process = nullptr;
process = new QProcess{};
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
process->setProcessEnvironment( env );
process->setWorkingDirectory( workpath );
process->start( latexCmd, arguments );
if( !process->waitForFinished( processTimeOut ) ) {
QMessageBox message_box{ QMessageBox::Warning, "dvi creation failed", "The creation of the receipts dvi timed out after 30 seconds. Automatic receipts creation will not work.", QMessageBox::Ok };
const QProcessEnvironment env{QProcessEnvironment::systemEnvironment()};
process->setProcessEnvironment(env);
process->setWorkingDirectory(workpath);
process->start(latexCmd, arguments);
if (!process->waitForFinished(processTimeOut)) {
QMessageBox message_box{QMessageBox::Warning, "dvi creation failed",
"The creation of the receipts dvi timed out"
" after 30 seconds. Automatic receipts creation will not work.",
QMessageBox::Ok};
message_box.exec();
delete process;
process = nullptr;
@ -58,14 +63,15 @@ class ReceiptsPrinter : public QThread {
// Convert the dvi file to postscript
arguments = QStringList{};
arguments << "-q*" << "-o" << QString{ dateString + ".ps" } << QString{ dateString + ".dvi" };
arguments << "-q*" << "-o" << QString{dateString + ".ps"} << QString{dateString + ".dvi"};
process = new QProcess{};
process->setProcessEnvironment( env );
process->setWorkingDirectory( workpath );
process->start( dvipsCmd, arguments );
if( !process->waitForFinished( processTimeOut ) ) {
emit ErrorOccurred(new QString{ "The conversion of the receipts dvi to postscript timed out after 30 seconds. Automatic receipts creation will not work." }, new QString{ "dvi to postscript conversion failed" } );
process->setProcessEnvironment(env);
process->setWorkingDirectory(workpath);
process->start(dvipsCmd, arguments);
if (!process->waitForFinished(processTimeOut)) {
emit ErrorOccurred(new QString{"The conversion of the receipts dvi to postscript timed out after 30 seconds. Automatic receipts creation will not work."},
new QString{"dvi to postscript conversion failed"});
delete process;
process = nullptr;
return;
@ -74,75 +80,79 @@ class ReceiptsPrinter : public QThread {
process = nullptr;
// Print the postscript file
if ( !lprCmd.isEmpty() ) {
if (!lprCmd.isEmpty()) {
arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".ps" };
arguments << QString{workpath + "/" + dateString + ".ps"};
process = new QProcess{};
process->setProcessEnvironment( env );
process->setWorkingDirectory( workpath );
process->start( lprCmd, arguments );
if( !process->waitForFinished( processTimeOut ) ) {
emit ErrorOccurred( new QString{ "The receipts postscript file was successfully created but could not be printed." }, new QString{ "Printing failed" } );
process->setProcessEnvironment(env);
process->setWorkingDirectory(workpath);
process->start(lprCmd, arguments);
if ( !process->waitForFinished( processTimeOut ) ) {
emit ErrorOccurred(new QString{"The receipts postscript file was successfully created but could not be printed."},
new QString{"Printing failed"});
}
delete process;
process = nullptr;
}
// Convert the postscript file to pdf
if ( !ps2pdfCmd.isEmpty() ) {
if (!ps2pdfCmd.isEmpty()) {
arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".ps" } << QString{ workpath + "/" + dateString + ".pdf" };
arguments << QString{workpath + "/" + dateString + ".ps"} << QString{workpath + "/" + dateString + ".pdf"};
process = new QProcess{};
process->setProcessEnvironment( env );
process->setWorkingDirectory( workpath );
process->start( ps2pdfCmd, arguments );
if( !process->waitForFinished( processTimeOut ) ) {
emit ErrorOccurred( new QString{ "The receipts were successfully printed but the creation of the PDF file failed." }, new QString{ "PDF creation failed" } );
process->setProcessEnvironment(env);
process->setWorkingDirectory(workpath);
process->start(ps2pdfCmd, arguments);
if (!process->waitForFinished(processTimeOut)) {
emit ErrorOccurred(new QString{"The receipts were successfully printed but the creation of the PDF file failed."},
new QString{"PDF creation failed"});
}
delete process;
process = nullptr;
// Show the postscript file if the conversion succeeded
if ( !postscriptViewer.isEmpty() ) {
if (!postscriptViewer.isEmpty()) {
arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".ps" };
arguments << QString{workpath + "/" + dateString + ".ps"};
process = new QProcess{};
process->setProcessEnvironment( env );
process->setWorkingDirectory( workpath );
process->startDetached( postscriptViewer, arguments );
process->setProcessEnvironment(env);
process->setWorkingDirectory(workpath);
process->startDetached(postscriptViewer, arguments);
delete process;
process = nullptr;
}
}
// Clean up the zTree working path
if ( !rmCmd.isEmpty() ) {
if (!rmCmd.isEmpty()) {
arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".aux" }
<< QString{ workpath + "/" + dateString + ".dvi" }
<< QString{ workpath + "/" + dateString + ".log" }
<< QString{ workpath + "/" + dateString + ".tex" };
arguments << QString{workpath + "/" + dateString + ".aux"}
<< QString{workpath + "/" + dateString + ".dvi"}
<< QString{workpath + "/" + dateString + ".log"}
<< QString{workpath + "/" + dateString + ".tex"};
process = new QProcess{};
process->setProcessEnvironment( env );
process->setWorkingDirectory( workpath );
process->setProcessEnvironment(env);
process->setWorkingDirectory(workpath);
process->start( rmCmd, arguments);
if( !process->waitForFinished( processTimeOut ) ) {
emit ErrorOccurred(new QString("The cleanup of the temporary files for receipts creation timed out. Some spare files may be left in your zTree working directory."), new QString("Cleanup failed"));
if (!process->waitForFinished(processTimeOut)) {
emit ErrorOccurred(new
QString("The cleanup of the temporary files for receipts creation timed out. Some spare files may be left in your zTree working directory."),
new QString("Cleanup failed"));
}
delete process;
process = nullptr;
}
}
emit PrintingFinished();
}
public:
explicit ReceiptsPrinter( const QString &argDateString,
const QString &argWorkpath,
QObject *argParent = nullptr );
explicit ReceiptsPrinter(const QString &argDateString,
const QString &argWorkpath,
QObject *argParent = nullptr);
signals:
void ErrorOccurred(QString *error_message, QString *heading);
@ -154,13 +164,14 @@ private:
const QString latexCmd;
const QString lprCmd;
const QString postscriptViewer;
const int processTimeOut = 15000; //! The maximum time which will be granted to a started process
const int processTimeOut =
15000; //! The maximum time which will be granted to a started process
const QString ps2pdfCmd;
const QString rmCmd;
const QString vncViewer;
const QString workpath; //!< The path were zTree was ordered to store all its data
};
}
} // namespace lc
#endif // RECEIPTSPRINTER_H

Loading…
Cancel
Save