parent
d0f0d604ed
commit
ee8c059441
@ -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<int>(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>();
|
||||
//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<Article>(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();
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
#ifndef BASKET_MODEL_H
|
||||
#define BASKET_MODEL_H
|
||||
|
||||
#include <marketplace.h>
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
|
||||
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
|
Loading…
Reference in new issue