forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalviewerwindow.cpp
401 lines (336 loc) · 12.6 KB
/
signalviewerwindow.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
#include "signalviewerwindow.h"
#include "ui_signalviewerwindow.h"
#include "helpwindow.h"
#include "mainwindow.h"
#include "utility.h"
#include <QDebug>
#define MSG_COL 1
#define VALUE_COL 2
SignalViewerWindow::SignalViewerWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::SignalViewerWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
modelFrames = frames;
QStringList headers;
headers << "Node" << "Signal" << "Value";
ui->tableViewer->setHorizontalHeaderLabels(headers);
ui->tableViewer->setColumnWidth(0, 100);
ui->tableViewer->setColumnWidth(1, 150);
QSettings settings;
QFont sysFont;
int fontSize = settings.value("Main/FontSize", 9).toUInt();
if(settings.value("Main/FontFixedWidth", false).toBool())
sysFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); //get default fixed width font
else
sysFont = QFont(); //get default font
sysFont.setPointSize(fontSize);
ui->tableViewer->setFont(sysFont);
QHeaderView *HorzHdr = ui->tableViewer->horizontalHeader();
HorzHdr->setStretchLastSection(true); //causes the data column to automatically fill the tableview
HorzHdr->setFont(QFont());
QHeaderView *verticalHeader = ui->tableViewer->verticalHeader();
verticalHeader->setFont(QFont());
dbcHandler = DBCHandler::getReference();
currentlySelectedMsg = nullptr;
connect(ui->cbNodes, SIGNAL(currentIndexChanged(int)), this, SLOT(loadMessages(int)));
connect(ui->cbMessages, SIGNAL(currentIndexChanged(int)), this, SLOT(loadSignals(int)));
connect(ui->btnAdd, SIGNAL(clicked(bool)), this, SLOT(addSignal()));
connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int)));
connect(ui->btnRemove, SIGNAL(clicked(bool)), this, SLOT(removeSelectedSignal()));
connect(ui->btnSave, SIGNAL(clicked(bool)), this, SLOT(saveSignalsFile()));
connect(ui->btnLoad, SIGNAL(clicked(bool)), this, SLOT(loadSignalsFile()));
connect(ui->btnAppend, SIGNAL(clicked(bool)), this, SLOT(appendSignalsFile()));
connect(ui->btnClear, SIGNAL(clicked(bool)), this, SLOT(clearSignalsTable()));
loadNodes();
}
SignalViewerWindow::~SignalViewerWindow()
{
delete ui;
}
void SignalViewerWindow::updatedFrames(int numFrames)
{
CANFrame thisFrame;
if (numFrames == -1) //all frames deleted. Don't care
{
}
else if (numFrames == -2) //all new set of frames. Reset
{
for (int i = 0; i < modelFrames->count(); i++)
{
thisFrame = modelFrames->at(i);
processFrame(thisFrame);
}
}
else //just got some new frames. See if they are relevant.
{
if (numFrames > modelFrames->count()) return;
for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++)
{
thisFrame = modelFrames->at(i);
processFrame(thisFrame);
}
}
}
void SignalViewerWindow::processFrame(CANFrame &frame)
{
QString sigString;
DBC_SIGNAL *sig;
for (int i = 0; i < signalList.count(); i++)
{
sig = signalList.at(i);
if (!sig) return;
if (sig->parentMessage->ID == frame.frameId())
{
if (sig->isSignalInMessage(frame)) //filter out multiplexed signals that aren't in this message.
{
if (sig->processAsText(frame, sigString, false)) //if true we could interpret the signal so update it in the list
{
QTableWidgetItem *item = ui->tableViewer->item(i, VALUE_COL);
if (!item)
{
item = new QTableWidgetItem(sigString);
ui->tableViewer->setItem(i, VALUE_COL, item);
}
else item->setText(sigString);
}
}
}
}
}
void SignalViewerWindow::removeSelectedSignal()
{
int selRow = ui->tableViewer->currentRow();
if (selRow < 0) return; //no selected row
signalList.removeAt(selRow);
ui->tableViewer->removeRow(selRow);
}
void SignalViewerWindow::loadNodes()
{
int numFiles;
ui->cbNodes->clear();
if (dbcHandler == nullptr) return;
if ((numFiles = dbcHandler->getFileCount()) == 0) return;
qDebug() << numFiles;
for (int f = 0; f < numFiles; f++)
{
DBCFile* thisFile = dbcHandler->getFileByIdx(f);
qDebug() << thisFile->messageHandler->getCount();
QList<QString> names;
for (int x = 0; x < thisFile->dbc_nodes.count(); x++)
{
bool messagesInNode = false;
for (int m = 0; m < thisFile->messageHandler->getCount(); m++)
{
if(thisFile->messageHandler->findMsgByIdx(m)->sender->name == thisFile->dbc_nodes[x].name)
{
messagesInNode = true;
break;
}
}
if(messagesInNode)
{
QString fullyQualifiedNodeName = thisFile->getFilenameNoExt() + Utility::fullyQualifiedNameSeperator + thisFile->dbc_nodes[x].name;
names.append(fullyQualifiedNodeName);
}
}
if(names.count() > 0)
{
names.sort();
ui->cbNodes->addItem("----" + thisFile->getFilename());
Utility::SetComboBoxItemEnabled(ui->cbNodes, ui->cbNodes->count() -1, false);
for(int i=0; i<names.count(); i++)
ui->cbNodes->addItem(names[i]);
}
}
}
void SignalViewerWindow::loadMessages(int idx)
{
int numFiles = 0;
ui->cbMessages->clear();
if (dbcHandler == nullptr) return;
if ((numFiles = dbcHandler->getFileCount()) == 0) return;
qDebug() << numFiles;
QString displayedNodeName = ui->cbNodes->itemText(idx);
for (int f = 0; f < numFiles; f++)
{
qDebug() << dbcHandler->getFileByIdx(f)->messageHandler->getCount();
for (int x = 0; x < dbcHandler->getFileByIdx(f)->messageHandler->getCount(); x++)
{
QString fullyQualifiedNodeName = dbcHandler->getFileByIdx(f)->getFilenameNoExt() + Utility::fullyQualifiedNameSeperator + dbcHandler->getFileByIdx(f)->messageHandler->findMsgByIdx(x)->sender->name;
if(fullyQualifiedNodeName == displayedNodeName)
ui->cbMessages->addItem(dbcHandler->getFileByIdx(f)->messageHandler->findMsgByIdx(x)->name);
}
}
}
void SignalViewerWindow::loadSignals(int idx)
{
Q_UNUSED(idx);
ui->cbSignals->clear();
QString fullyQualifiedNodeName = ui->cbNodes->itemText(ui->cbNodes->currentIndex());
QString msgName = ui->cbMessages->itemText(ui->cbMessages->currentIndex());
DBC_MESSAGE *msg = dbcHandler->findMessage(msgName, fullyQualifiedNodeName);
if (msg == nullptr) return;
for (int x = 0; x < msg->sigHandler->getCount(); x++)
{
ui->cbSignals->addItem(msg->sigHandler->findSignalByIdx(x)->name);
}
}
void SignalViewerWindow::addSignal()
{
DBC_MESSAGE *msg = nullptr;
QString fullyQualifiedNodeName = ui->cbNodes->itemText(ui->cbNodes->currentIndex());
QString msgName = ui->cbMessages->itemText(ui->cbMessages->currentIndex());
msg = dbcHandler->findMessage(msgName, fullyQualifiedNodeName);
if (!msg) return;
DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(ui->cbSignals->currentText());
if (!sig) return;
addSignal(sig);
}
void SignalViewerWindow::addSignal(DBC_SIGNAL *sig)
{
signalList.append(sig);
int rowIdx = ui->tableViewer->rowCount();
ui->tableViewer->insertRow(rowIdx);
QTableWidgetItem *nodeitem = new QTableWidgetItem(sig->parentMessage->sender->name);
ui->tableViewer->setItem(rowIdx, 0, nodeitem);
QTableWidgetItem *msgitem = new QTableWidgetItem(sig->name);
ui->tableViewer->setItem(rowIdx, 1, msgitem);
}
void SignalViewerWindow::saveSignalsFile()
{
saveDefinitions();
}
void SignalViewerWindow::loadSignalsFile()
{
loadDefinitions(false);
}
void SignalViewerWindow::appendSignalsFile()
{
loadDefinitions(true);
}
void SignalViewerWindow::clearSignalsTable()
{
clearSignalsTable(true);
}
void SignalViewerWindow::clearSignalsTable(bool askForConfirmation)
{
if(askForConfirmation)
{
QMessageBox::StandardButton confirmDialog;
confirmDialog = QMessageBox::question(this, "Danger Will Robinson", "Are you sure you want to clear all of your signals?",
QMessageBox::Yes|QMessageBox::No);
if (confirmDialog == QMessageBox::No)
{
return;
}
}
signalList.clear();
ui->tableViewer->setRowCount(0);
}
void SignalViewerWindow::saveDefinitions()
{
QString filename;
QFileDialog dialog(this);
QSettings settings;
QStringList filters;
filters.append(QString(tr("SignalViewer definition (*.sdf)")));
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setDirectory(settings.value("SignalViewer/LoadSaveDirectory", dialog.directory().path()).toString());
if (dialog.exec() == QDialog::Accepted)
{
filename = dialog.selectedFiles()[0];
settings.setValue("SignalViewer/LoadSaveDirectory", dialog.directory().path());
if (!filename.contains('.')) filename += ".sdf";
QFile *outFile = new QFile(filename);
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
return;
DBC_SIGNAL *sig;
for (int i = 0; i < signalList.count(); i++)
{
sig = signalList.at(i);
outFile->write("SV1");
outFile->putChar(',');
outFile->write(QString::number(sig->parentMessage->ID, 16).toUtf8());
outFile->putChar(',');
outFile->write(sig->parentMessage->name.toUtf8());
outFile->putChar(',');
outFile->write(sig->name.toUtf8());
outFile->write("\n");
}
outFile->close();
}
}
void SignalViewerWindow::loadDefinitions(bool append)
{
QString filename;
QFileDialog dialog;
QSettings settings;
QStringList filters;
filters.append(QString(tr("SignalViewer definition (*.sdf)")));
QList<DBC_SIGNAL *> loadedSignals;
if (dbcHandler == nullptr) return;
if (dbcHandler->getFileCount() == 0) dbcHandler->createBlankFile();
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
dialog.setDirectory(settings.value("SignalViewer/LoadSaveDirectory", dialog.directory().path()).toString());
if (dialog.exec() == QDialog::Accepted)
{
filename = dialog.selectedFiles()[0];
settings.setValue("SignalViewer/LoadSaveDirectory", dialog.directory().path());
QFile *inFile = new QFile(filename);
QByteArray line;
if (!inFile->open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!inFile->atEnd()) {
line = inFile->readLine().simplified();
if (line.length() > 2)
{
QList<QByteArray> tokens = line.split(',');
DBC_SIGNAL *sig;
if (tokens[0] == "SV1") //signal viewer save format v1
{
// = tokens[1].toUInt(nullptr, 16);
int msgId = tokens[1].toUInt(nullptr, 16);
QString msgName = QString(tokens[2]);
QString sigName = QString(tokens[3]);
DBC_MESSAGE *msg;;
if( (msg = dbcHandler->findMessage(msgId)) )
{
sig = msg->sigHandler->findSignalByName(sigName);
if(sig)
loadedSignals.append(sig);
}
else if ( (msg = dbcHandler->findMessage(msgName)) )
{
//this is not a very safe way to match since messages names can be duplicated
sig = msg->sigHandler->findSignalByName(sigName);
if(sig)
loadedSignals.append(sig);
}
else
{
qDebug() << "Couldn't find the message by ID or name! " << msgName << " " << sigName;
}
}
}
}
inFile->close();
if(loadedSignals.count() > 0)
{
if(append == false)
{
clearSignalsTable(false);
}
for (int i=0; i<loadedSignals.count(); i++)
{
addSignal(loadedSignals[i]);
}
}
}
}