207 lines
5.9 KiB
C++
207 lines
5.9 KiB
C++
#include "salemodel.h"
|
|
|
|
#include <article.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <QFont>
|
|
#include <QFontDatabase>
|
|
|
|
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{};
|
|
|
|
EntityUuid* ent = static_cast<EntityUuid*>(index.internalPointer());
|
|
|
|
sale = dynamic_cast<Sale*>(ent);
|
|
|
|
if (sale) {
|
|
if (sale == rootItem.get())
|
|
return QModelIndex();
|
|
else {
|
|
return createIndex(-1, 0, rootItem.get());
|
|
}
|
|
} else {
|
|
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;
|
|
QFont myFixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
|
if (myFixedFont.fixedPitch() == false) {
|
|
myFixedFont.setFamily("monospace");
|
|
}
|
|
|
|
switch (index.column()) {
|
|
case 0:
|
|
return myFixedFont;
|
|
case 1:
|
|
myFixedFont.setBold(true);
|
|
return myFixedFont;
|
|
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 (std::string("Verk. ") + article->getSeller()->getSellerNoAsString() + " (" +
|
|
article->getCompleteArticleNo() + ")")
|
|
.c_str();
|
|
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 / Verk.Nr. (Art.Nr)";
|
|
case 1:
|
|
return "Preis";
|
|
default:
|
|
return "???";
|
|
}
|
|
return QStringLiteral("%1").arg(section);
|
|
} else
|
|
return "";
|
|
}
|
|
|
|
void SaleModel::onBasketDataChanged()
|
|
{
|
|
emit beginResetModel();
|
|
auto& sales = marketplace_->getSales();
|
|
std::sort(sales.begin(), sales.end(), [](const auto& lhs, const auto& rhs) {
|
|
return lhs->getTimestamp() > rhs->getTimestamp();
|
|
});
|
|
emit endResetModel();
|
|
}
|
|
|
|
bool SaleModel::removeRows(int row, int count, const QModelIndex& parent)
|
|
{
|
|
if (!parent.isValid()) {
|
|
// remove complete sale
|
|
emit beginRemoveRows(parent, row, row + count - 1);
|
|
auto& sale = marketplace_->getSales().at(row);
|
|
sale->setState(Sale::State::DELETE);
|
|
std::for_each(sale->getArticles().begin(), sale->getArticles().end(),
|
|
[](auto& a) { a->setState(Article::State::DELETE); });
|
|
marketplace_->storeToDb();
|
|
emit endRemoveRows();
|
|
|
|
} else if (!parent.parent().isValid()) {
|
|
// remove one article from a sale
|
|
auto sale = static_cast<Sale*>(parent.internalPointer());
|
|
auto& articles = sale->getArticles();
|
|
articles.at(row)->setState(Article::State::DELETE);
|
|
emit beginRemoveRows(parent, row, row + count - 1);
|
|
sale->removeArticle(articles.at(row));
|
|
emit endRemoveRows();
|
|
if (articles.size() == 0) {
|
|
// marketplace_->removeSale(sale->getUuid());
|
|
sale->setState(Sale::State::DELETE);
|
|
}
|
|
emit beginRemoveRows(parent.parent(), 0, 0);
|
|
marketplace_->storeToDb();
|
|
emit endRemoveRows();
|
|
}
|
|
|
|
return true;
|
|
}
|