kima2/src/gui/salemodel.cpp

208 lines
5.9 KiB
C++
Raw Permalink Normal View History

2018-07-25 16:04:45 +02:00
#include "salemodel.h"
2019-10-10 08:09:16 +02:00
#include <core/article.h>
2018-07-25 16:04:45 +02:00
#include <algorithm>
#include <QFont>
2018-08-08 09:16:23 +02:00
#include <QFontDatabase>
2018-07-25 16:04:45 +02:00
SaleModel::SaleModel(Marketplace* market, QObject* parent) : QAbstractItemModel(parent)
{
2022-07-07 15:37:33 +02:00
m_marketplace = market;
2018-07-25 16:04:45 +02:00
}
QModelIndex SaleModel::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (!parent.isValid()) {
2022-07-07 15:37:33 +02:00
Sale* sale = m_marketplace->getSales().at(row).get();
2018-07-25 16:04:45 +02:00
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{};
2019-10-04 14:05:19 +02:00
EntityUuid* ent = static_cast<EntityUuid*>(index.internalPointer());
2018-07-25 16:04:45 +02:00
sale = dynamic_cast<Sale*>(ent);
if (sale) {
2022-07-07 15:37:33 +02:00
if (sale == m_rootItem.get())
2018-07-25 16:04:45 +02:00
return QModelIndex();
else {
2022-07-07 15:37:33 +02:00
return createIndex(-1, 0, m_rootItem.get());
2018-07-25 16:04:45 +02:00
}
} 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);
2019-04-08 10:48:24 +02:00
if (myFixedFont.fixedPitch() == false) {
2018-10-23 14:13:17 +02:00
myFixedFont.setFamily("monospace");
}
2018-07-25 16:04:45 +02:00
switch (index.column()) {
case 0:
return myFixedFont;
2018-07-25 16:04:45 +02:00
case 1:
myFixedFont.setBold(true);
return myFixedFont;
2018-07-25 16:04:45 +02:00
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:
2019-04-08 10:48:24 +02:00
return (std::string("Verk. ") + article->getSeller()->getSellerNoAsString() + " (" +
article->getCompleteArticleNo() + ")")
.c_str();
2018-07-25 16:04:45 +02:00
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()) {
2022-07-07 15:37:33 +02:00
return m_marketplace->getSales().size();
2018-07-25 16:04:45 +02:00
} 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:
2018-10-08 09:20:59 +02:00
return "Zeit / Verk.Nr. (Art.Nr)";
2018-07-25 16:04:45 +02:00
case 1:
return "Preis";
default:
return "???";
}
return QStringLiteral("%1").arg(section);
} else
return "";
2018-07-26 08:34:01 +02:00
}
void SaleModel::onBasketDataChanged()
{
emit beginResetModel();
2022-07-07 15:37:33 +02:00
auto& sales = m_marketplace->getSales();
2018-07-26 08:34:01 +02:00
std::sort(sales.begin(), sales.end(), [](const auto& lhs, const auto& rhs) {
return lhs->getTimestamp() > rhs->getTimestamp();
});
emit endResetModel();
2018-07-27 16:41:34 +02:00
}
bool SaleModel::removeRows(int row, int count, const QModelIndex& parent)
{
if (!parent.isValid()) {
// remove complete sale
2018-07-28 14:31:37 +02:00
emit beginRemoveRows(parent, row, row + count - 1);
2022-07-07 15:37:33 +02:00
auto& sale = m_marketplace->getSales().at(row);
2018-07-28 14:31:37 +02:00
sale->setState(Sale::State::DELETE);
std::for_each(sale->getArticles().begin(), sale->getArticles().end(),
[](auto& a) { a->setState(Article::State::DELETE); });
2022-07-07 15:37:33 +02:00
m_marketplace->storeToDb();
2018-07-28 14:31:37 +02:00
emit endRemoveRows();
2018-07-27 16:41:34 +02:00
} else if (!parent.parent().isValid()) {
// remove one article from a sale
auto sale = static_cast<Sale*>(parent.internalPointer());
2018-07-28 11:52:43 +02:00
auto& articles = sale->getArticles();
2018-07-27 16:41:34 +02:00
articles.at(row)->setState(Article::State::DELETE);
2018-07-28 11:52:43 +02:00
emit beginRemoveRows(parent, row, row + count - 1);
2018-07-27 16:41:34 +02:00
sale->removeArticle(articles.at(row));
2018-07-28 11:52:43 +02:00
emit endRemoveRows();
2018-07-27 16:41:34 +02:00
if (articles.size() == 0) {
2018-07-28 11:52:43 +02:00
// marketplace_->removeSale(sale->getUuid());
sale->setState(Sale::State::DELETE);
2018-07-27 16:41:34 +02:00
}
2018-07-28 11:52:43 +02:00
emit beginRemoveRows(parent.parent(), 0, 0);
2022-07-07 15:37:33 +02:00
m_marketplace->storeToDb();
2018-07-27 16:41:34 +02:00
emit endRemoveRows();
}
return true;
2018-10-23 14:13:17 +02:00
}