diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 119dc28..a48e833 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -184,7 +184,7 @@ void MainWindow::onCancelSaleButtonClicked([[maybe_unused]] bool checked) // Deleting the rows, beginning with the last one! for (auto iter = indexes.constEnd() - 1; iter >= indexes.constBegin(); --iter) { - ui_.salesView->model()->removeRow(iter->row()); + ui_.salesView->model()->removeRow(iter->row(), iter->parent()); } } diff --git a/src/gui/salemodel.cpp b/src/gui/salemodel.cpp index 0d6cefd..8cc813d 100644 --- a/src/gui/salemodel.cpp +++ b/src/gui/salemodel.cpp @@ -165,4 +165,26 @@ void SaleModel::onBasketDataChanged() return lhs->getTimestamp() > rhs->getTimestamp(); }); emit endResetModel(); +} + +bool SaleModel::removeRows(int row, int count, const QModelIndex& parent) +{ + if (!parent.isValid()) { + // remove complete sale + + } else if (!parent.parent().isValid()) { + // remove one article from a sale + auto sale = static_cast(parent.internalPointer()); + auto articles = sale->getArticles(); + emit beginRemoveRows(parent, row, row + count -1); + articles.at(row)->setState(Article::State::DELETE); + sale->removeArticle(articles.at(row)); + marketplace_->storeToDb(); + if (articles.size() == 0) { + std::cout << "No articles left.\n"; + } + emit endRemoveRows(); + } + + return true; } \ No newline at end of file diff --git a/src/gui/salemodel.h b/src/gui/salemodel.h index d212ed2..91190e9 100644 --- a/src/gui/salemodel.h +++ b/src/gui/salemodel.h @@ -11,13 +11,14 @@ class SaleModel : public QAbstractItemModel public: explicit SaleModel(Marketplace* market, QObject* parent = nullptr); - QModelIndex index(int row, int column, + virtual QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; - QModelIndex parent(const QModelIndex& index) const override; - QVariant data(const QModelIndex& index, int role) const override; - int rowCount(const QModelIndex& parent) const override; - int columnCount(const QModelIndex& parent) const override; - QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + virtual QModelIndex parent(const QModelIndex& index) const override; + virtual QVariant data(const QModelIndex& index, int role) const override; + virtual int rowCount(const QModelIndex& parent) const override; + virtual int columnCount(const QModelIndex& parent) const override; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; public slots: void onBasketDataChanged();