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
30 #include <QApplication>
38 #include <QPaintEvent>
40 #include "QHexView.hpp"
42 const unsigned int BYTES_PER_LINE = 16;
43 const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
44 const unsigned int GAP_ADR_HEX = 10;
45 const unsigned int GAP_HEX_ASCII = 10;
46 const unsigned int GAP_ASCII_SLIDER = 5;
49 QHexView::QHexView(QWidget *parent):
50 QAbstractScrollArea(parent),
51 mode_(ChunkedDataMode),
57 setFont(QFont("Courier", 10));
59 charWidth_ = fontMetrics().boundingRect('X').width();
60 charHeight_ = fontMetrics().height();
62 // Determine X coordinates of the three sub-areas
64 posHex_ = 10 * charWidth_ + GAP_ADR_HEX;
65 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
67 setFocusPolicy(Qt::StrongFocus);
69 if (palette().color(QPalette::ButtonText).toHsv().value() > 127) {
71 chunk_colors_.emplace_back(100, 149, 237); // QColorConstants::Svg::cornflowerblue
72 chunk_colors_.emplace_back(60, 179, 113); // QColorConstants::Svg::mediumseagreen
73 chunk_colors_.emplace_back(210, 180, 140); // QColorConstants::Svg::tan
76 chunk_colors_.emplace_back(0, 0, 139); // QColorConstants::Svg::darkblue
77 chunk_colors_.emplace_back(34, 139, 34); // QColorConstants::Svg::forestgreen
78 chunk_colors_.emplace_back(160, 82, 45); // QColorConstants::Svg::sienna
82 void QHexView::setMode(Mode m)
86 // This is not expected to be set when data is showing,
87 // so we don't update the viewport here
90 void QHexView::setData(const DecodeBinaryClass* data)
95 size_t chunks = data_->chunks.size();
96 for (size_t i = 0; i < chunks; i++)
97 size += data_->chunks[i].data.size();
100 viewport()->update();
103 void QHexView::clear()
105 verticalScrollBar()->setValue(0);
109 viewport()->update();
112 void QHexView::showFromOffset(size_t offset)
114 if (data_ && (offset < data_size_)) {
115 setCursorPos(offset * 2);
117 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
118 verticalScrollBar() -> setValue(cursorY);
121 viewport()->update();
124 void QHexView::initialize_byte_iterator(size_t offset)
126 current_chunk_id_ = 0;
127 current_chunk_offset_ = 0;
128 current_offset_ = offset;
130 size_t chunks = data_->chunks.size();
131 for (size_t i = 0; i < chunks; i++) {
132 size_t size = data_->chunks[i].data.size();
134 if (offset >= size) {
138 current_chunk_offset_ = offset;
143 if (current_chunk_id_ < data_->chunks.size())
144 current_chunk_ = data_->chunks[current_chunk_id_];
147 uint8_t QHexView::get_next_byte(bool* is_next_chunk)
149 if (is_next_chunk != nullptr)
150 *is_next_chunk = (current_chunk_offset_ == 0);
153 if (current_chunk_offset_ < current_chunk_.data.size())
154 v = current_chunk_.data[current_chunk_offset_];
157 current_chunk_offset_++;
159 if (current_offset_ > data_size_) {
160 qWarning() << "QHexView::get_next_byte() overran binary data boundary:" <<
161 current_offset_ << "of" << data_size_ << "bytes";
165 if ((current_chunk_offset_ == current_chunk_.data.size()) && (current_offset_ < data_size_)) {
167 current_chunk_offset_ = 0;
168 current_chunk_ = data_->chunks[current_chunk_id_];
174 QSize QHexView::getFullSize() const
176 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
177 GAP_ASCII_SLIDER + verticalScrollBar()->width();
180 return QSize(width, 0);
182 size_t height = data_size_ / BYTES_PER_LINE;
184 if (data_size_ % BYTES_PER_LINE)
187 height *= charHeight_;
189 return QSize(width, height);
192 void QHexView::paintEvent(QPaintEvent *event)
194 QPainter painter(viewport());
196 // Calculate and update the widget and paint area sizes
197 QSize widgetSize = getFullSize();
198 setMinimumWidth(widgetSize.width());
199 setMaximumWidth(widgetSize.width());
200 QSize areaSize = viewport()->size();
202 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
203 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
205 // Fill widget background
206 painter.fillRect(event->rect(), palette().color(QPalette::Base));
208 if (!data_ || (data_size_ == 0)) {
209 painter.setPen(palette().color(QPalette::Text));
210 QString s = tr("No data available");
211 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
212 int y = areaSize.height() / 2;
213 painter.drawText(x, y, s);
217 // Determine first/last line indices
218 size_t firstLineIdx = verticalScrollBar()->value();
220 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
221 if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
222 lastLineIdx = data_size_ / BYTES_PER_LINE;
223 if (data_size_ % BYTES_PER_LINE)
227 // Fill address area background
228 painter.fillRect(QRect(posAddr_, event->rect().top(),
229 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
231 // Paint divider line between hex and ASCII areas
232 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
233 painter.setPen(palette().color(QPalette::Midlight));
234 painter.drawLine(line_x, event->rect().top(), line_x, height());
236 // Paint address area
237 painter.setPen(palette().color(QPalette::ButtonText));
239 int yStart = charHeight_;
240 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
242 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
243 painter.drawText(posAddr_, y, address);
248 QBrush regular = palette().buttonText();
249 QBrush selected = palette().highlight();
251 bool multiple_chunks = (data_->chunks.size() > 1);
252 unsigned int chunk_color = 0;
254 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
255 yStart = charHeight_;
256 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
259 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
260 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
264 uint8_t byte_value = get_next_byte(&is_next_chunk);
268 if (chunk_color == chunk_colors_.size())
272 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
273 painter.setBackgroundMode(Qt::OpaqueMode);
274 painter.setBackground(selected);
275 painter.setPen(palette().color(QPalette::HighlightedText));
277 painter.setBackground(regular);
278 painter.setBackgroundMode(Qt::TransparentMode);
279 if (!multiple_chunks)
280 painter.setPen(palette().color(QPalette::Text));
282 painter.setPen(chunk_colors_[chunk_color]);
286 QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper();
287 painter.drawText(x, y, val);
290 val = QString::number((byte_value & 0xF), 16).toUpper();
291 painter.drawText(x + charWidth_, y, val);
299 // Paint ASCII characters
300 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
301 yStart = charHeight_;
302 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
305 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
307 uint8_t ch = get_next_byte();
309 if ((ch < 0x20) || (ch > 0x7E))
312 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
313 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
314 painter.setBackgroundMode(Qt::OpaqueMode);
315 painter.setBackground(selected);
316 painter.setPen(palette().color(QPalette::HighlightedText));
318 painter.setBackgroundMode(Qt::TransparentMode);
319 painter.setBackground(regular);
320 painter.setPen(palette().color(QPalette::Text));
323 painter.drawText(x, y, QString(ch));
332 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
333 int y = cursorPos_ / (2 * BYTES_PER_LINE);
335 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
336 int cursorY = y * charHeight_ + 4;
337 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
341 void QHexView::keyPressEvent(QKeyEvent *event)
343 bool setVisible = false;
346 if (event->matches(QKeySequence::MoveToNextChar)) {
347 setCursorPos(cursorPos_ + 1);
348 resetSelection(cursorPos_);
351 if (event->matches(QKeySequence::MoveToPreviousChar)) {
352 setCursorPos(cursorPos_ - 1);
353 resetSelection(cursorPos_);
357 if (event->matches(QKeySequence::MoveToEndOfLine)) {
358 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
359 resetSelection(cursorPos_);
362 if (event->matches(QKeySequence::MoveToStartOfLine)) {
363 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
364 resetSelection(cursorPos_);
367 if (event->matches(QKeySequence::MoveToPreviousLine)) {
368 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
369 resetSelection(cursorPos_);
372 if (event->matches(QKeySequence::MoveToNextLine)) {
373 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
374 resetSelection(cursorPos_);
378 if (event->matches(QKeySequence::MoveToNextPage)) {
379 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
380 resetSelection(cursorPos_);
383 if (event->matches(QKeySequence::MoveToPreviousPage)) {
384 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
385 resetSelection(cursorPos_);
388 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
389 setCursorPos(data_size_ * 2);
390 resetSelection(cursorPos_);
393 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
395 resetSelection(cursorPos_);
400 if (event->matches(QKeySequence::SelectAll)) {
402 setSelection(2 * data_size_);
405 if (event->matches(QKeySequence::SelectNextChar)) {
406 int pos = cursorPos_ + 1;
411 if (event->matches(QKeySequence::SelectPreviousChar)) {
412 int pos = cursorPos_ - 1;
417 if (event->matches(QKeySequence::SelectEndOfLine)) {
418 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
423 if (event->matches(QKeySequence::SelectStartOfLine)) {
424 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
429 if (event->matches(QKeySequence::SelectPreviousLine)) {
430 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
435 if (event->matches(QKeySequence::SelectNextLine)) {
436 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
442 if (event->matches(QKeySequence::SelectNextPage)) {
443 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
448 if (event->matches(QKeySequence::SelectPreviousPage)) {
449 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
454 if (event->matches(QKeySequence::SelectEndOfDocument)) {
455 int pos = data_size_ * 2;
460 if (event->matches(QKeySequence::SelectStartOfDocument)) {
466 if (event->matches(QKeySequence::Copy) && (data_)) {
469 initialize_byte_iterator(selectBegin_ / 2);
471 size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2;
472 for (size_t i = 0; i < selectedSize; i++) {
473 uint8_t byte_value = get_next_byte();
475 QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() +
476 QString::number((byte_value & 0xF), 16).toUpper() + " ";
479 if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
483 QClipboard *clipboard = QApplication::clipboard();
484 clipboard->setText(text, QClipboard::Clipboard);
485 if (clipboard->supportsSelection())
486 clipboard->setText(text, QClipboard::Selection);
492 viewport()->update();
495 void QHexView::mouseMoveEvent(QMouseEvent *event)
497 int actPos = cursorPosFromMousePos(event->pos());
498 setCursorPos(actPos);
499 setSelection(actPos);
501 viewport()->update();
504 void QHexView::mousePressEvent(QMouseEvent *event)
506 int cPos = cursorPosFromMousePos(event->pos());
508 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
511 resetSelection(cPos);
515 viewport()->update();
518 size_t QHexView::cursorPosFromMousePos(const QPoint &position)
522 if (((size_t)position.x() >= posHex_) &&
523 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
525 // Note: We add 1.5 character widths so that selection across
526 // byte gaps is smoother
527 size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
529 // Note: We allow only full bytes to be selected, not nibbles,
530 // so we round to the nearest byte gap
533 size_t firstLineIdx = verticalScrollBar()->value();
534 size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
535 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
538 size_t max_pos = data_size_ * 2;
540 return std::min(pos, max_pos);
543 void QHexView::resetSelection()
545 selectBegin_ = selectInit_;
546 selectEnd_ = selectInit_;
549 void QHexView::resetSelection(int pos)
559 void QHexView::setSelection(int pos)
564 if ((size_t)pos >= selectInit_) {
566 selectBegin_ = selectInit_;
569 selectEnd_ = selectInit_;
573 void QHexView::setCursorPos(int position)
578 int max_pos = data_size_ * 2;
580 if (position > max_pos)
583 cursorPos_ = position;
586 void QHexView::ensureVisible()
588 QSize areaSize = viewport()->size();
590 int firstLineIdx = verticalScrollBar()->value();
591 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
593 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
595 if (cursorY < firstLineIdx)
596 verticalScrollBar()->setValue(cursorY);
598 if(cursorY >= lastLineIdx)
599 verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1);