From b0d67cf93a1c725cef2e8daa0493991b7349fd88 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 28 Mar 2017 22:13:54 +0200 Subject: [PATCH] Added favorite buttons to shop form. closes #276 --- commodity/commodityservice.cpp | 6 + commodity/commodityservice.h | 4 +- commodity/data/commoditydata.h | 2 + shop/data/favorititem.cpp | 10 ++ shop/data/favorititem.h | 5 + shop/favbutton.h | 22 +++ shop/isellableservice.h | 3 +- shop/settings/favoriteservice.cpp | 6 - shop/settings/favoriteservice.h | 13 -- shop/settings/shopsettings.cpp | 34 ++++ shop/settings/shopsettings.h | 17 ++ shop/settings/shopsettingsform.cpp | 110 +++++++++++++ shop/settings/shopsettingsform.h | 13 ++ shop/settings/shopsettingsform.ui | 184 +++++++--------------- shop/shop.json | 12 +- shop/shop.pro | 5 +- shop/shopform.cpp | 53 ++++++- shop/shopform.h | 1 + shop/shopform.ui | 241 ++--------------------------- 19 files changed, 359 insertions(+), 382 deletions(-) create mode 100644 shop/favbutton.h delete mode 100644 shop/settings/favoriteservice.cpp delete mode 100644 shop/settings/favoriteservice.h diff --git a/commodity/commodityservice.cpp b/commodity/commodityservice.cpp index ecb1d3f..941292d 100644 --- a/commodity/commodityservice.cpp +++ b/commodity/commodityservice.cpp @@ -29,3 +29,9 @@ void CommodityService::addedToVoucher(int itemId, int countAdded) update(commodity); } + +ShopItemPtr CommodityService::shopItem(int itemId) +{ + CommodityDataPtr item = this->loadById(itemId); + return qSharedPointerDynamicCast(item); +} diff --git a/commodity/commodityservice.h b/commodity/commodityservice.h index 4f67690..0e10d97 100644 --- a/commodity/commodityservice.h +++ b/commodity/commodityservice.h @@ -12,8 +12,10 @@ public: // ISellableService interface public: - QList > shopItems() override; + QList shopItems() override; void addedToVoucher(int itemId, int countAdded) override; + virtual ShopItemPtr shopItem(int itemId) override; + }; #endif // COMMODITYSERVICE_H diff --git a/commodity/data/commoditydata.h b/commodity/data/commoditydata.h index 462b4f1..1655dc9 100644 --- a/commodity/data/commoditydata.h +++ b/commodity/data/commoditydata.h @@ -68,4 +68,6 @@ public: QString pluginId() override; }; +typedef QSharedPointer CommodityDataPtr; + #endif // COMMODITYDATA_H diff --git a/shop/data/favorititem.cpp b/shop/data/favorititem.cpp index 3d7e5e3..3bbe1ff 100644 --- a/shop/data/favorititem.cpp +++ b/shop/data/favorititem.cpp @@ -67,3 +67,13 @@ void FavoritItem::setFavButtonName(const QString &favButtonName) { m_favButtonName = favButtonName; } + +int FavoritItem::refId() const +{ + return m_refId; +} + +void FavoritItem::setRefId(int refId) +{ + m_refId = refId; +} diff --git a/shop/data/favorititem.h b/shop/data/favorititem.h index c569838..6650997 100644 --- a/shop/data/favorititem.h +++ b/shop/data/favorititem.h @@ -17,6 +17,7 @@ class FavoritItem : public QObject, public IShopItem Q_OBJECT Q_PROPERTY(int id READ id WRITE setId) + Q_PROPERTY(int refId READ refId WRITE setRefId) Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(QDecDouble unitPrice READ unitPrice WRITE setUnitPrice) Q_PROPERTY(Enums::VatType vatType READ vatType WRITE setVatType) @@ -46,10 +47,14 @@ public: QString favButtonName() const; void setFavButtonName(const QString &favButtonName); + int refId() const; + void setRefId(int refId); + private: friend class odb::access; #pragma db id auto int m_id; + int m_refId; QString m_name; int m_unitPrice; Enums::VatType m_vatType; diff --git a/shop/favbutton.h b/shop/favbutton.h new file mode 100644 index 0000000..f05b3a2 --- /dev/null +++ b/shop/favbutton.h @@ -0,0 +1,22 @@ +#ifndef FAVBUTTON_H +#define FAVBUTTON_H + +#include + +class FavButton : public QToolButton +{ + Q_OBJECT + +public: + FavButton(QWidget *parent = 0); + +signals: + void itemDropped(); + + // QWidget interface +protected: + virtual void dragEnterEvent(QDragEnterEvent *event) override; + virtual void dropEvent(QDropEvent *event) override; +}; + +#endif // FAVBUTTON_H diff --git a/shop/isellableservice.h b/shop/isellableservice.h index dc778c0..f1b3020 100644 --- a/shop/isellableservice.h +++ b/shop/isellableservice.h @@ -11,7 +11,8 @@ class SHOPSHARED_EXPORT ISellableService public: ISellableService(); - virtual QList > shopItems() = 0; + virtual QList shopItems() = 0; + virtual ShopItemPtr shopItem(int itemId) = 0; virtual void addedToVoucher(int itemId, int countAdded) = 0; }; diff --git a/shop/settings/favoriteservice.cpp b/shop/settings/favoriteservice.cpp deleted file mode 100644 index b90ace2..0000000 --- a/shop/settings/favoriteservice.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "favoriteservice.h" - -FavoriteService::FavoriteService() -{ - -} diff --git a/shop/settings/favoriteservice.h b/shop/settings/favoriteservice.h deleted file mode 100644 index d99648e..0000000 --- a/shop/settings/favoriteservice.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef FAVORITESERVICE_H -#define FAVORITESERVICE_H - -#include -#include "data/favorititem.h" - -class FavoriteService : public Service -{ -public: - FavoriteService(); -}; - -#endif // FAVORITESERVICE_H diff --git a/shop/settings/shopsettings.cpp b/shop/settings/shopsettings.cpp index 38e6ad5..f28d39b 100644 --- a/shop/settings/shopsettings.cpp +++ b/shop/settings/shopsettings.cpp @@ -9,6 +9,10 @@ ShopSettings::ShopSettings(QObject *parent) : QObject(parent) m_eetMode = 1; m_eetTest = 0; m_eetPlayground = 0; + + m_favBtnCols = 0; + m_favBtnRows = 0; + m_favBtnSize = 0; } QString ShopSettings::output() const @@ -130,3 +134,33 @@ void ShopSettings::setEetPlayground(bool eetPlayground) { m_eetPlayground = eetPlayground; } + +int ShopSettings::favBtnCols() const +{ + return m_favBtnCols; +} + +void ShopSettings::setFavBtnCols(int favBtnCols) +{ + m_favBtnCols = favBtnCols; +} + +int ShopSettings::favBtnRows() const +{ + return m_favBtnRows; +} + +void ShopSettings::setFavBtnRows(int favBtnRows) +{ + m_favBtnRows = favBtnRows; +} + +int ShopSettings::favBtnSize() const +{ + return m_favBtnSize; +} + +void ShopSettings::setFavBtnSize(int favBtnSize) +{ + m_favBtnSize = favBtnSize; +} diff --git a/shop/settings/shopsettings.h b/shop/settings/shopsettings.h index d57f892..ce68603 100644 --- a/shop/settings/shopsettings.h +++ b/shop/settings/shopsettings.h @@ -18,6 +18,10 @@ class ShopSettings : public QObject Q_PROPERTY(bool eetTest READ eetTest WRITE setEetTest) Q_PROPERTY(bool eetPlayground READ eetPlayground WRITE setEetPlayground) + Q_PROPERTY(int favBtnCols READ favBtnCols WRITE setFavBtnCols) + Q_PROPERTY(int favBtnRows READ favBtnRows WRITE setFavBtnRows) + Q_PROPERTY(int favBtnSize READ favBtnSize WRITE setFavBtnSize) + Q_OBJECT public: @@ -64,6 +68,15 @@ public: bool eetPlayground() const; void setEetPlayground(bool eetPlayground); + int favBtnCols() const; + void setFavBtnCols(int favBtnCols); + + int favBtnRows() const; + void setFavBtnRows(int favBtnRows); + + int favBtnSize() const; + void setFavBtnSize(int favBtnSize); + private: QString m_output; CODEPAGE m_codepage; @@ -78,6 +91,10 @@ private: QString m_eetKeyPassword; bool m_eetTest; bool m_eetPlayground; + + int m_favBtnCols; + int m_favBtnRows; + int m_favBtnSize; }; typedef QSharedPointer ShopSettingsPtr; diff --git a/shop/settings/shopsettingsform.cpp b/shop/settings/shopsettingsform.cpp index b919ee9..2ea5c8c 100644 --- a/shop/settings/shopsettingsform.cpp +++ b/shop/settings/shopsettingsform.cpp @@ -4,7 +4,11 @@ #include #include #include +#include +#include #include "shopservice.h" +#include "shop-odb.hxx" + ShopSettingsForm::ShopSettingsForm(QWidget *parent) : FormBinder(parent), @@ -27,7 +31,13 @@ ShopSettingsForm::ShopSettingsForm(QWidget *parent) : registerBinding(ui->eetTest); registerBinding(ui->eetPlayground); + registerBinding(ui->favBtnCols); + registerBinding(ui->favBtnRows); + registerBinding(ui->favBtnSize); + m_itemModel = new AutoTableModel(); + + } ShopSettingsForm::~ShopSettingsForm() @@ -35,6 +45,51 @@ ShopSettingsForm::~ShopSettingsForm() delete ui; } +void ShopSettingsForm::drawButtons() +{ + foreach (QWidget *child, ui->btnWidget->findChildren()) { + 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()] != NULL) + { + btn->setText(m_btnMap[btn->objectName()]->name()); + } + + if (entity()->favBtnSize() > 0) + { + btn->setMinimumHeight(entity()->favBtnSize()); + btn->setMinimumWidth(entity()->favBtnSize()); + } + ((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](){ + ShopItemPtr item = m_itemModel->itemFromIndex(ui->tableItems->currentIndex()); + FavoritItemPtr favItem = QSharedPointer(new FavoritItem); + favItem->setFavButtonName(btn->objectName()); + favItem->setName(item->name()); + favItem->setRefId(item->id()); + favItem->setPluginId(item->pluginId()); + m_btnMap[btn->objectName()] = favItem; + btn->setText(item->name()); + }); + } + } +} + void ShopSettingsForm::loadEntity() { SettingsService srv("SHOP"); @@ -43,7 +98,18 @@ void ShopSettingsForm::loadEntity() 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(3, true); + + Service srvFav; + + foreach (FavoritItemPtr favItem, srvFav.all()) { + m_btnMap[favItem->favButtonName()] = favItem; + } + drawButtons(); } bool ShopSettingsForm::saveRecord() @@ -52,6 +118,18 @@ bool ShopSettingsForm::saveRecord() SettingsService srv("SHOP"); srv.saveSettings(entity()); + Service srvFav; + foreach (FavoritItemPtr item, srvFav.all()) { + srvFav.erase(item); + } + + foreach (QString btnName, m_btnMap.keys()) { + if (m_btnMap[btnName] != NULL) + { + srvFav.save(m_btnMap[btnName]); + } + } + return true; } @@ -64,3 +142,35 @@ void ShopSettingsForm::on_btnCertBrowse_clicked() 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(); +} + +FavButton::FavButton(QWidget *parent) : QToolButton(parent) +{ +} + +void FavButton::dragEnterEvent(QDragEnterEvent *event) +{ + event->acceptProposedAction(); +} + +void FavButton::dropEvent(QDropEvent *) +{ + emit itemDropped(); +} diff --git a/shop/settings/shopsettingsform.h b/shop/settings/shopsettingsform.h index ecefb41..ca3db10 100644 --- a/shop/settings/shopsettingsform.h +++ b/shop/settings/shopsettingsform.h @@ -2,10 +2,14 @@ #define SHOPSETTINGSFORM_H #include +#include #include +#include #include "shopsettings.h" #include #include "shopitem.h" +#include "data/favorititem.h" +#include "favbutton.h" namespace Ui { class ShopSettingsForm; @@ -22,6 +26,11 @@ public: private: Ui::ShopSettingsForm *ui; AutoTableModel *m_itemModel; + int m_favBtnRows; + int m_favBtnCols; + int m_favBtnSize; + QMap m_btnMap; + void drawButtons(); // IForm interface public: @@ -29,8 +38,12 @@ public: public slots: bool saveRecord(); + private slots: void on_btnCertBrowse_clicked(); + void on_favBtnCols_textChanged(const QString &arg1); + void on_favBtnRows_textChanged(const QString &arg1); + void on_favBtnSize_textChanged(const QString &arg1); }; #endif // SHOPSETTINGSFORM_H diff --git a/shop/settings/shopsettingsform.ui b/shop/settings/shopsettingsform.ui index b3ba813..5bdc9f5 100644 --- a/shop/settings/shopsettingsform.ui +++ b/shop/settings/shopsettingsform.ui @@ -6,8 +6,8 @@ 0 0 - 645 - 463 + 760 + 505 @@ -33,7 +33,7 @@ true - QAbstractItemView::DragDrop + QAbstractItemView::DragOnly QAbstractItemView::SingleSelection @@ -48,150 +48,74 @@ - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - + - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - + + - - - - - 75 - 75 - - - - ... + + + + Qt::Vertical - - - - - + - 75 - 75 + 20 + 40 - - ... - - + - - - - 75 - 75 - - - - ... - - - - - + - Qt::Vertical + Qt::Horizontal - 20 - 40 + 40 + 20 + + + + + + + Columns + + + + + + + + + + Rows + + + + + + + + + + Size + + + + + + + + + diff --git a/shop/shop.json b/shop/shop.json index 92e2b84..100bdec 100644 --- a/shop/shop.json +++ b/shop/shop.json @@ -8,7 +8,7 @@ "default" : "", "CZ" : "" }, - "schemaVersion" : 3, + "schemaVersion" : 4, "sql" : [ "CREATE TABLE \"VoucherItem\" ( \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, @@ -60,6 +60,16 @@ CREATE TABLE \"Voucher\" ( "ALTER TABLE \"VoucherItem\" ADD \"insertDate\" TEXT NULL; ", "ALTER TABLE \"Voucher\" ADD \"saveDateTime\" TEXT NULL; +", + +"CREATE TABLE \"FavoritItem\" ( + \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + \"refId\" INTEGER NOT NULL, + \"name\" TEXT NULL, + \"unitPrice\" INTEGER NOT NULL, + \"vatType\" INTEGER NOT NULL, + \"pluginId\" TEXT NULL, + \"favButtonName\" TEXT NULL); " ], "dependencies" : [ "ADDRESSBOOK" ], diff --git a/shop/shop.pro b/shop/shop.pro index f6e9f90..3a4210f 100644 --- a/shop/shop.pro +++ b/shop/shop.pro @@ -31,7 +31,6 @@ SOURCES += shop.cpp \ shopitem.cpp \ isellableservice.cpp \ data/favorititem.cpp \ - settings/favoriteservice.cpp \ eetbatchdialog.cpp HEADERS += shop.h\ @@ -55,8 +54,8 @@ HEADERS += shop.h\ paydvouchersdialog.h \ shopitem.h \ data/favorititem.h \ - settings/favoriteservice.h \ - eetbatchdialog.h + eetbatchdialog.h \ + favbutton.h include(../config_plugin.pri) diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 11216f6..4c2d407 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -13,6 +13,8 @@ #include #include #include +#include "data/favorititem.h" +#include "favbutton.h" #include "shop-odb.hxx" @@ -36,6 +38,8 @@ ShopForm::~ShopForm() void ShopForm::loadLast() { + loadButtons(); + if (m_itemsModel == NULL) { m_itemsModel = new AutoTableModel(this); @@ -91,6 +95,53 @@ void ShopForm::loadLast() } } +void ShopForm::loadButtons() +{ + SettingsService srv("SHOP"); + ShopSettingsPtr settings = srv.loadSettings(); + + Service srvFav; + QMap btnMap; + + foreach (FavoritItemPtr item, srvFav.all()) { + btnMap[item->favButtonName()] = item; + } + + for (int i = 0; i < settings->favBtnRows(); i++) + { + for (int j = 0; j < settings->favBtnCols(); j++) + { + FavButton *btn = new FavButton(ui->favorites); + QString btnName = QString::number(i) + "_" + QString::number(j); + btn->setObjectName(btnName); + + if (settings->favBtnSize() > 0) + { + btn->setMinimumHeight(settings->favBtnSize()); + btn->setMinimumWidth(settings->favBtnSize()); + } + ((QGridLayout*)ui->favorites->layout())->addWidget(btn, i + 1, j); + + if (btnMap[btnName] != NULL) + { + btn->setText(btnMap[btnName]->name()); + connect(btn, &FavButton::clicked, [this, btnMap, btn](bool){ + FavoritItemPtr item = btnMap[btn->objectName()]; + + IPlugin *plugin = Context::instance().plugin(item->pluginId()); + IService *service = (plugin != NULL ? plugin->service() : NULL); + ISellableService *selSrv = dynamic_cast(service); + + if (selSrv != NULL) + { + addItem(selSrv->shopItem(item->refId()), 1); + } + }); + } + } + } +} + void ShopForm::fillRaceiptCombo() { bool oldState = ui->receiptCombo->blockSignals(true); @@ -430,7 +481,7 @@ void ShopForm::on_commoditySearch_textChanged(const QString &text) } } -void ShopForm::on_lblEetState_linkActivated(const QString &link) +void ShopForm::on_lblEetState_linkActivated(const QString &) { ShopService srv; srv.setEetOnline(!srv.isEetOnline()); diff --git a/shop/shopform.h b/shop/shopform.h index dee98dd..42ac1f8 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -21,6 +21,7 @@ public: explicit ShopForm(QWidget *parent = 0); ~ShopForm(); void loadLast(); + void loadButtons(); void fillRaceiptCombo(); private slots: diff --git a/shop/shopform.ui b/shop/shopform.ui index 35de415..3ed8b9c 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -6,7 +6,7 @@ 0 0 - 959 + 975 643 @@ -108,21 +108,8 @@ - + - - - - Qt::Horizontal - - - - 40 - 20 - - - - @@ -157,220 +144,22 @@ - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - - 75 - 75 - - - - ... - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - + + + + Qt::Horizontal + + + + 40 + 20 + + + +