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

master
Josef Rokos 2 years ago
parent 5b5f31f64d
commit 2597f79d2d

@ -125,7 +125,7 @@ void CampGrid::addToVoucher(const CampDataPtr& data)
shopSrv.addShopItem(voucher, campItem, 1); shopSrv.addShopItem(voucher, campItem, 1);
shopSrv.calculate(voucher); shopSrv.calculate(voucher);
shopSrv.saveVoucher(voucher); shopSrv.save(voucher);
data->setOnVoucher(true); data->setOnVoucher(true);

@ -357,8 +357,12 @@ void CampService::addAccFee(const CampDataPtr& data, const AddressItemPtr& item,
} }
} }
QList<IShopItemPtr> CampService::shopItems() QList<IShopItemPtr> CampService::shopItems(const QString& category)
{ {
if (!category.isEmpty()) {
return {};
}
CampShopItemPtr item(new CampShopItem); CampShopItemPtr item(new CampShopItem);
QList<IShopItemPtr> items; QList<IShopItemPtr> items;

@ -36,7 +36,7 @@ private:
// ISellableService interface // ISellableService interface
public: public:
QList<IShopItemPtr> shopItems() override; QList<IShopItemPtr> shopItems(const QString& category = "") override;
IShopItemPtr shopItem(int itemId) override; IShopItemPtr shopItem(int itemId) override;
void addedToVoucher(int itemId, int countAdded) override; void addedToVoucher(int itemId, int countAdded) override;
ISeller *seller() override; ISeller *seller() override;

@ -28,7 +28,7 @@ Enums::VatType CampShopItem::vatType()
return m_vatType; return m_vatType;
} }
QString CampShopItem::pluginId() QString CampShopItem::pluginId() const
{ {
return "CAMP"; return "CAMP";
} }
@ -48,7 +48,7 @@ void CampShopItem::setVatType(const Enums::VatType &vatType)
m_vatType = vatType; m_vatType = vatType;
} }
long CampShopItem::id() long CampShopItem::id() const
{ {
return m_id; return m_id;
} }

@ -10,12 +10,12 @@ public:
// IShopItem interface // IShopItem interface
public: public:
long id() override; long id() const override;
QString name() override; QString name() override;
QString shortName() override; QString shortName() override;
QDecDouble unitPrice() override; QDecDouble unitPrice() override;
Enums::VatType vatType() override; Enums::VatType vatType() override;
QString pluginId() override; QString pluginId() const override;
// ShopItem interface // ShopItem interface
public: public:

@ -40,7 +40,7 @@ add_library(commodity SHARED
data/commoditytypedata.h data/commoditytypedata.h
settings/commoditysettings.cpp settings/commoditysettings.cpp
settings/commoditysettings.h settings/commoditysettings.h
) coloritemdelegate.cpp coloritemdelegate.h)
target_compile_definitions(commodity PRIVATE -DCOMMODITY_LIBRARY) target_compile_definitions(commodity PRIVATE -DCOMMODITY_LIBRARY)

