]> sigrok.org Git - pulseview.git/blame - pv/views/decoder_output/QHexView.cpp
Fix segfault and use bin class description
[pulseview.git] / pv / views / decoder_output / QHexView.cpp
CommitLineData
c7b76823
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 Victor Anjin <virinext@gmail.com>
5 * Copyright (C) 2019 Soeren Apel <soeren@apelpie.net>
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include <QApplication>
31#include <QClipboard>
32#include <QFont>
33#include <QKeyEvent>
34#include <QScrollBar>
35#include <QSize>
36#include <QPainter>
37#include <QPaintEvent>
38
39#include "QHexView.hpp"
40
560f8377
SA
41const unsigned int BYTES_PER_LINE = 16;
42const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
43const unsigned int GAP_ADR_HEX = 10;
44const unsigned int GAP_HEX_ASCII = 10;
45const unsigned int GAP_ASCII_SLIDER = 5;
c7b76823 46
c7b76823
SA
47
48QHexView::QHexView(QWidget *parent):
560f8377 49 QAbstractScrollArea(parent),
628b45cc
SA
50 mode_(ChunkedDataMode),
51 data_(nullptr),
52 selectBegin_(0),
53 selectEnd_(0),
54 cursorPos_(0)
c7b76823
SA
55{
56 setFont(QFont("Courier", 10));
57
560f8377 58 charWidth_ = fontMetrics().boundingRect('X').width();
c7b76823
SA
59 charHeight_ = fontMetrics().height();
60
560f8377
SA
61 // Determine X coordinates of the three sub-areas
62 posAddr_ = 0;
63 posHex_ = 10 * charWidth_ + GAP_ADR_HEX;
c7b76823
SA
64 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
65
c7b76823 66 setFocusPolicy(Qt::StrongFocus);
a61abf09
SA
67
68 if (palette().color(QPalette::ButtonText).toHsv().value() > 127) {
69 // Color is bright
70 chunk_colors_.emplace_back(100, 149, 237); // QColorConstants::Svg::cornflowerblue
71 chunk_colors_.emplace_back(60, 179, 113); // QColorConstants::Svg::mediumseagreen
72 chunk_colors_.emplace_back(210, 180, 140); // QColorConstants::Svg::tan
73 } else {
74 // Color is dark
75 chunk_colors_.emplace_back(0, 0, 139); // QColorConstants::Svg::darkblue
76 chunk_colors_.emplace_back(34, 139, 34); // QColorConstants::Svg::forestgreen
77 chunk_colors_.emplace_back(160, 82, 45); // QColorConstants::Svg::sienna
78 }
c7b76823
SA
79}
80
628b45cc 81void QHexView::setMode(Mode m)
c7b76823 82{
628b45cc
SA
83 mode_ = m;
84
85 // This is not expected to be set when data is showing,
86 // so we don't update the viewport here
87}
c7b76823 88
628b45cc
SA
89void QHexView::setData(const DecodeBinaryClass* data)
90{
01ba5ed7 91 data_ = data;
628b45cc
SA
92
93 data_size_ = 0;
94 for (const DecodeBinaryDataChunk& chunk : data_->chunks)
95 data_size_ += chunk.data.size();
96
97 viewport()->update();
98}
99
100void QHexView::clear()
101{
102 verticalScrollBar()->setValue(0);
103 data_ = nullptr;
104 data_size_ = 0;
b2b18d3a
SA
105
106 viewport()->update();
c7b76823
SA
107}
108
109void QHexView::showFromOffset(size_t offset)
110{
628b45cc 111 if (data_ && (offset < data_size_)) {
c7b76823
SA
112 setCursorPos(offset * 2);
113
114 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
115 verticalScrollBar() -> setValue(cursorY);
116 }
4b97fe09
SA
117
118 viewport()->update();
c7b76823
SA
119}
120
628b45cc 121void QHexView::initialize_byte_iterator(size_t offset)
c7b76823 122{
628b45cc
SA
123 current_chunk_id_ = 0;
124 current_chunk_offset_ = 0;
cbf428c2 125 current_offset_ = offset;
628b45cc
SA
126
127 for (const DecodeBinaryDataChunk& chunk : data_->chunks)
128 if (offset >= chunk.data.size()) {
129 current_chunk_id_++;
130 offset -= chunk.data.size();
131 } else {
132 current_chunk_offset_ = offset;
133 break;
134 }
4b97fe09 135
628b45cc
SA
136 current_chunk_ = &(data_->chunks[current_chunk_id_]);
137}
138
139uint8_t QHexView::get_next_byte(bool* is_next_chunk)
140{
141 if (is_next_chunk != nullptr)
142 *is_next_chunk = (current_chunk_offset_ == 0);
143
144 uint8_t v = current_chunk_->data[current_chunk_offset_];
145
cbf428c2 146 current_offset_++;
628b45cc 147 current_chunk_offset_++;
cbf428c2 148
628b45cc
SA
149 if (current_chunk_offset_ == current_chunk_->data.size()) {
150 current_chunk_id_++;
151 current_chunk_offset_ = 0;
152 current_chunk_ = &(data_->chunks[current_chunk_id_]);
153 }
154
155 return v;
c7b76823
SA
156}
157
560f8377 158QSize QHexView::getFullSize() const
c7b76823 159{
560f8377
SA
160 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
161 GAP_ASCII_SLIDER + verticalScrollBar()->width();
162
01ba5ed7
SA
163 if (!data_)
164 return QSize(width, 0);
560f8377 165
628b45cc 166 size_t height = data_size_ / BYTES_PER_LINE;
01ba5ed7 167
628b45cc 168 if (data_size_ % BYTES_PER_LINE)
c7b76823
SA
169 height++;
170
171 height *= charHeight_;
172
173 return QSize(width, height);
174}
175
176void QHexView::paintEvent(QPaintEvent *event)
177{
c7b76823
SA
178 QPainter painter(viewport());
179
560f8377 180 // Calculate and update the widget and paint area sizes
560f8377
SA
181 QSize widgetSize = getFullSize();
182 setMinimumWidth(widgetSize.width());
183 setMaximumWidth(widgetSize.width());
01ba5ed7 184 QSize areaSize = viewport()->size();
560f8377 185
c7b76823
SA
186 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
187 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
188
01ba5ed7
SA
189 // Fill widget background
190 painter.fillRect(event->rect(), palette().color(QPalette::Base));
191
628b45cc 192 if (!data_ || (data_size_ == 0)) {
01ba5ed7
SA
193 painter.setPen(palette().color(QPalette::Text));
194 QString s = tr("No data available");
195 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
196 int y = areaSize.height() / 2;
197 painter.drawText(x, y, s);
198 return;
199 }
200
201 // Determine first/last line indices
c7b76823
SA
202 size_t firstLineIdx = verticalScrollBar()->value();
203
204 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
628b45cc
SA
205 if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
206 lastLineIdx = data_size_ / BYTES_PER_LINE;
207 if (data_size_ % BYTES_PER_LINE)
c7b76823
SA
208 lastLineIdx++;
209 }
210
01ba5ed7 211 // Fill address area background
c7b76823 212 painter.fillRect(QRect(posAddr_, event->rect().top(),
560f8377 213 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
c7b76823 214
560f8377
SA
215 // Paint divider line between hex and ASCII areas
216 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
217 painter.setPen(palette().color(QPalette::Midlight));
218 painter.drawLine(line_x, event->rect().top(), line_x, height());
c7b76823 219
560f8377
SA
220 // Paint address area
221 painter.setPen(palette().color(QPalette::ButtonText));
c7b76823 222
560f8377
SA
223 int yStart = charHeight_;
224 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
c7b76823 225
560f8377
SA
226 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
227 painter.drawText(posAddr_, y, address);
228 y += charHeight_;
229 }
c7b76823 230
560f8377 231 // Paint hex values
a61abf09
SA
232 QBrush regular = palette().buttonText();
233 QBrush selected = palette().highlight();
234
235 bool multiple_chunks = (data_->chunks.size() > 1);
236 unsigned int chunk_color = 0;
c7b76823 237
628b45cc 238 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
560f8377
SA
239 yStart = charHeight_;
240 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
241
560f8377 242 int x = posHex_;
cbf428c2 243 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
c7b76823 244 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
c7b76823 245
a61abf09
SA
246 // Fetch byte
247 bool is_next_chunk;
248 uint8_t byte_value = get_next_byte(&is_next_chunk);
249
250 if (is_next_chunk) {
251 chunk_color++;
252 if (chunk_color == chunk_colors_.size())
253 chunk_color = 0;
254 }
255
560f8377 256 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
a61abf09 257 painter.setBackgroundMode(Qt::OpaqueMode);
c7b76823 258 painter.setBackground(selected);
560f8377 259 painter.setPen(palette().color(QPalette::HighlightedText));
c7b76823 260 } else {
560f8377 261 painter.setBackground(regular);
a61abf09
SA
262 painter.setBackgroundMode(Qt::TransparentMode);
263 if (!multiple_chunks)
264 painter.setPen(palette().color(QPalette::Text));
265 else
266 painter.setPen(chunk_colors_[chunk_color]);
c7b76823
SA
267 }
268
560f8377 269 // First nibble
628b45cc 270 QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper();
560f8377 271 painter.drawText(x, y, val);
c7b76823 272
560f8377 273 // Second nibble
628b45cc 274 val = QString::number((byte_value & 0xF), 16).toUpper();
560f8377 275 painter.drawText(x + charWidth_, y, val);
c7b76823 276
560f8377 277 x += 3 * charWidth_;
c7b76823
SA
278 }
279
560f8377
SA
280 y += charHeight_;
281 }
282
283 // Paint ASCII characters
628b45cc 284 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
560f8377
SA
285 yStart = charHeight_;
286 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
287
288 int x = posAscii_;
cbf428c2 289 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
628b45cc
SA
290 // Fetch byte
291 uint8_t ch = get_next_byte();
c7b76823 292
560f8377 293 if ((ch < 0x20) || (ch > 0x7E))
c7b76823
SA
294 ch = '.';
295
560f8377
SA
296 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
297 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
a61abf09 298 painter.setBackgroundMode(Qt::OpaqueMode);
560f8377
SA
299 painter.setBackground(selected);
300 painter.setPen(palette().color(QPalette::HighlightedText));
301 } else {
a61abf09 302 painter.setBackgroundMode(Qt::TransparentMode);
560f8377
SA
303 painter.setBackground(regular);
304 painter.setPen(palette().color(QPalette::Text));
305 }
306
307 painter.drawText(x, y, QString(ch));
308 x += charWidth_;
c7b76823
SA
309 }
310
560f8377 311 y += charHeight_;
c7b76823
SA
312 }
313
560f8377 314 // Paint cursor
c7b76823
SA
315 if (hasFocus()) {
316 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
317 int y = cursorPos_ / (2 * BYTES_PER_LINE);
318 y -= firstLineIdx;
319 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
320 int cursorY = y * charHeight_ + 4;
560f8377 321 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
c7b76823
SA
322 }
323}
324
325void QHexView::keyPressEvent(QKeyEvent *event)
326{
327 bool setVisible = false;
328
560f8377 329 // Cursor movements
c7b76823
SA
330 if (event->matches(QKeySequence::MoveToNextChar)) {
331 setCursorPos(cursorPos_ + 1);
332 resetSelection(cursorPos_);
333 setVisible = true;
334 }
335 if (event->matches(QKeySequence::MoveToPreviousChar)) {
336 setCursorPos(cursorPos_ - 1);
337 resetSelection(cursorPos_);
338 setVisible = true;
339 }
340
341 if (event->matches(QKeySequence::MoveToEndOfLine)) {
342 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
343 resetSelection(cursorPos_);
344 setVisible = true;
345 }
346 if (event->matches(QKeySequence::MoveToStartOfLine)) {
347 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
348 resetSelection(cursorPos_);
349 setVisible = true;
350 }
351 if (event->matches(QKeySequence::MoveToPreviousLine)) {
352 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
353 resetSelection(cursorPos_);
354 setVisible = true;
355 }
356 if (event->matches(QKeySequence::MoveToNextLine)) {
357 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
358 resetSelection(cursorPos_);
359 setVisible = true;
360 }
361
362 if (event->matches(QKeySequence::MoveToNextPage)) {
363 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
364 resetSelection(cursorPos_);
365 setVisible = true;
366 }
367 if (event->matches(QKeySequence::MoveToPreviousPage)) {
368 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
369 resetSelection(cursorPos_);
370 setVisible = true;
371 }
372 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
628b45cc 373 setCursorPos(data_size_ * 2);
c7b76823
SA
374 resetSelection(cursorPos_);
375 setVisible = true;
376 }
377 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
378 setCursorPos(0);
379 resetSelection(cursorPos_);
380 setVisible = true;
381 }
382
560f8377 383 // Select commands
c7b76823
SA
384 if (event->matches(QKeySequence::SelectAll)) {
385 resetSelection(0);
628b45cc 386 setSelection(2 * data_size_);
c7b76823
SA
387 setVisible = true;
388 }
389 if (event->matches(QKeySequence::SelectNextChar)) {
390 int pos = cursorPos_ + 1;
391 setCursorPos(pos);
392 setSelection(pos);
393 setVisible = true;
394 }
395 if (event->matches(QKeySequence::SelectPreviousChar)) {
396 int pos = cursorPos_ - 1;
397 setSelection(pos);
398 setCursorPos(pos);
399 setVisible = true;
400 }
401 if (event->matches(QKeySequence::SelectEndOfLine)) {
402 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
403 setCursorPos(pos);
404 setSelection(pos);
405 setVisible = true;
406 }
407 if (event->matches(QKeySequence::SelectStartOfLine)) {
408 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
409 setCursorPos(pos);
410 setSelection(pos);
411 setVisible = true;
412 }
413 if (event->matches(QKeySequence::SelectPreviousLine)) {
414 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
415 setCursorPos(pos);
416 setSelection(pos);
417 setVisible = true;
418 }
419 if (event->matches(QKeySequence::SelectNextLine)) {
420 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
421 setCursorPos(pos);
422 setSelection(pos);
423 setVisible = true;
424 }
425
426 if (event->matches(QKeySequence::SelectNextPage)) {
427 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
428 setCursorPos(pos);
429 setSelection(pos);
430 setVisible = true;
431 }
432 if (event->matches(QKeySequence::SelectPreviousPage)) {
433 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
434 setCursorPos(pos);
435 setSelection(pos);
436 setVisible = true;
437 }
438 if (event->matches(QKeySequence::SelectEndOfDocument)) {
628b45cc 439 int pos = data_size_ * 2;
c7b76823
SA
440 setCursorPos(pos);
441 setSelection(pos);
442 setVisible = true;
443 }
444 if (event->matches(QKeySequence::SelectStartOfDocument)) {
628b45cc
SA
445 setCursorPos(0);
446 setSelection(0);
c7b76823
SA
447 setVisible = true;
448 }
449
01ba5ed7 450 if (event->matches(QKeySequence::Copy) && (data_)) {
c7b76823 451 QString text;
c7b76823 452
628b45cc 453 initialize_byte_iterator(selectBegin_ / 2);
c7b76823 454
628b45cc
SA
455 size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2;
456 for (size_t i = 0; i < selectedSize; i++) {
457 uint8_t byte_value = get_next_byte();
458
459 QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() +
460 QString::number((byte_value & 0xF), 16).toUpper() + " ";
461 text += s;
c7b76823 462
628b45cc
SA
463 if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
464 text += "\n";
c7b76823
SA
465 }
466
467 QClipboard *clipboard = QApplication::clipboard();
628b45cc 468 clipboard->setText(text, QClipboard::Clipboard);
c7b76823 469 if (clipboard->supportsSelection())
628b45cc 470 clipboard->setText(text, QClipboard::Selection);
c7b76823
SA
471 }
472
473 if (setVisible)
474 ensureVisible();
475
476 viewport()->update();
477}
478
479void QHexView::mouseMoveEvent(QMouseEvent *event)
480{
560f8377 481 int actPos = cursorPosFromMousePos(event->pos());
c7b76823
SA
482 setCursorPos(actPos);
483 setSelection(actPos);
484
485 viewport()->update();
486}
487
488void QHexView::mousePressEvent(QMouseEvent *event)
489{
560f8377 490 int cPos = cursorPosFromMousePos(event->pos());
c7b76823
SA
491
492 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
493 setSelection(cPos);
494 else
495 resetSelection(cPos);
496
497 setCursorPos(cPos);
498
499 viewport()->update();
500}
501
560f8377 502size_t QHexView::cursorPosFromMousePos(const QPoint &position)
c7b76823 503{
628b45cc 504 size_t pos = -1;
c7b76823
SA
505
506 if (((size_t)position.x() >= posHex_) &&
507 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
508
560f8377
SA
509 // Note: We add 1.5 character widths so that selection across
510 // byte gaps is smoother
628b45cc 511 size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
c7b76823 512
560f8377
SA
513 // Note: We allow only full bytes to be selected, not nibbles,
514 // so we round to the nearest byte gap
515 x = (2 * x + 1) / 3;
c7b76823 516
628b45cc
SA
517 size_t firstLineIdx = verticalScrollBar()->value();
518 size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
c7b76823
SA
519 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
520 }
521
628b45cc
SA
522 size_t max_pos = data_size_ * 2;
523
524 return std::min(pos, max_pos);
c7b76823
SA
525}
526
527void QHexView::resetSelection()
528{
529 selectBegin_ = selectInit_;
530 selectEnd_ = selectInit_;
531}
532
533void QHexView::resetSelection(int pos)
534{
535 if (pos < 0)
536 pos = 0;
537
538 selectInit_ = pos;
539 selectBegin_ = pos;
540 selectEnd_ = pos;
541}
542
543void QHexView::setSelection(int pos)
544{
545 if (pos < 0)
546 pos = 0;
547
548 if ((size_t)pos >= selectInit_) {
549 selectEnd_ = pos;
550 selectBegin_ = selectInit_;
551 } else {
552 selectBegin_ = pos;
553 selectEnd_ = selectInit_;
554 }
555}
556
557void QHexView::setCursorPos(int position)
558{
559 if (position < 0)
560 position = 0;
561
628b45cc 562 int max_pos = data_size_ * 2;
c7b76823 563
628b45cc
SA
564 if (position > max_pos)
565 position = max_pos;
c7b76823
SA
566
567 cursorPos_ = position;
568}
569
570void QHexView::ensureVisible()
571{
572 QSize areaSize = viewport()->size();
573
628b45cc 574 int firstLineIdx = verticalScrollBar()->value();
c7b76823
SA
575 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
576
577 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
578
579 if (cursorY < firstLineIdx)
628b45cc
SA
580 verticalScrollBar()->setValue(cursorY);
581 else
582 if(cursorY >= lastLineIdx)
583 verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1);
c7b76823 584}