Skip to content
Snippets Groups Projects
qt4projectmanager.cpp 11.9 KiB
Newer Older
/**************************************************************************
con's avatar
con committed
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
** Copyright (c) 2010 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
hjk's avatar
hjk committed
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
**
**************************************************************************/
hjk's avatar
hjk committed

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

con's avatar
con committed
#include "qt4projectmanagerconstants.h"
#include "qt4projectmanagerplugin.h"
#include "qt4nodes.h"
#include "qt4project.h"
Tobias Hunger's avatar
Tobias Hunger committed
#include "qt4target.h"
con's avatar
con committed
#include "profilereader.h"
#include "qmakestep.h"
dt's avatar
dt committed
#include "qt4buildconfiguration.h"
#include "wizards/qmlstandaloneapp.h"
con's avatar
con committed

con's avatar
con committed
#include <coreplugin/basefilewizard.h>
#include <coreplugin/messagemanager.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/session.h>
con's avatar
con committed
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <utils/qtcassert.h>
#include <designer/formwindoweditor.h>
con's avatar
con committed

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QVariant>
con's avatar
con committed
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
con's avatar
con committed

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;

using ProjectExplorer::BuildStep;
using ProjectExplorer::FileType;
using ProjectExplorer::HeaderType;
using ProjectExplorer::SourceType;
using ProjectExplorer::FormType;
using ProjectExplorer::ResourceType;
using ProjectExplorer::UnknownFileType;

// Known file types of a Qt 4 project
static const char* qt4FileTypes[] = {
    "CppHeaderFiles",
    "CppSourceFiles",
    "Qt4FormFiles",
    "Qt4ResourceFiles"
};
con's avatar
con committed

Qt4Manager::Qt4Manager(Qt4ProjectManagerPlugin *plugin)
  : m_plugin(plugin),
con's avatar
con committed
    m_contextProject(0),
dt's avatar
dt committed
    m_lastEditor(0),
    m_dirty(false)
con's avatar
con committed
{
}

Qt4Manager::~Qt4Manager()
{
}

void Qt4Manager::registerProject(Qt4Project *project)
{
    m_projects.append(project);
}

void Qt4Manager::unregisterProject(Qt4Project *project)
{
    m_projects.removeOne(project);
}

void Qt4Manager::notifyChanged(const QString &name)
{
hjk's avatar
hjk committed
    foreach (Qt4Project *pro, m_projects)
        pro->notifyChanged(name);
}

con's avatar
con committed
void Qt4Manager::init()
{
dt's avatar
dt committed
    connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
            this, SLOT(editorAboutToClose(Core::IEditor*)));

    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(editorChanged(Core::IEditor*)));
}

