From d59c4f92bae0613608aa239e7900c7c0d22dd834 Mon Sep 17 00:00:00 2001 From: Nav Date: Mon, 8 Aug 2022 22:26:32 +0100 Subject: [PATCH] Refactored PaneState management across the PaneWidget --- .../InsightWindow/InsightWindow.cpp | 120 +++++++----------- .../InsightWindow/Widgets/PaneWidget.cpp | 90 ++++--------- .../InsightWindow/Widgets/PaneWidget.hpp | 17 +-- .../InsightWindow/Widgets/PanelWidget.cpp | 2 +- .../TargetMemoryInspectionPane.cpp | 14 +- .../TargetMemoryInspectionPane.hpp | 1 + .../TargetRegistersPaneWidget.cpp | 15 ++- .../TargetRegistersPaneWidget.hpp | 1 + src/ProjectSettings.cpp | 18 +-- src/ProjectSettings.hpp | 6 +- 10 files changed, 117 insertions(+), 167 deletions(-) diff --git a/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp b/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp index 50c4fe6e..ee92b247 100644 --- a/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/InsightWindow.cpp @@ -266,6 +266,8 @@ namespace Bloom this->layoutContainer->setFixedSize(windowSize); this->adjustPanels(); + + this->insightProjectSettings.mainWindowSize = windowSize; } void InsightWindow::showEvent(QShowEvent* event) { @@ -358,43 +360,12 @@ namespace Bloom if (lastLeftPanelState.has_value() && this->leftPanel != nullptr) { this->leftPanel->setSize(lastLeftPanelState->size); - - } - if ( - this->targetRegistersSidePane != nullptr - && this->insightProjectSettings.previousRegistersPaneState.has_value() - ) { - auto& lastRegisterSidePaneState = this->insightProjectSettings.previousRegistersPaneState.value(); - - // The register pane cannot be detached - lastRegisterSidePaneState.attached = true; - lastRegisterSidePaneState.detachedWindowState = std::nullopt; - - this->targetRegistersSidePane->restoreLastPaneState(lastRegisterSidePaneState); } if (lastBottomPanelState.has_value()) { this->bottomPanel->setSize(lastBottomPanelState->size); } - if ( - this->ramInspectionPane != nullptr - && this->insightProjectSettings.previousRamInspectionPaneState.has_value() - ) { - this->ramInspectionPane->restoreLastPaneState( - this->insightProjectSettings.previousRamInspectionPaneState.value() - ); - } - - if ( - this->eepromInspectionPane != nullptr - && this->insightProjectSettings.previousEepromInspectionPaneState.has_value() - ) { - this->eepromInspectionPane->restoreLastPaneState( - this->insightProjectSettings.previousEepromInspectionPaneState.value() - ); - } - this->setUiDisabled(this->targetState != TargetState::STOPPED); this->activated = true; emit this->activatedSignal(); @@ -559,10 +530,15 @@ namespace Bloom void InsightWindow::createPanes() { // Target registers pane + if (!this->insightProjectSettings.registersPaneState.has_value()) { + this->insightProjectSettings.registersPaneState = PaneState(false, true, std::nullopt); + } + auto* leftPanelLayout = this->leftPanel->layout(); this->targetRegistersSidePane = new TargetRegistersPaneWidget( this->targetDescriptor, this->insightWorker, + *(this->insightProjectSettings.registersPaneState), this->leftPanel ); leftPanelLayout->addWidget(this->targetRegistersSidePane); @@ -589,6 +565,10 @@ namespace Bloom // Target memory inspection panes auto* bottomPanelLayout = this->bottomPanel->layout(); if (this->targetDescriptor.memoryDescriptorsByType.contains(TargetMemoryType::RAM)) { + if (!this->insightProjectSettings.ramInspectionPaneState.has_value()) { + this->insightProjectSettings.ramInspectionPaneState = PaneState(false, true, std::nullopt); + } + auto& ramDescriptor = this->targetDescriptor.memoryDescriptorsByType.at(TargetMemoryType::RAM); if (!memoryInspectionPaneSettingsByMemoryType.contains(TargetMemoryType::RAM)) { @@ -599,6 +579,7 @@ namespace Bloom ramDescriptor, memoryInspectionPaneSettingsByMemoryType[TargetMemoryType::RAM], this->insightWorker, + *(this->insightProjectSettings.ramInspectionPaneState), this->bottomPanel ); @@ -623,11 +604,15 @@ namespace Bloom &InsightWindow::onRamInspectionPaneStateChanged ); - this->ramInspectionPane->deactivate(); this->ramInspectionButton->setDisabled(false); + this->onRamInspectionPaneStateChanged(); } if (this->targetDescriptor.memoryDescriptorsByType.contains(TargetMemoryType::EEPROM)) { + if (!this->insightProjectSettings.eepromInspectionPaneState.has_value()) { + this->insightProjectSettings.eepromInspectionPaneState = PaneState(false, true, std::nullopt); + } + auto& eepromDescriptor = this->targetDescriptor.memoryDescriptorsByType.at(TargetMemoryType::EEPROM); if (!memoryInspectionPaneSettingsByMemoryType.contains(TargetMemoryType::EEPROM)) { @@ -638,6 +623,7 @@ namespace Bloom eepromDescriptor, memoryInspectionPaneSettingsByMemoryType[TargetMemoryType::EEPROM], this->insightWorker, + *(this->insightProjectSettings.eepromInspectionPaneState), this->bottomPanel ); @@ -662,8 +648,8 @@ namespace Bloom &InsightWindow::onEepromInspectionPaneStateChanged ); - this->eepromInspectionPane->deactivate(); this->eepromInspectionButton->setDisabled(false); + this->onEepromInspectionPaneStateChanged(); } } @@ -855,7 +841,7 @@ namespace Bloom if (this->targetState == TargetState::STOPPED) { this->targetPackageWidget->setDisabled(false); - if (this->targetRegistersSidePane == nullptr || !this->targetRegistersSidePane->activated) { + if (this->targetRegistersSidePane == nullptr || !this->targetRegistersSidePane->state.activated) { this->refreshIoInspectionButton->stopSpin(); this->setUiDisabled(false); } @@ -863,7 +849,7 @@ namespace Bloom }); } - if (this->targetRegistersSidePane != nullptr && this->targetRegistersSidePane->activated) { + if (this->targetRegistersSidePane != nullptr && this->targetRegistersSidePane->state.activated) { this->targetRegistersSidePane->refreshRegisterValues([this] { this->refreshIoInspectionButton->stopSpin(); this->setUiDisabled(false); @@ -911,7 +897,7 @@ namespace Bloom } void InsightWindow::toggleTargetRegistersPane() { - if (this->targetRegistersSidePane->activated) { + if (this->targetRegistersSidePane->state.activated) { this->targetRegistersSidePane->deactivate(); } else { @@ -920,8 +906,8 @@ namespace Bloom } void InsightWindow::toggleRamInspectionPane() { - if (this->ramInspectionPane->activated) { - if (!this->ramInspectionPane->attached) { + if (this->ramInspectionPane->state.activated) { + if (!this->ramInspectionPane->state.attached) { this->ramInspectionPane->activateWindow(); this->ramInspectionButton->setChecked(true); @@ -932,10 +918,10 @@ namespace Bloom } else { if ( - this->ramInspectionPane->attached + this->ramInspectionPane->state.attached && this->eepromInspectionPane != nullptr - && this->eepromInspectionPane->activated - && this->eepromInspectionPane->attached + && this->eepromInspectionPane->state.activated + && this->eepromInspectionPane->state.attached ) { this->eepromInspectionPane->deactivate(); } @@ -945,8 +931,8 @@ namespace Bloom } void InsightWindow::toggleEepromInspectionPane() { - if (this->eepromInspectionPane->activated) { - if (!this->eepromInspectionPane->attached) { + if (this->eepromInspectionPane->state.activated) { + if (!this->eepromInspectionPane->state.attached) { this->eepromInspectionPane->activateWindow(); this->eepromInspectionButton->setChecked(true); @@ -957,10 +943,10 @@ namespace Bloom } else { if ( - this->eepromInspectionPane->attached + this->eepromInspectionPane->state.attached && this->ramInspectionPane != nullptr - && this->ramInspectionPane->activated - && this->ramInspectionPane->attached + && this->ramInspectionPane->state.activated + && this->ramInspectionPane->state.attached ) { this->ramInspectionPane->deactivate(); } @@ -970,18 +956,18 @@ namespace Bloom } void InsightWindow::onRegistersPaneStateChanged() { - this->targetRegistersButton->setChecked(this->targetRegistersSidePane->activated); + this->targetRegistersButton->setChecked(this->targetRegistersSidePane->state.activated); } void InsightWindow::onRamInspectionPaneStateChanged() { - this->ramInspectionButton->setChecked(this->ramInspectionPane->activated); + this->ramInspectionButton->setChecked(this->ramInspectionPane->state.activated); if ( - this->ramInspectionPane->activated - && this->ramInspectionPane->attached + this->ramInspectionPane->state.activated + && this->ramInspectionPane->state.attached && this->eepromInspectionPane != nullptr - && this->eepromInspectionPane->activated - && this->eepromInspectionPane->attached + && this->eepromInspectionPane->state.activated + && this->eepromInspectionPane->state.attached ) { // Both panes cannot be attached and activated at the same time. this->eepromInspectionPane->deactivate(); @@ -989,14 +975,14 @@ namespace Bloom } void InsightWindow::onEepromInspectionPaneStateChanged() { - this->eepromInspectionButton->setChecked(this->eepromInspectionPane->activated); + this->eepromInspectionButton->setChecked(this->eepromInspectionPane->state.activated); if ( - this->eepromInspectionPane->activated - && this->eepromInspectionPane->attached + this->eepromInspectionPane->state.activated + && this->eepromInspectionPane->state.attached && this->ramInspectionPane != nullptr - && this->ramInspectionPane->activated - && this->ramInspectionPane->attached + && this->ramInspectionPane->state.activated + && this->ramInspectionPane->state.attached ) { // Both panes cannot be attached and activated at the same time. this->ramInspectionPane->deactivate(); @@ -1013,29 +999,13 @@ namespace Bloom } void InsightWindow::recordInsightSettings() { - auto& projectSettings = this->insightProjectSettings; - - projectSettings.mainWindowSize = this->size(); - if (this->activated) { if (this->leftPanel != nullptr) { - projectSettings.previousLeftPanelState = this->leftPanel->getCurrentState(); - - if (this->targetRegistersSidePane != nullptr) { - projectSettings.previousRegistersPaneState = this->targetRegistersSidePane->getCurrentState(); - } + this->insightProjectSettings.previousLeftPanelState = this->leftPanel->getCurrentState(); } if (this->bottomPanel != nullptr) { - projectSettings.previousBottomPanelState = this->bottomPanel->getCurrentState(); - - if (this->ramInspectionPane != nullptr) { - projectSettings.previousRamInspectionPaneState = this->ramInspectionPane->getCurrentState(); - } - - if (this->eepromInspectionPane != nullptr) { - projectSettings.previousEepromInspectionPaneState = this->eepromInspectionPane->getCurrentState(); - } + this->insightProjectSettings.previousBottomPanelState = this->bottomPanel->getCurrentState(); } } } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.cpp index 46ae5877..c6efda41 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.cpp @@ -2,9 +2,10 @@ namespace Bloom::Widgets { - PaneWidget::PaneWidget(PanelWidget* parent) - : QWidget(parent) + PaneWidget::PaneWidget(PaneState& state, PanelWidget* parent) + : state(state) , parentPanel(parent) + , QWidget(parent) { this->setMouseTracking(false); this->setAttribute(Qt::WA_Hover, true); @@ -16,94 +17,57 @@ namespace Bloom::Widgets QObject::connect(this, &PaneWidget::paneDetached, parent, &PanelWidget::updateVisibility); } - PaneState PaneWidget::getCurrentState() const { - return PaneState( - this->activated, - this->attached, - this->getDetachedWindowState() - ); - } - void PaneWidget::activate() { - if (this->activated) { - return; - } - this->show(); - this->activated = true; + this->state.activated = true; emit this->paneActivated(); } void PaneWidget::deactivate() { - if (!this->activated) { - return; + if (this->isVisible()) { + this->hide(); } - this->hide(); - this->activated = false; + this->state.activated = false; emit this->paneDeactivated(); } - void PaneWidget::restoreLastPaneState(const PaneState& lastPaneState) { - if (lastPaneState.detachedWindowState.has_value()) { - this->lastDetachedWindowState = lastPaneState.detachedWindowState; - } - - if (!lastPaneState.attached && lastPaneState.detachedWindowState.has_value()) { - this->detach(); - } - - if (lastPaneState.activated) { - this->activate(); - } - } - void PaneWidget::detach() { - if (!this->attached) { - return; - } - this->setWindowFlag(Qt::Window); - if (this->lastDetachedWindowState.has_value()) { - this->resize(this->lastDetachedWindowState->size); - this->move(this->lastDetachedWindowState->position); + if (this->state.detachedWindowState.has_value()) { + this->resize(this->state.detachedWindowState->size); + this->move(this->state.detachedWindowState->position); + + } else { + this->state.detachedWindowState = DetachedWindowState(this->size(), this->pos()); } - if (this->activated) { - this->show(); - } - - this->attached = false; + this->state.attached = false; emit this->paneDetached(); } void PaneWidget::attach() { - if (this->attached) { - return; - } - - this->lastDetachedWindowState = this->getDetachedWindowState(); this->setWindowFlag(Qt::Window, false); - if (this->activated) { - this->show(); - } - - this->attached = true; + this->state.attached = true; emit this->paneAttached(); } + void PaneWidget::resizeEvent(QResizeEvent* event) { + if (!this->state.attached && this->state.detachedWindowState.has_value()) { + this->state.detachedWindowState->size = this->size(); + } + } + + void PaneWidget::moveEvent(QMoveEvent* event) { + if (!this->state.attached && this->state.detachedWindowState.has_value()) { + this->state.detachedWindowState->position = this->pos(); + } + } + void PaneWidget::closeEvent(QCloseEvent* event) { this->deactivate(); QWidget::closeEvent(event); } - - std::optional PaneWidget::getDetachedWindowState() const { - if (!this->attached) { - return DetachedWindowState(this->size(), this->pos()); - } - - return this->lastDetachedWindowState; - } } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.hpp index d63208ff..b2fe5d6b 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/PaneWidget.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "PanelWidget.hpp" @@ -13,19 +14,14 @@ namespace Bloom::Widgets Q_OBJECT public: - bool activated = true; - bool attached = true; + PaneState& state; PanelWidget* parentPanel = nullptr; - explicit PaneWidget(PanelWidget* parent); - - [[nodiscard]] PaneState getCurrentState() const; + explicit PaneWidget(PaneState& state, PanelWidget* parent); void activate(); void deactivate(); - void restoreLastPaneState(const PaneState& lastPaneState); - signals: void paneActivated(); void paneDeactivated(); @@ -36,11 +32,8 @@ namespace Bloom::Widgets void detach(); void attach(); + void resizeEvent(QResizeEvent* event) override; + void moveEvent(QMoveEvent* event) override; void closeEvent(QCloseEvent* event) override; - - private: - std::optional lastDetachedWindowState; - - std::optional getDetachedWindowState() const; }; } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp index b8ec952e..41d85a96 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/PanelWidget.cpp @@ -64,7 +64,7 @@ namespace Bloom::Widgets auto visible = false; for (const auto& paneWidget : paneWidgets) { - if (paneWidget->activated && paneWidget->attached) { + if (paneWidget->state.activated && paneWidget->state.attached) { visible = true; break; } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.cpp index 7282b68e..57cf895e 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.cpp @@ -23,9 +23,10 @@ namespace Bloom::Widgets const TargetMemoryDescriptor& targetMemoryDescriptor, TargetMemoryInspectionPaneSettings& settings, InsightWorker& insightWorker, + PaneState& paneState, PanelWidget* parent ) - : PaneWidget(parent) + : PaneWidget(paneState, parent) , targetMemoryDescriptor(targetMemoryDescriptor) , settings(settings) , insightWorker(insightWorker) @@ -185,6 +186,15 @@ namespace Bloom::Widgets this, &TargetMemoryInspectionPane::onProgrammingModeDisabled ); + + // Restore the state + if (!this->state.attached) { + this->detach(); + } + + if (this->state.activated) { + this->activate(); + } } void TargetMemoryInspectionPane::refreshMemoryValues(std::optional> callback) { @@ -368,7 +378,7 @@ namespace Bloom::Widgets using Targets::TargetState; this->targetState = newState; - if (newState == TargetState::STOPPED && this->activated) { + if (newState == TargetState::STOPPED && this->state.activated) { if (this->settings.refreshOnTargetStop || !this->data.has_value()) { this->refreshMemoryValues([this] { this->hexViewerWidget->setDisabled(false); diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.hpp index 0a3c8b2e..67bbe39b 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetMemoryInspectionPane/TargetMemoryInspectionPane.hpp @@ -31,6 +31,7 @@ namespace Bloom::Widgets const Targets::TargetMemoryDescriptor& targetMemoryDescriptor, TargetMemoryInspectionPaneSettings& settings, InsightWorker& insightWorker, + PaneState& paneState, PanelWidget* parent ); diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.cpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.cpp index 8dc4ce98..c44343ad 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.cpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.cpp @@ -24,9 +24,10 @@ namespace Bloom::Widgets TargetRegistersPaneWidget::TargetRegistersPaneWidget( const TargetDescriptor& targetDescriptor, InsightWorker& insightWorker, + PaneState& paneState, PanelWidget* parent ) - : PaneWidget(parent) + : PaneWidget(paneState, parent) , targetDescriptor(targetDescriptor) , insightWorker(insightWorker) { @@ -141,6 +142,16 @@ namespace Bloom::Widgets this, &TargetRegistersPaneWidget::onRegistersRead ); + + // Restore the state + if (!this->state.attached) { + // The register pane cannot be detached. + this->state.attached = true; + } + + if (this->state.activated) { + this->activate(); + } } void TargetRegistersPaneWidget::filterRegisters(const QString& keyword) { @@ -244,7 +255,7 @@ namespace Bloom::Widgets using Targets::TargetState; this->targetState = newState; - if (newState == TargetState::STOPPED && this->activated) { + if (newState == TargetState::STOPPED && this->state.activated) { this->refreshRegisterValues(); } } diff --git a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.hpp b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.hpp index 404e85d2..1ddf2059 100644 --- a/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.hpp +++ b/src/Insight/UserInterfaces/InsightWindow/Widgets/TargetRegistersPane/TargetRegistersPaneWidget.hpp @@ -29,6 +29,7 @@ namespace Bloom::Widgets TargetRegistersPaneWidget( const Targets::TargetDescriptor& targetDescriptor, InsightWorker& insightWorker, + PaneState& paneState, PanelWidget *parent ); diff --git a/src/ProjectSettings.cpp b/src/ProjectSettings.cpp index 70f09d55..881b15d7 100644 --- a/src/ProjectSettings.cpp +++ b/src/ProjectSettings.cpp @@ -43,19 +43,19 @@ namespace Bloom } if (jsonObject.contains("previousRegistersPaneState")) { - this->previousRegistersPaneState = this->paneStateFromJson( + this->registersPaneState = this->paneStateFromJson( jsonObject.find("previousRegistersPaneState")->toObject() ); } if (jsonObject.contains("previousRamInspectionPaneState")) { - this->previousRamInspectionPaneState = this->paneStateFromJson( + this->ramInspectionPaneState = this->paneStateFromJson( jsonObject.find("previousRamInspectionPaneState")->toObject() ); } if (jsonObject.contains("previousEepromInspectionPaneState")) { - this->previousEepromInspectionPaneState = this->paneStateFromJson( + this->eepromInspectionPaneState = this->paneStateFromJson( jsonObject.find("previousEepromInspectionPaneState")->toObject() ); } @@ -119,24 +119,24 @@ namespace Bloom ); } - if (this->previousRegistersPaneState.has_value()) { + if (this->registersPaneState.has_value()) { insightObj.insert( "previousRegistersPaneState", - this->paneStateToJson(this->previousRegistersPaneState.value()) + this->paneStateToJson(this->registersPaneState.value()) ); } - if (this->previousRamInspectionPaneState.has_value()) { + if (this->ramInspectionPaneState.has_value()) { insightObj.insert( "previousRamInspectionPaneState", - this->paneStateToJson(this->previousRamInspectionPaneState.value()) + this->paneStateToJson(this->ramInspectionPaneState.value()) ); } - if (this->previousEepromInspectionPaneState.has_value()) { + if (this->eepromInspectionPaneState.has_value()) { insightObj.insert( "previousEepromInspectionPaneState", - this->paneStateToJson(this->previousEepromInspectionPaneState.value()) + this->paneStateToJson(this->eepromInspectionPaneState.value()) ); } diff --git a/src/ProjectSettings.hpp b/src/ProjectSettings.hpp index 986b309e..d82b4ede 100644 --- a/src/ProjectSettings.hpp +++ b/src/ProjectSettings.hpp @@ -22,9 +22,9 @@ namespace Bloom std::optional mainWindowSize; std::optional previousLeftPanelState; std::optional previousBottomPanelState; - std::optional previousRegistersPaneState; - std::optional previousRamInspectionPaneState; - std::optional previousEepromInspectionPaneState; + std::optional registersPaneState; + std::optional ramInspectionPaneState; + std::optional eepromInspectionPaneState; std::map< Targets::TargetMemoryType,