2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02: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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** 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.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
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"
|
2010-11-01 11:18:27 +01:00
|
|
|
#include "projectnodes.h"
|
2009-09-25 11:35:44 +02:00
|
|
|
#include "project.h"
|
|
|
|
|
#include "session.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "projectmodels.h"
|
2014-11-19 17:58:33 +01:00
|
|
|
#include "projecttree.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-05-23 14:02:36 +02:00
|
|
|
#include <coreplugin/actionmanager/command.h>
|
2015-11-23 16:41:54 +01:00
|
|
|
#include <coreplugin/coreicons.h>
|
2014-11-19 17:58:33 +01:00
|
|
|
#include <coreplugin/documentmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2015-02-26 13:38:54 +01:00
|
|
|
#include <coreplugin/idocument.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2013-01-30 18:13:46 +01:00
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2014-07-31 18:15:19 +02:00
|
|
|
#include <coreplugin/find/itemviewfind.h>
|
2010-11-12 17:25:39 +01:00
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
#include <utils/navigationtreeview.h>
|
2014-07-03 15:46:54 +02:00
|
|
|
#include <utils/algorithm.h>
|
2015-08-20 14:47:02 +02:00
|
|
|
#include <utils/tooltip/tooltip.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QSettings>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-01-16 15:27:03 +01:00
|
|
|
#include <QStyledItemDelegate>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include <QToolButton>
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QMenu>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-01-10 20:18:41 +02:00
|
|
|
using namespace Core;
|
2008-12-02 12:01:29 +01:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace ProjectExplorer::Internal;
|
|
|
|
|
|
2014-07-03 15:46:54 +02:00
|
|
|
QList<ProjectTreeWidget *> ProjectTreeWidget::m_projectTreeWidgets;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace {
|
2013-01-16 15:27:03 +01:00
|
|
|
|
|
|
|
|
class ProjectTreeItemDelegate : public QStyledItemDelegate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ProjectTreeItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
QStyleOptionViewItem opt = option;
|
2014-10-13 22:37:28 +03:00
|
|
|
if (!index.data(Project::EnabledRole).toBool())
|
2013-01-16 15:27:03 +01:00
|
|
|
opt.state &= ~QStyle::State_Enabled;
|
|
|
|
|
QStyledItemDelegate::paint(painter, opt, index);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bool debug = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-07-16 11:18:30 +02:00
|
|
|
class ProjectTreeView : public Utils::NavigationTreeView
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ProjectTreeView()
|
|
|
|
|
{
|
|
|
|
|
setEditTriggers(QAbstractItemView::EditKeyPressed);
|
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2014-09-04 13:37:43 +02:00
|
|
|
setDragEnabled(true);
|
|
|
|
|
setDragDropMode(QAbstractItemView::DragOnly);
|
2013-01-10 20:18:41 +02:00
|
|
|
m_context = new IContext(this);
|
|
|
|
|
m_context->setContext(Context(ProjectExplorer::Constants::C_PROJECT_TREE));
|
2011-04-26 13:18:56 +02:00
|
|
|
m_context->setWidget(this);
|
2015-05-07 15:40:56 +02:00
|
|
|
|
2013-01-10 20:18:41 +02:00
|
|
|
ICore::addContextObject(m_context);
|
2015-05-07 15:40:56 +02:00
|
|
|
|
|
|
|
|
connect(this, &ProjectTreeView::expanded,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
connect(this, &ProjectTreeView::collapsed,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
2010-11-12 17:25:39 +01:00
|
|
|
}
|
2015-05-07 15:40:56 +02:00
|
|
|
|
|
|
|
|
void invalidateSize()
|
|
|
|
|
{
|
|
|
|
|
m_cachedSize = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-20 12:25:04 +02:00
|
|
|
void setModel(QAbstractItemModel *newModel) override
|
2015-05-07 15:40:56 +02:00
|
|
|
{
|
|
|
|
|
// Note: Don't connect to column signals, as we have only one column
|
|
|
|
|
if (model()) {
|
|
|
|
|
QAbstractItemModel *m = model();
|
|
|
|
|
disconnect(m, &QAbstractItemModel::dataChanged,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
disconnect(m, &QAbstractItemModel::layoutChanged,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
disconnect(m, &QAbstractItemModel::modelReset,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
disconnect(m, &QAbstractItemModel::rowsInserted,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
disconnect(m, &QAbstractItemModel::rowsMoved,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
disconnect(m, &QAbstractItemModel::rowsRemoved,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
}
|
|
|
|
|
if (newModel) {
|
|
|
|
|
connect(newModel, &QAbstractItemModel::dataChanged,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
connect(newModel, &QAbstractItemModel::layoutChanged,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
connect(newModel, &QAbstractItemModel::modelReset,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
connect(newModel, &QAbstractItemModel::rowsInserted,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
connect(newModel, &QAbstractItemModel::rowsMoved,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
connect(newModel, &QAbstractItemModel::rowsRemoved,
|
|
|
|
|
this, &ProjectTreeView::invalidateSize);
|
|
|
|
|
}
|
|
|
|
|
Utils::NavigationTreeView::setModel(newModel);
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 17:25:39 +01:00
|
|
|
~ProjectTreeView()
|
|
|
|
|
{
|
2013-01-10 20:18:41 +02:00
|
|
|
ICore::removeContextObject(m_context);
|
2010-11-12 17:25:39 +01:00
|
|
|
delete m_context;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-11-12 17:25:39 +01:00
|
|
|
|
2015-05-07 15:40:56 +02:00
|
|
|
int sizeHintForColumn(int column) const override
|
|
|
|
|
{
|
|
|
|
|
if (m_cachedSize < 0)
|
|
|
|
|
m_cachedSize = Utils::NavigationTreeView::sizeHintForColumn(column);
|
|
|
|
|
|
|
|
|
|
return m_cachedSize;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 17:25:39 +01:00
|
|
|
private:
|
2015-05-07 15:40:56 +02:00
|
|
|
mutable int m_cachedSize = -1;
|
2013-01-10 20:18:41 +02:00
|
|
|
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),
|
2010-01-07 20:35:23 +01:00
|
|
|
m_autoSync(false),
|
2011-07-04 11:16:30 +02:00
|
|
|
m_autoExpand(true)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-09-05 11:46:07 +02:00
|
|
|
m_model = new FlatModel(SessionManager::sessionNode(), this);
|
|
|
|
|
Project *pro = SessionManager::startupProject();
|
2010-07-08 15:37:34 +02:00
|
|
|
if (pro)
|
|
|
|
|
m_model->setStartupProject(pro->rootProjectNode());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
m_view = new ProjectTreeView;
|
|
|
|
|
m_view->setModel(m_model);
|
2013-01-16 15:27:03 +01:00
|
|
|
m_view->setItemDelegate(new ProjectTreeItemDelegate(this));
|
2008-12-02 12:01:29 +01:00
|
|
|
setFocusProxy(m_view);
|
2014-11-19 17:58:33 +01:00
|
|
|
m_view->installEventFilter(this);
|
2008-12-02 12:01:29 +01:00
|
|
|
initView();
|
|
|
|
|
|
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout();
|
2015-02-03 23:59:04 +02:00
|
|
|
layout->addWidget(ItemViewFind::createSearchableWrapper(
|
2014-08-11 17:31:27 +02:00
|
|
|
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()));
|
2014-11-19 17:58:33 +01:00
|
|
|
connect(m_model, &FlatModel::renamed,
|
|
|
|
|
this, &ProjectTreeWidget::renamed);
|
2012-03-05 22:30:59 +01:00
|
|
|
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)));
|
2013-09-05 11:46:07 +02:00
|
|
|
|
|
|
|
|
QObject *sessionManager = SessionManager::instance();
|
|
|
|
|
connect(sessionManager, SIGNAL(singleProjectAdded(ProjectExplorer::Project*)),
|
2012-03-05 22:30:59 +01:00
|
|
|
this, SLOT(handleProjectAdded(ProjectExplorer::Project*)));
|
2013-09-05 11:46:07 +02:00
|
|
|
connect(sessionManager, SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
|
2012-03-05 22:30:59 +01:00
|
|
|
this, SLOT(startupProjectChanged(ProjectExplorer::Project*)));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-09-05 11:46:07 +02:00
|
|
|
connect(sessionManager, SIGNAL(aboutToLoadSession(QString)),
|
2011-03-25 16:48:18 +01:00
|
|
|
this, SLOT(disableAutoExpand()));
|
2013-09-05 11:46:07 +02:00
|
|
|
connect(sessionManager, SIGNAL(sessionLoaded(QString)),
|
2011-03-25 16:48:18 +01:00
|
|
|
this, SLOT(loadExpandData()));
|
2013-09-05 11:46:07 +02:00
|
|
|
connect(sessionManager, SIGNAL(aboutToSaveSession()),
|
2011-03-25 16:48:18 +01:00
|
|
|
this, SLOT(saveExpandData()));
|
|
|
|
|
|
2008-12-11 14:35:14 +01:00
|
|
|
m_toggleSync = new QToolButton;
|
2015-11-23 16:41:54 +01:00
|
|
|
m_toggleSync->setIcon(Core::Icons::LINK.icon());
|
2008-12-11 14:35:14 +01:00
|
|
|
m_toggleSync->setCheckable(true);
|
|
|
|
|
m_toggleSync->setChecked(autoSynchronization());
|
|
|
|
|
m_toggleSync->setToolTip(tr("Synchronize with Editor"));
|
|
|
|
|
connect(m_toggleSync, SIGNAL(clicked(bool)), this, SLOT(toggleAutoSynchronization()));
|
|
|
|
|
|
2009-01-20 17:06:46 +01:00
|
|
|
setAutoSynchronization(true);
|
2014-07-03 15:46:54 +02:00
|
|
|
|
|
|
|
|
m_projectTreeWidgets << this;
|
2014-11-19 17:58:33 +01:00
|
|
|
ProjectTree::registerWidget(this);
|
2014-07-03 15:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProjectTreeWidget::~ProjectTreeWidget()
|
|
|
|
|
{
|
|
|
|
|
m_projectTreeWidgets.removeOne(this);
|
2014-11-19 17:58:33 +01:00
|
|
|
ProjectTree::unregisterWidget(this);
|
2014-07-03 15:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-19 17:58:33 +01:00
|
|
|
void ProjectTreeWidget::rowsInserted(const QModelIndex &parent, int start, int end)
|
2014-10-21 11:57:46 +02:00
|
|
|
{
|
2015-04-22 11:36:51 +02:00
|
|
|
Node *node = m_model->nodeForIndex(parent);
|
2015-10-28 09:16:40 +02:00
|
|
|
QTC_ASSERT(node, return);
|
2015-10-29 17:53:47 +01:00
|
|
|
const QString path = node->filePath().toString();
|
2015-04-22 11:36:51 +02:00
|
|
|
const QString displayName = node->displayName();
|
|
|
|
|
|
|
|
|
|
auto it = m_toExpand.find(ExpandData(path, displayName));
|
|
|
|
|
if (it != m_toExpand.end()) {
|
2014-10-21 11:57:46 +02:00
|
|
|
m_view->expand(parent);
|
2015-04-22 11:36:51 +02:00
|
|
|
m_toExpand.erase(it);
|
2014-10-21 11:57:46 +02:00
|
|
|
}
|
2014-11-19 17:58:33 +01:00
|
|
|
int i = start;
|
|
|
|
|
while (i <= end) {
|
|
|
|
|
QModelIndex idx = m_model->index(i, 0, parent);
|
|
|
|
|
Node *n = m_model->nodeForIndex(idx);
|
2015-10-29 17:53:47 +01:00
|
|
|
if (n && n->filePath() == m_delayedRename) {
|
2014-11-19 17:58:33 +01:00
|
|
|
m_view->setCurrentIndex(idx);
|
|
|
|
|
m_delayedRename.clear();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
2014-10-21 11:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-02 00:37:38 +02:00
|
|
|
Node *ProjectTreeWidget::nodeForFile(const Utils::FileName &fileName)
|
2015-03-03 15:11:01 +01:00
|
|
|
{
|
|
|
|
|
return mostExpandedNode(SessionManager::nodesForFile(fileName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node *ProjectTreeWidget::mostExpandedNode(const QList<Node *> &nodes)
|
2014-07-03 15:46:54 +02:00
|
|
|
{
|
|
|
|
|
Node *bestNode = 0;
|
|
|
|
|
int bestNodeExpandCount = INT_MAX;
|
|
|
|
|
|
2015-03-03 15:11:01 +01:00
|
|
|
foreach (Node *node, nodes) {
|
2014-07-03 15:46:54 +02:00
|
|
|
if (!bestNode) {
|
|
|
|
|
bestNode = node;
|
2014-12-15 18:24:25 +01:00
|
|
|
bestNodeExpandCount = ProjectTreeWidget::expandedCount(node);
|
2014-07-03 15:46:54 +02:00
|
|
|
} else if (node->nodeType() < bestNode->nodeType()) {
|
|
|
|
|
bestNode = node;
|
2014-12-15 18:24:25 +01:00
|
|
|
bestNodeExpandCount = ProjectTreeWidget::expandedCount(node);
|
2014-07-03 15:46:54 +02:00
|
|
|
} else if (node->nodeType() == bestNode->nodeType()) {
|
2014-12-15 18:24:25 +01:00
|
|
|
int nodeExpandCount = ProjectTreeWidget::expandedCount(node);
|
2014-07-03 15:46:54 +02:00
|
|
|
if (nodeExpandCount < bestNodeExpandCount) {
|
|
|
|
|
bestNode = node;
|
2014-12-15 18:24:25 +01:00
|
|
|
bestNodeExpandCount = ProjectTreeWidget::expandedCount(node);
|
2014-07-03 15:46:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return bestNode;
|
2008-12-11 14:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
2011-03-25 16:48:18 +01:00
|
|
|
void ProjectTreeWidget::disableAutoExpand()
|
|
|
|
|
{
|
|
|
|
|
m_autoExpand = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTreeWidget::loadExpandData()
|
|
|
|
|
{
|
|
|
|
|
m_autoExpand = true;
|
2015-04-22 11:36:51 +02:00
|
|
|
QList<QVariant> data = SessionManager::value(QLatin1String("ProjectTree.ExpandData")).value<QList<QVariant>>();
|
|
|
|
|
QSet<ExpandData> set = Utils::transform<QSet>(data, [](const QVariant &v) {
|
|
|
|
|
QStringList list = v.toStringList();
|
|
|
|
|
if (list.size() != 2)
|
|
|
|
|
return ExpandData();
|
|
|
|
|
return ExpandData(list.at(0), list.at(1));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
set.remove(ExpandData());
|
|
|
|
|
|
|
|
|
|
recursiveLoadExpandData(m_view->rootIndex(), set);
|
2014-10-21 11:57:46 +02:00
|
|
|
|
|
|
|
|
// store remaning nodes to expand
|
2015-04-22 11:36:51 +02:00
|
|
|
m_toExpand = set;
|
2011-03-25 16:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-22 11:36:51 +02:00
|
|
|
void ProjectTreeWidget::recursiveLoadExpandData(const QModelIndex &index, QSet<ExpandData> &data)
|
2011-03-25 16:48:18 +01:00
|
|
|
{
|
2015-04-22 11:36:51 +02:00
|
|
|
Node *node = m_model->nodeForIndex(index);
|
2015-10-29 17:53:47 +01:00
|
|
|
const QString path = node->filePath().toString();
|
2015-04-22 11:36:51 +02:00
|
|
|
const QString displayName = node->displayName();
|
|
|
|
|
auto it = data.find(ExpandData(path, displayName));
|
|
|
|
|
if (it != data.end()) {
|
2011-03-25 16:48:18 +01:00
|
|
|
m_view->expand(index);
|
2015-04-22 11:36:51 +02:00
|
|
|
data.erase(it);
|
2011-03-25 16:48:18 +01:00
|
|
|
int count = m_model->rowCount(index);
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
|
recursiveLoadExpandData(index.child(i, 0), data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTreeWidget::saveExpandData()
|
|
|
|
|
{
|
2015-04-22 11:36:51 +02:00
|
|
|
QList<QVariant> data;
|
2011-03-25 16:48:18 +01:00
|
|
|
recursiveSaveExpandData(m_view->rootIndex(), &data);
|
|
|
|
|
// TODO if there are multiple ProjectTreeWidgets, the last one saves the data
|
2013-09-05 11:46:07 +02:00
|
|
|
SessionManager::setValue(QLatin1String("ProjectTree.ExpandData"), data);
|
2011-03-25 16:48:18 +01:00
|
|
|
}
|
|
|
|
|
|
2015-04-22 11:36:51 +02:00
|
|
|
void ProjectTreeWidget::recursiveSaveExpandData(const QModelIndex &index, QList<QVariant> *data)
|
2011-03-25 16:48:18 +01:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(data);
|
2014-10-21 11:57:46 +02:00
|
|
|
if (m_view->isExpanded(index) || index == m_view->rootIndex()) {
|
2015-04-22 11:36:51 +02:00
|
|
|
// Note: We store the path+displayname of the node, which isn't unique for e.g. .pri files
|
2014-10-21 11:57:46 +02:00
|
|
|
// but works for most nodes
|
2015-04-22 11:36:51 +02:00
|
|
|
Node *node = m_model->nodeForIndex(index);
|
2015-10-29 17:53:47 +01:00
|
|
|
const QStringList &list = ExpandData(node->filePath().toString(), node->displayName()).toStringList();
|
2015-04-22 11:36:51 +02:00
|
|
|
data->append(QVariant::fromValue(list));
|
2011-03-25 16:48:18 +01:00
|
|
|
int count = m_model->rowCount(index);
|
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
|
recursiveSaveExpandData(index.child(i, 0), data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-11 14:35:14 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-19 17:58:33 +01:00
|
|
|
void ProjectTreeWidget::setAutoSynchronization(bool sync)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2008-12-11 14:35:14 +01:00
|
|
|
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");
|
2014-11-19 17:58:33 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_autoSync) {
|
2014-11-19 17:58:33 +01:00
|
|
|
// sync from document manager
|
2015-02-02 00:37:38 +02:00
|
|
|
Utils::FileName fileName;
|
2015-01-16 17:45:06 +01:00
|
|
|
if (IDocument *doc = EditorManager::currentDocument())
|
2015-02-02 00:37:38 +02:00
|
|
|
fileName = doc->filePath();
|
2015-10-29 17:53:47 +01:00
|
|
|
if (!currentNode() || currentNode()->filePath() != fileName)
|
2014-11-19 17:58:33 +01:00
|
|
|
setCurrentItem(ProjectTreeWidget::nodeForFile(fileName));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 11:52:14 +02:00
|
|
|
void ProjectTreeWidget::collapseAll()
|
|
|
|
|
{
|
|
|
|
|
m_view->collapseAll();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void ProjectTreeWidget::editCurrentItem()
|
|
|
|
|
{
|
2014-11-19 17:58:33 +01:00
|
|
|
m_delayedRename.clear();
|
2011-06-09 17:43:41 +02:00
|
|
|
if (m_view->selectionModel()->currentIndex().isValid())
|
|
|
|
|
m_view->edit(m_view->selectionModel()->currentIndex());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-25 15:15:41 +01:00
|
|
|
|
2015-02-02 00:37:38 +02:00
|
|
|
void ProjectTreeWidget::renamed(const Utils::FileName &oldPath, const Utils::FileName &newPath)
|
2014-11-19 17:58:33 +01:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(oldPath);
|
2015-10-29 17:53:47 +01:00
|
|
|
if (!currentNode() || currentNode()->filePath() != newPath) {
|
2014-11-19 17:58:33 +01:00
|
|
|
// try to find the node
|
|
|
|
|
Node *node = nodeForFile(newPath);
|
|
|
|
|
if (node)
|
|
|
|
|
m_view->setCurrentIndex(m_model->indexForNode(node));
|
|
|
|
|
else
|
|
|
|
|
m_delayedRename = newPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2014-11-19 17:58:33 +01:00
|
|
|
void ProjectTreeWidget::setCurrentItem(Node *node)
|
|
|
|
|
{
|
2008-12-02 12:01:29 +01:00
|
|
|
const QModelIndex mainIndex = m_model->indexForNode(node);
|
|
|
|
|
|
2013-02-06 23:05:34 +02:00
|
|
|
if (mainIndex.isValid()) {
|
|
|
|
|
if (mainIndex != m_view->selectionModel()->currentIndex()) {
|
|
|
|
|
m_view->setCurrentIndex(mainIndex);
|
|
|
|
|
m_view->scrollTo(mainIndex);
|
|
|
|
|
}
|
2009-01-20 17:06:46 +01:00
|
|
|
} else {
|
|
|
|
|
m_view->clearSelection();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-01-20 17:06:46 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTreeWidget::handleCurrentItemChange(const QModelIndex ¤t)
|
|
|
|
|
{
|
2014-11-19 17:58:33 +01:00
|
|
|
Q_UNUSED(current);
|
|
|
|
|
ProjectTree::nodeChanged(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node *ProjectTreeWidget::currentNode()
|
|
|
|
|
{
|
|
|
|
|
return m_model->nodeForIndex(m_view->currentIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTreeWidget::sync(Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (m_autoSync)
|
|
|
|
|
setCurrentItem(node);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2015-08-20 14:47:02 +02:00
|
|
|
void ProjectTreeWidget::showMessage(Node *node, const QString &message)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex idx = m_model->indexForNode(node);
|
|
|
|
|
m_view->setCurrentIndex(idx);
|
|
|
|
|
m_view->scrollTo(idx);
|
|
|
|
|
|
|
|
|
|
QPoint pos = m_view->mapToGlobal(m_view->visualRect(idx).bottomLeft());
|
|
|
|
|
pos -= Utils::ToolTip::offsetFromPosition();
|
|
|
|
|
Utils::ToolTip::show(pos, message);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
void ProjectTreeWidget::showContextMenu(const QPoint &pos)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex index = m_view->indexAt(pos);
|
|
|
|
|
Node *node = m_model->nodeForIndex(index);
|
2015-01-13 15:50:37 +01:00
|
|
|
ProjectTree::showContextMenu(this, m_view->mapToGlobal(pos), node);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 22:37:28 +03:00
|
|
|
void ProjectTreeWidget::handleProjectAdded(Project *project)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
Node *node = project->rootProjectNode();
|
|
|
|
|
QModelIndex idx = m_model->indexForNode(node);
|
2011-03-25 16:48:18 +01:00
|
|
|
if (m_autoExpand) // disabled while session restoring
|
|
|
|
|
m_view->setExpanded(idx, true);
|
2010-01-07 20:35:23 +01:00
|
|
|
m_view->setCurrentIndex(idx);
|
2014-10-21 11:57:46 +02:00
|
|
|
|
|
|
|
|
connect(m_model, &FlatModel::rowsInserted,
|
|
|
|
|
this, &ProjectTreeWidget::rowsInserted);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 22:37:28 +03: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));
|
|
|
|
|
|
2014-11-19 17:58:33 +01:00
|
|
|
setCurrentItem(ProjectTree::currentNode());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTreeWidget::openItem(const QModelIndex &mainIndex)
|
|
|
|
|
{
|
|
|
|
|
Node *node = m_model->nodeForIndex(mainIndex);
|
2013-01-30 18:13:46 +01:00
|
|
|
if (node->nodeType() != FileNodeType)
|
|
|
|
|
return;
|
2015-10-29 17:53:47 +01:00
|
|
|
IEditor *editor = EditorManager::openEditor(node->filePath().toString());
|
2013-08-06 14:55:38 +02:00
|
|
|
if (editor && node->line() >= 0)
|
2013-01-30 18:13:46 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-11 14:35:14 +01:00
|
|
|
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
|
|
|
{
|
2014-06-24 11:15:32 +02:00
|
|
|
setDisplayName(tr("Projects"));
|
|
|
|
|
setPriority(100);
|
2015-08-20 14:47:02 +02:00
|
|
|
setId(ProjectExplorer::Constants::PROJECTTREE_ID);
|
2014-06-24 11:15:32 +02:00
|
|
|
setActivationSequence(QKeySequence(UseMacShortcuts ? tr("Meta+X") : tr("Alt+X")));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-10 20:18:41 +02:00
|
|
|
NavigationView ProjectTreeWidgetFactory::createWidget()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-01-10 20:18:41 +02: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;
|
2015-11-23 16:41:54 +01:00
|
|
|
filter->setIcon(Core::Icons::FILTER.icon());
|
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);
|
2012-02-01 21:39:05 +01:00
|
|
|
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);
|
|
|
|
|
|
2009-10-09 17:41:09 +02:00
|
|
|
n.dockToolBarWidgets << filter << ptw->toggleSync();
|
2008-12-02 12:01:29 +01:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-11 14:35:14 +01:00
|
|
|
void ProjectTreeWidgetFactory::saveSettings(int position, QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
|
|
|
|
|
Q_ASSERT(ptw);
|
2013-01-10 20:18:41 +02:00
|
|
|
QSettings *settings = ICore::settings();
|
2012-01-09 16:30:33 +01:00
|
|
|
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());
|
2008-12-11 14:35:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProjectTreeWidgetFactory::restoreSettings(int position, QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
ProjectTreeWidget *ptw = qobject_cast<ProjectTreeWidget *>(widget);
|
|
|
|
|
Q_ASSERT(ptw);
|
2013-01-10 20:18:41 +02:00
|
|
|
QSettings *settings = ICore::settings();
|
2012-01-09 16:30:33 +01:00
|
|
|
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());
|
2014-11-19 17:58:33 +01:00
|
|
|
ptw->setAutoSynchronization(settings->value(baseKey + QLatin1String(".SyncWithEditor")).toBool());
|
2008-12-11 14:35:14 +01:00
|
|
|
}
|