@ -0,0 +1,175 @@
//
// Created by Josef Rokos on 03.05.2023.
//
#include "coloritemdelegate.h"
#include <QPainter>
#include <QApplication>
#include <QPaintEvent>
#include <QHBoxLayout>
#include <QColorDialog>
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<QFocusEvent*>(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<ColorItemEditor*>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void ColorItemDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
auto edit = qobject_cast<ColorItemEditor*>(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<ColorItemEditor*>(editor);
if (edit) {
model->setData(index, edit->color().name());
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}

@ -0,0 +1,64 @@
//
// Created by Josef Rokos on 03.05.2023.
//
#ifndef COLORITEMDELEGATE_H
#define COLORITEMDELEGATE_H
#include <QStyledItemDelegate>
#include <QToolButton>
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

@ -8,7 +8,7 @@
"default" : "", "default" : "",
"CZ" : "" "CZ" : ""
}, },
"schemaVersion" : 1, "schemaVersion" : 2,
"sql" : [ "sql" : [
"CREATE TABLE \"CommodityTypeData\" ( "CREATE TABLE \"CommodityTypeData\" (
\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@ -26,8 +26,13 @@ CREATE TABLE \"CommodityData\" (
CONSTRAINT \"type_fk\" CONSTRAINT \"type_fk\"
FOREIGN KEY (\"type\") FOREIGN KEY (\"type\")
REFERENCES \"CommodityTypeData\" (\"id\") 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" ], "dependencies" : [ "SHOP" ],
"translations" : { "translations" : {

@ -25,6 +25,7 @@ CommodityForm::CommodityForm(QWidget *parent) :
<< ComboData(Enums::SECOND_LOWER,tr("Second Lower")); << ComboData(Enums::SECOND_LOWER,tr("Second Lower"));
registerBinding(ui->vat, vt); registerBinding(ui->vat, vt);
registerBinding(ui->count); registerBinding(ui->count);
registerBinding(ui->favorite);
m_codeAsNumber = false; m_codeAsNumber = false;
} }

@ -94,6 +94,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="1">
<widget class="QCheckBox" name="favorite">
<property name="text">
<string>Favorite</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<tabstops> <tabstops>

@ -1,11 +1,13 @@
#include "commodityservice.h" #include "commodityservice.h"
QList<QSharedPointer<IShopItem> > CommodityService::shopItems() QList<QSharedPointer<IShopItem> > CommodityService::shopItems(const QString& category/* = ""*/)
{ {
QList<QSharedPointer<IShopItem> > ret; QList<QSharedPointer<IShopItem> > ret;
foreach (QSharedPointer<CommodityData> data, all()) { foreach (QSharedPointer<CommodityData> data, all()) {
ret.append(qSharedPointerDynamicCast<IShopItem, CommodityData>(data)); if (category.isEmpty() || (data->favorite() && data->category() == category)) {
ret.append(qSharedPointerDynamicCast<IShopItem, CommodityData>(data));
}
} }
return ret; return ret;

@ -12,7 +12,7 @@ public:
// ISellableService interface // ISellableService interface
public: public:
QList<IShopItemPtr> shopItems() override; QList<IShopItemPtr> shopItems(const QString& category = "") override;
void addedToVoucher(int itemId, int countAdded) override; void addedToVoucher(int itemId, int countAdded) override;
IShopItemPtr shopItem(int itemId) override; IShopItemPtr shopItem(int itemId) override;
ISeller *seller() override; ISeller *seller() override;

@ -4,6 +4,8 @@
#include <service.h> #include <service.h>
#include <settingsservice.h> #include <settingsservice.h>
#include "coloritemdelegate.h"
CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) : CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) :
FormBinder<CommoditySettings>(parent), FormBinder<CommoditySettings>(parent),
ui(new Ui::CommoditySettingsForm) ui(new Ui::CommoditySettingsForm)
@ -13,9 +15,10 @@ CommoditySettingsForm::CommoditySettingsForm(QWidget *parent) :
registerBinding(ui->codeAsNumber); registerBinding(ui->codeAsNumber);
m_table = new AutoTableModel<CommodityTypeData>(); m_table = new AutoTableModel<CommodityTypeData>();
m_table->setEditableCols(QList<int>() << 0); m_table->setEditableCols(QList<int>() << 0 << 1);
ui->commodityTypesTable->setModel(m_table); ui->commodityTypesTable->setModel(m_table);
ui->commodityTypesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); ui->commodityTypesTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->commodityTypesTable->setItemDelegateForColumn(1, new ColorItemDelegate(this));
} }
CommoditySettingsForm::~CommoditySettingsForm() CommoditySettingsForm::~CommoditySettingsForm()

@ -13,6 +13,7 @@ namespace qx {
t.data(&CommodityData::m_price, "price"); t.data(&CommodityData::m_price, "price");
t.data(&CommodityData::m_vat, "vat"); t.data(&CommodityData::m_vat, "vat");
t.data(&CommodityData::m_count, "count"); t.data(&CommodityData::m_count, "count");
t.data(&CommodityData::m_favorite, "favorite");
t.relationManyToOne(&CommodityData::m_type, "type"); t.relationManyToOne(&CommodityData::m_type, "type");
} }
@ -25,7 +26,7 @@ CommodityData::CommodityData(QObject *parent)
m_price = 0; m_price = 0;
m_vat = Enums::NONE; m_vat = Enums::NONE;
} }
long CommodityData::id() long CommodityData::id() const
{ {
return m_id; return m_id;
} }
@ -114,7 +115,7 @@ Enums::VatType CommodityData::vatType()
return vat(); return vat();
} }
QString CommodityData::pluginId() QString CommodityData::pluginId() const
{ {
return "COMMODITY"; return "COMMODITY";
} }
@ -123,6 +124,22 @@ QStringList CommodityData::eagerLoad() {
return { "type" }; 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();
}

