kima2/src/gui/sellermodel.cpp

159 lines
4.6 KiB
C++
Raw Normal View History

2018-07-16 12:00:17 +02:00
#include "sellermodel.h"
2018-07-16 18:04:25 +02:00
#include <algorithm>
#include <QMessageBox>
SellerModel::SellerModel(Marketplace* market, QObject* parent)
: QAbstractTableModel(parent), marketplace_(market)
2018-07-16 12:00:17 +02:00
{
}
2018-07-20 11:52:26 +02:00
int SellerModel::rowCount([[maybe_unused]] const QModelIndex& parent) const
2018-07-16 18:04:25 +02:00
{
return marketplace_->getSellers().size();
}
2018-07-16 12:00:17 +02:00
2018-07-20 11:52:26 +02:00
int SellerModel::columnCount([[maybe_unused]] const QModelIndex& parent) const { return 5; }
2018-07-16 12:00:17 +02:00
QVariant SellerModel::data(const QModelIndex& index, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
2018-07-17 20:17:46 +02:00
if (marketplace_->getSellers().size() == 0)
2018-07-16 12:00:17 +02:00
return QVariant();
2018-07-16 18:04:25 +02:00
Seller* seller = marketplace_->getSellers().at(index.row()).get();
2018-07-17 11:40:09 +02:00
/* if (seller->getState() == Seller::State::DELETE)
return QVariant();
*/
2018-07-16 12:00:17 +02:00
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 "";
2018-07-16 15:30:24 +02:00
}
Qt::ItemFlags SellerModel::flags(const QModelIndex& index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
}
2018-07-16 18:04:25 +02:00
bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
2018-07-16 15:30:24 +02:00
if (role != Qt::EditRole)
return false;
2018-07-16 18:04:25 +02:00
Seller* seller = marketplace_->getSellers().at(index.row()).get();
2018-07-16 15:30:24 +02:00
switch (index.column()) {
case 0:
seller->setUuidFromString(value.toString().toStdString());
break;
2018-07-16 18:04:25 +02:00
case 1: {
if (value.toInt() < 0)
return false;
auto iter =
std::find_if(marketplace_->getSellers().begin(), marketplace_->getSellers().end(),
2018-07-20 11:52:26 +02:00
[&value](const std::unique_ptr<Seller>& s) {
return value.toInt() == s->getSellerNo();
2018-07-16 18:04:25 +02:00
});
if (iter != marketplace_->getSellers().end()) {
emit duplicateSellerNo(
2018-07-17 16:40:07 +02:00
"Die Verkäufernummer muss eindeutig sein.\n(Möglicherweise wird die Nummer von "
"einem (ausgeblendeten) Eintrag, der zum Löschen vorgemerkt ist, verwendet.)");
2018-07-16 18:04:25 +02:00
return false;
}
2018-07-16 15:30:24 +02:00
seller->setSellerNo(value.toInt());
break;
2018-07-16 18:04:25 +02:00
}
2018-07-16 15:30:24 +02:00
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;
}
2018-07-16 18:04:25 +02:00
if (seller->getState() == Seller::State::OK)
seller->setState(Seller::State::UPDATE);
2018-07-16 15:30:24 +02:00
dataChanged(index, index);
2018-07-16 18:04:25 +02:00
return true;
}
bool SellerModel::insertRows(int row, int count, const QModelIndex& parent)
{
2018-07-17 11:40:09 +02:00
emit beginInsertRows(parent, row, row + count - 1);
2018-07-16 18:04:25 +02:00
auto seller = std::make_unique<Seller>();
seller->createUuid();
seller->setSellerNo(marketplace_->getNextSellerNo());
marketplace_->getSellers().push_back(std::move(seller));
2018-07-17 11:40:09 +02:00
emit endInsertRows();
2018-07-16 18:04:25 +02:00
2018-07-16 15:30:24 +02:00
return true;
2018-07-17 15:32:16 +02:00
}
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);
2018-07-17 15:32:16 +02:00
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();
2018-07-23 13:39:49 +02:00
return true;
2018-07-17 15:32:16 +02:00
} else {
2018-07-18 08:17:30 +02:00
emit beginRemoveRows(parent, row, row + count - 1);
2018-07-17 15:32:16 +02:00
seller->setState(Seller::State::DELETE);
marketplace_->storeToDb(true);
2018-07-18 08:17:30 +02:00
emit endRemoveRows();
2018-07-23 13:39:49 +02:00
return true;
2018-07-17 15:32:16 +02:00
}
return false;
2018-07-16 12:00:17 +02:00
}