Improved HexViewerWidget performance

This commit is contained in:
Nav
2022-09-10 22:51:57 +01:00
parent 241d94da54
commit 16c559e70f
6 changed files with 35 additions and 20 deletions

View File

@@ -13,34 +13,30 @@ namespace Bloom::Widgets
const auto rowIndex = static_cast<std::size_t>(mappingPair.first); const auto rowIndex = static_cast<std::size_t>(mappingPair.first);
const auto& byteItems = mappingPair.second; const auto& byteItems = mappingPair.second;
if (byteItems.empty()) {
continue;
}
ByteAddressItem* addressLabel = nullptr; ByteAddressItem* addressLabel = nullptr;
if (static_cast<int>(rowIndex) > layoutItemMaxIndex) { if (static_cast<int>(rowIndex) > layoutItemMaxIndex) {
addressLabel = new ByteAddressItem(this); addressLabel = new ByteAddressItem(rowIndex, byteItemsByRowIndex, this);
this->addressItemsByRowIndex.insert(std::pair(rowIndex, addressLabel)); this->addressItemsByRowIndex.emplace(rowIndex, addressLabel);
} else { } else {
addressLabel = this->addressItemsByRowIndex.at(rowIndex); addressLabel = this->addressItemsByRowIndex.at(rowIndex);
addressLabel->update();
addressLabel->setVisible(true);
} }
const auto& firstByteItem = byteItems.front(); const auto& firstByteItem = byteItems.front();
addressLabel->setAddressHex(firstByteItem->relativeAddressHex);
addressLabel->setPos( addressLabel->setPos(
leftMargin, leftMargin,
firstByteItem->pos().y() + 3 // +3 to have the address item and byte item align vertically, from center firstByteItem->pos().y() + 3 // +3 to have the address item and byte item align vertically, from center
); );
} }
// Delete any address items we no longer need // Hide any address items we no longer need
const auto addressItemCount = this->addressItemsByRowIndex.size(); const auto addressItemCount = this->addressItemsByRowIndex.size();
if (newRowCount > 0 && newRowCount < addressItemCount) { if (newRowCount > 0 && newRowCount < addressItemCount) {
for (auto i = (addressItemCount - 1); i >= newRowCount; i--) { for (auto i = (addressItemCount - 1); i >= newRowCount; i--) {
delete this->addressItemsByRowIndex.at(i); this->addressItemsByRowIndex.at(i)->setVisible(false);
this->addressItemsByRowIndex.erase(i);
} }
} }

View File

@@ -4,12 +4,19 @@
namespace Bloom::Widgets namespace Bloom::Widgets
{ {
void ByteAddressItem::setAddressHex(const QString& addressHex) { ByteAddressItem::ByteAddressItem(
std::size_t rowIndex,
const std::map<std::size_t, std::vector<ByteItem*>>& byteItemsByRowIndex,
QGraphicsItem* parent
)
: rowIndex(rowIndex)
, byteItemsByRowIndex(byteItemsByRowIndex)
, QGraphicsItem(parent)
{
this->setCacheMode( this->setCacheMode(
QGraphicsItem::CacheMode::ItemCoordinateCache, QGraphicsItem::CacheMode::ItemCoordinateCache,
QSize(ByteAddressItem::WIDTH, ByteAddressItem::HEIGHT) QSize(ByteAddressItem::WIDTH, ByteAddressItem::HEIGHT)
); );
this->addressHex = addressHex;
} }
void ByteAddressItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { void ByteAddressItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
@@ -26,6 +33,10 @@ namespace Bloom::Widgets
painter->setFont(font); painter->setFont(font);
painter->setPen(fontColor); painter->setPen(fontColor);
painter->drawText(widgetRect, Qt::AlignLeft, this->addressHex); painter->drawText(
widgetRect,
Qt::AlignLeft,
this->byteItemsByRowIndex.at(this->rowIndex)[0]->relativeAddressHex
);
} }
} }

View File

@@ -1,9 +1,8 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <QEvent>
#include <QGraphicsItem> #include <QGraphicsItem>
#include <optional> #include <map>
#include "ByteItem.hpp" #include "ByteItem.hpp"
@@ -15,9 +14,13 @@ namespace Bloom::Widgets
static constexpr int WIDTH = 75; static constexpr int WIDTH = 75;
static constexpr int HEIGHT = ByteItem::HEIGHT; static constexpr int HEIGHT = ByteItem::HEIGHT;
explicit ByteAddressItem(QGraphicsItem* parent): QGraphicsItem(parent) {}; std::size_t rowIndex = 0;
void setAddressHex(const QString& address); explicit ByteAddressItem(
std::size_t rowIndex,
const std::map<std::size_t, std::vector<ByteItem*>>& byteItemsByRowIndex,
QGraphicsItem* parent
);
[[nodiscard]] QRectF boundingRect() const override { [[nodiscard]] QRectF boundingRect() const override {
return { return {
@@ -31,6 +34,6 @@ namespace Bloom::Widgets
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
private: private:
QString addressHex; const std::map<std::size_t, std::vector<ByteItem*>>& byteItemsByRowIndex;
}; };
} }

View File

@@ -22,9 +22,11 @@ namespace Bloom::Widgets
, highlightedByteItems(highlightedByteItems) , highlightedByteItems(highlightedByteItems)
, settings(settings) , settings(settings)
{ {
static const auto cacheResolution = QSize(ByteItem::WIDTH, ByteItem::HEIGHT);
this->setCacheMode( this->setCacheMode(
QGraphicsItem::CacheMode::ItemCoordinateCache, QGraphicsItem::CacheMode::ItemCoordinateCache,
QSize(ByteItem::WIDTH, ByteItem::HEIGHT) cacheResolution
); );
this->setAcceptHoverEvents(true); this->setAcceptHoverEvents(true);

View File

@@ -16,6 +16,9 @@ namespace Bloom::Widgets
this->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff); this->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
this->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate); this->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
this->setOptimizationFlag(QGraphicsView::DontSavePainterState, true);
this->setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing, true);
this->setCacheMode(QGraphicsView::CacheBackground);
} }
void ByteItemContainerGraphicsView::initScene( void ByteItemContainerGraphicsView::initScene(

View File

@@ -44,7 +44,7 @@ namespace Bloom::Widgets
settings settings
); );
this->byteItemsByAddress.insert(std::pair( this->byteItemsByAddress.emplace(std::pair(
address, address,
byteWidget byteWidget
)); ));