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/shop/settings/shopsettingsform.cpp

222 lines
6.3 KiB
C++

#include "shopsettingsform.h"
#include "ui_shopsettingsform.h"
#include <QFileDialog>
#include <QDragEnterEvent>
#include <QDebug>
#include "../shopservice.h"
ShopSettingsForm::ShopSettingsForm(QWidget *parent) :
FormBinder<ShopSettings>(parent),
ui(new Ui::ShopSettingsForm)
{
ui->setupUi(this);
registerBinding(ui->output);
registerBinding(ui->lettersPerLine);
registerBinding(ui->byMessage);
#ifdef EET
registerBinding(ui->eetActive);
registerBinding(ui->eetShopId);
registerBinding(ui->eetRegisterId);
QList<ComboData> listModes;
listModes << ComboData(0, tr("Simplifyed")) << ComboData(1, tr("Standard"));
registerBinding(ui->eetMode, listModes);
registerBinding(ui->eetCertificate);
registerBinding(ui->eetKeyPassword);
registerBinding(ui->eetTest);
registerBinding(ui->eetPlayground);
#else
ui->tabWidget->removeTab(2);
#endif
registerBinding(ui->favBtnCols);
registerBinding(ui->favBtnRows);
registerBinding(ui->favBtnSize);
QList<ComboData> listRounding;
listRounding
<< ComboData(Enums::R_NONE, tr("None"))
<< ComboData(Enums::R_UP, tr("Up"))
<< ComboData(Enums::R_DOWN, tr("Down"))
<< ComboData(Enums::R_MATH, tr("Mathematic"));
registerBinding(ui->rounding, listRounding);
registerBinding(ui->decimalPlaces);
registerBinding(ui->roundingItem);
registerBinding(ui->doublePrint);
registerBinding(ui->doublePrintItem);
registerBinding(ui->showCount);
QList<ComboData> listVatTypes;
listVatTypes
<< ComboData(Enums::NONE, tr("None"))
<< ComboData(Enums::HIGH, tr("High"))
<< ComboData(Enums::FIRST_LOWER, tr("First lower"))
<< ComboData(Enums::SECOND_LOWER, tr("Second lower"));
registerBinding(ui->defaultVat, listVatTypes);
m_itemModel = new AutoTableModel<IShopItem>(this);
}
ShopSettingsForm::~ShopSettingsForm()
{
delete ui;
}
void ShopSettingsForm::drawButtons()
{
foreach (QWidget *child, ui->btnWidget->findChildren<QWidget*>()) {
delete child;
}
for (int i = 0; i < entity()->favBtnRows(); i++)
{
for (int j = 0; j < entity()->favBtnCols(); j++)
{
FavButton *btn = new FavButton(ui->btnWidget);
btn->setObjectName(QString::number(i) + "_" + QString::number(j));
btn->setAcceptDrops(true);
if (m_btnMap[btn->objectName()] != nullptr)
{
btn->setText(m_btnMap[btn->objectName()]->shortName());
}
if (entity()->favBtnSize() > 0)
{
btn->setMinimumHeight(entity()->favBtnSize());
btn->setMinimumWidth(entity()->favBtnSize());
}
btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
((QGridLayout*)ui->btnWidget->layout())->addWidget(btn, i, j);
connect(btn, &FavButton::clicked, [this, btn](bool){
btn->setText("");
m_btnMap.remove(btn->objectName());
});
connect(btn, &FavButton::itemDropped, [this, btn](){
IShopItemPtr item = m_itemModel->itemFromIndex(ui->tableItems->currentIndex());
FavoritItemPtr favItem = QSharedPointer<FavoritItem>(new FavoritItem);
favItem->setFavButtonName(btn->objectName());
favItem->setName(item->name());
favItem->setShortName(item->shortName());
favItem->setRefId(item->id());
favItem->setPluginId(item->pluginId());
m_btnMap[btn->objectName()] = favItem;
btn->setText(item->shortName());
});
}
}
}
void ShopSettingsForm::loadEntity()
{
SettingsService srv("SHOP");
ShopSettingsPtr settings = srv.loadSettings<ShopSettings>();
setEntity(settings);
ShopService srvShop;
m_itemModel->setData(srvShop.allSellableItems());
m_itemModel->setTranslations(Context::instance().plugin("SHOP")->translations());
ui->tableItems->setModel(m_itemModel);
ui->tableItems->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->tableItems->setColumnHidden(0, true);
ui->tableItems->setColumnHidden(2, true);
ui->tableItems->setColumnHidden(4, true);
Service<FavoritItem> srvFav;
foreach (FavoritItemPtr favItem, srvFav.all()) {
m_btnMap[favItem->favButtonName()] = favItem;
}
drawButtons();
}
bool ShopSettingsForm::saveRecord()
{
bindToData();
SettingsService srv("SHOP");
srv.saveSettings(entity());
Service<FavoritItem> srvFav;
foreach (FavoritItemPtr item, srvFav.all()) {
srvFav.erase(item);
}
foreach (QString btnName, m_btnMap.keys()) {
if (m_btnMap[btnName] != nullptr)
{
srvFav.save(m_btnMap[btnName]);
}
}
return true;
}
void ShopSettingsForm::on_btnCertBrowse_clicked()
{
QString certFile = QFileDialog::getOpenFileName(this, "Certificate file", "", "P12 Files (*.p12)");
if (!certFile.isEmpty())
{
ui->eetCertificate->setText(certFile);
}
}
void ShopSettingsForm::on_favBtnCols_textChanged(const QString &arg1)
{
entity()->setFavBtnCols(arg1.toInt());
drawButtons();
}
void ShopSettingsForm::on_favBtnRows_textChanged(const QString &arg1)
{
entity()->setFavBtnRows(arg1.toInt());
drawButtons();
}
void ShopSettingsForm::on_favBtnSize_textChanged(const QString &arg1)
{
entity()->setFavBtnSize(arg1.toInt());
drawButtons();
}
//////////////////////////////////////////////////////////
/// \brief FavButtonStyle::FavButtonStyle
///
FavButtonStyle::FavButtonStyle()
:QProxyStyle()
{
}
void FavButtonStyle::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole) const
{
flags |= Qt::TextWordWrap;
QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, text, textRole);
}
///////////////////////////////////////////////////////////
/// \brief FavButton::FavButton
/// \param parent
///
FavButton::FavButton(QWidget *parent) : QToolButton(parent)
{
setStyle(&m_style);
}
void FavButton::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void FavButton::dropEvent(QDropEvent *)
{
emit itemDropped();
}