#include "commoditysettingsform.h" #include "ui_commoditysettingsform.h" #include #include #include CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) : FormBinder(parent), ui(new Ui::CommoditySettingsForm) { ui->setupUi(this); registerBinding(ui->codeAsNumber); m_table = new AutoTableModel(); m_table->setEditableCols(QList() << 0); ui->commodityTypesTable->setModel(m_table); ui->commodityTypesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); } CommoditySettingsForm::~CommoditySettingsForm() { delete ui; } void CommoditySettingsForm::loadEntity() { SettingsService srv("COMMODITY"); CommoditySettingsPtr settings = srv.loadSettings(); setEntity(settings); Service commodityService; m_table->setData(commodityService.all()); } bool CommoditySettingsForm::saveRecord() { bindToData(); SettingsService srv("COMMODITY"); srv.saveSettings(entity()); Service commodityService; foreach (QSharedPointer item, m_table->list()) { if(item->id() == 0) { commodityService.save(item); } else { commodityService.update(item); } } foreach (QSharedPointer item, m_delCommodityTypes) { commodityService.erase(item); } return true; } void CommoditySettingsForm::on_addCommodityType_clicked() { m_table->addRow(QSharedPointer(new CommodityTypeData)); } void CommoditySettingsForm::on_delCommodityType_clicked() { if(m_table->itemFromIndex(ui->commodityTypesTable->currentIndex())->id() != 0) { m_delCommodityTypes.append(m_table->itemFromIndex(ui->commodityTypesTable->currentIndex())); } m_table->removeRowAt(ui->commodityTypesTable->currentIndex()); }