-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSymView.cpp
More file actions
642 lines (544 loc) · 16.5 KB
/
Copy pathSymView.cpp
File metadata and controls
642 lines (544 loc) · 16.5 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//
// filename: description
// Copyright (C) 2018 Gonzalo José Carracedo Carballal
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>
//
#include "SymView.h"
#include <QPainter>
#include <fstream>
#include <iomanip>
#include <SuWidgetsHelpers.h>
#include <QApplication>
#include <QClipboard>
SymView::SymView(QWidget *parent) :
ThrottleableWidget(parent)
{
this->background = SYMVIEW_DEFAULT_BG_COLOR;
this->lowSym = SYMVIEW_DEFAULT_LO_COLOR;
this->highSym = SYMVIEW_DEFAULT_HI_COLOR;
this->setFocusPolicy(Qt::StrongFocus);
this->setMouseTracking(true);
this->invalidate();
}
void
SymView::assertImage(void)
{
if (this->viewPort.width() != this->width()
|| this->viewPort.height() != this->height()) {
if (this->autoStride)
this->setStride(static_cast<unsigned int>(this->width()));
this->viewPort = QImage(
this->width(),
this->height(),
QImage::Format_ARGB32);
}
}
void
SymView::drawToImage(
QImage &image,
unsigned int start,
unsigned int end,
unsigned int zoom,
unsigned int lineSize,
unsigned int lineSkip,
unsigned int lineStart,
bool showSelection)
{
unsigned int x = 0;
int y = 0;
int asInt, convD;
unsigned int p = start;
QRgb *scanLine;
QRgb color;
qint64 selStart = 0, selEnd = 0;
// Calculate conversion coefficients
convD = (1 << this->bps) - 1;
scanLine = reinterpret_cast<QRgb *>(image.scanLine(0));
if (lineSize == 0)
lineSize = static_cast<unsigned int>(image.width());
if (showSelection) {
if (this->selStart > this->selEnd) {
selStart = this->selEnd - 1;
selEnd = this->selStart + 1;
} else {
selStart = this->selStart;
selEnd = this->selEnd;
}
}
if (this->zoom == 1) {
bool inSel;
while (p < end) {
inSel = showSelection && selStart <= p && p < selEnd;
asInt = (static_cast<int>(this->buffer[p++]) * 255) / convD;
if (this->reverse)
asInt = ~asInt;
// You like Cobol, right?
if (x++ >= lineStart) {
if (inSel)
color = qRgb(255 - asInt, 255 - asInt, 255);
else
color = qRgb(
(this->lowSym.red() * (255 - asInt) + this->highSym.red() * asInt) / 255,
(this->lowSym.green() * (255 - asInt) + this->highSym.green() * asInt) / 255,
(this->lowSym.blue() * (255 - asInt) + this->highSym.blue() * asInt) / 255);
scanLine[x - 1 - lineStart] = color;
}
if (x >= lineSize) {
x = 0;
scanLine = reinterpret_cast<QRgb *>(image.scanLine(++y));
p += lineSkip; // Skip non visible pixels
}
}
} else {
// If zoom is bigger than one, we decide which pixel belongs to which
// symbol instead. We assume the symbol stream to be divided in
// blocks of lineSize symbols, starting by start. Therefore:
//
// x = i / zoom
// y = j / zoom
//
// if (x >= lineSize) p(x, y) = 0
// p(x, y) = start + x + y * lineSize
//
unsigned int stride = lineSize + lineSkip;
bool highlight = zoom > 2 && this->hoverX > 0 && this->hoverY > 0;
QRgb color;
bool inSel;
int width = static_cast<int>(stride * zoom);
if (width > image.width())
width = image.width();
for (int j = 0; j < image.height(); ++j) {
unsigned int y = static_cast<unsigned>(j) / zoom;
scanLine = reinterpret_cast<QRgb *>(image.scanLine(j));
for (int i = 0; i < width; ++i) {
unsigned x = static_cast<unsigned>(i) / zoom
+ lineStart;
if (x < stride) {
p = start + x + y * stride;
inSel = showSelection && selStart <= p && p < selEnd;
asInt = (static_cast<int>(this->buffer[p]) * 255) / convD;
if (p >= end)
break;
if (this->reverse)
asInt = ~asInt;
if (inSel)
color = qRgb(255 - asInt, 255 - asInt, 255);
else
color = qRgb(
(this->lowSym.red() * (255 - asInt) + this->highSym.red() * asInt) / 255,
(this->lowSym.green() * (255 - asInt) + this->highSym.green() * asInt) / 255,
(this->lowSym.blue() * (255 - asInt) + this->highSym.blue() * asInt) / 255);
scanLine[i] = color;
}
}
if (p > end)
break;
}
if (highlight) {
unsigned int y = static_cast<unsigned>(this->hoverY) / zoom;
unsigned int x = static_cast<unsigned>(this->hoverX) / zoom;
unsigned int highlightPtr = start + x + lineStart + y * stride;
unsigned int uWidth = stride - lineStart;
if (highlightPtr >= start
&& highlightPtr < end
&& x < uWidth) {
x *= zoom;
y *= zoom;
uWidth *= zoom;
emit hoverSymbol(highlightPtr);
for (unsigned int j = 0; j < zoom; ++j)
if (y + j < static_cast<unsigned>(image.height())) {
scanLine = reinterpret_cast<QRgb *>(
image.scanLine(static_cast<int>(y + j)));
if (j == 0 || j == zoom - 1) {
for (unsigned int i = x; i < qBound(0u, x + zoom, uWidth); ++i)
scanLine[i] = qRgb(255, 0, 0);
} else {
scanLine[x] = qRgb(255, 0, 0);
if (x + zoom <= uWidth)
scanLine[x + zoom - 1] = qRgb(255, 0, 0);
}
}
}
}
}
}
void
SymView::draw(void)
{
unsigned int available;
int width = this->viewPort.width();
int zoom = static_cast<int>(this->zoom);
int limitBar = static_cast<int>(stride * zoom);
if (!this->size().isValid())
return;
// Assert a few things before going on
this->assertImage();
int lineSize = this->stride > width / zoom
? width / zoom
: this->stride;
unsigned int lineSkip = static_cast<unsigned int>(this->stride - lineSize);
unsigned int lineStart = static_cast<unsigned int>(this->hOffset);
// lineSize: Number of visible symbols
// lineSkip: Number of invisible symbols
// If I start beyond the number of visible symbols, I will fall beyond
// the line limits.
if (lineStart > lineSkip)
lineStart = lineSkip;
unsigned int visibleLines =
(static_cast<unsigned>(this->height()) + this->zoom - 1) / this->zoom;
unsigned visible = static_cast<unsigned>(this->stride) * visibleLines;
this->viewPort.fill(this->background); // Fill in black
if (this->bps > 0 && this->buffer.size() > this->offset) {
available = static_cast<unsigned>(this->buffer.size()) - this->offset;
// Calculate how many symbols are visible
if (visible > available)
visible = available;
this->drawToImage(
this->viewPort,
this->offset,
this->offset + visible,
this->zoom,
static_cast<unsigned int>(lineSize) + lineStart, // lineSize
lineSkip - lineStart, // lineSkip
lineStart, // lineStart
true); // drawSelection
}
// Draw limitbar on the right
if (limitBar + zoom <= width) {
int height = this->viewPort.height();
for (auto i = 0; i < zoom; ++i) {
for (auto j = 0; j < height; ++j) {
QRgb *scanLine = reinterpret_cast<QRgb *>(
this->viewPort.scanLine(static_cast<int>(j)));
scanLine[i + limitBar] = qRgb(255, 0, 0);
}
}
}
}
void
SymView::clear(void)
{
this->buffer.clear();
this->offset = 0;
this->selStart = this->selEnd = 0;
this->invalidate();
}
// Can you belive that some people out there code like this for a living?
void
SymView::save(QString const &dest, FileFormat format)
{
QFile file(dest);
char b;
QImage img;
file.open(QIODevice::WriteOnly);
if (!file.isOpen())
throw std::ios_base::failure("Failed to save file " + dest.toStdString());
if (format > FILE_FORMAT_C_ARRAY) {
// Initialize image
img = QImage(this->stride, this->getLines(), QImage::Format_ARGB32);
drawToImage(
img,
this->offset % static_cast<unsigned>(this->stride),
static_cast<unsigned>(this->buffer.size()));
}
switch (format) {
case FILE_FORMAT_TEXT:
for (auto p = this->buffer.begin();
p != this->buffer.end();
++p) {
b = '0' + static_cast<char>(*p);
file.write(&b, 1);
}
break;
case FILE_FORMAT_RAW:
for (auto p = this->buffer.begin();
p != this->buffer.end();
++p) {
b = static_cast<char>(*p & ((1 << this->bps) - 1));
file.write(&b, 1);
}
break;
case FILE_FORMAT_C_ARRAY:
char buf[8];
file.write("#include <stdint.h>\n\n");
file.write(
("static uint8_t data[" + QString::number(this->buffer.size()) + "] = {\n")
.toUtf8());
for (unsigned int i = 0; i < this->buffer.size(); ++i) {
if (i % 16 == 0)
file.write(" ");
snprintf(buf, 8, "0x%02x, ", static_cast<unsigned int>(this->buffer[i]));
file.write(buf);
if (i % 16 == 15)
file.write("\n");
}
file.write("};\n");
break;
case FILE_FORMAT_BMP:
img.save(&file, "BMP");
break;
case FILE_FORMAT_PNG:
img.save(&file, "PNG");
break;
case FILE_FORMAT_JPEG:
img.save(&file, "JPEG");
break;
case FILE_FORMAT_PPM:
img.save(&file, "PPM");
break;
}
}
void
SymView::paint(void)
{
QPainter painter(this);
painter.drawImage(0, 0, this->viewPort);
}
void
SymView::scrollToBottom(void)
{
unsigned int new_offset = 0;
int size = static_cast<int>(this->buffer.size());
int lines = (size + this->stride - 1) / this->stride;
if (lines > this->height() / static_cast<int>(this->zoom))
new_offset = static_cast<unsigned int>(
(lines - this->height() / static_cast<int>(this->zoom))
* this->stride);
this->setOffset(new_offset);
}
void
SymView::feed(const Symbol *data, unsigned int length)
{
this->buffer.insert(
this->buffer.end(),
data,
data + length);
if (length > 0) {
if (this->autoScroll)
this->scrollToBottom();
this->invalidate();
}
}
void
SymView::feed(std::vector<Symbol> const &x)
{
this->feed(x.data(), static_cast<unsigned int>(x.size()));
}
void
SymView::mousePressEvent(QMouseEvent *ev)
{
qint64 off = coordToOffset(hoverX, hoverY);
if (off >= 0) {
if (ev->button() == Qt::MouseButton::LeftButton) {
this->selecting = true;
this->selStart = off;
this->selEnd = off;
this->invalidate();
}
}
}
void
SymView::mouseReleaseEvent(QMouseEvent *ev)
{
if (this->selecting) {
if (ev->button() == Qt::MouseButton::LeftButton) {
this->selecting = false;
this->invalidate();
}
}
}
qint64
SymView::coordToOffset(int x, int y)
{
qint64 off;
x /= this->zoom;
y /= this->zoom;
if (x >= this->stride)
x = this->stride - 1;
else if (x < 0)
x = 0;
x += this->hOffset;
off = this->offset + SCAST(qint64, x) + SCAST(qint64, y) * this->stride;
if (off < 0)
off = 0;
else if (off >= SCAST(qint64, this->buffer.size()))
off = SCAST(qint64, this->buffer.size()) - 1;
return off;
}
void
SymView::mouseMoveEvent(QMouseEvent *event)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
hoverX = event->position().x();
hoverY = event->position().y();
#else
hoverX = event->x();
hoverY = event->y();
#endif
if (this->selecting) {
qint64 end = this->coordToOffset(hoverX, hoverY);
if (end >= 0) {
this->selEnd = end;
this->invalidate();
}
}
if (this->zoom > 2)
this->invalidate();
}
void
SymView::copyToClipboard(void)
{
if (this->selStart != this->selEnd) {
qint64 start;
qint64 end;
QClipboard *clipboard = QApplication::clipboard();
QString string;
int p = 0;
if (this->selStart > this->selEnd) {
start = this->selEnd - 1;
end = this->selStart + 1;
} else {
start = this->selStart;
end = this->selEnd;
}
string.resize(SCAST(int, end - start));
for (auto i = start;
i < end;
++i)
string[p++] = QChar('0' + this->buffer[i]);
clipboard->setText(string);
}
}
void
SymView::keyPressEvent(QKeyEvent *event)
{
unsigned int lineSize = static_cast<unsigned>(this->stride);
unsigned int lineCount = static_cast<unsigned>(this->height()) / this->zoom;
unsigned int pageSize = lineSize * lineCount;
unsigned int visible = static_cast<unsigned>(this->width()) / this->zoom;
switch (event->key()) {
case Qt::Key_PageUp:
this->setOffset(
this->offset < pageSize
? 0
: this->offset - pageSize);
break;
case Qt::Key_PageDown:
if (this->getLength() > pageSize) {
this->setOffset(
static_cast<unsigned int>(
this->offset < this->getLength() - pageSize
? this->offset + pageSize
: this->getLength() - pageSize));
}
break;
case Qt::Key_Up:
this->setOffset(
this->offset < lineSize
? 0
: this->offset - lineSize);
break;
case Qt::Key_Down:
if (this->getLength() > pageSize) {
this->setOffset(
static_cast<unsigned int>(
this->offset + lineSize < this->getLength() - pageSize
? this->offset + lineSize
: this->getLength() - pageSize));
}
break;
case Qt::Key_Home:
this->setOffset(0);
break;
case Qt::Key_End:
this->setOffset(static_cast<unsigned int>(this->getLength() - pageSize));
break;
case Qt::Key_Left:
if (this->hOffset > 0)
this->setHOffset(this->hOffset - 1);
break;
case Qt::Key_Right:
if (static_cast<unsigned>(this->hOffset) + visible <= lineSize)
this->setHOffset(this->hOffset + 1);
break;
case Qt::Key_Plus:
if (event->modifiers() & Qt::ControlModifier)
this->setZoom(this->zoom + 1);
break;
case Qt::Key_Minus:
if (event->modifiers() & Qt::ControlModifier && this->zoom > 1)
this->setZoom(this->zoom - 1);
break;
case Qt::Key_Escape:
if (this->selecting) {
this->selStart = this->selEnd = 0;
this->selecting = false;
this->invalidate();
}
break;
case Qt::Key_C:
if (event->modifiers() & Qt::Modifier::CTRL)
this->copyToClipboard();
break;
case Qt::Key_A:
if (event->modifiers() & Qt::Modifier::CTRL) {
this->selStart = 0;
this->selEnd = SCAST(qint64, this->buffer.size());
this->invalidate();
}
break;
}
}
void
SymView::wheelEvent(QWheelEvent *event)
{
unsigned int lineSize = static_cast<unsigned>(this->stride);
unsigned int lineCount = static_cast<unsigned>(this->height()) / this->zoom;
unsigned int pageSize = lineSize * lineCount;
int count = (event->angleDelta().y() + 119) / 120;
if (event->modifiers() & Qt::ControlModifier) {
if (count <= 0) {
unsigned int delta = static_cast<unsigned>(-count) + 1;
this->setZoom(delta < this->zoom ? this->zoom - delta : 1);
} else {
unsigned int delta = static_cast<unsigned>(count);
this->setZoom(
this->zoom + delta > SYMVIEW_MAX_ZOOM
? SYMVIEW_MAX_ZOOM
: this->zoom + delta);
}
} else {
if (count > 0) {
unsigned int delta = static_cast<unsigned>(count);
unsigned int step = 5 * delta * lineSize * this->zoom;
this->setOffset(
this->offset < step
? 0
: this->offset - step);
} else {
unsigned int delta = static_cast<unsigned>(-count) + 1;
unsigned int step = 5 * delta * lineSize * this->zoom;
if (this->getLength() > pageSize) {
this->setOffset(
static_cast<unsigned int>(
this->offset + step < this->getLength() - pageSize
? this->offset + step
: this->getLength() - pageSize));
}
}
}
}