]> sigrok.org Git - pulseview.git/blobdiff - pv/views/decoder_output/QHexView.cpp
HexView: Add top row with offsets
[pulseview.git] / pv / views / decoder_output / QHexView.cpp
index 5c34d61bac5ad9c5022e0db133b2cdcb0eadd8f1..0104de1590930f63f3773cd31ca5ab11e63c02ec 100644 (file)
@@ -92,9 +92,11 @@ 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();
@@ -154,7 +156,7 @@ size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
        // Determine start address for the row
        uint64_t row = start / BYTES_PER_LINE;
        uint64_t offset = row * BYTES_PER_LINE;
-       end = std::min(end, offset + 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());
@@ -177,6 +179,9 @@ size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
                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
@@ -265,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()) {
@@ -290,7 +295,7 @@ 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)
@@ -309,7 +314,7 @@ void QHexView::paintEvent(QPaintEvent *event)
        // 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();
@@ -317,6 +322,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_, QString::number(offset, 16).toUpper());
+
        // Paint hex values
        QBrush regular = palette().buttonText();
        QBrush selected = palette().highlight();
@@ -325,7 +336,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_;
@@ -374,7 +385,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_;
@@ -409,7 +420,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));
        }
 }
@@ -607,7 +618,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;
        }