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"
41 const unsigned int BYTES_PER_LINE = 16;
42 const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
43 const unsigned int GAP_ADR_HEX = 10;
44 const unsigned int GAP_HEX_ASCII = 10;
45 const unsigned int GAP_ASCII_SLIDER = 5;
48 QHexView::QHexView(QWidget *parent):
49 QAbstractScrollArea(parent),
52 setFont(QFont("Courier", 10));
54 charWidth_ = fontMetrics().boundingRect('X').width();
55 charHeight_ = fontMetrics().height();
57 // Determine X coordinates of the three sub-areas
59 posHex_ = 10 * charWidth_ + GAP_ADR_HEX;
60 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
62 setFocusPolicy(Qt::StrongFocus);
65 void QHexView::setData(QByteArray *data)
67 verticalScrollBar()->setValue(0);
74 void QHexView::showFromOffset(size_t offset)
76 if (data_ && (offset < (size_t)data_->count())) {
77 setCursorPos(offset * 2);
79 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
80 verticalScrollBar() -> setValue(cursorY);
84 void QHexView::clear()
86 verticalScrollBar()->setValue(0);
90 QSize QHexView::getFullSize() const
92 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
93 GAP_ASCII_SLIDER + verticalScrollBar()->width();
96 return QSize(width, 0);
98 size_t height = data_->count() / BYTES_PER_LINE;
100 if (data_->count() % BYTES_PER_LINE)
103 height *= charHeight_;
105 return QSize(width, height);
108 void QHexView::paintEvent(QPaintEvent *event)
110 QPainter painter(viewport());
112 // Calculate and update the widget and paint area sizes
113 QSize widgetSize = getFullSize();
114 setMinimumWidth(widgetSize.width());
115 setMaximumWidth(widgetSize.width());
116 QSize areaSize = viewport()->size();
118 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
119 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
121 // Fill widget background
122 painter.fillRect(event->rect(), palette().color(QPalette::Base));
125 painter.setPen(palette().color(QPalette::Text));
126 QString s = tr("No data available");
127 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
128 int y = areaSize.height() / 2;
129 painter.drawText(x, y, s);
133 // Determine first/last line indices
134 size_t firstLineIdx = verticalScrollBar()->value();
136 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
137 if (lastLineIdx > (data_->count() / BYTES_PER_LINE)) {
138 lastLineIdx = data_->count() / BYTES_PER_LINE;
139 if (data_->count() % BYTES_PER_LINE)
143 // Fill address area background
144 painter.fillRect(QRect(posAddr_, event->rect().top(),
145 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
147 // Paint divider line between hex and ASCII areas
148 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
149 painter.setPen(palette().color(QPalette::Midlight));
150 painter.drawLine(line_x, event->rect().top(), line_x, height());
152 // Paint address area
153 painter.setPen(palette().color(QPalette::ButtonText));
155 int yStart = charHeight_;
156 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
158 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
159 painter.drawText(posAddr_, y, address);
164 QBrush regular = painter.brush();
165 QBrush selected = QBrush(palette().color(QPalette::Highlight));
166 QByteArray data = data_->mid(firstLineIdx * BYTES_PER_LINE,
167 (lastLineIdx - firstLineIdx) * BYTES_PER_LINE);
169 yStart = charHeight_;
170 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
172 painter.setBackgroundMode(Qt::OpaqueMode);
175 for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size(); i++) {
176 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
178 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
179 painter.setBackground(selected);
180 painter.setPen(palette().color(QPalette::HighlightedText));
182 painter.setBackground(regular);
183 painter.setPen(palette().color(QPalette::Text));
187 QString val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF0) >> 4, 16).toUpper();
188 painter.drawText(x, y, val);
191 val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF), 16).toUpper();
192 painter.drawText(x + charWidth_, y, val);
200 // Paint ASCII characters
201 yStart = charHeight_;
202 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
205 for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size() && (i < BYTES_PER_LINE); i++) {
206 char ch = data[(unsigned int)((lineIdx - firstLineIdx) * BYTES_PER_LINE + i)];
208 if ((ch < 0x20) || (ch > 0x7E))
211 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
212 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
213 painter.setBackground(selected);
214 painter.setPen(palette().color(QPalette::HighlightedText));
216 painter.setBackground(regular);
217 painter.setPen(palette().color(QPalette::Text));
220 painter.drawText(x, y, QString(ch));
229 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
230 int y = cursorPos_ / (2 * BYTES_PER_LINE);
232 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
233 int cursorY = y * charHeight_ + 4;
234 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
238 void QHexView::keyPressEvent(QKeyEvent *event)
240 bool setVisible = false;
243 if (event->matches(QKeySequence::MoveToNextChar)) {
244 setCursorPos(cursorPos_ + 1);
245 resetSelection(cursorPos_);
248 if (event->matches(QKeySequence::MoveToPreviousChar)) {
249 setCursorPos(cursorPos_ - 1);
250 resetSelection(cursorPos_);
254 if (event->matches(QKeySequence::MoveToEndOfLine)) {
255 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
256 resetSelection(cursorPos_);
259 if (event->matches(QKeySequence::MoveToStartOfLine)) {
260 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
261 resetSelection(cursorPos_);
264 if (event->matches(QKeySequence::MoveToPreviousLine)) {
265 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
266 resetSelection(cursorPos_);
269 if (event->matches(QKeySequence::MoveToNextLine)) {
270 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
271 resetSelection(cursorPos_);
275 if (event->matches(QKeySequence::MoveToNextPage)) {
276 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
277 resetSelection(cursorPos_);
280 if (event->matches(QKeySequence::MoveToPreviousPage)) {
281 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
282 resetSelection(cursorPos_);
285 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
287 setCursorPos(data_->count() * 2);
288 resetSelection(cursorPos_);
291 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
293 resetSelection(cursorPos_);
298 if (event->matches(QKeySequence::SelectAll)) {
301 setSelection(2 * data_->count() + 1);
304 if (event->matches(QKeySequence::SelectNextChar)) {
305 int pos = cursorPos_ + 1;
310 if (event->matches(QKeySequence::SelectPreviousChar)) {
311 int pos = cursorPos_ - 1;
316 if (event->matches(QKeySequence::SelectEndOfLine)) {
317 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
322 if (event->matches(QKeySequence::SelectStartOfLine)) {
323 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
328 if (event->matches(QKeySequence::SelectPreviousLine)) {
329 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
334 if (event->matches(QKeySequence::SelectNextLine)) {
335 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
341 if (event->matches(QKeySequence::SelectNextPage)) {
342 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
347 if (event->matches(QKeySequence::SelectPreviousPage)) {
348 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
353 if (event->matches(QKeySequence::SelectEndOfDocument)) {
356 pos = data_->count() * 2;
361 if (event->matches(QKeySequence::SelectStartOfDocument)) {
368 if (event->matches(QKeySequence::Copy) && (data_)) {
373 QByteArray data = data_->mid(selectBegin_ / 2, (selectEnd_ - selectBegin_) / 2 + 1);
374 if (selectBegin_ % 2) {
375 text += QString::number((data.at((idx+1) / 2) & 0xF), 16);
381 int selectedSize = selectEnd_ - selectBegin_;
382 while (idx < selectedSize) {
383 QString val = QString::number((data.at((copyOffset + idx) / 2) & 0xF0) >> 4, 16);
384 if ((idx + 1) < selectedSize) {
385 val += QString::number((data.at((copyOffset + idx) / 2) & 0xF), 16);
390 if ((idx/2) % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
396 QClipboard *clipboard = QApplication::clipboard();
397 clipboard->setText(text);
398 if (clipboard->supportsSelection())
399 clipboard->setText(text);
405 viewport()->update();
408 void QHexView::mouseMoveEvent(QMouseEvent *event)
410 int actPos = cursorPosFromMousePos(event->pos());
411 setCursorPos(actPos);
412 setSelection(actPos);
414 viewport()->update();
417 void QHexView::mousePressEvent(QMouseEvent *event)
419 int cPos = cursorPosFromMousePos(event->pos());
421 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
424 resetSelection(cPos);
428 viewport()->update();
431 size_t QHexView::cursorPosFromMousePos(const QPoint &position)
435 if (((size_t)position.x() >= posHex_) &&
436 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
438 // Note: We add 1.5 character widths so that selection across
439 // byte gaps is smoother
440 int x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
442 // Note: We allow only full bytes to be selected, not nibbles,
443 // so we round to the nearest byte gap
446 int firstLineIdx = verticalScrollBar()->value();
447 int y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
448 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
454 void QHexView::resetSelection()
456 selectBegin_ = selectInit_;
457 selectEnd_ = selectInit_;
460 void QHexView::resetSelection(int pos)
470 void QHexView::setSelection(int pos)
475 if ((size_t)pos >= selectInit_) {
477 selectBegin_ = selectInit_;
480 selectEnd_ = selectInit_;
484 void QHexView::setCursorPos(int position)
491 maxPos = data_->count() * 2;
492 if (data_->count() % BYTES_PER_LINE)
496 if (position > maxPos)
499 cursorPos_ = position;
502 void QHexView::ensureVisible()
504 QSize areaSize = viewport()->size();
506 int firstLineIdx = verticalScrollBar() -> value();
507 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
509 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
511 if (cursorY < firstLineIdx)
512 verticalScrollBar() -> setValue(cursorY);
513 else if(cursorY >= lastLineIdx)
514 verticalScrollBar() -> setValue(cursorY - areaSize.height() / charHeight_ + 1);