diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 204f6c1..57d2079 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "data/favorititem.h" #include "favbutton.h" @@ -71,6 +72,14 @@ ShopForm::ShopForm(QWidget *parent) : ui->temporarySaveButton->setEnabled(false); ui->saveButton->setEnabled(false); ui->payButton->setEnabled(false); + + m_itemCtxMenu.reset(new QMenu()); + m_itemCtxMenu->addAction(ui->actionDelete_items); + ui->actualReceipt->addAction(ui->actionDelete_items); + + connect(ui->actualReceipt->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected, const QItemSelection &) { + ui->actionDelete_items->setEnabled(!selected.isEmpty()); + }); } ShopForm::~ShopForm() @@ -341,29 +350,8 @@ void ShopForm::onCountChanged(int oldCount/* = 0*/) } } - ShopService srv; - srv.calculate(m_voucher); - this->m_itemsModel->setData(m_voucher->items()); - - setTotalText(); - ui->temporarySaveButton->setEnabled(!m_voucher->items().isEmpty()); - ui->saveButton->setEnabled(!m_voucher->items().isEmpty()); - ui->payButton->setEnabled(!m_voucher->items().isEmpty()); - - if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0) - { - srv.saveVoucher(m_voucher); - } - else - { - srv.updateVoucher(m_voucher); - } - - if (item != nullptr) - { - int countAdded = item->count() - oldCount; - srv.updateRelatedItem(item, countAdded); - } + recalculate(); + updateItemCount(item, oldCount); } void ShopForm::createVoucher() @@ -502,6 +490,37 @@ void ShopForm::setTotalText() ui->priceNoVat->setText(QString::number(m_voucher->priceNoVat().toDouble(), 'f', 2)); } +void ShopForm::updateItemCount(VoucherItem *item, int oldCount) +{ + if (item != nullptr) + { + ShopService srv; + int countAdded = item->count() - oldCount; + srv.updateRelatedItem(item, countAdded); + } +} + +void ShopForm::recalculate() +{ + ShopService srv; + srv.calculate(m_voucher); + this->m_itemsModel->setData(m_voucher->items()); + + setTotalText(); + ui->temporarySaveButton->setEnabled(!m_voucher->items().isEmpty()); + ui->saveButton->setEnabled(!m_voucher->items().isEmpty()); + ui->payButton->setEnabled(!m_voucher->items().isEmpty()); + + if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0) + { + srv.saveVoucher(m_voucher); + } + else + { + srv.updateVoucher(m_voucher); + } +} + void ShopForm::on_receiptCombo_currentIndexChanged(int) { if (!m_voucher.isNull() && m_voucher->items().isEmpty()) @@ -617,3 +636,30 @@ void ShopForm::on_commoditySearch_returnPressed() } ui->commoditySearch->clear(); } + +void ShopForm::on_actionDelete_items_triggered() +{ + if (QMessageBox::question(this, tr("Delete items"), tr("Realy delete selected voucher items?")) == QMessageBox::Yes) { + QList forDelete; + + for (const auto& row : ui->actualReceipt->selectionModel()->selectedIndexes()) { + forDelete << m_itemsModel->itemFromIndex(row); + } + + for (auto& item : forDelete) { + m_voucher->removeItem(item); + int oldCount = item->count(); + item->setCount(0); + updateItemCount(item.data(), oldCount); + } + + recalculate(); + m_itemsModel->setData(m_voucher->items()); + } +} + +void ShopForm::on_actualReceipt_customContextMenuRequested(const QPoint &pos) +{ + QPoint globalPos = ui->actualReceipt->mapToGlobal(pos); + m_itemCtxMenu->exec(globalPos); +} diff --git a/shop/shopform.h b/shop/shopform.h index 4128bf4..cc35bab 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include "data/shop-data.h" #include #include "settings/shopsettings.h" @@ -50,12 +52,18 @@ private slots: void on_commoditySearch_returnPressed(); + void on_actionDelete_items_triggered(); + + void on_actualReceipt_customContextMenuRequested(const QPoint &pos); + + private: Ui::ShopForm *ui; QSharedPointer m_voucher; AutoTableModel *m_itemsModel; AutoTableModel *m_commodityModel; bool m_itemFound; + QScopedPointer m_itemCtxMenu; void loadLast(); void loadButtons(const ShopSettingsPtr& settings); @@ -67,6 +75,8 @@ private: void createEmptyVoucher(); void addItem(QSharedPointer item, int count); void setTotalText(); + void recalculate(); + void updateItemCount(VoucherItem *item, int oldCount); }; #endif // SHOPFORM_H diff --git a/shop/shopform.ui b/shop/shopform.ui index 075cff4..2b0dc8e 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -6,7 +6,7 @@ 0 0 - 988 + 990 643 @@ -174,6 +174,12 @@ 0 + + Qt::CustomContextMenu + + + QAbstractItemView::SelectRows + @@ -599,6 +605,14 @@ + + + Delete items + + + Ctrl+Del + +