-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas_station.cpp
109 lines (96 loc) · 3.24 KB
/
gas_station.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
#include "gas_station.h"
#include "options.h"
#include "request_generator.h"
#include "./ui_gas_station.h"
#include <iostream>
#include <QTimer>
#include <QThread>
#include <QPainter>
TGasStation::TGasStation(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::TGasStation)
{
TOptions options;
ui->setupUi(this);
ui->petrols->setRowCount(5);
ui->petrols->setColumnCount(2);
ui->petrols->setHorizontalHeaderLabels({"price", "markup"});
ui->petrols->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
for (int row = 0; row < ui->petrols->horizontalHeader()->count(); ++row) {
ui->petrols->horizontalHeader()->setSectionResizeMode(row, QHeaderView::Stretch);
}
}
TGasStation::~TGasStation()
{
delete ui;
}
void TGasStation::on_run_clicked()
{
if (Running_) {
return;
}
Running_ = true;
TOptions options;
options.QueueCount = ui->queue_count->text().toInt();
options.MaxQueueSize = ui->max_queue_size->text().toInt();
options.SimulationPeriod = ui->simulation_period->text().toInt();
for (int petrolIndex = 0; petrolIndex < ui->petrols->rowCount(); ++petrolIndex) {
auto priceItem = ui->petrols->item(petrolIndex, 0);
auto markupItem = ui->petrols->item(petrolIndex, 1);
if (!priceItem || !markupItem) {
break;
}
auto price = priceItem->text();
auto markup = markupItem->text();
if (price.size() == 0 || markup.size() == 0) {
break;
}
TPetrol petrol;
petrol.Price = price.toDouble();
petrol.Markup = markup.toDouble();
options.Petrols.push_back(petrol);
}
TRequestGenerator requestGenerator(options);
auto requests = requestGenerator.GenerateRequests();
State_ = TState(requestGenerator.GenerateRequests(), options);
int time = 0;
while (time < Week) {
if (!Running_) {
break;
}
State_.Simulate(time);
ui->Statistics->setText(QString::fromUtf8(State_.GetStatistics().Print(time).c_str()));
auto queueSizes = State_.GetQueueSizes();
QPixmap pixmap(ui->image->width(), ui->image->height());
QPainter painter(&pixmap);
painter.fillRect(pixmap.rect(), Qt::white);
for (size_t queueIndex = 0; queueIndex < queueSizes.size(); ++queueIndex) {
int x = (GasStationWidth + Offset) * queueIndex;
painter.drawPixmap(x, 0, GasStationWidth, GasStationHeight, QPixmap(":shop.png"));
for (int carIndex = 0; carIndex < queueSizes[queueIndex]; ++carIndex) {
int y = GasStationHeight + Offset + (CarHeight + Offset) * carIndex;
painter.drawPixmap(x, y, CarWidth, CarHeight, QPixmap(":car.png"));
}
}
painter.end();
ui->image->setPixmap(pixmap);
if (time != Week - 1 && time + options.SimulationPeriod > Week) {
time = Week - 1;
} else {
time += options.SimulationPeriod;
}
repaint();
Delay(500);
}
Running_ = false;
}
void TGasStation::Delay(int msec) const
{
QEventLoop loop;
QTimer::singleShot(msec, &loop, &QEventLoop::quit);
loop.exec();
}
void TGasStation::on_stop_clicked()
{
Running_ = false;
}