Skip to content
Snippets Groups Projects
qt4nodes.cpp 39.3 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
**
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
**
** Commercial Usage
** 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.
** 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 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.
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
con's avatar
con committed
**
**************************************************************************/
hjk's avatar
hjk committed

con's avatar
con committed
#include "proeditormodel.h"
hjk's avatar
hjk committed

con's avatar
con committed
#include "profilereader.h"
#include "prowriter.h"
con's avatar
con committed
#include "qt4nodes.h"
#include "qt4project.h"
#include "qt4projectmanager.h"
dt's avatar
dt committed
#include "qtuicodemodelsupport.h"
con's avatar
con committed

#include <projectexplorer/nodesvisitor.h>
#include <projectexplorer/filewatcher.h>
con's avatar
con committed

#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/fileiconprovider.h>
con's avatar
con committed
#include <coreplugin/filemanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>

#include <cpptools/cppmodelmanagerinterface.h>
#include <cplusplus/CppDocument.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/buildmanager.h>
con's avatar
con committed

hjk's avatar
hjk committed
#include <utils/qtcassert.h>

con's avatar
con committed
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QTimer>
#include <QtGui/QPainter>
con's avatar
con committed
#include <QtGui/QMainWindow>
#include <QtGui/QMessageBox>
#include <QtGui/QPushButton>

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

namespace {
    bool debug = false;
}

namespace {
    // sorting helper function
    bool sortProjectFilesByPath(ProFile *f1, ProFile *f2)
    {
        return f1->fileName() < f2->fileName();
    }
}

/*!
  \class Qt4PriFileNode
  Implements abstract ProjectNode class
  */

Qt4PriFileNode::Qt4PriFileNode(Qt4Project *project, Qt4ProFileNode* qt4ProFileNode, const QString &filePath)
con's avatar
con committed
        : ProjectNode(filePath),
          m_project(project),
          m_qt4ProFileNode(qt4ProFileNode),
          m_projectFilePath(QDir::fromNativeSeparators(filePath)),
          m_projectDir(QFileInfo(filePath).absolutePath()),
          m_fileWatcher(new ProjectExplorer::FileWatcher(this))
con's avatar
con committed
{
    Q_ASSERT(project);
    setFolderName(QFileInfo(filePath).completeBaseName());

    static QIcon dirIcon;
    if (dirIcon.isNull()) {
        // Create a custom Qt dir icon based on the system icon
        Core::FileIconProvider *iconProvider = Core::FileIconProvider::instance();
        QPixmap dirIconPixmap = iconProvider->overlayIcon(QStyle::SP_DirIcon,
                                                          QIcon(":/qt4projectmanager/images/qt_project.png"),
                                                          QSize(16, 16));
        dirIcon.addPixmap(dirIconPixmap);
    }
    setIcon(dirIcon);
    m_fileWatcher->addFile(filePath);
    connect(m_fileWatcher, SIGNAL(fileChanged(QString)),
            this, SLOT(scheduleUpdate()));
}

void Qt4PriFileNode::scheduleUpdate()
{
    m_qt4ProFileNode->scheduleUpdate();
con's avatar
con committed
}

void Qt4PriFileNode::update(ProFile *includeFile, ProFileReader *reader)
{
    Q_ASSERT(includeFile);
    Q_ASSERT(reader);
con's avatar
con committed

    // add project file node
    if (m_fileNodes.isEmpty())
        addFileNodes(QList<FileNode*>() << new FileNode(m_projectFilePath, ProjectFileType, false), this);

    static QList<FileType> fileTypes =
               (QList<FileType>() << ProjectExplorer::HeaderType
                                  << ProjectExplorer::SourceType
                                  << ProjectExplorer::FormType
                                  << ProjectExplorer::ResourceType
                                  << ProjectExplorer::UnknownFileType);

    const QString &projectDir = m_qt4ProFileNode->m_projectDir;

    QStringList baseVPaths;
    baseVPaths += reader->absolutePathValues("VPATH", projectDir);
    baseVPaths << projectDir; // QMAKE_ABSOLUTE_SOURCE_PATH
    baseVPaths += reader->absolutePathValues("DEPENDPATH", projectDir);
    baseVPaths.removeDuplicates();

con's avatar
con committed
    // update files
    foreach (FileType type, fileTypes) {
        const QStringList qmakeVariables = varNames(type);

        QStringList newFilePaths;
        foreach (const QString &qmakeVariable, qmakeVariables) {
            QStringList vPaths;
            if (type == ProjectExplorer::SourceType)
                vPaths = reader->absolutePathValues("VPATH_" + qmakeVariable, projectDir);
            vPaths += baseVPaths;
            if (type == ProjectExplorer::HeaderType)
                vPaths += reader->absolutePathValues("INCLUDEPATH", projectDir);
            vPaths.removeDuplicates();
            newFilePaths += reader->absoluteFileValues(qmakeVariable, projectDir, vPaths, includeFile);
        }
        newFilePaths.removeDuplicates();
con's avatar
con committed

        QList<FileNode*> existingFileNodes;
        foreach (FileNode *fileNode, fileNodes()) {
            if (fileNode->fileType() == type && !fileNode->isGenerated())
                existingFileNodes << fileNode;
        }

        QList<FileNode*> toRemove;
        QList<FileNode*> toAdd;

        qSort(newFilePaths);
        qSort(existingFileNodes.begin(), existingFileNodes.end(), ProjectNode::sortNodesByPath);

        QList<FileNode*>::const_iterator existingNodeIter = existingFileNodes.constBegin();
        QList<QString>::const_iterator newPathIter = newFilePaths.constBegin();
        while (existingNodeIter != existingFileNodes.constEnd()
               && newPathIter != newFilePaths.constEnd()) {
            if ((*existingNodeIter)->path() < *newPathIter) {
                toRemove << *existingNodeIter;
                ++existingNodeIter;
            } else if ((*existingNodeIter)->path() > *newPathIter) {
                toAdd << new FileNode(*newPathIter, type, false);
                ++newPathIter;
            } else { // *existingNodeIter->path() == *newPathIter
                ++existingNodeIter;
                ++newPathIter;
            }
        }
        while (existingNodeIter != existingFileNodes.constEnd()) {
            toRemove << *existingNodeIter;
            ++existingNodeIter;
        }
        while (newPathIter != newFilePaths.constEnd()) {
            toAdd << new FileNode(*newPathIter, type, false);
            ++newPathIter;
        }

        if (!toRemove.isEmpty())
            removeFileNodes(toRemove, this);
        if (!toAdd.isEmpty())
            addFileNodes(toAdd, this);
    }
Loading
Loading full blame...