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& byteItems = mappingPair.second;
if (byteItems.empty()) {
continue;
}
ByteAddressItem* addressLabel = nullptr;
if (static_cast<int>(rowIndex) > layoutItemMaxIndex) {
addressLabel = new ByteAddressItem(this);
this->addressItemsByRowIndex.insert(std::pair(rowIndex, addressLabel));
addressLabel = new ByteAddressItem(rowIndex, byteItemsByRowIndex, this);
this->addressItemsByRowIndex.emplace(rowIndex, addressLabel);
} else {
addressLabel = this->addressItemsByRowIndex.at(rowIndex);
addressLabel->update();
addressLabel->setVisible(true);
}
const auto& firstByteItem = byteItems.front();
addressLabel->setAddressHex(firstByteItem->relativeAddressHex);
addressLabel->setPos(
leftMargin,
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();
if (newRowCount > 0 && newRowCount < addressItemCount) {
for (auto i = (addressItemCount - 1); i >= newRowCount; i--) {
delete this->addressItemsByRowIndex.at(i);
this->addressItemsByRowIndex.erase(i);
this->addressItemsByRowIndex.at(i)->setVisible(false);
}
}

View File

@@ -4,12 +4,19 @@
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(
QGraphicsItem::CacheMode::ItemCoordinateCache,
QSize(ByteAddressItem::WIDTH, ByteAddressItem::HEIGHT)
);
this->addressHex = addressHex;
}
void ByteAddressItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
@@ -26,6 +33,10 @@ namespace Bloom::Widgets
painter->setFont(font);
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
#include <cstdint>
#include <QEvent>
#include <QGraphicsItem>
#include <optional>
#include <map>
#include "ByteItem.hpp"
@@ -15,9 +14,13 @@ namespace Bloom::Widgets
static constexpr int WIDTH = 75;
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 {
return {
@@ -31,6 +34,6 @@ namespace Bloom::Widgets
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
private:
QString addressHex;
const std::map<std::size_t, std::vector<ByteItem*>>& byteItemsByRowIndex;
};
}

View File

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

View File

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

View File

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