From 77cefd8308d0913ef5589e165c9cab0857de2200 Mon Sep 17 00:00:00 2001 From: Nav Date: Fri, 29 Oct 2021 22:47:41 +0100 Subject: [PATCH] ByteAddressContainer and ByteAddressItem QGraphicsItems --- CMakeLists.txt | 2 + .../HexViewerWidget/ByteAddressContainer.cpp | 81 +++++++++++++++++++ .../HexViewerWidget/ByteAddressContainer.hpp | 23 ++---- .../HexViewerWidget/ByteAddressItem.cpp | 23 ++++++ .../HexViewerWidget/ByteAddressItem.hpp | 36 +++++++++ .../HexViewerWidget/ByteItemGraphicsScene.cpp | 9 ++- .../HexViewerWidget/ByteItemGraphicsScene.hpp | 3 + 7 files changed, 159 insertions(+), 18 deletions(-) create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.cpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.cpp create mode 100644 src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b4b2e040..06aab8c3 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,6 +180,8 @@ add_executable(Bloom src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemContainerGraphicsView.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.cpp src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItem.cpp + src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.cpp + src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.cpp ) set_target_properties(Bloom PROPERTIES OUTPUT_NAME bloom) diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.cpp new file mode 100644 index 00000000..b7e843a1 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.cpp @@ -0,0 +1,81 @@ +#include "ByteAddressContainer.hpp" + +#include +#include +#include +#include +#include + +#include "src/Logger/Logger.hpp" + +using namespace Bloom::Widgets; + +ByteAddressContainer::ByteAddressContainer() { +} + +void ByteAddressContainer::adjustAddressLabels( + const std::map>& byteItemsByRowIndex +) { + static const auto margins = QMargins(0, 10, 0, 0); + + const auto rowCount = byteItemsByRowIndex.size(); + const auto addressLabelCount = this->addressItemsByRowIndex.size(); + const auto layoutItemMaxIndex = static_cast(addressLabelCount - 1); + for (const auto& mappingPair : byteItemsByRowIndex) { + const auto rowIndex = static_cast(mappingPair.first); + const auto& byteWidgets = mappingPair.second; + + if (byteWidgets.empty()) { + continue; + } + + ByteAddressItem* addressLabel; + if (static_cast(rowIndex) > layoutItemMaxIndex) { + addressLabel = new ByteAddressItem(this); + this->addressItemsByRowIndex.insert(std::pair(rowIndex, addressLabel)); + + } else { + addressLabel = this->addressItemsByRowIndex.at(rowIndex); + } + + addressLabel->setAddressHex(byteWidgets.front()->relativeAddressHex); + addressLabel->setPos( + 0, + margins.top() + static_cast(rowIndex * (ByteAddressItem::HEIGHT + ByteItem::BOTTOM_MARGIN)) + ); + } + + if (rowCount > 0 && rowCount > byteItemsByRowIndex.size()) { + const auto rowCount = static_cast(byteItemsByRowIndex.size()); + for (std::size_t i = rowCount - 1; i >= rowCount; i--) { + delete this->addressItemsByRowIndex.at(i); + this->addressItemsByRowIndex.erase(i); + } + } + + Logger::error("Done with rows: " + std::to_string(this->addressItemsByRowIndex.size())); + + this->update(); +} + +void ByteAddressContainer::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { + static const auto backgroundColor = QColor(0x35, 0x36, 0x33); + static const auto borderColor = QColor(0x41, 0x42, 0x3F); + + painter->setPen(Qt::PenStyle::NoPen); + painter->setBrush(backgroundColor); + painter->drawRect( + 0, + 0, + ByteAddressContainer::WIDTH, + static_cast(this->boundingRect().height()) + ); + + painter->setPen(borderColor); + painter->drawLine( + ByteAddressContainer::WIDTH - 1, + 0, + ByteAddressContainer::WIDTH - 1, + static_cast(this->boundingRect().height()) + ); +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.hpp index 269880fa..572806b5 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressContainer.hpp @@ -4,9 +4,13 @@ #include #include #include +#include +#include #include +#include -#include "src/Insight/UserInterfaces/InsightWindow/Widgets/ClickableWidget.hpp" +#include "ByteItem.hpp" +#include "ByteAddressItem.hpp" namespace Bloom::Widgets { @@ -15,19 +19,6 @@ namespace Bloom::Widgets public: static constexpr int WIDTH = 85; - static constexpr int RIGHT_MARGIN = 5; - static constexpr int BOTTOM_MARGIN = 5; - - std::size_t byteIndex; - unsigned char value = 0x00; - std::uint32_t address = 0x00; - QString addressHex; - QString relativeAddressHex; - bool valueInitialised = false; - - std::size_t currentRowIndex = 0; - std::size_t currentColumnIndex = 0; - ByteAddressContainer(); [[nodiscard]] QRectF boundingRect() const override { @@ -39,8 +30,10 @@ namespace Bloom::Widgets ); } + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; + void adjustAddressLabels(const std::map>& byteItemsByRowIndex); private: - + std::map addressItemsByRowIndex; }; } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.cpp new file mode 100644 index 00000000..1a55cbba --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.cpp @@ -0,0 +1,23 @@ +#include "ByteAddressItem.hpp" + +#include +#include + +using namespace Bloom::Widgets; + +void ByteAddressItem::setAddressHex(const QString& addressHex) { + this->addressHex = addressHex; +} + +void ByteAddressItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { + painter->setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform, true); + + static const auto fontColor = QColor(0x8F, 0x91, 0x92); + static const auto widgetRect = this->boundingRect(); + static auto font = painter->font(); + font.setPixelSize(12); + + painter->setFont(font); + painter->setPen(fontColor); + painter->drawText(widgetRect, Qt::AlignCenter, this->addressHex); +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.hpp new file mode 100644 index 00000000..02c3f5e8 --- /dev/null +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteAddressItem.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include +#include + +#include "ByteItem.hpp" + +namespace Bloom::Widgets +{ + class ByteAddressItem: public QGraphicsItem + { + public: + static constexpr int WIDTH = 84; + static constexpr int HEIGHT = ByteItem::HEIGHT; + + ByteAddressItem(QGraphicsItem* parent): QGraphicsItem(parent) {}; + + void setAddressHex(const QString& address); + + [[nodiscard]] QRectF boundingRect() const override { + return QRectF( + 0, + 0, + ByteAddressItem::WIDTH, + ByteAddressItem::HEIGHT + ); + } + + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override; + + private: + QString addressHex; + }; +} diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.cpp index b14f68dd..47f569ef 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.cpp @@ -25,6 +25,9 @@ hoveredAddressLabel(hoveredAddressLabel), parent(parent) { this->setObjectName("byte-widget-container"); + this->byteAddressContainer = new ByteAddressContainer(); + this->addItem(this->byteAddressContainer); + /* * Construct ByteWidget objects * @@ -72,7 +75,7 @@ void ByteItemGraphicsScene::adjustByteWidgets() { constexpr auto byteWidgetWidth = ByteItem::WIDTH + ByteItem::RIGHT_MARGIN; constexpr auto byteWidgetHeight = ByteItem::HEIGHT + ByteItem::BOTTOM_MARGIN; const auto rowCapacity = static_cast( - std::floor((width - margins.left() - margins.right()) / byteWidgetWidth) + std::floor((width - margins.left() - margins.right() - ByteAddressContainer::WIDTH) / byteWidgetWidth) ); const auto rowCount = static_cast( std::ceil(static_cast(this->byteWidgetsByAddress.size()) / static_cast(rowCapacity)) @@ -88,7 +91,7 @@ void ByteItemGraphicsScene::adjustByteWidgets() { ); byteWidget->setPos( - static_cast(columnIndex * byteWidgetWidth + static_cast(margins.left())), + static_cast(columnIndex * byteWidgetWidth + margins.left() + ByteAddressContainer::WIDTH), static_cast(rowIndex * byteWidgetHeight + static_cast(margins.top())) ); @@ -109,7 +112,7 @@ void ByteItemGraphicsScene::adjustByteWidgets() { this->byteWidgetsByRowIndex = std::move(byteWidgetsByRowIndex); this->byteWidgetsByColumnIndex = std::move(byteWidgetsByColumnIndex); - emit this->byteWidgetsAdjusted(); + this->byteAddressContainer->adjustAddressLabels(this->byteWidgetsByRowIndex); } void ByteItemGraphicsScene::onTargetStateChanged(Targets::TargetState newState) { diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.hpp index 2f6fd4a5..35441407 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/HexViewerWidget/ByteItemGraphicsScene.hpp @@ -18,6 +18,7 @@ #include "src/Insight/InsightWorker/InsightWorker.hpp" #include "ByteItem.hpp" +#include "ByteAddressContainer.hpp" namespace Bloom::Widgets { @@ -57,6 +58,8 @@ namespace Bloom::Widgets QWidget* parent = nullptr; QLabel* hoveredAddressLabel = nullptr; + ByteAddressContainer* byteAddressContainer = nullptr; + private slots: void onTargetStateChanged(Targets::TargetState newState); void onByteWidgetEnter(Bloom::Widgets::ByteItem* widget);