kima2/src/gui/mainwindow.cpp

144 lines
4.7 KiB
C++
Raw Normal View History

2018-07-14 14:03:35 +02:00
#include "mainwindow.h"
2018-07-22 20:10:22 +02:00
#include "basketmodel.h"
2018-07-21 19:24:56 +02:00
#include "pricedialog.h"
2018-07-21 21:18:22 +02:00
#include "sellerdialog.h"
2018-07-16 12:00:17 +02:00
2018-07-20 22:07:42 +02:00
#include <regex>
2018-07-23 13:39:49 +02:00
#include <QMessageBox>
2018-07-23 12:49:19 +02:00
2018-07-17 10:37:21 +02:00
constexpr int STATUSBAR_TIMEOUT = 5000;
2018-07-14 14:03:35 +02:00
MainWindow::MainWindow()
{
2018-07-14 15:54:29 +02:00
ui_.setupUi(this);
marketplace_ = std::make_unique<Marketplace>();
2018-07-17 10:37:21 +02:00
marketplace_->loadFromDb();
statusBar()->showMessage("Gespeicherte Daten wurden geladen.", STATUSBAR_TIMEOUT);
2018-07-22 20:10:22 +02:00
BasketModel* model = new BasketModel(getMarketplace(), ui_.basketView);
ui_.basketView->setModel(model);
ui_.basketView->setColumnHidden(0, true); // hide the uuid
2018-07-23 12:49:19 +02:00
connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit);
connect(ui_.actionEditSeller, &QAction::triggered, this,
&MainWindow::on_actionEditSeller_triggered);
connect(ui_.sellerNoEdit, &QLineEdit::returnPressed, this,
&MainWindow::on_sellerNoEdit_checkSellerNo);
connect(ui_.paidButton, &QPushButton::clicked, this, &MainWindow::on_paidButton_triggered);
connect(ui_.basketView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
&MainWindow::onBasketViewSelectionChanged);
2018-07-23 13:39:49 +02:00
connect(ui_.cancelArticleButton, &QPushButton::clicked, this,
&MainWindow::onCancelArticleButtonClicked);
connect(ui_.cancelAllArticlesButton, &QPushButton::clicked, this,
&MainWindow::onCancelAllArticlesButtonClicked);
2018-07-14 15:54:29 +02:00
}
2018-07-16 12:00:17 +02:00
void MainWindow::on_actionEditSeller_triggered()
{
auto dialog = std::make_unique<SellerDialog>(this);
2018-07-16 18:04:25 +02:00
int retCode = dialog->exec();
if (retCode == QDialog::Accepted) {
2018-07-18 09:00:46 +02:00
marketplace_->sortSellers();
2018-07-16 18:04:25 +02:00
marketplace_->storeToDb();
2018-07-17 10:37:21 +02:00
statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten gespeichert.",
STATUSBAR_TIMEOUT);
2018-07-16 18:04:25 +02:00
} else {
marketplace_->loadFromDb();
2018-07-17 11:09:35 +02:00
statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten verworfen!",
2018-07-17 10:37:21 +02:00
STATUSBAR_TIMEOUT);
2018-07-16 18:04:25 +02:00
}
2018-07-20 22:07:42 +02:00
}
2018-07-23 08:21:28 +02:00
void MainWindow::on_paidButton_triggered()
{
if (marketplace_->basketSize() > 0) {
dynamic_cast<BasketModel*>(ui_.basketView->model())->finishSale();
}
}
2018-07-20 22:07:42 +02:00
void MainWindow::on_sellerNoEdit_checkSellerNo()
{
using std::regex, std::regex_match, std::smatch;
auto inputText = ui_.sellerNoEdit->text().toStdString();
if (inputText.empty()) {
2018-07-21 21:18:22 +02:00
if (marketplace_->basketSize() > 0) {
2018-07-22 20:10:22 +02:00
dynamic_cast<BasketModel*>(ui_.basketView->model())->finishSale();
2018-07-21 21:18:22 +02:00
}
2018-07-20 22:07:42 +02:00
return;
}
regex pattern{R"(\d{1,5})"};
smatch result;
2018-07-22 20:10:22 +02:00
2018-07-20 22:07:42 +02:00
if (!regex_match(inputText, result, pattern)) {
return;
}
2018-07-21 21:18:22 +02:00
2018-07-20 22:07:42 +02:00
int sellerNo = std::stoi(result[0]);
auto seller = marketplace_->findSellerWithSellerNo(sellerNo);
if (seller) {
2018-07-21 19:24:56 +02:00
PriceDialog priceDialog(this);
auto dialogResult = priceDialog.exec();
2018-07-21 21:18:22 +02:00
if (dialogResult == QDialog::Accepted) {
2018-07-21 19:24:56 +02:00
int price = priceDialog.getPrice();
2018-07-22 20:10:22 +02:00
dynamic_cast<BasketModel*>(ui_.basketView->model())->addArticle(seller, price);
2018-07-21 19:24:56 +02:00
}
2018-07-20 22:07:42 +02:00
}
2018-07-20 22:10:05 +02:00
2018-07-20 22:07:42 +02:00
ui_.sellerNoEdit->clear();
2018-07-23 12:49:19 +02:00
}
void MainWindow::onBasketViewSelectionChanged(const QItemSelection& selected,
2018-07-23 13:39:49 +02:00
[[maybe_unused]] const QItemSelection& deselected)
2018-07-23 12:49:19 +02:00
{
if (selected.size() > 0) {
ui_.cancelArticleButton->setEnabled(true);
} else {
ui_.cancelArticleButton->setEnabled(false);
}
2018-07-23 13:39:49 +02:00
}
void MainWindow::onCancelArticleButtonClicked([[maybe_unused]] bool checked)
{
auto selModel = ui_.basketView->selectionModel();
if (selModel->hasSelection() == false)
return;
auto dlgResult =
QMessageBox(QMessageBox::Icon::Warning, "Sind Sie sicher?",
"Möchten Sie wirklich den Artikel stornieren?",
QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, this)
.exec();
if (dlgResult == QMessageBox::No)
return;
auto indexes = selModel->selectedRows();
std::sort(indexes.begin(), indexes.end());
// Deleting the rows, beginning with the last one!
for (auto iter = indexes.constEnd() - 1; iter >= indexes.constBegin(); --iter) {
ui_.basketView->model()->removeRow(iter->row());
}
}
void MainWindow::onCancelAllArticlesButtonClicked([[maybe_unused]] bool checked)
{
if (ui_.basketView->model()->rowCount() == 0)
return;
auto dlgResult =
QMessageBox(QMessageBox::Icon::Warning, "Sind Sie sicher?",
"Möchten Sie wirklich *alle* Artikel des aktuellen Einkaufs stornieren?",
QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, this)
.exec();
if (dlgResult == QMessageBox::No)
return;
dynamic_cast<BasketModel*>(ui_.basketView->model())->cancelSale();
2018-07-16 12:00:17 +02:00
}