From 7dfe73df0c26733d68abc576c3eefab1a69e2149 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Thu, 25 Jul 2019 01:02:30 +0200 Subject: NOISSUE add context menus to pages with toolbars --- application/widgets/WideBar.cpp | 69 +++++++++++++++++++++++++++++++++++++---- application/widgets/WideBar.h | 8 ++++- 2 files changed, 70 insertions(+), 7 deletions(-) (limited to 'application/widgets') diff --git a/application/widgets/WideBar.cpp b/application/widgets/WideBar.cpp index ee0a67e3..cbd6c617 100644 --- a/application/widgets/WideBar.cpp +++ b/application/widgets/WideBar.cpp @@ -1,5 +1,6 @@ #include "WideBar.h" #include +#include class ActionButton : public QToolButton { @@ -20,6 +21,7 @@ private slots: setIcon(m_action->icon()); setToolTip(m_action->toolTip()); setHidden(!m_action->isVisible()); + setFocusPolicy(Qt::NoFocus); } private: QAction * m_action; @@ -38,22 +40,77 @@ WideBar::WideBar(QWidget* parent) : QToolBar(parent) setMovable(false); } +struct WideBar::BarEntry { + enum Type { + None, + Action, + Separator, + Spacer + } type = None; + QAction *qAction = nullptr; + QAction *wideAction = nullptr; +}; + + +WideBar::~WideBar() +{ + for(auto *iter: m_entries) { + delete iter; + } +} + void WideBar::addAction(QAction* action) { - auto actionButton = new ActionButton(action, this); - auto newAction = addWidget(actionButton); - m_actionMap[action] = newAction; + auto entry = new BarEntry(); + entry->qAction = addWidget(new ActionButton(action, this)); + entry->wideAction = action; + entry->type = BarEntry::Action; + m_entries.push_back(entry); +} + +void WideBar::addSeparator() +{ + auto entry = new BarEntry(); + entry->qAction = QToolBar::addSeparator(); + entry->type = BarEntry::Separator; + m_entries.push_back(entry); } void WideBar::insertSpacer(QAction* action) { - if(!m_actionMap.contains(action)) { + auto iter = std::find_if(m_entries.begin(), m_entries.end(), [action](BarEntry * entry) { + return entry->wideAction == action; + }); + if(iter == m_entries.end()) { return; } - QWidget* spacer = new QWidget(); spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - insertWidget(m_actionMap[action], spacer); + + auto entry = new BarEntry(); + entry->qAction = insertWidget((*iter)->qAction, spacer); + entry->type = BarEntry::Spacer; + m_entries.insert(iter, entry); +} + +QMenu * WideBar::createContextMenu(QWidget *parent, const QString & title) +{ + QMenu *contextMenu = new QMenu(title, parent); + for(auto & item: m_entries) { + switch(item->type) { + default: + case BarEntry::None: + break; + case BarEntry::Separator: + case BarEntry::Spacer: + contextMenu->addSeparator(); + break; + case BarEntry::Action: + contextMenu->addAction(item->wideAction); + break; + } + } + return contextMenu; } #include "WideBar.moc" diff --git a/application/widgets/WideBar.h b/application/widgets/WideBar.h index e5a1a737..d1b8cbe7 100644 --- a/application/widgets/WideBar.h +++ b/application/widgets/WideBar.h @@ -4,6 +4,8 @@ #include #include +class QMenu; + class WideBar : public QToolBar { Q_OBJECT @@ -11,10 +13,14 @@ class WideBar : public QToolBar public: explicit WideBar(const QString &title, QWidget * parent = nullptr); explicit WideBar(QWidget * parent = nullptr); + virtual ~WideBar(); void addAction(QAction *action); + void addSeparator(); void insertSpacer(QAction *action); + QMenu *createContextMenu(QWidget *parent = nullptr, const QString & title = QString()); private: - QMap m_actionMap; + struct BarEntry; + QList m_entries; }; -- cgit