-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulationwindow.cpp
87 lines (75 loc) · 2.72 KB
/
simulationwindow.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
#include "simulationwindow.h"
#include "ui_simulationwindow.h"
QColor generateRandomColor(QColor mix){
int red = rand() % 255 + 1;
int green = rand() % 255 + 1;
int blue = rand() % 255 + 1;
red = (red + mix.red()) /2;
green = (green + mix.green()) /2;
blue = (blue + mix.blue()) /2;
return QColor(red,green,blue);
}
SimulationWindow::SimulationWindow(QList<Process> processVector ,int quantum, std::string algo,bool preempetive,QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SimulationWindow)
{
ui->setupUi(this);
int currentColumnCount;
for (int i = 0; i < processVector.size(); ++i) {
processVector[i].setColor(generateRandomColor(Qt::white));
}
if(algo == "fcfs"){
algorithm = new FirstComeFirstServed();
}else if(algo == "sjf"){
algorithm = new ShortestJobFirst(preempetive);
}else if(algo == "ps"){
algorithm = new PriorityScheduling(preempetive);
}else if(algo == "rr"){
algorithm = new RoundRobin(quantum);
}
algorithm->InitializeScheduler(processVector);
thread =new DelayThread(this);
connect(thread,SIGNAL(wakeUp()),this,SLOT(onWakeUp()));
thread->start();
}
SimulationWindow::~SimulationWindow()
{
delete ui;
}
void SimulationWindow::onWakeUp()
{
int currentColumnCount=ui->tableWidget->columnCount();
if(!algorithm->allProcessesDone()) {
Process currentProcess = algorithm->getNextProcess();
int executionTime = algorithm->executeCurrentProcess();
for (int j = 0; j < executionTime; ++j) {
currentColumnCount = ui->tableWidget->columnCount();
ui->tableWidget->setColumnCount(++currentColumnCount);
QTableWidgetItem* currentColumn = new QTableWidgetItem;
ui->tableWidget->setItem(0,currentColumnCount-1 , currentColumn);
currentColumn->setBackground(currentProcess.getColor());
currentColumn->setTextAlignment(Qt::AlignCenter);
if(currentProcess.getColor()==Qt::white){
currentColumn->setTextColor(Qt::white);
}else{
currentColumn->setTextColor(Qt::black);
}
currentColumn->setText(QString::number(currentProcess.getId()));
}
QStringList list;
for(int i=0;i<currentColumnCount;i++){
list<<QString::number(i);
}
ui->tableWidget->setHorizontalHeaderLabels(list);
if(algorithm->allProcessesDone())
{
ui->time->setText(QString::number((double)algorithm->getAverageWaitingTime()));
thread->terminate();
}
}
else
{
ui->time->setText(QString::number((double)algorithm->getAverageWaitingTime()));
thread->terminate();
}
}