-
Notifications
You must be signed in to change notification settings - Fork 288
/
bisectwindow.cpp
239 lines (214 loc) · 7.02 KB
/
bisectwindow.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
#include "bisectwindow.h"
#include "ui_bisectwindow.h"
#include "mainwindow.h"
#include "framefileio.h"
#include "helpwindow.h"
#include <QDebug>
#include <algorithm>
BisectWindow::BisectWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::BisectWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
modelFrames = frames;
connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int)));
connect(ui->btnCalculate, &QAbstractButton::clicked, this, &BisectWindow::handleCalculateButton);
connect(ui->btnReplaceFrames, &QAbstractButton::clicked, this, &BisectWindow::handleReplaceButton);
connect(ui->btnSaveFrames, &QAbstractButton::clicked, this, &BisectWindow::handleSaveButton);
connect(ui->slideFrameNumber, &QSlider::sliderReleased, this, &BisectWindow::updateFrameNumText);
connect(ui->slidePercentage, &QSlider::sliderReleased, this, &BisectWindow::updatePercentText);
connect(ui->editFrameNumber, &QLineEdit::editingFinished, this, &BisectWindow::updateFrameNumSlider);
connect(ui->editPercentage, &QLineEdit::editingFinished, this, &BisectWindow::updatePercentSlider);
connect(ui->rbFrameNumber, &QRadioButton::toggled, this, &BisectWindow::updateSectionsText);
connect(ui->rbPercentage, &QRadioButton::toggled, this, &BisectWindow::updateSectionsText);
connect(ui->rbBusNum, &QRadioButton::toggled, this, &BisectWindow::updateSectionsText);
connect(ui->rbIDRange, &QRadioButton::toggled, this, &BisectWindow::updateSectionsText);
installEventFilter(this);
updateSectionsText();
}
BisectWindow::~BisectWindow()
{
removeEventFilter(this);
delete ui;
}
void BisectWindow::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
refreshIDList();
refreshFrameNumbers();
updateFrameNumText();
updatePercentText();
}
bool BisectWindow::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("bisector.md");
break;
}
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return false;
}
void BisectWindow::updateSectionsText()
{
if (ui->rbBusNum->isChecked())
{
ui->rbLowerSection->setText("Only this bus");
ui->rbUpperSection->setText("Not this bus");
}
if (ui->rbFrameNumber->isChecked())
{
ui->rbLowerSection->setText("Up to this frame number");
ui->rbUpperSection->setText("After this frame number");
}
if (ui->rbIDRange->isChecked())
{
ui->rbLowerSection->setText("Inside the ID range");
ui->rbUpperSection->setText("Outside the ID range");
}
if (ui->rbPercentage->isChecked())
{
ui->rbLowerSection->setText("Up to this percentage into the file");
ui->rbUpperSection->setText("After this percentage into the file");
}
}
void BisectWindow::refreshIDList()
{
int id;
foundID.clear();
ui->cbIDLower->clear();
ui->cbIDUpper->clear();
for (int i = 0; i < modelFrames->count(); i++)
{
id = modelFrames->at(i).frameId();
if (!foundID.contains(id))
{
foundID.append(id);
}
}
std::sort(foundID.begin(), foundID.end());
foreach (int id, foundID) {
ui->cbIDLower->addItem(Utility::formatCANID(id));
ui->cbIDUpper->addItem(Utility::formatCANID(id));
}
}
void BisectWindow::refreshFrameNumbers()
{
ui->labelMainListNum->setText(QString::number(modelFrames->count()));
ui->labelSplitNum->setText(QString::number(splitFrames.count()));
ui->slideFrameNumber->setMaximum(modelFrames->count());
}
void BisectWindow::updatedFrames(int numFrames)
{
if (numFrames == -1) //all frames deleted
{
refreshFrameNumbers();
}
else if (numFrames == -2) //all new set of frames. Reset
{
refreshFrameNumbers();
refreshIDList();
}
else //just got some new frames. See if they are relevant.
{
}
}
void BisectWindow::handleCalculateButton()
{
splitFrames.clear();
bool saveLower = ui->rbLowerSection->isChecked();
int targetFrameNum = 0;
if (ui->rbFrameNumber->isChecked() || ui->rbPercentage->isChecked())
{
if (ui->rbFrameNumber->isChecked()) targetFrameNum = ui->slideFrameNumber->value();
else targetFrameNum = modelFrames->count() * (ui->slidePercentage->value() / 10000.0);
qDebug() << "Target frame num " << targetFrameNum;
if (saveLower)
{
for (int i = 0; i < targetFrameNum; i++) splitFrames.append(modelFrames->at(i));
}
else
{
for (int i = targetFrameNum; i < modelFrames->count(); i++) splitFrames.append(modelFrames->at(i));
}
}
else if (ui->rbIDRange->isChecked())
{
uint32_t lowerID = Utility::ParseStringToNum2(ui->cbIDLower->currentText());
uint32_t upperID = Utility::ParseStringToNum2(ui->cbIDUpper->currentText());
for (int i = 0; i < modelFrames->count(); i++)
{
if (modelFrames->at(i).frameId() >= lowerID && modelFrames->at(i).frameId() <= upperID)
{
if (saveLower) splitFrames.append(modelFrames->at(i));
}
else
{
if (!saveLower) splitFrames.append(modelFrames->at(i));
}
}
}
else if (ui->rbBusNum->isChecked())
{
int targetBus = Utility::ParseStringToNum(ui->editBusNum->text());
for (int i = 0; i < modelFrames->count(); i++)
{
if (modelFrames->at(i).bus == targetBus)
{
if (saveLower) splitFrames.append(modelFrames->at(i));
}
else
{
if (!saveLower) splitFrames.append(modelFrames->at(i));
}
}
}
refreshFrameNumbers();
}
void BisectWindow::handleReplaceButton()
{
CANFrameModel *model;
model = MainWindow::getReference()->getCANFrameModel();
model->clearFrames();
model->insertFrames(splitFrames);
refreshFrameNumbers();
refreshIDList();
}
void BisectWindow::handleSaveButton()
{
QMessageBox msg;
QString filename;
if (FrameFileIO::saveFrameFile(filename, &splitFrames))
{
msg.setText(tr("Successfully saved file"));
}
else
{
msg.setText(tr("Error while attempting to save."));
}
msg.exec();
}
void BisectWindow::updateFrameNumSlider()
{
ui->slideFrameNumber->setValue(ui->editFrameNumber->text().toInt());
}
void BisectWindow::updatePercentSlider()
{
ui->slidePercentage->setValue(ui->editPercentage->text().toFloat() * 100);
}
void BisectWindow::updateFrameNumText()
{
ui->editFrameNumber->setText(QString::number(ui->slideFrameNumber->value()));
}
void BisectWindow::updatePercentText()
{
ui->editPercentage->setText(QString::number(ui->slidePercentage->value() / 100.0f));
}