Highlighted addresses in hex viewer

This commit is contained in:
Nav
2021-12-27 02:16:18 +00:00
parent dd5424b6bb
commit c3db0eba65
4 changed files with 36 additions and 10 deletions

View File

@@ -11,6 +11,7 @@ ByteItem::ByteItem(
std::optional<std::uint32_t>& currentStackPointer, std::optional<std::uint32_t>& currentStackPointer,
std::optional<ByteItem*>& hoveredByteItem, std::optional<ByteItem*>& hoveredByteItem,
std::optional<AnnotationItem*>& hoveredAnnotationItem, std::optional<AnnotationItem*>& hoveredAnnotationItem,
std::set<std::uint32_t>& highlightedAddresses,
const HexViewerWidgetSettings& settings const HexViewerWidgetSettings& settings
): ):
QGraphicsItem(nullptr), QGraphicsItem(nullptr),
@@ -19,6 +20,7 @@ address(address),
currentStackPointer(currentStackPointer), currentStackPointer(currentStackPointer),
hoveredByteItem(hoveredByteItem), hoveredByteItem(hoveredByteItem),
hoveredAnnotationItem(hoveredAnnotationItem), hoveredAnnotationItem(hoveredAnnotationItem),
highlightedAddresses(highlightedAddresses),
settings(settings) settings(settings)
{ {
this->setCacheMode( this->setCacheMode(
@@ -49,12 +51,14 @@ void ByteItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
painter->setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform, true); painter->setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform, true);
painter->setPen(Qt::PenStyle::NoPen); painter->setPen(Qt::PenStyle::NoPen);
// TODO: This code could do with some tidying. It's getting quite messy.
static const auto widgetRect = this->boundingRect(); static const auto widgetRect = this->boundingRect();
static const auto standardTextColor = QColor(0xAF, 0xB1, 0xB3); static const auto standardTextColor = QColor(0xAF, 0xB1, 0xB3);
static const auto valueChangedTextColor = QColor(0x54, 0x7F, 0xBA); static const auto valueChangedTextColor = QColor(0x54, 0x7F, 0xBA);
auto asciiTextColor = QColor(0xA7, 0x77, 0x26);
static auto font = QFont("'Ubuntu', sans-serif"); static auto font = QFont("'Ubuntu', sans-serif");
static const auto highlightedBackgroundColor = QColor(0x3C, 0x59, 0x5C, 255);
static const auto focusedRegionBackgroundColor = QColor(0x44, 0x44, 0x41, 255); static const auto focusedRegionBackgroundColor = QColor(0x44, 0x44, 0x41, 255);
static const auto stackMemoryBackgroundColor = QColor(0x67, 0x57, 0x20, 210); static const auto stackMemoryBackgroundColor = QColor(0x67, 0x57, 0x20, 210);
static const auto hoveredBackgroundColor = QColor(0x8E, 0x8B, 0x83, 70); static const auto hoveredBackgroundColor = QColor(0x8E, 0x8B, 0x83, 70);
@@ -62,13 +66,23 @@ void ByteItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
static const auto hoveredAnnotationBackgroundColor = QColor(0x8E, 0x8B, 0x83, 50); static const auto hoveredAnnotationBackgroundColor = QColor(0x8E, 0x8B, 0x83, 50);
const auto isEnabled = this->isEnabled(); const auto isEnabled = this->isEnabled();
const auto highlightingEnabled = !this->highlightedAddresses.empty();
const auto highlightedByte = highlightingEnabled && this->highlightedAddresses.contains(this->address);
auto textColor = standardTextColor; auto textColor = standardTextColor;
auto asciiTextColor = QColor(0xA7, 0x77, 0x26);
auto backgroundColor = std::optional<QColor>(); auto backgroundColor = std::optional<QColor>();
font.setPixelSize(11); font.setPixelSize(11);
painter->setFont(font); painter->setFont(font);
if (this->settings.highlightStackMemory && this->currentStackPointer.has_value() if (highlightedByte) {
backgroundColor = highlightedBackgroundColor;
asciiTextColor = standardTextColor;
} else if (this->settings.highlightStackMemory && this->currentStackPointer.has_value()
&& this->address > this->currentStackPointer && this->address > this->currentStackPointer
) { ) {
// This byte is within the stack memory // This byte is within the stack memory
@@ -115,7 +129,7 @@ void ByteItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
} }
if (backgroundColor.has_value()) { if (backgroundColor.has_value()) {
if (!isEnabled) { if (!isEnabled || (highlightingEnabled && !highlightedByte)) {
backgroundColor->setAlpha(100); backgroundColor->setAlpha(100);
} }
@@ -125,7 +139,7 @@ void ByteItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
if (this->valueInitialised && this->excludedMemoryRegion == nullptr) { if (this->valueInitialised && this->excludedMemoryRegion == nullptr) {
if (this->settings.displayAsciiValues && this->asciiValue.has_value()) { if (this->settings.displayAsciiValues && this->asciiValue.has_value()) {
if (!isEnabled) { if (!isEnabled || (highlightingEnabled && !highlightedByte)) {
asciiTextColor.setAlpha(100); asciiTextColor.setAlpha(100);
} }
@@ -133,7 +147,7 @@ void ByteItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
painter->drawText(widgetRect, Qt::AlignCenter, this->asciiValue.value()); painter->drawText(widgetRect, Qt::AlignCenter, this->asciiValue.value());
} else { } else {
if (!isEnabled || this->settings.displayAsciiValues) { if (!isEnabled || (highlightingEnabled && !highlightedByte) || this->settings.displayAsciiValues) {
textColor.setAlpha(100); textColor.setAlpha(100);
} }

View File

@@ -4,6 +4,7 @@
#include <QEvent> #include <QEvent>
#include <QGraphicsItem> #include <QGraphicsItem>
#include <optional> #include <optional>
#include <set>
#include "HexViewerWidgetSettings.hpp" #include "HexViewerWidgetSettings.hpp"
#include "AnnotationItem.hpp" #include "AnnotationItem.hpp"
@@ -40,6 +41,7 @@ namespace Bloom::Widgets
std::optional<std::uint32_t>& currentStackPointer, std::optional<std::uint32_t>& currentStackPointer,
std::optional<ByteItem*>& hoveredByteItem, std::optional<ByteItem*>& hoveredByteItem,
std::optional<AnnotationItem*>& hoveredAnnotationItem, std::optional<AnnotationItem*>& hoveredAnnotationItem,
std::set<std::uint32_t>& highlightedAddresses,
const HexViewerWidgetSettings& settings const HexViewerWidgetSettings& settings
); );
@@ -68,5 +70,6 @@ namespace Bloom::Widgets
std::optional<ByteItem*>& hoveredByteItem; std::optional<ByteItem*>& hoveredByteItem;
std::optional<AnnotationItem*>& hoveredAnnotationItem; std::optional<AnnotationItem*>& hoveredAnnotationItem;
std::optional<std::uint32_t>& currentStackPointer; std::optional<std::uint32_t>& currentStackPointer;
std::set<std::uint32_t>& highlightedAddresses;
}; };
} }

View File

@@ -41,6 +41,7 @@ ByteItemGraphicsScene::ByteItemGraphicsScene(
this->currentStackPointer, this->currentStackPointer,
this->hoveredByteWidget, this->hoveredByteWidget,
this->hoveredAnnotationItem, this->hoveredAnnotationItem,
this->highlightedAddresses,
settings settings
); );
@@ -77,6 +78,11 @@ void ByteItemGraphicsScene::updateStackPointer(std::uint32_t stackPointer) {
this->invalidateChildItemCaches(); this->invalidateChildItemCaches();
} }
void ByteItemGraphicsScene::setHighlightedAddresses(const std::set<std::uint32_t>& highlightedAddresses) {
this->highlightedAddresses = highlightedAddresses;
this->invalidateChildItemCaches();
}
void ByteItemGraphicsScene::refreshRegions() { void ByteItemGraphicsScene::refreshRegions() {
for (auto& [byteAddress, byteWidget] : this->byteItemsByAddress) { for (auto& [byteAddress, byteWidget] : this->byteItemsByAddress) {
byteWidget->focusedMemoryRegion = nullptr; byteWidget->focusedMemoryRegion = nullptr;

View File

@@ -38,9 +38,6 @@ namespace Bloom::Widgets
Q_OBJECT Q_OBJECT
public: public:
std::optional<ByteItem*> hoveredByteWidget;
std::optional<AnnotationItem*> hoveredAnnotationItem;
ByteItemGraphicsScene( ByteItemGraphicsScene(
const Targets::TargetMemoryDescriptor& targetMemoryDescriptor, const Targets::TargetMemoryDescriptor& targetMemoryDescriptor,
std::vector<FocusedMemoryRegion>& focusedMemoryRegions, std::vector<FocusedMemoryRegion>& focusedMemoryRegions,
@@ -53,6 +50,7 @@ namespace Bloom::Widgets
void updateValues(const Targets::TargetMemoryBuffer& buffer); void updateValues(const Targets::TargetMemoryBuffer& buffer);
void updateStackPointer(std::uint32_t stackPointer); void updateStackPointer(std::uint32_t stackPointer);
void setHighlightedAddresses(const std::set<std::uint32_t>& highlightedAddresses);
void refreshRegions(); void refreshRegions();
void adjustSize(bool forced = false); void adjustSize(bool forced = false);
void setEnabled(bool enabled); void setEnabled(bool enabled);
@@ -70,6 +68,11 @@ namespace Bloom::Widgets
std::vector<FocusedMemoryRegion>& focusedMemoryRegions; std::vector<FocusedMemoryRegion>& focusedMemoryRegions;
std::vector<ExcludedMemoryRegion>& excludedMemoryRegions; std::vector<ExcludedMemoryRegion>& excludedMemoryRegions;
bool enabled = true;
std::optional<ByteItem*> hoveredByteWidget;
std::optional<AnnotationItem*> hoveredAnnotationItem;
std::optional<std::uint32_t> currentStackPointer; std::optional<std::uint32_t> currentStackPointer;
Targets::TargetMemoryBuffer lastValueBuffer; Targets::TargetMemoryBuffer lastValueBuffer;
@@ -91,7 +94,7 @@ namespace Bloom::Widgets
ByteAddressContainer* byteAddressContainer = nullptr; ByteAddressContainer* byteAddressContainer = nullptr;
bool enabled = true; std::set<std::uint32_t> highlightedAddresses;
int getSceneWidth() { int getSceneWidth() {
/* /*