feat(WideBar): allow hiding buttons with right-click

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-11-19 13:39:43 -03:00
parent 2367903ac6
commit 6e1639551b
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 64 additions and 1 deletions

View File

@ -1,4 +1,6 @@
#include "WideBar.h"
#include <QContextMenuEvent>
#include <QToolButton>
class ActionButton : public QToolButton {
@ -13,7 +15,7 @@ class ActionButton : public QToolButton {
actionChanged();
};
private slots:
public slots:
void actionChanged()
{
setEnabled(m_action->isEnabled());
@ -34,12 +36,16 @@ WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent
{
setFloatable(false);
setMovable(false);
m_bar_menu = std::make_unique<QMenu>(this);
}
WideBar::WideBar(QWidget* parent) : QToolBar(parent)
{
setFloatable(false);
setMovable(false);
m_bar_menu = std::make_unique<QMenu>(this);
}
void WideBar::addAction(QAction* action)
@ -50,6 +56,8 @@ void WideBar::addAction(QAction* action)
entry.type = BarEntry::Type::Action;
m_entries.push_back(entry);
m_menu_state = MenuState::Dirty;
}
void WideBar::addSeparator()
@ -80,6 +88,8 @@ void WideBar::insertActionBefore(QAction* before, QAction* action)
entry.type = BarEntry::Type::Action;
m_entries.insert(iter, entry);
m_menu_state = MenuState::Dirty;
}
void WideBar::insertActionAfter(QAction* after, QAction* action)
@ -94,6 +104,8 @@ void WideBar::insertActionAfter(QAction* after, QAction* action)
entry.type = BarEntry::Type::Action;
m_entries.insert(iter + 1, entry);
m_menu_state = MenuState::Dirty;
}
void WideBar::insertSpacer(QAction* action)
@ -144,4 +156,48 @@ QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title)
return contextMenu;
}
static void copyAction(QAction* from, QAction* to)
{
Q_ASSERT(from);
Q_ASSERT(to);
to->setText(from->text());
to->setIcon(from->icon());
to->setToolTip(from->toolTip());
}
void WideBar::contextMenuEvent(QContextMenuEvent* event)
{
if (m_menu_state == MenuState::Dirty) {
for (auto* old_action : m_bar_menu->actions())
old_action->deleteLater();
m_bar_menu->clear();
for (auto& entry : m_entries) {
if (entry.type != BarEntry::Type::Action)
continue;
auto act = new QAction();
copyAction(entry.menu_action, act);
act->setCheckable(true);
act->setChecked(entry.bar_action->isVisible());
connect(act, &QAction::toggled, entry.bar_action, [this, &entry](bool toggled){
entry.bar_action->setVisible(toggled);
// NOTE: This is needed so that disabled actions get reflected on the button when it is made visible.
static_cast<ActionButton*>(widgetForAction(entry.bar_action))->actionChanged();
});
m_bar_menu->addAction(act);
}
m_menu_state = MenuState::Fresh;
}
m_bar_menu->popup(event->globalPos());
}
#include "WideBar.moc"

View File

@ -5,6 +5,8 @@
#include <QMenu>
#include <QToolBar>
#include <memory>
class WideBar : public QToolBar {
Q_OBJECT
@ -22,6 +24,7 @@ class WideBar : public QToolBar {
void insertActionAfter(QAction* after, QAction* action);
QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString());
void contextMenuEvent(QContextMenuEvent*) override;
private:
struct BarEntry {
@ -34,4 +37,8 @@ class WideBar : public QToolBar {
private:
QList<BarEntry> m_entries;
// Menu to toggle visibility from buttons in the bar
std::unique_ptr<QMenu> m_bar_menu = nullptr;
enum class MenuState { Fresh, Dirty } m_menu_state = MenuState::Dirty;
};