closes #4: Favorite buttons has been extended with category buttons.

This commit is contained in:
2023-05-17 21:31:32 +02:00
parent 5b5f31f64d
commit 5b77337b3f
33 changed files with 596 additions and 106 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ QX_REGISTER_CPP_SHOP(FavoritItem)
QX_REGISTER_ALL_QT_PROPERTIES(FavoritItem, "id")
long FavoritItem::id()
long FavoritItem::id() const
{
return m_id;
}
@@ -55,7 +55,7 @@ void FavoritItem::setVatType(const Enums::VatType &vatType)
m_vatType = vatType;
}
QString FavoritItem::pluginId()
QString FavoritItem::pluginId() const
{
return m_pluginId;
}
+3 -3
View File
@@ -30,13 +30,13 @@ public:
// IShopItem interface
public:
long id() override;
long id() const override;
void setId(long id);
QString name() override;
void setName(const QString &name);
virtual QString shortName() override;
QString shortName() override;
void setShortName(const QString &shortName);
QDecDouble unitPrice() override;
@@ -45,7 +45,7 @@ public:
Enums::VatType vatType() override;
void setVatType(const Enums::VatType &vatType);
QString pluginId() override;
QString pluginId() const override;
void setPluginId(const QString &pluginId);
QString favButtonName() const;
+2 -2
View File
@@ -6,7 +6,7 @@ DirectSaleItem::DirectSaleItem(QObject *parent) : IShopItem(parent)
m_vat = Enums::NONE;
}
long DirectSaleItem::id()
long DirectSaleItem::id() const
{
return 0;
}
@@ -26,7 +26,7 @@ QDecDouble DirectSaleItem::unitPrice()
return m_unitPrice;
}
QString DirectSaleItem::pluginId()
QString DirectSaleItem::pluginId() const
{
return "";
}
+2 -2
View File
@@ -22,11 +22,11 @@ public slots:
// IShopItem interface
public:
long id() override;
long id() const override;
QString name() override;
QString shortName() override;
QDecDouble unitPrice() override;
QString pluginId() override;
QString pluginId() const override;
Enums::VatType vatType() override;
int count() const;
+5 -5
View File
@@ -8,11 +8,11 @@ class FavButtonStyle : public QProxyStyle
{
public:
FavButtonStyle();
virtual ~FavButtonStyle();
~FavButtonStyle() override = default;
// QStyle interface
public:
void drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole) const;
void drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole) const override;
private:
Q_DISABLE_COPY(FavButtonStyle)
@@ -23,15 +23,15 @@ class FavButton : public QToolButton
Q_OBJECT
public:
FavButton(QWidget *parent = 0);
explicit FavButton(QWidget *parent = nullptr);
signals:
void itemDropped();
// QWidget interface
protected:
virtual void dragEnterEvent(QDragEnterEvent *event) override;
virtual void dropEvent(QDropEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
private:
FavButtonStyle m_style;
+1 -1
View File
@@ -12,7 +12,7 @@ class SHOPSHARED_EXPORT ISellableService
public:
ISellableService();
virtual QList<IShopItemPtr> shopItems() = 0;
virtual QList<IShopItemPtr> shopItems(const QString& category = "") = 0;
virtual IShopItemPtr shopItem(int itemId) = 0;
virtual void addedToVoucher(int itemId, int countAdded) = 0;
virtual ISeller *seller() = 0;
+10 -2
View File
@@ -17,20 +17,28 @@ class SHOPSHARED_EXPORT IShopItem : public QObject
Q_PROPERTY(QString shortName READ shortName)
Q_PROPERTY(QDecDouble unitPrice READ unitPrice)
Q_PROPERTY(Enums::VatType vatType READ vatType)
Q_PROPERTY(bool favorite READ favorite)
public:
explicit IShopItem(QObject* parent = nullptr);
~IShopItem() override = default;
virtual long id() { return {}; }
virtual long id() const { return {}; }
virtual QString name() { return {}; }
virtual QString code() { return {}; }
virtual QString shortName() { return {}; }
virtual QDecDouble unitPrice() { return {}; }
virtual Enums::VatType vatType() { return {}; }
virtual QString pluginId() { return {}; }
virtual QString pluginId() const { return {}; }
virtual QString color() { return {}; }
virtual QString category() { return {}; }
virtual bool favorite() { return false; }
};
inline bool operator==(const IShopItem& lhs, const IShopItem& rhs) {
return lhs.id() == rhs.id() && lhs.pluginId() == rhs.pluginId();
}
using IShopItemPtr = QSharedPointer<IShopItem>;
QX_REGISTER_HPP_SHOP(IShopItem, QObject, 0)
+1 -7
View File
@@ -1,8 +1,6 @@
#include "shopsettingsform.h"
#include "ui_shopsettingsform.h"
#include <settingsservice.h>
#include <combodata.h>
#include <QFileDialog>
#include <QDragEnterEvent>
#include <QDebug>
@@ -146,7 +144,7 @@ bool ShopSettingsForm::saveRecord()
}
foreach (QString btnName, m_btnMap.keys()) {
if (m_btnMap[btnName] != NULL)
if (m_btnMap[btnName] != nullptr)
{
srvFav.save(m_btnMap[btnName]);
}
@@ -192,10 +190,6 @@ FavButtonStyle::FavButtonStyle()
{
}
FavButtonStyle::~FavButtonStyle()
{
}
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;
+122 -8
View File
@@ -145,6 +145,7 @@ void ShopForm::loadLast()
m_commodityModel->setData(srv.allSellableItems());
ui->commodityTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->commodityTable->setColumnHidden(4, true);
ui->commodityTable->setColumnHidden(5, true);
ui->commodityTable->setColumnHidden(2, true);
ui->commodityTable->setColumnWidth(3, 90);
@@ -167,22 +168,20 @@ void ShopForm::loadButtons(const ShopSettingsPtr& settings)
Service<FavoritItem> srvFav;
QMap<QString, FavoritItemPtr> btnMap;
foreach (QWidget *child, ui->favorites->findChildren<QWidget*>()) {
if (child->objectName() != "directSale")
{
delete child;
}
}
clearFavButtons();
foreach (FavoritItemPtr item, srvFav.all()) {
btnMap[item->favButtonName()] = item;
}
ShopService srv;
auto allItems = srv.allSellableItems();
for (int i = 0; i < settings->favBtnRows(); i++)
{
for (int j = 0; j < settings->favBtnCols(); j++)
{
FavButton *btn = new FavButton(ui->favorites);
auto *btn = new QToolButton(ui->favorites);
QString btnName = QString::number(i) + "_" + QString::number(j);
btn->setObjectName(btnName);
@@ -197,6 +196,16 @@ void ShopForm::loadButtons(const ShopSettingsPtr& settings)
if (btnMap[btnName] != nullptr)
{
btn->setText(btnMap[btnName]->shortName());
auto shItem = std::find_if(allItems.begin(), allItems.end(),[btnMap, btn](auto it) -> bool {
return btnMap[btn->objectName()]->refId() == it->id() && btnMap[btn->objectName()]->pluginId() == it->pluginId();
});
if (shItem != allItems.end()) {
btn->setStyleSheet("background: " + shItem->data()->color() + ";");
btn->update();
}
connect(btn, &FavButton::clicked, [this, btnMap, btn](bool){
FavoritItemPtr item = btnMap[btn->objectName()];
@@ -249,6 +258,7 @@ void ShopForm::setupForm()
loadLast();
fillReceiptCombo();
loadButtons(settings);
loadCatButtons();
}
void ShopForm::on_directSale_clicked()
@@ -514,7 +524,7 @@ void ShopForm::recalculate()
if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0)
{
srv.saveVoucher(m_voucher);
srv.save(m_voucher);
}
else
{
@@ -664,3 +674,107 @@ void ShopForm::on_actualReceipt_customContextMenuRequested(const QPoint &pos)
QPoint globalPos = ui->actualReceipt->mapToGlobal(pos);
m_itemCtxMenu->exec(globalPos);
}
void ShopForm::clearFavButtons() {
foreach (QWidget *child, ui->favorites->findChildren<QWidget*>()) {
if (child->objectName() != "directSale")
{
delete child;
}
}
}
void ShopForm::loadFavCatButtons(const QString& category) {
SettingsService srvSettings("SHOP");
ShopSettingsPtr settings = srvSettings.loadSettings<ShopSettings>();
if (category.isEmpty()) {
loadButtons(settings);
return;
}
clearFavButtons();
ShopService srv;
auto all = srv.allSellableItems(category);
if (all.count() == 0) {
return;
}
auto square = std::sqrt(all.count());
int cols = static_cast<int>(square);
int rows = cols;
if (square - cols != 0) {
if (cols > 1) {
++rows;
} else {
++cols;
}
}
if (cols * rows < all.count()) {
++cols;
}
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (index >= all.count()) {
break;
}
auto item = all[index];
++index;
auto *btn = new QToolButton(ui->favorites);
if (settings->favBtnSize() > 0)
{
btn->setMinimumHeight(settings->favBtnSize());
btn->setMinimumWidth(settings->favBtnSize());
}
btn->setText(item->shortName());
btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
((QGridLayout*)ui->favorites->layout())->addWidget(btn, i + 1, j);
btn->setStyleSheet("background: " + item->color() + ";");
btn->update();
connect(btn, &QToolButton::clicked, [this, item](bool){
addItem(item, 1);
});
}
}
}
void ShopForm::loadCatButtons() {
ShopService srv;
auto allCat = srv.allCategories();
for (const auto btnFav : ui->widgetCategories->findChildren<QWidget*>()) {
if (btnFav->objectName() != "btnFav") {
delete btnFav;
}
}
for (const auto& cat : allCat.keys()) {
auto catButton = new QToolButton(ui->widgetCategories);
catButton->setText(cat);
catButton->setMinimumHeight(45);
catButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
catButton->setStyleSheet("background: " + allCat[cat] + ";");
catButton->update();
connect(catButton, &QPushButton::clicked, [this, cat](bool){
loadFavCatButtons(cat);
});
ui->widgetCategories->layout()->addWidget(catButton);
}
}
void ShopForm::on_btnFav_clicked() {
loadFavCatButtons("");
}
+5
View File
@@ -42,6 +42,8 @@ private slots:
void on_payButton_clicked();
void on_btnFav_clicked();
void on_showPaidButton_clicked();
void on_btnAddItem_clicked();
@@ -67,6 +69,9 @@ private:
void loadLast();
void loadButtons(const ShopSettingsPtr& settings);
void loadFavCatButtons(const QString& category);
void loadCatButtons();
void clearFavButtons();
void fillReceiptCombo();
void createVoucher();
void doTempSave(bool comboChanged);
+67 -43
View File
@@ -35,6 +35,73 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="widgetButtons" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QToolButton" name="directSale">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Direct Sell</string>
</property>
<property name="icon">
<iconset resource="shoprc.qrc">
<normaloff>:/icons/shop.svg</normaloff>:/icons/shop.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>45</width>
<height>45</height>
</size>
</property>
<property name="shortcut">
<string>Ctrl+D</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widgetCategories" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QPushButton" name="btnFav">
<property name="minimumSize">
<size>
<width>0</width>
<height>45</height>
</size>
</property>
<property name="text">
<string>Favorities</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="favorites" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QGridLayout" name="gridLayout"/>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widgetComodity" native="true">
<layout class="QVBoxLayout" name="verticalLayout_3">
@@ -110,49 +177,6 @@
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widgetButtons" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="favorites" native="true">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QToolButton" name="directSale">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Direct Sell</string>
</property>
<property name="icon">
<iconset resource="shoprc.qrc">
<normaloff>:/icons/shop.svg</normaloff>:/icons/shop.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>45</width>
<height>45</height>
</size>
</property>
<property name="shortcut">
<string>Ctrl+D</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextUnderIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QVBoxLayout" name="verticalLayout_6">
+2 -2
View File
@@ -25,13 +25,13 @@ public slots:
// IShopItem interface
public:
long id() override { return 0; }
long id() const override { return 0; }
QString code() override { return ""; }
QString name() override { return ""; }
QString shortName() override { return ""; }
QDecDouble unitPrice() override { return {}; }
Enums::VatType vatType() override { return Enums::NONE; }
QString pluginId() override { return ""; }
QString pluginId() const override { return ""; }
};
typedef QSharedPointer<ShopItem> ShopItemPtr;
+50 -5
View File
@@ -371,7 +371,7 @@ void ShopService::moveItems(QList<VoucherItemPtr> items, VoucherPtr source, Vouc
if (target->status() == Voucher::NEW && target->id() == 0)
{
this->saveVoucher(target, &session);
this->save(target, &session);
}
for (const auto& it : items) {
@@ -415,7 +415,7 @@ QList<VoucherPtr> ShopService::vouchersForEet()
.arg(QString::number(Voucher::PAID), QString::number(Voucher::EET_SENT), QString::number(Voucher::EET_NOT_ENTERING)));
}
QList<IShopItemPtr> ShopService::allSellableItems()
QList<IShopItemPtr> ShopService::allSellableItems(const QString& category/* = ""*/)
{
QList<QSharedPointer<IShopItem> > items;
foreach (IPlugin *plugin, Context::instance().plugins()) {
@@ -424,7 +424,7 @@ QList<IShopItemPtr> ShopService::allSellableItems()
if (selSrv != nullptr)
{
items.append(selSrv->shopItems());
items.append(selSrv->shopItems(category));
}
}
@@ -491,13 +491,58 @@ QDecDouble ShopService::vatRate(Enums::VatType vatType)
return vatRate;
}
void ShopService::saveVoucher(VoucherPtr entity, qx::QxSession* pSession/* = nullptr*/)
void ShopService::save(VoucherPtr entity, qx::QxSession* pSession/* = nullptr*/)
{
SeasonService seasonSrv;
SeasonPtr season = seasonSrv.active();
entity->setSeason(season);
addDateAndUser(entity, true);
save(entity, pSession);
Service::save(entity, pSession);
}
QMap<QString, QString> ShopService::allCategories() {
QMap<QString, QString> ret;
for (const auto& item : allSellableItems()) {
if (!item->category().isEmpty() && !ret.contains(item->category())) {
ret[item->category()] = item->color();
}
}
return ret;
}
void ShopService::update(VoucherPtr entity, qx::QxSession* pSession) {
bool reloadItems = pSession == nullptr;
{
QScopedPointer<qx::QxSession> ptrSession;
if (pSession == nullptr) {
ptrSession.reset(new qx::QxSession());
pSession = ptrSession.data();
}
Service::update(entity, pSession);
auto oldItems = entity->items();
entity->clearItems();
load(entity);
for (auto item : entity->items()) {
auto newItem = std::find_if(oldItems.begin(), oldItems.end(), [item](auto it){
return item->id() == it->id();
});
if (newItem == oldItems.end()) {
qx::dao::delete_by_id(item, pSession->database());
}
}
}
if (reloadItems) {
entity->clearItems();
load(entity);
}
}
+4 -2
View File
@@ -27,7 +27,8 @@ public:
void calculate(VoucherPtr voucher);
void calculateItem(VoucherItemPtr item);
void pay(VoucherPtr voucher);
void saveVoucher(VoucherPtr entity, qx::QxSession* pSession = nullptr);
void save(VoucherPtr entity, qx::QxSession* pSession = nullptr) override;
void update(VoucherPtr entity, qx::QxSession* pSession = nullptr) override;
void moveItems(QList<VoucherItemPtr> items, VoucherPtr source, VoucherPtr target);
void updateRelatedItem(VoucherItem* item, int countAdded);
bool processEet(VoucherPtr voucher, QString &message);
@@ -40,7 +41,8 @@ public:
QList<VoucherPtr> tempVouchers();
QList<VoucherPtr> paiedVouchers();
QList<VoucherPtr> vouchersForEet();
QList<IShopItemPtr> allSellableItems();
QList<IShopItemPtr> allSellableItems(const QString& category = "");
QMap<QString, QString> allCategories();
VoucherSum unpaidSummary();
VoucherSum unsendEET();