diff options
| author | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-05 18:10:03 +0800 | 
|---|---|---|
| committer | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-05 18:10:03 +0800 | 
| commit | dde8ee442f9551f416f8033feeb657eef6be6ebb (patch) | |
| tree | 6df626e667cf27466dfe7ec14ece555762c13881 | |
| parent | 61e8ac95e621544b32ae324d7aec44b4e15bc81d (diff) | |
Basket class
| -rw-r--r-- | basket.cxx | 60 | ||||
| -rw-r--r-- | qicclient.pro | 2 | ||||
| -rw-r--r-- | qicclient/basket.hxx | 72 | 
3 files changed, 134 insertions, 0 deletions
| diff --git a/basket.cxx b/basket.cxx new file mode 100644 index 0000000..c4905e0 --- /dev/null +++ b/basket.cxx @@ -0,0 +1,60 @@ +#include "qicclient/basket.hxx" + +namespace ICClient { + +	int Basket::rowCount(QModelIndex const& parent) const +	{ +		Q_UNUSED(parent) +			return items.count(); +	} + +	QVariant Basket::data(QModelIndex const& index, int role) const +	{ +		auto row = index.row(); + +		if (row < 0 || row >= items.count()) return QVariant(); + +		auto item = items[row]; +		switch (role) { +			case Product::SkuRole: +				return item.product.sku; +			case Product::DescriptionRole: +				return item.product.description; +			case Product::PriceRole: +				return item.product.price; +			case Item::QuantityRole: +				return item.quantity; +			default: +				return QVariant(); +		} +	} + +	QHash<int, QByteArray> Basket::roleNames() const +	{ +		return QHash<int, QByteArray>{ +			{Product::SkuRole, "sku"} +			, {Product::DescriptionRole, "description"} +			, {Product::PriceRole, "price"} +			, {Item::QuantityRole, "quantity"} +		}; +	} + +	void Basket::addItem(Item const& item) +	{ +		beginInsertRows(QModelIndex(), rowCount(), rowCount()); +		items << item; +		endInsertRows(); +		emit rowCountChanged(); +	} + +	void Basket::update(icclient_ord_order* order) +	{ +		if (order) { +			for (size_t i = 0; i < order->nitems; i++) +				addItem(Item{order->items[i]}); +			icclient_ord_free(order); +			emit updated(); +		} +	} + +} diff --git a/qicclient.pro b/qicclient.pro index 623a0b8..4d7150b 100644 --- a/qicclient.pro +++ b/qicclient.pro @@ -6,10 +6,12 @@ HEADERS += \  	qicclient/user.hxx \  	qicclient/product.hxx \  	qicclient/catalog.hxx \ +	qicclient/basket.hxx \  	qicclient/client.hxx  SOURCES += \  	user.cxx \  	catalog.cxx \ +	basket.cxx \  	client.cxx  INCLUDEPATH += $$PWD/libicclient  LIBS += $$PWD/libicclient/libicclient.a 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 <QAbstractListModel> +#include <icclient/ord.h> +#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<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; + +		signals: +			void updated(); +			void subtotalChanged(); +			void shippingChanged(); +			void totalCostChanged(); +			void rowCountChanged(); + +		private: +			void addItem(Item const& item); +			QList<Item> items; +			double m_subtotal; +			double m_shipping; +			double m_totalCost; +	}; + +} + +#endif // QICCLIENT_BASKET_HXX |