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