1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

VerticalTabs: Add Tab Tree context menu with tree actions

With Close Tree + Expand/Collapse all actions.
This commit is contained in:
David Rosca 2018-02-05 15:59:28 +01:00
parent 67e82e8923
commit 6aee383a9c
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
3 changed files with 62 additions and 0 deletions

View File

@ -86,6 +86,16 @@ void TabTreeView::setTabsInOrder(bool enable)
m_tabsInOrder = enable; m_tabsInOrder = enable;
} }
bool TabTreeView::haveTreeModel() const
{
return m_haveTreeModel;
}
void TabTreeView::setHaveTreeModel(bool enable)
{
m_haveTreeModel = enable;
}
void TabTreeView::setModel(QAbstractItemModel *model) void TabTreeView::setModel(QAbstractItemModel *model)
{ {
QTreeView::setModel(model); QTreeView::setModel(model);
@ -296,6 +306,7 @@ bool TabTreeView::viewportEvent(QEvent *event)
options |= TabContextMenu::ShowCloseOtherTabsActions; options |= TabContextMenu::ShowCloseOtherTabsActions;
} }
TabContextMenu menu(tabIndex, m_window, options); TabContextMenu menu(tabIndex, m_window, options);
addMenuActions(&menu, index);
menu.exec(ce->globalPos()); menu.exec(ce->globalPos());
break; break;
} }
@ -333,3 +344,44 @@ TabTreeView::DelegateButton TabTreeView::buttonAt(const QPoint &pos, const QMode
} }
return NoButton; return NoButton;
} }
void TabTreeView::addMenuActions(QMenu *menu, const QModelIndex &index) const
{
if (!m_haveTreeModel) {
return;
}
menu->addSeparator();
QMenu *m = menu->addMenu(tr("Tab Tree"));
if (index.isValid() && model()->rowCount(index) > 0) {
QPersistentModelIndex pindex = index;
m->addAction(tr("Close Tree"), this, [=]() {
QVector<WebTab*> tabs;
reverseTraverse(pindex, [&](const QModelIndex &index) {
WebTab *tab = index.data(TabModel::WebTabRole).value<WebTab*>();
if (tab) {
tabs.append(tab);
}
});
for (WebTab *tab : qAsConst(tabs)) {
tab->closeTab();
}
});
}
m->addSeparator();
m->addAction(tr("Expand All"), this, &TabTreeView::expandAll);
m->addAction(tr("Collapse All"), this, &TabTreeView::collapseAll);
}
void TabTreeView::reverseTraverse(const QModelIndex &root, std::function<void(const QModelIndex&)> callback) const
{
if (!root.isValid()) {
return;
}
for (int i = 0; i < model()->rowCount(root); ++i) {
reverseTraverse(model()->index(i, 0, root), callback);
}
callback(root);
}

View File

@ -19,6 +19,8 @@
#include <QTreeView> #include <QTreeView>
class QMenu;
class BrowserWindow; class BrowserWindow;
class TabTreeDelegate; class TabTreeDelegate;
@ -38,6 +40,9 @@ public:
bool areTabsInOrder() const; bool areTabsInOrder() const;
void setTabsInOrder(bool enable); void setTabsInOrder(bool enable);
bool haveTreeModel() const;
void setHaveTreeModel(bool enable);
void setModel(QAbstractItemModel *model) override; void setModel(QAbstractItemModel *model) override;
void updateIndex(const QModelIndex &index); void updateIndex(const QModelIndex &index);
@ -59,6 +64,8 @@ private:
void initView(); void initView();
DelegateButton buttonAt(const QPoint &pos, const QModelIndex &index) const; DelegateButton buttonAt(const QPoint &pos, const QModelIndex &index) const;
void addMenuActions(QMenu *menu, const QModelIndex &index) const;
void reverseTraverse(const QModelIndex &root, std::function<void(const QModelIndex&)> callback) const;
BrowserWindow *m_window; BrowserWindow *m_window;
TabTreeDelegate *m_delegate; TabTreeDelegate *m_delegate;
@ -66,6 +73,7 @@ private:
QPersistentModelIndex m_pressedIndex; QPersistentModelIndex m_pressedIndex;
QPersistentModelIndex m_hoveredIndex; QPersistentModelIndex m_hoveredIndex;
bool m_tabsInOrder = false; bool m_tabsInOrder = false;
bool m_haveTreeModel = false;
int m_backgroundIndentation = 0; int m_backgroundIndentation = 0;
QString m_expandedSessionKey; QString m_expandedSessionKey;
bool m_initializing = false; bool m_initializing = false;

View File

@ -81,6 +81,7 @@ void VerticalTabsWidget::setViewType(VerticalTabsPlugin::ViewType type)
model->setSourceModel(m_window->tabModel()); model->setSourceModel(m_window->tabModel());
m_normalView->setModel(model); m_normalView->setModel(model);
m_normalView->setTabsInOrder(true); m_normalView->setTabsInOrder(true);
m_normalView->setHaveTreeModel(false);
break; break;
case VerticalTabsPlugin::TabTreeView: case VerticalTabsPlugin::TabTreeView:
@ -89,6 +90,7 @@ void VerticalTabsWidget::setViewType(VerticalTabsPlugin::ViewType type)
model->setSourceModel(m_treeModel); model->setSourceModel(m_treeModel);
m_normalView->setModel(model); m_normalView->setModel(model);
m_normalView->setTabsInOrder(false); m_normalView->setTabsInOrder(false);
m_normalView->setHaveTreeModel(true);
break; break;
default: default: