more on sellers
This commit is contained in:
parent
a2ad2eb57d
commit
2812f582a1
7 changed files with 98 additions and 13 deletions
|
@ -1,9 +1,12 @@
|
||||||
#include "marketplace.h"
|
#include "marketplace.h"
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
Marketplace::Marketplace()
|
Marketplace::Marketplace()
|
||||||
{
|
{
|
||||||
auto seller = std::make_unique<Seller>("Max", "Mustermann");
|
auto seller = std::make_unique<Seller>("Max", "Mustermann");
|
||||||
|
seller->createUuid();
|
||||||
sellers_.push_back(std::move(seller));
|
sellers_.push_back(std::move(seller));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +15,20 @@ void Marketplace::storeToDb()
|
||||||
const std::string DB_PATH{"/tmp/kima2.db"};
|
const std::string DB_PATH{"/tmp/kima2.db"};
|
||||||
|
|
||||||
Database db(DB_PATH);
|
Database db(DB_PATH);
|
||||||
|
db.init();
|
||||||
db.storeSellers(sellers_);
|
db.storeSellers(sellers_);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }
|
void Marketplace::loadFromDb() {}
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<Seller>>& Marketplace::getSellers() { return sellers_; }
|
||||||
|
|
||||||
|
int Marketplace::getNextSellerNo()
|
||||||
|
{
|
||||||
|
auto iter = std::max_element(
|
||||||
|
sellers_.begin(), sellers_.end(),
|
||||||
|
[](const std::unique_ptr<Seller>& a, const std::unique_ptr<Seller>& b) -> bool {
|
||||||
|
return a->getSellerNo() < b->getSellerNo();
|
||||||
|
});
|
||||||
|
return (*iter)->getSellerNo() + 1;
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ class Marketplace
|
||||||
void storeToDb();
|
void storeToDb();
|
||||||
void loadFromDb();
|
void loadFromDb();
|
||||||
std::vector<std::unique_ptr<Seller>>& getSellers();
|
std::vector<std::unique_ptr<Seller>>& getSellers();
|
||||||
|
int getNextSellerNo();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<Seller>> sellers_;
|
std::vector<std::unique_ptr<Seller>> sellers_;
|
||||||
|
|
|
@ -15,5 +15,10 @@ MainWindow::MainWindow()
|
||||||
void MainWindow::on_actionEditSeller_triggered()
|
void MainWindow::on_actionEditSeller_triggered()
|
||||||
{
|
{
|
||||||
auto dialog = std::make_unique<SellerDialog>(this);
|
auto dialog = std::make_unique<SellerDialog>(this);
|
||||||
dialog->exec();
|
int retCode = dialog->exec();
|
||||||
|
if (retCode == QDialog::Accepted) {
|
||||||
|
marketplace_->storeToDb();
|
||||||
|
} else {
|
||||||
|
marketplace_->loadFromDb();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -2,11 +2,34 @@
|
||||||
|
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
|
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
SellerDialog::SellerDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f)
|
SellerDialog::SellerDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f)
|
||||||
{
|
{
|
||||||
ui_.setupUi(this);
|
ui_.setupUi(this);
|
||||||
SellerModel* model = new SellerModel(
|
SellerModel* model =
|
||||||
dynamic_cast<MainWindow*>(parent)->getMarketplace()->getSellers(), ui_.tableView);
|
new SellerModel(dynamic_cast<MainWindow*>(parent)->getMarketplace(), ui_.tableView);
|
||||||
ui_.tableView->setModel(model);
|
ui_.tableView->setModel(model);
|
||||||
ui_.tableView->setColumnHidden(0, true); // hide the uuid
|
ui_.tableView->setColumnHidden(0, true); // hide the uuid
|
||||||
|
connect(ui_.newButton, &QPushButton::clicked, this, &SellerDialog::on_newButton_clicked);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SellerDialog::on_newButton_clicked()
|
||||||
|
{
|
||||||
|
ui_.tableView->model()->insertRows(ui_.tableView->model()->rowCount(), 1);
|
||||||
|
ui_.tableView->scrollToBottom();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SellerDialog::accept()
|
||||||
|
{
|
||||||
|
Marketplace* market = dynamic_cast<MainWindow*>(parentWidget())->getMarketplace();
|
||||||
|
for (const auto& seller : market->getSellers()) {
|
||||||
|
if (seller->getFirstName().empty() || seller->getLastName().empty()) {
|
||||||
|
QMessageBox(QMessageBox::Icon::Critical, "Fehler",
|
||||||
|
"Bitte geben Sie bei jedem Verkäufer Vorname und Nachname an.",
|
||||||
|
QMessageBox::StandardButton::Ok, this).exec();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QDialog::accept();
|
||||||
|
}
|
|
@ -16,6 +16,8 @@ class SellerDialog : public QDialog
|
||||||
Qt::WindowFlags f = Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
|
Qt::WindowFlags f = Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void on_newButton_clicked();
|
||||||
|
virtual void accept() override;
|
||||||
Ui::SellerDialog ui_;
|
Ui::SellerDialog ui_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
#include "sellermodel.h"
|
#include "sellermodel.h"
|
||||||
|
|
||||||
SellerModel::SellerModel(std::vector<std::unique_ptr<Seller>>& sellers, QObject* parent)
|
#include <algorithm>
|
||||||
: QAbstractTableModel(parent), sellers_(&sellers)
|
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
|
SellerModel::SellerModel(Marketplace* market, QObject* parent)
|
||||||
|
: QAbstractTableModel(parent), marketplace_(market)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int SellerModel::rowCount(const QModelIndex& parent) const { return sellers_->size(); }
|
int SellerModel::rowCount(const QModelIndex& parent) const
|
||||||
|
{
|
||||||
|
return marketplace_->getSellers().size();
|
||||||
|
}
|
||||||
|
|
||||||
int SellerModel::columnCount(const QModelIndex& parent) const { return 5; }
|
int SellerModel::columnCount(const QModelIndex& parent) const { return 5; }
|
||||||
|
|
||||||
|
@ -14,7 +21,7 @@ QVariant SellerModel::data(const QModelIndex& index, int role) const
|
||||||
if (role != Qt::DisplayRole)
|
if (role != Qt::DisplayRole)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
Seller* seller = sellers_->at(index.row()).get();
|
Seller* seller = marketplace_->getSellers().at(index.row()).get();
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
return seller->getUuidAsString().c_str();
|
return seller->getUuidAsString().c_str();
|
||||||
|
@ -62,18 +69,34 @@ Qt::ItemFlags SellerModel::flags(const QModelIndex& index) const
|
||||||
return QAbstractTableModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
|
return QAbstractTableModel::flags(index) | Qt::ItemFlag::ItemIsEditable;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||||
|
{
|
||||||
if (role != Qt::EditRole)
|
if (role != Qt::EditRole)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Seller* seller = sellers_->at(index.row()).get();
|
Seller* seller = marketplace_->getSellers().at(index.row()).get();
|
||||||
|
|
||||||
switch (index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
seller->setUuidFromString(value.toString().toStdString());
|
seller->setUuidFromString(value.toString().toStdString());
|
||||||
break;
|
break;
|
||||||
case 1:
|
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>& seller) {
|
||||||
|
return value.toInt() == seller->getSellerNo();
|
||||||
|
});
|
||||||
|
if (iter != marketplace_->getSellers().end()) {
|
||||||
|
/* QMessageBox(QMessageBox::Icon::Warning, "Fehler",
|
||||||
|
"Die Verkäufernummer muss eindeutig sein.", QMessageBox::StandardButton::Ok)
|
||||||
|
.exec(); */
|
||||||
|
return false;
|
||||||
|
}
|
||||||
seller->setSellerNo(value.toInt());
|
seller->setSellerNo(value.toInt());
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 2:
|
case 2:
|
||||||
seller->setFirstName(value.toString().toStdString());
|
seller->setFirstName(value.toString().toStdString());
|
||||||
break;
|
break;
|
||||||
|
@ -86,6 +109,20 @@ bool SellerModel::setData(const QModelIndex& index, const QVariant& value, int r
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (seller->getState() == Seller::State::OK)
|
||||||
|
seller->setState(Seller::State::UPDATE);
|
||||||
dataChanged(index, index);
|
dataChanged(index, index);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SellerModel::insertRows(int row, int count, const QModelIndex& parent)
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
endInsertRows();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
|
@ -8,16 +8,17 @@
|
||||||
class SellerModel : public QAbstractTableModel
|
class SellerModel : public QAbstractTableModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit SellerModel(std::vector<std::unique_ptr<Seller>>& sellers, QObject* parent = nullptr);
|
explicit SellerModel(Marketplace* market, QObject* parent = nullptr);
|
||||||
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
|
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
|
||||||
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||||
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
|
virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||||||
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
|
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
|
||||||
|
virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<Seller>>* sellers_;
|
Marketplace* marketplace_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in a new issue