1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

TabTreeModel: Sync top-level tabs with main tabbar

This way the order of top-level tabs is also preserved in session.
This commit is contained in:
David Rosca 2018-02-02 16:39:27 +01:00
parent a1101c129c
commit e9f3679bb1
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
4 changed files with 50 additions and 0 deletions

View File

@ -19,6 +19,7 @@
#include "tabmodel.h"
#include "webtab.h"
#include <QTimer>
#include <QMimeData>
class TabTreeModelItem
@ -83,6 +84,11 @@ void TabTreeModelItem::addChild(TabTreeModelItem *item, int index)
TabTreeModel::TabTreeModel(QObject *parent)
: QAbstractProxyModel(parent)
{
m_syncTimer = new QTimer(this);
m_syncTimer->setInterval(100);
m_syncTimer->setSingleShot(true);
connect(m_syncTimer, &QTimer::timeout, this, &TabTreeModel::syncTopLevelTabs);
connect(this, &QAbstractProxyModel::sourceModelChanged, this, &TabTreeModel::init);
}
@ -233,6 +239,7 @@ bool TabTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, in
}
m_root->addChild(it, childPos);
endMoveRows();
m_syncTimer->start();
} else {
parentItem->tab->addChildTab(tab, row);
}
@ -383,6 +390,7 @@ void TabTreeModel::connectTab(WebTab *tab)
}
m_root->addChild(item, pos);
endMoveRows();
m_syncTimer->start();
});
connect(tab, &WebTab::childTabAdded, this, [=](WebTab *child, int pos) {
@ -399,5 +407,35 @@ void TabTreeModel::connectTab(WebTab *tab)
}
item->addChild(from, childPos);
endMoveRows();
if (item->parent == m_root) {
m_syncTimer->start();
}
});
}
void TabTreeModel::syncTopLevelTabs()
{
// Move all normal top-level tabs to the beginning to preserve order in session
int index = -1;
const auto items = m_root->children;
for (TabTreeModelItem *item : items) {
if (!item->tab->isPinned()) {
const int tabIndex = item->tab->tabIndex();
if (index < 0 || tabIndex < index) {
index = tabIndex;
}
}
}
if (index < 0) {
return;
}
for (TabTreeModelItem *item : items) {
if (!item->tab->isPinned()) {
item->tab->moveTab(index++);
}
}
}

View File

@ -21,6 +21,8 @@
#include "qzcommon.h"
class QTimer;
class WebTab;
class TabTreeModelItem;
@ -62,7 +64,9 @@ private:
void insertIndex(const QModelIndex &sourceIndex);
void removeIndex(const QModelIndex &sourceIndex);
void connectTab(WebTab *tab);
void syncTopLevelTabs();
TabTreeModelItem *m_root = nullptr;
QHash<WebTab*, TabTreeModelItem*> m_items;
QTimer *m_syncTimer = nullptr;
};

View File

@ -634,6 +634,13 @@ void WebTab::closeTab()
}
}
void WebTab::moveTab(int to)
{
if (m_tabBar) {
m_tabBar->tabWidget()->moveTab(tabIndex(), to);
}
}
int WebTab::tabIndex() const
{
return m_tabBar ? m_tabBar->tabWidget()->indexOf(const_cast<WebTab*>(this)) : -1;

View File

@ -102,6 +102,7 @@ public:
bool isCurrentTab() const;
void makeCurrentTab();
void closeTab();
void moveTab(int to);
bool haveInspector() const;
void showWebInspector(bool inspectElement = false);