summaryrefslogtreecommitdiffstats
path: root/application/widgets
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2019-07-25 01:02:30 +0200
committerPetr Mrázek <peterix@gmail.com>2019-07-25 01:02:30 +0200
commit7dfe73df0c26733d68abc576c3eefab1a69e2149 (patch)
tree0e490b13ad475f0d551f8087851f83869589eb09 /application/widgets
parentc3e61536a3ba7b0dca1171df36e9aeb09b6e2a0d (diff)
downloadMultiMC-7dfe73df0c26733d68abc576c3eefab1a69e2149.tar
MultiMC-7dfe73df0c26733d68abc576c3eefab1a69e2149.tar.gz
MultiMC-7dfe73df0c26733d68abc576c3eefab1a69e2149.tar.lz
MultiMC-7dfe73df0c26733d68abc576c3eefab1a69e2149.tar.xz
MultiMC-7dfe73df0c26733d68abc576c3eefab1a69e2149.zip
NOISSUE add context menus to pages with toolbars
Diffstat (limited to 'application/widgets')
-rw-r--r--application/widgets/WideBar.cpp69
-rw-r--r--application/widgets/WideBar.h8
2 files changed, 70 insertions, 7 deletions
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 <QToolButton>
+#include <QMenu>
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 <QAction>
#include <QMap>
+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<QAction *, QAction *> m_actionMap;
+ struct BarEntry;
+ QList<BarEntry *> m_entries;
};