]> sigrok.org Git - pulseview.git/blobdiff - pv/views/decoder_output/QHexView.cpp
Fix #1505 by always updating the DecodeTrace height when needed
[pulseview.git] / pv / views / decoder_output / QHexView.cpp
index 0fca4161fe02f6467799619a56862cb09db8b8c1..ccd50f87bccc1291195d2b515a4ee70418e422f0 100644 (file)
@@ -79,7 +79,7 @@ QHexView::QHexView(QWidget *parent):
        }
 }
 
-void QHexView::setMode(Mode m)
+void QHexView::set_mode(Mode m)
 {
        mode_ = m;
 
@@ -87,19 +87,26 @@ void QHexView::setMode(Mode m)
        // so we don't update the viewport here
 }
 
-void QHexView::setData(const DecodeBinaryClass* data)
+void QHexView::set_data(const DecodeBinaryClass* data)
 {
        data_ = data;
 
        size_t size = 0;
-       size_t chunks = data_->chunks.size();
-       for (size_t i = 0; i < chunks; i++)
-               size += data_->chunks[i].data.size();
+       if (data) {
+               size_t chunks = data_->chunks.size();
+               for (size_t i = 0; i < chunks; i++)
+                       size += data_->chunks[i].data.size();
+       }
        data_size_ = size;
 
        viewport()->update();
 }
 
+unsigned int QHexView::get_bytes_per_line() const
+{
+       return BYTES_PER_LINE;
+}
+
 void QHexView::clear()
 {
        verticalScrollBar()->setValue(0);
@@ -135,12 +142,56 @@ pair<size_t, size_t> QHexView::get_selection() const
                // Nothing is currently selected
                start = 0;
                end = data_size_;
-       } else
+       } if (end < data_size_)
                end++;
 
        return std::make_pair(start, end);
 }
 
