-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
84 lines (72 loc) · 2.53 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->textEditOutput->setReadOnly(true);
ui->text2morseBtn->setChecked(true);
initializeMorseCodeTable();
//signals and slots connection
connect(ui->textEditInput, &QTextEdit::textChanged, this, &MainWindow::reactToInputTextValueChanged);
connect(this, &MainWindow::outputTextValueChanged, ui->textEditOutput, &QTextEdit::setPlainText);
connect(ui->clearPushButton, &QPushButton::clicked, this, &MainWindow::reactToClearBtn);
connect(ui->text2morseBtn, &QRadioButton::toggled, this, &MainWindow::reactToToggledText2MorsBtn);
connect(ui->morse2textBtn, &QRadioButton::toggled, this, &MainWindow::reactToToggledMors2TextBtn);
}
void MainWindow::initializeMorseCodeTable()
{
//set table configuration
int maxRow = 8, maxCol = 10;
ui->morseCodeTable->setRowCount(maxRow);
ui->morseCodeTable->setColumnCount(maxCol);
ui->morseCodeTable->horizontalHeader()->hide();
ui->morseCodeTable->verticalHeader()->hide();
ui->morseCodeTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
//fill the table with corresponding values
int row = 0, col = 0;
QTableWidgetItem *item;
const QMap<QString, QString>& morseCodeMap = morseCodeAlg.getMorseCodeQMap();
for(QMap<QString, QString>::const_iterator it = morseCodeMap.begin(); it != morseCodeMap.end(); it++)
{
item = new QTableWidgetItem(it.key());
ui->morseCodeTable->setItem(row, col, item);
item = new QTableWidgetItem(it.value());
ui->morseCodeTable->setItem(row, col + 1, item);
row = (row + 1) % maxRow;
col = (row == 0) ? col + 2 : col;
}
}
void MainWindow::reactToInputTextValueChanged()
{
QString resultStr = "";
strToOperate = ui->textEditInput->toPlainText();
if(ui->text2morseBtn->isChecked())
morseCodeAlg.morseEncodeText(strToOperate, resultStr);
else
morseCodeAlg.morseDecodeText(strToOperate, resultStr);
emit outputTextValueChanged(resultStr);
}
void MainWindow::clear()
{
ui->textEditInput->clear();
ui->textEditOutput->clear();
strToOperate = "";
}
void MainWindow::reactToClearBtn()
{
clear();
}
void MainWindow::reactToToggledText2MorsBtn()
{
clear();
}
void MainWindow::reactToToggledMors2TextBtn()
{
clear();
}
MainWindow::~MainWindow()
{
delete ui;
}