diff options
-rw-r--r-- | catalog.cxx | 55 | ||||
-rw-r--r-- | qicclient/catalog.hxx | 10 | ||||
-rw-r--r-- | qicclient/product.hxx | 3 |
3 files changed, 21 insertions, 47 deletions
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<int, QByteArray> Catalog::roleNames() const { return QHash<int, QByteArray>{ - {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<icclient_catalog*>(catalog->constData()); - emit updated(); - } - } diff --git a/qicclient/catalog.hxx b/qicclient/catalog.hxx index 7d4dda2..244f4f0 100644 --- a/qicclient/catalog.hxx +++ b/qicclient/catalog.hxx @@ -14,17 +14,11 @@ namespace QICClient { public: Catalog(icclient_catalog* catalog, QObject* parent = nullptr); - Catalog(QObject* parent = nullptr) : QAbstractListModel{parent} {} + ~Catalog(); int rowCount(QModelIndex const& parent = QModelIndex()) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; icclient_catalog const* constData() const { return m_data; } - public slots: - void update(Catalog* catalog); - - signals: - void updated(); - protected: QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE; @@ -36,4 +30,4 @@ namespace QICClient { } -#endif // QICCLIENT_CATALOG_HXX +#endif diff --git a/qicclient/product.hxx b/qicclient/product.hxx index 5d19d41..18b850a 100644 --- a/qicclient/product.hxx +++ b/qicclient/product.hxx @@ -20,7 +20,6 @@ namespace QICClient { CrossSellRole }; - Product() {} Product(icclient_product* product) : price{product->price}, weight{product->weight} @@ -60,4 +59,4 @@ namespace QICClient { } -#endif // QICCLIENT_PRODUCT_HXX +#endif |