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