void Qt4Manager::editorChanged(Core::IEditor *editor)
{
    // Handle old editor
    Designer::FormWindowEditor *lastFormEditor = qobject_cast<Designer::FormWindowEditor *>(m_lastEditor);
    if (lastFormEditor) {
        disconnect(lastFormEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
dt's avatar
dt committed

        if (m_dirty) {
            const QString contents = lastFormEditor->contents();
dt's avatar
dt committed
            foreach(Qt4Project *project, m_projects)
                project->rootProjectNode()->updateCodeModelSupportFromEditor(lastFormEditor->file()->fileName(), contents);
dt's avatar
dt committed
            m_dirty = false;
        }
    }

    m_lastEditor = editor;

    // Handle new editor
    if (Designer::FormWindowEditor *fw = qobject_cast<Designer::FormWindowEditor *>(editor))
dt's avatar
dt committed
        connect(fw, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
}

void Qt4Manager::editorAboutToClose(Core::IEditor *editor)
{
    if (m_lastEditor == editor) {
        // Oh no our editor is going to be closed
        // get the content first
        Designer::FormWindowEditor *lastEditor = qobject_cast<Designer::FormWindowEditor *>(m_lastEditor);
dt's avatar
dt committed
        if (lastEditor) {
            disconnect(lastEditor, SIGNAL(changed()), this, SLOT(uiEditorContentsChanged()));
            if (m_dirty) {
                const QString contents = lastEditor->contents();
dt's avatar
dt committed
                foreach(Qt4Project *project, m_projects)
                    project->rootProjectNode()->updateCodeModelSupportFromEditor(lastEditor->file()->fileName(), contents);
dt's avatar
dt committed
                m_dirty = false;
            }
        }
        m_lastEditor = 0;
    }
}

void Qt4Manager::uiEditorContentsChanged()
{
    // cast sender, get filename
    if (m_dirty)
        return;
    Designer::FormWindowEditor *fw = qobject_cast<Designer::FormWindowEditor *>(sender());
dt's avatar
dt committed
    if (!fw)
        return;
    m_dirty = true;
con's avatar
con committed
}

dt's avatar
dt committed
Core::Context Qt4Manager::projectContext() const
con's avatar
con committed
{
     return m_plugin->projectContext();
}

Core::Context Qt4Manager::projectLanguage() const
con's avatar
con committed
{
    return Core::Context(ProjectExplorer::Constants::LANG_CXX);
con's avatar
con committed
}

QString Qt4Manager::mimeType() const
{
    return QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE);
con's avatar
con committed
}

// Prototype Ui for update of QmlApplicationView files.
// TODO implement a proper Ui for this. Maybe not as modal message box.
// When removing this, also remove the inclusions of "wizards/qmlstandaloneapp.h" and QtGui/QMessageBox
inline void updateQmlApplicationViewerFiles(const QString proFile)
{
    const QList<QmlAppGeneratedFileInfo> updates =
            QmlStandaloneApp::fileUpdates(proFile);
    if (!updates.empty()) {
        // TODO Translate the folloing strings when we want to keep the code
        QString message = QLatin1String("The following files are either outdated or have been modified:");
        message.append(QLatin1String("<ul>"));
        foreach (const QmlAppGeneratedFileInfo &info, updates) {
            QStringList reasons;
            if (info.wasModified())
                reasons.append(QLatin1String("modified"));
            if (info.isOutdated())
                reasons.append(QLatin1String("outdated"));
            message.append(QString::fromLatin1("<li><nobr>%1 (%2)</nobr></li>")
                           .arg(QDir::toNativeSeparators(info.fileInfo.canonicalFilePath()))
                           .arg(reasons.join(QLatin1String(", "))));
        }
        message.append(QLatin1String("</ul>"));
        message.append(QLatin1String("Do you want Qt Creator to update the files? Any changes will be lost."));
        const QString title = QLatin1String("Update of the QmlApplicationView files");
        if (QMessageBox::question(0, title, message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
            QString error;
            if (!QmlStandaloneApp::updateFiles(updates, error))
                QMessageBox::critical(0, title, error);
        }
    }
}

ProjectExplorer::Project *Qt4Manager::openProject(const QString &fileName)
con's avatar
con committed
{
    Core::MessageManager *messageManager = Core::ICore::instance()->messageManager();
con's avatar
con committed

    // TODO Make all file paths relative & remove this hack
    // We convert the path to an absolute one here because qt4project.cpp
    // && profileevaluator use absolute/canonical file paths all over the place
    // Correct fix would be to remove these calls ...
    QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();

    if (canonicalFilePath.isEmpty()) {
        messageManager->printToOutputPane(tr("Failed opening project '%1': Project file does not exist").arg(QDir::toNativeSeparators(canonicalFilePath)));
con's avatar
con committed
        return 0;
    }

    foreach (ProjectExplorer::Project *pi, projectExplorer()->session()->projects()) {
        if (canonicalFilePath == pi->file()->fileName()) {
            messageManager->printToOutputPane(tr("Failed opening project '%1': Project already open").arg(QDir::toNativeSeparators(canonicalFilePath)));
con's avatar
con committed
            return 0;
        }
    }

    updateQmlApplicationViewerFiles(canonicalFilePath);

con's avatar
con committed
    Qt4Project *pro = new Qt4Project(this, canonicalFilePath);
    return pro;
}

ProjectExplorer::ProjectExplorerPlugin *Qt4Manager::projectExplorer() const
{
    return ProjectExplorer::ProjectExplorerPlugin::instance();
con's avatar
con committed
}

ProjectExplorer::Node *Qt4Manager::contextNode() const
{
    return m_contextNode;
}

void Qt4Manager::setContextNode(ProjectExplorer::Node *node)
{
    m_contextNode = node;
}

void Qt4Manager::setContextProject(ProjectExplorer::Project *project)
{
    m_contextProject = project;
}

ProjectExplorer::Project *Qt4Manager::contextProject() const
{
    return m_contextProject;
}

void Qt4Manager::runQMake()
{
    runQMake(projectExplorer()->currentProject(), 0);
con's avatar
con committed
}

void Qt4Manager::runQMakeContextMenu()
{
    runQMake(m_contextProject, m_contextNode);
con's avatar
con committed
}

void Qt4Manager::runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node)
con's avatar
con committed
{
    if (!ProjectExplorer::ProjectExplorerPlugin::instance()->saveModifiedFiles())
        return;
dt's avatar
dt committed
    Qt4Project *qt4pro = qobject_cast<Qt4Project *>(p);
    QTC_ASSERT(qt4pro, return);
Tobias Hunger's avatar
Tobias Hunger committed
    if (!qt4pro->activeTarget() ||
        !qt4pro->activeTarget()->activeBuildConfiguration())
    return;

    Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
dt's avatar
dt committed
    QMakeStep *qs = bc->qmakeStep();
    if (!qs)
        return;
con's avatar
con committed
    //found qmakeStep, now use it
    qs->setForced(true);

    if (node != 0 && node != qt4pro->rootProjectNode())
        if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(node))
            bc->setSubNodeBuild(profile);

    projectExplorer()->buildManager()->appendStep(qs);
    bc->setSubNodeBuild(0);
}

