Storing and restoring DetachedWindowState

This commit is contained in:
Nav
2022-07-19 22:31:36 +01:00
parent 5b53040190
commit f43c88f61a
5 changed files with 155 additions and 22 deletions

View File

@@ -1,11 +1,39 @@
#pragma once
#include <QSize>
#include <QPoint>
#include <optional>
namespace Bloom::Widgets
{
struct DetachedWindowState
{
QSize size;
QPoint position;
DetachedWindowState() = default;
DetachedWindowState(QSize size, QPoint position)
: size(size)
, position(position)
{}
};
struct PaneState
{
bool activated = false;
bool attached = true;
explicit PaneState(bool activated): activated(activated) {};
std::optional<DetachedWindowState> detachedWindowState;
PaneState(
bool activated,
bool attached,
std::optional<DetachedWindowState> detachedWindowState
)
: activated(activated)
, attached(attached)
, detachedWindowState(detachedWindowState)
{};
};
}

View File

@@ -16,6 +16,14 @@ 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;
@@ -36,18 +44,51 @@ namespace Bloom::Widgets
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);
this->show();
if (this->lastDetachedWindowState.has_value()) {
this->resize(this->lastDetachedWindowState->size);
this->move(this->lastDetachedWindowState->position);
}
if (this->activated) {
this->show();
}
this->attached = false;
emit this->paneDetached();
}
void PaneWidget::attach() {
if (this->attached) {
return;
}
this->lastDetachedWindowState = this->getDetachedWindowState();
this->setWindowFlag(Qt::Window, false);
this->hide();
this->show();
if (this->activated) {
this->show();
}
this->attached = true;
emit this->paneAttached();
@@ -58,4 +99,11 @@ namespace Bloom::Widgets
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 <optional>
#include "PanelWidget.hpp"
#include "PaneState.hpp"
@@ -18,15 +19,13 @@ namespace Bloom::Widgets
explicit PaneWidget(PanelWidget* parent);
[[nodiscard]] PaneState getCurrentState() const {
return PaneState(
this->activated
);
}
[[nodiscard]] PaneState getCurrentState() const;
void activate();
void deactivate();
void restoreLastPaneState(const PaneState& lastPaneState);
signals:
void paneActivated();
void paneDeactivated();
@@ -38,5 +37,10 @@ namespace Bloom::Widgets
void attach();
void closeEvent(QCloseEvent* event) override;
private:
std::optional<DetachedWindowState> lastDetachedWindowState;
std::optional<DetachedWindowState> getDetachedWindowState() const;
};
}