forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbccomparatorwindow.cpp
400 lines (345 loc) · 13.8 KB
/
dbccomparatorwindow.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
#include "dbccomparatorwindow.h"
#include "ui_dbccomparatorwindow.h"
#include "helpwindow.h"
#include <QProgressDialog>
#include <QSettings>
#include <qevent.h>
DBCComparatorWindow::DBCComparatorWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::DBCComparatorWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
connect(ui->btnDBCFile1, SIGNAL(clicked(bool)), this, SLOT(loadFirstFile()));
connect(ui->btnDBCFile2, SIGNAL(clicked(bool)), this, SLOT(loadSecondFile()));
connect(ui->btnSaveDetails, SIGNAL(clicked(bool)), this, SLOT(saveDetails()));
ui->lblFirstFile->setText("");
ui->lblSecondFile->setText("");
firstDBC = nullptr;
secondDBC = nullptr;
installEventFilter(this);
}
DBCComparatorWindow::~DBCComparatorWindow()
{
removeEventFilter(this);
delete ui;
}
void DBCComparatorWindow::showEvent(QShowEvent *)
{
readSettings();
}
void DBCComparatorWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event)
writeSettings();
}
bool DBCComparatorWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key())
{
case Qt::Key_F1:
HelpWindow::getRef()->showHelp("filecomparison.md");
break;
}
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return false;
}
void DBCComparatorWindow::readSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
resize(settings.value("DBCComparator/WindowSize", QSize(720, 631)).toSize());
move(Utility::constrainedWindowPos(settings.value("DBCComparator/WindowPos", QPoint(50, 50)).toPoint()));
}
}
void DBCComparatorWindow::writeSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
settings.setValue("DBCComparator/WindowSize", size());
settings.setValue("DBCComparator/WindowPos", pos());
}
}
QString DBCComparatorWindow::loadDBC(DBCFile **file)
{
QString filename;
QFileDialog dialog;
QSettings settings;
QStringList filters;
filters.append(QString(tr("DBC Files (*.dbc *.DBC)")));
dialog.setDirectory(settings.value("FileIO/LoadSaveDirectory", dialog.directory().path()).toString());
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
if (dialog.exec() == QDialog::Accepted)
{
filename = dialog.selectedFiles()[0];
QProgressDialog progress(qApp->activeWindow());
progress.setWindowModality(Qt::WindowModal);
progress.setLabelText("Loading file...");
progress.setCancelButton(nullptr);
progress.setRange(0,0);
progress.setMinimumDuration(0);
progress.show();
qApp->processEvents();
if (dialog.selectedNameFilter() == filters[0])
{
if (file[0]) delete file[0];
file[0] = new DBCFile;
file[0]->loadFile(filename);
qDebug() << "Loaded the DBC file into first slot";
}
progress.cancel();
return filename;
}
return QString();
}
void DBCComparatorWindow::loadFirstFile()
{
QString filename;
filename = loadDBC(&firstDBC);
ui->lblFirstFile->setText(filename);
if (firstDBC && secondDBC) calculateDetails();
}
void DBCComparatorWindow::loadSecondFile()
{
QString filename;
filename = loadDBC(&secondDBC);
ui->lblSecondFile->setText(filename);
if (firstDBC && secondDBC) calculateDetails();
}
void DBCComparatorWindow::calculateDetails()
{
QProgressDialog progress(this);
progress.setWindowModality(Qt::WindowModal);
progress.setLabelText("Calculating differences");
progress.setCancelButton(nullptr);
progress.setRange(0,0);
progress.setMinimumDuration(0);
progress.show();
qApp->processEvents();
firstDBC->sort();
secondDBC->sort();
ui->treeDetails->clear();
//Find nodes missing in one of the DBC files
QTreeWidgetItem *nodeDiffRoot = new QTreeWidgetItem();
nodeDiffRoot->setText(0, "Node Differences");
QTreeWidgetItem *nodesMissingDBCFirst = new QTreeWidgetItem();
nodesMissingDBCFirst->setText(0, "Nodes not found in first DBC");
QTreeWidgetItem *nodesMissingDBCSecond = new QTreeWidgetItem();
nodesMissingDBCSecond->setText(0, "Nodes not found in second DBC");
nodeDiffRoot->addChild(nodesMissingDBCFirst);
nodeDiffRoot->addChild(nodesMissingDBCSecond);
ui->treeDetails->addTopLevelItem(nodeDiffRoot);
for (int i = 0; i < firstDBC->dbc_nodes.count(); i++)
{
QString nodeName = firstDBC->dbc_nodes[i].name;
DBC_NODE *node = secondDBC->findNodeByName(nodeName);
if (!node)
{
QTreeWidgetItem *missingNodeItem = new QTreeWidgetItem();
missingNodeItem->setText(0, nodeName);
nodesMissingDBCSecond->addChild(missingNodeItem);
}
}
for (int i = 0; i < secondDBC->dbc_nodes.count(); i++)
{
QString nodeName = secondDBC->dbc_nodes[i].name;
DBC_NODE *node = firstDBC->findNodeByName(nodeName);
if (!node)
{
QTreeWidgetItem *missingNodeItem = new QTreeWidgetItem();
missingNodeItem->setText(0, nodeName);
nodesMissingDBCFirst->addChild(missingNodeItem);
}
}
//Find messages that are missing in one of the DBC files
//If a message exists in both then check for missing signals
QTreeWidgetItem *msgDiffRoot = new QTreeWidgetItem();
msgDiffRoot->setText(0, "Message Differences");
QTreeWidgetItem *msgMissingDBCFirst = new QTreeWidgetItem();
msgMissingDBCFirst->setText(0, "Messages not found in first DBC");
QTreeWidgetItem *msgMissingDBCSecond = new QTreeWidgetItem();
msgMissingDBCSecond->setText(0, "Messages not found in second DBC");
QTreeWidgetItem *msgSignalsDiff = new QTreeWidgetItem();
msgSignalsDiff->setText(0, "Messages with missing signals");
QTreeWidgetItem *sigDiffOne = new QTreeWidgetItem();
sigDiffOne->setText(0, "Missing from first DBC");
QTreeWidgetItem *sigDiffTwo = new QTreeWidgetItem();
sigDiffTwo->setText(0, "Missing from second DBC");
QTreeWidgetItem *sigModifiedRoot = new QTreeWidgetItem();
sigModifiedRoot->setText(0, "Modified Signals");
msgSignalsDiff->addChild(sigDiffOne);
msgSignalsDiff->addChild(sigDiffTwo);
msgDiffRoot->addChild(msgMissingDBCFirst);
msgDiffRoot->addChild(msgMissingDBCSecond);
msgDiffRoot->addChild(msgSignalsDiff);
msgDiffRoot->addChild(sigModifiedRoot);
ui->treeDetails->addTopLevelItem(msgDiffRoot);
QTreeWidgetItem *msgItem;
QTreeWidgetItem *sigTemp;
for (int i = 0; i < firstDBC->messageHandler->getCount(); i++)
{
DBC_MESSAGE *thisMsg = firstDBC->messageHandler->findMsgByIdx(i);
QString msgName = thisMsg->name;
DBC_MESSAGE *otherMsg = secondDBC->messageHandler->findMsgByName(msgName);
if (!otherMsg)
{
QTreeWidgetItem *missingMsgItem = new QTreeWidgetItem();
missingMsgItem->setText(0, msgName+ " (" + Utility::formatCANID(thisMsg->ID) + ")");
msgMissingDBCSecond->addChild(missingMsgItem);
}
else //both have Msg. Check sigs for missing
{
bool thisMsgHasMissing = false;
bool thisMsgHasMods = false;
for (int i = 0; i < thisMsg->sigHandler->getCount(); i++)
{
DBC_SIGNAL *thisSig = thisMsg->sigHandler->findSignalByIdx(i);
QString sigName = thisSig->name;
DBC_SIGNAL *otherSig = otherMsg->sigHandler->findSignalByName(sigName);
if (!otherSig)
{
QTreeWidgetItem *missingSigItem = new QTreeWidgetItem();
missingSigItem->setText(0, sigName);
if (!thisMsgHasMissing)
{
thisMsgHasMissing = true;
msgItem = new QTreeWidgetItem();
msgItem->setText(0, msgName + " (" + Utility::formatCANID(thisMsg->ID) + ")");
sigDiffTwo->addChild(msgItem);
}
msgItem->addChild(missingSigItem);
}
else //signal exists on both sides. See if it as changed position or length
{
bool didChange = false;
QTreeWidgetItem *sigItem = new QTreeWidgetItem();
sigItem->setText(0, sigName);
if (thisSig->startBit != otherSig->startBit)
{
didChange = true;
QTreeWidgetItem *sigStartBit = new QTreeWidgetItem();
sigStartBit->setText(0, "Start Bit First DBC: " + QString::number(thisSig->startBit) + " Second: " + QString::number(otherSig->startBit));
sigItem->addChild(sigStartBit);
}
if (thisSig->signalSize != otherSig->signalSize)
{
didChange = true;
QTreeWidgetItem *sigSize = new QTreeWidgetItem();
sigSize->setText(0, "Size First DBC: " + QString::number(thisSig->signalSize) + " Second: " + QString::number(otherSig->signalSize));
sigItem->addChild(sigSize);
}
if (thisSig->bias != otherSig->bias)
{
didChange = true;
QTreeWidgetItem *sigBias = new QTreeWidgetItem();
sigBias->setText(0, "Bias First DBC: " + QString::number(thisSig->bias) + " Second: " + QString::number(otherSig->bias));
sigItem->addChild(sigBias);
}
if (thisSig->factor != otherSig->factor)
{
didChange = true;
QTreeWidgetItem *sigFactor = new QTreeWidgetItem();
sigFactor->setText(0, "Factor First DBC: " + QString::number(thisSig->factor) + " Second: " + QString::number(otherSig->factor));
sigItem->addChild(sigFactor);
}
if (didChange)
{
if (!thisMsgHasMods)
{
thisMsgHasMods = true;
sigTemp = new QTreeWidgetItem();
sigTemp->setText(0, msgName + " (" + Utility::formatCANID(thisMsg->ID) + ")");
sigModifiedRoot->addChild(sigTemp);
}
sigTemp->addChild(sigItem);
}
}
}
thisMsgHasMissing = false;
for (int i = 0; i < otherMsg->sigHandler->getCount(); i++)
{
DBC_SIGNAL *thisSig = otherMsg->sigHandler->findSignalByIdx(i);
QString sigName = thisSig->name;
DBC_SIGNAL *otherSig = thisMsg->sigHandler->findSignalByName(sigName);
if (!otherSig)
{
QTreeWidgetItem *missingSigItem = new QTreeWidgetItem();
missingSigItem->setText(0, sigName);
if (!thisMsgHasMissing)
{
thisMsgHasMissing = true;
msgItem = new QTreeWidgetItem();
msgItem->setText(0, msgName + " (" + Utility::formatCANID(thisMsg->ID) + ")");
sigDiffOne->addChild(msgItem);
}
msgItem->addChild(missingSigItem);
}
}
}
}
for (int i = 0; i < secondDBC->messageHandler->getCount(); i++)
{
DBC_MESSAGE *origMsg = secondDBC->messageHandler->findMsgByIdx(i);
QString msgName = origMsg->name;
DBC_MESSAGE *msg = firstDBC->messageHandler->findMsgByName(msgName);
if (!msg)
{
QTreeWidgetItem *missingMsgItem = new QTreeWidgetItem();
missingMsgItem->setText(0, msgName + " (" + Utility::formatCANID(origMsg->ID) + ")");
msgMissingDBCFirst->addChild(missingMsgItem);
}
}
QSettings settings;
if (settings.value("InfoCompare/AutoExpand", false).toBool())
{
ui->treeDetails->expandAll();
}
progress.cancel();
qApp->processEvents();
}
void DBCComparatorWindow::saveDetails()
{
QString filename;
QFileDialog dialog(this);
QSettings settings;
QStringList filters;
filters.append(QString(tr("Text File (*.txt)")));
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setDirectory(settings.value("FileComparator/LoadSaveDirectory", dialog.directory().path()).toString());
if (dialog.exec() == QDialog::Accepted)
{
filename = dialog.selectedFiles()[0];
settings.setValue("FileComparator/LoadSaveDirectory", dialog.directory().path());
if (!filename.contains('.')) filename += ".txt";
QFile *outFile = new QFile(filename);
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTreeWidget *tree = ui->treeDetails;
QTreeWidgetItemIterator it(tree);
while (*it) {
QTreeWidgetItem *item = *it;
QString itemText = item->text(0);
while (item->parent())
{
outFile->write(" ");
item = item->parent();
}
outFile->write(itemText.toUtf8() + "\n");
++it;
}
outFile->close();
}
}