Browse Source

Initial implementation of salemodel

allow_new_sellers
Martin Brodbeck 4 years ago
parent
commit
e24b6c4e5f
  1. 1
      src/core/article.cpp
  2. 1
      src/core/article.h
  3. 2
      src/core/marketplace.cpp
  4. 1
      src/core/marketplace.h
  5. 7
      src/core/sale.cpp
  6. 1
      src/core/sale.h
  7. 1
      src/gui/CMakeLists.txt
  8. 6
      src/gui/mainwindow.cpp
  9. 2
      src/gui/mainwindow.ui
  10. 164
      src/gui/salemodel.cpp
  11. 27
      src/gui/salemodel.h

1
src/core/article.cpp

@ -24,6 +24,7 @@ bool Article::isSold() { return salePtr_ ? true : false; }
std::string Article::getDescription() { return description_; }
Seller* Article::getSeller() { return sellerPtr_; }
Sale* Article::getSale() { return salePtr_; }
int Article::getPrice() const { return price_; }

1
src/core/article.h

@ -28,6 +28,7 @@ class Article : public Entity
int getArticleNo();
std::string getDescription();
Seller* getSeller();
Sale* getSale();
int getPrice() const;
std::string getPriceAsString() const;

2
src/core/marketplace.cpp

@ -29,6 +29,8 @@ void Marketplace::loadFromDb()
SellersVec& Marketplace::getSellers() { return sellers_; }
SalesVec& Marketplace::getSales() { return sales_; }
int Marketplace::getNextSellerNo()
{
auto iter = std::max_element(

1
src/core/marketplace.h

@ -26,6 +26,7 @@ class Marketplace
void loadFromDb();
SellersVec& getSellers();
SalesVec& getSales();
int getNextSellerNo();
int getNextArticleNo();
int getNumSellersDelete();

7
src/core/sale.cpp

@ -31,6 +31,13 @@ int Sale::sumInCents()
return sum;
}
std::string Sale::sumAsString()
{
std::stringstream sumStream;
sumStream << std::right << std::setw(12) << std::showbase << std::put_money(sumInCents(), false);
return sumStream.str();
}
std::string Sale::getTimestamp() { return timestamp_; }
void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; }

1
src/core/sale.h

@ -20,6 +20,7 @@ class Sale : public Entity
ArticlesVec& getArticles();
std::string getTimestamp();
int sumInCents();
std::string sumAsString();
void removeArticle(const Article* articlePtr);

1
src/gui/CMakeLists.txt

@ -18,6 +18,7 @@ set(GUI_SOURCES
pricedialog.cpp
pricedialog.ui
basketmodel.cpp
salemodel.cpp
)
add_executable(kima2 ${GUI_SOURCES})

6
src/gui/mainwindow.cpp

@ -3,6 +3,7 @@
#include "basketmodel.h"
#include "pricedialog.h"
#include "sellerdialog.h"
#include "salemodel.h"
#include <regex>
@ -22,6 +23,11 @@ MainWindow::MainWindow()
ui_.basketView->setModel(model);
ui_.basketView->setColumnHidden(0, true); // hide the uuid
SaleModel* saleModel = new SaleModel(getMarketplace(), ui_.salesView);
ui_.salesView->setModel(saleModel);
ui_.salesView->setColumnHidden(2, true);
ui_.salesView->resizeColumnToContents(0);
connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit);
connect(ui_.actionEditSeller, &QAction::triggered, this,
&MainWindow::on_actionEditSeller_triggered);

2
src/gui/mainwindow.ui

@ -229,7 +229,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QTreeView" name="treeView"/>
<widget class="QTreeView" name="salesView"/>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">

164
src/gui/salemodel.cpp

