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