]> sigrok.org Git - pulseview.git/blobdiff - pv/views/decoder_output/QHexView.cpp
QHexView: Improve vertical slider handling
[pulseview.git] / pv / views / decoder_output / QHexView.cpp
index b379026c1b16a74cf2169867e2852ac4ddfc31e9..82b1cd90769e6ca9c839b2b224ddad7b235f5d33 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <QApplication>
 #include <QClipboard>
+#include <QDebug>
 #include <QFont>
 #include <QKeyEvent>
 #include <QScrollBar>
 
 #include "QHexView.hpp"
 
-using std::size_t;
-
 const unsigned int BYTES_PER_LINE   = 16;
 const unsigned int HEXCHARS_IN_LINE = BYTES_PER_LINE * 3 - 1;
 const unsigned int GAP_ADR_HEX      = 10;
 const unsigned int GAP_HEX_ASCII    = 10;
 const unsigned int GAP_ASCII_SLIDER = 5;
 
-DataStorageArray::DataStorageArray(const QByteArray &arr)
-{
-       data_ = arr;
-}
-
-QByteArray DataStorageArray::getData(size_t position, size_t length)
-{
-       return data_.mid(position, length);
-}
-
-size_t DataStorageArray::size()
-{
-       return data_.count();
-}
-
-
 
 QHexView::QHexView(QWidget *parent):
        QAbstractScrollArea(parent),
-       pdata_(nullptr)
+       mode_(ChunkedDataMode),
+       data_(nullptr),
+       selectBegin_(0),
+       selectEnd_(0),
+       cursorPos_(0)
 {
        setFont(QFont("Courier", 10));
 
@@ -78,52 +65,125 @@ QHexView::QHexView(QWidget *parent):
        posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
 
        setFocusPolicy(Qt::StrongFocus);
+
+       if (palette().color(QPalette::ButtonText).toHsv().value() > 127) {
+               // Color is bright
+               chunk_colors_.emplace_back(100, 149, 237); // QColorConstants::Svg::cornflowerblue
+               chunk_colors_.emplace_back(60, 179, 113);  // QColorConstants::Svg::mediumseagreen
+               chunk_colors_.emplace_back(210, 180, 140); // QColorConstants::Svg::tan
+       } else {
+               // Color is dark
+               chunk_colors_.emplace_back(0, 0, 139);   // QColorConstants::Svg::darkblue
+               chunk_colors_.emplace_back(34, 139, 34); // QColorConstants::Svg::forestgreen
+               chunk_colors_.emplace_back(160, 82, 45); // QColorConstants::Svg::sienna
+       }
 }
 
-QHexView::~QHexView()
+void QHexView::setMode(Mode m)
 {
-       if (pdata_)
-               delete pdata_;
+       mode_ = m;
+
+       // This is not expected to be set when data is showing,
+       // so we don't update the viewport here
 }
 
-void QHexView::setData(DataStorage *pData)
+void QHexView::setData(const DecodeBinaryClass* data)
 {
-       verticalScrollBar()->setValue(0);
+       data_ = data;
 
-       if (pdata_)
-               delete pdata_;
+       size_t size = 0;
+       size_t chunks = data_->chunks.size();
+       for (size_t i = 0; i < chunks; i++)
+               size += data_->chunks[i].data.size();
+       data_size_ = size;
 
-       pdata_ = pData;
-       cursorPos_ = 0;
-       resetSelection(0);
+       viewport()->update();
+}
+
+void QHexView::clear()
+{
+       verticalScrollBar()->setValue(0);
+       data_ = nullptr;
+       data_size_ = 0;
+
+       viewport()->update();
 }
 
 void QHexView::showFromOffset(size_t offset)
 {
-       if (pdata_ && (offset < pdata_->size())) {
+       if (data_ && (offset < data_size_)) {
                setCursorPos(offset * 2);
 
                int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
                verticalScrollBar() -> setValue(cursorY);
        }
+
+       viewport()->update();
 }
 
-void QHexView::clear()
+void QHexView::initialize_byte_iterator(size_t offset)
 {
-       verticalScrollBar()->setValue(0);
+       current_chunk_id_ = 0;
+       current_chunk_offset_ = 0;
+       current_offset_ = offset;
+
+       size_t chunks = data_->chunks.size();
+       for (size_t i = 0; i < chunks; i++) {
+               size_t size = data_->chunks[i].data.size();
+
+               if (offset >= size) {
+                       current_chunk_id_++;
+                       offset -= size;
+               } else {
+                       current_chunk_offset_ = offset;
+                       break;
+               }
+       }
+
+       if (current_chunk_id_ < data_->chunks.size())
+               current_chunk_ = data_->chunks[current_chunk_id_];
+}
+
+uint8_t QHexView::get_next_byte(bool* is_next_chunk)
+{
+       if (is_next_chunk != nullptr)
+               *is_next_chunk = (current_chunk_offset_ == 0);
+
+       uint8_t v = 0;
+       if (current_chunk_offset_ < current_chunk_.data.size())
+               v = current_chunk_.data[current_chunk_offset_];
+
+       current_offset_++;
+       current_chunk_offset_++;
+
+       if (current_offset_ > data_size_) {
+               qWarning() << "QHexView::get_next_byte() overran binary data boundary:" <<
+                       current_offset_ << "of" << data_size_ << "bytes";
+               return 0xEE;
+       }
+
+       if ((current_chunk_offset_ == current_chunk_.data.size()) && (current_offset_ < data_size_)) {
+               current_chunk_id_++;
+               current_chunk_offset_ = 0;
+               current_chunk_ = data_->chunks[current_chunk_id_];
+       }
+
+       return v;
 }
 
 QSize QHexView::getFullSize() const
 {
-       if (!pdata_)
-               return QSize(0, 0);
+       size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_);
+
+       if (verticalScrollBar()->isEnabled())
+               width += GAP_ASCII_SLIDER + verticalScrollBar()->width();
 
-       size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) +
-               GAP_ASCII_SLIDER + verticalScrollBar()->width();
+       if (!data_ || (data_size_ == 0))
+               return QSize(width, 0);
 
