#include <QApplication>
#include <QClipboard>
+#include <QDebug>
#include <QFont>
#include <QKeyEvent>
#include <QScrollBar>
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)
{
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();
}
{
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)
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;
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 (verticalScrollBar()->isEnabled())
+ width += GAP_ASCII_SLIDER + verticalScrollBar()->width();
- if (!data_)
+ if (!data_ || (data_size_ == 0))
return QSize(width, 0);
size_t height = data_size_ / BYTES_PER_LINE;
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));
}
// 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);
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();
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));
}