-
Notifications
You must be signed in to change notification settings - Fork 24
/
MainWindow.cpp
391 lines (335 loc) · 11.6 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
#include <QSerialPortInfo>
#include <QFileDialog>
#include <QSettings>
#include <QMessageBox>
#include <QCloseEvent>
#include <QSortFilterProxyModel>
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "CanTree/CanTreeModel.h"
#include "CanTree/HeaderTreeNode.h"
#include "CanAdapter/CanAdapter.h"
#include "CanAdapter/CanAdapterFactory.h"
#include "WidgetUtils/MenuOpenKeeper.h"
#include "Commanders/CommanderDialog.h"
#include <QDirIterator>
#include <QDebug>
#include "Trace/TraceWindow.h"
#include "Simulator/TritiumSimulatorWindow.h"
#include "Cansole/Cansole.h"
#include "Cansole/CansoleIdDialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MenuOpenKeeper * menuOpenKepper = new MenuOpenKeeper(this);
ui->menuEnable_Columns->installEventFilter(menuOpenKepper);
m_model = new CanTreeModel();
if(useProxyModel)
{
m_proxyModel = new QSortFilterProxyModel(this);
m_proxyModel->setSourceModel(m_model);
m_proxyModel->setSortRole(Qt::UserRole);
m_proxyModel->setDynamicSortFilter(false);
ui->treeView->setModel(m_proxyModel);
}else{
ui->treeView->setModel(m_model);
}
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenu(const QPoint &)));
ui->treeView->setColumnWidth(0,150);
ui->treeView->setColumnWidth(2,50);
ui->treeView->setColumnWidth(3,70);
ui->treeView->setColumnWidth(4,70);
ui->treeView->setColumnWidth(5,200);
ui->treeView->setColumnWidth(6,220);
ui->treeView->header()->setSectionsClickable(true);
connect(ui->treeView->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
QString adapterName = QSettings().value("main/CanAdapter").toString();
ui->canAdapterComboBox->addItems(CanAdapterFactory::getAdapterNames());
ui->canAdapterComboBox->setCurrentText(adapterName);
QSettings settings;
QString path = settings.value("main/lastTreeFile").toString();
if(path != "")
loadTree(path);
m_guiCanHandle = m_canHub.getNewHandle();
connect(ui->transmitWidget, SIGNAL(onTransmit(can_message_t)), this, SLOT(canTransmit(can_message_t)));
connect(m_guiCanHandle, SIGNAL(received(can_message_t)), m_model, SLOT(inputMessage(can_message_t)));
populateCommanders();
}
MainWindow::~MainWindow()
{
//delete m_commanderDialog;
delete m_canAdapter;
delete ui;
delete m_model;
}
void MainWindow::onCustomContextMenu(const QPoint &point)
{
QModelIndexList selectedIndexes = ui->treeView->selectionModel()->selectedIndexes();
m_contextMenuContext.clickedIndex = ui->treeView->indexAt(point);
QMenu contextMenu(this);
contextMenu.addAction(ui->actionAdd_Group);
if(!selectedIndexes.empty())
contextMenu.addAction(ui->actionDeleteTreeNodes);
contextMenu.exec(ui->treeView->mapToGlobal(point));
}
void MainWindow::on_actionAdd_Group_triggered()
{
QModelIndex index;
if(m_proxyModel)
index = m_proxyModel->mapToSource(m_contextMenuContext.clickedIndex);
else
index = m_contextMenuContext.clickedIndex;
m_model->addNode(index, new HeaderTreeNode("New Group"));
}
void MainWindow::on_actionDeleteTreeNodes_triggered()
{
const QModelIndexList &originalList = ui->treeView->selectionModel()->selectedIndexes();
QModelIndexList list;
if(m_proxyModel){
foreach (auto index, originalList)
list.append(m_proxyModel->mapToSource(index));
}
else
list = originalList;
m_model->deleteBranches(list);
}
void MainWindow::headerSectionClicked(int index)
{
ui->treeView->header()->setSortIndicatorShown(true);
if(m_lastSortIndex != index)
{
m_nextSortMode = sortAscending;
m_lastSortIndex = index;
}
switch(m_nextSortMode)
{
case sortNone:
ui->treeView->sortByColumn(-1);
m_nextSortMode = sortAscending;
break;
case sortAscending:
ui->treeView->sortByColumn(index, Qt::AscendingOrder);
m_nextSortMode = sortDescending;
break;
case sortDescending:
ui->treeView->sortByColumn(index, Qt::DescendingOrder);
m_nextSortMode = sortNone;
break;
}
}
void MainWindow::canTransmit(can_message_t cmsg)
{
m_guiCanHandle->transmit(cmsg);
}
bool MainWindow::saveTreeInteractive()
{
QString path = QSettings().value("main/lastTreeFile").toString();
QString filename = QFileDialog::getSaveFileName(this,
tr("Save Tree"), path,
tr("Xml files (*.xml)"));
if(filename == "")
return false;
QFile file(filename);
if(file.open(QIODevice::WriteOnly))
{
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
m_model->writeTreeToXml(xmlWriter);
file.close();
m_model->isUserModified = false;
QSettings().setValue("main/lastTreeFile", filename);
return true;
}else{
QMessageBox::warning(0, tr("CAN Monitor"),
tr("The file\"") + filename + tr("\"could not be opened.\n") +
tr("The error message was: ") + file.errorString(),
QMessageBox::Ok);
return false;
}
}
void MainWindow::on_actionSave_Tree_triggered()
{
saveTreeInteractive();
}
void MainWindow::loadTree(QString &filename)
{
QFile file(filename);
if(file.open(QIODevice::ReadOnly)){
QXmlStreamReader xmlReader(&file);
m_model->readTreeFromXml(xmlReader);
file.close();
QSettings().setValue("main/lastTreeFile", filename);
}else{
QMessageBox::warning(0, tr("CAN Monitor"),
tr("The file\"") + filename + tr("\"could not be opened.\n") +
tr("The error message was: ") + file.errorString(),
QMessageBox::Ok);
}
}
void MainWindow::on_actionLoad_Tree_triggered()
{
QString path = QSettings().value("main/lastTreeFile").toString();
QString filename = QFileDialog::getOpenFileName(this,
tr("Load Tree"), path,
tr("Xml files (*.xml)"));
if(filename == "")
return;
loadTree(filename);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
foreach (auto commander, m_openCommanders) {
commander->closeEvent(event);
if(!event->isAccepted())
return;
}
if(!m_model->isUserModified){
event->accept();
return;
}
QMessageBox msgBox;
msgBox.setText("The tree has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
if (saveTreeInteractive())
event->accept();
else
event->ignore();
break;
case QMessageBox::Discard:
event->accept();
break;
case QMessageBox::Cancel:
event->ignore();
break;
}
}
void MainWindow::changeCanAdpapter(CanAdapter * ca)
{
QBoxLayout * layout = ui->horizontalLayout;
int layoutIndex = 2;
if(m_canAdapter)
delete m_canAdapter;
if(m_canAdpterControlWidget)
{
layout->removeWidget(m_canAdpterControlWidget);
delete m_canAdpterControlWidget;
}
m_canAdapter = ca;
auto w = m_canAdapter->getControlWidget(ui->centralWidget);
m_canAdpterControlWidget = w;
if(w)
layout->insertWidget(layoutIndex,w);
}
void MainWindow::on_canAdapterComboBox_currentTextChanged(const QString &adapterName)
{
CanAdapter * ca = CanAdapterFactory::createAdapter(adapterName, m_canHub);
if(ca) {
changeCanAdpapter(ca);
QSettings().setValue("main/CanAdapter", adapterName);
}
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::information(0, tr("CAN Monitor 3000"),
tr("Version 0.5 alpha\n\n") +
tr("A versatile and simple CAN bus diagnostic tool by Peter Fuhrmann"),
QMessageBox::Ok);
}
void MainWindow::on_actionSort_Items_Live_triggered(bool checked)
{
if(m_proxyModel)
m_proxyModel->setDynamicSortFilter(checked);
}
void MainWindow::on_actionEnableID_triggered(bool checked)
{ ui->treeView->setColumnHidden(1,!checked); }
void MainWindow::on_actionEnableDLC_triggered(bool checked)
{ ui->treeView->setColumnHidden(2,!checked); }
void MainWindow::on_actionEnableCount_triggered(bool checked)
{ ui->treeView->setColumnHidden(3,!checked); }
void MainWindow::on_actionEnablePeriod_triggered(bool checked)
{ ui->treeView->setColumnHidden(4,!checked); }
void MainWindow::on_actionEnableRaw_Data_triggered(bool checked)
{ ui->treeView->setColumnHidden(5,!checked); }
void MainWindow::on_actionEnableDecoded_Data_triggered(bool checked)
{ ui->treeView->setColumnHidden(6,!checked); }
void MainWindow::on_actionEnableFormat_triggered(bool checked)
{ ui->treeView->setColumnHidden(7,!checked); }
void MainWindow::populateCommanders()
{
ui->menuCommander->clear();
QString dir = QSettings().value("commanders/path").toString();
if(dir != "")
{
QDirIterator it(dir, QStringList() << "*.xml", QDir::Files);
while (it.hasNext()){
it.next();
QAction *action = new QAction(it.fileInfo().baseName(), this);
ui->menuCommander->addAction(action);
connect(action, SIGNAL(triggered(bool)), this, SLOT(actionCommanderTriggered()));
}
}
ui->menuCommander->addSeparator();
ui->menuCommander->addAction(ui->actionSetCommanderDirectory);
ui->menuCommander->addAction(ui->actionNewCommander);
}
void MainWindow::commanderWindowClosed(QObject* o)
{
auto closedCommander = static_cast<CommanderDialog*>(o);
m_openCommanders.removeOne(closedCommander);
populateCommanders();
}
void MainWindow::openCommander(QString name)
{
auto dlg = new CommanderDialog(this, &m_canHub, name);
connect(dlg, SIGNAL(destroyed(QObject*)), this, SLOT(commanderWindowClosed(QObject*)));
m_openCommanders.append(dlg);
dlg->show();
}
void MainWindow::actionCommanderTriggered()
{
openCommander(static_cast<QAction*>(sender())->text());
}
void MainWindow::on_actionSetCommanderDirectory_triggered()
{
QString dir = QSettings().value("commanders/path").toString();
QString newDir = QFileDialog::getExistingDirectory (this, "Select Commanders Directory", dir, 0);
if(newDir != "")
{
QSettings().setValue("commanders/path", newDir);
populateCommanders();
}
}
void MainWindow::on_actionNewCommander_triggered()
{
openCommander("");
}
void MainWindow::on_actionTrace_triggered()
{
auto w = new TraceWindow(this, m_canHub);
w->show();
}
void MainWindow::on_actionTritium_Simulator_triggered()
{
auto w = new TritiumSimulatorWindow(this, m_canHub);
w->show();
}
void MainWindow::on_actionOpen_Cansole_triggered()
{
auto dialog = new CansoleIdDialog();
bool ok;
dialog->cansoleId = QSettings().value("cansole/lastId").toString().toInt(&ok, 16);
if(!ok) dialog->cansoleId = 0x780;
int res = dialog->exec();
if(res == QDialog::Accepted){
QSettings().setValue("cansole/lastId", QString().number(dialog->cansoleId, 16));
new Cansole(this, m_canHub, dialog->cansoleId);
}
}