-
Notifications
You must be signed in to change notification settings - Fork 20
/
main_app.cpp
301 lines (252 loc) · 7.83 KB
/
main_app.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
/*
* Copyright (c) 2015, 2017, 2020 Kent A. Vander Velden, [email protected]
*
* This file is part of BinVis.
*
* BinVis is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BinVis is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BinVis. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <QtGui>
#include <QComboBox>
#include <QFileDialog>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QSettings>
#include "main_app.h"
#include "binary_viewer.h"
#include "overall_view.h"
#include "histogram_2d_view.h"
#include "image_view.h"
#include "dot_plot.h"
#include "histogram_3d_view.h"
#include "plot_view.h"
#include "histogram_calc.h"
static int scroller_w = 16 * 8;
MainApp::MainApp(QWidget *p)
: QDialog(p), cur_file_(-1), bin_(nullptr), bin_len_(0), start_(0), end_(0) {
done_flag_ = false;
auto top_layout = new QGridLayout;
{
auto layout = new QHBoxLayout;
{
auto pb = new QPushButton("Load file");
pb->setFixedSize(pb->sizeHint());
connect(pb, SIGNAL(clicked()), SLOT(loadFile()));
layout->addWidget(pb);
}
{
auto pb = new QPushButton("Prev");
pb->setFixedSize(pb->sizeHint());
connect(pb, SIGNAL(clicked()), SLOT(prevFile()));
layout->addWidget(pb);
}
{
auto pb = new QPushButton("Next");
pb->setFixedSize(pb->sizeHint());
connect(pb, SIGNAL(clicked()), SLOT(nextFile()));
layout->addWidget(pb);
}
top_layout->addLayout(layout, 0, 0);
}
{
overall_primary_ = new OverallView;
overall_zoomed_ = new OverallView;
plot_view_ = new PlotView;
connect(overall_primary_, SIGNAL(rangeSelected(float, float)), SLOT(rangeSelected(float, float)));
overall_primary_->setFixedWidth(scroller_w);
overall_zoomed_->setFixedWidth(scroller_w);
plot_view_->setFixedWidth(scroller_w);
overall_zoomed_->enableSelection(false);
plot_view_->enableSelection(false);
auto layout = new QHBoxLayout;
layout->addWidget(overall_primary_);
layout->addWidget(overall_zoomed_);
layout->addWidget(plot_view_);
top_layout->addLayout(layout, 1, 0);
}
{
auto layout = new QHBoxLayout;
{
cur_view_ = new QComboBox();
cur_view_->addItem("3D histogram");
cur_view_->addItem("2D histogram");
cur_view_->addItem("Binary view");
cur_view_->addItem("Image view");
cur_view_->addItem("Dot plot");
cur_view_->setFixedSize(cur_view_->sizeHint());
connect(cur_view_, SIGNAL(currentIndexChanged(int)), SLOT(switchView(int)));
layout->addWidget(cur_view_);
}
{
filename_ = new QLabel();
layout->addWidget(filename_);
}
top_layout->addLayout(layout, 0, 1);
}
{
histogram_3d_ = new Histogram3dView;
histogram_2d_ = new Histogram2dView;
binary_viewer_ = new BinaryViewer;
image_view_ = new ImageView;
dot_plot_ = new DotPlot;
views_.push_back(histogram_3d_);
views_.push_back(histogram_2d_);
views_.push_back(binary_viewer_);
views_.push_back(image_view_);
views_.push_back(dot_plot_);
auto layout = new QHBoxLayout;
for (const auto &j : views_) {
layout->addWidget(j);
}
top_layout->addLayout(layout, 1, 1);
}
switchView(-1);
setLayout(top_layout);
}
MainApp::~MainApp() {
quit();
}
void MainApp::resizeEvent(QResizeEvent *e) {
QDialog::resizeEvent(e);
update_views();
}
void MainApp::quit() {
if (!done_flag_) {
done_flag_ = true;
exit(EXIT_SUCCESS);
}
}
void MainApp::reject() {
quit();
}
bool MainApp::load_file(const QString &filename) {
QString title;
if (files_.size() > 1) {
title = QString("%1/%2: %3").arg(cur_file_ + 1).arg(files_.size()).arg(filename);
} else {
title = filename;
}
filename_->setText(title);
FILE *f = fopen(filename.toStdString().c_str(), "rb");
if (!f) {
fprintf(stderr, "Unable to open %s\n", filename.toStdString().c_str());
return false;
}
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
if (bin_ != nullptr) {
delete[] bin_;
bin_ = nullptr;
bin_len_ = 0;
start_ = 0;
end_ = 0;
}
bin_ = new unsigned char[len];
bin_len_ = fread(bin_, 1, len, f);
fclose(f);
if (len != bin_len_) {
printf("premature read %ld of %ld\n", bin_len_, len);
}
start_ = 0;
end_ = bin_len_;
update_views();
return true;
}
bool MainApp::load_files(const QStringList &filenames) {
files_ = filenames;
cur_file_ = -1;
return nextFile();
}
bool MainApp::prevFile() {
bool rv = false;
while (cur_file_ > 0 && !rv) {
cur_file_--;
rv = load_file(files_[cur_file_]);
}
return rv;
}
bool MainApp::nextFile() {
bool rv = false;
while (cur_file_ + 1 < files_.size() && !rv) {
cur_file_++;
rv = load_file(files_[cur_file_]);
}
return rv;
}
void MainApp::loadFile() {
QStringList files = QFileDialog::getOpenFileNames(
this,
"Select one or more files to open");
if (!files.empty()) {
load_files(files);
}
}
void MainApp::update_views(bool update_iv1) {
if (update_iv1) overall_primary_->clear();
if (bin_ == nullptr) return;
// iv1 shows the entire file, iv2 shows the current segment
if (update_iv1) overall_primary_->set_data(bin_ + 0, bin_len_);
overall_zoomed_->set_data(bin_ + start_, end_ - start_);
{
long n;
auto dd = generate_entropy(bin_ + start_, end_ - start_, n);
if (dd) {
plot_view_->set_data(0, dd, n);
delete[] dd;
}
}
{
auto dd = generate_histo(bin_ + start_, end_ - start_);
if (dd) {
plot_view_->set_data(1, dd, 256, false);
delete[] dd;
}
}
if (histogram_3d_->isVisible()) histogram_3d_->setData(bin_ + start_, end_ - start_);
if (histogram_2d_->isVisible()) histogram_2d_->setData(bin_ + start_, end_ - start_);
if (binary_viewer_->isVisible()) {
// binary_viewer_->setData(bin_ + start_, end_ - start_);
binary_viewer_->setData(bin_, end_);
binary_viewer_->setStart(start_ / 16);
}
if (image_view_->isVisible()) image_view_->setData(bin_ + start_, end_ - start_);
if (dot_plot_->isVisible()) dot_plot_->setData(bin_ + start_, end_ - start_);
}
void MainApp::rangeSelected(float s, float e) {
start_ = s * bin_len_;
end_ = e * bin_len_;
update_views(false);
}
void MainApp::switchView(int ind) {
for (const auto &j : views_) {
j->hide();
}
{
QSettings settings;
if (ind == -1) {
ind = settings.value("last_view", 0).toInt();
}
ind = std::max(ind, 0);
ind = std::min(ind, int(views_.size() - 1));
settings.setValue("last_view", ind);
cur_view_->blockSignals(true);
cur_view_->setCurrentIndex(ind);
cur_view_->blockSignals(false);
}
views_[ind]->show();
update_views(false);
}