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