@ -24,11 +24,12 @@ class CommodityData : public IShopItem
Q_PROPERTY(QDecDouble price READ price WRITE setPrice) Q_PROPERTY(QDecDouble price READ price WRITE setPrice)
Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat) Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat)
Q_PROPERTY(int count READ count WRITE setCount) Q_PROPERTY(int count READ count WRITE setCount)
Q_PROPERTY(bool favorite READ favorite WRITE setFavorite)
public: public:
explicit CommodityData(QObject *parent = nullptr); explicit CommodityData(QObject *parent = nullptr);
long id() override; long id() const override;
void setId(long id); void setId(long id);
QString name() override; QString name() override;
@ -52,6 +53,9 @@ public:
int count() const; int count() const;
void setCount(int count); void setCount(int count);
bool favorite() override;
void setFavorite(bool favorite);
Q_INVOKABLE QStringList eagerLoad(); Q_INVOKABLE QStringList eagerLoad();
private: private:
@ -63,13 +67,15 @@ private:
int m_price; int m_price;
Enums::VatType m_vat; Enums::VatType m_vat;
int m_count; int m_count;
bool m_favorite;
// IShopItem interface // IShopItem interface
public: public:
QDecDouble unitPrice() override; QDecDouble unitPrice() override;
Enums::VatType vatType() override; Enums::VatType vatType() override;
QString pluginId() override; QString pluginId() const override;
QString color() override;
QString category() override;
}; };
typedef QSharedPointer<CommodityData> CommodityDataPtr; typedef QSharedPointer<CommodityData> CommodityDataPtr;

@ -7,6 +7,7 @@ namespace qx {
t.setName("CommodityTypeData"); t.setName("CommodityTypeData");
t.id(&CommodityTypeData::m_id, "id"); t.id(&CommodityTypeData::m_id, "id");
t.data(&CommodityTypeData::m_name, "name"); t.data(&CommodityTypeData::m_name, "name");
t.data(&CommodityTypeData::m_color, "color");
} }
} }
@ -47,4 +48,12 @@ QString CommodityTypeData::toString()
return this->name(); return this->name();
} }
QString CommodityTypeData::color() const {
return m_color;
}
void CommodityTypeData::setColor(const QString& color) {
m_color = color;
}

@ -12,6 +12,7 @@ class CommodityTypeData :public ComboItem
QX_REGISTER_FRIEND_CLASS(CommodityTypeData) QX_REGISTER_FRIEND_CLASS(CommodityTypeData)
Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString color READ color WRITE setColor)
public: public:
explicit CommodityTypeData(QObject *parent = nullptr); explicit CommodityTypeData(QObject *parent = nullptr);
@ -22,9 +23,13 @@ public:
QString name() const; QString name() const;
void setName(const QString &name); void setName(const QString &name);
QString color() const;
void setColor(const QString& color);
private: private:
long m_id{0}; long m_id{0};
QString m_name; QString m_name;
QString m_color;
// ComboItem interface // ComboItem interface
public: public:

@ -58,7 +58,7 @@ public:
return ret; return ret;
} }
void save(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) { virtual void save(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
if (!checkPermission(PERM_ADD)) { if (!checkPermission(PERM_ADD)) {
return; return;
} }
@ -83,7 +83,7 @@ public:
emit dataChanged(); emit dataChanged();
} }
void update(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) { virtual void update(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
if (!checkPermission(PERM_EDIT)) { if (!checkPermission(PERM_EDIT)) {
return; return;
} }

@ -5,7 +5,7 @@ QX_REGISTER_CPP_SHOP(FavoritItem)
QX_REGISTER_ALL_QT_PROPERTIES(FavoritItem, "id") QX_REGISTER_ALL_QT_PROPERTIES(FavoritItem, "id")
long FavoritItem::id() long FavoritItem::id() const
{ {
return m_id; return m_id;
} }
@ -55,7 +55,7 @@ void FavoritItem::setVatType(const Enums::VatType &vatType)
m_vatType = vatType; m_vatType = vatType;
} }
QString FavoritItem::pluginId() QString FavoritItem::pluginId() const
{ {
return m_pluginId; return m_pluginId;
} }

