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