-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshopnavcontroller.cpp
More file actions
140 lines (116 loc) · 3.84 KB
/
Copy pathshopnavcontroller.cpp
File metadata and controls
140 lines (116 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "shopnavcontroller.h"
#include <QVBoxLayout>
#include <QResizeEvent>
ShopNavController::ShopNavController(Platform* platform,
Customer* customer,
QWidget* parent)
: QWidget(parent), m_platform(platform), m_customer(customer),
m_detailPage(nullptr)
{
QVBoxLayout* vl = new QVBoxLayout(this);
vl->setContentsMargins(0, 0, 0, 0);
m_stack = new QStackedWidget(this);
m_browserPage = new ShopBrowserWidget(
m_platform,
customer ? customer->getName() : "Customer",
this);
m_stack->addWidget(m_browserPage); // index 0
vl->addWidget(m_stack);
// Cart panel overlay (hidden initially)
m_cartPanel = new CartPanel(m_platform, m_customer, this);
m_cartPanel->hide();
// Connections
connect(m_browserPage, &ShopBrowserWidget::shopSelected,
this, &ShopNavController::goToShop);
connect(m_browserPage, &ShopBrowserWidget::cartClicked,
this, &ShopNavController::openCart);
connect(m_cartPanel, &CartPanel::closeRequested,
this, [this]() { m_cartPanel->slideOut(); });
connect(m_cartPanel, &CartPanel::checkoutCompleted,
this, [this]() {
m_cartPanel->slideOut();
emit checkoutRequested();
});
connect(m_cartPanel, &CartPanel::cartChanged,
this, &ShopNavController::syncCartBadge);
syncCartBadge();
}
void ShopNavController::goToShop(Seller* seller)
{
rebuildDetailPage(seller);
m_stack->setCurrentIndex(1);
}
void ShopNavController::goBack()
{
m_stack->setCurrentIndex(0);
syncCartBadge();
}
void ShopNavController::openCart()
{
m_cartPanel->resize(size());
m_cartPanel->refreshCart();
m_cartPanel->slideIn();
m_cartPanel->raise();
}
void ShopNavController::onCartUpdated(Product* product, int qty)
{
if (!m_customer || !product) return;
Cart& cart = m_customer->getCart();
if (qty == 0) {
cart.removeItem(product->getId());
} else {
bool found = false;
for (CartItem& ci : const_cast<QVector<CartItem>&>(cart.getItems())) {
if (ci.productId == product->getId()) {
ci.quantity = qty;
found = true;
break;
}
}
if (!found) {
cart.addItem(product, qty);
}
}
syncCartBadge();
// Keep cart panel in sync if open
if (m_cartPanel->isVisible())
m_cartPanel->refreshCart();
}
void ShopNavController::onCheckout()
{
// Handled via CartPanel::checkoutCompleted signal
}
void ShopNavController::rebuildDetailPage(Seller* seller)
{
// Remove old detail page if any
if (m_detailPage) {
m_stack->removeWidget(m_detailPage);
m_detailPage->deleteLater();
}
m_detailPage = new ShopDetailWidget(seller, m_platform, this);
// Restore current cart quantities so +/- show correct state
QMap<int, int> qtys;
if (m_customer) {
for (const CartItem& ci : m_customer->getCart().getItems())
qtys[ci.productId] = ci.quantity;
}
m_detailPage->setCartQuantities(qtys);
connect(m_detailPage, &ShopDetailWidget::backClicked,
this, &ShopNavController::goBack);
connect(m_detailPage, &ShopDetailWidget::cartUpdated,
this, &ShopNavController::onCartUpdated);
connect(m_detailPage, &ShopDetailWidget::cartBtnClicked,
this, &ShopNavController::openCart);
m_stack->addWidget(m_detailPage); // index 1
}
void ShopNavController::syncCartBadge()
{
int count = m_customer ? m_customer->getCart().getItemCount() : 0;
m_browserPage->refreshCartBadge(count);
}
void ShopNavController::resizeEvent(QResizeEvent* e)
{
QWidget::resizeEvent(e);
if (m_cartPanel && m_cartPanel->isVisible())
m_cartPanel->resize(size());
}