2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Victor Anjin <virinext@gmail.com>
5 * Copyright (C) 2019 Soeren Apel <soeren@apelpie.net>
7 * The MIT License (MIT)
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:
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
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
30 #include <QApplication>
37 #include <QPaintEvent>
39 #include "QHexView.hpp"
43 const unsigned int HEXCHARS_IN_LINE = 47;
44 const unsigned int GAP_ADR_HEX = 10;
45 const unsigned int GAP_HEX_ASCII = 16;
46 const unsigned int BYTES_PER_LINE = 16;
49 DataStorageArray::DataStorageArray(const QByteArray &arr)
54 QByteArray DataStorageArray::getData(size_t position, size_t length)
56 return data_.mid(position, length);
59 size_t DataStorageArray::size()
66 QHexView::QHexView(QWidget *parent):
67 QAbstractScrollArea(parent),
70 setFont(QFont("Courier", 10));
72 charWidth_ = fontMetrics().width(QLatin1Char('9'));
73 charHeight_ = fontMetrics().height();
76 posHex_ = 10 * charWidth_ + GAP_ADR_HEX;
77 posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
79 setMinimumWidth(posAscii_ + (BYTES_PER_LINE * charWidth_));
81 setFocusPolicy(Qt::StrongFocus);
90 void QHexView::setData(DataStorage *pData)
92 verticalScrollBar()->setValue(0);
102 void QHexView::showFromOffset(size_t offset)
104 if (pdata_ && (offset < pdata_->size())) {
105 setCursorPos(offset * 2);
107 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
108 verticalScrollBar() -> setValue(cursorY);
112 void QHexView::clear()
114 verticalScrollBar()->setValue(0);
117 QSize QHexView::fullSize() const
122 size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_);
123 size_t height = pdata_->size() / BYTES_PER_LINE;
124 if (pdata_->size() % BYTES_PER_LINE)
127 height *= charHeight_;
129 return QSize(width, height);
132 void QHexView::paintEvent(QPaintEvent *event)
137 QPainter painter(viewport());
139 QSize areaSize = viewport()->size();
140 QSize widgetSize = fullSize();
141 verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
142 verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
144 size_t firstLineIdx = verticalScrollBar()->value();
146 size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
147 if (lastLineIdx > (pdata_->size() / BYTES_PER_LINE)) {
148 lastLineIdx = pdata_->size() / BYTES_PER_LINE;
149 if (pdata_->size() % BYTES_PER_LINE)
153 painter.fillRect(event->rect(), this->palette().color(QPalette::Base));
155 QColor addressAreaColor = QColor(0xd4, 0xd4, 0xd4, 0xff);
156 painter.fillRect(QRect(posAddr_, event->rect().top(),
157 posHex_ - GAP_ADR_HEX + 2, height()), addressAreaColor);
159 int linePos = posAscii_ - (GAP_HEX_ASCII / 2);
160 painter.setPen(Qt::gray);
162 painter.drawLine(linePos, event->rect().top(), linePos, height());
164 painter.setPen(Qt::black);
166 int yPosStart = charHeight_;
168 QBrush def = painter.brush();
169 QBrush selected = QBrush(QColor(0x6d, 0x9e, 0xff, 0xff));
170 QByteArray data = pdata_->getData(firstLineIdx * BYTES_PER_LINE, (lastLineIdx - firstLineIdx) * BYTES_PER_LINE);
172 for (size_t lineIdx = firstLineIdx, yPos = yPosStart; lineIdx < lastLineIdx; lineIdx++) {
173 QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0'));
174 painter.drawText(posAddr_, yPos, address);
177 for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size(); i++) {
178 size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
179 if ((pos >= selectBegin_) && (pos < selectEnd_)) {
180 painter.setBackground(selected);
181 painter.setBackgroundMode(Qt::OpaqueMode);
184 QString val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF0) >> 4, 16);
185 painter.drawText(xPos, yPos, val);
187 if (((pos+1) >= selectBegin_) && ((pos+1) < selectEnd_)) {
188 painter.setBackground(selected);
189 painter.setBackgroundMode(Qt::OpaqueMode);
191 painter.setBackground(def);
192 painter.setBackgroundMode(Qt::OpaqueMode);
195 val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF), 16);
196 painter.drawText(xPos + charWidth_, yPos, val);
198 painter.setBackground(def);
199 painter.setBackgroundMode(Qt::OpaqueMode);
201 xPos += 3 * charWidth_;
204 int xPosAscii = posAscii_;
205 for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size() && (i < BYTES_PER_LINE); i++) {
206 char ch = data[(unsigned int)((lineIdx - firstLineIdx) * BYTES_PER_LINE + i)];
208 if ((ch < 0x20) || (ch > 0x7e))
211 painter.drawText(xPosAscii, yPos, QString(ch));
212 xPosAscii += charWidth_;
219 int x = (cursorPos_ % (2 * BYTES_PER_LINE));
220 int y = cursorPos_ / (2 * BYTES_PER_LINE);
222 int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
223 int cursorY = y * charHeight_ + 4;
224 painter.fillRect(cursorX, cursorY, 2, charHeight_, this->palette().color(QPalette::WindowText));
228 void QHexView::keyPressEvent(QKeyEvent *event)
230 bool setVisible = false;
232 /*****************************************************************************/
233 /* Cursor movements */
234 /*****************************************************************************/
235 if (event->matches(QKeySequence::MoveToNextChar)) {
236 setCursorPos(cursorPos_ + 1);
237 resetSelection(cursorPos_);
240 if (event->matches(QKeySequence::MoveToPreviousChar)) {
241 setCursorPos(cursorPos_ - 1);
242 resetSelection(cursorPos_);
246 if (event->matches(QKeySequence::MoveToEndOfLine)) {
247 setCursorPos(cursorPos_ | ((BYTES_PER_LINE * 2) - 1));
248 resetSelection(cursorPos_);
251 if (event->matches(QKeySequence::MoveToStartOfLine)) {
252 setCursorPos(cursorPos_ | (cursorPos_ % (BYTES_PER_LINE * 2)));
253 resetSelection(cursorPos_);
256 if (event->matches(QKeySequence::MoveToPreviousLine)) {
257 setCursorPos(cursorPos_ - BYTES_PER_LINE * 2);
258 resetSelection(cursorPos_);
261 if (event->matches(QKeySequence::MoveToNextLine)) {
262 setCursorPos(cursorPos_ + BYTES_PER_LINE * 2);
263 resetSelection(cursorPos_);
267 if (event->matches(QKeySequence::MoveToNextPage)) {
268 setCursorPos(cursorPos_ + (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
269 resetSelection(cursorPos_);
272 if (event->matches(QKeySequence::MoveToPreviousPage)) {
273 setCursorPos(cursorPos_ - (viewport()->height() / charHeight_ - 1) * 2 * BYTES_PER_LINE);
274 resetSelection(cursorPos_);
277 if (event->matches(QKeySequence::MoveToEndOfDocument)) {
279 setCursorPos(pdata_->size() * 2);
280 resetSelection(cursorPos_);
283 if (event->matches(QKeySequence::MoveToStartOfDocument)) {
285 resetSelection(cursorPos_);
289 /*****************************************************************************/
290 /* Select commands */
291 /*****************************************************************************/
292 if (event->matches(QKeySequence::SelectAll)) {
295 setSelection(2 * pdata_->size() + 1);
298 if (event->matches(QKeySequence::SelectNextChar)) {
299 int pos = cursorPos_ + 1;
304 if (event->matches(QKeySequence::SelectPreviousChar)) {
305 int pos = cursorPos_ - 1;
310 if (event->matches(QKeySequence::SelectEndOfLine)) {
311 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE)) + (2 * BYTES_PER_LINE);
316 if (event->matches(QKeySequence::SelectStartOfLine)) {
317 int pos = cursorPos_ - (cursorPos_ % (2 * BYTES_PER_LINE));
322 if (event->matches(QKeySequence::SelectPreviousLine)) {
323 int pos = cursorPos_ - (2 * BYTES_PER_LINE);
328 if (event->matches(QKeySequence::SelectNextLine)) {
329 int pos = cursorPos_ + (2 * BYTES_PER_LINE);
335 if (event->matches(QKeySequence::SelectNextPage)) {
336 int pos = cursorPos_ + (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
341 if (event->matches(QKeySequence::SelectPreviousPage)) {
342 int pos = cursorPos_ - (((viewport()->height() / charHeight_) - 1) * 2 * BYTES_PER_LINE);
347 if (event->matches(QKeySequence::SelectEndOfDocument)) {
350 pos = pdata_->size() * 2;
355 if (event->matches(QKeySequence::SelectStartOfDocument)) {
362 if (event->matches(QKeySequence::Copy) && (pdata_)) {
367 QByteArray data = pdata_->getData(selectBegin_ / 2, (selectEnd_ - selectBegin_) / 2 + 1);
368 if (selectBegin_ % 2) {
369 text += QString::number((data.at((idx+1) / 2) & 0xF), 16);
375 int selectedSize = selectEnd_ - selectBegin_;
376 while (idx < selectedSize) {
377 QString val = QString::number((data.at((copyOffset + idx) / 2) & 0xF0) >> 4, 16);
378 if ((idx + 1) < selectedSize) {
379 val += QString::number((data.at((copyOffset + idx) / 2) & 0xF), 16);
384 if ((idx/2) % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
390 QClipboard *clipboard = QApplication::clipboard();
391 clipboard->setText(text);
392 if (clipboard->supportsSelection())
393 clipboard->setText(text);
399 viewport()->update();
402 void QHexView::mouseMoveEvent(QMouseEvent *event)
404 int actPos = cursorPos(event->pos());
405 setCursorPos(actPos);
406 setSelection(actPos);
408 viewport()->update();
411 void QHexView::mousePressEvent(QMouseEvent *event)
413 int cPos = cursorPos(event->pos());
415 if ((QApplication::keyboardModifiers() & Qt::ShiftModifier) && (event->button() == Qt::LeftButton))
418 resetSelection(cPos);
422 viewport()->update();
425 size_t QHexView::cursorPos(const QPoint &position)
429 if (((size_t)position.x() >= posHex_) &&
430 ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
432 int x = (position.x() - posHex_) / charWidth_;
437 x = ((x / 3) * 2) + 1;
439 int firstLineIdx = verticalScrollBar()->value();
440 int y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
441 pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
447 void QHexView::resetSelection()
449 selectBegin_ = selectInit_;
450 selectEnd_ = selectInit_;
453 void QHexView::resetSelection(int pos)
463 void QHexView::setSelection(int pos)
468 if ((size_t)pos >= selectInit_) {
470 selectBegin_ = selectInit_;
473 selectEnd_ = selectInit_;
477 void QHexView::setCursorPos(int position)
484 maxPos = pdata_->size() * 2;
485 if (pdata_->size() % BYTES_PER_LINE)
489 if (position > maxPos)
492 cursorPos_ = position;
495 void QHexView::ensureVisible()
497 QSize areaSize = viewport()->size();
499 int firstLineIdx = verticalScrollBar() -> value();
500 int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
502 int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
504 if (cursorY < firstLineIdx)
505 verticalScrollBar() -> setValue(cursorY);
506 else if(cursorY >= lastLineIdx)
507 verticalScrollBar() -> setValue(cursorY - areaSize.height() / charHeight_ + 1);