-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
702 lines (622 loc) · 22 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPalette>
#include <QFileDialog>
#include <string>
/*
* Constructor
*/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Sets up the text editor
setupEditor();
// Sets up the action handlers for the Compatibility menu
QActionGroup* architectureActionGroup=new QActionGroup(this);
architectureActionGroup->addAction(ui->actionSigma16_0_1_7);
architectureActionGroup->addAction(ui->actionSigma16_1_4_4);
connect(ui->actionSigma16_0_1_7,&QAction::triggered,this,&MainWindow::toggleSigma);
connect(ui->actionSigma16_1_4_4,&QAction::triggered,this,&MainWindow::toggleSigma);
// Sets up the action handlers for the Speed menu
QActionGroup* speedActionGroup=new QActionGroup(this);
speedActionGroup->addAction(ui->actionSlow);
speedActionGroup->addAction(ui->actionMedium);
speedActionGroup->addAction(ui->actionFast);
speedActionGroup->addAction(ui->actionRealtime);
speedActionGroup->addAction(ui->actionNo_UI);
connect(ui->actionSlow,&QAction::toggled,this,&MainWindow::toggleSpeed);
connect(ui->actionMedium,&QAction::toggled,this,&MainWindow::toggleSpeed);
connect(ui->actionFast,&QAction::toggled,this,&MainWindow::toggleSpeed);
connect(ui->actionRealtime,&QAction::toggled,this,&MainWindow::toggleSpeed);
connect(ui->actionNo_UI,&QAction::toggled,this,&MainWindow::toggleSpeed);
// Sets up the action handlers for the File menu
connect(ui->actionNew,&QAction::triggered,this,&MainWindow::newFile);
connect(ui->actionOpen,&QAction::triggered,this,&MainWindow::openFile);
connect(ui->actionSave,&QAction::triggered,this,&MainWindow::saveFile);
connect(ui->actionSave_As,&QAction::triggered,this,&MainWindow::saveAsFile);
connect(ui->actionExit,&QAction::triggered,this,&MainWindow::close);
connect(ui->actionAbout,&QAction::triggered,this,&MainWindow::about);
// Sets up the action handlers for the Theme menu
QActionGroup* themeActionGroup=new QActionGroup(this);
themeActionGroup->addAction(ui->actionLight);
themeActionGroup->addAction(ui->actionDark);
connect(ui->actionLight,&QAction::toggled,this,&MainWindow::toggleTheme);
connect(ui->actionDark,&QAction::toggled,this,&MainWindow::toggleTheme);
// Sets up the action handlers for the buttons in the UI
connect(ui->assembleButton,SIGNAL(released()),this,SLOT(assemble()));
connect(ui->resetButton,SIGNAL(released()),this,SLOT(resetEmulator()));
connect(ui->loadButton,SIGNAL(released()),this,SLOT(loadData()));
connect(ui->stepButton,SIGNAL(released()),this,SLOT(stepEmulator()));
connect(ui->runButton,SIGNAL(released()),this,SLOT(runEmulator()));
connect(ui->stopButton,SIGNAL(released()),this,SLOT(stopEmulator()));
connect(ui->clearButton,SIGNAL(released()),this,SLOT(clearTrace()));
// Initializes the theme
theme=THEME_LIGHT;
background.setForeground(Qt::black);
blue.setForeground(Qt::blue);
pal.setColor(QPalette::Base,Qt::white);
pal.setColor(QPalette::Text,Qt::black);
highlighter->setTheme(theme);
// Applies the theme
applyTheme();
// Initializes the emulator
updateEmulator();
// Sets up the timer that runs the emulator
timer=new QTimer(this);
timer->setInterval(100);
timer->connect(timer,SIGNAL(timeout()),this,SLOT(timerRunEmulator()));
}
/*
* Changes the theme based on the user choice
*/
void MainWindow::toggleTheme() {
if(ui->actionLight->isChecked() && theme!=THEME_LIGHT) {
theme=THEME_LIGHT;
background.setForeground(Qt::black);
blue.setForeground(Qt::blue);
pal.setColor(QPalette::Base,Qt::white);
pal.setColor(QPalette::Text,Qt::black);
highlighter->setTheme(theme);
applyTheme();
}
else if(ui->actionDark->isChecked() && theme!=THEME_DARK) {
theme=THEME_DARK;
background.setForeground(Qt::white);
blue.setForeground(Qt::blue);
pal.setColor(QPalette::Base,Qt::black);
pal.setColor(QPalette::Text,Qt::yellow);
highlighter->setTheme(theme);
applyTheme();
}
}
/*
* Applies the theme to the individual elements
*/
void MainWindow::applyTheme() {
// Text inputs
ui->editorTextEdit->setPalette(pal);
ui->assemblerTextEdit->setPalette(pal);
// Labels
ui->emEffect->setPalette(pal);
ui->emOperands->setPalette(pal);
ui->emOperation->setPalette(pal);
ui->emType->setPalette(pal);
ui->registerADR->setPalette(pal);
ui->registerDAT->setPalette(pal);
ui->registerFile->setPalette(pal);
ui->registerIR->setPalette(pal);
ui->registerPC->setPalette(pal);
// Memory files
ui->memoryFileLeft->setPalette(pal);
ui->memoryFileRight->setPalette(pal);
// Output field
ui->inputOutput->setPalette(pal);
// Instruction list
ui->sourceCode->setPalette(pal);
// Trace
ui->traceTextEdit->setPalette(pal);
highlighter->rehighlight();
// Updates the highlighting in the memory files
QTextCursor cursorLeft(ui->memoryFileLeft->document());
cursorLeft.select(QTextCursor::Document);
cursorLeft.setCharFormat(background);
QTextCursor cursorRight(ui->memoryFileRight->document());
cursorRight.select(QTextCursor::Document);
cursorRight.setCharFormat(background);
}
/*
* Sets up the text editor
*/
void MainWindow::setupEditor() {
QTextEdit* textEdit=ui->editorTextEdit;
QPlainTextEdit* sourceCode=ui->sourceCode;
// Courier size 10
QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
font.setPointSize(10);
textEdit->setFont(font);
sourceCode->setFont(font);
// No word wrap
textEdit->setWordWrapMode(QTextOption::NoWrap);
sourceCode->setWordWrapMode(QTextOption::NoWrap);
// Intializes a new highlighter
highlighter=new Highlighter(textEdit->document());
highlighter->setLineError(4);
}
/*
* Changes the Sigma16 version based on user input
*/
void MainWindow::toggleSigma() {
// Toggles the version
if(ui->actionSigma16_0_1_7->isChecked() && architecture!=ARCHITECTURE_SIGMA16_0_1_7)
architecture=ARCHITECTURE_SIGMA16_0_1_7;
else if(ui->actionSigma16_1_4_4->isChecked() && architecture!=ARCHITECTURE_SIGMA16_1_4_4)
architecture=ARCHITECTURE_SIGMA16_1_4_4;
// Resets the emulator
resetEmulator();
}
/*
* Changes the speed of emulation based on user input
*/
void MainWindow::toggleSpeed() {
if(ui->actionSlow->isChecked() && speed!=SPEED_SLOW) {
timer->setInterval(1000);
speed=SPEED_SLOW;
uienabled=true;
}
else if(ui->actionMedium->isChecked() && speed!=SPEED_MEDIUM) {
timer->setInterval(250);
speed=SPEED_MEDIUM;
uienabled=true;
}
else if(ui->actionFast->isChecked() && speed!=SPEED_FAST) {
timer->setInterval(50);
speed=SPEED_FAST;
uienabled=true;
}
else if(ui->actionRealtime->isChecked() && speed!=SPEED_REALTIME) {
timer->setInterval(0);
speed=SPEED_REALTIME;
uienabled=true;
}
else if(ui->actionNo_UI->isChecked() && speed!=SPEED_NOUI) {
timer->setInterval(0);
speed=SPEED_NOUI;
uienabled=false;
}
}
/*
* Assembles the provided source into machine code
*/
void MainWindow::assemble() {
// Fetch a string representation of the text
QString tempString=ui->editorTextEdit->document()->toPlainText();
// Convert to C string
int dataToCopyLength = strlen(tempString.toStdString().c_str());
char* data=new char[dataToCopyLength+1];
memcpy(data,tempString.toStdString().c_str(),dataToCopyLength);
data[dataToCopyLength]='\0';
// Assemble based on Sigma16 version
if(architecture==ARCHITECTURE_SIGMA16_0_1_7) {
Assembler017 assembler(data,strlen(data));
assembler.assemble();
rawAssembly=QString(assembler.readAssembly().c_str());
}
else {
Assembler144 assembler(data,strlen(data));
assembler.assemble();
rawAssembly=QString(assembler.readAssembly().c_str());
}
// Remove the first five characters (ASM03)
QString test(rawAssembly);
if(test.left(5).compare("ASM03")!=0) {
ui->assemblerTextEdit->setText(test);
delete[] data;
return;
}
test=test.right(test.length()-5);
// Remove the source code
int sourceCodeLength=0;
int digitPosition;
for(digitPosition=0;digitPosition<test.length();digitPosition++) {
if(!isdigit(test[digitPosition].toLatin1())) {
break;
}
sourceCodeLength=sourceCodeLength*10+(test[digitPosition].toLatin1()-'0');
}
test=test.right(test.length()-(sourceCodeLength+digitPosition+1));
// Splits the assembly into groups that can be shown on the Assembly tab
assembly="";
int line=0;
char temp[50];
for(int i=0;i<test.length();i+=4) {
// Instruction line number
sprintf(temp,"%04hx",line);
assembly.append(temp[0]);
assembly.append(temp[1]);
assembly.append(temp[2]);
assembly.append(temp[3]);
assembly.append('\t');
i+=4;
// RX instruction
if(test[i]=='f') {
// First part
assembly.append(test[i]);
assembly.append(test[i+1]);
assembly.append(test[i+2]);
assembly.append(test[i+3]);
assembly.append(' ');
i+=4;
// Second part
assembly.append(test[i+4]);
assembly.append(test[i+5]);
assembly.append(test[i+6]);
assembly.append(test[i+7]);
assembly.append('\n');
i+=4;
line++;
}
// RRR instruction
else {
assembly.append(test[i]);
assembly.append(test[i+1]);
assembly.append(test[i+2]);
assembly.append(test[i+3]);
assembly.append('\n');
}
line++;
}
// Clear the text field and sets the newly assembled text
ui->assemblerTextEdit->clear();
ui->assemblerTextEdit->setText(assembly);
// Memory management
delete[] data;
}
/*
* Updates all emulator-related information in the UI
*/
void MainWindow::updateEmulator() {
// If there has been any new output since the last update, add it to the UI
QString output=emulator.getOutput().c_str();
if(output!="") {
ui->inputOutput->moveCursor(QTextCursor::End);
if(architecture==ARCHITECTURE_SIGMA16_0_1_7)
ui->inputOutput->moveCursor(QTextCursor::StartOfLine); // Sigma16 0.1.7 bugfix (printing is weird on it)
ui->inputOutput->insertPlainText(output);
}
// Don't proceed if we are in No UI mode
if(!uienabled)
return;
// Show PC/IR/ADR/DAT registers
char temp[50];
sprintf(temp,"%04hx",emulator.pc);
ui->registerPC->setText(QString(temp));
sprintf(temp,"%04hx",emulator.ir);
ui->registerIR->setText(QString(temp));
sprintf(temp,"%04hx",emulator.adr);
ui->registerADR->setText(QString(temp));
sprintf(temp,"%04hx",emulator.dat);
ui->registerDAT->setText(QString(temp));
// Update register file
ui->registerFile->clear();
ui->registerFile->appendPlainText(emulator.getRegisterFile().c_str());
// Update memory file if it's been modified since the last check (performance optimization)
if(emulator.isMemoryModified()) {
// Doesn't update the UI until the end of the needed operation (it's faster)
setUpdatesEnabled(false);
QString memoryFile=emulator.getMemoryFile().c_str();
ui->memoryFileLeft->setPlainText(memoryFile);
QTextCursor cursorLeft(ui->memoryFileLeft->document());
cursorLeft.select(QTextCursor::Document);
cursorLeft.setCharFormat(background);
ui->memoryFileRight->setPlainText(memoryFile);
QTextCursor cursorRight(ui->memoryFileRight->document());
cursorRight.select(QTextCursor::Document);
cursorRight.setCharFormat(background);
// The UI can be updated now
setUpdatesEnabled(true);
}
else {
oldLeft.setCharFormat(background);
oldRight.setCharFormat(background);
}
// If a memory address has been modified in the last instruction, move the cursor to that address
if(emulator.isMemoryAddressModified()!=-1) {
int address=emulator.isMemoryAddressModified();
QString memoryAddress=emulator.getMemoryAddress(address).c_str();
QTextCursor cursorLeft(ui->memoryFileLeft->document());
cursorLeft.setPosition(address*11,QTextCursor::MoveAnchor);
cursorLeft.setPosition((address+1)*11,QTextCursor::KeepAnchor);
cursorLeft.insertText(memoryAddress);
QTextCursor cursorRight(ui->memoryFileRight->document());
cursorRight.setPosition(address*11,QTextCursor::MoveAnchor);
cursorRight.setPosition((address+1)*11,QTextCursor::KeepAnchor);
cursorRight.insertText(memoryAddress);
}
// Set the changes to the cursor in the memory files
ui->memoryFileLeft->setTextCursor(QTextCursor(ui->memoryFileLeft->document()->findBlockByLineNumber(emulator.leftAffectedBegin)));
ui->memoryFileRight->setTextCursor(QTextCursor(ui->memoryFileRight->document()->findBlockByLineNumber(emulator.rightAffectedBegin)));
QTextCursor cursorLeft(ui->memoryFileLeft->document());
cursorLeft.setPosition(emulator.leftAffectedBegin*11,QTextCursor::MoveAnchor);
cursorLeft.setPosition(emulator.leftAffectedEnd*11,QTextCursor::KeepAnchor);
cursorLeft.setCharFormat(blue);
QTextCursor cursorRight(ui->memoryFileRight->document());
cursorRight.setPosition(emulator.rightAffectedBegin*11,QTextCursor::MoveAnchor);
cursorRight.setPosition(emulator.rightAffectedEnd*11,QTextCursor::KeepAnchor);
cursorRight.setCharFormat(blue);
oldLeft=cursorLeft;
oldRight=cursorRight;
// Updates debug information about the emulator
ui->emOperation->setText(emulator.lastOperation.c_str());
ui->emOperands->setText(emulator.lastOperands.c_str());
ui->emType->setText(emulator.lastType.c_str());
ui->emEffect->setText(emulator.lastEffect.c_str());
// Updates the line number on the source code
ui->sourceCode->setTextCursor(QTextCursor(ui->sourceCode->document()->findBlockByLineNumber(emulator.lastLine)));
// Restores the format of the previous instruction
oldSourceCodeCursor.setCharFormat(background);
// Colours the current executed instruction in the source code
QTextBlock sourceCodeBlock(ui->sourceCode->document()->findBlockByLineNumber(emulator.lastLine-1));
QTextCursor sourceCodeCursor(ui->sourceCode->document());
sourceCodeCursor.setPosition(sourceCodeBlock.position(),QTextCursor::MoveAnchor);
sourceCodeCursor.setPosition(sourceCodeBlock.position()+sourceCodeBlock.length(),QTextCursor::KeepAnchor);
sourceCodeCursor.setCharFormat(blue);
oldSourceCodeCursor=sourceCodeCursor;
// Checks for division by zero
if(emulator.lastEffect=="DIVISION BY ZERO")
showWarning("Division by zero detected!");
}
/*
* Resets the emulator
*/
void MainWindow::resetEmulator() {
emulator.reset();
updateEmulator();
ui->inputOutput->clear();
ui->sourceCode->clear();
timer->stop();
}
/*
* Loads the machine code into the emulator
*/
void MainWindow::loadData() {
std::string assembly = std::string(rawAssembly.toLocal8Bit().constData());
emulator.loadMemory(assembly);
// Updates the source code
ui->sourceCode->setPlainText(QString(emulator.getSourceCode().c_str()));
QTextCursor cursorSourceCode(ui->sourceCode->document());
cursorSourceCode.select(QTextCursor::Document);
cursorSourceCode.setCharFormat(background);
updateEmulator();
}
/*
* Performs a single step in the emulator
*/
void MainWindow::stepEmulator() {
bool temp=emulator.isHalted();
if(architecture==ARCHITECTURE_SIGMA16_0_1_7)
emulator.step017();
else if(architecture==ARCHITECTURE_SIGMA16_1_4_4)
emulator.step144();
// If the emulator hasn't been halted before the operation, add the previous instruction to the trace
if(!temp)
addToTrace();
updateEmulator();
}
/*
* Runs the emulator
*/
void MainWindow::runEmulator() {
running=true;
timer->start();
}
/*
* Helper function for the emulator run timer
*/
void MainWindow::timerRunEmulator() {
if(!running)
return;
// Executes an instruction and checks if there is a halt
if((architecture==ARCHITECTURE_SIGMA16_0_1_7 && !emulator.step017()) ||
(architecture==ARCHITECTURE_SIGMA16_1_4_4 && !emulator.step144())) {
// Halted
addToTrace();
updateEmulator();
running=false;
timer->stop();
return;
}
// Not halted
addToTrace();
updateEmulator();
}
/*
* Stops the emulator run timer
*/
void MainWindow::stopEmulator() {
running=false;
timer->stop();
}
/*
* Clears the trace
*/
void MainWindow::clearTrace() {
ui->traceTextEdit->clear();
}
/*
* Shows an About message box
*/
void MainWindow::about() {
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Information);
msgBox.setWindowTitle("Cigma16");
msgBox.setText("C++ - based Sigma16 Assembler/Emulator\n\
Based on the architecture provided by Dr John O'Donnell\n\
Created by Petar Petrov");
msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
}
/*
* Adds the last instruction information to the trace
*/
void MainWindow::addToTrace() {
if(!uienabled)
return;
char temp[50];
QString string;
sprintf(temp,"%d",emulator.executedInstructions);
string.append(temp);
string.append(". ");
string.append(emulator.lastOperation.c_str());
string.append(" ");
string.append(emulator.lastOperands.c_str());
string.append(" Effect: ");
string.append(emulator.lastEffect.c_str());
string.append(" pc=");
sprintf(temp,"%04hx",emulator.pc);
string.append(temp);
string.append(" ir=");
sprintf(temp,"%04hx",emulator.ir);
string.append(temp);
string.append(" adr=");
sprintf(temp,"%04hx",emulator.adr);
string.append(temp);
string.append(" dat=");
sprintf(temp,"%04hx",emulator.dat);
string.append(temp);
ui->traceTextEdit->appendPlainText(string);
}
/*
* Action handler for the File->New menu button
*/
void MainWindow::newFile() {
// Message box
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Are you sure you want to create a new file?");
msgBox.addButton(QMessageBox::Yes);
QAbstractButton* buttonNo=msgBox.addButton(QMessageBox::No);
msgBox.exec();
// No pressed
if(msgBox.clickedButton()==buttonNo)
return;
// Yes pressed
workingFile="";
ui->editorTextEdit->clear();
}
/*
* Action handler for the File->Open menu button
*/
void MainWindow::openFile() {
if(workingFile!="" || ui->editorTextEdit->document()->toPlainText()!="") {
// Message box warning
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Are you sure you want to dismiss all your changes?");
msgBox.addButton(QMessageBox::Yes);
QAbstractButton* buttonNo=msgBox.addButton(QMessageBox::No);
msgBox.exec();
if(msgBox.clickedButton()==buttonNo)
return;
}
// Open file dialog
QString fileName=QFileDialog::getOpenFileName(this,"Open File","","Text Files (*.txt)");
if(fileName.isEmpty())
return;
// Reads the selected file
FILE* f=fopen(fileName.toStdString().c_str(),"r");
if(f==NULL) {
showWarning("There was an error while trying to open the file");
return;
}
// Fills the text box with the file contents
ui->editorTextEdit->clear();
char* buffer=new char[1048576+1];
while(!feof(f)) {
int size=fread(buffer,sizeof(char),1048576,f);
buffer[size]='\0';
ui->editorTextEdit->insertPlainText(QString(buffer));
}
fclose(f);
delete[] buffer;
workingFile=fileName;
}
/*
* Action handler for the File->Save menu button
*/
void MainWindow::saveFile() {
if(workingFile=="") {
saveAsFile();
return;
}
FILE* f=fopen(workingFile.toStdString().c_str(),"w");
if(f==NULL) {
showWarning("There was an error while saving your file!");
return;
}
const char* buffer=ui->editorTextEdit->toPlainText().toStdString().c_str();
int size=ui->editorTextEdit->toPlainText().length();
fwrite(buffer,sizeof(char),size,f);
fclose(f);
}
/*
* Action handler for the File->Save As menu button
*/
void MainWindow::saveAsFile() {
QFileDialog fileDialog(this,Qt::Window);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.setDefaultSuffix("txt");
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setNameFilter("Text Files (*.txt)");
if(!fileDialog.exec())
return;
QStringList fileNames=fileDialog.selectedFiles();
QString fileName=fileNames.at(0);
if(fileName.isEmpty())
return;
workingFile=fileName;
FILE* f=fopen(workingFile.toStdString().c_str(),"w");
if(f==NULL) {
showWarning("There was an error while saving your file!");
return;
}
const char* buffer=ui->editorTextEdit->toPlainText().toStdString().c_str();
int size=ui->editorTextEdit->toPlainText().length();
fwrite(buffer,sizeof(char),size,f);
fclose(f);
}
/*
* Shows a warning with a certain text
*/
void MainWindow::showWarning(QString warning) {
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText(warning);
msgBox.addButton(QMessageBox::Ok);
msgBox.exec();
}
/*
* Deconstructor
*/
MainWindow::~MainWindow()
{
delete ui;
}
/*
* Handles Alt-F4 and window close events
*/
void MainWindow::closeEvent(QCloseEvent *event) {
QMessageBox msgBox;
msgBox.setWindowTitle("Warning");
msgBox.setText("Are you sure you want to exit Cigma16?");
QAbstractButton* buttonYes=msgBox.addButton(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.exec();
if(msgBox.clickedButton()==buttonYes)
event->accept();
else
event->ignore();
}