closes #4: Allowed to multiselect items on voucher and delete them via context menu or Ctrl+Del shortcut.

master
Josef Rokos 4 years ago
parent ab20fe9b34
commit 30e6180c82

@ -14,6 +14,7 @@
#include <QSortFilterProxyModel>
#include <QMessageBox>
#include <QScrollBar>
#include <QItemSelectionModel>
#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<VoucherItemPtr> 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);
}

@ -3,6 +3,8 @@
#include <QWidget>
#include <QList>
#include <QScopedPointer>
#include <QMenu>
#include "data/shop-data.h"
#include <autotablemodel.h>
#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<Voucher> m_voucher;
AutoTableModel<VoucherItem> *m_itemsModel;
AutoTableModel<ShopItem> *m_commodityModel;
bool m_itemFound;
QScopedPointer<QMenu> m_itemCtxMenu;
void loadLast();
void loadButtons(const ShopSettingsPtr& settings);
@ -67,6 +75,8 @@ private:
void createEmptyVoucher();
void addItem(QSharedPointer<IShopItem> item, int count);
void setTotalText();
void recalculate();
void updateItemCount(VoucherItem *item, int oldCount);
};
#endif // SHOPFORM_H

@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>988</width>
<width>990</width>
<height>643</height>
</rect>
</property>
@ -174,6 +174,12 @@
<height>0</height>
</size>
</property>
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
@ -599,6 +605,14 @@
</widget>
</item>
</layout>
<action name="actionDelete_items">
<property name="text">
<string>Delete items</string>
</property>
<property name="shortcut">
<string>Ctrl+Del</string>
</property>
</action>
</widget>
<resources>
<include location="../core/rc.qrc"/>

Loading…
Cancel
Save