]>
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 | |
a7af8bb0 SA |
366 | if ((pos >= selectBegin_) && (pos < selectEnd_ - 1) && (i < BYTES_PER_LINE - 1)) |
367 | painter.drawText(x + 2 * charWidth_, y, QString(' ')); | |
368 | ||
560f8377 | 369 | x += 3 * charWidth_; |
c7b76823 SA |
370 | } |
371 | ||
560f8377 SA |
372 | y += charHeight_; |
373 | } | |
374 | ||
375 | // Paint ASCII characters | |
628b45cc | 376 | initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE); |
560f8377 SA |
377 | yStart = charHeight_; |
378 | for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { | |
379 | ||
380 | int x = posAscii_; | |
cbf428c2 | 381 | for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) { |
628b45cc SA |
382 | // Fetch byte |
383 | uint8_t ch = get_next_byte(); | |
c7b76823 | 384 | |
560f8377 | 385 | if ((ch < 0x20) || (ch > 0x7E)) |
c7b76823 SA |
386 | ch = '.'; |
387 | ||
560f8377 SA |
388 | size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2; |
389 | if ((pos >= selectBegin_) && (pos < selectEnd_)) { | |
a61abf09 | 390 | painter.setBackgroundMode(Qt::OpaqueMode); |
560f8377 SA |
391 | painter.setBackground(selected); |
392 | painter.setPen(palette().color(QPalette::HighlightedText)); | |
393 | } else { | |
a61abf09 | 394 | painter.setBackgroundMode(Qt::TransparentMode); |
560f8377 SA |
395 | painter.setBackground(regular); |
396 | painter.setPen(palette().color(QPalette::Text)); | |
397 | } | |
398 | ||
399 | painter.drawText(x, y, QString(ch)); | |
400 | x += charWidth_; | |
c7b76823 SA |
401 | } |
402 | ||
560f8377 | 403 | y += charHeight_; |
c7b76823 SA |
404 | } |
405 | ||
560f8377 | 406 | // Paint cursor |
c7b76823 SA |
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; | |
560f8377 | 413 | painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText)); |
c7b76823 SA |
414 | } |
415 | } | |
416 | ||
417 | void QHexView::keyPressEvent(QKeyEvent *event) | |
418 | { | |
419 | bool setVisible = false; | |
420 | ||
560f8377 | 421 | // Cursor movements |
c7b76823 SA |
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)) { | |
628b45cc | 465 | setCursorPos(data_size_ * 2); |
c7b76823 SA |
466 | resetSelection(cursorPos_); |
467 | setVisible = true; | |
468 | } | |
469 | if (event->matches(QKeySequence::MoveToStartOfDocument)) { | |
470 | setCursorPos(0); | |
471 | resetSelection(cursorPos_); | |
472 | setVisible = true; | |
473 | } | |
474 | ||
560f8377 | 475 | // Select commands |
c7b76823 SA |
476 | if (event->matches(QKeySequence::SelectAll)) { |
477 | resetSelection(0); | |
628b45cc | 478 | setSelection(2 * data_size_); |
c7b76823 SA |
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)) { | |
628b45cc | 531 | int pos = data_size_ * 2; |
c7b76823 SA |
532 | setCursorPos(pos); |
533 | setSelection(pos); | |
534 | setVisible = true; | |
535 | } | |
536 | if (event->matches(QKeySequence::SelectStartOfDocument)) { | |
628b45cc SA |
537 | setCursorPos(0); |
538 | setSelection(0); | |
c7b76823 SA |
539 | setVisible = true; |
540 | } | |
541 | ||
01ba5ed7 | 542 | if (event->matches(QKeySequence::Copy) && (data_)) { |
c7b76823 | 543 | QString text; |
c7b76823 | 544 | |
628b45cc | 545 | initialize_byte_iterator(selectBegin_ / 2); |
c7b76823 | 546 | |
628b45cc SA |
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; | |
c7b76823 | 554 | |
628b45cc SA |
555 | if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1)) |
556 | text += "\n"; | |
c7b76823 SA |
557 | } |
558 | ||
559 | QClipboard *clipboard = QApplication::clipboard(); | |
628b45cc | 560 | clipboard->setText(text, QClipboard::Clipboard); |
c7b76823 | 561 | if (clipboard->supportsSelection()) |
628b45cc | 562 | clipboard->setText(text, QClipboard::Selection); |
c7b76823 SA |
563 | } |
564 | ||
565 | if (setVisible) | |
566 | ensureVisible(); | |
567 | ||
568 | viewport()->update(); | |
569 | } | |
570 | ||
571 | void QHexView::mouseMoveEvent(QMouseEvent *event) | |
572 | { | |
560f8377 | 573 | int actPos = cursorPosFromMousePos(event->pos()); |
c7b76823 SA |
574 | setCursorPos(actPos); |
575 | setSelection(actPos); | |
576 | ||
577 | viewport()->update(); | |
578 | } | |
579 | ||
580 | void QHexView::mousePressEvent(QMouseEvent *event) | |
581 | { | |
560f8377 | 582 | int cPos = cursorPosFromMousePos(event->pos()); |
c7b76823 SA |
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 | ||
560f8377 | 594 | size_t QHexView::cursorPosFromMousePos(const QPoint &position) |
c7b76823 | 595 | { |
628b45cc | 596 | size_t pos = -1; |
c7b76823 SA |
597 | |
598 | if (((size_t)position.x() >= posHex_) && | |
599 | ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) { | |
600 | ||
560f8377 SA |
601 | // Note: We add 1.5 character widths so that selection across |
602 | // byte gaps is smoother | |
628b45cc | 603 | size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_; |
c7b76823 | 604 | |
560f8377 SA |
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; | |
c7b76823 | 608 | |
628b45cc SA |
609 | size_t firstLineIdx = verticalScrollBar()->value(); |
610 | size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE; | |
c7b76823 SA |
611 | pos = x + y + firstLineIdx * BYTES_PER_LINE * 2; |
612 | } | |
613 | ||
628b45cc SA |
614 | size_t max_pos = data_size_ * 2; |
615 | ||
616 | return std::min(pos, max_pos); | |
c7b76823 SA |
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 | ||
628b45cc | 654 | int max_pos = data_size_ * 2; |
c7b76823 | 655 | |
628b45cc SA |
656 | if (position > max_pos) |
657 | position = max_pos; | |
c7b76823 SA |
658 | |
659 | cursorPos_ = position; | |
660 | } | |
661 | ||
662 | void QHexView::ensureVisible() | |
663 | { | |
664 | QSize areaSize = viewport()->size(); | |
665 | ||
628b45cc | 666 | int firstLineIdx = verticalScrollBar()->value(); |
c7b76823 SA |
667 | int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_; |
668 | ||
669 | int cursorY = cursorPos_ / (2 * BYTES_PER_LINE); | |
670 | ||
671 | if (cursorY < firstLineIdx) | |
628b45cc SA |
672 | verticalScrollBar()->setValue(cursorY); |
673 | else | |
674 | if(cursorY >= lastLineIdx) | |
675 | verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1); | |
c7b76823 | 676 | } |