forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisectwindow.cpp
184 lines (163 loc) · 5.19 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
#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);
installEventFilter(this);
}
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.html");
break;
}
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return false;
}
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;
if (ui->rbFrameNumber->isChecked() || ui->rbPercentage->isChecked())
{
if (ui->rbFrameNumber->isChecked()) targetFrameNum = ui->slideFrameNumber->value();
else targetFrameNum = modelFrames->count() * ui->slidePercentage->value() / 10000;
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) 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));
}