]> sigrok.org Git - pulseview.git/blobdiff - pv/views/decoder_output/QHexView.cpp
Fix another compile failure on non-64bit machines
[pulseview.git] / pv / views / decoder_output / QHexView.cpp
index 051e0b1f350f1044649aee98fff0bd11e9413fd6..951c818a5f7e26ec062b716387c821264385869d 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <QApplication>
 #include <QClipboard>
+#include <QDebug>
 #include <QFont>
 #include <QKeyEvent>
 #include <QScrollBar>
@@ -78,7 +79,7 @@ QHexView::QHexView(QWidget *parent):
        }
 }
 
-void QHexView::setMode(Mode m)
+void QHexView::set_mode(Mode m)
 {
        mode_ = m;
 
@@ -86,17 +87,24 @@ 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;
 
-       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();
 }
 
+unsigned int QHexView::get_bytes_per_line() const
+{
+       return BYTES_PER_LINE;
+}
+
 void QHexView::clear()
 {
        verticalScrollBar()->setValue(0);
@@ -118,21 +126,91 @@ void QHexView::showFromOffset(size_t offset)
        viewport()->update();
 }
 
+QSizePolicy QHexView::sizePolicy() const
+{
+       return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
+}
+
+pair<size_t, size_t> QHexView::get_selection() const
+{
+       size_t start = selectBegin_ / 2;
+       size_t end = selectEnd_ / 2;
+
+       if (start == end) {
+               // Nothing is currently selected
+               start = 0;
+               end = data_size_;
+       } else
+               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;
        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)
@@ -140,13 +218,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;
@@ -154,10 +242,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;
@@ -180,8 +270,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));
@@ -237,7 +332,7 @@ void QHexView::paintEvent(QPaintEvent *event)
        for (size_t lineIdx = firstLineIdx, y = yStart; lineIdx < lastLineIdx; lineIdx++) {
 
                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
@@ -271,6 +366,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_;
                }
 
@@ -283,7 +381,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();