@ -0,0 +1,164 @@
#include "salemodel.h"
#include <article.h>
#include <algorithm>
#include <QFont>
SaleModel::SaleModel(Marketplace* market, QObject* parent) : QAbstractItemModel(parent)
{
marketplace_ = market;
}
QModelIndex SaleModel::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!parent.isValid()) {
Sale* sale = marketplace_->getSales().at(row).get();
return createIndex(row, column, sale);
} else if (!parent.parent().isValid()) {
Sale* sale = static_cast<Sale*>(parent.internalPointer());
Article* article = sale->getArticles().at(row);
return createIndex(row, column, article);
} else if (!parent.parent().parent().isValid()) {
Sale* sale = static_cast<Sale*>(parent.internalPointer());
Article* childItem = sale->getArticles().at(row);
if (childItem) {
return createIndex(row, column, childItem);
} else {
return QModelIndex();
}
}
return QModelIndex();
}
QModelIndex SaleModel::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();
Sale* sale{};
Article* article{};
Entity* ent = static_cast<Entity*>(index.internalPointer());
sale = dynamic_cast<Sale*>(ent);
if (sale) {
if (sale == rootItem.get())
return QModelIndex();
else {
/* auto iter =
std::find_if(marketplace_->getSales().begin(), marketplace_->getSales().end(),
[&sale](const auto& s) { return s.get() == sale; });
auto pos = std::distance(marketplace_->getSales().begin(), iter); */
return createIndex(-1, 0, rootItem.get());
}
} else {
// article = static_cast<Article*>(index.internalPointer());
article = dynamic_cast<Article*>(ent);
if (!article) {
return QModelIndex();
}
if (article) {
sale = article->getSale();
auto iter = std::find(sale->getArticles().begin(), sale->getArticles().end(), article);
auto pos = std::distance(sale->getArticles().begin(), iter);
return createIndex(static_cast<int>(pos), 0, sale);
}
}
return QModelIndex();
}
QVariant SaleModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::FontRole) {
QFont myFont;
switch (index.column()) {
case 0:
return myFont;
case 1:
myFont.setFamily("monospace");
return myFont;
return myFont;
default:
return myFont;
}
}
if (role != Qt::DisplayRole)
return QVariant();
if (!index.parent().isValid()) {
Sale* sale = static_cast<Sale*>(index.internalPointer());
switch (index.column()) {
case 0:
return sale->getTimestamp().c_str();
case 1:
return sale->sumAsString().c_str();
case 2:
return sale->getUuidAsString().c_str();
}
}
if (!index.parent().parent().isValid()) {
Article* article = static_cast<Article*>(index.internalPointer());
switch (index.column()) {
case 0:
return article->getArticleNo();
case 1:
return article->getPriceAsString().c_str();
case 2:
return article->getUuidAsString().c_str();
}
}
return QVariant();
}
int SaleModel::rowCount(const QModelIndex& parent) const
{
if (parent.column() > 0)
return 0;
if (!parent.isValid()) {
return marketplace_->getSales().size();
} else if (!parent.parent().isValid()) {
Sale* sale = static_cast<Sale*>(parent.internalPointer());
return sale->getArticles().size();
} else {
return 0;
}
return 0;
}
int SaleModel::columnCount([[maybe_unused]] const QModelIndex& parent) const { return 3; }
QVariant SaleModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return "Zeit / Art.Nr.";
case 1:
return "Preis";
default:
return "???";
}
return QStringLiteral("%1").arg(section);
} else
return "";
}

27
src/gui/salemodel.h

@ -0,0 +1,27 @@
#ifndef SALEMODEL_H
#define SALEMODEL_H
#include <marketplace.h>
#include <QAbstractItemModel>
class SaleModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit SaleModel(Marketplace* market, QObject* parent = nullptr);
QModelIndex index(int row, int column,
const QModelIndex& parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex& index) const override;
QVariant data(const QModelIndex &index, int role) const override;
int rowCount(const QModelIndex& parent) const override;
int columnCount(const QModelIndex& parent) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
private:
Marketplace* marketplace_;
std::unique_ptr<Sale> rootItem{new Sale()};
};
#endif
Loading…
Cancel
Save