]>
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> | |
c8a6db0e | 32 | #include <QDebug> |
c7b76823 SA |
33 | #include <QFont> |
34 | #include <QKeyEvent> | |
35 | #include <QScrollBar> | |
36 | #include <QSize> | |
37 | #include <QPainter> | |
38 | #include <QPaintEvent> | |
39 | ||
40 | #include "QHexView.hpp" | |
41 | ||
560f8377 SA |
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; | |
c7b76823 | 47 | |
c7b76823 SA |
48 | |
49 | QHexView::QHexView(QWidget *parent): | |
560f8377 | 50 | QAbstractScrollArea(parent), |
628b45cc SA |
51 | mode_(ChunkedDataMode), |
52 | data_(nullptr), | |
53 | selectBegin_(0), | |
54 | selectEnd_(0), | |
55 | cursorPos_(0) | |
c7b76823 SA |
56 | { |
57 | setFont(QFont("Courier", 10)); | |
58 | ||
560f8377 | 59 | charWidth_ = fontMetrics().boundingRect('X').width(); |
c7b76823 SA |
60 | charHeight_ = fontMetrics().height(); |
61 | ||
560f8377 SA |
62 | // Determine X coordinates of the three sub-areas |
63 | posAddr_ = 0; | |
64 | posHex_ = 10 * charWidth_ + GAP_ADR_HEX; | |
c7b76823 SA |
65 | posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII; |
66 | ||
c7b76823 | 67 | setFocusPolicy(Qt::StrongFocus); |
a61abf09 SA |
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 | } | |
c7b76823 SA |
80 | } |
81 | ||
d5645564 | 82 | void QHexView::set_mode(Mode m) |
c7b76823 | 83 | { |
628b45cc SA |
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 | } | |
c7b76823 | 89 | |
d5645564 | 90 | void QHexView::set_data(const DecodeBinaryClass* data) |
628b45cc | 91 | { |
01ba5ed7 | 92 | data_ = data; |
628b45cc | 93 | |
c8a6db0e SA |
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; | |
628b45cc SA |
99 | |
100 | viewport()->update(); | |
101 | } | |
102 | ||
d5645564 SA |
103 | unsigned int QHexView::get_bytes_per_line() const |
104 | { | |
105 | return BYTES_PER_LINE; | |
106 | } | |
107 | ||
628b45cc SA |
108 | void QHexView::clear() |
109 | { | |
110 | verticalScrollBar()->setValue(0); | |
111 | data_ = nullptr; | |
112 | data_size_ = 0; | |
b2b18d3a SA |
113 | |
114 | viewport()->update(); | |
c7b76823 SA |
115 | } |
116 | ||
117 | void QHexView::showFromOffset(size_t offset) | |
118 | { | |
628b45cc | 119 | if (data_ && (offset < data_size_)) { |
c7b76823 SA |
120 | setCursorPos(offset * 2); |
121 | ||
122 | int cursorY = cursorPos_ / (2 * BYTES_PER_LINE); | |
123 | verticalScrollBar() -> setValue(cursorY); | |
124 | } | |
4b97fe09 SA |
125 | |
126 | viewport()->update(); | |
c7b76823 SA |
127 | } |
128 | ||
6961eab0 SA |
129 | QSizePolicy QHexView::sizePolicy() const |
130 | { | |
131 | return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); | |
132 | } | |
133 | ||
13b726cd SA |
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 | ||
d5645564 SA |
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 | ||
628b45cc | 190 | void QHexView::initialize_byte_iterator(size_t offset) |
c7b76823 | 191 | { |
628b45cc SA |
192 | current_chunk_id_ = 0; |
193 | current_chunk_offset_ = 0; | |
cbf428c2 | 194 | current_offset_ = offset; |
628b45cc | 195 | |
c8a6db0e SA |
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) { | |
628b45cc | 201 | current_chunk_id_++; |
c8a6db0e | 202 | offset -= size; |
628b45cc SA |
203 | } else { |
204 | current_chunk_offset_ = offset; | |
205 | break; | |
206 | } | |
c8a6db0e | 207 | } |
4b97fe09 | 208 | |
c8a6db0e SA |
209 | if (current_chunk_id_ < data_->chunks.size()) |
210 | current_chunk_ = data_->chunks[current_chunk_id_]; | |
628b45cc SA |
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 | ||
c8a6db0e SA |
218 | uint8_t v = 0; |
219 | if (current_chunk_offset_ < current_chunk_.data.size()) | |
220 | v = current_chunk_.data[current_chunk_offset_]; | |
628b45cc | 221 | |
cbf428c2 | 222 | current_offset_++; |
628b45cc | 223 | current_chunk_offset_++; |
cbf428c2 | 224 | |
c8a6db0e SA |
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_)) { | |
628b45cc SA |
232 | current_chunk_id_++; |
233 | current_chunk_offset_ = 0; | |
516d2128 | 234 | current_chunk_ = data_->chunks[current_chunk_id_]; |
628b45cc SA |
235 | } |
236 | ||
237 | return v; | |
c7b76823 SA |
238 | } |
239 | ||
560f8377 | 240 | QSize QHexView::getFullSize() const |
c7b76823 | 241 | { |
fae7037a | 242 | size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_); |
560f8377 | 243 | |
fae7037a SA |
244 | if (verticalScrollBar()->isEnabled()) |
245 | width += GAP_ASCII_SLIDER + verticalScrollBar()->width(); | |
246 | ||
247 | if (!data_ || (data_size_ == 0)) | |
01ba5ed7 | 248 | return QSize(width, 0); |
560f8377 | 249 | |
628b45cc | 250 | size_t height = data_size_ / BYTES_PER_LINE; |
01ba5ed7 | 251 | |
628b45cc | 252 | if (data_size_ % BYTES_PER_LINE) |
c7b76823 SA |
253 | height++; |
254 | ||
255 | height *= charHeight_; | |
256 | ||
257 | return QSize(width, height); | |
258 | } | |
259 | ||
260 | void QHexView::paintEvent(QPaintEvent *event) | |
261 | { | |
c7b76823 SA |
262 | QPainter painter(viewport()); |
263 | ||
560f8377 | 264 | // Calculate and update the widget and paint area sizes |
560f8377 SA |
265 | QSize widgetSize = getFullSize(); |
266 | setMinimumWidth(widgetSize.width()); | |
267 | setMaximumWidth(widgetSize.width()); | |
01ba5ed7 | 268 | QSize areaSize = viewport()->size(); |
560f8377 | 269 | |
fae7037a SA |
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); | |
c7b76823 | 277 | |
01ba5ed7 SA |
278 | // Fill widget background |
279 | painter.fillRect(event->rect(), palette().color(QPalette::Base)); | |
280 | ||
628b45cc | 281 | if (!data_ || (data_size_ == 0)) { |
01ba5ed7 SA |
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 | |
c7b76823 SA |
291 | size_t firstLineIdx = verticalScrollBar()->value(); |
292 | ||
293 | size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_; | |
628b45cc SA |
294 | if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) { |
295 | lastLineIdx = data_size_ / BYTES_PER_LINE; | |
296 | if (data_size_ % BYTES_PER_LINE) | |
c7b76823 SA |
297 | lastLineIdx++; |
298 | } | |
299 | ||
01ba5ed7 | 300 | // Fill address area background |
c7b76823 | 301 | painter.fillRect(QRect(posAddr_, event->rect().top(), |
560f8377 | 302 | posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window)); |
c7b76823 | 303 | |
560f8377 SA |
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()); | |
c7b76823 | 308 | |
560f8377 SA |
309 | // Paint address area |
310 | painter.setPen(palette().color(QPalette::ButtonText)); | |
c7b76823 | 311 | |
560f8377 SA |
312 | int yStart = charHeight_; |
313 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
c7b76823 | 314 | |
560f8377 SA |
315 | QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper(); |
316 | painter.drawText(posAddr_, y, address); | |
317 | y += charHeight_; | |
318 | } | |
c7b76823 | 319 | |
560f8377 | 320 | // Paint hex values |
a61abf09 SA |
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; | |
c7b76823 | 326 | |
628b45cc | 327 | initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE); |
560f8377 SA |
328 | yStart = charHeight_; |
329 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
330 | ||
560f8377 | 331 | int x = posHex_; |
cbf428c2 | 332 | for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) { |
c7b76823 | 333 | size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2; |
c7b76823 | 334 | |
a61abf09 SA |
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 | ||
560f8377 | 345 | if ((pos >= selectBegin_) && (pos < selectEnd_)) { |
a61abf09 | 346 | painter.setBackgroundMode(Qt::OpaqueMode); |
c7b76823 | 347 | painter.setBackground(selected); |
560f8377 | 348 | painter.setPen(palette().color(QPalette::HighlightedText)); |
c7b76823 | 349 | } else { |
560f8377 | 350 | painter.setBackground(regular); |
a61abf09 SA |
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]); | |
c7b76823 SA |
356 | } |
357 | ||
560f8377 | 358 | // First nibble |
628b45cc | 359 | QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper(); |
560f8377 | 360 | painter.drawText(x, y, val); |
c7b76823 | 361 | |
560f8377 | 362 | // Second nibble |
628b45cc | 363 | val = QString::number((byte_value & 0xF), 16).toUpper(); |
560f8377 | 364 | painter.drawText(x + charWidth_, y, val); |
c7b76823 | 365 | |
560f8377 | 366 | x += 3 * charWidth_; |
c7b76823 SA |
367 | } |
368 | ||
560f8377 SA |
369 | y += charHeight_; |
370 | } | |
371 | ||
372 | // Paint ASCII characters | |
628b45cc | 373 | initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE); |
560f8377 SA |
374 | yStart = charHeight_; |
375 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
376 | ||
377 | int x = posAscii_; | |
cbf428c2 | 378 | for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) { |
628b45cc SA |
379 | // Fetch byte |
380 | uint8_t ch = get_next_byte(); | |
c7b76823 | 381 | |
560f8377 | 382 | if ((ch < 0x20) || (ch > 0x7E)) |
c7b76823 SA |
383 | ch = '.'; |
384 | ||
560f8377 SA |
385 | size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2; |
386 | if ((pos >= selectBegin_) && (pos < selectEnd_)) { | |
a61abf09 | 387 | painter.setBackgroundMode(Qt::OpaqueMode); |
560f8377 SA |
388 | painter.setBackground(selected); |
389 | painter.setPen(palette().color(QPalette::HighlightedText)); | |
390 | } else { | |
a61abf09 | 391 | painter.setBackgroundMode(Qt::TransparentMode); |
560f8377 SA |
392 | painter.setBackground(regular); |
393 | painter.setPen(palette().color(QPalette::Text)); | |
394 | } | |
395 | ||
396 | painter.drawText(x, y, QString(ch)); | |
397 | x += charWidth_; | |
c7b76823 SA |
398 | } |
399 | ||
560f8377 | 400 | y += charHeight_; |
c7b76823 SA |
401 | } |
402 | ||
560f8377 | 403 | // Paint cursor |
c7b76823 SA |
404 | if (hasFocus()) { |
405 | int x = (cursorPos_ % (2 * BYTES_PER_LINE)); | |
406 | int y = cursorPos_ / (2 * BYTES_PER_LINE); | |
407 | y -= firstLineIdx; | |
408 | int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_; | |
409 | int cursorY = y * charHeight_ + 4; | |
560f8377 | 410 | painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText)); |
c7b76823 SA |
411 | } |
412 | } | |
413 | ||
414 | void QHexView::keyPressEvent(QKeyEvent *event) | |
415 | { | |
416 | bool setVisible = false; | |
417 | ||
560f8377 | 418 | // Cursor movements |
c7b76823 SA |
419 | if (event->matches(QKeySequence::MoveToNextChar)) { |
420 | setCursorPos(cursorPos_ + 1); | |
421 | resetSelection(cursorPos_); | |
422 | setVisible = true; | |
423 | } | |
424 | if (event->matches(QKeySequence::MoveToPreviousChar)) { | |
425 | setCursorPos(cursorPos_ - 1); | |
426 | resetSelection(cursorPos_); | |
427 | setVisible = true; | |
428 | } | |
429 | ||
430 | if (event->matches(QKeySequence::MoveToEndOfLine)) { | |
431 | setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1)); | |
432 | resetSelection(cursorPos_); | |
433 | setVisible = true; | |
434 | } | |
435 | if (event->matches(QKeySequence::MoveToStartOfLine)) { | |
436 | setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2))); | |
437 | resetSelection(cursorPos_); | |
438 | setVisible = true; | |
439 | } | |
440 | if (event->matches(QKeySequence::MoveToPreviousLine)) { | |
441 | setCursorPos(cursorPos_ - BYTES_PER_LINE * 2); | |
442 | resetSelection(cursorPos_); | |
443 | setVisible = true; | |
444 | } | |
445 | if (event->matches(QKeySequence::MoveToNextLine)) { | |
446 | setCursorPos(cursorPos_ + BYTES_PER_LINE * 2); | |
447 | resetSelection(cursorPos_); | |
448 | setVisible = true; | |
449 | } | |
450 | ||
451 | if (event->matches(QKeySequence::MoveToNextPage)) { | |
452 | setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE); | |
453 | resetSelection(cursorPos_); | |
454 | setVisible = true; | |
455 | } | |
456 | if (event->matches(QKeySequence::MoveToPreviousPage)) { | |
457 | setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE); | |
458 | resetSelection(cursorPos_); | |
459 | setVisible = true; | |
460 | } | |
461 | if (event->matches(QKeySequence::MoveToEndOfDocument)) { | |
628b45cc | 462 | setCursorPos(data_size_ * 2); |
c7b76823 SA |
463 | resetSelection(cursorPos_); |
464 | setVisible = true; | |
465 | } | |
466 | if (event->matches(QKeySequence::MoveToStartOfDocument)) { | |
467 | setCursorPos(0); | |
468 | resetSelection(cursorPos_); | |
469 | setVisible = true; | |
470 | } | |
471 | ||
560f8377 | 472 | // Select commands |
c7b76823 SA |
473 | if (event->matches(QKeySequence::SelectAll)) { |
474 | resetSelection(0); | |
628b45cc | 475 | setSelection(2 * data_size_); |
c7b76823 SA |
476 | setVisible = true; |
477 | } | |
478 | if (event->matches(QKeySequence::SelectNextChar)) { | |
479 | int pos = cursorPos_ + 1; | |
480 | setCursorPos(pos); | |
481 | setSelection(pos); | |
482 | setVisible = true; | |
483 | } | |
484 | if (event->matches(QKeySequence::SelectPreviousChar)) { | |
485 | int pos = cursorPos_ - 1; | |
486 | setSelection(pos); | |
487 | setCursorPos(pos); | |
488 | setVisible = true; | |
489 | } | |
490 | if (event->matches(QKeySequence::SelectEndOfLine)) { | |
491 | int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE); | |
492 | setCursorPos(pos); | |
493 | setSelection(pos); | |
494 | setVisible = true; | |
495 | } | |
496 | if (event->matches(QKeySequence::SelectStartOfLine)) { | |
497 | int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)); | |
498 | setCursorPos(pos); | |
499 | setSelection(pos); | |
500 | setVisible = true; | |
501 | } | |
502 | if (event->matches(QKeySequence::SelectPreviousLine)) { | |
503 | int pos = cursorPos_ - (2 * BYTES_PER_LINE); | |
504 | setCursorPos(pos); | |
505 | setSelection(pos); | |
506 | setVisible = true; | |
507 | } | |
508 | if (event->matches(QKeySequence::SelectNextLine)) { | |
509 | int pos = cursorPos_ + (2 * BYTES_PER_LINE); | |
510 | setCursorPos(pos); | |
511 | setSelection(pos); | |
512 | setVisible = true; | |
513 | } | |
514 | ||
515 | if (event->matches(QKeySequence::SelectNextPage)) { | |
516 | int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE); | |
517 | setCursorPos(pos); | |
518 | setSelection(pos); | |
519 | setVisible = true; | |
520 | } | |
521 | if (event->matches(QKeySequence::SelectPreviousPage)) { | |
522 | int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE); | |
523 | setCursorPos(pos); | |
524 | setSelection(pos); | |
525 | setVisible = true; | |
526 | } | |
527 | if (event->matches(QKeySequence::SelectEndOfDocument)) { | |
628b45cc | 528 | int pos = data_size_ * 2; |
c7b76823 SA |
529 | setCursorPos(pos); |
530 | setSelection(pos); | |
531 | setVisible = true; | |
532 | } | |
533 | if (event->matches(QKeySequence::SelectStartOfDocument)) { | |
628b45cc SA |
534 | setCursorPos(0); |
535 | setSelection(0); | |
c7b76823 SA |
536 | setVisible = true; |
537 | } | |
538 | ||
01ba5ed7 | 539 | if (event->matches(QKeySequence::Copy) && (data_)) { |
c7b76823 | 540 | QString text; |
c7b76823 | 541 | |
628b45cc | 542 | initialize_byte_iterator(selectBegin_ / 2); |
c7b76823 | 543 | |
628b45cc SA |
544 | size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2; |
545 | for (size_t i = 0; i < selectedSize; i++) { | |
546 | uint8_t byte_value = get_next_byte(); | |
547 | ||
548 | QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() + | |
549 | QString::number((byte_value & 0xF), 16).toUpper() + " "; | |
550 | text += s; | |
c7b76823 | 551 | |
628b45cc SA |
552 | if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1)) |
553 | text += "\n"; | |
c7b76823 SA |
554 | } |
555 | ||
556 | QClipboard *clipboard = QApplication::clipboard(); | |
628b45cc | 557 | clipboard->setText(text, QClipboard::Clipboard); |
c7b76823 | 558 | if (clipboard->supportsSelection()) |
628b45cc | 559 | clipboard->setText(text, QClipboard::Selection); |
c7b76823 SA |
560 | } |
561 | ||
562 | if (setVisible) | |
563 | ensureVisible(); | |
564 | ||
565 | viewport()->update(); | |
566 | } | |
567 | ||
568 | void QHexView::mouseMoveEvent(QMouseEvent *event) | |
569 | { | |
560f8377 | 570 | int actPos = cursorPosFromMousePos(event->pos()); |
c7b76823 SA |
571 | setCursorPos(actPos); |
572 | setSelection(actPos); | |
573 | ||
574 | viewport()->update(); | |
575 | } | |
576 | ||
577 | void QHexView::mousePressEvent(QMouseEvent *event) | |
578 | { | |
560f8377 | 579 | int cPos = cursorPosFromMousePos(event->pos()); |
c7b76823 SA |
580 | |
581 | if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton)) | |
582 | setSelection(cPos); | |
583 | else | |
584 | resetSelection(cPos); | |
585 | ||
586 | setCursorPos(cPos); | |
587 | ||
588 | viewport()->update(); | |
589 | } | |
590 | ||
560f8377 | 591 | size_t QHexView::cursorPosFromMousePos(const QPoint &position) |
c7b76823 | 592 | { |
628b45cc | 593 | size_t pos = -1; |
c7b76823 SA |
594 | |
595 | if (((size_t)position.x() >= posHex_) && | |
596 | ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) { | |
597 | ||
560f8377 SA |
598 | // Note: We add 1.5 character widths so that selection across |
599 | // byte gaps is smoother | |
628b45cc | 600 | size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_; |
c7b76823 | 601 | |
560f8377 SA |
602 | // Note: We allow only full bytes to be selected, not nibbles, |
603 | // so we round to the nearest byte gap | |
604 | x = (2 * x + 1) / 3; | |
c7b76823 | 605 | |
628b45cc SA |
606 | size_t firstLineIdx = verticalScrollBar()->value(); |
607 | size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE; | |
c7b76823 SA |
608 | pos = x + y + firstLineIdx * BYTES_PER_LINE * 2; |
609 | } | |
610 | ||
628b45cc SA |
611 | size_t max_pos = data_size_ * 2; |
612 | ||
613 | return std::min(pos, max_pos); | |
c7b76823 SA |
614 | } |
615 | ||
616 | void QHexView::resetSelection() | |
617 | { | |
618 | selectBegin_ = selectInit_; | |
619 | selectEnd_ = selectInit_; | |
620 | } | |
621 | ||
622 | void QHexView::resetSelection(int pos) | |
623 | { | |
624 | if (pos < 0) | |
625 | pos = 0; | |
626 | ||
627 | selectInit_ = pos; | |
628 | selectBegin_ = pos; | |
629 | selectEnd_ = pos; | |
630 | } | |
631 | ||
632 | void QHexView::setSelection(int pos) | |
633 | { | |
634 | if (pos < 0) | |
635 | pos = 0; | |
636 | ||
637 | if ((size_t)pos >= selectInit_) { | |
638 | selectEnd_ = pos; | |
639 | selectBegin_ = selectInit_; | |
640 | } else { | |
641 | selectBegin_ = pos; | |
642 | selectEnd_ = selectInit_; | |
643 | } | |
644 | } | |
645 | ||
646 | void QHexView::setCursorPos(int position) | |
647 | { | |
648 | if (position < 0) | |
649 | position = 0; | |
650 | ||
628b45cc | 651 | int max_pos = data_size_ * 2; |
c7b76823 | 652 | |
628b45cc SA |
653 | if (position > max_pos) |
654 | position = max_pos; | |
c7b76823 SA |
655 | |
656 | cursorPos_ = position; | |
657 | } | |
658 | ||
659 | void QHexView::ensureVisible() | |
660 | { | |
661 | QSize areaSize = viewport()->size(); | |
662 | ||
628b45cc | 663 | int firstLineIdx = verticalScrollBar()->value(); |
c7b76823 SA |
664 | int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_; |
665 | ||
666 | int cursorY = cursorPos_ / (2 * BYTES_PER_LINE); | |
667 | ||
668 | if (cursorY < firstLineIdx) | |
628b45cc SA |
669 | verticalScrollBar()->setValue(cursorY); |
670 | else | |
671 | if(cursorY >= lastLineIdx) | |
672 | verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1); | |
c7b76823 | 673 | } |