Reformat ReceiptsPrinter

remotes/origin/HEAD
markuspg 7 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. * This file is part of Labcontrol.
* *
@ -22,20 +22,20 @@
#include "receiptsprinter.h" #include "receiptsprinter.h"
#include "settings.h" #include "settings.h"
extern std::unique_ptr< lc::Settings > settings; extern std::unique_ptr<lc::Settings> settings;
lc::ReceiptsPrinter::ReceiptsPrinter( const QString &argDateString, lc::ReceiptsPrinter::ReceiptsPrinter(const QString &argDateString,
const QString &argWorkpath, const QString &argWorkpath,
QObject *argParent ) : QObject *argParent) :
QThread{ argParent }, QThread{argParent},
dateString{ argDateString }, dateString{argDateString},
dvipsCmd{ settings->dvipsCmd }, dvipsCmd{settings->dvipsCmd},
latexCmd{ settings->latexCmd }, latexCmd{settings->latexCmd},
lprCmd{ settings->lprCmd }, lprCmd{settings->lprCmd},
postscriptViewer{ settings->postscriptViewer }, postscriptViewer{ settings->postscriptViewer},
ps2pdfCmd{ settings->ps2pdfCmd }, ps2pdfCmd{settings->ps2pdfCmd},
rmCmd{ settings->rmCmd }, rmCmd{settings->rmCmd},
vncViewer{ settings->vncViewer }, vncViewer{settings->vncViewer},
workpath{ argWorkpath } workpath{argWorkpath}
{ {
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2016 Markus Prasser * Copyright 2014-2018 Markus Prasser, Tobias Weiss
* *
* This file is part of Labcontrol. * 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. 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 Q_OBJECT
void run() Q_DECL_OVERRIDE { void run() override
{
// Compile the TeX file to dvi // Compile the TeX file to dvi
QStringList arguments; QStringList arguments;
arguments << "-interaction" << "batchmode" << QString{ dateString + ".tex" }; arguments << "-interaction" << "batchmode" << QString{dateString + ".tex"};
QProcess *process = nullptr; QProcess *process = nullptr;
process = new QProcess{}; process = new QProcess{};
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); const QProcessEnvironment env{QProcessEnvironment::systemEnvironment()};
process->setProcessEnvironment( env ); process->setProcessEnvironment(env);
process->setWorkingDirectory( workpath ); process->setWorkingDirectory(workpath);
process->start( latexCmd, arguments ); process->start(latexCmd, arguments);
if( !process->waitForFinished( processTimeOut ) ) { 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 }; 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(); message_box.exec();
delete process; delete process;
process = nullptr; process = nullptr;
@ -58,14 +63,15 @@ class ReceiptsPrinter : public QThread {
// Convert the dvi file to postscript // Convert the dvi file to postscript
arguments = QStringList{}; arguments = QStringList{};
arguments << "-q*" << "-o" << QString{ dateString + ".ps" } << QString{ dateString + ".dvi" }; arguments << "-q*" << "-o" << QString{dateString + ".ps"} << QString{dateString + ".dvi"};
process = new QProcess{}; process = new QProcess{};
process->setProcessEnvironment( env ); process->setProcessEnvironment(env);
process->setWorkingDirectory( workpath ); process->setWorkingDirectory(workpath);
process->start( dvipsCmd, arguments ); process->start(dvipsCmd, arguments);
if( !process->waitForFinished( processTimeOut ) ) { 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" } ); 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; delete process;
process = nullptr; process = nullptr;
return; return;
@ -74,64 +80,68 @@ class ReceiptsPrinter : public QThread {
process = nullptr; process = nullptr;
// Print the postscript file // Print the postscript file
if ( !lprCmd.isEmpty() ) { if (!lprCmd.isEmpty()) {
arguments = QStringList{}; arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".ps" }; arguments << QString{workpath + "/" + dateString + ".ps"};
process = new QProcess{}; process = new QProcess{};
process->setProcessEnvironment( env ); process->setProcessEnvironment(env);
process->setWorkingDirectory( workpath ); process->setWorkingDirectory(workpath);
process->start( lprCmd, arguments ); process->start(lprCmd, arguments);
if( !process->waitForFinished( processTimeOut ) ) { if ( !process->waitForFinished( processTimeOut ) ) {
emit ErrorOccurred( new QString{ "The receipts postscript file was successfully created but could not be printed." }, new QString{ "Printing failed" } ); emit ErrorOccurred(new QString{"The receipts postscript file was successfully created but could not be printed."},
new QString{"Printing failed"});
} }
delete process; delete process;
process = nullptr; process = nullptr;
} }
// Convert the postscript file to pdf // Convert the postscript file to pdf
if ( !ps2pdfCmd.isEmpty() ) { if (!ps2pdfCmd.isEmpty()) {
arguments = QStringList{}; arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".ps" } << QString{ workpath + "/" + dateString + ".pdf" }; arguments << QString{workpath + "/" + dateString + ".ps"} << QString{workpath + "/" + dateString + ".pdf"};
process = new QProcess{}; process = new QProcess{};
process->setProcessEnvironment( env ); process->setProcessEnvironment(env);
process->setWorkingDirectory( workpath ); process->setWorkingDirectory(workpath);
process->start( ps2pdfCmd, arguments ); process->start(ps2pdfCmd, arguments);
if( !process->waitForFinished( processTimeOut ) ) { 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" } ); emit ErrorOccurred(new QString{"The receipts were successfully printed but the creation of the PDF file failed."},
new QString{"PDF creation failed"});
} }
delete process; delete process;
process = nullptr; process = nullptr;
// Show the postscript file if the conversion succeeded // Show the postscript file if the conversion succeeded
if ( !postscriptViewer.isEmpty() ) { if (!postscriptViewer.isEmpty()) {
arguments = QStringList{}; arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".ps" }; arguments << QString{workpath + "/" + dateString + ".ps"};
process = new QProcess{}; process = new QProcess{};
process->setProcessEnvironment( env ); process->setProcessEnvironment(env);
process->setWorkingDirectory( workpath ); process->setWorkingDirectory(workpath);
process->startDetached( postscriptViewer, arguments ); process->startDetached(postscriptViewer, arguments);
delete process; delete process;
process = nullptr; process = nullptr;
} }
} }
// Clean up the zTree working path // Clean up the zTree working path
if ( !rmCmd.isEmpty() ) { if (!rmCmd.isEmpty()) {
arguments = QStringList{}; arguments = QStringList{};
arguments << QString{ workpath + "/" + dateString + ".aux" } arguments << QString{workpath + "/" + dateString + ".aux"}
<< QString{ workpath + "/" + dateString + ".dvi" } << QString{workpath + "/" + dateString + ".dvi"}
<< QString{ workpath + "/" + dateString + ".log" } << QString{workpath + "/" + dateString + ".log"}
<< QString{ workpath + "/" + dateString + ".tex" }; << QString{workpath + "/" + dateString + ".tex"};
process = new QProcess{}; process = new QProcess{};
process->setProcessEnvironment( env ); process->setProcessEnvironment(env);
process->setWorkingDirectory( workpath ); process->setWorkingDirectory(workpath);
process->start( rmCmd, arguments); process->start( rmCmd, arguments);
if( !process->waitForFinished( processTimeOut ) ) { 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")); 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; delete process;
process = nullptr; process = nullptr;
@ -140,9 +150,9 @@ class ReceiptsPrinter : public QThread {
emit PrintingFinished(); emit PrintingFinished();
} }
public: public:
explicit ReceiptsPrinter( const QString &argDateString, explicit ReceiptsPrinter(const QString &argDateString,
const QString &argWorkpath, const QString &argWorkpath,
QObject *argParent = nullptr ); QObject *argParent = nullptr);
signals: signals:
void ErrorOccurred(QString *error_message, QString *heading); void ErrorOccurred(QString *error_message, QString *heading);
@ -154,13 +164,14 @@ private:
const QString latexCmd; const QString latexCmd;
const QString lprCmd; const QString lprCmd;
const QString postscriptViewer; 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 ps2pdfCmd;
const QString rmCmd; const QString rmCmd;
const QString vncViewer; const QString vncViewer;
const QString workpath; //!< The path were zTree was ordered to store all its data const QString workpath; //!< The path were zTree was ordered to store all its data
}; };
} } // namespace lc
#endif // RECEIPTSPRINTER_H #endif // RECEIPTSPRINTER_H

Loading…
Cancel
Save