-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
539 lines (452 loc) · 13.5 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//local includes
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "creategraphscene.h"
#include "graphcoloring.h"
#include "decryption.h"
#include "basicdictionary.h"
//Qt Includes
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QGraphicsItem>
#include <QGraphicsEllipseItem>
#include <QTextStream>
#include <QMap>
#include <QPair>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("SITH - Join the Dark Side");
// const QPixmap pix(":/dw_sith.jpg");
// ui->label->setPixmap(pix);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_released() //display file
{
inFile.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&inFile);
//extracting all text from input file
QString mText = in.readAll();
inFile.close();
//test output of string
qDebug() << "Test of QString:";
qDebug() << mText;
//using decryption class and decrypting the whole text
decryption Dec;
mText = Dec.getDecrypted(mText,this->decryptMap);
//test output of decrypted string
qDebug() << "Test output of decrypted String";
qDebug() << mText;
//adding new scene
scene = new QGraphicsScene(this);
//setting graphics view to view scene
ui->graphicsView->setScene(scene);
//adding text to scene
text = scene->addText(mText);
//showing graphicsView
ui->graphicsView->show();
}
void MainWindow::on_pushButton_2_released() // show graph
{
qDebug() << "In pushButton_2_released function";
CreateGraphScene cgs(this->vertices, this->edges);
qDebug() << "Object Created";
scene = cgs.getGraphicsScene();
qDebug() << "Scene Received";
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
}
void MainWindow::on_pushButton_3_released() //export file
{
//While exporting the file, the file should be the same as in the initial
//file till the last paragraph. In the last paragraph, we would have the
//edited messages instead of the original messages
//Step 1. Create output file name
QString outputFileName;
int index;
outputFileName = "";
index = this->inputFileName.lastIndexOf('.');
outputFileName = inputFileName.left(index);
outputFileName = outputFileName + "-edited" + ".txt";
qDebug() << "Output File Name: " << outputFileName;
//Step 2. Create output file stream
if (!inFile.open((QFile::ReadOnly | QFile::Text)))
{
qDebug() << "Could not read from file";
return;
}
QFile oFile(outputFileName);
if (!oFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "Could not open file to write";
return;
}
qDebug() << "File opened successfully";
//Step 3. Copy from input file to output file till the 3rd paragraph
QTextStream in(&inFile);
QTextStream out(&oFile);
QString mText;
//traverse till the third blank line
int flag = 0;
int count = 0;
while(true)
{
count ++;
mText = in.readLine();
qDebug() << mText;
out << mText << "\n";
if(mText.trimmed().length() == 0)
{
flag++;
qDebug() << count;
if (flag == 3)
break;
}
}
out << in.readLine();
out << "\n";
//Step 5. Encrypt these set of Messages
//Step 5a. Make reverse Map
this->encryptMap = reverseMap(this->decryptMap);
//Step 5b. Pass QStrings through reverseMap, and get corresponding encrypted string
QString messageOutput;
int rebelBaseCount = this->vertices.length();
int i;
decryption Dec;
for ( i = 0 ; i < rebelBaseCount ; i++ )
{
messageOutput = this->editedMessages[this->graphColor[i]];
qDebug() << "got messageOutput" << messageOutput;
messageOutput = Dec.getDecrypted(messageOutput,this->encryptMap);
qDebug() << "got encrypted Output" << messageOutput;
out << messageOutput << "\n";
}
//Step 7. Save and Exit
oFile.flush();
oFile.close();
inFile.close();
}
void MainWindow::on_pushButton_4_released() //open file
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("*.txt"));
if (fileName == "")
{
QMessageBox::critical(this, tr("Error"), tr("Could not open File"));
return;
}
QFile file(fileName);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for read";
return;
}
qDebug() << "File opened successfully";
this->inFile.setFileName(fileName);
this->inputFileName = fileName;
//display graph -- Remove
//scene = new QGraphicsScene(this);
//ui->graphicsView->setScene(scene);
QBrush redBrush(Qt::red);
QBrush blueBrush(Qt::blue);
QPen blackPen(Qt::black);
blackPen.setWidth(2);
//ellipse = scene->addEllipse(0,0,100,100,blackPen,redBrush);
//ellipse = scene->addEllipse(100,200,50,50,blackPen,blueBrush);
//ui->graphicsView->show();
//--Remove till here
//Divide the whole file into 3 strings
QString newPara = getNewsParagraph();
this->paragraph = newPara;
// text = scene->addText(newPara);
// scene->clear();
// ui->graphicsView->show();
//Get the decrypted file
decryption Dec;
QMap <char, char> dMap = Dec.getMap(newPara);
this->decryptMap = dMap;
qDebug() << dMap;
//Work on dividing the file, into lists of vertices(rebel bases)
QList <QString> vertices = getVerticesList();
this->vertices = vertices;
//decrypt Vertices
for (int i = 0 ; i < this->vertices.length() ; i ++)
{
this->vertices[i] = Dec.getDecrypted(this->vertices[i],dMap);
}
//Get edges (communication channels)
QList <QString> edges = getChannels();
this->edges = edges;
//decrypt Edges
for (int i = 0 ; i < this->edges.length() ; i ++)
{
this->edges[i] = Dec.getDecrypted(this->edges[i],dMap);
}
//Get Messages
QList <QString> messages = getMessages();
this->messages = messages;
//decrypt Messages
for (int i = 0 ; i < this->messages.length() ; i ++)
{
this->messages[i] = Dec.getDecrypted(this->messages[i],dMap);
}
//Edit Messages
QList <QString> eMsgs = editMessages();
this->editedMessages = eMsgs;
//Test Print
qDebug() << "Vertices (Rebel Bases) : " << this->vertices;
qDebug() << "Edges (Communication Channels) : " << this->edges;
qDebug() << "Messages :" << this->messages;
qDebug() << "Edited Messages: " << this->editedMessages;
//Form Graph
}
void MainWindow::on_pushButton_5_released() //Edit Messages
{
//Make QMap to index each rebel base to a number
int rebelBaseCount = this->vertices.length();
int i;
/*
int spacePos;
QString base1;
QString base2;
QMap <QString, int> baseIndex;
for ( i = 0; i < rebelBaseCount; i++)
{
baseIndex [this->vertices[i]] = i;
}
int channelCount = this->edges.length();
graphColoring g1(rebelBaseCount);
for (i = 0; i < channelCount; i++)
{
spacePos = this->edges[i].indexOf(' ');
base1=this->edges[i].left(spacePos);
temp = this->edges[i].length() - spacePos -1;
base2=this->edges[i].right(temp);
qDebug() << "Test of Base Index";
qDebug() << baseIndex[base1];
qDebug() << baseIndex[base2];
g1.addEdge(baseIndex[base1], baseIndex[base2]);
//--- test printing----
qDebug()<<"Base1:"<<base1<<"Base2:"<<base2;
}
qDebug() << "Coloring of graph";
QVector <int> g1Color = g1.greedyColoring();
qDebug() << "vector is: " << g1Color;
//now making initial message the most used message
g1Color = g1.sortMessages(rebelBaseCount,g1Color);
qDebug() << "Sorted Vector is : " << g1Color;
int messageCount = g1.messageCount(rebelBaseCount, g1Color);
QString messageOutput;
messageOutput = QString::number(messageCount);
messageOutput = messageOutput + "\n";
basicDictionary bd;
QList <QString> fakeMsgs = bd.getWordList(this->messages[0], messageCount);
qDebug() << "Fake messages generated: ";
*/
QString messageOutput;
messageOutput = "Total Messages Used: " + QString::number(messageCount);
messageOutput = messageOutput + "\n";
for ( i = 0 ; i < rebelBaseCount ; i++ )
{
messageOutput = messageOutput + this->vertices[i] + ": " + this->editedMessages[this->graphColor[i]] + "\n";
}
//adding new scene
scene = new QGraphicsScene(this);
//setting graphics view to view scene
ui->graphicsView->setScene(scene);
//adding text to scene
text = scene->addText(messageOutput);
//showing graphicsView
ui->graphicsView->show();
}
QMap<char, char> MainWindow::reverseMap(QMap<char, char> input)
{
QMap <char, char> output;
qDebug() << "in reverse map";
output.insert('.','.');
output.insert(' ', ' ');
char allChar[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',\
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',\
'y', 'z'};
int i;
char temp;
for ( i = 0 ; i < 26; i ++ )
{
temp = input[allChar[i]];
output.insert(temp, allChar[i]);
}
qDebug() << output;
qDebug() << "returning from reverse Map";
return output;
}
QString MainWindow::getNewsParagraph()
{
inFile.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&inFile);
QString mText = in.readLine();
mText=in.readAll();
qDebug() << mText;
inFile.close();
return mText;
}
QList<QString> MainWindow::getVerticesList()
{
//Task is to traverse till the first blank line
inFile.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&inFile);
QString mText;
QList <QString> list;
//traverse till the first blank space
int count = 0;
while(true)
{
count ++;
mText = in.readLine();
if(mText.trimmed().length() == 0)
{
break;
}
}
qDebug() << count;
//reached first blank line
mText = in.readLine();
//now the list starts
count ++;
while(true)
{
count ++;
mText = in.readLine();
if (mText.trimmed().length() == 0)
{
break;
//break at next blank line
}
list.append(mText);
}
inFile.close();
return list;
}
QList<QString> MainWindow::getChannels()
{
//Task is to traverse till the second blank line
inFile.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&inFile);
QString mText;
QList <QString> list;
//traverse till the second blank line
int count = 0;
int flag = 0;
while(true)
{
count ++;
mText = in.readLine();
if(mText.trimmed().length() == 0)
{
flag++;
if (flag == 2)
break;
}
}
qDebug() << count;
//reached second blank line
mText = in.readLine();
//now the list starts
count ++;
while(true)
{
count ++;
mText = in.readLine();
if (mText.trimmed().length() == 0)
{
break;
//break at next blank line
}
list.append(mText);
}
inFile.close();
return list;
}
QList<QString> MainWindow::getMessages()
{
//Task is to traverse till the third blank line
inFile.open(QFile::ReadOnly | QFile::Text);
QTextStream in(&inFile);
QString mText;
QList <QString> list;
//traverse till the second blank line
int count = 0;
int flag = 0;
while(true)
{
count ++;
mText = in.readLine();
if(mText.trimmed().length() == 0)
{
flag++;
if (flag == 3)
break;
}
}
//reached third blank line
mText = in.readLine();
//now the list starts
count ++;
while(true)
{
count ++;
mText = in.readLine();
if (mText.trimmed().length() == 0)
{
break;
}
list.append(mText);
}
inFile.close();
return list;
}
QList<QString> MainWindow::editMessages()
{
int rebelBaseCount = this->vertices.length();
int spacePos;
int i,temp;
QString base1;
QString base2;
QMap <QString, int> baseIndex;
for ( i = 0; i < rebelBaseCount; i++)
{
baseIndex [this->vertices[i]] = i;
}
int channelCount = this->edges.length();
graphColoring g1(rebelBaseCount);
for (i = 0; i < channelCount; i++)
{
spacePos = this->edges[i].indexOf(' ');
base1=this->edges[i].left(spacePos);
temp = this->edges[i].length() - spacePos -1;
base2=this->edges[i].right(temp);
qDebug() << "Test of Base Index";
qDebug() << baseIndex[base1];
qDebug() << baseIndex[base2];
g1.addEdge(baseIndex[base1], baseIndex[base2]);
//--- test printing----
qDebug()<<"Base1:"<<base1<<"Base2:"<<base2;
}
qDebug() << "Coloring of graph";
QVector <int> g1Color = g1.greedyColoring();
qDebug() << "vector is: " << g1Color;
//now making initial message the most used message
g1Color = g1.sortMessages(rebelBaseCount,g1Color);
qDebug() << "Sorted Vector is : " << g1Color;
this->messageCount = g1.messageCount(rebelBaseCount, g1Color);
basicDictionary bd;
QList <QString> fakeMsgs = bd.getWordList(this->messages[0], messageCount);
qDebug() << "Fake messages generated: ";
this->graphColor = g1Color;
return fakeMsgs;
}