-
Notifications
You must be signed in to change notification settings - Fork 2
/
layout.cpp
305 lines (261 loc) · 9.64 KB
/
layout.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "layout.h"
#include "ui_layout.h"
#include<QStringList>
#include<QDebug>
#include <QInputDialog>
#include <QMessageBox>
#include <fstream>
#include <sstream>
#include <QFileDialog>
using std::fstream;
Layout::Layout(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Layout)
{
ui->setupUi(this);
holedialog = new HolesDialog(this);
processdialog = new ProcessDialog(this);
ui->HolesTableViewTBlwdgt->setColumnCount(2);
ui->HolesTableViewTBlwdgt->setHorizontalHeaderLabels({"Starting Address","Hole Size"});
ui->tabWidget->setTabText(0,"Main");
ui->tabWidget->setTabText(1,"Processes");
ui->CBAlgorithmType->addItem("First-Fit");
ui->CBAlgorithmType->addItem("Best-Fit");
ui->HolesTableViewTBlwdgt->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->ProcessTree->setColumnCount(3);
ui->ProcessTree->setHeaderLabels({"Process name","Segment name","Segment Size"});
ui->AllocationInfoTree->setColumnCount(4);
ui->AllocationInfoTree->setHeaderLabels({"Process name","Segment Name","Starting Address","Segment Size"});
ui->DealocateButton->setEnabled(false);
ui->ProcessDealocate->setEnabled(false);
ui->Holesbutton->setEnabled(false);
ui->Processbutton->setEnabled(false);
ui->SimulateButton->setEnabled(false);
}
Layout::~Layout()
{
delete ui;
}
void Layout::on_Holesbutton_clicked()
{
int ret = holedialog->exec();
auto result = holedialog->HoleData();
if(ret == QDialog::Accepted)
{
int rowcount{ui->HolesTableViewTBlwdgt->rowCount()+1};
ui->HolesTableViewTBlwdgt->setRowCount(rowcount);
ui->HolesTableViewTBlwdgt->setItem(rowcount-1,0,new QTableWidgetItem(result.first));
ui->HolesTableViewTBlwdgt->setItem(rowcount-1,1,new QTableWidgetItem(result.second));
auto HoleAdd = result.first.toInt();
auto HoleSize = result.second.toInt();
MemoryOperations.set_free_space(HoleAdd,HoleSize);
graph.AddHoles(HoleAdd,HoleSize);
ui->verticalLayout_5->addWidget(graph.ViewChart());
}
else
{
return;
}
}
void Layout::on_Processbutton_clicked()
{
int retval = processdialog->exec();
if(retval == QDialog::Accepted)
{
auto Result= processdialog->ProcessInfo();
// qDebug() << Result.second.size();
AddProccesRoot(Result.first,Result.second);
// ui->ProcessDealocate->addItem(QString::fromStdString(Result.first));
vector<segment> v;
for(auto& a:Result.second)
{
v.push_back({Result.first,a.first, atoi(a.second.c_str())});
}
if(ui->CBAlgorithmType->currentIndex() == FirstFit)
MemoryOperations.create_new_process_first_fit(v);
else
MemoryOperations.create_new_process_best_fit(v);
}
else {
return;
}
}
void Layout::on_DrawingView_clicked()
{
ui->stackedWidget->setCurrentIndex(0);
}
void Layout::on_TablesView_clicked()
{
ui->stackedWidget->setCurrentIndex(1);
}
void Layout::on_Memorysizebutton_clicked()
{
if(ui->MemorySizeInput->text().toStdString() == "")
{
QMessageBox::information(this,"Error","No Memory Size");
return;
}
auto Memsize = ui->MemorySizeInput->text().toUInt();
MemoryOperations.set_size_of_memory(Memsize);
graph.BuildMemory(Memsize);
ui->verticalLayout_5->addWidget(graph.ViewChart());
ui->Memorysizebutton->setEnabled(false);
ui->Holesbutton->setEnabled(true);
ui->Processbutton->setEnabled(true);
ui->SimulateButton->setEnabled(true);
}
void Layout::on_SimulateButton_clicked()
{
auto Info = MemoryOperations.return_process_information();
AllocatioRoot(Info);
auto ToGraph = MemoryOperations.return_memory_section();
auto procNames = MemoryOperations.ProcessNames();
for(auto Nm: procNames)
ui->ProcessDealocate->addItem(QString::fromStdString(Nm));
graph.Draw(ToGraph,procNames);
ui->verticalLayout_5->addWidget(graph.ViewChart());
if(MemoryOperations.return_waiting_queue().size())
QMessageBox::information(this,"Un Allocated Process","There are "+QString::number(MemoryOperations.return_waiting_queue().size())+" unallocated process Look for the allocation table");
ui->Holesbutton->setEnabled(false);
ui->Processbutton->setEnabled(false);
ui->SimulateButton->setEnabled(false);
ui->DealocateButton->setEnabled(true);
ui->ProcessDealocate->setEnabled(true);
ui->ProcessDealocate->addItem("allocated space");
}
void Layout::AddProccesRoot(std::string Processname, vector<std::pair<std::string,std::string>>Processchild)
{
QTreeWidgetItem* Process = new QTreeWidgetItem(ui->ProcessTree);
Process->setText(0,QString::fromStdString(Processname));
ui->ProcessTree->addTopLevelItem(Process);
ProcessChildrens(Process,Processchild);
}
void Layout::ProcessChildrens(QTreeWidgetItem *parent, vector<std::pair<std::string,std::string>> Processchild)
{
for(auto& childs: Processchild)
{
QTreeWidgetItem *Childs = new QTreeWidgetItem();
Childs->setText(1,QString::fromStdString(childs.first));
Childs->setText(2,QString::fromStdString(childs.second));
parent->addChild(Childs);
}
}
void Layout::AllocatioRoot(vector<vector<allocated_item_information> > AllocatedINfo)
{
for(auto& EachProcinfo: AllocatedINfo)
{
QTreeWidgetItem* ProcessN= new QTreeWidgetItem(ui->AllocationInfoTree);
ProcessN->setText(0,QString::fromStdString(EachProcinfo[0].process_name));
ui->AllocationInfoTree->addTopLevelItem(ProcessN);
AllocationChild(ProcessN,EachProcinfo);
}
}
void Layout::AllocationChild(QTreeWidgetItem *parent,vector<allocated_item_information> ProcessInfo)
{
for(auto& ProcChild: ProcessInfo)
{
QTreeWidgetItem* Childs = new QTreeWidgetItem();
Childs->setText(1,QString::fromStdString(ProcChild.segment_name));
Childs->setText(2,QString::number(ProcChild.base_address));
Childs->setText(3,QString::number(ProcChild.limit));
parent->addChild(Childs);
}
}
void Layout::on_actionRun_triggered()
{
if(ui->MemorySizeInput->text().toStdString() == "")
{
QMessageBox::information(this,"Error","No Memory Size");
return;
}
auto Info = MemoryOperations.return_process_information();
AllocatioRoot(Info);
auto ToGraph = MemoryOperations.return_memory_section();
auto procNames = MemoryOperations.ProcessNames();
for(auto Nm: procNames)
ui->ProcessDealocate->addItem(QString::fromStdString(Nm));
graph.Draw(ToGraph,procNames);
ui->verticalLayout_5->addWidget(graph.ViewChart());
if(MemoryOperations.return_waiting_queue().size())
QMessageBox::information(this,"Un Allocated Process","There are "+QString::number(MemoryOperations.return_waiting_queue().size())+" unallocated process Look for the allocation table");
ui->Holesbutton->setEnabled(false);
ui->Processbutton->setEnabled(false);
ui->SimulateButton->setEnabled(false);
ui->DealocateButton->setEnabled(true);
ui->ProcessDealocate->setEnabled(true);
ui->ProcessDealocate->addItem("allocated space");
}
void Layout::on_actionClear_triggered()
{
ui->AllocationInfoTree->clear();
ui->ProcessTree->clear();
while(ui->HolesTableViewTBlwdgt->rowCount())
ui->HolesTableViewTBlwdgt->removeRow(0);
ui->MemorySizeInput->clear();
MemoryOperations.ClearAllData();
graph.CLeaRData();
ui->ProcessDealocate->clear();
ui->Memorysizebutton->setEnabled(true);
ui->Holesbutton->setEnabled(false);
ui->Processbutton->setEnabled(false);
ui->SimulateButton->setEnabled(true);
ui->DealocateButton->setEnabled(false);
ui->ProcessDealocate->setEnabled(false);
}
void Layout::on_DealocateButton_clicked()
{
auto name = ui->ProcessDealocate->currentText().toStdString();
if(name == "allocated space")
MemoryOperations.deallocation_old_process(QInputDialog::getInt(this,"Dealocate old space","Set Start address"));
MemoryOperations.deallocated(ui->ProcessDealocate->currentText().toStdString());
ui->ProcessDealocate->removeItem(ui->ProcessDealocate->currentIndex());
ui->AllocationInfoTree->setStyleSheet("QTreeWidget::branch{ border-bottom: 1px solid blue;}");
auto Info = MemoryOperations.return_process_information();
AllocatioRoot(Info);
auto ToGraph = MemoryOperations.return_memory_section();
// qDebug() << "Size is" << ToGraph.size();
graph.Draw(ToGraph,MemoryOperations.ProcessNames());
ui->verticalLayout_5->addWidget(graph.ViewChart());
}
void Layout::on_actionOpen_triggered()
{
auto FileName = QFileDialog::getOpenFileName(this,"open File","E:",tr("Text File (*.txt)")).toStdString();
if(!FileName.length())
return;
ifstream File (FileName);
std::string line,val,segname;
int startadd,size;
if(File.is_open())
{
while(std::getline(File,line))
{
std::istringstream Mystream(line);
// qDebug() << QString::fromStdString(line);
Mystream >> val;
if(val == "H")
{
Mystream >> startadd >> size;
// qDebug() << QString::number(startadd) << QString::number(size);
}
else if (val[0] >= '0' && val[0] <= '9')
{
// qDebug() << QString::fromStdString(val);
}
else
{
// Mystream >> segname >> size;
// qDebug() << QString::fromStdString(val) << QString::fromStdString(segname) << QString::number(size);
}
}
}
else
QMessageBox::warning(this,"open File","File not Found");
return;
}
void Layout::on_actionAbout_Project_triggered()
{
}
void Layout::on_actionSaveAs_triggered()
{
// std::ifstream FileWrite("File.txt");
}