]> sigrok.org Git - pulseview.git/blame - pv/views/decoder_output/QHexView.cpp
DecoderOutputView: Directly use DecodeBinaryClass as data source
[pulseview.git] / pv / views / decoder_output / QHexView.cpp
CommitLineData
c7b76823
SA
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 Victor Anjin <virinext@gmail.com>
5 * Copyright (C) 2019 Soeren Apel <soeren@apelpie.net>
6 *
7 * The MIT License (MIT)
8 *
9 * Copyright (c) 2015
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
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
27 * SOFTWARE.
28 */
29
30#include <QApplication>
31#include <QClipboard>
32#include <QFont>
33#include <QKeyEvent>
34#include <QScrollBar>
35#include <QSize>
36#include <QPainter>
37#include <QPaintEvent>
38
39#include "QHexView.hpp"
40
560f8377
SA
41const unsigned int BYTES_PER_LINE = 16;
42const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
43const unsigned int GAP_ADR_HEX = 10;
44const unsigned int GAP_HEX_ASCII = 10;
45const unsigned int GAP_ASCII_SLIDER = 5;
c7b76823 46
c7b76823
SA
47
48QHexView::QHexView(QWidget *parent):
560f8377 49 QAbstractScrollArea(parent),
628b45cc
SA
50 mode_(ChunkedDataMode),
51 data_(nullptr),
52 selectBegin_(0),
53 selectEnd_(0),
54 cursorPos_(0)
c7b76823
SA
55{
56 setFont(QFont("Courier", 10));
57
560f8377 58 charWidth_ = fontMetrics().boundingRect('X').width();
c7b76823
SA
59 charHeight_ = fontMetrics().height();
60
560f8377
SA
61 // Determine X coordinates of the three sub-areas
62 posAddr_ = 0;
63 posHex_ = 10 * charWidth_ + GAP_ADR_HEX;
c7b76823
SA
64 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
65
c7b76823
SA
66 setFocusPolicy(Qt::StrongFocus);
67}
68
628b45cc 69void QHexView::setMode(Mode m)
c7b76823 70{
628b45cc
SA
71 mode_ = m;
72
73 // This is not expected to be set when data is showing,
74 // so we don't update the viewport here
75}
c7b76823 76
628b45cc
SA
77void QHexView::setData(const DecodeBinaryClass* data)
78{
01ba5ed7 79 data_ = data;
628b45cc
SA
80
81 data_size_ = 0;
82 for (const DecodeBinaryDataChunk& chunk : data_->chunks)
83 data_size_ += chunk.data.size();
84
85 viewport()->update();
86}
87
88void QHexView::clear()
89{
90 verticalScrollBar()->setValue(0);
91 data_ = nullptr;
92 data_size_ = 0;
b2b18d3a
SA
93
94 viewport()->update();
c7b76823
SA
95}
96
97void QHexView::showFromOffset(size_t offset)
98{
628b45cc 99 if (data_ && (offset < data_size_)) {
c7b76823
SA
100 setCursorPos(offset * 2);
101
102 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
103 verticalScrollBar() -> setValue(cursorY);
104 }
4b97fe09
SA
105
106 viewport()->update();
c7b76823
SA
107}
108
628b45cc 109void QHexView::initialize_byte_iterator(size_t offset)
c7b76823 110{
628b45cc
SA
111 current_chunk_id_ = 0;
112 current_chunk_offset_ = 0;
113
114 for (const DecodeBinaryDataChunk& chunk : data_->chunks)
115 if (offset >= chunk.data.size()) {
116 current_chunk_id_++;
117 offset -= chunk.data.size();
118 } else {
119 current_chunk_offset_ = offset;
120 break;
121 }
4b97fe09 122
628b45cc
SA
123 current_chunk_ = &(data_->chunks[current_chunk_id_]);
124}
125
126uint8_t QHexView::get_next_byte(bool* is_next_chunk)
127{
128 if (is_next_chunk != nullptr)
129 *is_next_chunk = (current_chunk_offset_ == 0);
130
131 uint8_t v = current_chunk_->data[current_chunk_offset_];
132
133 current_chunk_offset_++;
134 if (current_chunk_offset_ == current_chunk_->data.size()) {
135 current_chunk_id_++;
136 current_chunk_offset_ = 0;
137 current_chunk_ = &(data_->chunks[current_chunk_id_]);
138 }
139
140 return v;
c7b76823
SA
141}
142
560f8377 143QSize QHexView::getFullSize() const
c7b76823 144{
560f8377
SA
145 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
146 GAP_ASCII_SLIDER + verticalScrollBar()->width();
147
01ba5ed7
SA
148 if (!data_)
149 return QSize(width, 0);
560f8377 150
628b45cc 151 size_t height = data_size_ / BYTES_PER_LINE;
01ba5ed7 152
628b45cc 153 if (data_size_ % BYTES_PER_LINE)
c7b76823
SA
154 height++;
155
156 height *= charHeight_;
157
158 return QSize(width, height);
159}
160
161void QHexView::paintEvent(QPaintEvent *event)
162{
c7b76823
SA
163 QPainter painter(viewport());
164
560f8377 165 // Calculate and update the widget and paint area sizes
560f8377
SA
166 QSize widgetSize = getFullSize();
167 setMinimumWidth(widgetSize.width());
168 setMaximumWidth(widgetSize.width());
01ba5ed7 169 QSize areaSize = viewport()->size();
560f8377 170
c7b76823
SA
171 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
172 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
173
01ba5ed7
SA
174 // Fill widget background
175 painter.fillRect(event->rect(), palette().color(QPalette::Base));
176
628b45cc 177 if (!data_ || (data_size_ == 0)) {
01ba5ed7
SA
178 painter.setPen(palette().color(QPalette::Text));
179 QString s = tr("No data available");
180 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
181 int y = areaSize.height() / 2;
182 painter.drawText(x, y, s);
183 return;
184 }
185
186 // Determine first/last line indices
c7b76823
SA
187 size_t firstLineIdx = verticalScrollBar()->value();
188
189 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
628b45cc
SA
190 if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
191 lastLineIdx = data_size_ / BYTES_PER_LINE;
192 if (data_size_ % BYTES_PER_LINE)
c7b76823
SA
193 lastLineIdx++;
194 }
195
01ba5ed7 196 // Fill address area background
c7b76823 197 painter.fillRect(QRect(posAddr_, event->rect().top(),
560f8377 198 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
c7b76823 199
560f8377
SA
200 // Paint divider line between hex and ASCII areas
201 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
202 painter.setPen(palette().color(QPalette::Midlight));
203 painter.drawLine(line_x, event->rect().top(), line_x, height());
c7b76823 204
560f8377
SA
205 // Paint address area
206 painter.setPen(palette().color(QPalette::ButtonText));
c7b76823 207
560f8377
SA
208 int yStart = charHeight_;
209 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
c7b76823 210
560f8377
SA
211 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
212 painter.drawText(posAddr_, y, address);
213 y += charHeight_;
214 }
c7b76823 215
560f8377
SA
216 // Paint hex values
217 QBrush regular = painter.brush();
218 QBrush selected = QBrush(palette().color(QPalette::Highlight));
c7b76823 219
628b45cc 220 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
560f8377
SA
221 yStart = charHeight_;
222 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
223
224 painter.setBackgroundMode(Qt::OpaqueMode);
c7b76823 225
560f8377 226 int x = posHex_;
628b45cc 227 for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < data_size_; i++) {
c7b76823 228 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
c7b76823 229
560f8377 230 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
c7b76823 231 painter.setBackground(selected);
560f8377 232 painter.setPen(palette().color(QPalette::HighlightedText));
c7b76823 233 } else {
560f8377
SA
234 painter.setBackground(regular);
235 painter.setPen(palette().color(QPalette::Text));
c7b76823
SA
236 }
237
628b45cc
SA
238 // Fetch byte
239 uint8_t byte_value = get_next_byte();
240
560f8377 241 // First nibble
628b45cc 242 QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper();
560f8377 243 painter.drawText(x, y, val);
c7b76823 244
560f8377 245 // Second nibble
628b45cc 246 val = QString::number((byte_value & 0xF), 16).toUpper();
560f8377 247 painter.drawText(x + charWidth_, y, val);
c7b76823 248
560f8377 249 x += 3 * charWidth_;
c7b76823
SA
250 }
251
560f8377
SA
252 y += charHeight_;
253 }
254
255 // Paint ASCII characters
628b45cc 256 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
560f8377
SA
257 yStart = charHeight_;
258 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
259
260 int x = posAscii_;
628b45cc
SA
261 for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < data_size_ && (i < BYTES_PER_LINE); i++) {
262 // Fetch byte
263 uint8_t ch = get_next_byte();
c7b76823 264
560f8377 265 if ((ch < 0x20) || (ch > 0x7E))
c7b76823
SA
266 ch = '.';
267
560f8377
SA
268 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
269 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
270 painter.setBackground(selected);
271 painter.setPen(palette().color(QPalette::HighlightedText));
272 } else {
273 painter.setBackground(regular);
274 painter.setPen(palette().color(QPalette::Text));
275 }
276
277 painter.drawText(x, y, QString(ch));
278 x += charWidth_;
c7b76823
SA
279 }
280
560f8377 281 y += charHeight_;
c7b76823
SA
282 }
283
560f8377 284 // Paint cursor
c7b76823
SA
285 if (hasFocus()) {
286 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
287 int y = cursorPos_ / (2 * BYTES_PER_LINE);
288 y -= firstLineIdx;
289 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
290 int cursorY = y * charHeight_ + 4;
560f8377 291 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
c7b76823
SA
292 }
293}
294
295void QHexView::keyPressEvent(QKeyEvent *event)
296{
297 bool setVisible = false;
298
560f8377 299 // Cursor movements
c7b76823
SA
300 if (event->matches(QKeySequence::MoveToNextChar)) {
301 setCursorPos(cursorPos_ + 1);
302 resetSelection(cursorPos_);
303 setVisible = true;
304 }
305 if (event->matches(QKeySequence::MoveToPreviousChar)) {
306 setCursorPos(cursorPos_ - 1);
307 resetSelection(cursorPos_);
308 setVisible = true;
309 }
310
311 if (event->matches(QKeySequence::MoveToEndOfLine)) {
312 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
313 resetSelection(cursorPos_);
314 setVisible = true;
315 }
316 if (event->matches(QKeySequence::MoveToStartOfLine)) {
317 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
318 resetSelection(cursorPos_);
319 setVisible = true;
320 }
321 if (event->matches(QKeySequence::MoveToPreviousLine)) {
322 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
323 resetSelection(cursorPos_);
324 setVisible = true;
325 }
326 if (event->matches(QKeySequence::MoveToNextLine)) {
327 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
328 resetSelection(cursorPos_);
329 setVisible = true;
330 }
331
332 if (event->matches(QKeySequence::MoveToNextPage)) {
333 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
334 resetSelection(cursorPos_);
335 setVisible = true;
336 }
337 if (event->matches(QKeySequence::MoveToPreviousPage)) {
338 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
339 resetSelection(cursorPos_);
340 setVisible = true;
341 }
342 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
628b45cc 343 setCursorPos(data_size_ * 2);
c7b76823
SA
344 resetSelection(cursorPos_);
345 setVisible = true;
346 }
347 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
348 setCursorPos(0);
349 resetSelection(cursorPos_);
350 setVisible = true;
351 }
352
560f8377 353 // Select commands
c7b76823
SA
354 if (event->matches(QKeySequence::SelectAll)) {
355 resetSelection(0);
628b45cc 356 setSelection(2 * data_size_);
c7b76823
SA
357 setVisible = true;
358 }
359 if (event->matches(QKeySequence::SelectNextChar)) {
360 int pos = cursorPos_ + 1;
361 setCursorPos(pos);
362 setSelection(pos);
363 setVisible = true;
364 }
365 if (event->matches(QKeySequence::SelectPreviousChar)) {
366 int pos = cursorPos_ - 1;
367 setSelection(pos);
368 setCursorPos(pos);
369 setVisible = true;
370 }
371 if (event->matches(QKeySequence::SelectEndOfLine)) {
372 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
373 setCursorPos(pos);
374 setSelection(pos);
375 setVisible = true;
376 }
377 if (event->matches(QKeySequence::SelectStartOfLine)) {
378 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
379 setCursorPos(pos);
380 setSelection(pos);
381 setVisible = true;
382 }
383 if (event->matches(QKeySequence::SelectPreviousLine)) {
384 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
385 setCursorPos(pos);
386 setSelection(pos);
387 setVisible = true;
388 }
389 if (event->matches(QKeySequence::SelectNextLine)) {
390 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
391 setCursorPos(pos);
392 setSelection(pos);
393 setVisible = true;
394 }
395
396 if (event->matches(QKeySequence::SelectNextPage)) {
397 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
398 setCursorPos(pos);
399 setSelection(pos);
400 setVisible = true;
401 }
402 if (event->matches(QKeySequence::SelectPreviousPage)) {
403 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
404 setCursorPos(pos);
405 setSelection(pos);
406 setVisible = true;
407 }
408 if (event->matches(QKeySequence::SelectEndOfDocument)) {
628b45cc 409 int pos = data_size_ * 2;
c7b76823
SA
410 setCursorPos(pos);
411 setSelection(pos);
412 setVisible = true;
413 }
414 if (event->matches(QKeySequence::SelectStartOfDocument)) {
628b45cc
SA
415 setCursorPos(0);
416 setSelection(0);
c7b76823
SA
417 setVisible = true;
418 }
419
01ba5ed7 420 if (event->matches(QKeySequence::Copy) && (data_)) {
c7b76823 421 QString text;
c7b76823 422
628b45cc 423 initialize_byte_iterator(selectBegin_ / 2);
c7b76823 424
628b45cc
SA
425 size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2;
426 for (size_t i = 0; i < selectedSize; i++) {
427 uint8_t byte_value = get_next_byte();
428
429 QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() +
430 QString::number((byte_value & 0xF), 16).toUpper() + " ";
431 text += s;
c7b76823 432
628b45cc
SA
433 if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
434 text += "\n";
c7b76823
SA
435 }
436
437 QClipboard *clipboard = QApplication::clipboard();
628b45cc 438 clipboard->setText(text, QClipboard::Clipboard);
c7b76823 439 if (clipboard->supportsSelection())
628b45cc 440 clipboard->setText(text, QClipboard::Selection);
c7b76823
SA
441 }
442
443 if (setVisible)
444 ensureVisible();
445
446 viewport()->update();
447}
448
449void QHexView::mouseMoveEvent(QMouseEvent *event)
450{
560f8377 451 int actPos = cursorPosFromMousePos(event->pos());
c7b76823
SA
452 setCursorPos(actPos);
453 setSelection(actPos);
454
455 viewport()->update();
456}
457
458void QHexView::mousePressEvent(QMouseEvent *event)
459{
560f8377 460 int cPos = cursorPosFromMousePos(event->pos());
c7b76823
SA
461
462 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
463 setSelection(cPos);
464 else
465 resetSelection(cPos);
466
467 setCursorPos(cPos);
468
469 viewport()->update();
470}
471
560f8377 472size_t QHexView::cursorPosFromMousePos(const QPoint &position)
c7b76823 473{
628b45cc 474 size_t pos = -1;
c7b76823
SA
475
476 if (((size_t)position.x() >= posHex_) &&
477 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
478
560f8377
SA
479 // Note: We add 1.5 character widths so that selection across
480 // byte gaps is smoother
628b45cc 481 size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
c7b76823 482
560f8377
SA
483 // Note: We allow only full bytes to be selected, not nibbles,
484 // so we round to the nearest byte gap
485 x = (2 * x + 1) / 3;
c7b76823 486
628b45cc
SA
487 size_t firstLineIdx = verticalScrollBar()->value();
488 size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
c7b76823
SA
489 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
490 }
491
628b45cc
SA
492 size_t max_pos = data_size_ * 2;
493
494 return std::min(pos, max_pos);
c7b76823
SA
495}
496
497void QHexView::resetSelection()
498{
499 selectBegin_ = selectInit_;
500 selectEnd_ = selectInit_;
501}
502
503void QHexView::resetSelection(int pos)
504{
505 if (pos < 0)
506 pos = 0;
507
508 selectInit_ = pos;
509 selectBegin_ = pos;
510 selectEnd_ = pos;
511}
512
513void QHexView::setSelection(int pos)
514{
515 if (pos < 0)
516 pos = 0;
517
518 if ((size_t)pos >= selectInit_) {
519 selectEnd_ = pos;
520 selectBegin_ = selectInit_;
521 } else {
522 selectBegin_ = pos;
523 selectEnd_ = selectInit_;
524 }
525}
526
527void QHexView::setCursorPos(int position)
528{
529 if (position < 0)
530 position = 0;
531
628b45cc 532 int max_pos = data_size_ * 2;
c7b76823 533
628b45cc
SA
534 if (position > max_pos)
535 position = max_pos;
c7b76823
SA
536
537 cursorPos_ = position;
538}
539
540void QHexView::ensureVisible()
541{
542 QSize areaSize = viewport()->size();
543
628b45cc 544 int firstLineIdx = verticalScrollBar()->value();
c7b76823
SA
545 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
546
547 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
548
549 if (cursorY < firstLineIdx)
628b45cc
SA
550 verticalScrollBar()->setValue(cursorY);
551 else
552 if(cursorY >= lastLineIdx)
553 verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1);
c7b76823 554}