diff --git a/camp/campgrid.cpp b/camp/campgrid.cpp index f0ae749..6ddb7f2 100644 --- a/camp/campgrid.cpp +++ b/camp/campgrid.cpp @@ -125,7 +125,7 @@ void CampGrid::addToVoucher(const CampDataPtr& data) shopSrv.addShopItem(voucher, campItem, 1); shopSrv.calculate(voucher); - shopSrv.saveVoucher(voucher); + shopSrv.save(voucher); data->setOnVoucher(true); diff --git a/camp/campservice.cpp b/camp/campservice.cpp index 4c4e5a4..39ebb08 100644 --- a/camp/campservice.cpp +++ b/camp/campservice.cpp @@ -357,8 +357,12 @@ void CampService::addAccFee(const CampDataPtr& data, const AddressItemPtr& item, } } -QList CampService::shopItems() +QList CampService::shopItems(const QString& category) { + if (!category.isEmpty()) { + return {}; + } + CampShopItemPtr item(new CampShopItem); QList items; diff --git a/camp/campservice.h b/camp/campservice.h index 68d4ff4..c4de1a1 100644 --- a/camp/campservice.h +++ b/camp/campservice.h @@ -36,7 +36,7 @@ private: // ISellableService interface public: - QList shopItems() override; + QList shopItems(const QString& category = "") override; IShopItemPtr shopItem(int itemId) override; void addedToVoucher(int itemId, int countAdded) override; ISeller *seller() override; diff --git a/camp/campshopitem.cpp b/camp/campshopitem.cpp index b396e44..f9286f3 100644 --- a/camp/campshopitem.cpp +++ b/camp/campshopitem.cpp @@ -28,7 +28,7 @@ Enums::VatType CampShopItem::vatType() return m_vatType; } -QString CampShopItem::pluginId() +QString CampShopItem::pluginId() const { return "CAMP"; } @@ -48,7 +48,7 @@ void CampShopItem::setVatType(const Enums::VatType &vatType) m_vatType = vatType; } -long CampShopItem::id() +long CampShopItem::id() const { return m_id; } diff --git a/camp/campshopitem.h b/camp/campshopitem.h index 1faed75..c2e0af6 100644 --- a/camp/campshopitem.h +++ b/camp/campshopitem.h @@ -10,12 +10,12 @@ public: // IShopItem interface public: - long id() override; + long id() const override; QString name() override; QString shortName() override; QDecDouble unitPrice() override; Enums::VatType vatType() override; - QString pluginId() override; + QString pluginId() const override; // ShopItem interface public: diff --git a/commodity/CMakeLists.txt b/commodity/CMakeLists.txt index 7290576..00d335b 100644 --- a/commodity/CMakeLists.txt +++ b/commodity/CMakeLists.txt @@ -40,7 +40,7 @@ add_library(commodity SHARED data/commoditytypedata.h settings/commoditysettings.cpp settings/commoditysettings.h - ) + coloritemdelegate.cpp coloritemdelegate.h) target_compile_definitions(commodity PRIVATE -DCOMMODITY_LIBRARY) diff --git a/commodity/coloritemdelegate.cpp b/commodity/coloritemdelegate.cpp new file mode 100644 index 0000000..d54c2ed --- /dev/null +++ b/commodity/coloritemdelegate.cpp @@ -0,0 +1,175 @@ +// +// Created by Josef Rokos on 03.05.2023. +// + +#include "coloritemdelegate.h" +#include +#include +#include +#include +#include + +ColorLabel::ColorLabel(QWidget* parent) : QWidget(parent), m_color(Qt::white) { + setAttribute(Qt::WA_StaticContents); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); + setFocusPolicy(Qt::NoFocus); +} + +QColor ColorLabel::color() const { + return m_color; +} + +void ColorLabel::setColor(const QColor& color) { + m_color = color; +} + +QSize ColorLabel::sizeHint() const { + return {20,20}; +} + +void ColorLabel::paintEvent(QPaintEvent* event) { + QPainter painter(this); + + QStyle* style = QApplication::style(); + painter.save(); + painter.setBrush(m_color); + + bool dark = false; + qreal darkness = 1-(0.299*m_color.red() + 0.587*m_color.green() + 0.114*m_color.blue())/255; + if(darkness >= 0.5){ + dark = true; + } + + QColor penColor = dark ? Qt::transparent : Qt::darkGray; + + painter.setPen(penColor); + int border = (event->rect().height() - style->pixelMetric(QStyle::PM_IndicatorWidth))/2; + + QRect rect(event->rect().x()+border, event->rect().y()+border, + style->pixelMetric(QStyle::PM_IndicatorWidth), + style->pixelMetric(QStyle::PM_IndicatorWidth));// = option.rect.adjusted(4,4,-4,-6); + + painter.drawRect(rect); + painter.restore(); +} + +ColorItemEditor::ColorItemEditor(QWidget* parent) : QWidget(parent), m_buttonPressed(false) +{ + m_colorIndicator = new ColorLabel(this); + m_colorIndicator->setColor(m_color); + m_button = new QToolButton(this); + m_button->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); + m_button->setText("..."); + m_button->installEventFilter(this); + QHBoxLayout* layout = new QHBoxLayout(this); + layout->addWidget(m_colorIndicator); + layout->addWidget(m_button); + layout->setSpacing(0); + layout->setContentsMargins(1,1,1,1); + setFocusProxy(m_button); + setAutoFillBackground(true); + setLayout(layout); + setAutoFillBackground(true); + connect(m_button,SIGNAL(clicked()),this,SLOT(slotClicked())); +} + +void ColorItemEditor::setColor(const QColor& value) { + m_color = value; + m_colorIndicator->setColor(value); +} + +bool ColorItemEditor::eventFilter(QObject* obj, QEvent* event) { + if (obj == m_button){ + if (event->type() == QEvent::FocusOut && !m_buttonPressed){ + auto focusEvent = dynamic_cast(event); + if (focusEvent && focusEvent->reason()!=Qt::MouseFocusReason){ + setFocusToParent(); + emit(editingFinished()); + } + return false; + } + } + return false; +} + +void ColorItemEditor::setFocusToParent() { + if (parentWidget()) { + parentWidget()->setFocus(); + } +} + +void ColorItemEditor::slotClicked() { + m_buttonPressed = true; + auto dialog = new QColorDialog(this); + dialog->setCurrentColor(m_color); + + if (dialog->exec()) { + setColor(dialog->currentColor()); + } + + delete dialog; + setFocusToParent(); + emit(editingFinished()); +} + +ColorItemDelegate::ColorItemDelegate(QObject* parent) : QStyledItemDelegate(parent) { + +} + +void ColorItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + QColor color(index.data().toString()); + + if (color.isValid()) { + painter->save(); + painter->setBrush(color); + + QRect rect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height()); + painter->drawRect(rect); + painter->restore(); + } else { + QStyledItemDelegate::paint(painter, option, index); + } +} + +QSize ColorItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { + return QStyledItemDelegate::sizeHint(option, index); +} + +QWidget* ColorItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { + QColor color(index.data().toString()); + if (!color.isValid()) { + color = QColorConstants::Gray; + } + + auto editor = new ColorItemEditor(parent); + editor->setColor(color); + connect(editor, &ColorItemEditor::editingFinished, this, &ColorItemDelegate::commitAndCloseEditor); + return editor; +} + +void ColorItemDelegate::commitAndCloseEditor() { + auto editor = qobject_cast(sender()); + emit commitData(editor); + emit closeEditor(editor); +} + +void ColorItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { + auto edit = qobject_cast(editor); + QColor color(index.data().toString()); + + if (edit && color.isValid()) { + edit->setColor(color); + } else { + QStyledItemDelegate::setEditorData(editor, index); + } +} + +void ColorItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { + auto edit = qobject_cast(editor); + + if (edit) { + model->setData(index, edit->color().name()); + } else { + QStyledItemDelegate::setModelData(editor, model, index); + } +} diff --git a/commodity/coloritemdelegate.h b/commodity/coloritemdelegate.h new file mode 100644 index 0000000..f4c0729 --- /dev/null +++ b/commodity/coloritemdelegate.h @@ -0,0 +1,64 @@ +// +// Created by Josef Rokos on 03.05.2023. +// + +#ifndef COLORITEMDELEGATE_H +#define COLORITEMDELEGATE_H + +#include +#include + +class ColorItemDelegate : public QStyledItemDelegate { + Q_OBJECT +public: + explicit ColorItemDelegate(QObject* parent = nullptr); + ~ColorItemDelegate() override = default; + + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void setEditorData(QWidget *editor, const QModelIndex &index) const override; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + +private slots: + void commitAndCloseEditor(); + + +}; + +class ColorLabel : public QWidget{ +Q_OBJECT +public: + explicit ColorLabel(QWidget* parent = nullptr); + QColor color() const; + void setColor(const QColor &color); + QSize sizeHint() const override; +protected: + void paintEvent(QPaintEvent *event) override; +private: + QColor m_color; +}; + +class ColorItemEditor : public QWidget +{ +Q_OBJECT +public: + explicit ColorItemEditor(QWidget *parent = nullptr); + QColor color(){return m_color;} + void setColor(const QColor& value); +protected: + bool eventFilter(QObject *obj, QEvent *event) override; +private: + void setFocusToParent(); +signals: + void editingFinished(); +private slots: + void slotClicked(); +private: + QColor m_color; + QToolButton* m_button; + ColorLabel* m_colorIndicator; + bool m_buttonPressed; +}; + +#endif //COLORITEMDELEGATE_H diff --git a/commodity/commodity.json b/commodity/commodity.json index de892f8..ec44962 100644 --- a/commodity/commodity.json +++ b/commodity/commodity.json @@ -8,7 +8,7 @@ "default" : "", "CZ" : "" }, - "schemaVersion" : 1, + "schemaVersion" : 2, "sql" : [ "CREATE TABLE \"CommodityTypeData\" ( \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, @@ -26,8 +26,13 @@ CREATE TABLE \"CommodityData\" ( CONSTRAINT \"type_fk\" FOREIGN KEY (\"type\") REFERENCES \"CommodityTypeData\" (\"id\") - DEFERRABLE INITIALLY DEFERRED);" - + DEFERRABLE INITIALLY DEFERRED); +", +" +ALTER TABLE \"CommodityTypeData\" ADD \"color\" TEXT NULL; +ALTER TABLE \"CommodityData\" ADD \"favorite\" INTEGER NULL; +UPDATE \"CommodityData\" SET \"favorite\"=0; +" ], "dependencies" : [ "SHOP" ], "translations" : { diff --git a/commodity/commodityform.cpp b/commodity/commodityform.cpp index 6a645fd..51d824a 100644 --- a/commodity/commodityform.cpp +++ b/commodity/commodityform.cpp @@ -25,6 +25,7 @@ CommodityForm::CommodityForm(QWidget *parent) : << ComboData(Enums::SECOND_LOWER,tr("Second Lower")); registerBinding(ui->vat, vt); registerBinding(ui->count); + registerBinding(ui->favorite); m_codeAsNumber = false; } diff --git a/commodity/commodityform.ui b/commodity/commodityform.ui index 7c376d5..4993d28 100644 --- a/commodity/commodityform.ui +++ b/commodity/commodityform.ui @@ -94,6 +94,13 @@ + + + + Favorite + + + diff --git a/commodity/commodityservice.cpp b/commodity/commodityservice.cpp index 9613b3d..f70e206 100644 --- a/commodity/commodityservice.cpp +++ b/commodity/commodityservice.cpp @@ -1,11 +1,13 @@ #include "commodityservice.h" -QList > CommodityService::shopItems() +QList > CommodityService::shopItems(const QString& category/* = ""*/) { QList > ret; foreach (QSharedPointer data, all()) { - ret.append(qSharedPointerDynamicCast(data)); + if (category.isEmpty() || (data->favorite() && data->category() == category)) { + ret.append(qSharedPointerDynamicCast(data)); + } } return ret; diff --git a/commodity/commodityservice.h b/commodity/commodityservice.h index c1eda86..f5ec0ee 100644 --- a/commodity/commodityservice.h +++ b/commodity/commodityservice.h @@ -12,7 +12,7 @@ public: // ISellableService interface public: - QList shopItems() override; + QList shopItems(const QString& category = "") override; void addedToVoucher(int itemId, int countAdded) override; IShopItemPtr shopItem(int itemId) override; ISeller *seller() override; diff --git a/commodity/commoditysettingsform.cpp b/commodity/commoditysettingsform.cpp index 8a27df4..0266206 100644 --- a/commodity/commoditysettingsform.cpp +++ b/commodity/commoditysettingsform.cpp @@ -4,6 +4,8 @@ #include #include +#include "coloritemdelegate.h" + CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) : FormBinder(parent), ui(new Ui::CommoditySettingsForm) @@ -13,9 +15,10 @@ CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) : registerBinding(ui->codeAsNumber); m_table = new AutoTableModel(); - m_table->setEditableCols(QList() << 0); + m_table->setEditableCols(QList() << 0 << 1); ui->commodityTypesTable->setModel(m_table); ui->commodityTypesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + ui->commodityTypesTable->setItemDelegateForColumn(1, new ColorItemDelegate(this)); } CommoditySettingsForm::~CommoditySettingsForm() diff --git a/commodity/data/commoditydata.cpp b/commodity/data/commoditydata.cpp index ad94843..54173ca 100644 --- a/commodity/data/commoditydata.cpp +++ b/commodity/data/commoditydata.cpp @@ -13,6 +13,7 @@ namespace qx { t.data(&CommodityData::m_price, "price"); t.data(&CommodityData::m_vat, "vat"); t.data(&CommodityData::m_count, "count"); + t.data(&CommodityData::m_favorite, "favorite"); t.relationManyToOne(&CommodityData::m_type, "type"); } @@ -25,7 +26,7 @@ CommodityData::CommodityData(QObject *parent) m_price = 0; m_vat = Enums::NONE; } -long CommodityData::id() +long CommodityData::id() const { return m_id; } @@ -114,7 +115,7 @@ Enums::VatType CommodityData::vatType() return vat(); } -QString CommodityData::pluginId() +QString CommodityData::pluginId() const { return "COMMODITY"; } @@ -123,6 +124,22 @@ QStringList CommodityData::eagerLoad() { return { "type" }; } +bool CommodityData::favorite() { + return m_favorite; +} + +void CommodityData::setFavorite(bool favorite) { + m_favorite = favorite; +} + +QString CommodityData::color() { + return m_type->color(); +} + +QString CommodityData::category() { + return m_type->name(); +} + diff --git a/commodity/data/commoditydata.h b/commodity/data/commoditydata.h index e01d1aa..9d370cc 100644 --- a/commodity/data/commoditydata.h +++ b/commodity/data/commoditydata.h @@ -24,11 +24,12 @@ class CommodityData : public IShopItem Q_PROPERTY(QDecDouble price READ price WRITE setPrice) Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat) Q_PROPERTY(int count READ count WRITE setCount) + Q_PROPERTY(bool favorite READ favorite WRITE setFavorite) public: explicit CommodityData(QObject *parent = nullptr); - long id() override; + long id() const override; void setId(long id); QString name() override; @@ -52,6 +53,9 @@ public: int count() const; void setCount(int count); + bool favorite() override; + void setFavorite(bool favorite); + Q_INVOKABLE QStringList eagerLoad(); private: @@ -63,13 +67,15 @@ private: int m_price; Enums::VatType m_vat; int m_count; + bool m_favorite; // IShopItem interface public: QDecDouble unitPrice() override; Enums::VatType vatType() override; - QString pluginId() override; - + QString pluginId() const override; + QString color() override; + QString category() override; }; typedef QSharedPointer CommodityDataPtr; diff --git a/commodity/data/commoditytypedata.cpp b/commodity/data/commoditytypedata.cpp index 452ad18..112dbd5 100644 --- a/commodity/data/commoditytypedata.cpp +++ b/commodity/data/commoditytypedata.cpp @@ -7,6 +7,7 @@ namespace qx { t.setName("CommodityTypeData"); t.id(&CommodityTypeData::m_id, "id"); t.data(&CommodityTypeData::m_name, "name"); + t.data(&CommodityTypeData::m_color, "color"); } } @@ -47,4 +48,12 @@ QString CommodityTypeData::toString() return this->name(); } +QString CommodityTypeData::color() const { + return m_color; +} + +void CommodityTypeData::setColor(const QString& color) { + m_color = color; +} + diff --git a/commodity/data/commoditytypedata.h b/commodity/data/commoditytypedata.h index 3b6eb5f..d6e3c28 100644 --- a/commodity/data/commoditytypedata.h +++ b/commodity/data/commoditytypedata.h @@ -12,6 +12,7 @@ class CommodityTypeData :public ComboItem QX_REGISTER_FRIEND_CLASS(CommodityTypeData) Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString color READ color WRITE setColor) public: explicit CommodityTypeData(QObject *parent = nullptr); @@ -22,9 +23,13 @@ public: QString name() const; void setName(const QString &name); + QString color() const; + void setColor(const QString& color); + private: long m_id{0}; QString m_name; + QString m_color; // ComboItem interface public: diff --git a/core/service.h b/core/service.h index 95c6a4d..69c9d78 100644 --- a/core/service.h +++ b/core/service.h @@ -58,7 +58,7 @@ public: return ret; } - void save(QSharedPointer entity, qx::QxSession* pSession = nullptr) { + virtual void save(QSharedPointer entity, qx::QxSession* pSession = nullptr) { if (!checkPermission(PERM_ADD)) { return; } @@ -83,7 +83,7 @@ public: emit dataChanged(); } - void update(QSharedPointer entity, qx::QxSession* pSession = nullptr) { + virtual void update(QSharedPointer entity, qx::QxSession* pSession = nullptr) { if (!checkPermission(PERM_EDIT)) { return; } diff --git a/shop/data/favorititem.cpp b/shop/data/favorititem.cpp index 77b434e..63738dd 100644 --- a/shop/data/favorititem.cpp +++ b/shop/data/favorititem.cpp @@ -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; } diff --git a/shop/data/favorititem.h b/shop/data/favorititem.h index 4b49372..7ebc922 100644 --- a/shop/data/favorititem.h +++ b/shop/data/favorititem.h @@ -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; diff --git a/shop/directsaleitem.cpp b/shop/directsaleitem.cpp index dcd7b93..1d85dd0 100644 --- a/shop/directsaleitem.cpp +++ b/shop/directsaleitem.cpp @@ -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 ""; } diff --git a/shop/directsaleitem.h b/shop/directsaleitem.h index 3fb7a49..12fd286 100644 --- a/shop/directsaleitem.h +++ b/shop/directsaleitem.h @@ -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; diff --git a/shop/favbutton.h b/shop/favbutton.h index a44134e..88ab899 100644 --- a/shop/favbutton.h +++ b/shop/favbutton.h @@ -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; diff --git a/shop/isellableservice.h b/shop/isellableservice.h index 994b9e6..734200e 100644 --- a/shop/isellableservice.h +++ b/shop/isellableservice.h @@ -12,7 +12,7 @@ class SHOPSHARED_EXPORT ISellableService public: ISellableService(); - virtual QList shopItems() = 0; + virtual QList shopItems(const QString& category = "") = 0; virtual IShopItemPtr shopItem(int itemId) = 0; virtual void addedToVoucher(int itemId, int countAdded) = 0; virtual ISeller *seller() = 0; diff --git a/shop/ishopitem.h b/shop/ishopitem.h index 60054d2..9503ebf 100644 --- a/shop/ishopitem.h +++ b/shop/ishopitem.h @@ -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; QX_REGISTER_HPP_SHOP(IShopItem, QObject, 0) diff --git a/shop/settings/shopsettingsform.cpp b/shop/settings/shopsettingsform.cpp index 8dcf4a4..c270aa2 100644 --- a/shop/settings/shopsettingsform.cpp +++ b/shop/settings/shopsettingsform.cpp @@ -1,8 +1,6 @@ #include "shopsettingsform.h" #include "ui_shopsettingsform.h" -#include -#include #include #include #include @@ -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; diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 35cd74d..1e4e934 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -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 srvFav; QMap btnMap; - foreach (QWidget *child, ui->favorites->findChildren()) { - 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()) { + if (child->objectName() != "directSale") + { + delete child; + } + } +} + +void ShopForm::loadFavCatButtons(const QString& category) { + SettingsService srvSettings("SHOP"); + ShopSettingsPtr settings = srvSettings.loadSettings(); + + 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(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()) { + 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(""); +} diff --git a/shop/shopform.h b/shop/shopform.h index 61abdc3..5a494b0 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -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); diff --git a/shop/shopform.ui b/shop/shopform.ui index a87b397..e9037af 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -35,6 +35,73 @@ 0 + + + + + + + + 10 + + + + Direct Sell + + + + :/icons/shop.svg:/icons/shop.svg + + + + 45 + 45 + + + + Ctrl+D + + + Qt::ToolButtonTextUnderIcon + + + true + + + + + + + + + + + 0 + 45 + + + + Favorities + + + + + + + + + + + 0 + 0 + + + + + + + + @@ -110,49 +177,6 @@ - - - - - - - - - - - 10 - - - - Direct Sell - - - - :/icons/shop.svg:/icons/shop.svg - - - - 45 - 45 - - - - Ctrl+D - - - Qt::ToolButtonTextUnderIcon - - - true - - - - - - - - - diff --git a/shop/shopitem.h b/shop/shopitem.h index ebd183c..ed04f34 100644 --- a/shop/shopitem.h +++ b/shop/shopitem.h @@ -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 ShopItemPtr; diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index 39b90ed..c3a18b7 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -371,7 +371,7 @@ void ShopService::moveItems(QList 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 ShopService::vouchersForEet() .arg(QString::number(Voucher::PAID), QString::number(Voucher::EET_SENT), QString::number(Voucher::EET_NOT_ENTERING))); } -QList ShopService::allSellableItems() +QList ShopService::allSellableItems(const QString& category/* = ""*/) { QList > items; foreach (IPlugin *plugin, Context::instance().plugins()) { @@ -424,7 +424,7 @@ QList 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 ShopService::allCategories() { + QMap 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 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); + } } diff --git a/shop/shopservice.h b/shop/shopservice.h index 780dfcc..a7b2a91 100644 --- a/shop/shopservice.h +++ b/shop/shopservice.h @@ -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 items, VoucherPtr source, VoucherPtr target); void updateRelatedItem(VoucherItem* item, int countAdded); bool processEet(VoucherPtr voucher, QString &message); @@ -40,7 +41,8 @@ public: QList tempVouchers(); QList paiedVouchers(); QList vouchersForEet(); - QList allSellableItems(); + QList allSellableItems(const QString& category = ""); + QMap allCategories(); VoucherSum unpaidSummary(); VoucherSum unsendEET();