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