2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "projectmodels.h"
|
2008-12-09 15:25:01 +01:00
|
|
|
|
|
|
|
|
#include "project.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "projectexplorerconstants.h"
|
2009-09-29 11:39:55 +02:00
|
|
|
#include "projectnodes.h"
|
2010-05-11 14:13:38 +02:00
|
|
|
#include "projectexplorer.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <coreplugin/fileiconprovider.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
#include <QtGui/QIcon>
|
|
|
|
|
#include <QtGui/QMessageBox>
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <QtGui/QStyle>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
using namespace ProjectExplorer::Internal;
|
|
|
|
|
using Core::FileIconProvider;
|
|
|
|
|
|
|
|
|
|
namespace {
|
2008-12-02 16:19:05 +01:00
|
|
|
|
|
|
|
|
// sorting helper function
|
|
|
|
|
bool sortNodes(Node *n1, Node *n2)
|
|
|
|
|
{
|
|
|
|
|
// Ordering is: project files, project, folder, file
|
|
|
|
|
|
|
|
|
|
const NodeType n1Type = n1->nodeType();
|
|
|
|
|
const NodeType n2Type = n2->nodeType();
|
|
|
|
|
|
|
|
|
|
// project files
|
|
|
|
|
FileNode *file1 = qobject_cast<FileNode*>(n1);
|
|
|
|
|
FileNode *file2 = qobject_cast<FileNode*>(n2);
|
|
|
|
|
if (file1 && file1->fileType() == ProjectFileType) {
|
|
|
|
|
if (file2 && file2->fileType() == ProjectFileType) {
|
|
|
|
|
const QString fileName1 = QFileInfo(file1->path()).fileName();
|
|
|
|
|
const QString fileName2 = QFileInfo(file2->path()).fileName();
|
|
|
|
|
|
|
|
|
|
if (fileName1 != fileName2)
|
2010-04-30 17:01:41 +02:00
|
|
|
return fileName1.compare(fileName2, Qt::CaseInsensitive) < 0;
|
2008-12-02 16:19:05 +01:00
|
|
|
else
|
|
|
|
|
return file1 < file2;
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
2008-12-02 16:19:05 +01:00
|
|
|
return true; // project file is before everything else
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (file2 && file2->fileType() == ProjectFileType) {
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2008-12-02 16:19:05 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 16:19:05 +01:00
|
|
|
// projects
|
|
|
|
|
if (n1Type == ProjectNodeType) {
|
|
|
|
|
if (n2Type == ProjectNodeType) {
|
|
|
|
|
ProjectNode *project1 = static_cast<ProjectNode*>(n1);
|
|
|
|
|
ProjectNode *project2 = static_cast<ProjectNode*>(n2);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-07 18:17:24 +01:00
|
|
|
if (project1->displayName() != project2->displayName())
|
2010-04-30 17:01:41 +02:00
|
|
|
return project1->displayName().compare(project2->displayName(), Qt::CaseInsensitive) < 0; // sort by name
|
2008-12-02 16:19:05 +01:00
|
|
|
else
|
|
|
|
|
return project1 < project2; // sort by pointer value
|
|
|
|
|
} else {
|
|
|
|
|
return true; // project is before folder & file
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (n2Type == ProjectNodeType)
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 16:19:05 +01:00
|
|
|
if (n1Type == FolderNodeType) {
|
|
|
|
|
if (n2Type == FolderNodeType) {
|
|
|
|
|
FolderNode *folder1 = static_cast<FolderNode*>(n1);
|
|
|
|
|
FolderNode *folder2 = static_cast<FolderNode*>(n2);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-08-10 16:27:35 +02:00
|
|
|
if (folder1->path() != folder2->path())
|
|
|
|
|
return folder1->path().compare(folder2->path(), Qt::CaseInsensitive) < 0;
|
2008-12-02 16:19:05 +01:00
|
|
|
else
|
|
|
|
|
return folder1 < folder2;
|
|
|
|
|
} else {
|
|
|
|
|
return true; // folder is before file
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2008-12-02 16:19:05 +01:00
|
|
|
}
|
|
|
|
|
if (n2Type == FolderNodeType)
|
|
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 16:19:05 +01:00
|
|
|
// must be file nodes
|
|
|
|
|
{
|
|
|
|
|
const QString filePath1 = n1->path();
|
|
|
|
|
const QString filePath2 = n2->path();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 16:19:05 +01:00
|
|
|
const QString fileName1 = QFileInfo(filePath1).fileName();
|
|
|
|
|
const QString fileName2 = QFileInfo(filePath2).fileName();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 16:19:05 +01:00
|
|
|
if (fileName1 != fileName2) {
|
2010-04-30 17:01:41 +02:00
|
|
|
return fileName1.compare(fileName2, Qt::CaseInsensitive) < 0; // sort by file names
|
2008-12-02 16:19:05 +01:00
|
|
|
} else {
|
|
|
|
|
if (filePath1 != filePath2) {
|
2010-04-30 17:01:41 +02:00
|
|
|
return filePath1.compare(filePath2, Qt::CaseInsensitive) < 0; // sort by path names
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
2008-12-02 16:19:05 +01:00
|
|
|
return n1 < n2; // sort by pointer value
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-02 16:19:05 +01:00
|
|
|
return false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 16:19:05 +01:00
|
|
|
} // namespace anon
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
FlatModel::FlatModel(SessionNode *rootNode, QObject *parent)
|
|
|
|
|
: QAbstractItemModel(parent),
|
2008-12-10 17:31:19 +01:00
|
|
|
m_filterProjects(false),
|
2008-12-02 12:01:29 +01:00
|
|
|
m_filterGeneratedFiles(true),
|
|
|
|
|
m_rootNode(rootNode),
|
|
|
|
|
m_startupProject(0),
|
|
|
|
|
m_parentFolderForChange(0)
|
|
|
|
|
{
|
|
|
|
|
NodesWatcher *watcher = new NodesWatcher(this);
|
|
|
|
|
m_rootNode->registerWatcher(watcher);
|
|
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(foldersAboutToBeAdded(FolderNode *, const QList<FolderNode*> &)),
|
|
|
|
|
this, SLOT(foldersAboutToBeAdded(FolderNode *, const QList<FolderNode*> &)));
|
|
|
|
|
connect(watcher, SIGNAL(foldersAdded()),
|
|
|
|
|
this, SLOT(foldersAdded()));
|
|
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(foldersAboutToBeRemoved(FolderNode *, const QList<FolderNode*> &)),
|
|
|
|
|
this, SLOT(foldersAboutToBeRemoved(FolderNode *, const QList<FolderNode*> &)));
|
|
|
|
|
connect(watcher, SIGNAL(foldersRemoved()),
|
|
|
|
|
this, SLOT(foldersRemoved()));
|
|
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(filesAboutToBeAdded(FolderNode *,const QList<FileNode*> &)),
|
|
|
|
|
this, SLOT(filesAboutToBeAdded(FolderNode *, const QList<FileNode *> &)));
|
|
|
|
|
connect(watcher, SIGNAL(filesAdded()),
|
|
|
|
|
this, SLOT(filesAdded()));
|
|
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(filesAboutToBeRemoved(FolderNode *, const QList<FileNode*> &)),
|
|
|
|
|
this, SLOT(filesAboutToBeRemoved(FolderNode *, const QList<FileNode*> &)));
|
|
|
|
|
connect(watcher, SIGNAL(filesRemoved()),
|
|
|
|
|
this, SLOT(filesRemoved()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FlatModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
QModelIndex result;
|
|
|
|
|
if (!parent.isValid() && row == 0 && column == 0) { // session
|
|
|
|
|
result = createIndex(0, 0, m_rootNode);
|
|
|
|
|
} else if (parent.isValid() && column == 0) {
|
|
|
|
|
FolderNode *parentNode = qobject_cast<FolderNode*>(nodeForIndex(parent));
|
2008-12-17 15:51:48 +01:00
|
|
|
Q_ASSERT(parentNode);
|
2008-12-02 12:01:29 +01:00
|
|
|
QHash<FolderNode*, QList<Node*> >::const_iterator it = m_childNodes.constFind(parentNode);
|
|
|
|
|
if (it == m_childNodes.constEnd()) {
|
|
|
|
|
fetchMore(parentNode);
|
|
|
|
|
it = m_childNodes.constFind(parentNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (row < it.value().size())
|
|
|
|
|
result = createIndex(row, 0, it.value().at(row));
|
|
|
|
|
}
|
|
|
|
|
// qDebug() << "index of " << row << column << parent.data(Project::FilePathRole) << " is " << result.data(Project::FilePathRole);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FlatModel::parent(const QModelIndex &idx) const
|
|
|
|
|
{
|
|
|
|
|
QModelIndex parentIndex;
|
|
|
|
|
if (Node *node = nodeForIndex(idx)) {
|
|
|
|
|
FolderNode *parentNode = visibleFolderNode(node->parentFolderNode());
|
|
|
|
|
if (parentNode) {
|
|
|
|
|
FolderNode *grandParentNode = visibleFolderNode(parentNode->parentFolderNode());
|
|
|
|
|
if (grandParentNode) {
|
|
|
|
|
QHash<FolderNode*, QList<Node*> >::const_iterator it = m_childNodes.constFind(grandParentNode);
|
|
|
|
|
if (it == m_childNodes.constEnd()) {
|
|
|
|
|
fetchMore(grandParentNode);
|
|
|
|
|
it = m_childNodes.constFind(grandParentNode);
|
|
|
|
|
}
|
2008-12-17 15:51:48 +01:00
|
|
|
Q_ASSERT(it != m_childNodes.constEnd());
|
2008-12-02 12:01:29 +01:00
|
|
|
const int row = it.value().indexOf(parentNode);
|
2008-12-17 15:51:48 +01:00
|
|
|
Q_ASSERT(row >= 0);
|
2008-12-02 12:01:29 +01:00
|
|
|
parentIndex = createIndex(row, 0, parentNode);
|
|
|
|
|
} else {
|
|
|
|
|
// top level node, parent is session
|
|
|
|
|
parentIndex = index(0, 0, QModelIndex());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// qDebug() << "parent of " << idx.data(Project::FilePathRole) << " is " << parentIndex.data(Project::FilePathRole);
|
|
|
|
|
|
|
|
|
|
return parentIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant FlatModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
QVariant result;
|
|
|
|
|
|
|
|
|
|
if (Node *node = nodeForIndex(index)) {
|
|
|
|
|
FolderNode *folderNode = qobject_cast<FolderNode*>(node);
|
|
|
|
|
switch (role) {
|
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
|
case Qt::EditRole: {
|
|
|
|
|
if (folderNode)
|
2010-01-07 18:17:24 +01:00
|
|
|
result = folderNode->displayName();
|
2008-12-02 12:01:29 +01:00
|
|
|
else
|
|
|
|
|
result = QFileInfo(node->path()).fileName(); //TODO cache that?
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Qt::ToolTipRole: {
|
2009-04-15 17:32:53 +02:00
|
|
|
result = QDir::toNativeSeparators(node->path());
|
2008-12-02 12:01:29 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Qt::DecorationRole: {
|
|
|
|
|
if (folderNode)
|
|
|
|
|
result = folderNode->icon();
|
|
|
|
|
else
|
|
|
|
|
result = FileIconProvider::instance()->icon(QFileInfo(node->path()));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Qt::FontRole: {
|
|
|
|
|
QFont font;
|
|
|
|
|
if (node == m_startupProject)
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
result = font;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ProjectExplorer::Project::FilePathRole: {
|
|
|
|
|
result = node->path();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-11 14:13:38 +02:00
|
|
|
Qt::ItemFlags FlatModel::flags(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return 0;
|
|
|
|
|
// We claim that everything is editable
|
|
|
|
|
// That's slightly wrong
|
|
|
|
|
// We control the only view, and that one does the checks
|
|
|
|
|
return Qt::ItemIsSelectable|Qt::ItemIsEnabled | Qt::ItemIsEditable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlatModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid())
|
|
|
|
|
return false;
|
|
|
|
|
if (role != Qt::EditRole)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ProjectExplorerPlugin::instance()->renameFile(nodeForIndex(index), value.toString());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
int FlatModel::rowCount(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
int rows = 0;
|
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
|
rows = 1;
|
|
|
|
|
} else {
|
|
|
|
|
FolderNode *folderNode = qobject_cast<FolderNode*>(nodeForIndex(parent));
|
|
|
|
|
if (folderNode && m_childNodes.contains(folderNode))
|
|
|
|
|
rows = m_childNodes.value(folderNode).size();
|
|
|
|
|
}
|
|
|
|
|
return rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FlatModel::columnCount(const QModelIndex &/*parent*/) const
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlatModel::hasChildren(const QModelIndex &parent) const
|
|
|
|
|
{
|
|
|
|
|
if (!parent.isValid())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
FolderNode *folderNode = qobject_cast<FolderNode*>(nodeForIndex(parent));
|
|
|
|
|
if (!folderNode)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QHash<FolderNode*, QList<Node*> >::const_iterator it = m_childNodes.constFind(folderNode);
|
|
|
|
|
if (it == m_childNodes.constEnd()) {
|
|
|
|
|
fetchMore(folderNode);
|
|
|
|
|
it = m_childNodes.constFind(folderNode);
|
|
|
|
|
}
|
|
|
|
|
return !it.value().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlatModel::canFetchMore(const QModelIndex & parent) const
|
|
|
|
|
{
|
|
|
|
|
if (!parent.isValid()) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
if (FolderNode *folderNode = qobject_cast<FolderNode*>(nodeForIndex(parent)))
|
|
|
|
|
return !m_childNodes.contains(folderNode);
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::recursiveAddFolderNodes(FolderNode *startNode, QList<Node *> *list, const QSet<Node *> &blackList) const
|
|
|
|
|
{
|
|
|
|
|
foreach (FolderNode *folderNode, startNode->subFolderNodes()) {
|
|
|
|
|
if (folderNode && !blackList.contains(folderNode))
|
|
|
|
|
recursiveAddFolderNodesImpl(folderNode, list, blackList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::recursiveAddFolderNodesImpl(FolderNode *startNode, QList<Node *> *list, const QSet<Node *> &blackList) const
|
|
|
|
|
{
|
|
|
|
|
if (!filter(startNode)) {
|
|
|
|
|
if (!blackList.contains(startNode))
|
|
|
|
|
list->append(startNode);
|
|
|
|
|
} else {
|
|
|
|
|
foreach (FolderNode *folderNode, startNode->subFolderNodes()) {
|
|
|
|
|
if (folderNode && !blackList.contains(folderNode))
|
|
|
|
|
recursiveAddFolderNodesImpl(folderNode, list, blackList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::recursiveAddFileNodes(FolderNode *startNode, QList<Node *> *list, const QSet<Node *> &blackList) const
|
|
|
|
|
{
|
|
|
|
|
foreach (FolderNode *subFolderNode, startNode->subFolderNodes()) {
|
|
|
|
|
if (!blackList.contains(subFolderNode))
|
|
|
|
|
recursiveAddFileNodes(subFolderNode, list, blackList);
|
|
|
|
|
}
|
|
|
|
|
foreach (Node *node, startNode->fileNodes()) {
|
|
|
|
|
if (!blackList.contains(node) && !filter(node))
|
|
|
|
|
list->append(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<Node*> FlatModel::childNodes(FolderNode *parentNode, const QSet<Node*> &blackList) const
|
|
|
|
|
{
|
|
|
|
|
QList<Node*> nodeList;
|
|
|
|
|
|
|
|
|
|
if (parentNode->nodeType() == SessionNodeType) {
|
|
|
|
|
SessionNode *sessionNode = static_cast<SessionNode*>(parentNode);
|
|
|
|
|
QList<ProjectNode*> projectList = sessionNode->projectNodes();
|
|
|
|
|
for (int i = 0; i < projectList.size(); ++i) {
|
|
|
|
|
if (!blackList.contains(projectList.at(i)))
|
|
|
|
|
nodeList << projectList.at(i);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
recursiveAddFolderNodes(parentNode, &nodeList, blackList);
|
|
|
|
|
recursiveAddFileNodes(parentNode, &nodeList, blackList + nodeList.toSet());
|
|
|
|
|
}
|
|
|
|
|
qSort(nodeList.begin(), nodeList.end(), sortNodes);
|
|
|
|
|
return nodeList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::fetchMore(FolderNode *folderNode) const
|
|
|
|
|
{
|
2008-12-17 15:51:48 +01:00
|
|
|
Q_ASSERT(folderNode);
|
|
|
|
|
Q_ASSERT(!m_childNodes.contains(folderNode));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
QList<Node*> nodeList = childNodes(folderNode);
|
|
|
|
|
m_childNodes.insert(folderNode, nodeList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::fetchMore(const QModelIndex &parent)
|
|
|
|
|
{
|
|
|
|
|
FolderNode *folderNode = qobject_cast<FolderNode*>(nodeForIndex(parent));
|
2008-12-17 15:51:48 +01:00
|
|
|
Q_ASSERT(folderNode);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
fetchMore(folderNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::setStartupProject(ProjectNode *projectNode)
|
|
|
|
|
{
|
|
|
|
|
if (m_startupProject != projectNode) {
|
|
|
|
|
QModelIndex oldIndex = (m_startupProject ? indexForNode(m_startupProject) : QModelIndex());
|
|
|
|
|
QModelIndex newIndex = (projectNode ? indexForNode(projectNode) : QModelIndex());
|
|
|
|
|
m_startupProject = projectNode;
|
|
|
|
|
if (oldIndex.isValid())
|
|
|
|
|
emit dataChanged(oldIndex, oldIndex);
|
|
|
|
|
if (newIndex.isValid())
|
|
|
|
|
emit dataChanged(newIndex, newIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::reset()
|
|
|
|
|
{
|
|
|
|
|
m_childNodes.clear();
|
|
|
|
|
QAbstractItemModel::reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex FlatModel::indexForNode(const Node *node_)
|
|
|
|
|
{
|
|
|
|
|
// We assume that we are only called for nodes that are represented
|
|
|
|
|
|
|
|
|
|
// we use non-const pointers internally
|
|
|
|
|
Node *node = const_cast<Node*>(node_);
|
|
|
|
|
if (!node)
|
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
|
|
if (node == m_rootNode)
|
|
|
|
|
return createIndex(0, 0, m_rootNode);
|
|
|
|
|
|
|
|
|
|
FolderNode *parentNode = visibleFolderNode(node->parentFolderNode());
|
|
|
|
|
|
|
|
|
|
// Do we have the parent mapped?
|
|
|
|
|
QHash<FolderNode*, QList<Node*> >::const_iterator it = m_childNodes.constFind(parentNode);
|
|
|
|
|
if (it == m_childNodes.constEnd()) {
|
|
|
|
|
fetchMore(parentNode);
|
|
|
|
|
it = m_childNodes.constFind(parentNode);
|
|
|
|
|
}
|
|
|
|
|
if (it != m_childNodes.constEnd()) {
|
|
|
|
|
const int row = it.value().indexOf(node);
|
|
|
|
|
if (row != -1)
|
|
|
|
|
return createIndex(row, 0, node);
|
|
|
|
|
}
|
|
|
|
|
return QModelIndex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::setProjectFilterEnabled(bool filter)
|
|
|
|
|
{
|
2008-12-10 17:31:19 +01:00
|
|
|
if (filter == m_filterProjects)
|
|
|
|
|
return;
|
2008-12-02 12:01:29 +01:00
|
|
|
m_filterProjects = filter;
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::setGeneratedFilesFilterEnabled(bool filter)
|
|
|
|
|
{
|
|
|
|
|
m_filterGeneratedFiles = filter;
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-11 14:35:14 +01:00
|
|
|
bool FlatModel::projectFilterEnabled()
|
|
|
|
|
{
|
|
|
|
|
return m_filterProjects;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlatModel::generatedFilesFilterEnabled()
|
|
|
|
|
{
|
|
|
|
|
return m_filterGeneratedFiles;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Node *FlatModel::nodeForIndex(const QModelIndex &index) const
|
|
|
|
|
{
|
|
|
|
|
if (index.isValid())
|
|
|
|
|
return (Node*)index.internalPointer();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Returns the first folder node in the ancestors
|
|
|
|
|
for the given node that is not filtered
|
|
|
|
|
out by the Flat Model.
|
|
|
|
|
*/
|
|
|
|
|
FolderNode *FlatModel::visibleFolderNode(FolderNode *node) const
|
|
|
|
|
{
|
|
|
|
|
if (!node)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (FolderNode *folderNode = node;
|
|
|
|
|
folderNode;
|
|
|
|
|
folderNode = folderNode->parentFolderNode()) {
|
|
|
|
|
if (!filter(folderNode))
|
|
|
|
|
return folderNode;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FlatModel::filter(Node *node) const
|
|
|
|
|
{
|
|
|
|
|
bool isHidden = false;
|
2009-08-17 17:59:57 +02:00
|
|
|
if (node->nodeType() == SessionNodeType) {
|
|
|
|
|
isHidden = false;
|
|
|
|
|
} else if (ProjectNode *projectNode = qobject_cast<ProjectNode*>(node)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_filterProjects && projectNode->parentFolderNode() != m_rootNode)
|
2010-02-02 11:59:24 +01:00
|
|
|
isHidden = !projectNode->hasBuildTargets();
|
2009-08-17 17:59:57 +02:00
|
|
|
} else if (node->nodeType() == FolderNodeType) {
|
|
|
|
|
if (m_filterProjects)
|
|
|
|
|
isHidden = true;
|
|
|
|
|
} else if (FileNode *fileNode = qobject_cast<FileNode*>(node)) {
|
2008-12-02 12:01:29 +01:00
|
|
|
if (m_filterGeneratedFiles)
|
|
|
|
|
isHidden = fileNode->isGenerated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isHidden;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// slots and all the fun
|
|
|
|
|
void FlatModel::added(FolderNode* parentNode, const QList<Node*> &newNodeList)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex parentIndex = indexForNode(parentNode);
|
|
|
|
|
// Old list
|
|
|
|
|
QHash<FolderNode*, QList<Node*> >::const_iterator it = m_childNodes.constFind(parentNode);
|
|
|
|
|
if (it == m_childNodes.constEnd())
|
|
|
|
|
return;
|
|
|
|
|
QList<Node *> oldNodeList = it.value();
|
|
|
|
|
|
|
|
|
|
// Compare lists and emit signals, and modify m_childNodes on the fly
|
|
|
|
|
QList<Node *>::const_iterator oldIter = oldNodeList.constBegin();
|
|
|
|
|
QList<Node *>::const_iterator newIter = newNodeList.constBegin();
|
|
|
|
|
|
|
|
|
|
// optimization, check for old list is empty
|
|
|
|
|
if (oldIter == oldNodeList.constEnd()) {
|
|
|
|
|
// New Node List is empty, nothing added which intrest us
|
|
|
|
|
if (newIter == newNodeList.constEnd())
|
|
|
|
|
return;
|
|
|
|
|
// So all we need to do is easy
|
|
|
|
|
beginInsertRows(parentIndex, 0, newNodeList.size() - 1);
|
|
|
|
|
m_childNodes.insert(parentNode, newNodeList);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-09 11:07:24 +01:00
|
|
|
while (true) {
|
2008-12-02 12:01:29 +01:00
|
|
|
// Skip all that are the same
|
2008-12-09 11:07:24 +01:00
|
|
|
while (*oldIter == *newIter) {
|
2008-12-02 12:01:29 +01:00
|
|
|
++oldIter;
|
|
|
|
|
++newIter;
|
|
|
|
|
if (oldIter == oldNodeList.constEnd()) {
|
|
|
|
|
// At end of oldNodeList, sweep up rest of newNodeList
|
|
|
|
|
QList<Node *>::const_iterator startOfBlock = newIter;
|
|
|
|
|
newIter = newNodeList.constEnd();
|
|
|
|
|
int pos = oldIter - oldNodeList.constBegin();
|
|
|
|
|
int count = newIter - startOfBlock;
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
beginInsertRows(parentIndex, pos, pos+count-1);
|
2008-12-09 11:07:24 +01:00
|
|
|
while (startOfBlock != newIter) {
|
2008-12-02 12:01:29 +01:00
|
|
|
oldNodeList.insert(pos, *startOfBlock);
|
|
|
|
|
++pos;
|
|
|
|
|
++startOfBlock;
|
|
|
|
|
}
|
|
|
|
|
m_childNodes.insert(parentNode, oldNodeList);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
}
|
|
|
|
|
return; // Done with the lists, leave the function
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<Node *>::const_iterator startOfBlock = newIter;
|
2008-12-09 11:07:24 +01:00
|
|
|
while (*oldIter != *newIter)
|
2008-12-02 12:01:29 +01:00
|
|
|
++newIter;
|
|
|
|
|
// startOfBlock is the first that was diffrent
|
|
|
|
|
// newIter points to the new position of oldIter
|
|
|
|
|
// newIter - startOfBlock is number of new items
|
|
|
|
|
// oldIter is the position where those are...
|
|
|
|
|
int pos = oldIter - oldNodeList.constBegin();
|
|
|
|
|
int count = newIter - startOfBlock;
|
|
|
|
|
beginInsertRows(parentIndex, pos, pos + count - 1);
|
2008-12-09 11:07:24 +01:00
|
|
|
while (startOfBlock != newIter) {
|
2008-12-02 12:01:29 +01:00
|
|
|
oldNodeList.insert(pos, *startOfBlock);
|
|
|
|
|
++pos;
|
|
|
|
|
++startOfBlock;
|
|
|
|
|
}
|
|
|
|
|
m_childNodes.insert(parentNode, oldNodeList);
|
|
|
|
|
endInsertRows();
|
|
|
|
|
oldIter = oldNodeList.constBegin() + pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::removed(FolderNode* parentNode, const QList<Node*> &newNodeList)
|
|
|
|
|
{
|
|
|
|
|
QModelIndex parentIndex = indexForNode(parentNode);
|
|
|
|
|
// Old list
|
|
|
|
|
QHash<FolderNode*, QList<Node*> >::const_iterator it = m_childNodes.constFind(parentNode);
|
|
|
|
|
if (it == m_childNodes.constEnd())
|
|
|
|
|
return;
|
|
|
|
|
QList<Node *> oldNodeList = it.value();
|
|
|
|
|
// Compare lists and emit signals, and modify m_childNodes on the fly
|
|
|
|
|
QList<Node *>::const_iterator oldIter = oldNodeList.constBegin();
|
|
|
|
|
QList<Node *>::const_iterator newIter = newNodeList.constBegin();
|
|
|
|
|
|
|
|
|
|
// optimization, check for new list is empty
|
|
|
|
|
if (newIter == newNodeList.constEnd()) {
|
|
|
|
|
// New Node List is empty, everything removed
|
|
|
|
|
if (oldIter == oldNodeList.constEnd())
|
|
|
|
|
return;
|
|
|
|
|
// So all we need to do is easy
|
|
|
|
|
beginRemoveRows(parentIndex, 0, oldNodeList.size() - 1);
|
|
|
|
|
m_childNodes.insert(parentNode, newNodeList);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-09 11:07:24 +01:00
|
|
|
while (true) {
|
2008-12-02 12:01:29 +01:00
|
|
|
// Skip all that are the same
|
2008-12-09 11:07:24 +01:00
|
|
|
while (*oldIter == *newIter) {
|
2008-12-02 12:01:29 +01:00
|
|
|
++oldIter;
|
|
|
|
|
++newIter;
|
|
|
|
|
if (newIter == newNodeList.constEnd()) {
|
|
|
|
|
// At end of newNodeList, sweep up rest of oldNodeList
|
|
|
|
|
QList<Node *>::const_iterator startOfBlock = oldIter;
|
|
|
|
|
oldIter = oldNodeList.constEnd();
|
|
|
|
|
int pos = startOfBlock - oldNodeList.constBegin();
|
|
|
|
|
int count = oldIter - startOfBlock;
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
beginRemoveRows(parentIndex, pos, pos+count-1);
|
2008-12-09 11:07:24 +01:00
|
|
|
while (startOfBlock != oldIter) {
|
2008-12-02 12:01:29 +01:00
|
|
|
++startOfBlock;
|
|
|
|
|
oldNodeList.removeAt(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_childNodes.insert(parentNode, oldNodeList);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
}
|
|
|
|
|
return; // Done with the lists, leave the function
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<Node *>::const_iterator startOfBlock = oldIter;
|
2008-12-09 11:07:24 +01:00
|
|
|
while (*oldIter != *newIter)
|
2008-12-02 12:01:29 +01:00
|
|
|
++oldIter;
|
|
|
|
|
// startOfBlock is the first that was diffrent
|
|
|
|
|
// oldIter points to the new position of newIter
|
|
|
|
|
// oldIter - startOfBlock is number of new items
|
|
|
|
|
// newIter is the position where those are...
|
|
|
|
|
int pos = startOfBlock - oldNodeList.constBegin();
|
|
|
|
|
int count = oldIter - startOfBlock;
|
|
|
|
|
beginRemoveRows(parentIndex, pos, pos + count - 1);
|
2008-12-09 11:07:24 +01:00
|
|
|
while (startOfBlock != oldIter) {
|
2008-12-02 12:01:29 +01:00
|
|
|
++startOfBlock;
|
|
|
|
|
oldNodeList.removeAt(pos);
|
|
|
|
|
}
|
|
|
|
|
m_childNodes.insert(parentNode, oldNodeList);
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
oldIter = oldNodeList.constBegin() + pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::foldersAboutToBeAdded(FolderNode *parentFolder, const QList<FolderNode*> &newFolders)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(newFolders)
|
|
|
|
|
m_parentFolderForChange = parentFolder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::foldersAdded()
|
|
|
|
|
{
|
|
|
|
|
// First found out what the folder is that we are adding the files to
|
|
|
|
|
FolderNode *folderNode = visibleFolderNode(m_parentFolderForChange);
|
|
|
|
|
|
|
|
|
|
// Now get the new list for that folder
|
|
|
|
|
QList<Node *> newNodeList = childNodes(folderNode);
|
|
|
|
|
|
|
|
|
|
added(folderNode, newNodeList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::foldersAboutToBeRemoved(FolderNode *parentFolder, const QList<FolderNode*> &staleFolders)
|
|
|
|
|
{
|
|
|
|
|
QSet<Node *> blackList;
|
2008-12-09 11:07:24 +01:00
|
|
|
foreach (FolderNode *node, staleFolders)
|
2008-12-02 12:01:29 +01:00
|
|
|
blackList.insert(node);
|
|
|
|
|
|
|
|
|
|
FolderNode *folderNode = visibleFolderNode(parentFolder);
|
|
|
|
|
QList<Node *> newNodeList = childNodes(folderNode, blackList);
|
|
|
|
|
|
2009-01-08 17:44:48 +01:00
|
|
|
removed(folderNode, newNodeList);
|
2008-12-02 12:01:29 +01:00
|
|
|
removeFromCache(staleFolders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::removeFromCache(QList<FolderNode *> list)
|
|
|
|
|
{
|
2008-12-09 11:07:24 +01:00
|
|
|
foreach (FolderNode *fn, list) {
|
2008-12-02 12:01:29 +01:00
|
|
|
removeFromCache(fn->subFolderNodes());
|
|
|
|
|
m_childNodes.remove(fn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::foldersRemoved()
|
|
|
|
|
{
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::filesAboutToBeAdded(FolderNode *folder, const QList<FileNode*> &newFiles)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(newFiles)
|
|
|
|
|
m_parentFolderForChange = folder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::filesAdded()
|
|
|
|
|
{
|
|
|
|
|
// First find out what the folder is that we are adding the files to
|
|
|
|
|
FolderNode *folderNode = visibleFolderNode(m_parentFolderForChange);
|
|
|
|
|
|
|
|
|
|
// Now get the new List for that folder
|
|
|
|
|
QList<Node *> newNodeList = childNodes(folderNode);
|
|
|
|
|
added(folderNode, newNodeList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::filesAboutToBeRemoved(FolderNode *folder, const QList<FileNode*> &staleFiles)
|
|
|
|
|
{
|
|
|
|
|
// First found out what the folder (that is the project) is that we are adding the files to
|
|
|
|
|
FolderNode *folderNode = visibleFolderNode(folder);
|
|
|
|
|
|
|
|
|
|
QSet<Node *> blackList;
|
2008-12-09 11:07:24 +01:00
|
|
|
foreach(Node *node, staleFiles)
|
2008-12-02 12:01:29 +01:00
|
|
|
blackList.insert(node);
|
|
|
|
|
|
|
|
|
|
// Now get the new List for that folder
|
|
|
|
|
QList<Node *> newNodeList = childNodes(folderNode, blackList);
|
|
|
|
|
removed(folderNode, newNodeList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlatModel::filesRemoved()
|
|
|
|
|
{
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|