Files
qt-creator/src/plugins/projectexplorer/projecttreewidget.cpp

482 lines
16 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
2008-12-02 16:19:05 +01:00
2008-12-02 12:01:29 +01:00
#include "projecttreewidget.h"
2008-12-09 15:25:01 +01:00
2008-12-02 12:01:29 +01:00
#include "projectexplorer.h"
#include "projectnodes.h"
#include "project.h"
#include "session.h"
2008-12-02 12:01:29 +01:00
#include "projectmodels.h"
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
2008-12-02 12:01:29 +01:00
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/find/itemviewfind.h>
#include <utils/navigationtreeview.h>
#include <utils/algorithm.h>
2008-12-09 15:25:01 +01:00
#include <QDebug>
#include <QSettings>
2008-12-02 12:01:29 +01:00
#include <QStyledItemDelegate>
#include <QVBoxLayout>
#include <QToolButton>
#include <QAction>
#include <QMenu>
2008-12-02 12:01:29 +01:00
using namespace Core;
2008-12-02 12:01:29 +01:00
using namespace ProjectExplorer;
using namespace ProjectExplorer::Internal;
QList<ProjectTreeWidget *> ProjectTreeWidget::m_projectTreeWidgets;
2008-12-02 12:01:29 +01:00
namespace {
class ProjectTreeItemDelegate : public QStyledItemDelegate
{
public:
ProjectTreeItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{ }
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
if (!index.data(Project::EnabledRole).toBool())
opt.state &= ~QStyle::State_Enabled;
QStyledItemDelegate::paint(painter, opt, index);
}
};
bool debug = false;
2008-12-02 12:01:29 +01:00
}
class ProjectTreeView : public Utils::NavigationTreeView
2008-12-02 12:01:29 +01:00
{
public:
ProjectTreeView()
{
setEditTriggers(QAbstractItemView::EditKeyPressed);
setContextMenuPolicy(Qt::CustomContextMenu);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
m_context = new IContext(this);
m_context->setContext(Context(ProjectExplorer::Constants::C_PROJECT_TREE));
m_context->setWidget(this);
ICore::addContextObject(m_context);
}
~ProjectTreeView()
{
ICore::removeContextObject(m_context);
delete m_context;
2008-12-02 12:01:29 +01:00
}
private:
IContext *m_context;
2008-12-02 12:01:29 +01:00
};
/*!
/class ProjectTreeWidget
Shows the projects in form of a tree.
*/
2009-01-20 17:14:00 +01:00
ProjectTreeWidget::ProjectTreeWidget(QWidget *parent)
2008-12-02 12:01:29 +01:00
: QWidget(parent),
m_view(0),
m_model(0),
m_filterProjectsAction(0),
m_autoSync(false),
m_autoExpand(true)
2008-12-02 12:01:29 +01:00
{
m_model = new FlatModel(SessionManager::sessionNode(), this);
Project *pro = SessionManager::startupProject();
if (pro)
m_model->setStartupProject(pro->rootProjectNode());
2008-12-02 12:01:29 +01:00
m_view = new ProjectTreeView;
m_view->setModel(m_model);
m_view->setItemDelegate(new ProjectTreeItemDelegate(this));
2008-12-02 12:01:29 +01:00
setFocusProxy(m_view);
initView();
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(Core::ItemViewFind::createSearchableWrapper(
m_view, ItemViewFind::DarkColored,
ItemViewFind::FetchMoreWhileSearching));
2008-12-02 12:01:29 +01:00
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
2010-09-28 17:29:05 +02:00
m_filterProjectsAction = new QAction(tr("Simplify Tree"), this);
2008-12-02 12:01:29 +01:00
m_filterProjectsAction->setCheckable(true);
m_filterProjectsAction->setChecked(false); // default is the traditional complex tree
connect(m_filterProjectsAction, SIGNAL(toggled(bool)), this, SLOT(setProjectFilter(bool)));
2010-09-28 17:29:05 +02:00
m_filterGeneratedFilesAction = new QAction(tr("Hide Generated Files"), this);
2008-12-02 12:01:29 +01:00
m_filterGeneratedFilesAction->setCheckable(true);
m_filterGeneratedFilesAction->setChecked(true);
connect(m_filterGeneratedFilesAction, SIGNAL(toggled(bool)), this, SLOT(setGeneratedFilesFilter(bool)));
// connections
connect(m_model, SIGNAL(modelReset()),
this, SLOT(initView()));
connect(m_view, SIGNAL(activated(QModelIndex)),
this, SLOT(openItem(QModelIndex)));
connect(m_view->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(handleCurrentItemChange(QModelIndex)));
connect(m_view, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(showContextMenu(QPoint)));
QObject *sessionManager = SessionManager::instance();
connect(sessionManager, SIGNAL(singleProjectAdded(ProjectExplorer::Project*)),
this, SLOT(handleProjectAdded(ProjectExplorer::Project*)));
connect(sessionManager, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
this, SLOT(startupProjectChanged(ProjectExplorer::Project*)));
2008-12-02 12:01:29 +01:00
connect(sessionManager, SIGNAL(aboutToLoadSession(QString)),
this, SLOT(disableAutoExpand()));
connect(sessionManager, SIGNAL(sessionLoaded(QString)),
this, SLOT(loadExpandData()));
connect(sessionManager, SIGNAL(aboutToSaveSession()),
this, SLOT(saveExpandData()));
m_toggleSync = new QToolButton;
m_toggleSync->setIcon(QIcon(QLatin1String(Core::Constants::ICON_LINK)));
m_toggleSync->setCheckable(true);
m_toggleSync->setChecked(autoSynchronization());
m_toggleSync->setToolTip(tr("Synchronize with Editor"));
connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
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()
{
m_autoExpand = false;
}
void ProjectTreeWidget::loadExpandData()
{
m_autoExpand = true;
QStringList data = SessionManager::value(QLatin1String("ProjectTree.ExpandData")).toStringList();
recursiveLoadExpandData(m_view->rootIndex(), data.toSet());
}
void ProjectTreeWidget::recursiveLoadExpandData(const QModelIndex &index, const QSet<QString> &data)
{
if (data.contains(m_model->nodeForIndex(index)->path())) {
m_view->expand(index);
int count = m_model->rowCount(index);
for (int i = 0; i < count; ++i)
recursiveLoadExpandData(index.child(i, 0), data);
}
}
void ProjectTreeWidget::saveExpandData()
{
QStringList data;
recursiveSaveExpandData(m_view->rootIndex(), &data);
// TODO if there are multiple ProjectTreeWidgets, the last one saves the data
SessionManager::setValue(QLatin1String("ProjectTree.ExpandData"), data);
}
void ProjectTreeWidget::recursiveSaveExpandData(const QModelIndex &index, QStringList *data)
{
Q_ASSERT(data);
if (m_view->isExpanded(index)) {
data->append(m_model->nodeForIndex(index)->path());
int count = m_model->rowCount(index);
for (int i = 0; i < count; ++i)
recursiveSaveExpandData(index.child(i, 0), data);
}
}
QToolButton *ProjectTreeWidget::toggleSync()
{
return m_toggleSync;
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::toggleAutoSynchronization()
{
setAutoSynchronization(!m_autoSync);
}
bool ProjectTreeWidget::autoSynchronization() const
{
return m_autoSync;
}
void ProjectTreeWidget::setAutoSynchronization(bool sync, bool syncNow)
{
m_toggleSync->setChecked(sync);
2008-12-02 12:01:29 +01:00
if (sync == m_autoSync)
return;
m_autoSync = sync;
if (debug)
qDebug() << (m_autoSync ? "Enabling auto synchronization" : "Disabling auto synchronization");
if (m_autoSync) {
connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::currentNodeChanged,
this, &ProjectTreeWidget::setCurrentItem);
2008-12-02 12:01:29 +01:00
if (syncNow)
setCurrentItem(ProjectExplorerPlugin::currentNode(), ProjectExplorerPlugin::currentProject());
2008-12-02 12:01:29 +01:00
} else {
disconnect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::currentNodeChanged,
this, &ProjectTreeWidget::setCurrentItem);
2008-12-02 12:01:29 +01:00
}
}
void ProjectTreeWidget::collapseAll()
{
m_view->collapseAll();
}
2008-12-02 12:01:29 +01:00
void ProjectTreeWidget::editCurrentItem()
{
if (m_view->selectionModel()->currentIndex().isValid())
m_view->edit(m_view->selectionModel()->currentIndex());
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::setCurrentItem(Node *node, Project *project)
{
if (debug)
qDebug() << "ProjectTreeWidget::setCurrentItem(" << (project ? project->displayName() : QLatin1String("0"))
<< ", " << (node ? node->path() : QLatin1String("0")) << ")";
if (!project)
2008-12-02 12:01:29 +01:00
return;
const QModelIndex mainIndex = m_model->indexForNode(node);
if (mainIndex.isValid()) {
if (mainIndex != m_view->selectionModel()->currentIndex()) {
m_view->setCurrentIndex(mainIndex);
m_view->scrollTo(mainIndex);
}
} else {
if (debug)
qDebug() << "clear selection";
m_view->clearSelection();
2008-12-02 12:01:29 +01:00
}
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::handleCurrentItemChange(const QModelIndex &current)
{
Node *node = m_model->nodeForIndex(current);
2008-12-17 14:20:01 +01:00
// node might be 0. that's okay
2008-12-02 12:01:29 +01:00
bool autoSync = autoSynchronization();
setAutoSynchronization(false);
ProjectExplorerPlugin::setCurrentNode(node);
2008-12-02 12:01:29 +01:00
setAutoSynchronization(autoSync, false);
}
void ProjectTreeWidget::showContextMenu(const QPoint &pos)
{
QModelIndex index = m_view->indexAt(pos);
Node *node = m_model->nodeForIndex(index);
ProjectExplorerPlugin::showContextMenu(this, m_view->mapToGlobal(pos), node);
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::handleProjectAdded(Project *project)
2008-12-02 12:01:29 +01:00
{
Node *node = project->rootProjectNode();
QModelIndex idx = m_model->indexForNode(node);
if (m_autoExpand) // disabled while session restoring
m_view->setExpanded(idx, true);
m_view->setCurrentIndex(idx);
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::startupProjectChanged(Project *project)
2008-12-02 12:01:29 +01:00
{
if (project) {
ProjectNode *node = project->rootProjectNode();
m_model->setStartupProject(node);
} else {
m_model->setStartupProject(0);
}
}
void ProjectTreeWidget::initView()
{
QModelIndex sessionIndex = m_model->index(0, 0);
// hide root folder
m_view->setRootIndex(sessionIndex);
while (m_model->canFetchMore(sessionIndex))
m_model->fetchMore(sessionIndex);
// expand top level projects
2009-01-20 17:14:00 +01:00
for (int i = 0; i < m_model->rowCount(sessionIndex); ++i)
2008-12-02 12:01:29 +01:00
m_view->expand(m_model->index(i, 0, sessionIndex));
setCurrentItem(ProjectExplorerPlugin::currentNode(), ProjectExplorerPlugin::currentProject());
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::openItem(const QModelIndex &mainIndex)
{
Node *node = m_model->nodeForIndex(mainIndex);
if (node->nodeType() != FileNodeType)
return;
IEditor *editor = EditorManager::openEditor(node->path());
if (editor && node->line() >= 0)
editor->gotoLine(node->line());
2008-12-02 12:01:29 +01:00
}
void ProjectTreeWidget::setProjectFilter(bool filter)
{
m_model->setProjectFilterEnabled(filter);
m_filterProjectsAction->setChecked(filter);
}
void ProjectTreeWidget::setGeneratedFilesFilter(bool filter)
{
m_model->setGeneratedFilesFilterEnabled(filter);
m_filterGeneratedFilesAction->setChecked(filter);
}
bool ProjectTreeWidget::generatedFilesFilter()
{
return m_model->generatedFilesFilterEnabled();
}
bool ProjectTreeWidget::projectFilter()
{
return m_model->projectFilterEnabled();
}
2009-01-20 17:14:00 +01:00
ProjectTreeWidgetFactory::ProjectTreeWidgetFactory()
2008-12-02 12:01:29 +01:00
{
setDisplayName(tr("Projects"));
setPriority(100);
setId("Projects");
setActivationSequence(QKeySequence(UseMacShortcuts ? tr("Meta+X") : tr("Alt+X")));
2008-12-02 12:01:29 +01:00
}
NavigationView ProjectTreeWidgetFactory::createWidget()
2008-12-02 12:01:29 +01:00
{
NavigationView n;
2009-01-20 17:14:00 +01:00
ProjectTreeWidget *ptw = new ProjectTreeWidget;
2008-12-02 12:01:29 +01:00
n.widget = ptw;
QToolButton *filter = new QToolButton;
filter->setIcon(QIcon(QLatin1String(Core::Constants::ICON_FILTER)));
2010-09-28 17:29:05 +02:00
filter->setToolTip(tr("Filter Tree"));
2008-12-02 12:01:29 +01:00
filter->setPopupMode(QToolButton::InstantPopup);
filter->setProperty("noArrow", true);
2008-12-02 12:01:29 +01:00
QMenu *filterMenu = new QMenu(filter);
filterMenu->addAction(ptw->m_filterProjectsAction);
filterMenu->addAction(ptw->m_filterGeneratedFilesAction);
filter->setMenu(filterMenu);
n.dockToolBarWidgets << filter << ptw->toggleSync();
2008-12-02 12:01:29 +01:00
return n;
}
void ProjectTreeWidgetFactory::saveSettings(int position, QWidget *widget)
{
ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
Q_ASSERT(ptw);
QSettings *settings = ICore::settings();
const QString baseKey = QLatin1String("ProjectTreeWidget.") + QString::number(position);
settings->setValue(baseKey + QLatin1String(".ProjectFilter"), ptw->projectFilter());
settings->setValue(baseKey + QLatin1String(".GeneratedFilter"), ptw->generatedFilesFilter());
settings->setValue(baseKey + QLatin1String(".SyncWithEditor"), ptw->autoSynchronization());
}
void ProjectTreeWidgetFactory::restoreSettings(int position, QWidget *widget)
{
ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
Q_ASSERT(ptw);
QSettings *settings = ICore::settings();
const QString baseKey = QLatin1String("ProjectTreeWidget.") + QString::number(position);
ptw->setProjectFilter(settings->value(baseKey + QLatin1String(".ProjectFilter"), false).toBool());
ptw->setGeneratedFilesFilter(settings->value(baseKey + QLatin1String(".GeneratedFilter"), true).toBool());
ptw->setAutoSynchronization(settings->value(baseKey + QLatin1String(".SyncWithEditor"), true).toBool());
}