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