From dde8ee442f9551f416f8033feeb657eef6be6ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Sat, 5 Oct 2019 18:10:03 +0800 Subject: Basket class --- qicclient/basket.hxx | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 qicclient/basket.hxx (limited to 'qicclient/basket.hxx') diff --git a/qicclient/basket.hxx b/qicclient/basket.hxx new file mode 100644 index 0000000..eabec33 --- /dev/null +++ b/qicclient/basket.hxx @@ -0,0 +1,72 @@ +#ifndef QICCLIENT_BASKET_HXX +#define QICCLIENT_BASKET_HXX + +#include +#include +#include "product.hxx" + +struct icclient_ord_order; + +namespace ICClient { + + struct Item + { + enum ItemRoles { + QuantityRole = Product::PriceRole + 1 + }; + + Item(icclient_ord_item* item) + : product{item->product} + , quantity{item->quantity} + {} + + Product product; + unsigned int quantity; + }; + + class Basket : public QAbstractListModel + { + Q_OBJECT + Q_PROPERTY(double subtotal READ subtotal NOTIFY subtotalChanged) + Q_PROPERTY(double shipping READ shipping NOTIFY shippingChanged) + Q_PROPERTY(double totalCost READ totalCost NOTIFY totalCostChanged) + Q_PROPERTY(int rowCount READ rowCount NOTIFY rowCountChanged) + + public: + explicit Basket(QObject* parent = nullptr) + : QAbstractListModel{parent} {} + + int rowCount(QModelIndex const& parent + = QModelIndex()) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex& index + , int role = Qt::DisplayRole + ) const Q_DECL_OVERRIDE; + + double subtotal() const { return m_subtotal; } + double shipping() const { return m_shipping; } + double totalCost() const { return m_totalCost; } + + public slots: + void update(icclient_ord_order* order); + + protected: + QHash roleNames() const Q_DECL_OVERRIDE; + + signals: + void updated(); + void subtotalChanged(); + void shippingChanged(); + void totalCostChanged(); + void rowCountChanged(); + + private: + void addItem(Item const& item); + QList items; + double m_subtotal; + double m_shipping; + double m_totalCost; + }; + +} + +#endif // QICCLIENT_BASKET_HXX -- cgit v1.2.3