@ -30,13 +30,13 @@ public:
// IShopItem interface // IShopItem interface
public: public:
long id() override; long id() const override;
void setId(long id); void setId(long id);
QString name() override; QString name() override;
void setName(const QString &name); void setName(const QString &name);
virtual QString shortName() override; QString shortName() override;
void setShortName(const QString &shortName); void setShortName(const QString &shortName);
QDecDouble unitPrice() override; QDecDouble unitPrice() override;
@ -45,7 +45,7 @@ public:
Enums::VatType vatType() override; Enums::VatType vatType() override;
void setVatType(const Enums::VatType &vatType); void setVatType(const Enums::VatType &vatType);
QString pluginId() override; QString pluginId() const override;
void setPluginId(const QString &pluginId); void setPluginId(const QString &pluginId);
QString favButtonName() const; QString favButtonName() const;

@ -6,7 +6,7 @@ DirectSaleItem::DirectSaleItem(QObject *parent) : IShopItem(parent)
m_vat = Enums::NONE; m_vat = Enums::NONE;
} }
long DirectSaleItem::id() long DirectSaleItem::id() const
{ {
return 0; return 0;
} }
@ -26,7 +26,7 @@ QDecDouble DirectSaleItem::unitPrice()
return m_unitPrice; return m_unitPrice;
} }
QString DirectSaleItem::pluginId() QString DirectSaleItem::pluginId() const
{ {
return ""; return "";
} }

@ -22,11 +22,11 @@ public slots:
// IShopItem interface // IShopItem interface
public: public:
long id() override; long id() const override;
QString name() override; QString name() override;
QString shortName() override; QString shortName() override;
QDecDouble unitPrice() override; QDecDouble unitPrice() override;
QString pluginId() override; QString pluginId() const override;
Enums::VatType vatType() override; Enums::VatType vatType() override;
int count() const; int count() const;

