]> sigrok.org Git - pulseview.git/blame - pv/views/decoder_binary/QHexView.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / views / decoder_binary / 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
978dbc9a
SA
30#include <limits>
31
c7b76823
SA
32#include <QApplication>
33#include <QClipboard>
c8a6db0e 34#include <QDebug>
c7b76823
SA
35#include <QFont>
36#include <QKeyEvent>
37#include <QScrollBar>
38#include <QSize>
d5397ccb 39#include <QString>
c7b76823
SA
40#include <QPainter>
41#include <QPaintEvent>
42
43#include "QHexView.hpp"
44
978dbc9a
SA
45using std::make_pair;
46
560f8377
SA
47const unsigned int BYTES_PER_LINE = 16;
48const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
49const unsigned int GAP_ADR_HEX = 10;
50const unsigned int GAP_HEX_ASCII = 10;
51const unsigned int GAP_ASCII_SLIDER = 5;
c7b76823 52
c7b76823
SA
53
54QHexView::QHexView(QWidget *parent):
560f8377 55 QAbstractScrollArea(parent),
628b45cc
SA
56 mode_(ChunkedDataMode),
57 data_(nullptr),
58 selectBegin_(0),
59 selectEnd_(0),
978dbc9a
SA
60 cursorPos_(0),
61 visible_range_(0, 0),
62 highlighted_sample_(std::numeric_limits<uint64_t>::max())
c7b76823
SA
63{
64 setFont(QFont("Courier", 10));
65
560f8377 66 charWidth_ = fontMetrics().boundingRect('X').width();
c7b76823
SA
67 charHeight_ = fontMetrics().height();
68
c7b76823 69 setFocusPolicy(Qt::StrongFocus);
a61abf09
SA
70
71 if (palette().color(QPalette::ButtonText).toHsv().value() > 127) {
72 // Color is bright
73 chunk_colors_.emplace_back(100, 149, 237); // QColorConstants::Svg::cornflowerblue
74 chunk_colors_.emplace_back(60, 179, 113); // QColorConstants::Svg::mediumseagreen
75 chunk_colors_.emplace_back(210, 180, 140); // QColorConstants::Svg::tan
978dbc9a 76 visible_range_color_ = QColor("#fff5ee"); // QColorConstants::Svg::seashell
a61abf09
SA
77 } else {
78 // Color is dark
978dbc9a
SA
79 chunk_colors_.emplace_back(0, 0, 139); // QColorConstants::Svg::darkblue
80 chunk_colors_.emplace_back(34, 139, 34); // QColorConstants::Svg::forestgreen
81 chunk_colors_.emplace_back(160, 82, 45); // QColorConstants::Svg::sienna
82 visible_range_color_ = QColor("#fff5ee"); // QColorConstants::Svg::seashell
a61abf09 83 }
c7b76823
SA
84}
85
d5645564 86void QHexView::set_mode(Mode m)
c7b76823 87{
628b45cc
SA
88 mode_ = m;
89
90 // This is not expected to be set when data is showing,
91 // so we don't update the viewport here
92}
c7b76823 93
d5645564 94void QHexView::set_data(const DecodeBinaryClass* data)
628b45cc 95{
01ba5ed7 96 data_ = data;
628b45cc 97
c8a6db0e 98 size_t size = 0;
ee4ba097
SA
99 if (data) {
100 size_t chunks = data_->chunks.size();
101 for (size_t i = 0; i < chunks; i++)
102 size += data_->chunks[i].data.size();
103 }
c8a6db0e 104 data_size_ = size;
628b45cc 105
d5397ccb
SA
106 address_digits_ = (uint8_t)QString::number(data_size_, 16).length();
107
108 // Calculate X coordinates of the three sub-areas
109 posAddr_ = 0;
110 posHex_ = address_digits_ * charWidth_ + GAP_ADR_HEX;
111 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
112
628b45cc
SA
113 viewport()->update();
114}
115
978dbc9a
SA
116void QHexView::set_visible_sample_range(uint64_t start, uint64_t end)
117{
118 visible_range_ = make_pair(start, end);
119
120 viewport()->update();
121}
122
123void QHexView::set_highlighted_data_sample(uint64_t sample)
124{
125 highlighted_sample_ = sample;
126
127 viewport()->update();
128}
129
d5645564
SA
130unsigned int QHexView::get_bytes_per_line() const
131{
132 return BYTES_PER_LINE;
133}
134
628b45cc
SA
135void QHexView::clear()
136{
137 verticalScrollBar()->setValue(0);
138 data_ = nullptr;
139 data_size_ = 0;
b2b18d3a 140
978dbc9a
SA
141 highlighted_sample_ = std::numeric_limits<uint64_t>::max();
142
b2b18d3a 143 viewport()->update();
c7b76823
SA
144}
145
146void QHexView::showFromOffset(size_t offset)
147{
628b45cc 148 if (data_ && (offset < data_size_)) {
c7b76823
SA
149 setCursorPos(offset * 2);
150
151 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
152 verticalScrollBar() -> setValue(cursorY);
153 }
4b97fe09
SA
154
155 viewport()->update();
c7b76823
SA
156}
157
6961eab0
SA
158QSizePolicy QHexView::sizePolicy() const
159{
160 return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
161}
162
13b726cd
SA
163pair<size_t, size_t> QHexView::get_selection() const
164{
165 size_t start = selectBegin_ / 2;
166 size_t end = selectEnd_ / 2;
167
168 if (start == end) {
169 // Nothing is currently selected
170 start = 0;
171 end = data_size_;
e094bcb2 172 } if (end < data_size_)
13b726cd
SA
173 end++;
174
175 return std::make_pair(start, end);
176}
177
d5645564
SA
178size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
179 bool with_offset, bool with_ascii)
180{
181 dest->clear();
182
183 // Determine start address for the row
184 uint64_t row = start / BYTES_PER_LINE;
185 uint64_t offset = row * BYTES_PER_LINE;
b6d0bcb8 186 end = std::min((uint64_t)end, offset + BYTES_PER_LINE);
d5645564
SA
187
188 if (with_offset)
d5397ccb 189 dest->append(QString("%1 ").arg(row * BYTES_PER_LINE, address_digits_, 16, QChar('0')).toUpper());
d5645564
SA
190
191 initialize_byte_iterator(offset);
192 for (size_t i = offset; i < offset + BYTES_PER_LINE; i++) {
193 uint8_t value = 0;
194
195 if (i < end)
196 value = get_next_byte();
197
198 if ((i < start) || (i >= end))
199 dest->append(" ");
200 else
201 dest->append(QString("%1 ").arg(value, 2, 16, QChar('0')).toUpper());
202 }
203
204 if (with_ascii) {
205 initialize_byte_iterator(offset);
206 for (size_t i = offset; i < end; i++) {
207 uint8_t value = get_next_byte();
208
5cdf186d
SA
209 if ((value < 0x20) || (value > 0x7E))
210 value = '.';
211
d5645564
SA
212 if (i < start)
213 dest->append(' ');
214 else
215 dest->append((char)value);
216 }
217 }
218
219 return end;
220}
221
628b45cc 222void QHexView::initialize_byte_iterator(size_t offset)
c7b76823 223{
628b45cc
SA
224 current_chunk_id_ = 0;
225 current_chunk_offset_ = 0;
cbf428c2 226 current_offset_ = offset;
628b45cc 227
c8a6db0e
SA
228 size_t chunks = data_->chunks.size();
229 for (size_t i = 0; i < chunks; i++) {
230 size_t size = data_->chunks[i].data.size();
231
232 if (offset >= size) {
628b45cc 233 current_chunk_id_++;
c8a6db0e 234 offset -= size;
628b45cc
SA
235 } else {
236 current_chunk_offset_ = offset;
237 break;
238 }
c8a6db0e 239 }
4b97fe09 240
c8a6db0e
SA
241 if (current_chunk_id_ < data_->chunks.size())
242 current_chunk_ = data_->chunks[current_chunk_id_];
978dbc9a
SA
243
244 current_chunk_sample_ = current_chunk_.sample;
245
246 // Obtain sample of next chunk if there is one
247 if ((current_chunk_id_ + 1) < data_->chunks.size())
248 next_chunk_sample_ = data_->chunks[current_chunk_id_ + 1].sample;
249 else
250 next_chunk_sample_ = std::numeric_limits<uint64_t>::max();
628b45cc
SA
251}
252
978dbc9a 253uint8_t QHexView::get_next_byte(bool* is_new_chunk)
628b45cc 254{
978dbc9a
SA
255 if (is_new_chunk != nullptr)
256 *is_new_chunk = (current_chunk_offset_ == 0);
628b45cc 257
c8a6db0e
SA
258 uint8_t v = 0;
259 if (current_chunk_offset_ < current_chunk_.data.size())
260 v = current_chunk_.data[current_chunk_offset_];
628b45cc 261
978dbc9a
SA
262 current_chunk_sample_ = current_chunk_.sample;
263
264 if (is_new_chunk) {
265 // Obtain sample of next chunk if there is one
266 if ((current_chunk_id_ + 1) < data_->chunks.size())
267 next_chunk_sample_ = data_->chunks[current_chunk_id_ + 1].sample;
268 else
269 next_chunk_sample_ = std::numeric_limits<uint64_t>::max();
270 }
271
cbf428c2 272 current_offset_++;
628b45cc 273 current_chunk_offset_++;
cbf428c2 274
c8a6db0e
SA
275 if (current_offset_ > data_size_) {
276 qWarning() << "QHexView::get_next_byte() overran binary data boundary:" <<
277 current_offset_ << "of" << data_size_ << "bytes";
278 return 0xEE;
279 }
280
281 if ((current_chunk_offset_ == current_chunk_.data.size()) && (current_offset_ < data_size_)) {
628b45cc
SA
282 current_chunk_id_++;
283 current_chunk_offset_ = 0;
516d2128 284 current_chunk_ = data_->chunks[current_chunk_id_];
628b45cc
SA
285 }
286
287 return v;
c7b76823
SA
288}
289
560f8377 290QSize QHexView::getFullSize() const
c7b76823 291{
fae7037a 292 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_);
560f8377 293
fae7037a
SA
294 if (verticalScrollBar()->isEnabled())
295 width += GAP_ASCII_SLIDER + verticalScrollBar()->width();
296
297 if (!data_ || (data_size_ == 0))
01ba5ed7 298 return QSize(width, 0);
560f8377 299
628b45cc 300 size_t height = data_size_ / BYTES_PER_LINE;
01ba5ed7 301
628b45cc 302 if (data_size_ % BYTES_PER_LINE)
c7b76823
SA
303 height++;
304
305 height *= charHeight_;
306
307 return QSize(width, height);
308}
309
310void QHexView::paintEvent(QPaintEvent *event)
311{
c7b76823
SA
312 QPainter painter(viewport());
313
978dbc9a
SA
314 QFont normal_font = painter.font();
315 QFont bold_font = painter.font();
316 bold_font.setWeight(QFont::Bold);
317
318 bool bold_font_was_used = false;
319
560f8377 320 // Calculate and update the widget and paint area sizes
560f8377
SA
321 QSize widgetSize = getFullSize();
322 setMinimumWidth(widgetSize.width());
323 setMaximumWidth(widgetSize.width());
ee4ba097 324 QSize areaSize = viewport()->size() - QSize(0, charHeight_);
560f8377 325
fae7037a
SA
326 // Only show scrollbar if the content goes beyond the visible area
327 if (widgetSize.height() > areaSize.height()) {
328 verticalScrollBar()->setEnabled(true);
329 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
330 verticalScrollBar()->setRange(0, ((widgetSize.height() - areaSize.height())) / charHeight_ + 1);
331 } else
332 verticalScrollBar()->setEnabled(false);
c7b76823 333
01ba5ed7
SA
334 // Fill widget background
335 painter.fillRect(event->rect(), palette().color(QPalette::Base));
336
ff49e70c 337 if (!data_ || (data_size_ == 0) || (data_->chunks.empty())) {
01ba5ed7
SA
338 painter.setPen(palette().color(QPalette::Text));
339 QString s = tr("No data available");
340 int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
341 int y = areaSize.height() / 2;
342 painter.drawText(x, y, s);
343 return;
344 }
345
346 // Determine first/last line indices
c7b76823
SA
347 size_t firstLineIdx = verticalScrollBar()->value();
348
ee4ba097 349 size_t lastLineIdx = firstLineIdx + (areaSize.height() / charHeight_);
628b45cc
SA
350 if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
351 lastLineIdx = data_size_ / BYTES_PER_LINE;
352 if (data_size_ % BYTES_PER_LINE)
c7b76823
SA
353 lastLineIdx++;
354 }
355
560f8377
SA
356 // Paint divider line between hex and ASCII areas
357 int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
358 painter.setPen(palette().color(QPalette::Midlight));
359 painter.drawLine(line_x, event->rect().top(), line_x, height());
c7b76823 360
48b168aa
SA
361 // Fill address area background
362 painter.fillRect(QRect(posAddr_, event->rect().top(),
363 posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
364 painter.fillRect(QRect(posAddr_, event->rect().top(),
365 posAscii_ - (GAP_HEX_ASCII / 2), charHeight_ + 2), palette().color(QPalette::Window));
366
560f8377
SA
367 // Paint address area
368 painter.setPen(palette().color(QPalette::ButtonText));
c7b76823 369
ee4ba097 370 int yStart = 2 * charHeight_;
560f8377 371 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
c7b76823 372
d5397ccb 373 QString address = QString("%1").arg(lineIdx * 16, address_digits_, 16, QChar('0')).toUpper();
560f8377
SA
374 painter.drawText(posAddr_, y, address);
375 y += charHeight_;
376 }
c7b76823 377
ee4ba097
SA
378 // Paint top row with hex offsets
379 painter.setPen(palette().color(QPalette::ButtonText));
380 for (int offset = 0; offset <= 0xF; offset++)
381 painter.drawText(posHex_ + (1 + offset * 3) * charWidth_,
48b168aa 382 charHeight_ - 3, QString::number(offset, 16).toUpper());
ee4ba097 383
560f8377 384 // Paint hex values
978dbc9a
SA
385 QBrush regular_brush = palette().buttonText();
386 QBrush selected_brush = palette().highlight();
387 QBrush visible_range_brush = QBrush(visible_range_color_);
a61abf09
SA
388
389 bool multiple_chunks = (data_->chunks.size() > 1);
390 unsigned int chunk_color = 0;
c7b76823 391
628b45cc 392 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
978dbc9a 393
ee4ba097 394 yStart = 2 * charHeight_;
560f8377
SA
395 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
396
560f8377 397 int x = posHex_;
cbf428c2 398 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
c7b76823 399 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
c7b76823 400
a61abf09 401 // Fetch byte
978dbc9a
SA
402 bool is_new_chunk;
403 uint8_t byte_value = get_next_byte(&is_new_chunk);
a61abf09 404
978dbc9a 405 if (is_new_chunk) {
a61abf09
SA
406 chunk_color++;
407 if (chunk_color == chunk_colors_.size())
408 chunk_color = 0;
978dbc9a
SA
409
410 // New chunk means also new chunk sample, so check for required changes
411 if (bold_font_was_used)
412 painter.setFont(normal_font);
413 if ((highlighted_sample_ >= current_chunk_sample_) && (highlighted_sample_ < next_chunk_sample_)) {
414 painter.setFont(bold_font);
415 bold_font_was_used = true;
416 }
417 }
418
419 // Restore default paint style
420 painter.setBackground(regular_brush);
421 painter.setBackgroundMode(Qt::TransparentMode);
422 if (!multiple_chunks)
423 painter.setPen(palette().color(QPalette::Text));
424 else
425 painter.setPen(chunk_colors_[chunk_color]);
426
427 // Highlight needed because it's the range visible in main view?
428 if ((current_chunk_sample_ >= visible_range_.first) && (current_chunk_sample_ < visible_range_.second)) {
429 painter.setBackgroundMode(Qt::OpaqueMode);
430 painter.setBackground(visible_range_brush);
a61abf09
SA
431 }
432
978dbc9a 433 // Highlight for selection range needed? (takes priority over visible range highlight)
560f8377 434 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
a61abf09 435 painter.setBackgroundMode(Qt::OpaqueMode);
978dbc9a 436 painter.setBackground(selected_brush);
560f8377 437 painter.setPen(palette().color(QPalette::HighlightedText));
c7b76823
SA
438 }
439
560f8377 440 // First nibble
628b45cc 441 QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper();
560f8377 442 painter.drawText(x, y, val);
c7b76823 443
560f8377 444 // Second nibble
628b45cc 445 val = QString::number((byte_value & 0xF), 16).toUpper();
560f8377 446 painter.drawText(x + charWidth_, y, val);
c7b76823 447
978dbc9a 448 if ((i < BYTES_PER_LINE - 1) && (current_offset_ < data_size_))
a7af8bb0
SA
449 painter.drawText(x + 2 * charWidth_, y, QString(' '));
450
560f8377 451 x += 3 * charWidth_;
c7b76823
SA
452 }
453
560f8377
SA
454 y += charHeight_;
455 }
456
457 // Paint ASCII characters
628b45cc 458 initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
ee4ba097 459 yStart = 2 * charHeight_;
560f8377
SA
460 for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
461
462 int x = posAscii_;
cbf428c2 463 for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
628b45cc 464 // Fetch byte
978dbc9a
SA
465 bool is_new_chunk;
466 uint8_t ch = get_next_byte(&is_new_chunk);
467
468 if (is_new_chunk) {
469 // New chunk means also new chunk sample, so check for required changes
470 if (bold_font_was_used)
471 painter.setFont(normal_font);
472 if ((highlighted_sample_ >= current_chunk_sample_) && (highlighted_sample_ < next_chunk_sample_)) {
473 painter.setFont(bold_font);
474 bold_font_was_used = true;
475 }
476 }
c7b76823 477
560f8377 478 if ((ch < 0x20) || (ch > 0x7E))
c7b76823
SA
479 ch = '.';
480
978dbc9a
SA
481 // Restore default paint style
482 painter.setBackgroundMode(Qt::TransparentMode);
483 painter.setBackground(regular_brush);
484 painter.setPen(palette().color(QPalette::Text));
485
486 // Highlight needed because it's the range visible in main view?
487 if ((current_chunk_sample_ >= visible_range_.first) && (current_chunk_sample_ < visible_range_.second)) {
488 painter.setBackgroundMode(Qt::OpaqueMode);
489 painter.setBackground(visible_range_brush);
490 }
491
492 // Highlight for selection range needed? (takes priority over visible range highlight)
560f8377
SA
493 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
494 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
a61abf09 495 painter.setBackgroundMode(Qt::OpaqueMode);
978dbc9a 496 painter.setBackground(selected_brush);
560f8377 497 painter.setPen(palette().color(QPalette::HighlightedText));
560f8377
SA
498 }
499
1ed73ebd
VPP
500#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
501 painter.drawText(x, y, QString(QChar(ch)));
502#else
560f8377 503 painter.drawText(x, y, QString(ch));
1ed73ebd 504#endif
560f8377 505 x += charWidth_;
c7b76823
SA
506 }
507
560f8377 508 y += charHeight_;
c7b76823
SA
509 }
510
978dbc9a
SA
511 // Restore painter defaults
512 painter.setBackgroundMode(Qt::TransparentMode);
513 painter.setBackground(regular_brush);
514
560f8377 515 // Paint cursor
c7b76823
SA
516 if (hasFocus()) {
517 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
518 int y = cursorPos_ / (2 * BYTES_PER_LINE);
519 y -= firstLineIdx;
520 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
ee4ba097 521 int cursorY = charHeight_ + y * charHeight_ + 4;
560f8377 522 painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
c7b76823
SA
523 }
524}
525
526void QHexView::keyPressEvent(QKeyEvent *event)
527{
528 bool setVisible = false;
529
560f8377 530 // Cursor movements
c7b76823
SA
531 if (event->matches(QKeySequence::MoveToNextChar)) {
532 setCursorPos(cursorPos_ + 1);
533 resetSelection(cursorPos_);
534 setVisible = true;
535 }
536 if (event->matches(QKeySequence::MoveToPreviousChar)) {
537 setCursorPos(cursorPos_ - 1);
538 resetSelection(cursorPos_);
539 setVisible = true;
540 }
541
542 if (event->matches(QKeySequence::MoveToEndOfLine)) {
543 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
544 resetSelection(cursorPos_);
545 setVisible = true;
546 }
547 if (event->matches(QKeySequence::MoveToStartOfLine)) {
548 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
549 resetSelection(cursorPos_);
550 setVisible = true;
551 }
552 if (event->matches(QKeySequence::MoveToPreviousLine)) {
553 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
554 resetSelection(cursorPos_);
555 setVisible = true;
556 }
557 if (event->matches(QKeySequence::MoveToNextLine)) {
558 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
559 resetSelection(cursorPos_);
560 setVisible = true;
561 }
562
563 if (event->matches(QKeySequence::MoveToNextPage)) {
564 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
565 resetSelection(cursorPos_);
566 setVisible = true;
567 }
568 if (event->matches(QKeySequence::MoveToPreviousPage)) {
569 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
570 resetSelection(cursorPos_);
571 setVisible = true;
572 }
573 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
628b45cc 574 setCursorPos(data_size_ * 2);
c7b76823
SA
575 resetSelection(cursorPos_);
576 setVisible = true;
577 }
578 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
579 setCursorPos(0);
580 resetSelection(cursorPos_);
581 setVisible = true;
582 }
583
560f8377 584 // Select commands
c7b76823
SA
585 if (event->matches(QKeySequence::SelectAll)) {
586 resetSelection(0);
628b45cc 587 setSelection(2 * data_size_);
c7b76823
SA
588 setVisible = true;
589 }
590 if (event->matches(QKeySequence::SelectNextChar)) {
591 int pos = cursorPos_ + 1;
592 setCursorPos(pos);
593 setSelection(pos);
594 setVisible = true;
595 }
596 if (event->matches(QKeySequence::SelectPreviousChar)) {
597 int pos = cursorPos_ - 1;
598 setSelection(pos);
599 setCursorPos(pos);
600 setVisible = true;
601 }
602 if (event->matches(QKeySequence::SelectEndOfLine)) {
603 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
604 setCursorPos(pos);
605 setSelection(pos);
606 setVisible = true;
607 }
608 if (event->matches(QKeySequence::SelectStartOfLine)) {
609 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
610 setCursorPos(pos);
611 setSelection(pos);
612 setVisible = true;
613 }
614 if (event->matches(QKeySequence::SelectPreviousLine)) {
615 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
616 setCursorPos(pos);
617 setSelection(pos);
618 setVisible = true;
619 }
620 if (event->matches(QKeySequence::SelectNextLine)) {
621 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
622 setCursorPos(pos);
623 setSelection(pos);
624 setVisible = true;
625 }
626
627 if (event->matches(QKeySequence::SelectNextPage)) {
628 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
629 setCursorPos(pos);
630 setSelection(pos);
631 setVisible = true;
632 }
633 if (event->matches(QKeySequence::SelectPreviousPage)) {
634 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
635 setCursorPos(pos);
636 setSelection(pos);
637 setVisible = true;
638 }
639 if (event->matches(QKeySequence::SelectEndOfDocument)) {
628b45cc 640 int pos = data_size_ * 2;
c7b76823
SA
641 setCursorPos(pos);
642 setSelection(pos);
643 setVisible = true;
644 }
645 if (event->matches(QKeySequence::SelectStartOfDocument)) {
628b45cc
SA
646 setCursorPos(0);
647 setSelection(0);
c7b76823
SA
648 setVisible = true;
649 }
650
01ba5ed7 651 if (event->matches(QKeySequence::Copy) && (data_)) {
c7b76823 652 QString text;
c7b76823 653
628b45cc 654 initialize_byte_iterator(selectBegin_ / 2);
c7b76823 655
628b45cc
SA
656 size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2;
657 for (size_t i = 0; i < selectedSize; i++) {
658 uint8_t byte_value = get_next_byte();
659
660 QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() +
661 QString::number((byte_value & 0xF), 16).toUpper() + " ";
662 text += s;
c7b76823 663
628b45cc
SA
664 if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
665 text += "\n";
c7b76823
SA
666 }
667
668 QClipboard *clipboard = QApplication::clipboard();
628b45cc 669 clipboard->setText(text, QClipboard::Clipboard);
c7b76823 670 if (clipboard->supportsSelection())
628b45cc 671 clipboard->setText(text, QClipboard::Selection);
c7b76823
SA
672 }
673
674 if (setVisible)
675 ensureVisible();
676
677 viewport()->update();
678}
679
680void QHexView::mouseMoveEvent(QMouseEvent *event)
681{
560f8377 682 int actPos = cursorPosFromMousePos(event->pos());
c7b76823
SA
683 setCursorPos(actPos);
684 setSelection(actPos);
685
686 viewport()->update();
687}
688
689void QHexView::mousePressEvent(QMouseEvent *event)
690{
560f8377 691 int cPos = cursorPosFromMousePos(event->pos());
c7b76823
SA
692
693 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
694 setSelection(cPos);
695 else
696 resetSelection(cPos);
697
698 setCursorPos(cPos);
699
700 viewport()->update();
701}
702
560f8377 703size_t QHexView::cursorPosFromMousePos(const QPoint &position)
c7b76823 704{
628b45cc 705 size_t pos = -1;
c7b76823
SA
706
707 if (((size_t)position.x() >= posHex_) &&
708 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
709
560f8377
SA
710 // Note: We add 1.5 character widths so that selection across
711 // byte gaps is smoother
628b45cc 712 size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
c7b76823 713
560f8377
SA
714 // Note: We allow only full bytes to be selected, not nibbles,
715 // so we round to the nearest byte gap
716 x = (2 * x + 1) / 3;
c7b76823 717
628b45cc 718 size_t firstLineIdx = verticalScrollBar()->value();
ee4ba097 719 size_t y = ((position.y() / charHeight_) - 1) * 2 * BYTES_PER_LINE;
c7b76823
SA
720 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
721 }
722
628b45cc
SA
723 size_t max_pos = data_size_ * 2;
724
725 return std::min(pos, max_pos);
c7b76823
SA
726}
727
728void QHexView::resetSelection()
729{
730 selectBegin_ = selectInit_;
731 selectEnd_ = selectInit_;
732}
733
734void QHexView::resetSelection(int pos)
735{
736 if (pos < 0)
737 pos = 0;
738
739 selectInit_ = pos;
740 selectBegin_ = pos;
741 selectEnd_ = pos;
742}
743
744void QHexView::setSelection(int pos)
745{
746 if (pos < 0)
747 pos = 0;
748
749 if ((size_t)pos >= selectInit_) {
750 selectEnd_ = pos;
751 selectBegin_ = selectInit_;
752 } else {
753 selectBegin_ = pos;
754 selectEnd_ = selectInit_;
755 }
756}
757
758void QHexView::setCursorPos(int position)
759{
760 if (position < 0)
761 position = 0;
762
628b45cc 763 int max_pos = data_size_ * 2;
c7b76823 764
628b45cc
SA
765 if (position > max_pos)
766 position = max_pos;
c7b76823
SA
767
768 cursorPos_ = position;
769}
770
771void QHexView::ensureVisible()
772{
773 QSize areaSize = viewport()->size();
774
628b45cc 775 int firstLineIdx = verticalScrollBar()->value();
c7b76823
SA
776 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
777
778 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
779
780 if (cursorY < firstLineIdx)
628b45cc
SA
781 verticalScrollBar()->setValue(cursorY);
782 else
783 if(cursorY >= lastLineIdx)
784 verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1);
c7b76823 785}