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

VerticalTabs: Save expanded state in session

This commit is contained in:
David Rosca 2018-02-04 12:02:39 +01:00
parent 095e0ff517
commit dbd4e75941
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
2 changed files with 46 additions and 0 deletions

View File

@ -29,6 +29,7 @@
TabTreeView::TabTreeView(QWidget *parent)
: QTreeView(parent)
, m_expandedSessionKey(QSL("VerticalTabs-expanded"))
{
setDragEnabled(true);
setAcceptDrops(true);
@ -50,6 +51,18 @@ TabTreeView::TabTreeView(QWidget *parent)
// Enable hover to force redrawing close button
viewport()->setAttribute(Qt::WA_Hover);
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));
}
int TabTreeView::backgroundIndentation() const
@ -72,6 +85,14 @@ void TabTreeView::setTabsInOrder(bool enable)
m_tabsInOrder = enable;
}
void TabTreeView::setModel(QAbstractItemModel *model)
{
QTreeView::setModel(model);
m_initializing = true;
QTimer::singleShot(0, this, &TabTreeView::initView);
}
void TabTreeView::updateIndex(const QModelIndex &index)
{
QRect rect = visualRect(index);
@ -134,6 +155,10 @@ void TabTreeView::rowsInserted(const QModelIndex &parent, int start, int end)
{
QTreeView::rowsInserted(parent, start, end);
if (m_initializing) {
return;
}
// Parent for WebTab is set after insertTab is emitted
const QPersistentModelIndex index = model()->index(start, 0, parent);
QTimer::singleShot(0, this, [=]() {
@ -277,6 +302,22 @@ bool TabTreeView::viewportEvent(QEvent *event)
return QTreeView::viewportEvent(event);
}
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;
}
TabTreeView::DelegateButton TabTreeView::buttonAt(const QPoint &pos, const QModelIndex &index) const
{
if (m_delegate->expandButtonRect(index).contains(pos)) {

View File

@ -36,6 +36,8 @@ public:
bool areTabsInOrder() const;
void setTabsInOrder(bool enable);
void setModel(QAbstractItemModel *model) override;
void updateIndex(const QModelIndex &index);
void adjustStyleOption(QStyleOptionViewItem *option);
@ -53,6 +55,7 @@ private:
CloseButton
};
void initView();
DelegateButton buttonAt(const QPoint &pos, const QModelIndex &index) const;
TabTreeDelegate *m_delegate;
@ -61,4 +64,6 @@ private:
QPersistentModelIndex m_hoveredIndex;
bool m_tabsInOrder = false;
int m_backgroundIndentation = 0;
QString m_expandedSessionKey;
bool m_initializing = false;
};