ProjectTree: Fix saving of tree expansion state in session

Change-Id: I747b36b0f6a63dcf60eebd95cb1acf72e1629c1c
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
Daniel Teske
2014-10-21 11:57:46 +02:00
parent dd93301b73
commit be4a581e8c
2 changed files with 29 additions and 6 deletions

View File

@@ -208,6 +208,15 @@ int ProjectTreeWidget::expandedCount(Node *node)
return count; return count;
} }
void ProjectTreeWidget::rowsInserted(const QModelIndex &parent, int, int)
{
const QString &path = m_model->nodeForIndex(parent)->path();
if (m_toExpand.contains(path)) {
m_view->expand(parent);
m_toExpand.remove(path);
}
}
Node *ProjectTreeWidget::nodeForFile(const QString &fileName, Project *project) Node *ProjectTreeWidget::nodeForFile(const QString &fileName, Project *project)
{ {
Node *bestNode = 0; Node *bestNode = 0;
@@ -240,14 +249,19 @@ void ProjectTreeWidget::disableAutoExpand()
void ProjectTreeWidget::loadExpandData() void ProjectTreeWidget::loadExpandData()
{ {
m_autoExpand = true; m_autoExpand = true;
QStringList data = SessionManager::value(QLatin1String("ProjectTree.ExpandData")).toStringList(); QSet<QString> data = SessionManager::value(QLatin1String("ProjectTree.ExpandData")).toStringList().toSet();
recursiveLoadExpandData(m_view->rootIndex(), data.toSet()); recursiveLoadExpandData(m_view->rootIndex(), data);
// store remaning nodes to expand
m_toExpand = data;
} }
void ProjectTreeWidget::recursiveLoadExpandData(const QModelIndex &index, const QSet<QString> &data) void ProjectTreeWidget::recursiveLoadExpandData(const QModelIndex &index, QSet<QString> &data)
{ {
if (data.contains(m_model->nodeForIndex(index)->path())) { const QString &path = m_model->nodeForIndex(index)->path();
if (data.contains(path)) {
m_view->expand(index); m_view->expand(index);
data.remove(path);
int count = m_model->rowCount(index); int count = m_model->rowCount(index);
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
recursiveLoadExpandData(index.child(i, 0), data); recursiveLoadExpandData(index.child(i, 0), data);
@@ -265,7 +279,9 @@ void ProjectTreeWidget::saveExpandData()
void ProjectTreeWidget::recursiveSaveExpandData(const QModelIndex &index, QStringList *data) void ProjectTreeWidget::recursiveSaveExpandData(const QModelIndex &index, QStringList *data)
{ {
Q_ASSERT(data); Q_ASSERT(data);
if (m_view->isExpanded(index)) { if (m_view->isExpanded(index) || index == m_view->rootIndex()) {
// Note: We store the path of the node, which isn't unique for e.g. .pri files
// but works for most nodes
data->append(m_model->nodeForIndex(index)->path()); data->append(m_model->nodeForIndex(index)->path());
int count = m_model->rowCount(index); int count = m_model->rowCount(index);
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
@@ -368,6 +384,9 @@ void ProjectTreeWidget::handleProjectAdded(Project *project)
if (m_autoExpand) // disabled while session restoring if (m_autoExpand) // disabled while session restoring
m_view->setExpanded(idx, true); m_view->setExpanded(idx, true);
m_view->setCurrentIndex(idx); m_view->setCurrentIndex(idx);
connect(m_model, &FlatModel::rowsInserted,
this, &ProjectTreeWidget::rowsInserted);
} }
void ProjectTreeWidget::startupProjectChanged(Project *project) void ProjectTreeWidget::startupProjectChanged(Project *project)

View File

@@ -35,6 +35,7 @@
#include <QWidget> #include <QWidget>
#include <QModelIndex> #include <QModelIndex>
#include <QSet>
QT_FORWARD_DECLARE_CLASS(QTreeView) QT_FORWARD_DECLARE_CLASS(QTreeView)
@@ -86,9 +87,12 @@ private slots:
void disableAutoExpand(); void disableAutoExpand();
private: private:
void recursiveLoadExpandData(const QModelIndex &index, const QSet<QString> &data); void recursiveLoadExpandData(const QModelIndex &index, QSet<QString> &data);
void recursiveSaveExpandData(const QModelIndex &index, QStringList *data); void recursiveSaveExpandData(const QModelIndex &index, QStringList *data);
static int expandedCount(Node *node); static int expandedCount(Node *node);
void rowsInserted(const QModelIndex &parent, int, int);
QSet<QString> m_toExpand;
QTreeView *m_view; QTreeView *m_view;
FlatModel *m_model; FlatModel *m_model;
QAction *m_filterProjectsAction; QAction *m_filterProjectsAction;