-
Notifications
You must be signed in to change notification settings - Fork 20
/
plot_view.cpp
247 lines (199 loc) · 5.92 KB
/
plot_view.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
/*
* 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 <algorithm>
#include <QtGui>
#include "plot_view.h"
using std::min;
using std::max;
PlotView::PlotView(QWidget *p)
: QLabel(p),
m1_(0.), m2_(1.), px_(-1), py_(-1), ind_(0), s_(none), allow_selection_(true) {
}
void PlotView::enableSelection(bool v) {
allow_selection_ = v;
update();
}
void PlotView::setImage(int ind, QImage &img) {
img_[ind] = img;
update_pix();
update();
}
void PlotView::set_data(const float *dat, long len, bool normalize) {
ind_ = 0;
set_data(0, dat, len, normalize);
}
void PlotView::set_data(int ind, const float *dat, long len, bool normalize) {
int w = width();
int h = height();
float mn = 0.;
float mx = 1.;
if (normalize) {
mn = 99999999.;
mx = -99999999.;
for (int i = 0; i < len; i++) {
mn = min(mn, dat[i]);
mx = max(mx, dat[i]);
}
if (mn == mx) {
mn -= .5;
mx += .5;
}
}
{
QImage img(w, h, QImage::Format_RGB32);
img.fill(0);
auto acc = new float[h];
memset(acc, 0, h * sizeof(float));
auto cnt = new int[h];
memset(cnt, 0, h * sizeof(int));
for (int i = 0; i < len; i++) {
float v = dat[i];
int ind2 = int((i / float(len)) * (h - 1) + .5);
acc[ind2] += (v - mn) / (mx - mn);
cnt[ind2]++;
}
auto p = (unsigned int *) img.bits();
int px = -1;
int pc = -1;
for (int i = 0; i < h; i++) {
int x = px;
int c = pc;
if (cnt[i] == 0 && px == -1) continue;
if (cnt[i] > 0) {
float na = acc[i] / cnt[i];
x = int(na * (w - 4) + .5) + 2; // slight offset so not to interfere with border
px = x;
c = 20 + int(na * (255 - 20));
pc = c;
}
unsigned char r = 20;
unsigned char g = min(c + 60, 255);
unsigned char b = 20;
unsigned int v = 0xff000000 | (r << 16) | (g << 8) | (b << 0);
p[i * w + x] = v;
}
delete[] acc;
delete[] cnt;
setImage(ind, img);
}
}
void PlotView::paintEvent(QPaintEvent *e) {
QLabel::paintEvent(e);
QPainter p(this);
if (allow_selection_) {
int ry1 = m1_ * height();
int ry2 = m2_ * height();
QBrush brush(QColor(128, 64, 64, 128 + 32));
QPen pen(brush, 5, Qt::SolidLine, Qt::RoundCap);
p.setPen(pen);
p.drawLine(0 + 3, ry1, width() - 1 - 3, ry1);
p.drawLine(0 + 3, ry2, width() - 1 - 3, ry2);
}
{
// a border around the image helps to see the border of a dark image
p.setPen(Qt::darkGray);
p.drawRect(0, 0, width() - 1, height() - 1);
}
}
void PlotView::resizeEvent(QResizeEvent *e) {
QLabel::resizeEvent(e);
update_pix();
}
void PlotView::update_pix() {
if (img_[ind_].isNull()) return;
int vw = width() - 4;
int vh = height() - 4; // TODO BUG: With QDarkStyle, without the subtraction, the height or width of the application grows without bounds.
pix_ = QPixmap::fromImage(img_[ind_]).scaled(vw, vh); //, Qt::KeepAspectRatio);
setPixmap(pix_);
}
void PlotView::mousePressEvent(QMouseEvent *e) {
e->accept();
if (!allow_selection_) return;
if (e->button() != Qt::LeftButton) return;
int x = e->pos().x();
int y = e->pos().y();
if (x < 0) x = 0;
if (x > width() - 1) x = width() - 1;
if (y < 0) y = 0;
if (y > height() - 1) y = height() - 1;
float yp = y / float(height());
if (yp > m1_ && (yp - m1_) < .01) {
s_ = m1_moving;
} else if (yp < m2_ && (m2_ - yp) < .01) {
s_ = m2_moving;
} else if (m1_ < yp && yp < m2_) {
s_ = m12_moving;
} else {
s_ = none;
}
px_ = x;
py_ = y;
}
void PlotView::mouseMoveEvent(QMouseEvent *e) {
e->accept();
if (s_ == none) return;
int x = e->pos().x();
int y = e->pos().y();
if (x < 0) x = 0;
if (x > width() - 1) x = width() - 1;
if (y < 0) y = 0;
if (y > height() - 1) y = height() - 1;
if (y == py_) return;
int h = height();
float m1 = m1_;
float m2 = m2_;
if (s_ == m1_moving) {
m1 = y / float(h);
} else if (s_ == m2_moving) {
m2 = y / float(h);
} else if (s_ == m12_moving) {
float dy = (y - py_) / float(h);
m1 += dy;
m2 += dy;
}
if (m1 >= m2_ - .01) m1 = m2_ - .01;
if (m1 < 0.) m1 = 0.;
if (m2 <= m1_ + .01) m2 = m1_ + .01;
if (m2 > 1.) m2 = 1.;
m1_ = m1;
m2_ = m2;
px_ = x;
py_ = y;
update();
emit(rangeSelected(m1_, m2_));
}
void PlotView::mouseReleaseEvent(QMouseEvent *e) {
e->accept();
if (e->button() == Qt::RightButton) {
ind_ = (ind_ + 1) % 2;
update_pix();
update();
}
if (e->button() != Qt::LeftButton) return;
// int x = e->pos().x();
// int y = e->pos().y();
//
// if (x < 0) x = 0;
// if (x > width() - 1) x = width() - 1;
// if (y < 0) y = 0;
// if (y > height() - 1) y = height() - 1;
px_ = -1;
py_ = -1;
s_ = none;
}