-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
183 lines (147 loc) · 5.65 KB
/
mainwindow.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDate>
#include <QIcon>
#include <QMessageBox>
#include <QTime>
#include "order.h"
#include "menu.h"
MainWindow::MainWindow(QWidget *parent, int table_number, int dine_number) :
QMainWindow(parent),
ui(new Ui::MainWindow),
dine_number(dine_number),
table_number(table_number),
confirm(false),
ispayment(false)
{
ui->setupUi(this);
mtime = new QTimer(this);
mtime->start(1000); // 定时器每秒触发一次
connect(mtime, &QTimer::timeout, this, &MainWindow::update_time); // 连接定时器的timeout信号到update_time槽
ui->t_num->setText("餐桌号:" + QString::number(table_number));
ui->d_num->setText("就餐人数:" + QString::number(dine_number));
// 创建并连接菜单项的信号和槽
setupMenuConnections();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_payBt_clicked()
{
if (!confirm) {
QMessageBox::warning(this, "提示", "请确认订单后再付款!");
return;
} else {
if (ispayment) {
QMessageBox::information(this, "提示", "你已支付订单");
return;
}
// 生成订单号
orderNumber = generateOrderNumber();
//获取支付价格
menuprice = getPrice();
int ret = QMessageBox::information(this, "支付", QString("你需要支付 %1 元").arg(this->menuprice), QMessageBox::No, QMessageBox::Yes);
if (ret == QMessageBox::Yes) {
QString payInfo = QString("B.(订单号: %1)\n%2号桌已支付 %3 元").arg(orderNumber).arg(table_number).arg(menuprice);
QTcpSocket *socket = new QTcpSocket(this);
connect(socket, &QTcpSocket::connected, [this, socket, payInfo]() {
socket->write(payInfo.toUtf8());
socket->flush();
socket->disconnectFromHost();
this->ispayment = true;
});
connect(socket,QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, [this, socket](QAbstractSocket::SocketError socketError)
{
QMessageBox::warning(this, "错误", "无法连接到服务器: " + socket->errorString());
socket->deleteLater();
});
socket->connectToHost("192.168.1.16", 50001); // 替换为服务器的 IP 地址和端口
}
}
}
void MainWindow::on_serverBt_clicked()
{
QTcpSocket *socket = new QTcpSocket(this);
connect(socket, &QTcpSocket::connected, [this, socket]()
{
QString serviceRequest = QString::number(table_number) + "号桌子需要服务";
socket->write(serviceRequest.toUtf8());
socket->flush();
socket->disconnectFromHost();
});
connect(socket,QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, [this, socket](QAbstractSocket::SocketError socketError)
{
QMessageBox::warning(this, "错误", "无法连接到服务器: " + socket->errorString());
socket->deleteLater();
});
socket->connectToHost("192.168.1.16", 50001); // 替换为服务器的 IP 地址和端口
}
void MainWindow::on_orderBt_clicked()
{
if (this->confirm) {
QMessageBox::warning(this, "提示", "已提交订单,如需修改请呼叫前台!");
return;
}
if (this->orderedItems.size() == 0) {
QMessageBox::warning(this, "提示", "请先选菜品!");
return;
}
Order *om = new Order(this->table_number, this->dine_number, this); // 新建一个订单窗口
connect(om, &Order::ret_confirm_menu, this, &MainWindow::get_confirm_menu);
// 将 orderedItems 传递给订单窗口
om->setOrderedItems(orderedItems);
om->show();
}
//======加菜和减菜的槽函数===========
void MainWindow::add_food(const QString &foodName, const QString &foodPrice, int count)
{
orderedItems[foodName] = qMakePair(count, foodPrice);
qDebug() << "Added food:" << foodName << "Price:" << foodPrice << "Count:" << count;
}
void MainWindow::sub_food(const QString &foodName, const QString &foodPrice, int count)
{
if (count == 0) {
orderedItems.remove(foodName);
} else {
orderedItems[foodName] = qMakePair(count, foodPrice);
}
qDebug() << "Subtracted food:" << foodName << "Price:" << foodPrice << "Count:" << count;
}
//=======================================
void MainWindow::get_confirm_menu(bool now)
{
confirm = now;
}
void MainWindow::setupMenuConnections()
{
auto dishesWidget = ui->widget; // 假设 dishesWidget 是 Dishes 类的一个实例
const auto &menuItems = dishesWidget->getMenuItems(); // 假设 Dishes 类有一个 getMenuItems 方法返回所有 menu 项
for (auto &menuItem : menuItems) {
connect(menuItem, &menu::addsignals, this, &MainWindow::add_food);
connect(menuItem, &menu::subsignals, this, &MainWindow::sub_food);
}
}
//更新时间函数
void MainWindow::update_time()
{
ui->time->setText(QDate::currentDate().toString("yyyy-MM-dd")+" "+QTime::currentTime().toString("hh:mm:ss"));
}
// 生成订单号
QString MainWindow::generateOrderNumber()
{
QDateTime currentDateTime = QDateTime::currentDateTime();
QString orderNumber = currentDateTime.toString("MMddhhmmss");
return orderNumber;
}
//获取订单价格
double MainWindow::getPrice() const
{
double totalPrice = 0;
for (auto it = orderedItems.begin(); it != orderedItems.end(); ++it) {
int quantity = it.value().first;
double price = it.value().second.toDouble();
totalPrice += price * quantity;
}
return totalPrice;
}