kima2/src/gui/sellermodel.cpp

159 lines
No EOL
4.6 KiB
C++

#include "sellermodel.h"
#include <algorithm>
#include <QMessageBox>
SellerModel::SellerModel(Marketplace* market, QObject* parent)
: QAbstractTableModel(parent), marketplace_(market)
{
}
int SellerModel::rowCount([[maybe_unused]] const QModelIndex& parent) const
{
return marketplace_->getSellers().size();
}
int SellerModel::columnCount([[maybe_unused]] const QModelIndex& parent) const { return 5; }
QVariant SellerModel::data(const QModelIndex& index, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (marketplace_->getSellers().size() == 0)
return QVariant();
Seller* seller = marketplace_->getSellers().at(index.row()).get();
/* if (seller->getState() == Seller::State::DELETE)
return QVariant();
*/
switch (index.column()) {
case 0:
return seller->getUuidAsString().c_str();
case 1:
return seller->getSellerNo();
case 2:
return seller->getFirstName().c_str();
case 3:
return QString(seller->getLastName().c_str());
case 4:
return seller->numArticlesOffered();
default:
return "???";
}
return QVariant{};
}
QVariant SellerModel::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 "Nummer";
case 2:
return "Vorname";
case 3:
return "Nachname";
case 4:
return "Anz. Artikel";
default:
return "???";
}
return QStringLiteral("%1").arg(section);
} else
return "";
}
Qt::ItemFlags SellerModel::flags(const QModelIndex& index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
}
bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (role != Qt::EditRole)
return false;
Seller* seller = marketplace_->getSellers().at(index.row()).get();
switch (index.column()) {
case 0:
seller->setUuidFromString(value.toString().toStdString());
break;
case 1: {
if (value.toInt() < 0)
return false;
auto iter =
std::find_if(marketplace_->getSellers().begin(), marketplace_->getSellers().end(),
[&value](const std::unique_ptr<Seller>& s) {
return value.toInt() == s->getSellerNo();
});
if (iter != marketplace_->getSellers().end()) {
emit duplicateSellerNo(
"Die Verkäufernummer muss eindeutig sein.\n(Möglicherweise wird die Nummer von "
"einem (ausgeblendeten) Eintrag, der zum Löschen vorgemerkt ist, verwendet.)");
return false;
}
seller->setSellerNo(value.toInt());
break;
}
case 2:
seller->setFirstName(value.toString().toStdString());
break;
case 3:
seller->setLastName(value.toString().toStdString());
break;
case 4:
seller->setNumArticlesOffered(value.toInt());
break;
default:
return false;
}
if (seller->getState() == Seller::State::OK)
seller->setState(Seller::State::UPDATE);
dataChanged(index, index);
return true;
}
bool SellerModel::insertRows(int row, int count, const QModelIndex& parent)
{
emit beginInsertRows(parent, row, row + count - 1);
auto seller = std::make_unique<Seller>();
seller->createUuid();
seller->setSellerNo(marketplace_->getNextSellerNo());
marketplace_->getSellers().push_back(std::move(seller));
emit endInsertRows();
return true;
}
bool SellerModel::removeRows(int row, int count, const QModelIndex& parent)
{
auto seller = marketplace_->getSellers().at(row).get();
if (seller->getState() == Seller::State::NEW) {
emit beginRemoveRows(parent, row, row + count - 1);
marketplace_->getSellers().erase(
std::remove_if(marketplace_->getSellers().begin(), marketplace_->getSellers().end(),
[&seller](const std::unique_ptr<Seller>& a) {
return a->getUuid() == seller->getUuid();
}),
marketplace_->getSellers().end());
emit endRemoveRows();
return true;
} else {
emit beginRemoveRows(parent, row, row + count - 1);
seller->setState(Seller::State::DELETE);
marketplace_->storeToDb(true);
emit endRemoveRows();
return true;
}
return false;
}