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>
37 #include <QPaintEvent>
39 #include "QHexView.hpp"
43 const unsigned int BYTES_PER_LINE = 16;
44 const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
45 const unsigned int GAP_ADR_HEX = 10;
46 const unsigned int GAP_HEX_ASCII = 10;
47 const unsigned int GAP_ASCII_SLIDER = 5;
49 DataStorageArray::DataStorageArray(const QByteArray &arr)
54 QByteArray DataStorageArray::getData(size_t position, size_t length)
56 return data_.mid(position, length);
59 size_t DataStorageArray::size()
66 QHexView::QHexView(QWidget *parent):
67 QAbstractScrollArea(parent),
70 setFont(QFont("Courier", 10));
72 charWidth_ = fontMetrics().boundingRect('X').width();
73 charHeight_ = fontMetrics().height();
75 // Determine X coordinates of the three sub-areas
77 posHex_ = 10 * charWidth_ + GAP_ADR_HEX;
78 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
80 setFocusPolicy(Qt::StrongFocus);
89 void QHexView::setData(DataStorage *pData)
91 verticalScrollBar()->setValue(0);
101 void QHexView::showFromOffset(size_t offset)
103 if (pdata_ && (offset < pdata_->size())) {
104 setCursorPos(offset * 2);
106 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
107 verticalScrollBar() -> setValue(cursorY);
111 void QHexView::clear()
113 verticalScrollBar()->setValue(0);
116 QSize QHexView::getFullSize() const
121 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
122 GAP_ASCII_SLIDER + verticalScrollBar()->width();
124 size_t height = pdata_->size() / BYTES_PER_LINE;
126 if (pdata_->size() % BYTES_PER_LINE)
129 height *= charHeight_;
131 return QSize(width, height);
134 void QHexView::paintEvent(QPaintEvent *event)
139 QPainter painter(viewport());
141 // Calculate and update the widget and paint area sizes
142 QSize areaSize = viewport()->size();
143 QSize widgetSize = getFullSize();
144 setMinimumWidth(widgetSize.width());
145 setMaximumWidth(widgetSize.width());
147 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
148 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
150 size_t firstLineIdx = verticalScrollBar()->value();
152 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
153 if (lastLineIdx > (pdata_->size() / BYTES_PER_LINE)) {
154 lastLineIdx = pdata_->size() / BYTES_PER_LINE;
155 if (pdata_->size() % BYTES_PER_LINE)
159 // Fill widget and address area backgrounds
160 painter.fillRect(event->rect(), palette().color(QPalette::Base));
161 painter.fillRect(QRect(posAddr_, event->rect().top(),
162 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
164 // Paint divider line between hex and ASCII areas
165 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
166 painter.setPen(palette().color(QPalette::Midlight));
167 painter.drawLine(line_x, event->rect().top(), line_x, height());
169 // Paint address area
170 painter.setPen(palette().color(QPalette::ButtonText));
172 int yStart = charHeight_;
173 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
175 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
176 painter.drawText(posAddr_, y, address);
181 QBrush regular = painter.brush();
182 QBrush selected = QBrush(palette().color(QPalette::Highlight));
183 QByteArray data = pdata_->getData(firstLineIdx * BYTES_PER_LINE, (lastLineIdx - firstLineIdx) * BYTES_PER_LINE);
185 yStart = charHeight_;
186 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
188 painter.setBackgroundMode(Qt::OpaqueMode);
191 for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size(); i++) {
192 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
194 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
195 painter.setBackground(selected);
196 painter.setPen(palette().color(QPalette::HighlightedText));
198 painter.setBackground(regular);
199 painter.setPen(palette().color(QPalette::Text));
203 QString val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF0) >> 4, 16).toUpper();
204 painter.drawText(x, y, val);
207 val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF), 16).toUpper();
208 painter.drawText(x + charWidth_, y, val);
216 // Paint ASCII characters
217 yStart = charHeight_;
218 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
221 for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size() && (i < BYTES_PER_LINE); i++) {
222 char ch = data[(unsigned int)((lineIdx - firstLineIdx) * BYTES_PER_LINE + i)];
224 if ((ch < 0x20) || (ch > 0x7E))
227 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
228 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
229 painter.setBackground(selected);
230 painter.setPen(palette().color(QPalette::HighlightedText));
232 painter.setBackground(regular);
233 painter.setPen(palette().color(QPalette::Text));
236 painter.drawText(x, y, QString(ch));
245 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
246 int y = cursorPos_ / (2 * BYTES_PER_LINE);
248 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
249 int cursorY = y * charHeight_ + 4;
250 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
254 void QHexView::keyPressEvent(QKeyEvent *event)
256 bool setVisible = false;
259 if (event->matches(QKeySequence::MoveToNextChar)) {
260 setCursorPos(cursorPos_ + 1);
261 resetSelection(cursorPos_);
264 if (event->matches(QKeySequence::MoveToPreviousChar)) {
265 setCursorPos(cursorPos_ - 1);
266 resetSelection(cursorPos_);
270 if (event->matches(QKeySequence::MoveToEndOfLine)) {
271 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
272 resetSelection(cursorPos_);
275 if (event->matches(QKeySequence::MoveToStartOfLine)) {
276 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
277 resetSelection(cursorPos_);
280 if (event->matches(QKeySequence::MoveToPreviousLine)) {
281 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
282 resetSelection(cursorPos_);
285 if (event->matches(QKeySequence::MoveToNextLine)) {
286 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
287 resetSelection(cursorPos_);
291 if (event->matches(QKeySequence::MoveToNextPage)) {
292 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
293 resetSelection(cursorPos_);
296 if (event->matches(QKeySequence::MoveToPreviousPage)) {
297 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
298 resetSelection(cursorPos_);
301 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
303 setCursorPos(pdata_->size() * 2);
304 resetSelection(cursorPos_);
307 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
309 resetSelection(cursorPos_);
314 if (event->matches(QKeySequence::SelectAll)) {
317 setSelection(2 * pdata_->size() + 1);
320 if (event->matches(QKeySequence::SelectNextChar)) {
321 int pos = cursorPos_ + 1;
326 if (event->matches(QKeySequence::SelectPreviousChar)) {
327 int pos = cursorPos_ - 1;
332 if (event->matches(QKeySequence::SelectEndOfLine)) {
333 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
338 if (event->matches(QKeySequence::SelectStartOfLine)) {
339 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
344 if (event->matches(QKeySequence::SelectPreviousLine)) {
345 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
350 if (event->matches(QKeySequence::SelectNextLine)) {
351 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
357 if (event->matches(QKeySequence::SelectNextPage)) {
358 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
363 if (event->matches(QKeySequence::SelectPreviousPage)) {
364 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
369 if (event->matches(QKeySequence::SelectEndOfDocument)) {
372 pos = pdata_->size() * 2;
377 if (event->matches(QKeySequence::SelectStartOfDocument)) {
384 if (event->matches(QKeySequence::Copy) && (pdata_)) {
389 QByteArray data = pdata_->getData(selectBegin_ / 2, (selectEnd_ - selectBegin_) / 2 + 1);
390 if (selectBegin_ % 2) {
391 text += QString::number((data.at((idx+1) / 2) & 0xF), 16);
397 int selectedSize = selectEnd_ - selectBegin_;
398 while (idx < selectedSize) {
399 QString val = QString::number((data.at((copyOffset + idx) / 2) & 0xF0) >> 4, 16);
400 if ((idx + 1) < selectedSize) {
401 val += QString::number((data.at((copyOffset + idx) / 2) & 0xF), 16);
406 if ((idx/2) % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
412 QClipboard *clipboard = QApplication::clipboard();
413 clipboard->setText(text);
414 if (clipboard->supportsSelection())
415 clipboard->setText(text);
421 viewport()->update();
424 void QHexView::mouseMoveEvent(QMouseEvent *event)
426 int actPos = cursorPosFromMousePos(event->pos());
427 setCursorPos(actPos);
428 setSelection(actPos);
430 viewport()->update();
433 void QHexView::mousePressEvent(QMouseEvent *event)
435 int cPos = cursorPosFromMousePos(event->pos());
437 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
440 resetSelection(cPos);
444 viewport()->update();
447 size_t QHexView::cursorPosFromMousePos(const QPoint &position)
451 if (((size_t)position.x() >= posHex_) &&
452 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
454 // Note: We add 1.5 character widths so that selection across
455 // byte gaps is smoother
456 int x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
458 // Note: We allow only full bytes to be selected, not nibbles,
459 // so we round to the nearest byte gap
462 int firstLineIdx = verticalScrollBar()->value();
463 int y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
464 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
470 void QHexView::resetSelection()
472 selectBegin_ = selectInit_;
473 selectEnd_ = selectInit_;
476 void QHexView::resetSelection(int pos)
486 void QHexView::setSelection(int pos)
491 if ((size_t)pos >= selectInit_) {
493 selectBegin_ = selectInit_;
496 selectEnd_ = selectInit_;
500 void QHexView::setCursorPos(int position)
507 maxPos = pdata_->size() * 2;
508 if (pdata_->size() % BYTES_PER_LINE)
512 if (position > maxPos)
515 cursorPos_ = position;
518 void QHexView::ensureVisible()
520 QSize areaSize = viewport()->size();
522 int firstLineIdx = verticalScrollBar() -> value();
523 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
525 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
527 if (cursorY < firstLineIdx)
528 verticalScrollBar() -> setValue(cursorY);
529 else if(cursorY >= lastLineIdx)
530 verticalScrollBar() -> setValue(cursorY - areaSize.height() / charHeight_ + 1);