From ee8c059441d2090450efc9d1f3fd9c71f1ec1f7c Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Sun, 22 Jul 2018 20:10:22 +0200 Subject: [PATCH] More on basket --- src/core/marketplace.cpp | 16 +++++++ src/core/marketplace.h | 4 ++ src/core/seller.cpp | 9 ++++ src/core/seller.h | 1 + src/gui/CMakeLists.txt | 1 + src/gui/basketmodel.cpp | 94 ++++++++++++++++++++++++++++++++++++++++ src/gui/basketmodel.h | 29 +++++++++++++ src/gui/mainwindow.cpp | 15 ++++--- src/gui/mainwindow.ui | 21 ++++++++- 9 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 src/gui/basketmodel.cpp create mode 100644 src/gui/basketmodel.h diff --git a/src/core/marketplace.cpp b/src/core/marketplace.cpp index bf77cec..677e95f 100644 --- a/src/core/marketplace.cpp +++ b/src/core/marketplace.cpp @@ -34,6 +34,16 @@ int Marketplace::getNextSellerNo() return (*iter)->getSellerNo() + 1; } +int Marketplace::getNextArticleNo() +{ + auto iter = std::max_element( + sellers_.begin(), sellers_.end(), + [](const auto& a, const auto& b) -> bool { return a->getMaxArticleNo() < b->getMaxArticleNo(); }); + if (iter == sellers_.end()) + return 1; + return (*iter)->getMaxArticleNo() + 1; +} + int Marketplace::getNumSellersDelete() { int count = std::count_if(sellers_.begin(), sellers_.end(), @@ -72,4 +82,10 @@ void Marketplace::finishCurrentSale() } sales_.push_back(std::move(sale)); + basket_.clear(); +} + +BasketVec& Marketplace::getBasket() +{ + return basket_; } \ No newline at end of file diff --git a/src/core/marketplace.h b/src/core/marketplace.h index 9887e4f..7b8c4ea 100644 --- a/src/core/marketplace.h +++ b/src/core/marketplace.h @@ -24,9 +24,13 @@ class Marketplace void storeToDb(bool onlyDelete = false); void loadFromDb(); + SellersVec& getSellers(); int getNextSellerNo(); + int getNextArticleNo(); int getNumSellersDelete(); + BasketVec& getBasket(); + void sortSellers(); Seller* findSellerWithSellerNo(int sellerNo); void addArticleToBasket(std::unique_ptr
article); diff --git a/src/core/seller.cpp b/src/core/seller.cpp index a83ec0d..97b5f82 100644 --- a/src/core/seller.cpp +++ b/src/core/seller.cpp @@ -46,6 +46,15 @@ int Seller::numArticlesSold() const { return static_cast(getArticles(true). int Seller::numArticlesOffered() const { return numArticlesOffered_; } +int Seller::getMaxArticleNo() const{ + auto iter = std::max_element( + articles_.begin(), articles_.end(), + [](const auto& a, const auto& b) -> bool { return a->getArticleNo() < b->getArticleNo(); }); + if (iter == articles_.end()) + return 0; + return (*iter)->getArticleNo(); +} + void Seller::cleanupArticles() { articles_.erase(std::remove_if(articles_.begin(), articles_.end(), diff --git a/src/core/seller.h b/src/core/seller.h index e1da001..c2ab2a7 100644 --- a/src/core/seller.h +++ b/src/core/seller.h @@ -32,6 +32,7 @@ class Seller : public Entity int numArticlesSold() const; // int numArticlesTotal() const; std::vector getArticles(bool onlySold = true) const; + int getMaxArticleNo() const; friend bool operator<(const Seller& li, const Seller& re); friend bool operator<(const std::unique_ptr& li, const std::unique_ptr& re); diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index ce0bab3..fb28766 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -17,6 +17,7 @@ set(GUI_SOURCES sellermodel.cpp pricedialog.cpp pricedialog.ui + basketmodel.cpp ) add_executable(kima2 ${GUI_SOURCES}) diff --git a/src/gui/basketmodel.cpp b/src/gui/basketmodel.cpp new file mode 100644 index 0000000..36d040c --- /dev/null +++ b/src/gui/basketmodel.cpp @@ -0,0 +1,94 @@ +#include "basketmodel.h" + +BasketModel::BasketModel(Marketplace* market, QObject* parent) + : QAbstractTableModel(parent), marketplace_(market) +{ +} + +int BasketModel::rowCount([[maybe_unused]] const QModelIndex& parent) const +{ + return static_cast(marketplace_->basketSize()); +} + +int BasketModel::columnCount([[maybe_unused]] const QModelIndex& parent) const { return 4; } + +QVariant BasketModel::data(const QModelIndex& index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (marketplace_->basketSize() == 0) + return QVariant(); + + Article* article = marketplace_->getBasket().at(index.row()).get(); + + switch (index.column()) { + case 0: + return article->getUuidAsString().c_str(); + case 1: + return article->getArticleNo(); + case 2: + return article->getSeller()->getSellerNo(); + case 3: + return article->getPrice(); + default: + return "???"; + } + return QVariant{}; +} + +QVariant BasketModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Horizontal) { + switch (section) { + case 0: + return "ID"; + case 1: + return "Art.Nr."; + case 2: + return "Verk.Nr."; + case 3: + return "Preis"; + default: + return "???"; + } + return QStringLiteral("%1").arg(section); + } else + return ""; +} + +/* bool BasketModel::insertRows(int row, int count, const QModelIndex& parent) +{ + //emit beginInsertRows(parent, row, row + count - 1); + //auto article = std::make_unique
(); + //article->createUuid(); + //article->setArticleNo(marketplace_->getNextArticleNo()); + //marketplace_->addArticleToBasket(std::move(article)); + //emit endInsertRows(); + + emit layoutChanged(); + + return true; +} */ + +void BasketModel::addArticle(Seller* seller, int price) +{ + emit beginInsertRows(QModelIndex(), marketplace_->getBasket().size(), marketplace_->getBasket().size()); + auto article = std::make_unique
(price); + article->createUuid(); + article->setArticleNo(marketplace_->getNextArticleNo() + marketplace_->getBasket().size()); + article->setSeller(seller); + std::cout << "!!! Neuer Artikel: " << article->getPrice() << " Cent \n"; + marketplace_->addArticleToBasket(std::move(article)); + emit endInsertRows(); +} + +void BasketModel::finishSale() +{ + emit beginRemoveRows(QModelIndex(), 0, marketplace_->getBasket().size()-1); + marketplace_->finishCurrentSale(); + emit endRemoveRows(); +} \ No newline at end of file diff --git a/src/gui/basketmodel.h b/src/gui/basketmodel.h new file mode 100644 index 0000000..1909d35 --- /dev/null +++ b/src/gui/basketmodel.h @@ -0,0 +1,29 @@ +#ifndef BASKET_MODEL_H +#define BASKET_MODEL_H + +#include + +#include + +class BasketModel : public QAbstractTableModel +{ + Q_OBJECT + + public: + explicit BasketModel(Marketplace* market, QObject* parent = nullptr); + virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override; + virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override; + virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + //virtual Qt::ItemFlags flags(const QModelIndex& index) const override; + //virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override; + //virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; + void addArticle(Seller* seller, int price); + void finishSale(); + //virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; + + private: + Marketplace* marketplace_; +}; + +#endif \ No newline at end of file diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 77c063e..01727af 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1,5 +1,6 @@ #include "mainwindow.h" +#include "basketmodel.h" #include "pricedialog.h" #include "sellerdialog.h" @@ -20,6 +21,11 @@ MainWindow::MainWindow() marketplace_ = std::make_unique(); marketplace_->loadFromDb(); statusBar()->showMessage("Gespeicherte Daten wurden geladen.", STATUSBAR_TIMEOUT); + + BasketModel* model = new BasketModel(getMarketplace(), ui_.basketView); + ui_.basketView->setModel(model); + ui_.basketView->setColumnHidden(0, true); // hide the uuid + } void MainWindow::on_actionEditSeller_triggered() @@ -46,14 +52,14 @@ void MainWindow::on_sellerNoEdit_checkSellerNo() if (inputText.empty()) { if (marketplace_->basketSize() > 0) { - marketplace_->finishCurrentSale(); + dynamic_cast(ui_.basketView->model())->finishSale(); } return; } regex pattern{R"(\d{1,5})"}; smatch result; - + if (!regex_match(inputText, result, pattern)) { return; } @@ -66,10 +72,7 @@ void MainWindow::on_sellerNoEdit_checkSellerNo() auto dialogResult = priceDialog.exec(); if (dialogResult == QDialog::Accepted) { int price = priceDialog.getPrice(); - auto article = std::make_unique
(price); - article->setSeller(seller); - std::cout << "!!! Neuer Artikel: " << article->getPrice() << " Cent \n"; - marketplace_->addArticleToBasket(std::move(article)); + dynamic_cast(ui_.basketView->model())->addArticle(seller, price); } } diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index f1d128b..8e3c4c4 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -170,7 +170,26 @@ - + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + false + + + 75 + + + true + + + false + +