feat(WideBar): allow hiding buttons with right-click
Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
parent
2367903ac6
commit
6e1639551b
@ -1,4 +1,6 @@
|
|||||||
#include "WideBar.h"
|
#include "WideBar.h"
|
||||||
|
|
||||||
|
#include <QContextMenuEvent>
|
||||||
#include <QToolButton>
|
#include <QToolButton>
|
||||||
|
|
||||||
class ActionButton : public QToolButton {
|
class ActionButton : public QToolButton {
|
||||||
@ -13,7 +15,7 @@ class ActionButton : public QToolButton {
|
|||||||
|
|
||||||
actionChanged();
|
actionChanged();
|
||||||
};
|
};
|
||||||
private slots:
|
public slots:
|
||||||
void actionChanged()
|
void actionChanged()
|
||||||
{
|
{
|
||||||
setEnabled(m_action->isEnabled());
|
setEnabled(m_action->isEnabled());
|
||||||
@ -34,12 +36,16 @@ WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent
|
|||||||
{
|
{
|
||||||
setFloatable(false);
|
setFloatable(false);
|
||||||
setMovable(false);
|
setMovable(false);
|
||||||
|
|
||||||
|
m_bar_menu = std::make_unique<QMenu>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
WideBar::WideBar(QWidget* parent) : QToolBar(parent)
|
WideBar::WideBar(QWidget* parent) : QToolBar(parent)
|
||||||
{
|
{
|
||||||
setFloatable(false);
|
setFloatable(false);
|
||||||
setMovable(false);
|
setMovable(false);
|
||||||
|
|
||||||
|
m_bar_menu = std::make_unique<QMenu>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WideBar::addAction(QAction* action)
|
void WideBar::addAction(QAction* action)
|
||||||
@ -50,6 +56,8 @@ void WideBar::addAction(QAction* action)
|
|||||||
entry.type = BarEntry::Type::Action;
|
entry.type = BarEntry::Type::Action;
|
||||||
|
|
||||||
m_entries.push_back(entry);
|
m_entries.push_back(entry);
|
||||||
|
|
||||||
|
m_menu_state = MenuState::Dirty;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WideBar::addSeparator()
|
void WideBar::addSeparator()
|
||||||
@ -80,6 +88,8 @@ void WideBar::insertActionBefore(QAction* before, QAction* action)
|
|||||||
entry.type = BarEntry::Type::Action;
|
entry.type = BarEntry::Type::Action;
|
||||||
|
|
||||||
m_entries.insert(iter, entry);
|
m_entries.insert(iter, entry);
|
||||||
|
|
||||||
|
m_menu_state = MenuState::Dirty;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WideBar::insertActionAfter(QAction* after, QAction* action)
|
void WideBar::insertActionAfter(QAction* after, QAction* action)
|
||||||
@ -94,6 +104,8 @@ void WideBar::insertActionAfter(QAction* after, QAction* action)
|
|||||||
entry.type = BarEntry::Type::Action;
|
entry.type = BarEntry::Type::Action;
|
||||||
|
|
||||||
m_entries.insert(iter + 1, entry);
|
m_entries.insert(iter + 1, entry);
|
||||||
|
|
||||||
|
m_menu_state = MenuState::Dirty;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WideBar::insertSpacer(QAction* action)
|
void WideBar::insertSpacer(QAction* action)
|
||||||
@ -144,4 +156,48 @@ QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title)
|
|||||||
return contextMenu;
|
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"
|
#include "WideBar.moc"
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QToolBar>
|
#include <QToolBar>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
class WideBar : public QToolBar {
|
class WideBar : public QToolBar {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@ -22,6 +24,7 @@ class WideBar : public QToolBar {
|
|||||||
void insertActionAfter(QAction* after, QAction* action);
|
void insertActionAfter(QAction* after, QAction* action);
|
||||||
|
|
||||||
QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString());
|
QMenu* createContextMenu(QWidget* parent = nullptr, const QString& title = QString());
|
||||||
|
void contextMenuEvent(QContextMenuEvent*) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct BarEntry {
|
struct BarEntry {
|
||||||
@ -34,4 +37,8 @@ class WideBar : public QToolBar {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QList<BarEntry> m_entries;
|
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;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user