diff options
Diffstat (limited to 'qicclient')
-rw-r--r-- | qicclient/basket.hxx | 76 | ||||
-rw-r--r-- | qicclient/catalog.hxx | 43 | ||||
-rw-r--r-- | qicclient/client.hxx | 5 | ||||
-rw-r--r-- | qicclient/product.hxx | 40 |
4 files changed, 120 insertions, 44 deletions
diff --git a/qicclient/basket.hxx b/qicclient/basket.hxx new file mode 100644 index 0000000..b3b5e11 --- /dev/null +++ b/qicclient/basket.hxx @@ -0,0 +1,76 @@ +#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} + , m_subtotal{.0} + , m_shipping{.0} + , m_totalCost{.0} + {} + + 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 diff --git a/qicclient/catalog.hxx b/qicclient/catalog.hxx index 782c864..38e257a 100644 --- a/qicclient/catalog.hxx +++ b/qicclient/catalog.hxx @@ -2,56 +2,17 @@ #define QICCLIENT_CATALOG_HXX #include <QAbstractListModel> -#include <icclient/product.h> +#include "product.hxx" struct icclient_catalog; namespace ICClient { - class Product - { - public: - Product(icclient_product* product) : m_sku{product->sku} - { - if (product->description) - m_description - = QString{product->description}; - if (product->comment) - m_comment = QString{product->comment}; - if (product->image) - m_image = QString{product->image}; - m_price = product->price; - } - QString const& sku() const { return m_sku; } - QString const& description() const - { - return m_description; - } - QString const& comment() const { return m_comment; } - QString const& image() const { return m_image; } - double price() const { return m_price; } - - private: - QString m_sku; - QString m_description; - QString m_comment; - QString m_image; - double m_price; - }; - class Catalog : public QAbstractListModel { Q_OBJECT public: - enum ProductRoles { - SkuRole = Qt::UserRole + 1, - DescriptionRole, - CommentRole, - ImageRole, - PriceRole - }; - explicit Catalog(QObject* parent = nullptr) : QAbstractListModel{parent} {} @@ -71,8 +32,8 @@ namespace ICClient { QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; private: - QList<Product> products; void addProduct(Product const& product); + QList<Product> products; }; } diff --git a/qicclient/client.hxx b/qicclient/client.hxx index bccd665..6121c5e 100644 --- a/qicclient/client.hxx +++ b/qicclient/client.hxx @@ -2,10 +2,9 @@ #define QICCLIENT_CLIENT_HXX #include <QObject> -#include <icclient/product.h> struct icclient_catalog; -struct icclient_order; +struct icclient_ord_order; namespace ICClient { @@ -53,7 +52,7 @@ namespace ICClient { signals: void gotAllProducts(icclient_catalog* catalog); - void ordered(icclient_order* order); + void ordered(icclient_ord_order* order); void loggedIn(QString const& userName); void loggedOut(); }; diff --git a/qicclient/product.hxx b/qicclient/product.hxx new file mode 100644 index 0000000..7271fe9 --- /dev/null +++ b/qicclient/product.hxx @@ -0,0 +1,40 @@ +#ifndef QICCLIENT_PRODUCT_HXX +#define QICCLIENT_PRODUCT_HXX + +#include <icclient/product.h> + +namespace ICClient { + + struct Product + { + enum ProductRoles { + SkuRole = Qt::UserRole + 1, + DescriptionRole, + CommentRole, + ImageRole, + PriceRole + }; + + Product(icclient_product* product) + : sku{product->sku} + , price{product->price} + { + if (product->description) + description + = QString{product->description}; + if (product->comment) + comment = QString{product->comment}; + if (product->image) + image = QString{product->image}; + } + + QString sku; + QString description; + QString comment; + QString image; + double price; + }; + +} + +#endif // QICCLIENT_PRODUCT_HXX |