Initial implementation of salemodel
This commit is contained in:
parent
bc67018c1c
commit
e24b6c4e5f
11 changed files with 212 additions and 1 deletions
164
src/gui/salemodel.cpp
Normal file
164
src/gui/salemodel.cpp
Normal file
|
@ -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 "";
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue