Reformat ReceiptsHandler

remotes/origin/HEAD
markuspg 6 years ago
parent 2ec99238fe
commit f5f32c4bcf

@ -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.
* *
@ -70,7 +70,8 @@ lc::ReceiptsHandler::ReceiptsHandler( const QString &argZTreeDataTargetPath,
PrintReceipts(); PrintReceipts();
} }
void lc::ReceiptsHandler::PrintReceipts() { void lc::ReceiptsHandler::PrintReceipts()
{
// If the payment file exists, print it // If the payment file exists, print it
if (paymentFile.exists()) { if (paymentFile.exists()) {
qDebug() << "The payment file has been created and will be printed"; qDebug() << "The payment file has been created and will be printed";
@ -82,12 +83,14 @@ void lc::ReceiptsHandler::PrintReceipts() {
} }
} }
void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() { void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile()
{
// Get the data needed for receipts creation from the payment file // Get the data needed for receipts creation from the payment file
QVector<QString> *rawParticipantsData = nullptr; QVector<QString> *rawParticipantsData = nullptr;
rawParticipantsData = GetParticipantsDataFromPaymentFile(); rawParticipantsData = GetParticipantsDataFromPaymentFile();
for (int i = 0; i < rawParticipantsData->size(); i++) { for (int i = 0; i < rawParticipantsData->size(); i++) {
qDebug() << "Payment file line" << QString::number( i ) << ":\t" << rawParticipantsData->at( i ); qDebug() << "Payment file line" << QString::number(i)
<< ":\t" << rawParticipantsData->at(i);
} }
// Extract the data of the participant's whose receipts shall be printed // Extract the data of the participant's whose receipts shall be printed
@ -96,14 +99,14 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
*/ */
QVector<paymentEntry_t *> *participants = new QVector<paymentEntry_t *>; QVector<paymentEntry_t *> *participants = new QVector<paymentEntry_t *>;
double overall_payoff = 0.0; double overall_payoff = 0.0;
for ( QVector<QString>::iterator it = rawParticipantsData->begin(); it != rawParticipantsData->end() - 1; ++it ) { for (QVector<QString>::iterator it = rawParticipantsData->begin();
it != rawParticipantsData->end() - 1; ++it) {
// Split the lines containing the participants' data into their inidivual parts // Split the lines containing the participants' data into their inidivual parts
QStringList temp_participant_data = it->split('\t', QString::KeepEmptyParts); QStringList temp_participant_data = it->split('\t', QString::KeepEmptyParts);
qDebug() << temp_participant_data.join(" - "); qDebug() << temp_participant_data.join(" - ");
if (!printReceiptsForLocalClients && temp_participant_data.at(3).contains("local")) { if (!printReceiptsForLocalClients && temp_participant_data.at(3).contains("local")) {
qDebug() << "Receipt for local client" << temp_participant_data.at(1) << "will not be printed."; qDebug() << "Receipt for local client" << temp_participant_data.at(1) << "will not be printed.";
} } else {
else {
// Create a new struct instance for participant data and fill it // Create a new struct instance for participant data and fill it
paymentEntry_t *participant = new paymentEntry_t; paymentEntry_t *participant = new paymentEntry_t;
participant->computer = temp_participant_data.at(1); participant->computer = temp_participant_data.at(1);
@ -136,7 +139,8 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
latexText->append("\n\\COMPREHENSION{\n"); latexText->append("\n\\COMPREHENSION{\n");
unsigned short int zeile = 0; unsigned short int zeile = 0;
for (auto s : *participants) { for (auto s : *participants) {
latexText->append( expectedPaymentFileName + " & " + s->computer + " & " + s->name + " & " + QString::number( s->payoff, 'f', 2 ) + " \\EUR\\\\\n" ); latexText->append(expectedPaymentFileName + " & " + s->computer + " & " + s->name + " & " +
QString::number(s->payoff, 'f', 2) + " \\EUR\\\\\n");
if (zeile % 2 == 0) { if (zeile % 2 == 0) {
latexText->append("\\rowcolor[gray]{0.9}\n"); latexText->append("\\rowcolor[gray]{0.9}\n");
} }
@ -156,10 +160,11 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
// Write the single receipts // Write the single receipts
for (auto s : *participants) { for (auto s : *participants) {
if (s->payoff >= 0) { if (s->payoff >= 0) {
latexText->append( "\\GAINRECEIPT{" + expectedPaymentFileName + "}{" + s->computer + "}{" + s->name + "}{" + QString::number( s->payoff, 'f', 2 ) + "}\n" ); latexText->append("\\GAINRECEIPT{" + expectedPaymentFileName + "}{" + s->computer + "}{" + s->name
} + "}{" + QString::number(s->payoff, 'f', 2) + "}\n");
else { } else {
latexText->append( "\\LOSSRECEIPT{" + expectedPaymentFileName + "}{" + s->computer + "}{" + s->name + "}{" + QString::number( s->payoff, 'f', 2 ) + "}\n" ); latexText->append("\\LOSSRECEIPT{" + expectedPaymentFileName + "}{" + s->computer + "}{" + s->name
+ "}{" + QString::number(s->payoff, 'f', 2) + "}\n");
} }
delete s; delete s;
} }
@ -177,14 +182,16 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
// Clean up any already existing files // Clean up any already existing files
if (texFile->exists()) { if (texFile->exists()) {
if (!texFile->remove()) { if (!texFile->remove()) {
QMessageBox messageBox( QMessageBox::Critical, "Tex file removing failed", "There already exists a tex file at '" + texFile->fileName() QMessageBox messageBox(QMessageBox::Critical, "Tex file removing failed",
"There already exists a tex file at '" + texFile->fileName()
+ "' which cannot be removed. The creation of the receipts printout may fail.", QMessageBox::Ok); + "' which cannot be removed. The creation of the receipts printout may fail.", QMessageBox::Ok);
messageBox.exec(); messageBox.exec();
} }
} }
// Create a new file // Create a new file
if (!texFile->open(QIODevice::Text | QIODevice::WriteOnly)) { if (!texFile->open(QIODevice::Text | QIODevice::WriteOnly)) {
QMessageBox messageBox( QMessageBox::Critical, "Tex file creation failed", "The creation of the tex file for receipts printing at '" + texFile->fileName() QMessageBox messageBox( QMessageBox::Critical, "Tex file creation failed",
"The creation of the tex file for receipts printing at '" + texFile->fileName()
+ "' failed. Receipts printing will not work.", QMessageBox::Ok); + "' failed. Receipts printing will not work.", QMessageBox::Ok);
messageBox.exec(); messageBox.exec();
return; return;
@ -209,7 +216,8 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
delete texFile; delete texFile;
} }
void lc::ReceiptsHandler::DeleteReceiptsPrinterInstance() { void lc::ReceiptsHandler::DeleteReceiptsPrinterInstance()
{
receiptsPrinter->quit(); receiptsPrinter->quit();
receiptsPrinter->wait(); receiptsPrinter->wait();
receiptsPrinter->deleteLater(); receiptsPrinter->deleteLater();
@ -219,14 +227,18 @@ void lc::ReceiptsHandler::DeleteReceiptsPrinterInstance() {
emit PrintingFinished(); emit PrintingFinished();
} }
void lc::ReceiptsHandler::DisplayMessageBox( QString *argErrorMessage, QString *argHeading ) { void lc::ReceiptsHandler::DisplayMessageBox(QString *argErrorMessage,
QMessageBox messageBox( QMessageBox::Warning, *argHeading, *argErrorMessage, QMessageBox::Ok ); QString *argHeading)
{
QMessageBox messageBox(QMessageBox::Warning, *argHeading,
*argErrorMessage, QMessageBox::Ok);
delete argHeading; delete argHeading;
delete argErrorMessage; delete argErrorMessage;
messageBox.exec(); messageBox.exec();
} }
QVector<QString> *lc::ReceiptsHandler::GetParticipantsDataFromPaymentFile() { QVector<QString> *lc::ReceiptsHandler::GetParticipantsDataFromPaymentFile()
{
// Create the vector to store the single lines of the file // Create the vector to store the single lines of the file
QVector<QString> *participantsData = new QVector<QString>; QVector<QString> *participantsData = new QVector<QString>;
@ -253,12 +265,15 @@ QVector<QString> *lc::ReceiptsHandler::GetParticipantsDataFromPaymentFile() {
return participantsData; return participantsData;
} }
QString *lc::ReceiptsHandler::LoadLatexHeader() { QString *lc::ReceiptsHandler::LoadLatexHeader()
{
// Prepare all facilities to read the latex header file // Prepare all facilities to read the latex header file
QFile latexHeaderFile(settings->lcDataDir + "/" + latexHeaderName + "_header.tex"); QFile latexHeaderFile(settings->lcDataDir + "/" + latexHeaderName + "_header.tex");
if (!latexHeaderFile.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!latexHeaderFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox messageBox{ QMessageBox::Critical, tr( "LaTeX header could not be loaded" ), QMessageBox messageBox{QMessageBox::Critical,
tr( "The LaTeX header at '%1/%2_header.tex' could not be loaded. Receipts printing will not work." ) tr("LaTeX header could not be loaded"),
tr("The LaTeX header at '%1/%2_header.tex' could"
" not be loaded. Receipts printing will not work.")
.arg(settings->lcDataDir).arg(latexHeaderName), QMessageBox::Ok}; .arg(settings->lcDataDir).arg(latexHeaderName), QMessageBox::Ok};
messageBox.exec(); messageBox.exec();
return nullptr; return nullptr;
@ -273,16 +288,19 @@ QString *lc::ReceiptsHandler::LoadLatexHeader() {
return header; return header;
} }
void lc::ReceiptsHandler::MakeReceiptsAnonymous( QVector<paymentEntry_t*> *argDataVector, bool argAlsoAnonymizeClients ) { void lc::ReceiptsHandler::MakeReceiptsAnonymous(QVector<paymentEntry_t *> *argDataVector,
bool argAlsoAnonymizeClients)
{
if (!argAlsoAnonymizeClients) { if (!argAlsoAnonymizeClients) {
qDebug() << "Names are made anonymous"; qDebug() << "Names are made anonymous";
for ( QVector< paymentEntry_t* >::iterator it = argDataVector->begin(); it != argDataVector->end(); ++it ) { for (QVector<paymentEntry_t *>::iterator it = argDataVector->begin(); it != argDataVector->end();
++it) {
(*it)->name = anonymousReceiptsPlaceholder; (*it)->name = anonymousReceiptsPlaceholder;
} }
} } else {
else {
qDebug() << "Clients and names are made anonymous"; qDebug() << "Clients and names are made anonymous";
for ( QVector< paymentEntry_t* >::iterator it = argDataVector->begin(); it != argDataVector->end(); ++it ) { for (QVector<paymentEntry_t *>::iterator it = argDataVector->begin(); it != argDataVector->end();
++it) {
(*it)->name = anonymousReceiptsPlaceholder; (*it)->name = anonymousReceiptsPlaceholder;
(*it)->computer = "\\hspace{1cm}"; (*it)->computer = "\\hspace{1cm}";
} }

@ -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.
* *
@ -36,13 +36,18 @@ namespace lc {
/*! /*!
This class represents a single payoff entry which will be used in the receipts creation process. Multiple instances of this will be used to represent the individual participants' outcomes. This class represents a single payoff entry which will be used in the receipts creation process. Multiple instances of this will be used to represent the individual participants' outcomes.
*/ */
struct paymentEntry_t {QString computer; QString name; double payoff;}; struct paymentEntry_t {
QString computer;
QString name;
double payoff;
};
//! A class to handle receipts printing. //! A class to handle receipts printing.
/*! /*!
This class is element of every session and is used to handle the receipts printing. This class is element of every session and is used to handle the receipts printing.
*/ */
class ReceiptsHandler : public QObject { class ReceiptsHandler : public QObject
{
Q_OBJECT Q_OBJECT
public: public:
@ -74,20 +79,31 @@ private:
void CreateReceiptsFromPaymentFile(); void CreateReceiptsFromPaymentFile();
QVector<QString> *GetParticipantsDataFromPaymentFile(); QVector<QString> *GetParticipantsDataFromPaymentFile();
QString *LoadLatexHeader(); QString *LoadLatexHeader();
void MakeReceiptsAnonymous( QVector<paymentEntry_t*> *argDataVector, bool argAlsoAnonymizeClients ); void MakeReceiptsAnonymous(QVector<paymentEntry_t *> *argDataVector,
bool argAlsoAnonymizeClients);
const QString anonymousReceiptsPlaceholder; //!< Placeholder which shall be inserted for participant names if anonymous printing is desired (QString != "") //! Placeholder which shall be inserted for participant names if anonymous printing is desired (QString != "")
const QString dateString; //!< The expected date string of the payment file in form 'yyMMdd_hhmm' const QString anonymousReceiptsPlaceholder;
const QString expectedPaymentFileName; //!< The name of the expected payment file //! The expected date string of the payment file in form 'yyMMdd_hhmm'
const QString expectedPaymentFilePath; //!< The path of the expected payment file const QString dateString;
const QString latexHeaderName; //!< The name of the chosen LaTeX header template //! The name of the expected payment file
QFile paymentFile; //!< A pointer to the '*.pay' file being watched for existance and starting the printing process const QString expectedPaymentFileName;
const bool printReceiptsForLocalClients; //!< Stores if receipts shall be printed for local clients //! The path of the expected payment file
ReceiptsPrinter *receiptsPrinter = nullptr; //!< Creates new thread for receipts printing const QString expectedPaymentFilePath;
QTimer *timer = nullptr; //!< Used for regular checking if the payment file was created //! The name of the chosen LaTeX header template
const QString zTreeDataTargetPath; //!< A reference to the data target path stored in the session class instance const QString latexHeaderName;
//! A pointer to the '*.pay' file being watched for existance and starting the printing process
QFile paymentFile;
//! Stores if receipts shall be printed for local clients
const bool printReceiptsForLocalClients;
//! Creates new thread for receipts printing
ReceiptsPrinter *receiptsPrinter = nullptr;
//! Used for regular checking if the payment file was created
QTimer *timer = nullptr;
//! A reference to the data target path stored in the session class instance
const QString zTreeDataTargetPath;
}; };
} } // namespace lc
#endif // RECEIPTS_HANDLER_H #endif // RECEIPTS_HANDLER_H

Loading…
Cancel
Save