void Qt4Manager::buildSubDirContextMenu()
dt's avatar
dt committed
{
    handleSubDirContexMenu(BUILD);
}

void Qt4Manager::cleanSubDirContextMenu()
{
    handleSubDirContexMenu(CLEAN);
}

void Qt4Manager::rebuildSubDirContextMenu()
{
    handleSubDirContexMenu(REBUILD);
}

void Qt4Manager::handleSubDirContexMenu(Qt4Manager::Action action)
{
    Qt4Project *qt4pro = qobject_cast<Qt4Project *>(m_contextProject);
    QTC_ASSERT(qt4pro, return);

Tobias Hunger's avatar
Tobias Hunger committed
    if (!qt4pro->activeTarget() ||
        !qt4pro->activeTarget()->activeBuildConfiguration())
    return;

    Qt4BuildConfiguration *bc = qt4pro->activeTarget()->activeBuildConfiguration();
    if (m_contextNode != 0 && m_contextNode != qt4pro->rootProjectNode())
        if (Qt4ProFileNode *profile = qobject_cast<Qt4ProFileNode *>(m_contextNode))
            bc->setSubNodeBuild(profile);

dt's avatar
dt committed
    if (projectExplorer()->saveModifiedFiles()) {
Tobias Hunger's avatar
Tobias Hunger committed
        if (action == BUILD) {
            projectExplorer()->buildManager()->buildList(bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD));
        } else if (action == CLEAN) {
            projectExplorer()->buildManager()->buildList(bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN));
        } else if (action == REBUILD) {
            QList<ProjectExplorer::BuildStepList *> stepLists;
            stepLists << bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_CLEAN);
            stepLists << bc->stepList(ProjectExplorer::Constants::BUILDSTEPS_BUILD);
            projectExplorer()->buildManager()->buildLists(stepLists);
dt's avatar
dt committed
        }
    }
con's avatar
con committed
}

QString Qt4Manager::fileTypeId(ProjectExplorer::FileType type)
{
    switch (type) {
    case HeaderType:
        return QLatin1String(qt4FileTypes[0]);
    case SourceType:
        return QLatin1String(qt4FileTypes[1]);
    case FormType:
        return QLatin1String(qt4FileTypes[2]);
    case ResourceType:
        return QLatin1String(qt4FileTypes[3]);
    case UnknownFileType:
    default:
        break;
    }
    return QString();
}