-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
221 lines (178 loc) · 5.85 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
documentIsDirty = false;
config = new Configuration(this);
portValid = false;
loadDocument(settings.value("Last Configuration").toString());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::closeEvent(QCloseEvent *e)
{
if(portValid) port->requestToStop();
if(!checkDocument()) e->ignore();
}
void MainWindow::on_actionNew_triggered()
{
if(checkDocument()){
ui->configurationText->clear();
documentFilePath = "";
ui->statusBar->showMessage(documentFilePath);
documentIsDirty = false;
}
}
void MainWindow::on_actionExit_triggered()
{
this->close();
}
void MainWindow::on_actionOpen_triggered()
{
if(!checkDocument()) return;
QString filePath = QFileDialog::getOpenFileName(this,
tr("Open File"), documentFilePath, tr("SerialChart Configuration (*.scc);;All Files (*.*)"));
if(!filePath.isEmpty()) loadDocument(filePath);
}
void MainWindow::on_actionSave_triggered()
{
if(documentFilePath.isEmpty())
on_actionSaveAs_triggered();
else
saveDocument(documentFilePath);
}
void MainWindow::on_actionSaveAs_triggered()
{
QString filePath = QFileDialog::getSaveFileName(this, tr("Save File"),
documentFilePath,
tr("SerialChart Configuration (*.scc);;All Files (*.*)"));
if(!filePath.isEmpty()) saveDocument(filePath);
}
bool MainWindow::checkDocument(){
if(documentIsDirty)
if(QMessageBox::No == QMessageBox::warning(0,"","Current configuration was changed but not saved.\nAre you sure you want to proceed ?",QMessageBox::Yes,QMessageBox::No|QMessageBox::Default))
return false;
return true;
}
bool MainWindow::saveDocument(const QString& filePath)
{
bool success = false;
QFile file(filePath);
if(file.open(QIODevice::WriteOnly)){
QTextStream textStream(&file);
textStream<<ui->configurationText->toPlainText();
textStream.flush();
documentIsDirty = false;
updateDocumentFilePath(filePath);
success = true;
}
if(!success)
QMessageBox::critical(0,"","Could not save file: " + filePath);
else
ui->statusBar->showMessage("File Saved",2000);
return success;
}
bool MainWindow::loadDocument(const QString& filePath)
{
bool success = true;
QFile file(filePath);
if(file.open(QIODevice::ReadOnly)){
QTextStream textStream(&file);
ui->configurationText->setPlainText(textStream.readAll());
updateDocumentFilePath(filePath);
success = true;
}
if(!success) QMessageBox::critical(0,"","Could not load file: " + filePath);
return success;
}
void MainWindow::updateDocumentFilePath(const QString& filePath){
documentFilePath = filePath;
settings.setValue("Last Configuration",filePath);
this->setWindowTitle("SerialChart - " + QFileInfo(filePath).fileName() );
documentIsDirty = false;
};
void MainWindow::on_configurationText_textChanged()
{
documentIsDirty = true;
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(0,"","Developed by Starlino\nwww.Starlino.com");
}
void MainWindow::on_actionRun_triggered()
{
config->parse( ui->configurationText->toPlainText());
ui->dataText->setMaximumBlockCount(MAX(1,config->get("_setup_","width").toInt()));
ui->chart->init(config);
ui->actionRun->setEnabled(false);
ui->actionStop->setEnabled(true);
ui->sendButton->setEnabled(true);
ui->dataText->clear();
port = createPort(config);
decoder = createDecoder(this,config);
display = createDisplay(this,config);
portValid = true;
//port signals
connect(port,SIGNAL(newData(const QByteArray&)),decoder,SLOT(newData(const QByteArray&)));
connect(port,SIGNAL(packetSeparator()),decoder,SLOT(packetSeparator()));
connect(port,SIGNAL(stopped()),this,SLOT(portStopped()));
connect(port,SIGNAL(message(const QString&,const QString&)),this,SLOT(message(const QString&,const QString&)));
//decoder signals
connect(decoder,SIGNAL(newPacket(DecoderBase*)),ui->chart,SLOT(newPacket(DecoderBase*)));
connect(decoder,SIGNAL(newPacket(DecoderBase*)),display,SLOT(newPacket(DecoderBase*)));
//display signals
connect(display,SIGNAL(newDisplay(const QString&)),ui->dataText,SLOT(appendPlainText(const QString&)));
port->start();
}
void MainWindow::on_actionStop_triggered()
{
ui->actionStop->setEnabled(false);
ui->sendButton->setEnabled(false);
port->requestToStop();
}
void MainWindow::portStopped(){
ui->actionStop->setEnabled(false);
ui->sendButton->setEnabled(false);
ui->actionRun->setEnabled(true);
port->deleteLater();
portValid = false;
delete decoder;
delete display;
}
void MainWindow::on_actionToolbar_toggled(bool b)
{
ui->mainToolBar->setVisible(b);
}
void MainWindow::on_actionChart_toggled(bool b)
{
ui->dockWidgetChart->setVisible(b);
}
void MainWindow::on_actionConfiguration_toggled(bool b)
{
ui->dockWidgetConfiguration->setVisible(b);
}
void MainWindow::on_sendButton_clicked()
{
if(portValid) port->send(ui->sendText->text());
}
void MainWindow::message(const QString& text,const QString& type)
{
if("critical"==type) QMessageBox::critical(0,"",text);
else QMessageBox::information(0,"",text);
}