]>
Commit | Line | Data |
---|---|---|
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> | |
32 | #include <QFont> | |
33 | #include <QKeyEvent> | |
34 | #include <QScrollBar> | |
35 | #include <QSize> | |
36 | #include <QPainter> | |
37 | #include <QPaintEvent> | |
38 | ||
39 | #include "QHexView.hpp" | |
40 | ||
560f8377 SA |
41 | const unsigned int BYTES_PER_LINE = 16; |
42 | const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1; | |
43 | const unsigned int GAP_ADR_HEX = 10; | |
44 | const unsigned int GAP_HEX_ASCII = 10; | |
45 | const unsigned int GAP_ASCII_SLIDER = 5; | |
c7b76823 | 46 | |
c7b76823 SA |
47 | |
48 | QHexView::QHexView(QWidget *parent): | |
560f8377 | 49 | QAbstractScrollArea(parent), |
01ba5ed7 | 50 | data_(nullptr) |
c7b76823 SA |
51 | { |
52 | setFont(QFont("Courier", 10)); | |
53 | ||
560f8377 | 54 | charWidth_ = fontMetrics().boundingRect('X').width(); |
c7b76823 SA |
55 | charHeight_ = fontMetrics().height(); |
56 | ||
560f8377 SA |
57 | // Determine X coordinates of the three sub-areas |
58 | posAddr_ = 0; | |
59 | posHex_ = 10 * charWidth_ + GAP_ADR_HEX; | |
c7b76823 SA |
60 | posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII; |
61 | ||
c7b76823 SA |
62 | setFocusPolicy(Qt::StrongFocus); |
63 | } | |
64 | ||
01ba5ed7 | 65 | void QHexView::setData(QByteArray *data) |
c7b76823 SA |
66 | { |
67 | verticalScrollBar()->setValue(0); | |
68 | ||
01ba5ed7 | 69 | data_ = data; |
c7b76823 SA |
70 | cursorPos_ = 0; |
71 | resetSelection(0); | |
72 | } | |
73 | ||
74 | void QHexView::showFromOffset(size_t offset) | |
75 | { | |
01ba5ed7 | 76 | if (data_ && (offset < (size_t)data_->count())) { |
c7b76823 SA |
77 | setCursorPos(offset * 2); |
78 | ||
79 | int cursorY = cursorPos_ / (2 * BYTES_PER_LINE); | |
80 | verticalScrollBar() -> setValue(cursorY); | |
81 | } | |
82 | } | |
83 | ||
84 | void QHexView::clear() | |
85 | { | |
86 | verticalScrollBar()->setValue(0); | |
01ba5ed7 | 87 | data_ = nullptr; |
c7b76823 SA |
88 | } |
89 | ||
560f8377 | 90 | QSize QHexView::getFullSize() const |
c7b76823 | 91 | { |
560f8377 SA |
92 | size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) + |
93 | GAP_ASCII_SLIDER + verticalScrollBar()->width(); | |
94 | ||
01ba5ed7 SA |
95 | if (!data_) |
96 | return QSize(width, 0); | |
560f8377 | 97 | |
01ba5ed7 SA |
98 | size_t height = data_->count() / BYTES_PER_LINE; |
99 | ||
100 | if (data_->count() % BYTES_PER_LINE) | |
c7b76823 SA |
101 | height++; |
102 | ||
103 | height *= charHeight_; | |
104 | ||
105 | return QSize(width, height); | |
106 | } | |
107 | ||
108 | void QHexView::paintEvent(QPaintEvent *event) | |
109 | { | |
c7b76823 SA |
110 | QPainter painter(viewport()); |
111 | ||
560f8377 | 112 | // Calculate and update the widget and paint area sizes |
560f8377 SA |
113 | QSize widgetSize = getFullSize(); |
114 | setMinimumWidth(widgetSize.width()); | |
115 | setMaximumWidth(widgetSize.width()); | |
01ba5ed7 | 116 | QSize areaSize = viewport()->size(); |
560f8377 | 117 | |
c7b76823 SA |
118 | verticalScrollBar()->setPageStep(areaSize.height() / charHeight_); |
119 | verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1); | |
120 | ||
01ba5ed7 SA |
121 | // Fill widget background |
122 | painter.fillRect(event->rect(), palette().color(QPalette::Base)); | |
123 | ||
124 | if (!data_) { | |
125 | painter.setPen(palette().color(QPalette::Text)); | |
126 | QString s = tr("No data available"); | |
127 | int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2; | |
128 | int y = areaSize.height() / 2; | |
129 | painter.drawText(x, y, s); | |
130 | return; | |
131 | } | |
132 | ||
133 | // Determine first/last line indices | |
c7b76823 SA |
134 | size_t firstLineIdx = verticalScrollBar()->value(); |
135 | ||
136 | size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_; | |
01ba5ed7 SA |
137 | if (lastLineIdx > (data_->count() / BYTES_PER_LINE)) { |
138 | lastLineIdx = data_->count() / BYTES_PER_LINE; | |
139 | if (data_->count() % BYTES_PER_LINE) | |
c7b76823 SA |
140 | lastLineIdx++; |
141 | } | |
142 | ||
01ba5ed7 | 143 | // Fill address area background |
c7b76823 | 144 | painter.fillRect(QRect(posAddr_, event->rect().top(), |
560f8377 | 145 | posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window)); |
c7b76823 | 146 | |
560f8377 SA |
147 | // Paint divider line between hex and ASCII areas |
148 | int line_x = posAscii_ - (GAP_HEX_ASCII / 2); | |
149 | painter.setPen(palette().color(QPalette::Midlight)); | |
150 | painter.drawLine(line_x, event->rect().top(), line_x, height()); | |
c7b76823 | 151 | |
560f8377 SA |
152 | // Paint address area |
153 | painter.setPen(palette().color(QPalette::ButtonText)); | |
c7b76823 | 154 | |
560f8377 SA |
155 | int yStart = charHeight_; |
156 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
c7b76823 | 157 | |
560f8377 SA |
158 | QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper(); |
159 | painter.drawText(posAddr_, y, address); | |
160 | y += charHeight_; | |
161 | } | |
c7b76823 | 162 | |
560f8377 SA |
163 | // Paint hex values |
164 | QBrush regular = painter.brush(); | |
165 | QBrush selected = QBrush(palette().color(QPalette::Highlight)); | |
01ba5ed7 SA |
166 | QByteArray data = data_->mid(firstLineIdx * BYTES_PER_LINE, |
167 | (lastLineIdx - firstLineIdx) * BYTES_PER_LINE); | |
c7b76823 | 168 | |
560f8377 SA |
169 | yStart = charHeight_; |
170 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
171 | ||
172 | painter.setBackgroundMode(Qt::OpaqueMode); | |
c7b76823 | 173 | |
560f8377 | 174 | int x = posHex_; |
c7b76823 SA |
175 | for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size(); i++) { |
176 | size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2; | |
c7b76823 | 177 | |
560f8377 | 178 | if ((pos >= selectBegin_) && (pos < selectEnd_)) { |
c7b76823 | 179 | painter.setBackground(selected); |
560f8377 | 180 | painter.setPen(palette().color(QPalette::HighlightedText)); |
c7b76823 | 181 | } else { |
560f8377 SA |
182 | painter.setBackground(regular); |
183 | painter.setPen(palette().color(QPalette::Text)); | |
c7b76823 SA |
184 | } |
185 | ||
560f8377 SA |
186 | // First nibble |
187 | QString val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF0) >> 4, 16).toUpper(); | |
188 | painter.drawText(x, y, val); | |
c7b76823 | 189 | |
560f8377 SA |
190 | // Second nibble |
191 | val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF), 16).toUpper(); | |
192 | painter.drawText(x + charWidth_, y, val); | |
c7b76823 | 193 | |
560f8377 | 194 | x += 3 * charWidth_; |
c7b76823 SA |
195 | } |
196 | ||
560f8377 SA |
197 | y += charHeight_; |
198 | } | |
199 | ||
200 | // Paint ASCII characters | |
201 | yStart = charHeight_; | |
202 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
203 | ||
204 | int x = posAscii_; | |
c7b76823 SA |
205 | for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size() && (i < BYTES_PER_LINE); i++) { |
206 | char ch = data[(unsigned int)((lineIdx - firstLineIdx) * BYTES_PER_LINE + i)]; | |
207 | ||
560f8377 | 208 | if ((ch < 0x20) || (ch > 0x7E)) |
c7b76823 SA |
209 | ch = '.'; |
210 | ||
560f8377 SA |
211 | size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2; |
212 | if ((pos >= selectBegin_) && (pos < selectEnd_)) { | |
213 | painter.setBackground(selected); | |
214 | painter.setPen(palette().color(QPalette::HighlightedText)); | |
215 | } else { | |
216 | painter.setBackground(regular); | |
217 | painter.setPen(palette().color(QPalette::Text)); | |
218 | } | |
219 | ||
220 | painter.drawText(x, y, QString(ch)); | |
221 | x += charWidth_; | |
c7b76823 SA |
222 | } |
223 | ||
560f8377 | 224 | y += charHeight_; |
c7b76823 SA |
225 | } |
226 | ||
560f8377 | 227 | // Paint cursor |
c7b76823 SA |
228 | if (hasFocus()) { |
229 | int x = (cursorPos_ % (2 * BYTES_PER_LINE)); | |
230 | int y = cursorPos_ / (2 * BYTES_PER_LINE); | |
231 | y -= firstLineIdx; | |
232 | int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_; | |
233 | int cursorY = y * charHeight_ + 4; | |
560f8377 | 234 | painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText)); |
c7b76823 SA |
235 | } |
236 | } | |
237 | ||
238 | void QHexView::keyPressEvent(QKeyEvent *event) | |
239 | { | |
240 | bool setVisible = false; | |
241 | ||
560f8377 | 242 | // Cursor movements |
c7b76823 SA |
243 | if (event->matches(QKeySequence::MoveToNextChar)) { |
244 | setCursorPos(cursorPos_ + 1); | |
245 | resetSelection(cursorPos_); | |
246 | setVisible = true; | |
247 | } | |
248 | if (event->matches(QKeySequence::MoveToPreviousChar)) { | |
249 | setCursorPos(cursorPos_ - 1); | |
250 | resetSelection(cursorPos_); | |
251 | setVisible = true; | |
252 | } | |
253 | ||
254 | if (event->matches(QKeySequence::MoveToEndOfLine)) { | |
255 | setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1)); | |
256 | resetSelection(cursorPos_); | |
257 | setVisible = true; | |
258 | } | |
259 | if (event->matches(QKeySequence::MoveToStartOfLine)) { | |
260 | setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2))); | |
261 | resetSelection(cursorPos_); | |
262 | setVisible = true; | |
263 | } | |
264 | if (event->matches(QKeySequence::MoveToPreviousLine)) { | |
265 | setCursorPos(cursorPos_ - BYTES_PER_LINE * 2); | |
266 | resetSelection(cursorPos_); | |
267 | setVisible = true; | |
268 | } | |
269 | if (event->matches(QKeySequence::MoveToNextLine)) { | |
270 | setCursorPos(cursorPos_ + BYTES_PER_LINE * 2); | |
271 | resetSelection(cursorPos_); | |
272 | setVisible = true; | |
273 | } | |
274 | ||
275 | if (event->matches(QKeySequence::MoveToNextPage)) { | |
276 | setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE); | |
277 | resetSelection(cursorPos_); | |
278 | setVisible = true; | |
279 | } | |
280 | if (event->matches(QKeySequence::MoveToPreviousPage)) { | |
281 | setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE); | |
282 | resetSelection(cursorPos_); | |
283 | setVisible = true; | |
284 | } | |
285 | if (event->matches(QKeySequence::MoveToEndOfDocument)) { | |
01ba5ed7 SA |
286 | if (data_) |
287 | setCursorPos(data_->count() * 2); | |
c7b76823 SA |
288 | resetSelection(cursorPos_); |
289 | setVisible = true; | |
290 | } | |
291 | if (event->matches(QKeySequence::MoveToStartOfDocument)) { | |
292 | setCursorPos(0); | |
293 | resetSelection(cursorPos_); | |
294 | setVisible = true; | |
295 | } | |
296 | ||
560f8377 | 297 | // Select commands |
c7b76823 SA |
298 | if (event->matches(QKeySequence::SelectAll)) { |
299 | resetSelection(0); | |
01ba5ed7 SA |
300 | if (data_) |
301 | setSelection(2 * data_->count() + 1); | |
c7b76823 SA |
302 | setVisible = true; |
303 | } | |
304 | if (event->matches(QKeySequence::SelectNextChar)) { | |
305 | int pos = cursorPos_ + 1; | |
306 | setCursorPos(pos); | |
307 | setSelection(pos); | |
308 | setVisible = true; | |
309 | } | |
310 | if (event->matches(QKeySequence::SelectPreviousChar)) { | |
311 | int pos = cursorPos_ - 1; | |
312 | setSelection(pos); | |
313 | setCursorPos(pos); | |
314 | setVisible = true; | |
315 | } | |
316 | if (event->matches(QKeySequence::SelectEndOfLine)) { | |
317 | int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE); | |
318 | setCursorPos(pos); | |
319 | setSelection(pos); | |
320 | setVisible = true; | |
321 | } | |
322 | if (event->matches(QKeySequence::SelectStartOfLine)) { | |
323 | int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)); | |
324 | setCursorPos(pos); | |
325 | setSelection(pos); | |
326 | setVisible = true; | |
327 | } | |
328 | if (event->matches(QKeySequence::SelectPreviousLine)) { | |
329 | int pos = cursorPos_ - (2 * BYTES_PER_LINE); | |
330 | setCursorPos(pos); | |
331 | setSelection(pos); | |
332 | setVisible = true; | |
333 | } | |
334 | if (event->matches(QKeySequence::SelectNextLine)) { | |
335 | int pos = cursorPos_ + (2 * BYTES_PER_LINE); | |
336 | setCursorPos(pos); | |
337 | setSelection(pos); | |
338 | setVisible = true; | |
339 | } | |
340 | ||
341 | if (event->matches(QKeySequence::SelectNextPage)) { | |
342 | int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE); | |
343 | setCursorPos(pos); | |
344 | setSelection(pos); | |
345 | setVisible = true; | |
346 | } | |
347 | if (event->matches(QKeySequence::SelectPreviousPage)) { | |
348 | int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE); | |
349 | setCursorPos(pos); | |
350 | setSelection(pos); | |
351 | setVisible = true; | |
352 | } | |
353 | if (event->matches(QKeySequence::SelectEndOfDocument)) { | |
354 | int pos = 0; | |
01ba5ed7 SA |
355 | if (data_) |
356 | pos = data_->count() * 2; | |
c7b76823 SA |
357 | setCursorPos(pos); |
358 | setSelection(pos); | |
359 | setVisible = true; | |
360 | } | |
361 | if (event->matches(QKeySequence::SelectStartOfDocument)) { | |
362 | int pos = 0; | |
363 | setCursorPos(pos); | |
364 | setSelection(pos); | |
365 | setVisible = true; | |
366 | } | |
367 | ||
01ba5ed7 | 368 | if (event->matches(QKeySequence::Copy) && (data_)) { |
c7b76823 SA |
369 | QString text; |
370 | int idx = 0; | |
371 | int copyOffset = 0; | |
372 | ||
01ba5ed7 | 373 | QByteArray data = data_->mid(selectBegin_ / 2, (selectEnd_ - selectBegin_) / 2 + 1); |
c7b76823 SA |
374 | if (selectBegin_ % 2) { |
375 | text += QString::number((data.at((idx+1) / 2) & 0xF), 16); | |
376 | text += " "; | |
377 | idx++; | |
378 | copyOffset = 1; | |
379 | } | |
380 | ||
381 | int selectedSize = selectEnd_ - selectBegin_; | |
382 | while (idx < selectedSize) { | |
383 | QString val = QString::number((data.at((copyOffset + idx) / 2) & 0xF0) >> 4, 16); | |
384 | if ((idx + 1) < selectedSize) { | |
385 | val += QString::number((data.at((copyOffset + idx) / 2) & 0xF), 16); | |
386 | val += " "; | |
387 | } | |
388 | text += val; | |
389 | ||
390 | if ((idx/2) % BYTES_PER_LINE == (BYTES_PER_LINE - 1)) | |
391 | text += "\n"; | |
392 | ||
393 | idx += 2; | |
394 | } | |
395 | ||
396 | QClipboard *clipboard = QApplication::clipboard(); | |
397 | clipboard->setText(text); | |
398 | if (clipboard->supportsSelection()) | |
399 | clipboard->setText(text); | |
400 | } | |
401 | ||
402 | if (setVisible) | |
403 | ensureVisible(); | |
404 | ||
405 | viewport()->update(); | |
406 | } | |
407 | ||
408 | void QHexView::mouseMoveEvent(QMouseEvent *event) | |
409 | { | |
560f8377 | 410 | int actPos = cursorPosFromMousePos(event->pos()); |
c7b76823 SA |
411 | setCursorPos(actPos); |
412 | setSelection(actPos); | |
413 | ||
414 | viewport()->update(); | |
415 | } | |
416 | ||
417 | void QHexView::mousePressEvent(QMouseEvent *event) | |
418 | { | |
560f8377 | 419 | int cPos = cursorPosFromMousePos(event->pos()); |
c7b76823 SA |
420 | |
421 | if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton)) | |
422 | setSelection(cPos); | |
423 | else | |
424 | resetSelection(cPos); | |
425 | ||
426 | setCursorPos(cPos); | |
427 | ||
428 | viewport()->update(); | |
429 | } | |
430 | ||
560f8377 | 431 | size_t QHexView::cursorPosFromMousePos(const QPoint &position) |
c7b76823 SA |
432 | { |
433 | int pos = -1; | |
434 | ||
435 | if (((size_t)position.x() >= posHex_) && | |
436 | ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) { | |
437 | ||
560f8377 SA |
438 | // Note: We add 1.5 character widths so that selection across |
439 | // byte gaps is smoother | |
440 | int x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_; | |
c7b76823 | 441 | |
560f8377 SA |
442 | // Note: We allow only full bytes to be selected, not nibbles, |
443 | // so we round to the nearest byte gap | |
444 | x = (2 * x + 1) / 3; | |
c7b76823 SA |
445 | |
446 | int firstLineIdx = verticalScrollBar()->value(); | |
447 | int y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE; | |
448 | pos = x + y + firstLineIdx * BYTES_PER_LINE * 2; | |
449 | } | |
450 | ||
451 | return pos; | |
452 | } | |
453 | ||
454 | void QHexView::resetSelection() | |
455 | { | |
456 | selectBegin_ = selectInit_; | |
457 | selectEnd_ = selectInit_; | |
458 | } | |
459 | ||
460 | void QHexView::resetSelection(int pos) | |
461 | { | |
462 | if (pos < 0) | |
463 | pos = 0; | |
464 | ||
465 | selectInit_ = pos; | |
466 | selectBegin_ = pos; | |
467 | selectEnd_ = pos; | |
468 | } | |
469 | ||
470 | void QHexView::setSelection(int pos) | |
471 | { | |
472 | if (pos < 0) | |
473 | pos = 0; | |
474 | ||
475 | if ((size_t)pos >= selectInit_) { | |
476 | selectEnd_ = pos; | |
477 | selectBegin_ = selectInit_; | |
478 | } else { | |
479 | selectBegin_ = pos; | |
480 | selectEnd_ = selectInit_; | |
481 | } | |
482 | } | |
483 | ||
484 | void QHexView::setCursorPos(int position) | |
485 | { | |
486 | if (position < 0) | |
487 | position = 0; | |
488 | ||
489 | int maxPos = 0; | |
01ba5ed7 SA |
490 | if (data_) { |
491 | maxPos = data_->count() * 2; | |
492 | if (data_->count() % BYTES_PER_LINE) | |
c7b76823 SA |
493 | maxPos++; |
494 | } | |
495 | ||
496 | if (position > maxPos) | |
497 | position = maxPos; | |
498 | ||
499 | cursorPos_ = position; | |
500 | } | |
501 | ||
502 | void QHexView::ensureVisible() | |
503 | { | |
504 | QSize areaSize = viewport()->size(); | |
505 | ||
506 | int firstLineIdx = verticalScrollBar() -> value(); | |
507 | int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_; | |
508 | ||
509 | int cursorY = cursorPos_ / (2 * BYTES_PER_LINE); | |
510 | ||
511 | if (cursorY < firstLineIdx) | |
512 | verticalScrollBar() -> setValue(cursorY); | |
513 | else if(cursorY >= lastLineIdx) | |
514 | verticalScrollBar() -> setValue(cursorY - areaSize.height() / charHeight_ + 1); | |
515 | } |