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