2018-02-02 11:28:29 +01:00
|
|
|
/* ============================================================
|
|
|
|
* VerticalTabs plugin for Falkon
|
|
|
|
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "tabtreeview.h"
|
|
|
|
#include "tabtreedelegate.h"
|
|
|
|
#include "loadinganimator.h"
|
|
|
|
|
|
|
|
#include "tabmodel.h"
|
|
|
|
#include "webtab.h"
|
|
|
|
#include "tabcontextmenu.h"
|
|
|
|
|
2018-02-02 14:25:54 +01:00
|
|
|
#include <QTimer>
|
2018-02-02 11:28:29 +01:00
|
|
|
#include <QToolTip>
|
|
|
|
#include <QHoverEvent>
|
|
|
|
|
2018-02-05 12:18:55 +01:00
|
|
|
TabTreeView::TabTreeView(BrowserWindow *window, QWidget *parent)
|
2018-02-02 11:28:29 +01:00
|
|
|
: QTreeView(parent)
|
2018-02-05 12:18:55 +01:00
|
|
|
, m_window(window)
|
2018-02-04 12:02:39 +01:00
|
|
|
, m_expandedSessionKey(QSL("VerticalTabs-expanded"))
|
2018-02-02 11:28:29 +01:00
|
|
|
{
|
|
|
|
setDragEnabled(true);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setHeaderHidden(true);
|
|
|
|
setUniformRowHeights(true);
|
|
|
|
setDropIndicatorShown(true);
|
|
|
|
setAllColumnsShowFocus(true);
|
|
|
|
setMouseTracking(true);
|
2018-02-03 17:45:48 +01:00
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2018-02-02 14:02:59 +01:00
|
|
|
setFrameShape(QFrame::NoFrame);
|
2018-02-02 12:00:52 +01:00
|
|
|
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
2018-02-02 11:28:29 +01:00
|
|
|
setIndentation(0);
|
|
|
|
|
|
|
|
m_delegate = new TabTreeDelegate(this);
|
|
|
|
setItemDelegate(m_delegate);
|
2018-02-02 15:18:36 +01:00
|
|
|
|
|
|
|
// Move scrollbar to the left
|
|
|
|
setLayoutDirection(isRightToLeft() ? Qt::LeftToRight : Qt::RightToLeft);
|
2018-02-03 12:46:42 +01:00
|
|
|
|
|
|
|
// Enable hover to force redrawing close button
|
|
|
|
viewport()->setAttribute(Qt::WA_Hover);
|
2018-02-04 12:02:39 +01:00
|
|
|
|
|
|
|
auto saveExpandedState = [this](const QModelIndex &index, bool expanded) {
|
|
|
|
if (m_initializing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WebTab *tab = index.data(TabModel::WebTabRole).value<WebTab*>();
|
|
|
|
if (tab) {
|
|
|
|
tab->setSessionData(m_expandedSessionKey, expanded);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
connect(this, &TabTreeView::expanded, this, std::bind(saveExpandedState, std::placeholders::_1, true));
|
|
|
|
connect(this, &TabTreeView::collapsed, this, std::bind(saveExpandedState, std::placeholders::_1, false));
|
2018-02-02 11:28:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-03 16:55:06 +01:00
|
|
|
int TabTreeView::backgroundIndentation() const
|
|
|
|
{
|
|
|
|
return m_backgroundIndentation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabTreeView::setBackgroundIndentation(int indentation)
|
|
|
|
{
|
|
|
|
m_backgroundIndentation = indentation;
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
bool TabTreeView::areTabsInOrder() const
|
|
|
|
{
|
|
|
|
return m_tabsInOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabTreeView::setTabsInOrder(bool enable)
|
|
|
|
{
|
|
|
|
m_tabsInOrder = enable;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:59:28 +01:00
|
|
|
bool TabTreeView::haveTreeModel() const
|
|
|
|
{
|
|
|
|
return m_haveTreeModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabTreeView::setHaveTreeModel(bool enable)
|
|
|
|
{
|
|
|
|
m_haveTreeModel = enable;
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:02:39 +01:00
|
|
|
void TabTreeView::setModel(QAbstractItemModel *model)
|
|
|
|
{
|
|
|
|
QTreeView::setModel(model);
|
|
|
|
|
|
|
|
m_initializing = true;
|
|
|
|
QTimer::singleShot(0, this, &TabTreeView::initView);
|
|
|
|
}
|
|
|
|
|
2018-02-03 16:35:20 +01:00
|
|
|
void TabTreeView::updateIndex(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
QRect rect = visualRect(index);
|
|
|
|
if (!rect.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Need to update a little above/under to account for negative margins
|
|
|
|
rect.moveTop(rect.y() - rect.height() / 2);
|
|
|
|
rect.setHeight(rect.height() * 2);
|
|
|
|
viewport()->update(rect);
|
|
|
|
}
|
|
|
|
|
2018-02-03 15:33:51 +01:00
|
|
|
void TabTreeView::adjustStyleOption(QStyleOptionViewItem *option)
|
|
|
|
{
|
|
|
|
const QModelIndex index = option->index;
|
|
|
|
|
|
|
|
option->state.setFlag(QStyle::State_Active, true);
|
|
|
|
option->state.setFlag(QStyle::State_HasFocus, false);
|
|
|
|
option->state.setFlag(QStyle::State_Selected, index.data(TabModel::CurrentTabRole).toBool());
|
|
|
|
|
|
|
|
if (!index.isValid()) {
|
|
|
|
option->viewItemPosition = QStyleOptionViewItem::Invalid;
|
|
|
|
} else if (model()->rowCount() == 1) {
|
|
|
|
option->viewItemPosition = QStyleOptionViewItem::OnlyOne;
|
|
|
|
} else {
|
|
|
|
if (!indexAbove(index).isValid()) {
|
|
|
|
option->viewItemPosition = QStyleOptionViewItem::Beginning;
|
|
|
|
} else if (!indexBelow(index).isValid()) {
|
|
|
|
option->viewItemPosition = QStyleOptionViewItem::End;
|
|
|
|
} else {
|
|
|
|
option->viewItemPosition = QStyleOptionViewItem::Middle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
void TabTreeView::drawBranches(QPainter *, const QRect &, const QModelIndex &) const
|
|
|
|
{
|
|
|
|
// Disable drawing branches
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:51:32 +01:00
|
|
|
void TabTreeView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
|
2018-02-02 11:28:29 +01:00
|
|
|
{
|
2018-02-02 11:51:32 +01:00
|
|
|
if (current.data(TabModel::CurrentTabRole).toBool()) {
|
|
|
|
QTreeView::currentChanged(current, previous);
|
|
|
|
} else if (previous.data(TabModel::CurrentTabRole).toBool()) {
|
|
|
|
setCurrentIndex(previous);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
|
|
|
|
{
|
|
|
|
QTreeView::dataChanged(topLeft, bottomRight, roles);
|
|
|
|
|
|
|
|
if (roles.size() == 1 && roles.at(0) == TabModel::CurrentTabRole && topLeft.data(TabModel::CurrentTabRole).toBool()) {
|
|
|
|
setCurrentIndex(topLeft);
|
|
|
|
}
|
2018-02-02 11:28:29 +01:00
|
|
|
}
|
|
|
|
|
2018-02-02 14:25:54 +01:00
|
|
|
void TabTreeView::rowsInserted(const QModelIndex &parent, int start, int end)
|
|
|
|
{
|
|
|
|
QTreeView::rowsInserted(parent, start, end);
|
|
|
|
|
2018-02-04 12:02:39 +01:00
|
|
|
if (m_initializing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:25:54 +01:00
|
|
|
// Parent for WebTab is set after insertTab is emitted
|
|
|
|
const QPersistentModelIndex index = model()->index(start, 0, parent);
|
|
|
|
QTimer::singleShot(0, this, [=]() {
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QModelIndex idx = index;
|
|
|
|
QVector<QModelIndex> stack;
|
|
|
|
do {
|
|
|
|
stack.append(idx);
|
|
|
|
idx = idx.parent();
|
|
|
|
} while (idx.isValid());
|
|
|
|
for (const QModelIndex &index : qAsConst(stack)) {
|
|
|
|
expand(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
bool TabTreeView::viewportEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::MouseButtonPress: {
|
|
|
|
QMouseEvent *me = static_cast<QMouseEvent*>(event);
|
|
|
|
const QModelIndex index = indexAt(me->pos());
|
2018-02-03 16:35:20 +01:00
|
|
|
updateIndex(index);
|
2018-02-02 11:28:29 +01:00
|
|
|
WebTab *tab = index.data(TabModel::WebTabRole).value<WebTab*>();
|
|
|
|
if (me->buttons() == Qt::MiddleButton && tab) {
|
|
|
|
tab->closeTab();
|
|
|
|
}
|
|
|
|
if (me->buttons() != Qt::LeftButton) {
|
|
|
|
m_pressedIndex = QModelIndex();
|
|
|
|
m_pressedButton = NoButton;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_pressedIndex = index;
|
|
|
|
m_pressedButton = buttonAt(me->pos(), m_pressedIndex);
|
|
|
|
if (m_pressedIndex.isValid()) {
|
|
|
|
if (m_pressedButton == ExpandButton) {
|
|
|
|
if (isExpanded(m_pressedIndex)) {
|
|
|
|
collapse(m_pressedIndex);
|
|
|
|
} else {
|
|
|
|
expand(m_pressedIndex);
|
|
|
|
}
|
|
|
|
} else if (m_pressedButton == NoButton && tab) {
|
|
|
|
tab->makeCurrentTab();
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 12:46:42 +01:00
|
|
|
if (m_pressedButton == CloseButton) {
|
|
|
|
me->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::MouseMove: {
|
|
|
|
QMouseEvent *me = static_cast<QMouseEvent*>(event);
|
|
|
|
if (m_pressedButton == CloseButton) {
|
|
|
|
me->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-02 11:28:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::MouseButtonRelease: {
|
|
|
|
QMouseEvent *me = static_cast<QMouseEvent*>(event);
|
|
|
|
if (me->buttons() != Qt::NoButton) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const QModelIndex index = indexAt(me->pos());
|
2018-02-03 16:35:20 +01:00
|
|
|
updateIndex(index);
|
2018-02-02 11:28:29 +01:00
|
|
|
if (m_pressedIndex != index) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DelegateButton button = buttonAt(me->pos(), index);
|
|
|
|
if (m_pressedButton == button) {
|
|
|
|
if (m_pressedButton == ExpandButton) {
|
|
|
|
me->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
WebTab *tab = index.data(TabModel::WebTabRole).value<WebTab*>();
|
|
|
|
if (tab) {
|
|
|
|
if (m_pressedButton == CloseButton) {
|
|
|
|
tab->closeTab();
|
|
|
|
} else if (m_pressedButton == AudioButton) {
|
|
|
|
tab->toggleMuted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-03 12:46:42 +01:00
|
|
|
if (m_pressedButton == CloseButton) {
|
|
|
|
me->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::HoverEnter:
|
|
|
|
case QEvent::HoverLeave:
|
|
|
|
case QEvent::HoverMove: {
|
|
|
|
QHoverEvent *he = static_cast<QHoverEvent*>(event);
|
2018-02-03 16:35:20 +01:00
|
|
|
updateIndex(m_hoveredIndex);
|
2018-02-03 12:46:42 +01:00
|
|
|
m_hoveredIndex = indexAt(he->pos());
|
2018-02-03 16:35:20 +01:00
|
|
|
updateIndex(m_hoveredIndex);
|
2018-02-02 11:28:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::ToolTip: {
|
|
|
|
QHelpEvent *he = static_cast<QHelpEvent*>(event);
|
|
|
|
const QModelIndex index = indexAt(he->pos());
|
|
|
|
DelegateButton button = buttonAt(he->pos(), index);
|
|
|
|
if (button == AudioButton) {
|
|
|
|
const bool muted = index.data(TabModel::AudioMutedRole).toBool();
|
|
|
|
QToolTip::showText(he->globalPos(), muted ? tr("Unmute Tab") : tr("Mute Tab"), this, visualRect(index));
|
|
|
|
he->accept();
|
|
|
|
return true;
|
|
|
|
} else if (button == CloseButton) {
|
|
|
|
QToolTip::showText(he->globalPos(), tr("Close Tab"), this, visualRect(index));
|
|
|
|
he->accept();
|
|
|
|
return true;
|
2018-02-02 16:45:46 +01:00
|
|
|
} else if (button == NoButton) {
|
|
|
|
QToolTip::showText(he->globalPos(), index.data().toString(), this, visualRect(index));
|
|
|
|
he->accept();
|
|
|
|
return true;
|
2018-02-02 11:28:29 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case QEvent::ContextMenu: {
|
|
|
|
QContextMenuEvent *ce = static_cast<QContextMenuEvent*>(event);
|
|
|
|
const QModelIndex index = indexAt(ce->pos());
|
|
|
|
WebTab *tab = index.data(TabModel::WebTabRole).value<WebTab*>();
|
2018-02-05 12:18:55 +01:00
|
|
|
const int tabIndex = tab ? tab->tabIndex() : -1;
|
|
|
|
TabContextMenu::Options options = TabContextMenu::VerticalTabs;
|
|
|
|
if (m_tabsInOrder) {
|
|
|
|
options |= TabContextMenu::ShowCloseOtherTabsActions;
|
2018-02-02 11:28:29 +01:00
|
|
|
}
|
2018-02-05 12:18:55 +01:00
|
|
|
TabContextMenu menu(tabIndex, m_window, options);
|
2018-02-05 15:59:28 +01:00
|
|
|
addMenuActions(&menu, index);
|
2018-02-05 12:18:55 +01:00
|
|
|
menu.exec(ce->globalPos());
|
2018-02-02 11:28:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QTreeView::viewportEvent(event);
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:02:39 +01:00
|
|
|
void TabTreeView::initView()
|
|
|
|
{
|
|
|
|
// Restore expanded state
|
|
|
|
expandAll();
|
|
|
|
QModelIndex index = model()->index(0, 0);
|
|
|
|
while (index.isValid()) {
|
|
|
|
WebTab *tab = index.data(TabModel::WebTabRole).value<WebTab*>();
|
|
|
|
if (tab) {
|
|
|
|
setExpanded(index, tab->sessionData().value(m_expandedSessionKey, true).toBool());
|
|
|
|
}
|
|
|
|
index = indexBelow(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_initializing = false;
|
|
|
|
}
|
|
|
|
|
2018-02-02 11:28:29 +01:00
|
|
|
TabTreeView::DelegateButton TabTreeView::buttonAt(const QPoint &pos, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (m_delegate->expandButtonRect(index).contains(pos)) {
|
|
|
|
return ExpandButton;
|
|
|
|
} else if (m_delegate->audioButtonRect(index).contains(pos)) {
|
|
|
|
return AudioButton;
|
|
|
|
} else if (m_delegate->closeButtonRect(index).contains(pos)) {
|
|
|
|
return CloseButton;
|
|
|
|
}
|
|
|
|
return NoButton;
|
|
|
|
}
|
2018-02-05 15:59:28 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|