From 4a92832e19e03d4eec4a6cbcdb089470f26d2764 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Mon, 6 Aug 2018 20:57:12 +0200 Subject: [PATCH] printer bugfixes --- src/gui/mainwindow.cpp | 3 +- src/gui/settingsdialog.cpp | 19 +++++++----- src/printer/posprinter.cpp | 60 +++++++++++++++++++++++--------------- src/printer/posprinter.h | 1 + 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 93f52b8..e9014e1 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -238,7 +238,8 @@ void MainWindow::onPrintSaleReceiptButtonClicked([[maybe_unused]] bool checked) auto indexes = selModel->selectedRows(); auto& sale = marketplace_->getSales().at(indexes[0].row()); PosPrinter printer; - printer.printSaleReceipt(sale.get()); + if (printer.isValid()) + printer.printSaleReceipt(sale.get()); } void MainWindow::onCancelAllArticlesButtonClicked([[maybe_unused]] bool checked) diff --git a/src/gui/settingsdialog.cpp b/src/gui/settingsdialog.cpp index 3506238..b3a4a3a 100644 --- a/src/gui/settingsdialog.cpp +++ b/src/gui/settingsdialog.cpp @@ -14,12 +14,12 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par QSettings settings{}; int cashPointNo = settings.value("global/cashPointNo").toInt(); - //QString posPrinterDevice = settings.value("global/posPrinterDevice").toString(); + // QString posPrinterDevice = settings.value("global/posPrinterDevice").toString(); int feeInPercent = settings.value("global/feeInPercent").toInt(); int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt(); ui_.cashPointNoSpinBox->setValue(cashPointNo); - //ui_.posPrinterDeviceEdit->setText(posPrinterDevice); + // ui_.posPrinterDeviceEdit->setText(posPrinterDevice); ui_.feePercentSpinBox->setValue(feeInPercent); ui_.maxFeeSpinBox->setValue(maxFeeInEuro); @@ -28,14 +28,19 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par try { PosPrinter printer; printer.printTest(); + if (printer.isValid()) + this->ui_.posPrinterDeviceEdit->setText(""); + else + this->ui_.posPrinterDeviceEdit->setText(""); + } catch (std::runtime_error& err) { QMessageBox(QMessageBox::Icon::Warning, "Bondrucker Fehler", - QString("Test schlug fehl: ") + err.what(), QMessageBox::StandardButton::Ok, this) + QString("Test schlug fehl: ") + err.what(), QMessageBox::StandardButton::Ok, + this) .exec(); - this->ui_.posPrinterDeviceEdit->setText(""); - return; + this->ui_.posPrinterDeviceEdit->setText(""); + return; } - this->ui_.posPrinterDeviceEdit->setText(""); }); } @@ -46,7 +51,7 @@ void SettingsDialog::accept() int oldCashPointNo = settings.value("global/cashPointNo").toInt(); int newCashPointNo = ui_.cashPointNoSpinBox->value(); - //settings.setValue("global/posPrinterDevice", ui_.posPrinterDeviceEdit->text()); + // settings.setValue("global/posPrinterDevice", ui_.posPrinterDeviceEdit->text()); settings.setValue("global/feeInPercent", ui_.feePercentSpinBox->value()); settings.setValue("global/maxFeeInEuro", ui_.maxFeeSpinBox->value()); diff --git a/src/printer/posprinter.cpp b/src/printer/posprinter.cpp index 8e33bc5..a9a2cfc 100644 --- a/src/printer/posprinter.cpp +++ b/src/printer/posprinter.cpp @@ -2,7 +2,7 @@ #include -#include "boost/date_time/posix_time/posix_time.hpp" +#include #include #include @@ -29,10 +29,6 @@ PosPrinter::PosPrinter() throw std::runtime_error("Init error"); } -#if defined(_WIN32) - // libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK); -#endif - libusb_device** devList; int devCount = libusb_get_device_list(contextPtr_, &devList); if (devCount <= 0) { @@ -52,7 +48,6 @@ PosPrinter::PosPrinter() } if (numDevice < 0) { - libusb_exit(contextPtr_); return; } @@ -81,16 +76,20 @@ PosPrinter::PosPrinter() PosPrinter::~PosPrinter() { - int retValue; - retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface - if (retValue != 0) { - std::cout << "Cannot release printer interface" << std::endl; - } else { - std::cout << "Printer interface released" << std::endl; - } + if (devicePtr_) { + int retValue; + retValue = libusb_release_interface(devicePtr_, 0); // release the claimed interface + if (retValue != 0) { + std::cout << "Cannot release printer interface" << std::endl; + } else { + std::cout << "Printer interface released" << std::endl; + } - libusb_close(devicePtr_); // close the device we opened - libusb_exit(contextPtr_); // close the session + libusb_close(devicePtr_); // close the device we opened + } + if (contextPtr_) { + libusb_exit(contextPtr_); // close the session + } } void PosPrinter::write(const std::string& text) @@ -147,8 +146,9 @@ void PosPrinter::printSaleReceipt(Sale* sale) commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN; commandStream << sale->getTimestamp() << "\n\n"; commandStream << Command::LEFT_ALIGN; - for(const auto& article : sale->getArticles()) { - commandStream << "Art. " << article->getCompleteArticleNo() << "........... " << article->getPriceAsString() << "\n"; + for (const auto& article : sale->getArticles()) { + commandStream << "Art. " << article->getCompleteArticleNo() << "........... " + << article->getPriceAsString() << "\n"; } commandStream << "\nGesamt................. " << sale->sumAsString() << "\n"; commandStream << Command::CENTERING; @@ -162,22 +162,36 @@ void PosPrinter::printSellerReceipt(Seller* seller, int percent, int maxFee) std::stringstream commandStream; printHeader(); commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN; - std::string timeStr = boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); + std::string timeStr = + boost::posix_time::to_simple_string(boost::posix_time::second_clock::local_time()); commandStream << timeStr << "\n\n"; commandStream << Command::LEFT_ALIGN; commandStream << "Verkäufernummer: " << std::setw(4) << seller->getSellerNo() << "\n\n"; - commandStream << "Anzahl Artikel geliefert: " << std::setw(4) << seller->numArticlesOffered() << "\n"; - commandStream << "Anzahl Artikel verkauft: " << std::setw(4) << seller->numArticlesSold() << "\n\n"; + commandStream << "Anzahl Artikel geliefert: " << std::setw(4) << seller->numArticlesOffered() + << "\n"; + commandStream << "Anzahl Artikel verkauft: " << std::setw(4) << seller->numArticlesSold() + << "\n\n"; if (seller->getArticles(true).size() == 0) { commandStream << "Verkaufte Artikel:\n *** keine ***\n"; } else { commandStream << "Verkaufte Artikel:\n"; } for (auto& article : seller->getArticles(true)) { - commandStream << "Art. " << article->getCompleteArticleNo() << "........... " << article->getPriceAsString() << "\n"; + commandStream << "Art. " << article->getCompleteArticleNo() << "........... " + << article->getPriceAsString() << "\n"; } commandStream << "\nGesamt................." << seller->sumAsString() << "\n"; - commandStream << "./. Gebühr............." << marketFeeAsString(seller->sumInCents(), percent, maxFee * 100) << "\n"; - commandStream << "\nAuszahlung............." << paymentAsString(seller->sumInCents(), percent, maxFee * 100) << "\n"; + commandStream << "./. Gebühr............." + << marketFeeAsString(seller->sumInCents(), percent, maxFee * 100) << "\n"; + commandStream << "\nAuszahlung............." + << paymentAsString(seller->sumInCents(), percent, maxFee * 100) << "\n"; commandStream << Command::FEED; +} + +bool PosPrinter::isValid() +{ + if (devicePtr_) + return true; + else + return false; } \ No newline at end of file diff --git a/src/printer/posprinter.h b/src/printer/posprinter.h index a2ac543..eda0332 100644 --- a/src/printer/posprinter.h +++ b/src/printer/posprinter.h @@ -26,6 +26,7 @@ class PosPrinter void printTest(); void printSaleReceipt(Sale* sale); void printSellerReceipt(Seller* seller, int percent, int maxFee); + bool isValid(); struct Command { static const std::string RESET;