+size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
+       bool with_offset, bool with_ascii)
+{
+       dest->clear();
+
+       // Determine start address for the row
+       uint64_t row = start / BYTES_PER_LINE;
+       uint64_t offset = row * BYTES_PER_LINE;
+       end = std::min((uint64_t)end, offset + BYTES_PER_LINE);
+
+       if (with_offset)
+               dest->append(QString("%1 ").arg(row * BYTES_PER_LINE, 10, 16, QChar('0')).toUpper());
+
+       initialize_byte_iterator(offset);
+       for (size_t i = offset; i < offset + BYTES_PER_LINE; i++) {
+               uint8_t value = 0;
+
+               if (i < end)
+                       value = get_next_byte();
+
+               if ((i < start) || (i >= end))
+                       dest->append("   ");
+               else
+                       dest->append(QString("%1 ").arg(value, 2, 16, QChar('0')).toUpper());
+       }
+
+       if (with_ascii) {
+               initialize_byte_iterator(offset);
+               for (size_t i = offset; i < end; i++) {
+                       uint8_t value = get_next_byte();
+
+                       if ((value < 0x20) || (value > 0x7E))
+                               value = '.';
+
+                       if (i < start)
+                               dest->append(' ');
+                       else
+                               dest->append((char)value);
+               }
+       }
+
+       return end;
+}
+
 void QHexView::initialize_byte_iterator(size_t offset)
 {
        current_chunk_id_ = 0;
@@ -219,7 +270,7 @@ void QHexView::paintEvent(QPaintEvent *event)
        QSize widgetSize = getFullSize();
        setMinimumWidth(widgetSize.width());
        setMaximumWidth(widgetSize.width());
-       QSize areaSize = viewport()->size();
+       QSize areaSize = viewport()->size() - QSize(0, charHeight_);
 
        // Only show scrollbar if the content goes beyond the visible area
        if (widgetSize.height() > areaSize.height()) {
@@ -244,26 +295,28 @@ void QHexView::paintEvent(QPaintEvent *event)
        // Determine first/last line indices
        size_t firstLineIdx = verticalScrollBar()->value();
 
-       size_t lastLineIdx = firstLineIdx + areaSize.height() / charHeight_;
+       size_t lastLineIdx = firstLineIdx + (areaSize.height() / charHeight_);
        if (lastLineIdx > (data_size_ / BYTES_PER_LINE)) {
                lastLineIdx = data_size_ / BYTES_PER_LINE;
                if (data_size_ % BYTES_PER_LINE)
                        lastLineIdx++;
        }
 
-       // Fill address area background
-       painter.fillRect(QRect(posAddr_, event->rect().top(),
-               posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
-
        // Paint divider line between hex and ASCII areas
        int line_x = posAscii_ - (GAP_HEX_ASCII / 2);
        painter.setPen(palette().color(QPalette::Midlight));
        painter.drawLine(line_x, event->rect().top(), line_x, height());
 
+       // Fill address area background
+       painter.fillRect(QRect(posAddr_, event->rect().top(),
+               posHex_ - (GAP_ADR_HEX / 2), height()), palette().color(QPalette::Window));
+       painter.fillRect(QRect(posAddr_, event->rect().top(),
+               posAscii_ - (GAP_HEX_ASCII / 2), charHeight_ + 2), palette().color(QPalette::Window));
+
        // Paint address area
        painter.setPen(palette().color(QPalette::ButtonText));
 
-       int yStart = charHeight_;
+       int yStart = 2 * charHeight_;
        for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
 
                QString address = QString("%1").arg(lineIdx * 16, 10, 16, QChar('0')).toUpper();
@@ -271,6 +324,12 @@ void QHexView::paintEvent(QPaintEvent *event)
                y += charHeight_;
        }
 
+       // Paint top row with hex offsets
+       painter.setPen(palette().color(QPalette::ButtonText));
+       for (int offset = 0; offset <= 0xF; offset++)
+               painter.drawText(posHex_ + (1 + offset * 3) * charWidth_,
+                       charHeight_ - 3, QString::number(offset, 16).toUpper());
+
        // Paint hex values
        QBrush regular = palette().buttonText();
        QBrush selected = palette().highlight();
@@ -279,7 +338,7 @@ void QHexView::paintEvent(QPaintEvent *event)
        unsigned int chunk_color = 0;
 
        initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
-       yStart = charHeight_;
+       yStart = 2 * charHeight_;
        for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
 
                int x = posHex_;
@@ -317,6 +376,9 @@ void QHexView::paintEvent(QPaintEvent *event)
                        val = QString::number((byte_value & 0xF), 16).toUpper();
                        painter.drawText(x + charWidth_, y, val);
 
+                       if ((pos >= selectBegin_) && (pos < selectEnd_ - 1) && (i < BYTES_PER_LINE - 1))
+                               painter.drawText(x + 2 * charWidth_, y, QString(' '));
+
                        x += 3 * charWidth_;
                }
 
@@ -325,7 +387,7 @@ void QHexView::paintEvent(QPaintEvent *event)
 
        // Paint ASCII characters
        initialize_byte_iterator(firstLineIdx * BYTES_PER_LINE);
-       yStart = charHeight_;
+       yStart = 2 * charHeight_;
        for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
 
                int x = posAscii_;
@@ -360,7 +422,7 @@ void QHexView::paintEvent(QPaintEvent *event)
                int y = cursorPos_ / (2 * BYTES_PER_LINE);
                y -= firstLineIdx;
                int cursorX = (((x / 2) * 3) + (x % 2)) * charWidth_ + posHex_;
-               int cursorY = y * charHeight_ + 4;
+               int cursorY = charHeight_ + y * charHeight_ + 4;
                painter.fillRect(cursorX, cursorY, 2, charHeight_, palette().color(QPalette::WindowText));
        }
 }
@@ -558,7 +620,7 @@ size_t QHexView::cursorPosFromMousePos(const QPoint &position)
                x = (2 * x + 1) / 3;
 
                size_t firstLineIdx = verticalScrollBar()->value();
-               size_t y = (position.y() / charHeight_) * 2 * BYTES_PER_LINE;
+               size_t y = ((position.y() / charHeight_) - 1) * 2 * BYTES_PER_LINE;
                pos = x + y + firstLineIdx * BYTES_PER_LINE * 2;
        }