You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prodejna/commodity/commoditysettingsform.cpp

80 lines
2.1 KiB
C++

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