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

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