X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=pv%2Fviews%2Fdecoder_output%2FQHexView.cpp;h=06df103b7f45c43b9aa19cc75d3e22260eace604;hb=6961eab0d4bf976cee015afacfa7c25795e574b9;hp=945e5ed3364bdabae1a6ae4ab7d5e8b8cbb02dd0;hpb=628b45cc0ba7ac38ac6a003fedbbc746f15dd724;p=pulseview.git diff --git a/pv/views/decoder_output/QHexView.cpp b/pv/views/decoder_output/QHexView.cpp index 945e5ed3..06df103b 100644 --- a/pv/views/decoder_output/QHexView.cpp +++ b/pv/views/decoder_output/QHexView.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -64,6 +65,18 @@ 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 + } } void QHexView::setMode(Mode m) @@ -78,9 +91,11 @@ void QHexView::setData(const DecodeBinaryClass* data) { data_ = data; - data_size_ = 0; - for (const DecodeBinaryDataChunk& chunk : data_->chunks) - data_size_ += chunk.data.size(); + 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; viewport()->update(); } @@ -106,21 +121,32 @@ void QHexView::showFromOffset(size_t offset) viewport()->update(); } +QSizePolicy QHexView::sizePolicy() const +{ + return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); +} + void QHexView::initialize_byte_iterator(size_t offset) { 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(); - for (const DecodeBinaryDataChunk& chunk : data_->chunks) - if (offset >= chunk.data.size()) { + if (offset >= size) { current_chunk_id_++; - offset -= chunk.data.size(); + offset -= size; } else { current_chunk_offset_ = offset; break; } + } - current_chunk_ = &(data_->chunks[current_chunk_id_]); + if (current_chunk_id_ < data_->chunks.size()) + current_chunk_ = data_->chunks[current_chunk_id_]; } uint8_t QHexView::get_next_byte(bool* is_next_chunk) @@ -128,13 +154,23 @@ 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 = current_chunk_->data[current_chunk_offset_]; + 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_chunk_offset_ == current_chunk_->data.size()) { + + 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_]); + current_chunk_ = data_->chunks[current_chunk_id_]; } return v; @@ -142,10 +178,12 @@ uint8_t QHexView::get_next_byte(bool* is_next_chunk) QSize QHexView::getFullSize() const { - size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_) + - GAP_ASCII_SLIDER + verticalScrollBar()->width(); + size_t width = posAscii_ + (BYTES_PER_LINE * charWidth_); - if (!data_) + if (verticalScrollBar()->isEnabled()) + width += GAP_ASCII_SLIDER + verticalScrollBar()->width(); + + if (!data_ || (data_size_ == 0)) return QSize(width, 0); size_t height = data_size_ / BYTES_PER_LINE; @@ -168,8 +206,13 @@ void QHexView::paintEvent(QPaintEvent *event) 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)); @@ -214,30 +257,43 @@ void QHexView::paintEvent(QPaintEvent *event) } // Paint hex values - QBrush regular = painter.brush(); - QBrush selected = QBrush(palette().color(QPalette::Highlight)); + 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) < 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]); } - // Fetch byte - uint8_t byte_value = get_next_byte(); - // First nibble QString val = QString::number((byte_value & 0xF0) >> 4, 16).toUpper(); painter.drawText(x, y, val); @@ -258,7 +314,7 @@ void QHexView::paintEvent(QPaintEvent *event) for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) { int x = posAscii_; - for (size_t i = 0; ((lineIdx - firstLineIdx) * BYTES_PER_LINE + i) < data_size_ && (i < 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(); @@ -267,9 +323,11 @@ void QHexView::paintEvent(QPaintEvent *event) 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)); }