-       size_t height = pdata_->size() / BYTES_PER_LINE;
+       size_t height = data_size_ / BYTES_PER_LINE;
 
-       if (pdata_->size() % BYTES_PER_LINE)
+       if (data_size_ % BYTES_PER_LINE)
                height++;
 
        height *= charHeight_;
@@ -133,31 +193,45 @@ QSize QHexView::getFullSize() const
 
 void QHexView::paintEvent(QPaintEvent *event)
 {
-       if (!pdata_)
-               return;
-
        QPainter painter(viewport());
 
        // Calculate and update the widget and paint area sizes
-       QSize areaSize = viewport()->size();
        QSize widgetSize = getFullSize();
        setMinimumWidth(widgetSize.width());
        setMaximumWidth(widgetSize.width());
+       QSize areaSize = viewport()->size();
 
-       verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
-       verticalScrollBar()->setRange(0, (widgetSize.height() - areaSize.height()) / charHeight_ + 1);
+       // Only show scrollbar if the content goes beyond the visible area
+       if (widgetSize.height() > areaSize.height()) {
+               verticalScrollBar()->setEnabled(true);
+               verticalScrollBar()->setPageStep(areaSize.height() / charHeight_);
+               verticalScrollBar()->setRange(0, ((widgetSize.height() - areaSize.height())) / charHeight_ + 1);
+       } else
+               verticalScrollBar()->setEnabled(false);
 
+       // Fill widget background
+       painter.fillRect(event->rect(), palette().color(QPalette::Base));
+
+       if (!data_ || (data_size_ == 0)) {
+               painter.setPen(palette().color(QPalette::Text));
+               QString s = tr("No data available");
+               int x = (areaSize.width() - fontMetrics().boundingRect(s).width()) / 2;
+               int y = areaSize.height() / 2;
+               painter.drawText(x, y, s);
+               return;
+       }
+
+       // Determine first/last line indices
        size_t firstLineIdx = verticalScrollBar()->value();
 
        size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
-       if (lastLineIdx > (pdata_->size() / BYTES_PER_LINE)) {
-               lastLineIdx = pdata_->size() / BYTES_PER_LINE;
-               if (pdata_->size() % BYTES_PER_LINE)
+       if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
+               lastLineIdx = data_size_ / BYTES_PER_LINE;
+               if (data_size_ % BYTES_PER_LINE)
                        lastLineIdx++;
        }
 
-       // Fill widget and address area backgrounds
-       painter.fillRect(event->rect(), palette().color(QPalette::Base));
+       // Fill address area background
        painter.fillRect(QRect(posAddr_, event->rect().top(),
                posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
 
@@ -178,33 +252,49 @@ void QHexView::paintEvent(QPaintEvent *event)
        }
 
        // Paint hex values
-       QBrush regular = painter.brush();
-       QBrush selected = QBrush(palette().color(QPalette::Highlight));
-       QByteArray data = pdata_->getData(firstLineIdx * BYTES_PER_LINE, (lastLineIdx - firstLineIdx) * BYTES_PER_LINE);
+       QBrush regular = palette().buttonText();
+       QBrush selected = palette().highlight();
+
+       bool multiple_chunks = (data_->chunks.size() > 1);
+       unsigned int chunk_color = 0;
 
+       initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
        yStart = charHeight_;
        for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
 
-               painter.setBackgroundMode(Qt::OpaqueMode);
-
                int x = posHex_;
-               for (size_t i = 0; i < BYTES_PER_LINE && ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size(); i++) {
+               for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
                        size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
 
+                       // Fetch byte
+                       bool is_next_chunk;
+                       uint8_t byte_value = get_next_byte(&is_next_chunk);
+
+                       if (is_next_chunk) {
+                               chunk_color++;
+                               if (chunk_color == chunk_colors_.size())
+                                       chunk_color = 0;
+                       }
+
                        if ((pos >= selectBegin_) && (pos < selectEnd_)) {
+                               painter.setBackgroundMode(Qt::OpaqueMode);
                                painter.setBackground(selected);
                                painter.setPen(palette().color(QPalette::HighlightedText));
                        } else {
                                painter.setBackground(regular);
-                               painter.setPen(palette().color(QPalette::Text));
+                               painter.setBackgroundMode(Qt::TransparentMode);
+                               if (!multiple_chunks)
+                                       painter.setPen(palette().color(QPalette::Text));
+                               else
+                                       painter.setPen(chunk_colors_[chunk_color]);
                        }
 
                        // First nibble
-                       QString val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF0) >> 4, 16).toUpper();
+                       QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper();
                        painter.drawText(x, y, val);
 
                        // Second nibble
-                       val = QString::number((data.at((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) & 0xF), 16).toUpper();
+                       val = QString::number((byte_value & 0xF), 16).toUpper();
                        painter.drawText(x + charWidth_, y, val);
 
                        x += 3 * charWidth_;
@@ -214,21 +304,25 @@ void QHexView::paintEvent(QPaintEvent *event)
        }
 
        // Paint ASCII characters
+       initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
        yStart = charHeight_;
        for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
 
                int x = posAscii_;
-               for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < (size_t)data.size() && (i < BYTES_PER_LINE); i++) {
-                       char ch = data[(unsigned int)((lineIdx - firstLineIdx) * BYTES_PER_LINE + i)];
+               for (size_t i = 0; (i < BYTES_PER_LINE) && (current_offset_ < data_size_); i++) {
+                       // Fetch byte
+                       uint8_t ch = get_next_byte();
 
                        if ((ch < 0x20) || (ch > 0x7E))
                                ch = '.';
 
                        size_t pos = (lineIdx * BYTES_PER_LINE + i) * 2;
                        if ((pos >= selectBegin_) && (pos < selectEnd_)) {
+                               painter.setBackgroundMode(Qt::OpaqueMode);
                                painter.setBackground(selected);
                                painter.setPen(palette().color(QPalette::HighlightedText));
                        } else {
+                               painter.setBackgroundMode(Qt::TransparentMode);
                                painter.setBackground(regular);
                                painter.setPen(palette().color(QPalette::Text));
                        }
@@ -299,8 +393,7 @@ void QHexView::keyPressEvent(QKeyEvent *event)
                setVisible = true;
        }
        if (event->matches(QKeySequence::MoveToEndOfDocument)) {
-               if (pdata_)
-                       setCursorPos(pdata_->size() * 2);
+               setCursorPos(data_size_ * 2);
                resetSelection(cursorPos_);
                setVisible = true;
        }
@@ -313,8 +406,7 @@ void QHexView::keyPressEvent(QKeyEvent *event)
        // Select commands
        if (event->matches(QKeySequence::SelectAll)) {
                resetSelection(0);
-               if (pdata_)
-                       setSelection(2 * pdata_->size() + 1);
+               setSelection(2 * data_size_);
                setVisible = true;
        }
        if (event->matches(QKeySequence::SelectNextChar)) {
@@ -367,52 +459,38 @@ void QHexView::keyPressEvent(QKeyEvent *event)
                setVisible = true;
        }
        if (event->matches(QKeySequence::SelectEndOfDocument)) {
-               int pos = 0;
-               if (pdata_)
-                       pos = pdata_->size() * 2;
+               int pos = data_size_ * 2;
                setCursorPos(pos);
                setSelection(pos);
                setVisible = true;
        }
        if (event->matches(QKeySequence::SelectStartOfDocument)) {
-               int pos = 0;
-               setCursorPos(pos);
-               setSelection(pos);
+               setCursorPos(0);
+               setSelection(0);
                setVisible = true;
        }
 
-       if (event->matches(QKeySequence::Copy) && (pdata_)) {
+       if (event->matches(QKeySequence::Copy) && (data_)) {
                QString text;
-               int idx = 0;
-               int copyOffset = 0;
-
-               QByteArray data = pdata_->getData(selectBegin_ / 2, (selectEnd_ - selectBegin_) / 2 + 1);
-               if (selectBegin_ % 2) {
-                       text += QString::number((data.at((idx+1) / 2) & 0xF), 16);
-                       text += " ";
-                       idx++;
-                       copyOffset = 1;
-               }
 
-               int selectedSize = selectEnd_ - selectBegin_;
-               while (idx < selectedSize) {
-                       QString val = QString::number((data.at((copyOffset + idx) / 2) & 0xF0) >> 4, 16);
-                       if ((idx + 1) < selectedSize) {
-                               val += QString::number((data.at((copyOffset + idx) / 2) & 0xF), 16);
-                               val += " ";
-                       }
-                       text += val;
+               initialize_byte_iterator(selectBegin_ / 2);
 
-                       if ((idx/2) % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
-                               text += "\n";
+               size_t selectedSize = (selectEnd_ - selectBegin_ + 1) / 2;
+               for (size_t i = 0; i < selectedSize; i++) {
+                       uint8_t byte_value = get_next_byte();
 
-                       idx += 2;
+                       QString s = QString::number((byte_value & 0xF0) >> 4, 16).toUpper() +
+                               QString::number((byte_value & 0xF), 16).toUpper() + " ";
+                       text += s;
+
+                       if (i % BYTES_PER_LINE == (BYTES_PER_LINE - 1))
+                               text += "\n";
                }
 
                QClipboard *clipboard = QApplication::clipboard();
-               clipboard->setText(text);
+               clipboard->setText(text, QClipboard::Clipboard);
                if (clipboard->supportsSelection())
-                       clipboard->setText(text);
+                       clipboard->setText(text, QClipboard::Selection);
        }
 
        if (setVisible)
@@ -446,25 +524,27 @@ void QHexView::mousePressEvent(QMouseEvent *event)
 
 size_t QHexView::cursorPosFromMousePos(const QPoint &position)
 {
-       int pos = -1;
+       size_t pos = -1;
 
        if (((size_t)position.x() >= posHex_) &&
                ((size_t)position.x() < (posHex_ + HEXCHARS_IN_LINE * charWidth_))) {
 
                // Note: We add 1.5 character widths so that selection across
                // byte gaps is smoother
-               int x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
+               size_t x = (position.x() + (1.5 * charWidth_ / 2) - posHex_) / charWidth_;
 
                // Note: We allow only full bytes to be selected, not nibbles,
                // so we round to the nearest byte gap
                x = (2 * x + 1) / 3;
 
-               int firstLineIdx = verticalScrollBar()->value();
-               int y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
+               size_t firstLineIdx = verticalScrollBar()->value();
+               size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
                pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
        }
 
-       return pos;
+       size_t max_pos = data_size_ * 2;
+
+       return std::min(pos, max_pos);
 }
 
 void QHexView::resetSelection()
@@ -502,15 +582,10 @@ void QHexView::setCursorPos(int position)
        if (position < 0)
                position = 0;
 
-       int maxPos = 0;
-       if (pdata_) {
-               maxPos = pdata_->size() * 2;
-               if (pdata_->size() % BYTES_PER_LINE)
-                       maxPos++;
-       }
+       int max_pos = data_size_ * 2;
 
-       if (position > maxPos)
-               position = maxPos;
+       if (position > max_pos)
+               position = max_pos;
 
        cursorPos_ = position;
 }
@@ -519,13 +594,14 @@ void QHexView::ensureVisible()
 {
        QSize areaSize = viewport()->size();
 
-       int firstLineIdx = verticalScrollBar() -> value();
+       int firstLineIdx = verticalScrollBar()->value();
        int lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
 
        int cursorY = cursorPos_ / (2 * BYTES_PER_LINE);
 
        if (cursorY < firstLineIdx)
-               verticalScrollBar() -> setValue(cursorY);
-       else if(cursorY >= lastLineIdx)
-               verticalScrollBar() -> setValue(cursorY - areaSize.height() / charHeight_ + 1);
+               verticalScrollBar()->setValue(cursorY);
+       else
+               if(cursorY >= lastLineIdx)
+                       verticalScrollBar()->setValue(cursorY - areaSize.height() / charHeight_ + 1);
 }