forked from wimleers/QVisualizer
/
HeatMapVisualization.cpp
401 lines (328 loc) · 14.9 KB
/
HeatMapVisualization.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "HeatMapVisualization.h"
HeatMapVisualization::HeatMapVisualization(Database *database) : QWidget() {
lastEvents = NULL;
height = database->getResolution().height();
width = database->getResolution().width();
heatMap = new int*[height];
for (int i = 0; i < height; ++i)
heatMap[i] = new int[width];
clickHeatMap = new int*[height];
for (int i = 0; i < height; ++i)
clickHeatMap[i] = new int[width];
mouseRoute = new QVector<QPoint>();
clearHeatMap();
marge = 20;
mouseRouteInterval = 1;
clickThresholdArea = 10;
showLeftClicks = showRightClicks = showSingleClicks = showDoubleClicks = showMouseMoveRoute = true;
showMouseMove = false;
screenWidth = (QApplication::desktop()->width() >= 1920) ? 1050: 470;
screenWidth = (screenWidth > width) ? width : screenWidth;
screenHeight = (QApplication::desktop()->width() > 800) ? 500 : 300;
screenHeight = (screenHeight > height) ? height : screenHeight;
heatMapImage = new QImage(width, height, QImage::Format_ARGB32_Premultiplied);
scene = new HeatMapVisQGraphicsScene();
view = new QGraphicsView(scene);
view->setRenderHints(QPainter::Antialiasing);
scene->setSceneRect(0, 0, screenWidth, screenHeight);
connect(scene, SIGNAL(clicked(QPointF)), SLOT(pixelSelected(QPointF)));
mainLayout = new QVBoxLayout();
mainLayout->addWidget(view, 0, Qt::AlignCenter);
createCheckBoxes();
determineAvailableBackgroundImages(database->getMaxEventTime());
renderVisualization();
setLayout(mainLayout);
}
void HeatMapVisualization::update(QVector<Event*> *events = NULL) {
clearHeatMap();
if(events != NULL)
lastEvents = events;
else if(lastEvents != NULL)
events = lastEvents;
else
return;
lastEventTime = events->isEmpty() ? 0 : events->last()->getTime();
if(scene->numClicksLabel != NULL)
scene->removeItem(scene->numClicksLabel);
scene->numClicksLabel = NULL;
if (scene->timeLineClickLocation != NULL)
scene->removeItem(scene->timeLineClickLocation);
scene->timeLineClickLocation = NULL;
int counter = 0, mouseRouteX = 0, mouseRouteY = 0;
for(int i = 0; i < events->count(); ++i) {
if((showSingleClicks && events->at(i)->getEventType().compare("MouseButtonPress") == 0) ||
(showDoubleClicks && events->at(i)->getEventType().compare("MouseButtonDblClick") == 0) ||
((showMouseMove || showMouseMoveRoute) && events->at(i)->getEventType().compare("MouseMove") == 0)) {
// enkel muiskliks (enkel en dubbel)
QString details = events->at(i)->getDetails();
details.remove("\"");
QStringList args = details.split(";");
if (args.count() >= 2) {
int x = args.at(0).toInt();
int y = args.at(1).toInt();
//qDebug() << details << x << y;
if (x >= 0 && y >= 0 && x < height && y < width) {
if (showMouseMove && events->at(i)->getEventType().compare("MouseMove") == 0)
clickOnHeatMap(x,y);
if ((showSingleClicks || showDoubleClicks) && args.count() == 3 && events->at(i)->getEventType().compare("MouseMove") != 0) {
// controleren: links en rechts filteren indien nodig (checkBoxes)
if (showLeftClicks && args.at(2).indexOf("L") != -1)// args.at(2) == ""L" " "
clickOnHeatMap(x,y);
else if (showRightClicks && args.at(2).indexOf("R") != -1)
clickOnHeatMap(x,y);
clickHeatMap[x][y]++;
}
if (showMouseMoveRoute && events->at(i)->getEventType().compare("MouseMove") == 0) { //muisbewegingen tekenen => gemiddeld punt berekenen per mouseRouteInterval bewegingen
if (counter < mouseRouteInterval) {
mouseRouteX += x;
mouseRouteY += y;
counter++;
}
if(counter == mouseRouteInterval) {
mouseRoute->append(QPoint(mouseRouteY/mouseRouteInterval, mouseRouteX/mouseRouteInterval));
counter = 0;
mouseRouteX = 0;
mouseRouteY = 0;
}
}
}
}
}
}
renderVisualization();
}
void HeatMapVisualization::clickOnHeatMap(int x, int y) {
// waar geklikt is, een hoge score toekennen, de marge errond score alsmaar verkleinen
for (int k = -marge; k <= marge && x + k < height; ++k)
for (int l = -marge; l <= marge && y+l < width; ++l)
if(x + k > 0 && y + l > 0) {
heatMap[x + k][y + l] += (marge + 1) - max(abs(k),abs(l));
}
if (maxClicks < heatMap[x][y])//hoogst voorkomend aantal kliks bijhouden
maxClicks = heatMap[x][y];
}
void HeatMapVisualization::renderVisualization() {
QColor color;
int h, v, s, a;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
if (heatMap[i][j] == 0) {// er is niet geklikt
h = 255;
v = 0;
s = 255;
a = 0;
color.setHsv(h,v,s,a);
heatMapImage->setPixel(j, i, color.rgba());
}
else {// geklikt
s = 255;
float value = float(heatMap[i][j])/float(maxClicks);// verhouding: is op deze plaats veel geklikt
v = value * 255.0;
a = v * 1/30;
if (value > 0.333){//matig tot veel geklikt => de kleuren geel tot rood hiervoor gebruiken
float inversDiff = 3/2, min = 1/3;
int color = 60;//==colorRange
//0 rood
//60 geel
h = color - (inversDiff * (value - min)) * color;
if (heatMap[i][j] > maxClicks) {
h = 0;
v = 255;//in deze omgeving is heel veel geklikt
s = 230;// => extra benadrukken
}
}
else {// weinig geklikt (marge rond klik) => blauw tinten
float inversDiff = 3/1, min = 0;
int color = 240;
int colorRange = 180;
//60 geel
//240 blauw
h = color - (inversDiff * (value - min)) * colorRange;
}
//qDebug() << "color " << h << v << s;
color.setHsv(h,v,s,a);
heatMapImage->setPixel(j, i, color.rgba());
}
}
}
//muisbewigingen tekenen
QPainter painter;
painter.begin(heatMapImage);
for(int i = 0; i + 1 < mouseRoute->size(); ++i){
QLineF line(mouseRoute->at(i), mouseRoute->at(i+1));
painter.drawLine(line);
}
painter.end();
scene->setBackgroundImage(determineBackgroundImage(lastEventTime));
scene->setForegroundImage(heatMapImage);
scene->invalidate();
}
void HeatMapVisualization::highlightEventLocation(int msec) {
update();
for (int i = 0; i < lastEvents->size(); ++i)
if (lastEvents->at(i)->getTime() == msec) {
QString details = lastEvents->at(i)->getDetails();
details.remove("\"");
QStringList args = details.split(";");
if (args.count() >= 2) {
QPoint p = QPoint(args.at(1).toInt(), args.at(0).toInt());
if (p.x() -5 > 0)//midden van bolleke waar geklikt is
p.setX(p.x() - 5);
if (p.y() - 5 > 0)
p.setY(p.y() - 5);
/*QPainter painter;
painter.begin(heatMapImage);
painter.setBrush(QBrush(QColor(255,0,255)));
painter.drawEllipse(p, 5, 5);
painter.end();*/
scene->timeLineClickLocation = scene->addEllipse(QRectF(0,0,10,10),QPen(), QBrush(QColor(255,0,255)));
scene->timeLineClickLocation->setPos(p);
}
}
//scene->setBackgroundImage(determineBackgroundImage(lastEventTime));
//scene->setForegroundImage(heatMapImage);
scene->invalidate();
}
void HeatMapVisualization::pixelSelected(QPointF p) {
int numClicks = 0;
for (int i = -clickThresholdArea; i <= clickThresholdArea && p.y() + i < height; ++i)
for (int j = -clickThresholdArea; j <= clickThresholdArea && p.x() + j < width; ++j)
if(p.x() + i > 0 && p.y() + j > 0)
numClicks += clickHeatMap[int(p.y()) + i][int(p.x()) + j];
if(scene->numClicksLabel == NULL)
scene->numClicksLabel = scene->addText("");
scene->numClicksLabel->setHtml("<html><body bgcolor=\"white\"><p style=\"color: #000000;\">" + QString::number(numClicks) + "</p></body></html>");
scene->numClicksLabel->setPos(p);
scene->invalidate();
}
int HeatMapVisualization::max(int a, int b) {
return (a>b)?a:b;
}
void HeatMapVisualization::clearHeatMap() {
maxClicks = 0;
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
heatMap[i][j] = 0;
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
clickHeatMap[i][j] = 0;
mouseRoute->clear();
}
void HeatMapVisualization::updateParameters(int state) {
Q_UNUSED(state);
showLeftClicks = showLeftClicksCheckBox->isChecked();
showRightClicks = showRightClicksCheckBox->isChecked();
showSingleClicks = showSingleClicksCheckBox->isChecked();
showDoubleClicks = showDoubleClicksCheckBox->isChecked();
showMouseMove = showMouseMoveCheckBox->isChecked();
showMouseMoveRoute = showMouseMoveRouteCheckBox->isChecked();
showLeftClicksCheckBox->setEnabled(true);
showRightClicksCheckBox->setEnabled(true);
showSingleClicksCheckBox->setEnabled(true);
showDoubleClicksCheckBox->setEnabled(true);
showMouseMoveCheckBox->setEnabled(true);
showMouseMoveRouteCheckBox->setEnabled(true);
if(!showSingleClicks && !showDoubleClicks) {
showLeftClicksCheckBox->setEnabled(false);
showRightClicksCheckBox->setEnabled(false);
}
update();
}
void HeatMapVisualization::updateMarge(int marge) {
this->marge = marge;
update();
}
void HeatMapVisualization::updateMouseRouteInterval(int interval) {
mouseRouteInterval = interval;
update();
}
void HeatMapVisualization::updateClickThresholdArea(int threshold) {
clickThresholdArea = threshold;
update();
}
void HeatMapVisualization::createCheckBoxes() {
QGroupBox *box1 = new QGroupBox();
QVBoxLayout *vbox1 = new QVBoxLayout();
showLeftClicksCheckBox = new QCheckBox("Show left-clicks");
showRightClicksCheckBox = new QCheckBox("Show right-clicks");
vbox1->addWidget(showLeftClicksCheckBox);
vbox1->addWidget(showRightClicksCheckBox);
box1->setLayout(vbox1);
QGroupBox *box2 = new QGroupBox();
QVBoxLayout *vbox2 = new QVBoxLayout();
showSingleClicksCheckBox = new QCheckBox("Show single-clicks");
showDoubleClicksCheckBox = new QCheckBox("Show double-clicks");
vbox2->addWidget(showSingleClicksCheckBox);
vbox2->addWidget(showDoubleClicksCheckBox);
box2->setLayout(vbox2);
QGroupBox *box3 = new QGroupBox();
QVBoxLayout *vbox3 = new QVBoxLayout();
showMouseMoveRouteCheckBox = new QCheckBox("Show mouse route");
showMouseMoveCheckBox = new QCheckBox("Show mouse moves");
vbox3->addWidget(showMouseMoveRouteCheckBox);
vbox3->addWidget(showMouseMoveCheckBox);
box3->setLayout(vbox3);
showLeftClicksCheckBox->setChecked(showLeftClicks);
showRightClicksCheckBox->setChecked(showRightClicks);
showSingleClicksCheckBox->setChecked(showSingleClicks);
showDoubleClicksCheckBox->setChecked(showDoubleClicks);
showMouseMoveCheckBox->setChecked(showMouseMove);
showMouseMoveRouteCheckBox->setChecked(showMouseMoveRoute);
connect(showLeftClicksCheckBox, SIGNAL(stateChanged(int)), SLOT(updateParameters(int)));
connect(showRightClicksCheckBox, SIGNAL(stateChanged(int)), SLOT(updateParameters(int)));
connect(showSingleClicksCheckBox, SIGNAL(stateChanged(int)), SLOT(updateParameters(int)));
connect(showDoubleClicksCheckBox, SIGNAL(stateChanged(int)), SLOT(updateParameters(int)));
connect(showMouseMoveCheckBox, SIGNAL(stateChanged(int)), SLOT(updateParameters(int)));
connect(showMouseMoveRouteCheckBox, SIGNAL(stateChanged(int)), SLOT(updateParameters(int)));
QLabel *margeLabel = new QLabel("Click radius: ");
margeSpinBox = new QSpinBox();
margeSpinBox->setRange(1, 100);
margeSpinBox->setValue(marge);
connect(margeSpinBox, SIGNAL(valueChanged(int)), SLOT(updateMarge(int)));
QLabel *mouseRouteIntervalLabel = new QLabel("Mouse move accuracy: ");
mouseRouteIntervalSpinBox = new QSpinBox();
mouseRouteIntervalSpinBox->setRange(1, 100);
mouseRouteIntervalSpinBox->setValue(mouseRouteInterval);
connect(mouseRouteIntervalSpinBox, SIGNAL(valueChanged(int)), SLOT(updateMouseRouteInterval(int)));
QLabel *clickThresholdAreaLabel = new QLabel("Threshold of click area: ");
clickThresholdAreaSpinBox = new QSpinBox();
clickThresholdAreaSpinBox->setRange(1, 100);
clickThresholdAreaSpinBox->setValue(clickThresholdArea);
connect(clickThresholdAreaSpinBox, SIGNAL(valueChanged(int)), SLOT(updateClickThresholdArea(int)));
QGroupBox *box4 = new QGroupBox();
QVBoxLayout *vbox4 = new QVBoxLayout();
QHBoxLayout *hbox4_1 = new QHBoxLayout();
hbox4_1->addWidget(margeLabel);
hbox4_1->addWidget(margeSpinBox);
QHBoxLayout *hbox4_2 = new QHBoxLayout();
hbox4_2->addWidget(mouseRouteIntervalLabel);
hbox4_2->addWidget(mouseRouteIntervalSpinBox);
QHBoxLayout *hbox4_3 = new QHBoxLayout();
hbox4_3->addWidget(clickThresholdAreaLabel);
hbox4_3->addWidget(clickThresholdAreaSpinBox);
vbox4->addLayout(hbox4_1);
vbox4->addLayout(hbox4_2);
vbox4->addLayout(hbox4_3);
box4->setLayout(vbox4);
QHBoxLayout *inputLayout = new QHBoxLayout();
inputLayout->addWidget(box1);
inputLayout->addWidget(box2);
inputLayout->addWidget(box3);
inputLayout->addWidget(box4);
mainLayout->addLayout(inputLayout);
}
void HeatMapVisualization::determineAvailableBackgroundImages(int msecs) {
availableBackgroundImages = new QHash<int, bool>();
QString filename;
foreach(filename, QDir("./screenshots").entryList())
if(filename.contains(".png"))
availableBackgroundImages->insert((filename.split('.')[0]).toInt(), true);
}
QImage* HeatMapVisualization::determineBackgroundImage(int msecs) {
while(msecs > 0) {
if(availableBackgroundImages->contains(msecs))
return new QImage("./screenshots/" + QString::number(msecs) + ".png");
--msecs;
}
return new QImage();
}