summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2023-06-19 10:58:31 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2023-06-19 10:58:31 +0800
commit9d6608227a46e62f81b0bad78f12406ba29819cc (patch)
treecb5fb60b7d753b9200ef941232eab608a68c101b
parenta6cfa494405bef00571ef676ac153c21622946f9 (diff)
Ord items list now contain pointers to the Items
so that it can contain subclasses of Item, since the ones contained are just the pointers. The space reserved for an Item wouldn't be enough for a subclass' additional members. The space reserved for *a pointer to* an Item, on the other hand, should be enough for a later casted pointer to something derived from an Item.
-rw-r--r--interchange/ord.hxx11
-rw-r--r--ord.cxx29
2 files changed, 22 insertions, 18 deletions
diff --git a/interchange/ord.hxx b/interchange/ord.hxx
index 302e6db..390d415 100644
--- a/interchange/ord.hxx
+++ b/interchange/ord.hxx
@@ -48,7 +48,7 @@ namespace QInterchange {
Ord() {}
explicit Ord(struct interchange_ord_order *order,
QObject* parent = nullptr);
- ~Ord() {}
+ virtual ~Ord() { for (auto item : items) delete item; }
int rowCount(QModelIndex const& parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
Q_DECL_OVERRIDE;
@@ -76,13 +76,16 @@ namespace QInterchange {
protected:
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;
void init(struct interchange_ord_order *order);
- void addItem(Item const &item);
- const Item &itemAt(int row) const { return items[row]; }
+ void addItem(Item *item);
+ const Item *itemAt(int row) const
+ {
+ return items.at(row);
+ }
void emitUpdate(const QString &response);
void emitTransaction(QString const& response);
private:
- QList<Item> items;
+ QList<Item *> items;
QString profile;
double m_subtotal;
double m_shipping;
diff --git a/ord.cxx b/ord.cxx
index 8472361..c2c9723 100644
--- a/ord.cxx
+++ b/ord.cxx
@@ -14,7 +14,7 @@ namespace QInterchange {
{
init(order);
for (size_t i = 0; i < order->nitems; i++)
- addItem(Item{&order->items[i]});
+ addItem(new Item{&order->items[i]});
}
void Ord::init(struct interchange_ord_order *order)
@@ -35,24 +35,24 @@ namespace QInterchange {
{
auto row = index.row();
if (row < 0 || row >= items.count()) return QVariant();
- auto item = items[row];
+ auto item = items.at(row);
switch (role) {
case Product::SkuRole:
- return item.sku;
+ return item->sku;
case Product::TitleRole:
- return item.title;
+ return item->title;
case Product::DescriptionRole:
- return item.description;
+ return item->description;
case Product::ImageRole:
- return item.image;
+ return item->image;
case Product::PriceRole:
- return item.price;
+ return item->price;
case Product::OptionTypeRole:
- return item.optionType;
+ return item->optionType;
case Item::QuantityRole:
- return item.quantity;
+ return item->quantity;
case Item::NameRole:
- return item.name;
+ return item->name;
default:
return QVariant();
}
@@ -72,18 +72,19 @@ namespace QInterchange {
};
}
- void Ord::addItem(Item const& item)
+ void Ord::addItem(Item *item)
{
- auto sku = item.sku;
+ auto sku = item->sku;
auto iterator = std::find_if(items.begin(), items.end(),
- [&sku](Item const& item) {
- return sku == item.sku;
+ [&sku](Item *item) {
+ return sku == item->sku;
});
if (iterator != items.end()) {
auto index = items.indexOf(*iterator);
beginRemoveRows(QModelIndex(), index, index);
items.removeAt(index);
endRemoveRows();
+ delete *iterator;
}
beginInsertRows(QModelIndex(), rowCount(), rowCount());
items << item;