diff --git a/src/core/marketplace.cpp b/src/core/marketplace.cpp index 5bd3a1f..f0e9d8b 100644 --- a/src/core/marketplace.cpp +++ b/src/core/marketplace.cpp @@ -32,4 +32,10 @@ int Marketplace::getNextSellerNo() return a->getSellerNo() < b->getSellerNo(); }); return (*iter)->getSellerNo() + 1; +} + +int Marketplace::getNumSellersDelete() +{ + int count = std::count_if(sellers_.begin(), sellers_.end(), [](const auto& a){return a->getState() == Seller::State::DELETE;}); + return count; } \ No newline at end of file diff --git a/src/core/marketplace.h b/src/core/marketplace.h index 4bf73bd..580e12c 100644 --- a/src/core/marketplace.h +++ b/src/core/marketplace.h @@ -15,6 +15,7 @@ class Marketplace void loadFromDb(); std::vector>& getSellers(); int getNextSellerNo(); + int getNumSellersDelete(); private: std::vector> sellers_; diff --git a/src/gui/sellerdialog.cpp b/src/gui/sellerdialog.cpp index 7f8987f..499f755 100644 --- a/src/gui/sellerdialog.cpp +++ b/src/gui/sellerdialog.cpp @@ -13,6 +13,7 @@ SellerDialog::SellerDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, ui_.tableView->setModel(model); ui_.tableView->setColumnHidden(0, true); // hide the uuid connect(ui_.newButton, &QPushButton::clicked, this, &SellerDialog::on_newButton_clicked); + connect(ui_.deleteButton, &QPushButton::clicked, this, &SellerDialog::on_deleteButton_clicked); } void SellerDialog::on_newButton_clicked() @@ -26,6 +27,21 @@ void SellerDialog::on_newButton_clicked() ui_.tableView->edit(idx); } +void SellerDialog::on_deleteButton_clicked() +{ + auto selModel = ui_.tableView->selectionModel(); + if (selModel->hasSelection() == false) + return; + auto indexes = selModel->selectedRows(); + + std::sort(indexes.begin(), indexes.end()); + + // Deleting the rows, beginning with the last one! + for (auto iter = indexes.constEnd() - 1; iter >= indexes.constBegin(); --iter) { + ui_.tableView->model()->removeRow(iter->row()); + } +} + void SellerDialog::accept() { Marketplace* market = dynamic_cast(parentWidget())->getMarketplace(); diff --git a/src/gui/sellerdialog.h b/src/gui/sellerdialog.h index 8d0dc0a..d539f4a 100644 --- a/src/gui/sellerdialog.h +++ b/src/gui/sellerdialog.h @@ -17,6 +17,7 @@ class SellerDialog : public QDialog private: void on_newButton_clicked(); + void on_deleteButton_clicked(); virtual void accept() override; Ui::SellerDialog ui_; }; diff --git a/src/gui/sellermodel.cpp b/src/gui/sellermodel.cpp index a88541d..b59f49e 100644 --- a/src/gui/sellermodel.cpp +++ b/src/gui/sellermodel.cpp @@ -23,9 +23,9 @@ QVariant SellerModel::data(const QModelIndex& index, int role) const Seller* seller = marketplace_->getSellers().at(index.row()).get(); - if (seller->getState() == Seller::State::DELETE) - return QVariant::Invalid; - +/* if (seller->getState() == Seller::State::DELETE) + return QVariant(); + */ switch (index.column()) { case 0: return seller->getUuidAsString().c_str(); @@ -129,4 +129,23 @@ bool SellerModel::insertRows(int row, int count, const QModelIndex& parent) emit endInsertRows(); return true; +} + +bool SellerModel::removeRows(int row, int count, const QModelIndex& parent) +{ + emit beginRemoveRows(parent, row, row + count - 1); + auto seller = marketplace_->getSellers().at(row).get(); + if (seller->getState() == Seller::State::NEW) { + marketplace_->getSellers().erase( + std::remove_if(marketplace_->getSellers().begin(), marketplace_->getSellers().end(), + [&seller](const std::unique_ptr& a) { + return a->getUuid() == seller->getUuid(); + }), + marketplace_->getSellers().end()); + } else { + seller->setState(Seller::State::DELETE); + } + emit endRemoveRows(); + + return false; } \ No newline at end of file diff --git a/src/gui/sellermodel.h b/src/gui/sellermodel.h index 7ec2244..8a7f61e 100644 --- a/src/gui/sellermodel.h +++ b/src/gui/sellermodel.h @@ -16,6 +16,7 @@ class SellerModel : public QAbstractTableModel virtual Qt::ItemFlags flags(const QModelIndex& index) const override; 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; + virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; private: Marketplace* marketplace_;