From a01bfc11355f041eb4225d5d104fee83e17db444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Fri, 18 Jun 2021 15:33:05 +0800 Subject: Simplify catalog and product --- catalog.cxx | 55 ++++++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) (limited to 'catalog.cxx') diff --git a/catalog.cxx b/catalog.cxx index 271dedd..e978858 100644 --- a/catalog.cxx +++ b/catalog.cxx @@ -5,11 +5,15 @@ namespace QICClient { Catalog::Catalog(icclient_catalog* catalog, QObject* parent) : - m_data{catalog}, - QAbstractListModel{parent} + QAbstractListModel{parent}, + m_data{catalog} { - for (size_t i = 0; i < catalog->length; i++) - addProduct(Product{catalog->products[i]}); + for (size_t i = 0; i < catalog->length; i++) addProduct(Product{catalog->products[i]}); + } + + Catalog::~Catalog() + { + icclient_free_catalog(m_data); } int Catalog::rowCount(QModelIndex const& parent) const @@ -21,9 +25,7 @@ namespace QICClient { QVariant Catalog::data(QModelIndex const& index, int role) const { auto row = index.row(); - if (row < 0 || row >= products.count()) return QVariant(); - auto product = products[row]; switch (role) { case Product::SkuRole: @@ -54,16 +56,16 @@ namespace QICClient { QHash Catalog::roleNames() const { return QHash{ - {Product::SkuRole, "sku"} - , {Product::DescriptionRole, "description"} - , {Product::CommentRole, "comment"} - , {Product::ThumbRole, "thumb"} - , {Product::ImageRole, "image"} - , {Product::PriceRole, "price"} - , {Product::ProdGroupRole, "prodGroup"} - , {Product::WeightRole, "weight"} - , {Product::AuthorRole, "author"} - , {Product::CrossSellRole, "crossSell"} + { Product::SkuRole, "sku" }, + { Product::DescriptionRole, "description" }, + { Product::CommentRole, "comment" }, + { Product::ThumbRole, "thumb" }, + { Product::ImageRole, "image" }, + { Product::PriceRole, "price" }, + { Product::ProdGroupRole, "prodGroup" }, + { Product::WeightRole, "weight" }, + { Product::AuthorRole, "author" }, + { Product::CrossSellRole, "crossSell" } }; } @@ -74,25 +76,4 @@ namespace QICClient { endInsertRows(); } - void Catalog::update(Catalog* catalog) - { - for (int i = 0; i < catalog->rowCount(); i++) { - auto index = catalog->index(i, 0); - Product product; - product.sku = catalog->data(index, Product::SkuRole).toString(); - product.description = catalog->data(index, Product::DescriptionRole).toString(); - product.comment = catalog->data(index, Product::CommentRole).toString(); - product.thumb = catalog->data(index, Product::ThumbRole).toString(); - product.image = catalog->data(index, Product::ImageRole).toString(); - product.price = catalog->data(index, Product::PriceRole).toDouble(); - product.prodGroup = catalog->data(index, Product::ProdGroupRole).toString(); - product.weight = catalog->data(index, Product::WeightRole).toDouble(); - product.author = catalog->data(index, Product::AuthorRole).toString(); - product.crossSell = catalog->data(index, Product::CrossSellRole).toStringList(); - addProduct(product); - } - this->m_data = const_cast(catalog->constData()); - emit updated(); - } - } -- cgit v1.2.3