kima2/src/gui/basketmodel.cpp

137 lines
3.9 KiB
C++
Raw Normal View History

2018-07-22 20:10:22 +02:00
#include "basketmodel.h"
2018-07-23 21:21:05 +02:00
#include <QFont>
#include <QSettings>
2018-08-08 09:16:23 +02:00
#include <QFontDatabase>
2018-07-23 21:21:05 +02:00
2018-07-22 20:10:22 +02:00
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
{
2018-07-23 21:21:05 +02:00
if (role == Qt::FontRole) {
QFont myFont;
QFont myFixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
2018-10-23 14:13:17 +02:00
if (myFixedFont.fixedPitch() == false) {
myFixedFont.setFamily("monospace");
}
2018-07-23 21:21:05 +02:00
switch (index.column()) {
case 0:
[[fallthrough]];
case 1:
return myFixedFont;
2018-07-23 21:21:05 +02:00
case 2:
2018-10-08 09:52:41 +02:00
myFixedFont.setPointSize(myFixedFont.pointSize() + 3);
return myFixedFont;
2018-07-23 21:21:05 +02:00
case 3:
2018-10-08 09:52:41 +02:00
myFixedFont.setPointSize(myFixedFont.pointSize() + 3);
myFixedFont.setBold(true);
return myFixedFont;
default:
return myFont;
2018-07-23 21:21:05 +02:00
}
}
2018-07-22 20:10:22 +02:00
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:
2018-07-30 14:43:02 +02:00
return article->getCompleteArticleNo().c_str();
2018-07-22 20:10:22 +02:00
case 2:
return article->getSeller()->getSellerNo();
case 3:
2018-07-23 21:21:05 +02:00
// return article->getPrice();
2018-07-23 14:25:18 +02:00
return article->getPriceAsString().c_str();
2018-07-22 20:10:22 +02:00
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 "";
}
void BasketModel::addArticle(Seller* seller, int price, const std::string& desc)
2018-07-22 20:10:22 +02:00
{
2018-07-23 13:39:49 +02:00
emit beginInsertRows(QModelIndex(), marketplace_->getBasket().size(),
marketplace_->getBasket().size());
2018-07-22 20:10:22 +02:00
auto article = std::make_unique<Article>(price);
article->createUuid();
article->setDescription(desc);
2018-07-23 13:39:49 +02:00
article->setArticleNo(marketplace_->getNextArticleNo());
article->setSourceNo(QSettings().value("global/cashPointNo").toInt());
2018-07-22 20:10:22 +02:00
article->setSeller(seller);
marketplace_->addArticleToBasket(std::move(article));
emit endInsertRows();
}
void BasketModel::finishSale()
{
2018-07-23 13:39:49 +02:00
emit beginRemoveRows(QModelIndex(), 0, marketplace_->getBasket().size() - 1);
2018-07-30 14:43:02 +02:00
auto sale = std::make_unique<Sale>();
sale->createUuid();
sale->setSourceNo(QSettings().value("global/cashPointNo").toInt());
marketplace_->finishCurrentSale(std::move(sale));
2018-07-22 20:10:22 +02:00
emit endRemoveRows();
2018-07-26 08:34:01 +02:00
emit basketDataChanged();
2018-07-23 13:39:49 +02:00
}
void BasketModel::cancelSale()
{
emit beginRemoveRows(QModelIndex(), 0, marketplace_->getBasket().size() - 1);
marketplace_->getBasket().clear();
emit endRemoveRows();
}
bool BasketModel::removeRows(int row, int count, const QModelIndex& parent)
{
auto article = marketplace_->getBasket().at(row).get();
emit beginRemoveRows(parent, row, row + count - 1);
marketplace_->getBasket().erase(
std::remove_if(marketplace_->getBasket().begin(), marketplace_->getBasket().end(),
[&article](const auto& a) { return a->getUuid() == article->getUuid(); }),
marketplace_->getBasket().end());
emit endRemoveRows();
return true;
2018-10-23 14:13:17 +02:00
}