forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewgraphdialog.cpp
168 lines (141 loc) · 5.11 KB
/
newgraphdialog.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
#include "newgraphdialog.h"
#include "ui_newgraphdialog.h"
#include <QColorDialog>
#include "utility.h"
NewGraphDialog::NewGraphDialog(DBCHandler *handler, QWidget *parent) :
QDialog(parent),
ui(new Ui::NewGraphDialog)
{
ui->setupUi(this);
dbcHandler = handler;
connect(ui->colorSwatch, SIGNAL(clicked(bool)), this, SLOT(colorSwatchClick()));
connect(ui->btnAddGraph, SIGNAL(clicked(bool)), this, SLOT(addButtonClicked()));
// Seed the random generator with current time
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
QPalette p = ui->colorSwatch->palette();
//using 160 instead of 255 so that colors are always at least a little dark
p.setColor(QPalette::Button, QColor(qrand() % 160,qrand() % 160,qrand() % 160));
ui->colorSwatch->setPalette(p);
connect(ui->cbMessages, SIGNAL(currentIndexChanged(int)), this, SLOT(loadSignals(int)));
connect(ui->cbSignals, SIGNAL(currentIndexChanged(int)), this, SLOT(fillFormFromSignal(int)));
loadMessages();
}
NewGraphDialog::~NewGraphDialog()
{
delete ui;
}
void NewGraphDialog::addButtonClicked()
{
accept();
}
void NewGraphDialog::colorSwatchClick()
{
QColor newColor = QColorDialog::getColor(ui->colorSwatch->palette().button().color());
QPalette p = ui->colorSwatch->palette();
p.setColor(QPalette::Button, newColor);
ui->colorSwatch->setPalette(p);
}
void NewGraphDialog::setParams(GraphParams ¶ms)
{
ui->txtID->setText(Utility::formatNumber(params.ID));
ui->txtBias->setText(QString::number(params.bias));
ui->txtMask->setText(Utility::formatNumber(params.mask));
ui->txtScale->setText(QString::number(params.scale));
ui->txtStride->setText(QString::number(params.stride));
ui->cbSigned->setChecked(params.isSigned);
ui->txtName->setText(params.graphName);
if (params.endByte > -1)
{
ui->txtData->setText(QString::number(params.startByte) + "-" + QString::number(params.endByte));
}
else
{
ui->txtData->setText(QString::number(params.startByte));
}
QPalette p = ui->colorSwatch->palette();
p.setColor(QPalette::Button, params.color);
ui->colorSwatch->setPalette(p);
}
void NewGraphDialog::getParams(GraphParams ¶ms)
{
params.ID = Utility::ParseStringToNum(ui->txtID->text());
params.bias = ui->txtBias->text().toFloat();
params.color = ui->colorSwatch->palette().button().color();
params.isSigned = ui->cbSigned->isChecked();
params.mask = Utility::ParseStringToNum(ui->txtMask->text());
params.scale = ui->txtScale->text().toFloat();
params.stride = Utility::ParseStringToNum(ui->txtStride->text());
QStringList values = ui->txtData->text().split('-');
params.startByte = -1;
params.endByte = -1;
if (values.count() > 0)
{
params.startByte = values[0].toInt();
if (values.count() > 1)
{
params.endByte = values[1].toInt();
}
}
//now catch stupidity and bring it to defaults
if (params.mask == 0) params.mask = 0xFFFFFFFF;
if (fabs(params.scale) < 0.00000001) params.scale = 1.0f;
if (params.stride < 1) params.stride = 1;
params.graphName = ui->txtName->text();
}
void NewGraphDialog::loadMessages()
{
ui->cbMessages->clear();
if (dbcHandler == NULL) return;
for (int x = 0; x < dbcHandler->dbc_messages.count(); x++)
{
ui->cbMessages->addItem(dbcHandler->dbc_messages[x].name);
}
}
void NewGraphDialog::loadSignals(int idx)
{
Q_UNUSED(idx);
//messages were placed into the list in the same order as they exist
//in the data structure so it should have been possible to just
//look it up based on index but by name is probably safer and this operation
//is not time critical at all.
DBC_MESSAGE *msg = dbcHandler->findMsgByName(ui->cbMessages->currentText());
if (msg == NULL) return;
ui->cbSignals->clear();
for (int x = 0; x < msg->msgSignals.count(); x++)
{
ui->cbSignals->addItem(msg->msgSignals[x].name);
}
}
void NewGraphDialog::fillFormFromSignal(int idx)
{
Q_UNUSED(idx);
GraphParams params;
DBC_MESSAGE *msg = dbcHandler->findMsgByName(ui->cbMessages->currentText());
if (msg == NULL) return;
DBC_SIGNAL *sig = dbcHandler->findSignalByName(msg, ui->cbSignals->currentText());
if (sig == NULL) return;
params.graphName = sig->name;
params.ID = msg->ID;
params.bias = sig->bias;
params.scale = sig->factor;
params.stride = 1;
params.mask = (1 << (sig->signalSize)) - 1;
if (sig->valType == SIGNED_INT) params.isSigned = true;
else params.isSigned = false;
params.color = ui->colorSwatch->palette().color(QPalette::Button);
if (sig->intelByteOrder)
{
//for this ordering the byte order is reserved and starting byte
//will be the higher value
params.endByte = sig->startBit / 8;
params.startByte = (sig->startBit + sig->signalSize - 1) / 8;
}
else
{
//for this ordering it goes in normal numerical order
params.startByte = sig->startBit / 8;
params.endByte = (sig->startBit + sig->signalSize - 1) / 8;
}
setParams(params);
}