Refactored PaneState management across the PaneWidget

This commit is contained in:
Nav
2022-08-08 22:26:32 +01:00
parent c88395b8eb
commit d59c4f92ba
10 changed files with 117 additions and 167 deletions

View File

@@ -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();
}
}
}

View File

@@ -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<DetachedWindowState> PaneWidget::getDetachedWindowState() const {
if (!this->attached) {
return DetachedWindowState(this->size(), this->pos());
}
return this->lastDetachedWindowState;
}
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <QWidget>
#include <QEvent>
#include <optional>
#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<DetachedWindowState> lastDetachedWindowState;
std::optional<DetachedWindowState> getDetachedWindowState() const;
};
}

View File

@@ -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;
}

View File

@@ -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<std::function<void(void)>> 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);

View File

@@ -31,6 +31,7 @@ namespace Bloom::Widgets
const Targets::TargetMemoryDescriptor& targetMemoryDescriptor,
TargetMemoryInspectionPaneSettings& settings,
InsightWorker& insightWorker,
PaneState& paneState,
PanelWidget* parent
);

View File

@@ -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();
}
}

View File

@@ -29,6 +29,7 @@ namespace Bloom::Widgets
TargetRegistersPaneWidget(
const Targets::TargetDescriptor& targetDescriptor,
InsightWorker& insightWorker,
PaneState& paneState,
PanelWidget *parent
);

View File

@@ -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())
);
}

View File

@@ -22,9 +22,9 @@ namespace Bloom
std::optional<QSize> mainWindowSize;
std::optional<Widgets::PanelState> previousLeftPanelState;
std::optional<Widgets::PanelState> previousBottomPanelState;
std::optional<Widgets::PaneState> previousRegistersPaneState;
std::optional<Widgets::PaneState> previousRamInspectionPaneState;
std::optional<Widgets::PaneState> previousEepromInspectionPaneState;
std::optional<Widgets::PaneState> registersPaneState;
std::optional<Widgets::PaneState> ramInspectionPaneState;
std::optional<Widgets::PaneState> eepromInspectionPaneState;
std::map<
Targets::TargetMemoryType,