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