From ea432a7de76ba61dbc65b2e74f4d782be2df3700 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 5 Feb 2018 12:18:55 +0100 Subject: [PATCH] TabContextMenu: Add Options flags --- src/lib/tabwidget/tabbar.cpp | 2 +- src/lib/tabwidget/tabcontextmenu.cpp | 50 +++++++++---------- src/lib/tabwidget/tabcontextmenu.h | 19 +++++-- src/plugins/TabManager/tabmanagerwidget.cpp | 6 ++- src/plugins/VerticalTabs/tablistview.cpp | 10 ++-- src/plugins/VerticalTabs/tablistview.h | 5 +- src/plugins/VerticalTabs/tabtreeview.cpp | 12 +++-- src/plugins/VerticalTabs/tabtreeview.h | 5 +- .../VerticalTabs/verticaltabswidget.cpp | 4 +- 9 files changed, 67 insertions(+), 46 deletions(-) diff --git a/src/lib/tabwidget/tabbar.cpp b/src/lib/tabwidget/tabbar.cpp index 2e8c50b18..162dec4ed 100644 --- a/src/lib/tabwidget/tabbar.cpp +++ b/src/lib/tabwidget/tabbar.cpp @@ -347,7 +347,7 @@ void TabBar::contextMenuEvent(QContextMenuEvent* event) int index = tabAt(event->pos()); - TabContextMenu menu(index, Qt::Horizontal, m_window, m_tabWidget); + TabContextMenu menu(index, m_window); // Prevent choosing first option with double rightclick const QPoint pos = event->globalPos(); diff --git a/src/lib/tabwidget/tabcontextmenu.cpp b/src/lib/tabwidget/tabcontextmenu.cpp index 67ad5c663..0ffb61639 100644 --- a/src/lib/tabwidget/tabcontextmenu.cpp +++ b/src/lib/tabwidget/tabcontextmenu.cpp @@ -27,25 +27,24 @@ #include "checkboxdialog.h" -TabContextMenu::TabContextMenu(int index, Qt::Orientation orientation, BrowserWindow* window, TabWidget* tabWidget, bool showCloseOtherTabs) +TabContextMenu::TabContextMenu(int index, BrowserWindow *window, Options options) : QMenu() , m_clickedTab(index) - , m_tabsOrientation(orientation) , m_window(window) - , m_tabWidget(tabWidget) - , m_showCloseOtherTabs(showCloseOtherTabs) + , m_options(options) { setObjectName("tabcontextmenu"); - connect(this, SIGNAL(tabCloseRequested(int)), m_tabWidget->tabBar(), SIGNAL(tabCloseRequested(int))); - connect(this, SIGNAL(reloadTab(int)), m_tabWidget, SLOT(reloadTab(int))); - connect(this, SIGNAL(stopTab(int)), m_tabWidget, SLOT(stopTab(int))); - connect(this, SIGNAL(closeAllButCurrent(int)), m_tabWidget, SLOT(closeAllButCurrent(int))); - connect(this, SIGNAL(closeToRight(int)), m_tabWidget, SLOT(closeToRight(int))); - connect(this, SIGNAL(closeToLeft(int)), m_tabWidget, SLOT(closeToLeft(int))); - connect(this, SIGNAL(duplicateTab(int)), m_tabWidget, SLOT(duplicateTab(int))); - connect(this, SIGNAL(loadTab(int)), m_tabWidget, SLOT(loadTab(int))); - connect(this, SIGNAL(unloadTab(int)), m_tabWidget, SLOT(unloadTab(int))); + TabWidget *tabWidget = m_window->tabWidget(); + connect(this, SIGNAL(tabCloseRequested(int)), tabWidget->tabBar(), SIGNAL(tabCloseRequested(int))); + connect(this, SIGNAL(reloadTab(int)), tabWidget, SLOT(reloadTab(int))); + connect(this, SIGNAL(stopTab(int)), tabWidget, SLOT(stopTab(int))); + connect(this, SIGNAL(closeAllButCurrent(int)), tabWidget, SLOT(closeAllButCurrent(int))); + connect(this, SIGNAL(closeToRight(int)), tabWidget, SLOT(closeToRight(int))); + connect(this, SIGNAL(closeToLeft(int)), tabWidget, SLOT(closeToLeft(int))); + connect(this, SIGNAL(duplicateTab(int)), tabWidget, SLOT(duplicateTab(int))); + connect(this, SIGNAL(loadTab(int)), tabWidget, SLOT(loadTab(int))); + connect(this, SIGNAL(unloadTab(int)), tabWidget, SLOT(unloadTab(int))); init(); } @@ -84,7 +83,7 @@ void TabContextMenu::closeAllButCurrent() void TabContextMenu::closeToRight() { - const QString label = m_tabsOrientation == Qt::Horizontal + const QString label = m_options & HorizontalTabs ? tr("Do you really want to close all tabs to the right?") : tr("Do you really want to close all tabs to the bottom?"); @@ -95,7 +94,7 @@ void TabContextMenu::closeToRight() void TabContextMenu::closeToLeft() { - const QString label = m_tabsOrientation == Qt::Horizontal + const QString label = m_options & HorizontalTabs ? tr("Do you really want to close all tabs to the left?") : tr("Do you really want to close all tabs to the top?"); @@ -106,8 +105,9 @@ void TabContextMenu::closeToLeft() void TabContextMenu::init() { + TabWidget *tabWidget = m_window->tabWidget(); if (m_clickedTab != -1) { - WebTab* webTab = qobject_cast(m_tabWidget->widget(m_clickedTab)); + WebTab* webTab = tabWidget->webTab(m_clickedTab); if (!webTab) { return; } @@ -131,14 +131,14 @@ void TabContextMenu::init() } addSeparator(); - addAction(tr("Re&load All Tabs"), m_tabWidget, SLOT(reloadAllTabs())); + addAction(tr("Re&load All Tabs"), tabWidget, SLOT(reloadAllTabs())); addAction(tr("Bookmark &All Tabs"), m_window, SLOT(bookmarkAllTabs())); addSeparator(); - if (m_showCloseOtherTabs) { + if (m_options & ShowCloseOtherTabsActions) { addAction(tr("Close Ot&her Tabs"), this, SLOT(closeAllButCurrent())); - addAction(m_tabsOrientation == Qt::Horizontal ? tr("Close Tabs To The Right") : tr("Close Tabs To The Bottom"), this, SLOT(closeToRight())); - addAction(m_tabsOrientation == Qt::Horizontal ? tr("Close Tabs To The Left") : tr("Close Tabs To The Top"), this, SLOT(closeToLeft())); + addAction(m_options & HorizontalTabs ? tr("Close Tabs To The Right") : tr("Close Tabs To The Bottom"), this, SLOT(closeToRight())); + addAction(m_options & HorizontalTabs ? tr("Close Tabs To The Left") : tr("Close Tabs To The Top"), this, SLOT(closeToLeft())); addSeparator(); } @@ -147,19 +147,18 @@ void TabContextMenu::init() } else { addAction(IconProvider::newTabIcon(), tr("&New tab"), m_window, SLOT(addTab())); addSeparator(); - addAction(tr("Reloa&d All Tabs"), m_tabWidget, SLOT(reloadAllTabs())); + addAction(tr("Reloa&d All Tabs"), tabWidget, SLOT(reloadAllTabs())); addAction(tr("Bookmark &All Tabs"), m_window, SLOT(bookmarkAllTabs())); addSeparator(); addAction(m_window->action(QSL("Other/RestoreClosedTab"))); } - m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(m_tabWidget->canRestoreTab()); + m_window->action(QSL("Other/RestoreClosedTab"))->setEnabled(tabWidget->canRestoreTab()); } void TabContextMenu::pinTab() { - WebTab* webTab = qobject_cast(m_tabWidget->widget(m_clickedTab)); - + WebTab* webTab = m_window->tabWidget()->webTab(m_clickedTab); if (webTab) { webTab->togglePinned(); } @@ -167,8 +166,7 @@ void TabContextMenu::pinTab() void TabContextMenu::muteTab() { - WebTab* webTab = qobject_cast(m_tabWidget->widget(m_clickedTab)); - + WebTab* webTab = m_window->tabWidget()->webTab(m_clickedTab); if (webTab) { webTab->toggleMuted(); } diff --git a/src/lib/tabwidget/tabcontextmenu.h b/src/lib/tabwidget/tabcontextmenu.h index 0d162623b..d72a29aab 100644 --- a/src/lib/tabwidget/tabcontextmenu.h +++ b/src/lib/tabwidget/tabcontextmenu.h @@ -28,8 +28,19 @@ class TabWidget; class FALKON_EXPORT TabContextMenu : public QMenu { Q_OBJECT + public: - explicit TabContextMenu(int index, Qt::Orientation orientation, BrowserWindow* window, TabWidget* tabWidget, bool showCloseOtherTabs = true); + enum Option { + InvalidOption = 0, + HorizontalTabs = 1 << 0, + VerticalTabs = 1 << 1, + ShowCloseOtherTabsActions = 1 << 2, + + DefaultOptions = HorizontalTabs | ShowCloseOtherTabsActions + }; + Q_DECLARE_FLAGS(Options, Option) + + explicit TabContextMenu(int index, BrowserWindow *window, Options options = DefaultOptions); signals: void reloadTab(int index); @@ -61,10 +72,8 @@ private: void init(); int m_clickedTab; - Qt::Orientation m_tabsOrientation; - BrowserWindow* m_window; - TabWidget* m_tabWidget; - bool m_showCloseOtherTabs; + BrowserWindow *m_window; + Options m_options = InvalidOption; }; #endif // TABCONTEXTMENU_H diff --git a/src/plugins/TabManager/tabmanagerwidget.cpp b/src/plugins/TabManager/tabmanagerwidget.cpp index 383286194..03c8dadec 100644 --- a/src/plugins/TabManager/tabmanagerwidget.cpp +++ b/src/plugins/TabManager/tabmanagerwidget.cpp @@ -288,7 +288,11 @@ void TabManagerWidget::customContextMenuRequested(const QPoint &pos) // if items are not grouped by Window then actions "Close Other Tabs", // "Close Tabs To The Bottom" and "Close Tabs To The Top" // are ambiguous and should be hidden. - menu = new TabContextMenu(index, Qt::Vertical, mainWindow, mainWindow->tabWidget(), m_groupType == GroupByWindow); + TabContextMenu::Options options = TabContextMenu::VerticalTabs; + if (m_groupType == GroupByWindow) { + options |= TabContextMenu::ShowCloseOtherTabsActions; + } + menu = new TabContextMenu(index, mainWindow, options); menu->addSeparator(); } } diff --git a/src/plugins/VerticalTabs/tablistview.cpp b/src/plugins/VerticalTabs/tablistview.cpp index c5a3920a4..99f2980c7 100644 --- a/src/plugins/VerticalTabs/tablistview.cpp +++ b/src/plugins/VerticalTabs/tablistview.cpp @@ -27,8 +27,9 @@ #include #include -TabListView::TabListView(QWidget *parent) +TabListView::TabListView(BrowserWindow *window, QWidget *parent) : QListView(parent) + , m_window(window) { setDragEnabled(true); setAcceptDrops(true); @@ -206,10 +207,9 @@ bool TabListView::viewportEvent(QEvent *event) QContextMenuEvent *ce = static_cast(event); const QModelIndex index = indexAt(ce->pos()); WebTab *tab = index.data(TabModel::WebTabRole).value(); - if (tab) { - TabContextMenu menu(tab, Qt::Horizontal, false); - menu.exec(ce->globalPos()); - } + const int tabIndex = tab ? tab->tabIndex() : -1; + TabContextMenu menu(tabIndex, m_window, TabContextMenu::HorizontalTabs); + menu.exec(ce->globalPos()); break; } default: diff --git a/src/plugins/VerticalTabs/tablistview.h b/src/plugins/VerticalTabs/tablistview.h index 83ea86d71..2c649628a 100644 --- a/src/plugins/VerticalTabs/tablistview.h +++ b/src/plugins/VerticalTabs/tablistview.h @@ -19,6 +19,8 @@ #include +class BrowserWindow; + class TabListDelegate; class TabListView : public QListView @@ -26,7 +28,7 @@ class TabListView : public QListView Q_OBJECT public: - explicit TabListView(QWidget *parent = nullptr); + explicit TabListView(BrowserWindow *window, QWidget *parent = nullptr); bool isHidingWhenEmpty() const; void setHideWhenEmpty(bool enable); @@ -52,6 +54,7 @@ private: DelegateButton buttonAt(const QPoint &pos, const QModelIndex &index) const; void updateVisibility(); + BrowserWindow *m_window; TabListDelegate *m_delegate; DelegateButton m_pressedButton = NoButton; QModelIndex m_pressedIndex; diff --git a/src/plugins/VerticalTabs/tabtreeview.cpp b/src/plugins/VerticalTabs/tabtreeview.cpp index 79c632de7..258d67c3c 100644 --- a/src/plugins/VerticalTabs/tabtreeview.cpp +++ b/src/plugins/VerticalTabs/tabtreeview.cpp @@ -27,8 +27,9 @@ #include #include -TabTreeView::TabTreeView(QWidget *parent) +TabTreeView::TabTreeView(BrowserWindow *window, QWidget *parent) : QTreeView(parent) + , m_window(window) , m_expandedSessionKey(QSL("VerticalTabs-expanded")) { setDragEnabled(true); @@ -289,10 +290,13 @@ bool TabTreeView::viewportEvent(QEvent *event) QContextMenuEvent *ce = static_cast(event); const QModelIndex index = indexAt(ce->pos()); WebTab *tab = index.data(TabModel::WebTabRole).value(); - if (tab) { - TabContextMenu menu(tab, Qt::Vertical, m_tabsInOrder); - menu.exec(ce->globalPos()); + const int tabIndex = tab ? tab->tabIndex() : -1; + TabContextMenu::Options options = TabContextMenu::VerticalTabs; + if (m_tabsInOrder) { + options |= TabContextMenu::ShowCloseOtherTabsActions; } + TabContextMenu menu(tabIndex, m_window, options); + menu.exec(ce->globalPos()); break; } diff --git a/src/plugins/VerticalTabs/tabtreeview.h b/src/plugins/VerticalTabs/tabtreeview.h index a0ef9a587..b2dba4d61 100644 --- a/src/plugins/VerticalTabs/tabtreeview.h +++ b/src/plugins/VerticalTabs/tabtreeview.h @@ -19,6 +19,8 @@ #include +class BrowserWindow; + class TabTreeDelegate; class TabTreeView : public QTreeView @@ -27,7 +29,7 @@ class TabTreeView : public QTreeView Q_PROPERTY(int backgroundIndentation READ backgroundIndentation WRITE setBackgroundIndentation) public: - explicit TabTreeView(QWidget *parent = nullptr); + explicit TabTreeView(BrowserWindow *window, QWidget *parent = nullptr); int backgroundIndentation() const; void setBackgroundIndentation(int indentation); @@ -58,6 +60,7 @@ private: void initView(); DelegateButton buttonAt(const QPoint &pos, const QModelIndex &index) const; + BrowserWindow *m_window; TabTreeDelegate *m_delegate; DelegateButton m_pressedButton = NoButton; QPersistentModelIndex m_pressedIndex; diff --git a/src/plugins/VerticalTabs/verticaltabswidget.cpp b/src/plugins/VerticalTabs/verticaltabswidget.cpp index d2a82eb47..b9d5f0a78 100644 --- a/src/plugins/VerticalTabs/verticaltabswidget.cpp +++ b/src/plugins/VerticalTabs/verticaltabswidget.cpp @@ -40,7 +40,7 @@ VerticalTabsWidget::VerticalTabsWidget(BrowserWindow *window) layout->setSpacing(0); layout->setContentsMargins(0, 0, 0, 0); - m_pinnedView = new TabListView(this); + m_pinnedView = new TabListView(m_window, this); TabFilterModel *model = new TabFilterModel(m_pinnedView); model->setFilterPinnedTabs(false); model->setRejectDropOnLastIndex(true); @@ -48,7 +48,7 @@ VerticalTabsWidget::VerticalTabsWidget(BrowserWindow *window) m_pinnedView->setModel(model); m_pinnedView->setHideWhenEmpty(true); - m_normalView = new TabTreeView(this); + m_normalView = new TabTreeView(m_window, this); m_pinnedView->setFocusProxy(m_normalView); ToolButton *buttonAddTab = new ToolButton(this);