2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Victor Anjin <virinext@gmail.com>
5 * Copyright (C) 2019 Soeren Apel <soeren@apelpie.net>
7 * The MIT License (MIT)
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:
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
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
32 #include <QApplication>
41 #include <QPaintEvent>
43 #include "QHexView.hpp"
47 const unsigned int BYTES_PER_LINE = 16;
48 const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
49 const unsigned int GAP_ADR_HEX = 10;
50 const unsigned int GAP_HEX_ASCII = 10;
51 const unsigned int GAP_ASCII_SLIDER = 5;
54 QHexView::QHexView(QWidget *parent):
55 QAbstractScrollArea(parent),
56 mode_(ChunkedDataMode),
62 highlighted_sample_(std::numeric_limits<uint64_t>::max())
64 setFont(QFont("Courier", 10));
66 charWidth_ = fontMetrics().boundingRect('X').width();
67 charHeight_ = fontMetrics().height();
69 setFocusPolicy(Qt::StrongFocus);
71 if (palette().color(QPalette::ButtonText).toHsv().value() > 127) {
73 chunk_colors_.emplace_back(100, 149, 237); // QColorConstants::Svg::cornflowerblue
74 chunk_colors_.emplace_back(60, 179, 113); // QColorConstants::Svg::mediumseagreen
75 chunk_colors_.emplace_back(210, 180, 140); // QColorConstants::Svg::tan
76 visible_range_color_ = QColor("#fff5ee"); // QColorConstants::Svg::seashell
79 chunk_colors_.emplace_back(0, 0, 139); // QColorConstants::Svg::darkblue
80 chunk_colors_.emplace_back(34, 139, 34); // QColorConstants::Svg::forestgreen
81 chunk_colors_.emplace_back(160, 82, 45); // QColorConstants::Svg::sienna
82 visible_range_color_ = QColor("#fff5ee"); // QColorConstants::Svg::seashell
86 void QHexView::set_mode(Mode m)
90 // This is not expected to be set when data is showing,
91 // so we don't update the viewport here
94 void QHexView::set_data(const DecodeBinaryClass* data)
100 size_t chunks = data_->chunks.size();
101 for (size_t i = 0; i < chunks; i++)
102 size += data_->chunks[i].data.size();
106 address_digits_ = (uint8_t)QString::number(data_size_, 16).length();
108 // Calculate X coordinates of the three sub-areas
110 posHex_ = address_digits_ * charWidth_ + GAP_ADR_HEX;
111 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
113 viewport()->update();
116 void QHexView::set_visible_sample_range(uint64_t start, uint64_t end)
118 visible_range_ = make_pair(start, end);
120 viewport()->update();
123 void QHexView::set_highlighted_data_sample(uint64_t sample)
125 highlighted_sample_ = sample;
127 viewport()->update();
130 unsigned int QHexView::get_bytes_per_line() const
132 return BYTES_PER_LINE;
135 void QHexView::clear()
137 verticalScrollBar()->setValue(0);
141 highlighted_sample_ = std::numeric_limits<uint64_t>::max();
143 viewport()->update();
146 void QHexView::showFromOffset(size_t offset)
148 if (data_ && (offset < data_size_)) {
149 setCursorPos(offset * 2);
151 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
152 verticalScrollBar() -> setValue(cursorY);
155 viewport()->update();
158 QSizePolicy QHexView::sizePolicy() const
160 return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
163 pair<size_t, size_t> QHexView::get_selection() const
165 size_t start = selectBegin_ / 2;
166 size_t end = selectEnd_ / 2;
169 // Nothing is currently selected
172 } if (end < data_size_)
175 return std::make_pair(start, end);
178 size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
179 bool with_offset, bool with_ascii)
183 // Determine start address for the row
184 uint64_t row = start / BYTES_PER_LINE;
185 uint64_t offset = row * BYTES_PER_LINE;
186 end = std::min((uint64_t)end, offset + BYTES_PER_LINE);
189 dest->append(QString("%1 ").arg(row * BYTES_PER_LINE, address_digits_, 16, QChar('0')).toUpper());
191 initialize_byte_iterator(offset);
192 for (size_t i = offset; i < offset + BYTES_PER_LINE; i++) {
196 value = get_next_byte();
198 if ((i < start) || (i >= end))
201 dest->append(QString("%1 ").arg(value, 2, 16, QChar('0')).toUpper());
205 initialize_byte_iterator(offset);
206 for (size_t i = offset; i < end; i++) {
207 uint8_t value = get_next_byte();
209 if ((value < 0x20) || (value > 0x7E))
215 dest->append((char)value);
222 void QHexView::initialize_byte_iterator(size_t offset)
224 current_chunk_id_ = 0;
225 current_chunk_offset_ = 0;
226 current_offset_ = offset;
228 size_t chunks = data_->chunks.size();
229 for (size_t i = 0; i < chunks; i++) {
230 size_t size = data_->chunks[i].data.size();
232 if (offset >= size) {
236 current_chunk_offset_ = offset;
241 if (current_chunk_id_ < data_->chunks.size())
242 current_chunk_ = data_->chunks[current_chunk_id_];
244 current_chunk_sample_ = current_chunk_.sample;
246 // Obtain sample of next chunk if there is one
247 if ((current_chunk_id_ + 1) < data_->chunks.size())
248 next_chunk_sample_ = data_->chunks[current_chunk_id_ + 1].sample;
250 next_chunk_sample_ = std::numeric_limits<uint64_t>::max();
253 uint8_t QHexView::get_next_byte(bool* is_new_chunk)
255 if (is_new_chunk != nullptr)
256 *is_new_chunk = (current_chunk_offset_ == 0);
259 if (current_chunk_offset_ < current_chunk_.data.size())
260 v = current_chunk_.data[current_chunk_offset_];
262 current_chunk_sample_ = current_chunk_.sample;
265 // Obtain sample of next chunk if there is one
266 if ((current_chunk_id_ + 1) < data_->chunks.size())
267 next_chunk_sample_ = data_->chunks[current_chunk_id_ + 1].sample;
269 next_chunk_sample_ = std::numeric_limits<uint64_t>::max();
273 current_chunk_offset_++;
275 if (current_offset_ > data_size_) {
276 qWarning() << "QHexView::get_next_byte() overran binary data boundary:" <<
277 current_offset_ << "of" << data_size_ << "bytes";
281 if ((current_chunk_offset_ == current_chunk_.data.size()) && (current_offset_ < data_size_)) {
283 current_chunk_offset_ = 0;
284 current_chunk_ = data_->chunks[current_chunk_id_];
290 QSize QHexView::getFullSize() const
292 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_);
294 if (verticalScrollBar()->isEnabled())
295 width += GAP_ASCII_SLIDER + verticalScrollBar()->width();
297 if (!data_ || (data_size_ == 0))
298 return QSize(width, 0);
300 size_t height = data_size_ / BYTES_PER_LINE;
302 if (data_size_ % BYTES_PER_LINE)
305 height *= charHeight_;
307 return QSize(width, height);
310 void QHexView::paintEvent(QPaintEvent *event)
312 QPainter painter(viewport());
314 QFont normal_font = painter.font();
315 QFont bold_font = painter.font();
316 bold_font.setWeight(QFont::Bold);
318 bool bold_font_was_used = false;
320 // Calculate and update the widget and paint area sizes
321 QSize widgetSize = getFullSize();
322 setMinimumWidth(widgetSize.width());
323 setMaximumWidth(widgetSize.width());
324 QSize areaSize = viewport()->size() - QSize(0, charHeight_);
326 // Only show scrollbar if the content goes beyond the visible area
327 if (widgetSize.height() > areaSize.height()) {
328 verticalScrollBar()->setEnabled(true);
329 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
330 verticalScrollBar()->setRange(0, ((widgetSize.height() - areaSize.height())) / charHeight_ + 1);
332 verticalScrollBar()->setEnabled(false);
334 // Fill widget background
335 painter.fillRect(event->rect(), palette().color(QPalette::Base));
337 if (!data_ || (data_size_ == 0) || (data_->chunks.empty())) {
338 painter.setPen(palette().color(QPalette::Text));
339 QString s = tr("No data available");
340 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
341 int y = areaSize.height() / 2;
342 painter.drawText(x, y, s);
346 // Determine first/last line indices
347 size_t firstLineIdx = verticalScrollBar()->value();
349 size_t lastLineIdx = firstLineIdx + (areaSize.height() / charHeight_);
350 if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
351 lastLineIdx = data_size_ / BYTES_PER_LINE;
352 if (data_size_ % BYTES_PER_LINE)
356 // Paint divider line between hex and ASCII areas
357 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
358 painter.setPen(palette().color(QPalette::Midlight));
359 painter.drawLine(line_x, event->rect().top(), line_x, height());
361 // Fill address area background
362 painter.fillRect(QRect(posAddr_, event->rect().top(),
363 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
364 painter.fillRect(QRect(posAddr_, event->rect().top(),
365 posAscii_ - (GAP_HEX_ASCII / 2), charHeight_ + 2), palette().color(QPalette::Window));
367 // Paint address area
368 painter.setPen(palette().color(QPalette::ButtonText));
370 int yStart = 2 * charHeight_;
371 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
373 QString address = QString("%1").arg(lineIdx * 16, address_digits_, 16, QChar('0')).toUpper();
374 painter.drawText(posAddr_, y, address);
378 // Paint top row with hex offsets
379 painter.setPen(palette().color(QPalette::ButtonText));
380 for (int offset = 0; offset <= 0xF; offset++)
381 painter.drawText(posHex_ + (1 + offset * 3) * charWidth_,
382 charHeight_ - 3, QString::number(offset, 16).toUpper());
385 QBrush regular_brush = palette().buttonText();
386 QBrush selected_brush = palette().highlight();
387 QBrush visible_range_brush = QBrush(visible_range_color_);
389 bool multiple_chunks = (data_->chunks.size() > 1);
390 unsigned int chunk_color = 0;
392 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
394 yStart = 2 * charHeight_;
395 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
398 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
399 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
403 uint8_t byte_value = get_next_byte(&is_new_chunk);
407 if (chunk_color == chunk_colors_.size())
410 // New chunk means also new chunk sample, so check for required changes
411 if (bold_font_was_used)
412 painter.setFont(normal_font);
413 if ((highlighted_sample_ >= current_chunk_sample_) && (highlighted_sample_ < next_chunk_sample_)) {
414 painter.setFont(bold_font);
415 bold_font_was_used = true;
419 // Restore default paint style
420 painter.setBackground(regular_brush);
421 painter.setBackgroundMode(Qt::TransparentMode);
422 if (!multiple_chunks)
423 painter.setPen(palette().color(QPalette::Text));
425 painter.setPen(chunk_colors_[chunk_color]);
427 // Highlight needed because it's the range visible in main view?
428 if ((current_chunk_sample_ >= visible_range_.first) && (current_chunk_sample_ < visible_range_.second)) {
429 painter.setBackgroundMode(Qt::OpaqueMode);
430 painter.setBackground(visible_range_brush);
433 // Highlight for selection range needed? (takes priority over visible range highlight)
434 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
435 painter.setBackgroundMode(Qt::OpaqueMode);
436 painter.setBackground(selected_brush);
437 painter.setPen(palette().color(QPalette::HighlightedText));
441 QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper();
442 painter.drawText(x, y, val);
445 val = QString::number((byte_value & 0xF), 16).toUpper();
446 painter.drawText(x + charWidth_, y, val);
448 if ((i < BYTES_PER_LINE - 1) && (current_offset_ < data_size_))
449 painter.drawText(x + 2 * charWidth_, y, QString(' '));
457 // Paint ASCII characters
458 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
459 yStart = 2 * charHeight_;
460 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
463 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
466 uint8_t ch = get_next_byte(&is_new_chunk);
469 // New chunk means also new chunk sample, so check for required changes
470 if (bold_font_was_used)
471 painter.setFont(normal_font);
472 if ((highlighted_sample_ >= current_chunk_sample_) && (highlighted_sample_ < next_chunk_sample_)) {
473 painter.setFont(bold_font);
474 bold_font_was_used = true;
478 if ((ch < 0x20) || (ch > 0x7E))
481 // Restore default paint style
482 painter.setBackgroundMode(Qt::TransparentMode);
483 painter.setBackground(regular_brush);
484 painter.setPen(palette().color(QPalette::Text));
486 // Highlight needed because it's the range visible in main view?
487 if ((current_chunk_sample_ >= visible_range_.first) && (current_chunk_sample_ < visible_range_.second)) {
488 painter.setBackgroundMode(Qt::OpaqueMode);
489 painter.setBackground(visible_range_brush);
492 // Highlight for selection range needed? (takes priority over visible range highlight)
493 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
494 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
495 painter.setBackgroundMode(Qt::OpaqueMode);
496 painter.setBackground(selected_brush);
497 painter.setPen(palette().color(QPalette::HighlightedText));
500 painter.drawText(x, y, QString(ch));
507 // Restore painter defaults
508 painter.setBackgroundMode(Qt::TransparentMode);
509 painter.setBackground(regular_brush);
513 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
514 int y = cursorPos_ / (2 * BYTES_PER_LINE);
516 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
517 int cursorY = charHeight_ + y * charHeight_ + 4;
518 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
522 void QHexView::keyPressEvent(QKeyEvent *event)
524 bool setVisible = false;
527 if (event->matches(QKeySequence::MoveToNextChar)) {
528 setCursorPos(cursorPos_ + 1);
529 resetSelection(cursorPos_);
532 if (event->matches(QKeySequence::MoveToPreviousChar)) {
533 setCursorPos(cursorPos_ - 1);
534 resetSelection(cursorPos_);
538 if (event->matches(QKeySequence::MoveToEndOfLine)) {
539 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
540 resetSelection(cursorPos_);
543 if (event->matches(QKeySequence::MoveToStartOfLine)) {
544 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
545 resetSelection(cursorPos_);
548 if (event->matches(QKeySequence::MoveToPreviousLine)) {
549 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
550 resetSelection(cursorPos_);
553 if (event->matches(QKeySequence::MoveToNextLine)) {
554 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
555 resetSelection(cursorPos_);
559 if (event->matches(QKeySequence::MoveToNextPage)) {
560 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
561 resetSelection(cursorPos_);
564 if (event->matches(QKeySequence::MoveToPreviousPage)) {
565 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
566 resetSelection(cursorPos_);
569 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
570 setCursorPos(data_size_ * 2);
571 resetSelection(cursorPos_);
574 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
576 resetSelection(cursorPos_);
581 if (event->matches(QKeySequence::SelectAll)) {
583 setSelection(2 * data_size_);
586 if (event->matches(QKeySequence::SelectNextChar)) {
587 int pos = cursorPos_ + 1;
592 if (event->matches(QKeySequence::SelectPreviousChar)) {
593 int pos = cursorPos_ - 1;
598 if (event->matches(QKeySequence::SelectEndOfLine)) {
599 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
604 if (event->matches(QKeySequence::SelectStartOfLine)) {
605 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
610 if (event->matches(QKeySequence::SelectPreviousLine)) {
611 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
616 if (event->matches(QKeySequence::SelectNextLine)) {
617 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
623 if (event->matches(QKeySequence::SelectNextPage)) {
624 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
629 if (event->matches(QKeySequence::SelectPreviousPage)) {
630 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
635 if (event->matches(QKeySequence::SelectEndOfDocument)) {
636 int pos = data_size_ * 2;
641 if (event->matches(QKeySequence::SelectStartOfDocument)) {
647 if (event->matches(QKeySequence::Copy) && (data_)) {
650 initialize_byte_iterator(selectBegin_ / 2);
652 size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2;
653 for (size_t i = 0; i < selectedSize; i++) {
654 uint8_t byte_value = get_next_byte();
656 QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() +
657 QString::number((byte_value & 0xF), 16).toUpper() + " ";
660 if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
664 QClipboard *clipboard = QApplication::clipboard();
665 clipboard->setText(text, QClipboard::Clipboard);
666 if (clipboard->supportsSelection())
667 clipboard->setText(text, QClipboard::Selection);
673 viewport()->update();
676 void QHexView::mouseMoveEvent(QMouseEvent *event)
678 int actPos = cursorPosFromMousePos(event->pos());
679 setCursorPos(actPos);
680 setSelection(actPos);
682 viewport()->update();
685 void QHexView::mousePressEvent(QMouseEvent *event)
687 int cPos = cursorPosFromMousePos(event->pos());
689 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
692 resetSelection(cPos);
696 viewport()->update();
699 size_t QHexView::cursorPosFromMousePos(const QPoint &position)
703 if (((size_t)position.x() >= posHex_) &&
704 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
706 // Note: We add 1.5 character widths so that selection across
707 // byte gaps is smoother
708 size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
710 // Note: We allow only full bytes to be selected, not nibbles,
711 // so we round to the nearest byte gap
714 size_t firstLineIdx = verticalScrollBar()->value();
715 size_t y = ((position.y() / charHeight_) - 1) * 2 * BYTES_PER_LINE;
716 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
719 size_t max_pos = data_size_ * 2;
721 return std::min(pos, max_pos);
724 void QHexView::resetSelection()
726 selectBegin_ = selectInit_;
727 selectEnd_ = selectInit_;
730 void QHexView::resetSelection(int pos)
740 void QHexView::setSelection(int pos)
745 if ((size_t)pos >= selectInit_) {
747 selectBegin_ = selectInit_;
750 selectEnd_ = selectInit_;
754 void QHexView::setCursorPos(int position)
759 int max_pos = data_size_ * 2;
761 if (position > max_pos)
764 cursorPos_ = position;
767 void QHexView::ensureVisible()
769 QSize areaSize = viewport()->size();
771 int firstLineIdx = verticalScrollBar()->value();
772 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
774 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
776 if (cursorY < firstLineIdx)
777 verticalScrollBar()->setValue(cursorY);
779 if(cursorY >= lastLineIdx)
780 verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1);