From: Soeren Apel Date: Tue, 5 May 2020 22:02:38 +0000 (+0200) Subject: QHexView: Only use as many digits for the address as needed X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d5397ccbb61e1937757c95c2603156fdd18d1300;p=pulseview.git QHexView: Only use as many digits for the address as needed --- diff --git a/pv/views/decoder_binary/QHexView.cpp b/pv/views/decoder_binary/QHexView.cpp index cdaf1b5d..ed003949 100644 --- a/pv/views/decoder_binary/QHexView.cpp +++ b/pv/views/decoder_binary/QHexView.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -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_; } diff --git a/pv/views/decoder_binary/QHexView.hpp b/pv/views/decoder_binary/QHexView.hpp index c39dcb20..12729f80 100644 --- a/pv/views/decoder_binary/QHexView.hpp +++ b/pv/views/decoder_binary/QHexView.hpp @@ -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