kima2/src/gui/settingsdialog.cpp
2022-07-07 15:37:33 +02:00

120 lines
4.8 KiB
C++

#include "settingsdialog.h"
#include "mainwindow.h"
#include <core/database.h>
#include <core/utils.h>
#include <printer/posprinter.h>
#include <printer/utils.h>
#include <exception>
#include <stdexcept>
#include <QMessageBox>
#include <QSettings>
SettingsDialog::SettingsDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
{
m_ui.setupUi(this);
QSettings settings{};
int cashPointNo = settings.value("global/cashPointNo").toInt();
QString commune = settings.value("global/commune", "Dettingen").toString();
QString posPrinterDevice = settings.value("global/posPrinterDevice").toString();
QString posPrinterEndpoint = settings.value("global/posPrinterEndpoint").toString();
int feeInPercent = settings.value("global/feeInPercent").toInt();
int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt();
if (parent)
m_market = dynamic_cast<MainWindow *>(parent)->getMarketplace();
m_ui.cashPointNoSpinBox->setValue(cashPointNo);
m_ui.communeEdit->setText(commune);
m_ui.posPrinterDeviceEdit->setText(posPrinterDevice);
m_ui.posPrinterEndpointEdit->setText(posPrinterEndpoint);
m_ui.feePercentSpinBox->setValue(feeInPercent);
m_ui.maxFeeSpinBox->setValue(maxFeeInEuro);
connect(m_ui.testPosPrinterButton, &QPushButton::clicked, this, [this]() {
using namespace std::string_literals;
try {
if (m_ui.posPrinterDeviceEdit->text().isEmpty()) {
PosPrinter printer;
printer.printTest();
} else {
std::string posPrinterDeviceString = m_ui.posPrinterDeviceEdit->text().toStdString();
std::string posPrinterEndpointString =
m_ui.posPrinterEndpointEdit->text().toStdString();
try {
auto printerDevice =
convertToPosPrinterDevice(posPrinterDeviceString, posPrinterEndpointString);
if (printerDevice) {
PosPrinter printer(*printerDevice);
printer.printTest();
}
} catch (std::exception &) {
QMessageBox(QMessageBox::Icon::Warning, "Falsche Eingabe",
QString("Eingabeformat für Device (hexadezimale IDs): "
"<VendorID>:<ProductID>\nBeispiel: 0416:5011\n "
"Eingabeformat für Endpoint: Hexadezimale Adresse"),
QMessageBox::StandardButton::Ok, this)
.exec();
return;
}
}
} catch (std::runtime_error &err) {
QMessageBox(QMessageBox::Icon::Warning, "Bondrucker Fehler",
QString("Test schlug fehl: ") + err.what(), QMessageBox::StandardButton::Ok,
this)
.exec();
return;
}
});
}
void SettingsDialog::accept()
{
QSettings settings;
int oldCashPointNo = settings.value("global/cashPointNo").toInt();
int newCashPointNo = m_ui.cashPointNoSpinBox->value();
settings.setValue("global/commune", m_ui.communeEdit->text().trimmed());
settings.setValue("global/posPrinterDevice", m_ui.posPrinterDeviceEdit->text().trimmed());
settings.setValue("global/posPrinterEndpoint", m_ui.posPrinterEndpointEdit->text().trimmed());
settings.setValue("global/feeInPercent", m_ui.feePercentSpinBox->value());
settings.setValue("global/maxFeeInEuro", m_ui.maxFeeSpinBox->value());
if (oldCashPointNo != newCashPointNo) {
int result{0};
if (m_market && m_market->getSales().size() > 0) {
result = QMessageBox(QMessageBox::Icon::Question, "Sind Sie sicher?",
"Möchten Sie die Kassen-Nr wirklich ändern? Diese muss über alle "
"Installationen hinweg eindeutig sein.",
QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No,
this)
.exec();
} else {
result = QMessageBox::Yes;
}
if (result == QMessageBox::Yes) {
try {
Database().updateCashPointNo(oldCashPointNo, newCashPointNo);
} catch (std::exception &ex) {
std::string errMsg("Das Ändern der Kassen-Nr. ist fehlgeschlagen: ");
errMsg.append(ex.what());
QMessageBox(QMessageBox::Icon::Critical, "Fehler", errMsg.c_str(),
QMessageBox::StandardButton::Ok, this)
.exec();
QDialog::accept();
return;
}
settings.setValue("global/cashPointNo", m_ui.cashPointNoSpinBox->value());
}
}
QDialog::accept();
}