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);
86 void QHexView::clear()
88 verticalScrollBar()->setValue(0);
94 QSize QHexView::getFullSize() const
96 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
97 GAP_ASCII_SLIDER + verticalScrollBar()->width();
100 return QSize(width, 0);
102 size_t height = data_->count() / BYTES_PER_LINE;
104 if (data_->count() % BYTES_PER_LINE)
107 height *= charHeight_;
109 return QSize(width, height);
112 void QHexView::paintEvent(QPaintEvent *event)
114 QPainter painter(viewport());
116 // Calculate and update the widget and paint area sizes
117 QSize widgetSize = getFullSize();
118 setMinimumWidth(widgetSize.width());
119 setMaximumWidth(widgetSize.width());
120 QSize areaSize = viewport()->size();
122 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
123 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
125 // Fill widget background
126 painter.fillRect(event->rect(), palette().color(QPalette::Base));
128 if (!data_ || (data_->size() == 0)) {
129 painter.setPen(palette().color(QPalette::Text));
130 QString s = tr("No data available");
131 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
132 int y = areaSize.height() / 2;
133 painter.drawText(x, y, s);
137 // Determine first/last line indices
138 size_t firstLineIdx = verticalScrollBar()->value();
140 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
141 if (lastLineIdx > (data_->count() / BYTES_PER_LINE)) {
142 lastLineIdx = data_->count() / BYTES_PER_LINE;
143 if (data_->count() % BYTES_PER_LINE)
147 // Fill address area background
148 painter.fillRect(QRect(posAddr_, event->rect().top(),
149 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
151 // Paint divider line between hex and ASCII areas
152 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
153 painter.setPen(palette().color(QPalette::Midlight));
154 painter.drawLine(line_x, event->rect().top(), line_x, height());
156 // Paint address area
157 painter.setPen(palette().color(QPalette::ButtonText));
159 int yStart = charHeight_;
160 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
162 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
163 painter.drawText(posAddr_, y, address);
168 QBrush regular = painter.brush();
169 QBrush selected = QBrush(palette().color(QPalette::Highlight));
170 QByteArray data = data_->mid(firstLineIdx * BYTES_PER_LINE,
171 (lastLineIdx - firstLineIdx) * BYTES_PER_LINE);
173 yStart = charHeight_;
174 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
176 painter.setBackgroundMode(Qt::OpaqueMode);
179 for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size(); i++) {
180 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
182 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
183 painter.setBackground(selected);
184 painter.setPen(palette().color(QPalette::HighlightedText));
186 painter.setBackground(regular);
187 painter.setPen(palette().color(QPalette::Text));
191 QString val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF0) >> 4, 16).toUpper();
192 painter.drawText(x, y, val);
195 val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF), 16).toUpper();
196 painter.drawText(x + charWidth_, y, val);
204 // Paint ASCII characters
205 yStart = charHeight_;
206 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
209 for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size() && (i < BYTES_PER_LINE); i++) {
210 char ch = data[(unsigned int)((lineIdx - firstLineIdx) * BYTES_PER_LINE + i)];
212 if ((ch < 0x20) || (ch > 0x7E))
215 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
216 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
217 painter.setBackground(selected);
218 painter.setPen(palette().color(QPalette::HighlightedText));
220 painter.setBackground(regular);
221 painter.setPen(palette().color(QPalette::Text));
224 painter.drawText(x, y, QString(ch));
233 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
234 int y = cursorPos_ / (2 * BYTES_PER_LINE);
236 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
237 int cursorY = y * charHeight_ + 4;
238 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
242 void QHexView::keyPressEvent(QKeyEvent *event)
244 bool setVisible = false;
247 if (event->matches(QKeySequence::MoveToNextChar)) {
248 setCursorPos(cursorPos_ + 1);
249 resetSelection(cursorPos_);
252 if (event->matches(QKeySequence::MoveToPreviousChar)) {
253 setCursorPos(cursorPos_ - 1);
254 resetSelection(cursorPos_);
258 if (event->matches(QKeySequence::MoveToEndOfLine)) {
259 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
260 resetSelection(cursorPos_);
263 if (event->matches(QKeySequence::MoveToStartOfLine)) {
264 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
265 resetSelection(cursorPos_);
268 if (event->matches(QKeySequence::MoveToPreviousLine)) {
269 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
270 resetSelection(cursorPos_);
273 if (event->matches(QKeySequence::MoveToNextLine)) {
274 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
275 resetSelection(cursorPos_);
279 if (event->matches(QKeySequence::MoveToNextPage)) {
280 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
281 resetSelection(cursorPos_);
284 if (event->matches(QKeySequence::MoveToPreviousPage)) {
285 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
286 resetSelection(cursorPos_);
289 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
291 setCursorPos(data_->count() * 2);
292 resetSelection(cursorPos_);
295 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
297 resetSelection(cursorPos_);
302 if (event->matches(QKeySequence::SelectAll)) {
305 setSelection(2 * data_->count() + 1);
308 if (event->matches(QKeySequence::SelectNextChar)) {
309 int pos = cursorPos_ + 1;
314 if (event->matches(QKeySequence::SelectPreviousChar)) {
315 int pos = cursorPos_ - 1;
320 if (event->matches(QKeySequence::SelectEndOfLine)) {
321 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
326 if (event->matches(QKeySequence::SelectStartOfLine)) {
327 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
332 if (event->matches(QKeySequence::SelectPreviousLine)) {
333 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
338 if (event->matches(QKeySequence::SelectNextLine)) {
339 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
345 if (event->matches(QKeySequence::SelectNextPage)) {
346 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
351 if (event->matches(QKeySequence::SelectPreviousPage)) {
352 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
357 if (event->matches(QKeySequence::SelectEndOfDocument)) {
360 pos = data_->count() * 2;
365 if (event->matches(QKeySequence::SelectStartOfDocument)) {
372 if (event->matches(QKeySequence::Copy) && (data_)) {
377 QByteArray data = data_->mid(selectBegin_ / 2, (selectEnd_ - selectBegin_) / 2 + 1);
378 if (selectBegin_ % 2) {
379 text += QString::number((data.at((idx+1) / 2) & 0xF), 16);
385 int selectedSize = selectEnd_ - selectBegin_;
386 while (idx < selectedSize) {
387 QString val = QString::number((data.at((copyOffset + idx) / 2) & 0xF0) >> 4, 16);
388 if ((idx + 1) < selectedSize) {
389 val += QString::number((data.at((copyOffset + idx) / 2) & 0xF), 16);
394 if ((idx/2) % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
400 QClipboard *clipboard = QApplication::clipboard();
401 clipboard->setText(text);
402 if (clipboard->supportsSelection())
403 clipboard->setText(text);
409 viewport()->update();
412 void QHexView::mouseMoveEvent(QMouseEvent *event)
414 int actPos = cursorPosFromMousePos(event->pos());
415 setCursorPos(actPos);
416 setSelection(actPos);
418 viewport()->update();
421 void QHexView::mousePressEvent(QMouseEvent *event)
423 int cPos = cursorPosFromMousePos(event->pos());
425 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
428 resetSelection(cPos);
432 viewport()->update();
435 size_t QHexView::cursorPosFromMousePos(const QPoint &position)
439 if (((size_t)position.x() >= posHex_) &&
440 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
442 // Note: We add 1.5 character widths so that selection across
443 // byte gaps is smoother
444 int x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
446 // Note: We allow only full bytes to be selected, not nibbles,
447 // so we round to the nearest byte gap
450 int firstLineIdx = verticalScrollBar()->value();
451 int y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
452 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
458 void QHexView::resetSelection()
460 selectBegin_ = selectInit_;
461 selectEnd_ = selectInit_;
464 void QHexView::resetSelection(int pos)
474 void QHexView::setSelection(int pos)
479 if ((size_t)pos >= selectInit_) {
481 selectBegin_ = selectInit_;
484 selectEnd_ = selectInit_;
488 void QHexView::setCursorPos(int position)
495 maxPos = data_->count() * 2;
496 if (data_->count() % BYTES_PER_LINE)
500 if (position > maxPos)
503 cursorPos_ = position;
506 void QHexView::ensureVisible()
508 QSize areaSize = viewport()->size();
510 int firstLineIdx = verticalScrollBar() -> value();
511 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
513 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
515 if (cursorY < firstLineIdx)
516 verticalScrollBar() -> setValue(cursorY);
517 else if(cursorY >= lastLineIdx)
518 verticalScrollBar() -> setValue(cursorY - areaSize.height() / charHeight_ + 1);