-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMachineGUI.cpp
More file actions
65 lines (60 loc) · 1.73 KB
/
MachineGUI.cpp
File metadata and controls
65 lines (60 loc) · 1.73 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
#include "MachineGUI.hpp"
#include <QtGui/QtGui>
MachineGUI::MachineGUI( Transformer* transformer, QWidget *parent ): QMainWindow(parent), m_sink()
{
if(!transformer.bind(&m_sink)){
//Failed to bind
}
m_machine = transformer;
ui.setupUi( this );
connect( ui.rotorsButton, SIGNAL( clicked()), this, SLOT( load_rotor_files() ) );
connect( ui.plugboardButton, SIGNAL( clicked()), this, SLOT( load_plug_file() ) );
connect( ui.encodeButton, SIGNAL( clicked()), this, SLOT( encrypt_text() ) );
}
void MachineGUI::load_rotor_files()
{
QStringList files = QFileDialog::getOpenFileNames(
this,
"Select files to open",
QString::null,
"Rotor files (*.rot)");
for (int i = 0; i < files.size(); i++){
m_machine->load_rotor_file(files.at(i).toAscii().constData());
}
}
void MachineGUI::load_plug_file()
{
QString file = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
"Plugboard files (*.pb)");
m_machine->load_plug_file( file.toAscii().constData() );
}
void MachineGUI::encrypt_text()
{
QString input = ui.inputText->toPlainText();
ui.outputText->clear();
m_machine->reset_rotors();
QString output;
try
{
for( QString::iterator itr = input.begin(); itr != input.end(); itr++ )
{
output.append(m_machine->encrypt((*itr).toAscii()));
}
ui.outputText->setPlainText(output);
}
catch( const CharacterException& e )
{
QErrorMessage error;
error.showMessage(e.what());
error.exec();
}
}
int encode(EnigmaLetter value) {
if (!m_machine->encode(value)) {
// encode failed
}
return m_sink.value();
}