forked from qt-creator/qt-creator
ProjectExplorer: Select the current node with more magic
If there are multiple file nodes for the same file, check in how many project views they are expanded and select the one that is expanded in most. Task-number: QTCREATORBUG-12595 Change-Id: Ic4a640a80b8244b30b8dec62248aebeeaf9216cf Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
@@ -1886,7 +1886,7 @@ void ProjectExplorerPlugin::setCurrent(Project *project, QString filePath, Node
|
|||||||
if (node)
|
if (node)
|
||||||
filePath = pathFor(node);
|
filePath = pathFor(node);
|
||||||
else
|
else
|
||||||
node = SessionManager::nodeForFile(filePath, project);
|
node = ProjectTreeWidget::nodeForFile(filePath, project);
|
||||||
|
|
||||||
bool projectChanged = false;
|
bool projectChanged = false;
|
||||||
if (d->m_currentProject != project) {
|
if (d->m_currentProject != project) {
|
||||||
|
|||||||
@@ -43,6 +43,7 @@
|
|||||||
#include <coreplugin/find/treeviewfind.h>
|
#include <coreplugin/find/treeviewfind.h>
|
||||||
|
|
||||||
#include <utils/navigationtreeview.h>
|
#include <utils/navigationtreeview.h>
|
||||||
|
#include <utils/algorithm.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
@@ -57,6 +58,8 @@ using namespace Core;
|
|||||||
using namespace ProjectExplorer;
|
using namespace ProjectExplorer;
|
||||||
using namespace ProjectExplorer::Internal;
|
using namespace ProjectExplorer::Internal;
|
||||||
|
|
||||||
|
QList<ProjectTreeWidget *> ProjectTreeWidget::m_projectTreeWidgets;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class ProjectTreeItemDelegate : public QStyledItemDelegate
|
class ProjectTreeItemDelegate : public QStyledItemDelegate
|
||||||
@@ -178,6 +181,59 @@ ProjectTreeWidget::ProjectTreeWidget(QWidget *parent)
|
|||||||
connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
|
connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
|
||||||
|
|
||||||
setAutoSynchronization(true);
|
setAutoSynchronization(true);
|
||||||
|
|
||||||
|
m_projectTreeWidgets << this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ProjectTreeWidget::~ProjectTreeWidget()
|
||||||
|
{
|
||||||
|
m_projectTreeWidgets.removeOne(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns how many nodes need to be expanded to make node visible
|
||||||
|
int ProjectTreeWidget::expandedCount(Node *node)
|
||||||
|
{
|
||||||
|
if (m_projectTreeWidgets.isEmpty())
|
||||||
|
return 0;
|
||||||
|
FlatModel *model = m_projectTreeWidgets.first()->m_model;
|
||||||
|
QModelIndex index = model->indexForNode(node);
|
||||||
|
if (!index.isValid())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
foreach (ProjectTreeWidget *tree, m_projectTreeWidgets) {
|
||||||
|
QModelIndex idx = index;
|
||||||
|
while (idx.isValid() && idx != tree->m_view->rootIndex()) {
|
||||||
|
if (!tree->m_view->isExpanded(idx))
|
||||||
|
++count;
|
||||||
|
idx = model->parent(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *ProjectTreeWidget::nodeForFile(const QString &fileName, Project *project)
|
||||||
|
{
|
||||||
|
Node *bestNode = 0;
|
||||||
|
int bestNodeExpandCount = INT_MAX;
|
||||||
|
|
||||||
|
foreach (Node *node, SessionManager::nodesForFile(fileName, project)) {
|
||||||
|
if (!bestNode) {
|
||||||
|
bestNode = node;
|
||||||
|
bestNodeExpandCount = ProjectTreeWidget::expandedCount(node->parentFolderNode());
|
||||||
|
} else if (node->nodeType() < bestNode->nodeType()) {
|
||||||
|
bestNode = node;
|
||||||
|
bestNodeExpandCount = ProjectTreeWidget::expandedCount(node->parentFolderNode());
|
||||||
|
} else if (node->nodeType() == bestNode->nodeType()) {
|
||||||
|
int nodeExpandCount = ProjectTreeWidget::expandedCount(node->parentFolderNode());
|
||||||
|
if (nodeExpandCount < bestNodeExpandCount) {
|
||||||
|
bestNode = node;
|
||||||
|
bestNodeExpandCount = ProjectTreeWidget::expandedCount(node->parentFolderNode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectTreeWidget::disableAutoExpand()
|
void ProjectTreeWidget::disableAutoExpand()
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ class ProjectTreeWidget : public QWidget
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit ProjectTreeWidget(QWidget *parent = 0);
|
explicit ProjectTreeWidget(QWidget *parent = 0);
|
||||||
|
~ProjectTreeWidget();
|
||||||
|
|
||||||
bool autoSynchronization() const;
|
bool autoSynchronization() const;
|
||||||
void setAutoSynchronization(bool sync, bool syncNow = true);
|
void setAutoSynchronization(bool sync, bool syncNow = true);
|
||||||
@@ -61,6 +62,8 @@ public:
|
|||||||
bool generatedFilesFilter();
|
bool generatedFilesFilter();
|
||||||
QToolButton *toggleSync();
|
QToolButton *toggleSync();
|
||||||
|
|
||||||
|
static Node *nodeForFile(const QString &fileName, Project *project);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void toggleAutoSynchronization();
|
void toggleAutoSynchronization();
|
||||||
void editCurrentItem();
|
void editCurrentItem();
|
||||||
@@ -88,6 +91,7 @@ private slots:
|
|||||||
private:
|
private:
|
||||||
void recursiveLoadExpandData(const QModelIndex &index, const QSet<QString> &data);
|
void recursiveLoadExpandData(const QModelIndex &index, const QSet<QString> &data);
|
||||||
void recursiveSaveExpandData(const QModelIndex &index, QStringList *data);
|
void recursiveSaveExpandData(const QModelIndex &index, QStringList *data);
|
||||||
|
static int expandedCount(Node *node);
|
||||||
ProjectExplorerPlugin *m_explorer;
|
ProjectExplorerPlugin *m_explorer;
|
||||||
QTreeView *m_view;
|
QTreeView *m_view;
|
||||||
FlatModel *m_model;
|
FlatModel *m_model;
|
||||||
@@ -99,6 +103,8 @@ private:
|
|||||||
QString m_modelId;
|
QString m_modelId;
|
||||||
bool m_autoSync;
|
bool m_autoSync;
|
||||||
bool m_autoExpand;
|
bool m_autoExpand;
|
||||||
|
|
||||||
|
static QList<ProjectTreeWidget *> m_projectTreeWidgets;
|
||||||
friend class ProjectTreeWidgetFactory;
|
friend class ProjectTreeWidgetFactory;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,7 @@
|
|||||||
#include <texteditor/itexteditor.h>
|
#include <texteditor/itexteditor.h>
|
||||||
|
|
||||||
#include <utils/stylehelper.h>
|
#include <utils/stylehelper.h>
|
||||||
|
#include <utils/algorithm.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
@@ -518,23 +519,30 @@ Project *SessionManager::projectForNode(Node *node)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *SessionManager::nodeForFile(const QString &fileName, Project *project)
|
QList<Node *> SessionManager::nodesForFile(const QString &fileName, Project *project)
|
||||||
{
|
{
|
||||||
Node *node = 0;
|
|
||||||
if (!project)
|
if (!project)
|
||||||
project = projectForFile(fileName);
|
project = projectForFile(fileName);
|
||||||
|
|
||||||
if (project) {
|
if (project) {
|
||||||
FindNodesForFileVisitor findNodes(fileName);
|
FindNodesForFileVisitor findNodes(fileName);
|
||||||
project->rootProjectNode()->accept(&findNodes);
|
project->rootProjectNode()->accept(&findNodes);
|
||||||
|
return findNodes.nodes();
|
||||||
foreach (Node *n, findNodes.nodes()) {
|
|
||||||
// prefer file nodes
|
|
||||||
if (!node || (node->nodeType() != FileNodeType && n->nodeType() == FileNodeType))
|
|
||||||
node = n;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return QList<Node *>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// node for file returns a randomly selected node if there are multiple
|
||||||
|
// prefer to use nodesForFile and figure out which node you want
|
||||||
|
Node *SessionManager::nodeForFile(const QString &fileName, Project *project)
|
||||||
|
{
|
||||||
|
Node *node = 0;
|
||||||
|
foreach (Node *n, nodesForFile(fileName, project)) {
|
||||||
|
// prefer file nodes
|
||||||
|
if (!node || (node->nodeType() != FileNodeType && n->nodeType() == FileNodeType))
|
||||||
|
node = n;
|
||||||
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ public:
|
|||||||
static SessionNode *sessionNode();
|
static SessionNode *sessionNode();
|
||||||
|
|
||||||
static Project *projectForNode(ProjectExplorer::Node *node);
|
static Project *projectForNode(ProjectExplorer::Node *node);
|
||||||
|
static QList<Node *> nodesForFile(const QString &fileName, Project *project = 0);
|
||||||
static Node *nodeForFile(const QString &fileName, Project *project = 0);
|
static Node *nodeForFile(const QString &fileName, Project *project = 0);
|
||||||
static Project *projectForFile(const QString &fileName);
|
static Project *projectForFile(const QString &fileName);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user