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