QHexView: Only use as many digits for the address as needed
authorSoeren Apel <soeren@apelpie.net>
Tue, 5 May 2020 22:02:38 +0000 (00:02 +0200)
committerSoeren Apel <soeren@apelpie.net>
Mon, 11 May 2020 19:24:22 +0000 (21:24 +0200)
pv/views/decoder_binary/QHexView.cpp
pv/views/decoder_binary/QHexView.hpp

index cdaf1b5d4ecba0d6c78055a5d85adfe3ccb81d22..ed003949f242c67f482667a26b315554c1b90fd8 100644 (file)
@@ -34,6 +34,7 @@
 #include <QKeyEvent>
 #include <QScrollBar>
 #include <QSize>
+#include <QString>
 #include <QPainter>
 #include <QPaintEvent>
 
@@ -59,11 +60,6 @@ QHexView::QHexView(QWidget *parent):
        charWidth_ = fontMetrics().boundingRect('X').width();
        charHeight_ = fontMetrics().height();
 
-       // Determine X coordinates of the three sub-areas
-       posAddr_  = 0;
-       posHex_   = 10 * charWidth_ + GAP_ADR_HEX;
-       posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
-
        setFocusPolicy(Qt::StrongFocus);
 
        if (palette().color(QPalette::ButtonText).toHsv().value() > 127) {
@@ -99,6 +95,13 @@ void QHexView::set_data(const DecodeBinaryClass* data)
        }
        data_size_ = size;
 
+       address_digits_ = (uint8_t)QString::number(data_size_, 16).length();
+
+       // Calculate X coordinates of the three sub-areas
+       posAddr_  = 0;
+       posHex_   = address_digits_ * charWidth_ + GAP_ADR_HEX;
+       posAscii_ = posHex_ + HEXCHARS_IN_LINE * charWidth_ + GAP_HEX_ASCII;
+
        viewport()->update();
 }
 
@@ -159,7 +162,7 @@ size_t QHexView::create_hex_line(size_t start, size_t end, QString* dest,
        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());
+               dest->append(QString("%1 ").arg(row * BYTES_PER_LINE, address_digits_, 16, QChar('0')).toUpper());
 
        initialize_byte_iterator(offset);
        for (size_t i = offset; i < offset + BYTES_PER_LINE; i++) {
@@ -319,7 +322,7 @@ void QHexView::paintEvent(QPaintEvent *event)
        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();
+               QString address = QString("%1").arg(lineIdx * 16, address_digits_, 16, QChar('0')).toUpper();
                painter.drawText(posAddr_, y, address);
                y += charHeight_;
        }
index c39dcb2099064064570fd45b52da834303f76ea4..12729f80b28ddf86aa14fd48933d0da7c8da0549 100644 (file)
@@ -91,6 +91,7 @@ private:
        size_t posAddr_, posHex_, posAscii_;
        size_t charWidth_, charHeight_;
        size_t selectBegin_, selectEnd_, selectInit_, cursorPos_;
+       uint8_t address_digits_;
 
        size_t current_chunk_id_, current_chunk_offset_, current_offset_;
        DecodeBinaryDataChunk current_chunk_; // Cache locally so that we're not messed up when the vector is re-allocating its data