From 5f25bd6a8166258aa361b66d17cc7161d624a1ea 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: Fri, 4 Oct 2019 22:25:52 +0800 Subject: Catalog update frees the C version catalog --- catalog.cxx | 4 +++- libicclient | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/catalog.cxx b/catalog.cxx index 8664cdc..709d20d 100644 --- a/catalog.cxx +++ b/catalog.cxx @@ -51,9 +51,11 @@ namespace ICClient { void Catalog::update(icclient_catalog* catalog) { - if (catalog) + if (catalog) { for (size_t i = 0; i < catalog->length; i++) addProduct(Product{catalog->products[i]}); + icclient_catalog_free(catalog); + } emit updated(); } diff --git a/libicclient b/libicclient index 685d975..7c2232c 160000 --- a/libicclient +++ b/libicclient @@ -1 +1 @@ -Subproject commit 685d975434d9311e5afebe9f933509f8bb2ad619 +Subproject commit 7c2232c3af7edf93a6caa71ffc969916b4f88c4a -- cgit v1.2.3 From 5d36cbe8062af072958c37bb9c71b1e158074d3a 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 14:41:50 +0800 Subject: updated signal is emitted only when the catalog *is* updated --- catalog.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/catalog.cxx b/catalog.cxx index 709d20d..03a90a1 100644 --- a/catalog.cxx +++ b/catalog.cxx @@ -55,8 +55,8 @@ namespace ICClient { for (size_t i = 0; i < catalog->length; i++) addProduct(Product{catalog->products[i]}); icclient_catalog_free(catalog); + emit updated(); } - emit updated(); } } -- cgit v1.2.3 From 61e8ac95e621544b32ae324d7aec44b4e15bc81d 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:09:48 +0800 Subject: Separate the product structure so the catalog doesn't have to included when not needed. --- catalog.cxx | 35 +++++++++++++++++++---------------- libicclient | 2 +- qicclient.pro | 1 + qicclient/catalog.hxx | 43 ++----------------------------------------- qicclient/client.hxx | 1 - qicclient/product.hxx | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 59 deletions(-) create mode 100644 qicclient/product.hxx diff --git a/catalog.cxx b/catalog.cxx index 03a90a1..ecc01a0 100644 --- a/catalog.cxx +++ b/catalog.cxx @@ -1,3 +1,6 @@ +#include +#include +#include #include "qicclient/catalog.hxx" namespace ICClient { @@ -16,16 +19,16 @@ namespace ICClient { auto product = products[row]; switch (role) { - case SkuRole: - return product.sku(); - case DescriptionRole: - return product.description(); - case CommentRole: - return product.comment(); - case ImageRole: - return product.image(); - case PriceRole: - return product.price(); + case Product::SkuRole: + return product.sku; + case Product::DescriptionRole: + return product.description; + case Product::CommentRole: + return product.comment; + case Product::ImageRole: + return product.image; + case Product::PriceRole: + return product.price; default: return QVariant(); } @@ -34,11 +37,11 @@ namespace ICClient { QHash Catalog::roleNames() const { return QHash{ - {SkuRole, "sku"} - , {DescriptionRole, "description"} - , {CommentRole, "comment"} - , {ImageRole, "image"} - , {PriceRole, "price"} + {Product::SkuRole, "sku"} + , {Product::DescriptionRole, "description"} + , {Product::CommentRole, "comment"} + , {Product::ImageRole, "image"} + , {Product::PriceRole, "price"} }; } @@ -54,7 +57,7 @@ namespace ICClient { if (catalog) { for (size_t i = 0; i < catalog->length; i++) addProduct(Product{catalog->products[i]}); - icclient_catalog_free(catalog); + icclient_freecatalog(catalog); emit updated(); } } diff --git a/libicclient b/libicclient index 7c2232c..c2c89d3 160000 --- a/libicclient +++ b/libicclient @@ -1 +1 @@ -Subproject commit 7c2232c3af7edf93a6caa71ffc969916b4f88c4a +Subproject commit c2c89d376b0d80299ad0fe8228ecef4279557160 diff --git a/qicclient.pro b/qicclient.pro index 371bd20..623a0b8 100644 --- a/qicclient.pro +++ b/qicclient.pro @@ -4,6 +4,7 @@ CONFIG += staticlib HEADERS += \ qicclient/user.hxx \ + qicclient/product.hxx \ qicclient/catalog.hxx \ qicclient/client.hxx SOURCES += \ diff --git a/qicclient/catalog.hxx b/qicclient/catalog.hxx index 782c864..d539149 100644 --- a/qicclient/catalog.hxx +++ b/qicclient/catalog.hxx @@ -2,56 +2,17 @@ #define QICCLIENT_CATALOG_HXX #include -#include +#include 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 roleNames() const Q_DECL_OVERRIDE; private: - QList products; void addProduct(Product const& product); + QList products; }; } diff --git a/qicclient/client.hxx b/qicclient/client.hxx index bccd665..9af124d 100644 --- a/qicclient/client.hxx +++ b/qicclient/client.hxx @@ -2,7 +2,6 @@ #define QICCLIENT_CLIENT_HXX #include -#include struct icclient_catalog; struct icclient_order; 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 + +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 -- cgit v1.2.3 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 --- basket.cxx | 60 +++++++++++++++++++++++++++++++++++++++++++ qicclient.pro | 2 ++ qicclient/basket.hxx | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 basket.cxx create mode 100644 qicclient/basket.hxx 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 Basket::roleNames() const + { + return QHash{ + {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 +#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 From c2d38b094ceba47591b0524646b1e1c88e1005c0 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: Sun, 6 Oct 2019 18:23:59 +0800 Subject: Initialise basket --- libicclient | 2 +- qicclient/basket.hxx | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libicclient b/libicclient index c2c89d3..fc55559 160000 --- a/libicclient +++ b/libicclient @@ -1 +1 @@ -Subproject commit c2c89d376b0d80299ad0fe8228ecef4279557160 +Subproject commit fc555594c636e87ffe2384e6b5240cfa934c6cf4 diff --git a/qicclient/basket.hxx b/qicclient/basket.hxx index eabec33..b3b5e11 100644 --- a/qicclient/basket.hxx +++ b/qicclient/basket.hxx @@ -34,7 +34,11 @@ namespace ICClient { public: explicit Basket(QObject* parent = nullptr) - : QAbstractListModel{parent} {} + : QAbstractListModel{parent} + , m_subtotal{.0} + , m_shipping{.0} + , m_totalCost{.0} + {} int rowCount(QModelIndex const& parent = QModelIndex()) const Q_DECL_OVERRIDE; -- cgit v1.2.3 From e76505f6f147736f12282912655160dd531995a6 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: Sun, 6 Oct 2019 19:06:05 +0800 Subject: Adjusted order type --- qicclient/client.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qicclient/client.hxx b/qicclient/client.hxx index 9af124d..6121c5e 100644 --- a/qicclient/client.hxx +++ b/qicclient/client.hxx @@ -4,7 +4,7 @@ #include struct icclient_catalog; -struct icclient_order; +struct icclient_ord_order; namespace ICClient { @@ -52,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(); }; -- cgit v1.2.3 From fdf9119b9131bde5ab15bc725a3ddc86739c057f 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: Sun, 6 Oct 2019 23:51:01 +0800 Subject: libicclient update --- libicclient | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libicclient b/libicclient index fc55559..b4791f6 160000 --- a/libicclient +++ b/libicclient @@ -1 +1 @@ -Subproject commit fc555594c636e87ffe2384e6b5240cfa934c6cf4 +Subproject commit b4791f689f28b05462645db8e6d83639515c358e -- cgit v1.2.3 From 8d44a7111c52e84046879f8392ef324dbd302024 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: Mon, 7 Oct 2019 10:14:37 +0800 Subject: libicclient various memory allocations fix --- libicclient | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libicclient b/libicclient index fc55559..6fa0df3 160000 --- a/libicclient +++ b/libicclient @@ -1 +1 @@ -Subproject commit fc555594c636e87ffe2384e6b5240cfa934c6cf4 +Subproject commit 6fa0df3c46986b15dec3be672f41eb7ea62c20ef -- cgit v1.2.3