From 479843f56b42d7044d3d02278a9cabc2c24e147a Mon Sep 17 00:00:00 2001 From: flow Date: Sat, 19 Nov 2022 17:10:43 -0300 Subject: [PATCH] feat(WideBar): allow loading/unloading visibility via a byte array I really wanted to use a QBitArray :c Signed-off-by: flow --- launcher/ui/widgets/WideBar.cpp | 30 ++++++++++++++++++++++++++++++ launcher/ui/widgets/WideBar.h | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/launcher/ui/widgets/WideBar.cpp b/launcher/ui/widgets/WideBar.cpp index ed7dc5fa..2ad2caec 100644 --- a/launcher/ui/widgets/WideBar.cpp +++ b/launcher/ui/widgets/WideBar.cpp @@ -200,4 +200,34 @@ void WideBar::contextMenuEvent(QContextMenuEvent* event) m_bar_menu->popup(event->globalPos()); } +[[nodiscard]] QByteArray WideBar::getVisibilityState() const +{ + QByteArray state; + + for (auto const& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + + state.append(entry.bar_action->isVisible() ? '1' : '0'); + } + + return state; +} + +void WideBar::setVisibilityState(QByteArray&& state) +{ + qsizetype i = 0; + for (auto& entry : m_entries) { + if (entry.type != BarEntry::Type::Action) + continue; + if (i == state.size()) + break; + + entry.bar_action->setVisible(state.at(i++) == '1'); + + // NOTE: This is needed so that disabled actions get reflected on the button when it is made visible. + static_cast(widgetForAction(entry.bar_action))->actionChanged(); + } +} + #include "WideBar.moc" diff --git a/launcher/ui/widgets/WideBar.h b/launcher/ui/widgets/WideBar.h index 8421eaf4..0d60f8a4 100644 --- a/launcher/ui/widgets/WideBar.h +++ b/launcher/ui/widgets/WideBar.h @@ -26,6 +26,12 @@ class WideBar : public QToolBar { QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString()); void contextMenuEvent(QContextMenuEvent*) override; + // Ideally we would use a QBitArray for this, but it doesn't support string conversion, + // so using it in settings is very messy. + + [[nodiscard]] QByteArray getVisibilityState() const; + void setVisibilityState(QByteArray&&); + private: struct BarEntry { enum class Type { None, Action, Separator, Spacer } type = Type::None;