Skip to content

Commit fe0bd65

Browse files
committed
Optimize restoring expanded namespaces on reload
1 parent 697af5c commit fe0bd65

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/modules/connections-tree/items/abstractnamespaceitem.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ AbstractNamespaceItem::AbstractNamespaceItem(
2121
m_combinator(nullptr) {}
2222

2323
QList<QSharedPointer<TreeItem>> AbstractNamespaceItem::getAllChilds() const {
24-
return m_childItems;
24+
return m_childItems;
25+
}
26+
27+
QList<QSharedPointer<AbstractNamespaceItem>> AbstractNamespaceItem::getAllChildNamespaces() const
28+
{
29+
return m_childNamespaces.values();
2530
}
2631

2732
QSharedPointer<TreeItem> AbstractNamespaceItem::child(uint row) const {

src/modules/connections-tree/items/abstractnamespaceitem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class AbstractNamespaceItem : public TreeItem, public MemoryUsage {
2424

2525
QList<QSharedPointer<TreeItem>> getAllChilds() const override;
2626

27+
QList<QSharedPointer<AbstractNamespaceItem>> getAllChildNamespaces() const;
28+
2729
uint childCount(bool recursive = false) const override;
2830

2931
QSharedPointer<TreeItem> child(uint row) const override;

src/modules/connections-tree/model.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <QWeakPointer>
55
#include <algorithm>
66
#include "items/serveritem.h"
7+
#include "items/databaseitem.h"
78

89
using namespace ConnectionsTree;
910

@@ -184,11 +185,10 @@ void Model::onItemChildsLoaded(QWeakPointer<TreeItem> item) {
184185
emit expand(index);
185186

186187
QSettings settings;
187-
if (settings.value("app/reopenNamespacesOnReload", true).toBool()) {
188-
restoreOpenedNamespaces(index);
189-
} else {
190-
qDebug() << "Namespace reopening is disabled in settings";
191-
m_expanded.clear();
188+
m_expanded.clear();
189+
190+
if (settings.value("app/reopenNamespacesOnReload", true).toBool()) {
191+
restoreOpenedNamespaces(treeItem.staticCast<AbstractNamespaceItem>());
192192
}
193193
} else if (treeItem->type() == "server" || treeItem->type() == "namespace") {
194194
emit expand(index);
@@ -334,13 +334,17 @@ void Model::removeRootItem(QSharedPointer<ServerItem> item) {
334334
endRemoveRows();
335335
}
336336

337-
void Model::restoreOpenedNamespaces(const QModelIndex &dbIndex) {
338-
m_expanded.clear();
337+
void Model::restoreOpenedNamespaces(QSharedPointer<AbstractNamespaceItem> ns)
338+
{
339+
if (ns->type() == "namespace" && !ns->isExpanded())
340+
return;
341+
342+
if (ns->isExpanded())
343+
emit expand(getIndexFromItem(ns.staticCast<TreeItem>().toWeakRef()));
339344

340-
QModelIndex searchFrom = index(0, 0, dbIndex);
341-
QModelIndexList matches =
342-
match(searchFrom, itemIsInitiallyExpanded, true, -1,
343-
Qt::MatchFixedString | Qt::MatchCaseSensitive | Qt::MatchRecursive);
345+
auto childs = ns->getAllChildNamespaces();
344346

345-
foreach (QModelIndex i, matches) { emit expand(i); }
347+
for (auto childNs : childs) {
348+
restoreOpenedNamespaces(childNs);
349+
}
346350
}

src/modules/connections-tree/model.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace ConnectionsTree {
1212

1313
class ServerItem;
14+
class AbstractNamespaceItem;
1415

1516
class Model : public QAbstractItemModel {
1617
Q_OBJECT
@@ -114,9 +115,9 @@ class Model : public QAbstractItemModel {
114115
protected:
115116
void addRootItem(QSharedPointer<ServerItem> item);
116117

117-
void removeRootItem(QSharedPointer<ServerItem> item);
118+
void removeRootItem(QSharedPointer<ServerItem> item);
118119

119-
void restoreOpenedNamespaces(const QModelIndex &dbIndex);
120+
void restoreOpenedNamespaces(QSharedPointer<AbstractNamespaceItem> ns);
120121

121122
private:
122123
QList<QSharedPointer<TreeItem>> m_treeItems;

0 commit comments

Comments
 (0)