@ -8,11 +8,11 @@ class FavButtonStyle : public QProxyStyle
{ {
public: public:
FavButtonStyle(); FavButtonStyle();
virtual ~FavButtonStyle(); ~FavButtonStyle() override = default;
// QStyle interface // QStyle interface
public: 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: private:
Q_DISABLE_COPY(FavButtonStyle) Q_DISABLE_COPY(FavButtonStyle)
@ -23,15 +23,15 @@ class FavButton : public QToolButton
Q_OBJECT Q_OBJECT
public: public:
FavButton(QWidget *parent = 0); explicit FavButton(QWidget *parent = nullptr);
signals: signals:
void itemDropped(); void itemDropped();
// QWidget interface // QWidget interface
protected: protected:
virtual void dragEnterEvent(QDragEnterEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override;
virtual void dropEvent(QDropEvent *event) override; void dropEvent(QDropEvent *event) override;
private: private:
FavButtonStyle m_style; FavButtonStyle m_style;

@ -12,7 +12,7 @@ class SHOPSHARED_EXPORT ISellableService
public: public:
ISellableService(); ISellableService();
virtual QList<IShopItemPtr> shopItems() = 0; virtual QList<IShopItemPtr> shopItems(const QString& category = "") = 0;
virtual IShopItemPtr shopItem(int itemId) = 0; virtual IShopItemPtr shopItem(int itemId) = 0;
virtual void addedToVoucher(int itemId, int countAdded) = 0; virtual void addedToVoucher(int itemId, int countAdded) = 0;
virtual ISeller *seller() = 0; virtual ISeller *seller() = 0;

@ -17,20 +17,28 @@ class SHOPSHARED_EXPORT IShopItem : public QObject
Q_PROPERTY(QString shortName READ shortName) Q_PROPERTY(QString shortName READ shortName)
Q_PROPERTY(QDecDouble unitPrice READ unitPrice) Q_PROPERTY(QDecDouble unitPrice READ unitPrice)
Q_PROPERTY(Enums::VatType vatType READ vatType) Q_PROPERTY(Enums::VatType vatType READ vatType)
Q_PROPERTY(bool favorite READ favorite)
public: public:
explicit IShopItem(QObject* parent = nullptr); explicit IShopItem(QObject* parent = nullptr);
~IShopItem() override = default; ~IShopItem() override = default;
virtual long id() { return {}; } virtual long id() const { return {}; }
virtual QString name() { return {}; } virtual QString name() { return {}; }
virtual QString code() { return {}; } virtual QString code() { return {}; }
virtual QString shortName() { return {}; } virtual QString shortName() { return {}; }
virtual QDecDouble unitPrice() { return {}; } virtual QDecDouble unitPrice() { return {}; }
virtual Enums::VatType vatType() { 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>; using IShopItemPtr = QSharedPointer<IShopItem>;
QX_REGISTER_HPP_SHOP(IShopItem, QObject, 0) QX_REGISTER_HPP_SHOP(IShopItem, QObject, 0)

@ -1,8 +1,6 @@
#include "shopsettingsform.h" #include "shopsettingsform.h"
#include "ui_shopsettingsform.h" #include "ui_shopsettingsform.h"
#include <settingsservice.h>
#include <combodata.h>
#include <QFileDialog> #include <QFileDialog>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QDebug> #include <QDebug>
@ -146,7 +144,7 @@ bool ShopSettingsForm::saveRecord()
} }
foreach (QString btnName, m_btnMap.keys()) { foreach (QString btnName, m_btnMap.keys()) {
if (m_btnMap[btnName] != NULL) if (m_btnMap[btnName] != nullptr)
{ {
srvFav.save(m_btnMap[btnName]); 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 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; flags |= Qt::TextWordWrap;

@ -145,6 +145,7 @@ void ShopForm::loadLast()
m_commodityModel->setData(srv.allSellableItems()); m_commodityModel->setData(srv.allSellableItems());
ui->commodityTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); ui->commodityTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->commodityTable->setColumnHidden(4, true); ui->commodityTable->setColumnHidden(4, true);
ui->commodityTable->setColumnHidden(5, true);
ui->commodityTable->setColumnHidden(2, true); ui->commodityTable->setColumnHidden(2, true);
ui->commodityTable->setColumnWidth(3, 90); ui->commodityTable->setColumnWidth(3, 90);
@ -167,22 +168,20 @@ void ShopForm::loadButtons(const ShopSettingsPtr& settings)
Service<FavoritItem> srvFav; Service<FavoritItem> srvFav;
QMap<QString, FavoritItemPtr> btnMap; QMap<QString, FavoritItemPtr> btnMap;
foreach (QWidget *child, ui->favorites->findChildren<QWidget*>()) { clearFavButtons();
if (child->objectName() != "directSale")
{
delete child;
}
}
foreach (FavoritItemPtr item, srvFav.all()) { foreach (FavoritItemPtr item, srvFav.all()) {
btnMap[item->favButtonName()] = item; btnMap[item->favButtonName()] = item;
} }
ShopService srv;
auto allItems = srv.allSellableItems();
for (int i = 0; i < settings->favBtnRows(); i++) for (int i = 0; i < settings->favBtnRows(); i++)
{ {
for (int j = 0; j < settings->favBtnCols(); j++) 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); QString btnName = QString::number(i) + "_" + QString::number(j);
btn->setObjectName(btnName); btn->setObjectName(btnName);
@ -197,6 +196,16 @@ void ShopForm::loadButtons(const ShopSettingsPtr& settings)
if (btnMap[btnName] != nullptr) if (btnMap[btnName] != nullptr)
{ {
btn->setText(btnMap[btnName]->shortName()); 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){ connect(btn, &FavButton::clicked, [this, btnMap, btn](bool){
FavoritItemPtr item = btnMap[btn->objectName()]; FavoritItemPtr item = btnMap[btn->objectName()];
@ -249,6 +258,7 @@ void ShopForm::setupForm()
loadLast(); loadLast();
fillReceiptCombo(); fillReceiptCombo();
loadButtons(settings); loadButtons(settings);
loadCatButtons();
} }
void ShopForm::on_directSale_clicked() void ShopForm::on_directSale_clicked()
@ -514,7 +524,7 @@ void ShopForm::recalculate()
if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0) if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0)
{ {
srv.saveVoucher(m_voucher); srv.save(m_voucher);
} }
else else
{ {
@ -664,3 +674,107 @@ void ShopForm::on_actualReceipt_customContextMenuRequested(const QPoint &pos)
QPoint globalPos = ui->actualReceipt->mapToGlobal(pos); QPoint globalPos = ui->actualReceipt->mapToGlobal(pos);
m_itemCtxMenu->exec(globalPos); 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("");
}

@ -42,6 +42,8 @@ private slots:
void on_payButton_clicked(); void on_payButton_clicked();
void on_btnFav_clicked();
void on_showPaidButton_clicked(); void on_showPaidButton_clicked();
void on_btnAddItem_clicked(); void on_btnAddItem_clicked();
@ -67,6 +69,9 @@ private:
void loadLast(); void loadLast();
void loadButtons(const ShopSettingsPtr& settings); void loadButtons(const ShopSettingsPtr& settings);
void loadFavCatButtons(const QString& category);
void loadCatButtons();
void clearFavButtons();
void fillReceiptCombo(); void fillReceiptCombo();
void createVoucher(); void createVoucher();
void doTempSave(bool comboChanged); void doTempSave(bool comboChanged);

@ -35,6 +35,73 @@
<property name="bottomMargin"> <property name="bottomMargin">
<number>0</number> <number>0</number>
</property> </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> <item>
<widget class="QWidget" name="widgetComodity" native="true"> <widget class="QWidget" name="widgetComodity" native="true">
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
@ -110,49 +177,6 @@
</layout> </layout>
</widget> </widget>
</item> </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> <item>
<widget class="QWidget" name="widget_2" native="true"> <widget class="QWidget" name="widget_2" native="true">
<layout class="QVBoxLayout" name="verticalLayout_6"> <layout class="QVBoxLayout" name="verticalLayout_6">

@ -25,13 +25,13 @@ public slots:
// IShopItem interface // IShopItem interface
public: public:
long id() override { return 0; } long id() const override { return 0; }
QString code() override { return ""; } QString code() override { return ""; }
QString name() override { return ""; } QString name() override { return ""; }
QString shortName() override { return ""; } QString shortName() override { return ""; }
QDecDouble unitPrice() override { return {}; } QDecDouble unitPrice() override { return {}; }
Enums::VatType vatType() override { return Enums::NONE; } Enums::VatType vatType() override { return Enums::NONE; }
QString pluginId() override { return ""; } QString pluginId() const override { return ""; }
}; };
typedef QSharedPointer<ShopItem> ShopItemPtr; typedef QSharedPointer<ShopItem> ShopItemPtr;

@ -371,7 +371,7 @@ void ShopService::moveItems(QList<VoucherItemPtr> items, VoucherPtr source, Vouc
if (target->status() == Voucher::NEW && target->id() == 0) if (target->status() == Voucher::NEW && target->id() == 0)
{ {
this->saveVoucher(target, &session); this->save(target, &session);
} }
for (const auto& it : items) { 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))); .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; QList<QSharedPointer<IShopItem> > items;
foreach (IPlugin *plugin, Context::instance().plugins()) { foreach (IPlugin *plugin, Context::instance().plugins()) {
@ -424,7 +424,7 @@ QList<IShopItemPtr> ShopService::allSellableItems()
if (selSrv != nullptr) if (selSrv != nullptr)
{ {
items.append(selSrv->shopItems()); items.append(selSrv->shopItems(category));
} }
} }
@ -491,13 +491,58 @@ QDecDouble ShopService::vatRate(Enums::VatType vatType)
return vatRate; return vatRate;
} }
void ShopService::saveVoucher(VoucherPtr entity, qx::QxSession* pSession/* = nullptr*/) void ShopService::save(VoucherPtr entity, qx::QxSession* pSession/* = nullptr*/)
{ {
SeasonService seasonSrv; SeasonService seasonSrv;
SeasonPtr season = seasonSrv.active(); SeasonPtr season = seasonSrv.active();
entity->setSeason(season); entity->setSeason(season);
addDateAndUser(entity, true); 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);
}
} }

@ -27,7 +27,8 @@ public:
void calculate(VoucherPtr voucher); void calculate(VoucherPtr voucher);
void calculateItem(VoucherItemPtr item); void calculateItem(VoucherItemPtr item);
void pay(VoucherPtr voucher); 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 moveItems(QList<VoucherItemPtr> items, VoucherPtr source, VoucherPtr target);
void updateRelatedItem(VoucherItem* item, int countAdded); void updateRelatedItem(VoucherItem* item, int countAdded);
bool processEet(VoucherPtr voucher, QString &message); bool processEet(VoucherPtr voucher, QString &message);
@ -40,7 +41,8 @@ public:
QList<VoucherPtr> tempVouchers(); QList<VoucherPtr> tempVouchers();
QList<VoucherPtr> paiedVouchers(); QList<VoucherPtr> paiedVouchers();
QList<VoucherPtr> vouchersForEet(); QList<VoucherPtr> vouchersForEet();
QList<IShopItemPtr> allSellableItems(); QList<IShopItemPtr> allSellableItems(const QString& category = "");
QMap<QString, QString> allCategories();
VoucherSum unpaidSummary(); VoucherSum unpaidSummary();
VoucherSum unsendEET(); VoucherSum unsendEET();

Loading…
Cancel
Save