2019-07-23 04:18:14 +05:30
|
|
|
#include "WideBar.h"
|
2022-11-19 22:09:43 +05:30
|
|
|
|
|
|
|
#include <QContextMenuEvent>
|
2019-07-23 04:18:14 +05:30
|
|
|
#include <QToolButton>
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
class ActionButton : public QToolButton {
|
2019-07-23 04:18:14 +05:30
|
|
|
Q_OBJECT
|
2022-11-19 20:25:40 +05:30
|
|
|
public:
|
|
|
|
ActionButton(QAction* action, QWidget* parent = nullptr) : QToolButton(parent), m_action(action)
|
|
|
|
{
|
2019-07-23 04:18:14 +05:30
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2022-11-19 20:25:40 +05:30
|
|
|
|
2019-07-23 04:18:14 +05:30
|
|
|
connect(action, &QAction::changed, this, &ActionButton::actionChanged);
|
|
|
|
connect(this, &ActionButton::clicked, action, &QAction::trigger);
|
2022-11-19 20:25:40 +05:30
|
|
|
|
2019-07-23 04:18:14 +05:30
|
|
|
actionChanged();
|
|
|
|
};
|
2022-11-19 22:09:43 +05:30
|
|
|
public slots:
|
2022-11-19 20:25:40 +05:30
|
|
|
void actionChanged()
|
|
|
|
{
|
2019-07-23 04:18:14 +05:30
|
|
|
setEnabled(m_action->isEnabled());
|
|
|
|
setChecked(m_action->isChecked());
|
|
|
|
setCheckable(m_action->isCheckable());
|
|
|
|
setText(m_action->text());
|
|
|
|
setIcon(m_action->icon());
|
|
|
|
setToolTip(m_action->toolTip());
|
|
|
|
setHidden(!m_action->isVisible());
|
2019-07-25 04:32:30 +05:30
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
private:
|
|
|
|
QAction* m_action;
|
|
|
|
};
|
2019-07-23 04:18:14 +05:30
|
|
|
|
|
|
|
WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent)
|
|
|
|
{
|
|
|
|
setFloatable(false);
|
|
|
|
setMovable(false);
|
2022-11-19 22:09:43 +05:30
|
|
|
|
|
|
|
m_bar_menu = std::make_unique<QMenu>(this);
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
WideBar::WideBar(QWidget* parent) : QToolBar(parent)
|
|
|
|
{
|
|
|
|
setFloatable(false);
|
|
|
|
setMovable(false);
|
2022-11-19 22:09:43 +05:30
|
|
|
|
|
|
|
m_bar_menu = std::make_unique<QMenu>(this);
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void WideBar::addAction(QAction* action)
|
|
|
|
{
|
2022-11-19 20:25:40 +05:30
|
|
|
BarEntry entry;
|
|
|
|
entry.bar_action = addWidget(new ActionButton(action, this));
|
|
|
|
entry.menu_action = action;
|
|
|
|
entry.type = BarEntry::Type::Action;
|
|
|
|
|
2019-07-25 04:32:30 +05:30
|
|
|
m_entries.push_back(entry);
|
2022-11-19 22:09:43 +05:30
|
|
|
|
|
|
|
m_menu_state = MenuState::Dirty;
|
2019-07-25 04:32:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void WideBar::addSeparator()
|
|
|
|
{
|
2022-11-19 20:25:40 +05:30
|
|
|
BarEntry entry;
|
|
|
|
entry.bar_action = QToolBar::addSeparator();
|
|
|
|
entry.type = BarEntry::Type::Separator;
|
|
|
|
|
2019-07-25 04:32:30 +05:30
|
|
|
m_entries.push_back(entry);
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
auto WideBar::getMatching(QAction* act) -> QList<BarEntry>::iterator
|
2022-04-22 20:06:00 +05:30
|
|
|
{
|
2022-11-19 20:25:40 +05:30
|
|
|
auto iter = std::find_if(m_entries.begin(), m_entries.end(), [act](BarEntry const& entry) { return entry.menu_action == act; });
|
|
|
|
|
2022-04-22 20:06:00 +05:30
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
void WideBar::insertActionBefore(QAction* before, QAction* action)
|
|
|
|
{
|
2022-04-22 20:06:00 +05:30
|
|
|
auto iter = getMatching(before);
|
2022-11-19 20:25:40 +05:30
|
|
|
if (iter == m_entries.end())
|
2022-01-29 00:02:42 +05:30
|
|
|
return;
|
2022-04-22 20:06:00 +05:30
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
BarEntry entry;
|
|
|
|
entry.bar_action = insertWidget(iter->bar_action, new ActionButton(action, this));
|
|
|
|
entry.menu_action = action;
|
|
|
|
entry.type = BarEntry::Type::Action;
|
|
|
|
|
2022-01-29 00:02:42 +05:30
|
|
|
m_entries.insert(iter, entry);
|
2022-11-19 22:09:43 +05:30
|
|
|
|
|
|
|
m_menu_state = MenuState::Dirty;
|
2022-01-29 00:02:42 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
void WideBar::insertActionAfter(QAction* after, QAction* action)
|
|
|
|
{
|
2022-04-22 20:06:00 +05:30
|
|
|
auto iter = getMatching(after);
|
2022-11-19 20:25:40 +05:30
|
|
|
if (iter == m_entries.end())
|
2022-04-22 20:06:00 +05:30
|
|
|
return;
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
BarEntry entry;
|
|
|
|
entry.bar_action = insertWidget((iter + 1)->bar_action, new ActionButton(action, this));
|
|
|
|
entry.menu_action = action;
|
|
|
|
entry.type = BarEntry::Type::Action;
|
|
|
|
|
2022-04-22 20:06:00 +05:30
|
|
|
m_entries.insert(iter + 1, entry);
|
2022-11-19 22:09:43 +05:30
|
|
|
|
|
|
|
m_menu_state = MenuState::Dirty;
|
2022-04-22 20:06:00 +05:30
|
|
|
}
|
|
|
|
|
2019-07-23 04:18:14 +05:30
|
|
|
void WideBar::insertSpacer(QAction* action)
|
|
|
|
{
|
2022-04-22 20:06:00 +05:30
|
|
|
auto iter = getMatching(action);
|
2022-11-19 20:25:40 +05:30
|
|
|
if (iter == m_entries.end())
|
2019-07-23 04:18:14 +05:30
|
|
|
return;
|
2022-04-22 20:06:00 +05:30
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
auto* spacer = new QWidget();
|
2019-07-23 04:18:14 +05:30
|
|
|
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
2019-07-25 04:32:30 +05:30
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
BarEntry entry;
|
|
|
|
entry.bar_action = insertWidget(iter->bar_action, spacer);
|
|
|
|
entry.type = BarEntry::Type::Spacer;
|
2019-07-25 04:32:30 +05:30
|
|
|
m_entries.insert(iter, entry);
|
|
|
|
}
|
|
|
|
|
2022-04-22 20:06:00 +05:30
|
|
|
void WideBar::insertSeparator(QAction* before)
|
|
|
|
{
|
|
|
|
auto iter = getMatching(before);
|
2022-11-19 20:25:40 +05:30
|
|
|
if (iter == m_entries.end())
|
2022-04-22 20:06:00 +05:30
|
|
|
return;
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
BarEntry entry;
|
|
|
|
entry.bar_action = QToolBar::insertSeparator(before);
|
|
|
|
entry.type = BarEntry::Type::Separator;
|
|
|
|
|
2022-04-22 20:06:00 +05:30
|
|
|
m_entries.insert(iter, entry);
|
|
|
|
}
|
|
|
|
|
2022-11-19 20:25:40 +05:30
|
|
|
QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title)
|
2019-07-25 04:32:30 +05:30
|
|
|
{
|
2022-11-19 20:25:40 +05:30
|
|
|
auto* contextMenu = new QMenu(title, parent);
|
|
|
|
for (auto& item : m_entries) {
|
|
|
|
switch (item.type) {
|
2019-07-25 04:32:30 +05:30
|
|
|
default:
|
2022-11-19 20:25:40 +05:30
|
|
|
case BarEntry::Type::None:
|
2019-07-25 04:32:30 +05:30
|
|
|
break;
|
2022-11-19 20:25:40 +05:30
|
|
|
case BarEntry::Type::Separator:
|
|
|
|
case BarEntry::Type::Spacer:
|
2019-07-25 04:32:30 +05:30
|
|
|
contextMenu->addSeparator();
|
|
|
|
break;
|
2022-11-19 20:25:40 +05:30
|
|
|
case BarEntry::Type::Action:
|
|
|
|
contextMenu->addAction(item.menu_action);
|
2019-07-25 04:32:30 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return contextMenu;
|
2019-07-23 04:18:14 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 22:09:43 +05:30
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-07-23 04:18:14 +05:30
|
|
|
#include "WideBar.moc"
|