-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
224 lines (170 loc) · 6.02 KB
/
Copy pathmainwindow.cpp
File metadata and controls
224 lines (170 loc) · 6.02 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QGraphicsTextItem>
#include <QGuiApplication>
#include <QMessageBox>
#include <QScreen>
#include "algorithm"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(0);
ui->startSimulationButton->setDisabled(true);
ui->fullSimulationButton->setDisabled(true);
ui->graphicsView->setScene(new QGraphicsScene(this));
this->resize(1024, 768);
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
int x = (screenGeometry.width() - this->width()) / 2;
int y = (screenGeometry.height() - this->height()) / 2;
this->move(x, y);
}
MainWindow::~MainWindow()
{
delete ui;
Simulator::free();
}
void MainWindow::on_selectFileButton_clicked()
{
QString filePath = QFileDialog::getOpenFileName(this,
tr("Abrir Arquivo"),
QDir::homePath(),
tr("*.txt"));
ui->messagesList->clear();
if (filePath.isEmpty()) {
ui->messagesList->addItem("Nenhum arquivo selecionado.");
return;
}
auto errors = Simulator::load(filePath);
if (errors.empty()) {
ui->messagesList->addItem("Configuração carregada com sucesso!");
ui->startSimulationButton->setDisabled(false);
ui->fullSimulationButton->setDisabled(false);
this->simulator = Simulator::getInstance();
return;
}
for (const QString &error : errors) {
this->ui->messagesList->addItem(error);
}
}
void MainWindow::on_startSimulationButton_clicked()
{
if (!this->simulator) {
return;
}
this->ui->stackedWidget->setCurrentIndex(1);
this->simulator->start();
this->ui->previousQuantunButton->setDisabled(true);
updateGanttChart();
}
void MainWindow::on_nextQuantumButton_clicked()
{
if (!this->simulator || this->simulator->hasFinished()) {
return;
}
this->simulator->runQuantum();
updateGanttChart();
this->ui->previousQuantunButton->setDisabled(false);
if (this->simulator->hasFinished()) {
this->ui->nextQuantumButton->setDisabled(true);
}
}
void MainWindow::on_fullSimulationButton_clicked()
{
if (!this->simulator) {
return;
}
this->ui->stackedWidget->setCurrentIndex(1);
this->simulator->start();
while (!this->simulator->hasFinished()) {
this->simulator->runQuantum();
}
this->ui->previousQuantunButton->setDisabled(false);
this->ui->nextQuantumButton->setDisabled(true);
updateGanttChart();
}
void MainWindow::updateGanttChart()
{
if (!this->simulator) return;
QGraphicsScene *scene = this->ui->graphicsView->scene();
scene->clear();
const int BOX_SIZE = 30;
const int TASK_SPACING = 40;
const int TIME_HEADER = 20;
auto tasks = this->simulator->getTasks();
auto history = this->simulator->getHistory();
int taskYPosition = TIME_HEADER;
// Set tasks ids at the left of the graph
for (int i = tasks.size() - 1; i >= 0; i--) {
QGraphicsTextItem *taskName = scene->addText(tasks.at(i)->getId());
taskName->setPos(-50, taskYPosition + 5);
taskYPosition += TASK_SPACING;
}
int time = 0;
// Draw the tasks blocks
for (auto history : this->simulator->getHistory()) {
taskYPosition = TIME_HEADER;
for (int i = tasks.size() - 1; i >= 0; i--) {
QPen boxPen(Qt::gray);
QBrush boxBrush;
auto currentId = tasks.at(i)->getId();
auto activeTasks = history.getActiveTasks();
auto it = std::find_if(activeTasks.begin(),
activeTasks.end(),
[currentId](TaskControlBlock *task) {
return task->getId() == currentId;
});
if (it == activeTasks.end()) {
taskYPosition += TASK_SPACING;
continue;
}
QString runningTaskID = history.getRunningTask() ? history.getRunningTask()->getId()
: "";
if (tasks.at(i)->getId() == runningTaskID) {
boxPen.setColor(Qt::black);
boxBrush.setColor(tasks.at(i)->getColor());
boxBrush.setStyle(Qt::SolidPattern);
} else {
boxBrush.setStyle(Qt::NoBrush);
}
scene->addRect(time * BOX_SIZE, taskYPosition, BOX_SIZE, BOX_SIZE, boxPen, boxBrush);
taskYPosition += TASK_SPACING;
}
QGraphicsTextItem *timeText = scene->addText(QString::number(history.getInstant()));
timeText->setPos(time * BOX_SIZE, 0);
time++;
}
this->ui->graphicsView->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
void MainWindow::on_saveButton_clicked()
{
QString filePath
= QFileDialog::getSaveFileName(this,
tr("Salvar Gráfico como Imagem"),
QDir::homePath(),
tr("Imagens PNG (*.png);;Imagens JPEG (*.jpg *.jpeg)"));
if (filePath.isEmpty()) {
return;
}
if (!filePath.endsWith(".png")) {
filePath += ".png";
}
QPixmap pixmap = this->ui->graphicsView->grab();
if (!pixmap.save(filePath)) {
QMessageBox::warning(this,
tr("Erro ao Salvar"),
tr("Não foi possível salvar a imagem no local especificado."));
}
}
void MainWindow::on_previousQuantunButton_clicked()
{
this->simulator->undoQuantun();
updateGanttChart();
if (this->simulator->getHistory().size() <= 1) {
this->ui->previousQuantunButton->setDisabled(true);
}
this->ui->nextQuantumButton->setDisabled(false);
}