Files
qt-creator/src/plugins/qmakeprojectmanager/qmakeprojectmanager.cpp

332 lines
10 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2013 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://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/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 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.
**
** 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
#include "qmakeprojectmanager.h"
2008-12-02 16:19:05 +01:00
#include "qmakeprojectmanagerconstants.h"
#include "qmakeprojectmanagerplugin.h"
#include "qmakenodes.h"
#include "qmakeproject.h"
#include "profileeditor.h"
2008-12-02 12:01:29 +01:00
#include "qmakestep.h"
#include "qmakebuildconfiguration.h"
#include "addlibrarywizard.h"
#include "wizards/qtquickapp.h"
#include "wizards/html5app.h"
2008-12-02 12:01:29 +01:00
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <utils/qtcassert.h>
2008-12-02 12:01:29 +01:00
#include <QDir>
#include <QFileInfo>
#include <QVariant>
#include <QMessageBox>
2008-12-02 12:01:29 +01:00
using namespace ProjectExplorer;
using namespace QmakeProjectManager;
using namespace QmakeProjectManager::Internal;
2008-12-02 12:01:29 +01:00
// Known file types of a Qt 4 project
static const char *qt4FileTypes[] = {
"CppHeaderFiles",
"CppSourceFiles",
"Qt4FormFiles",
"Qt4ResourceFiles"
};
2008-12-02 12:01:29 +01:00
QmakeManager::QmakeManager(QmakeProjectManagerPlugin *plugin)
: m_plugin(plugin),
m_contextNode(0),
2008-12-02 12:01:29 +01:00
m_contextProject(0),
m_contextFile(0)
2008-12-02 12:01:29 +01:00
{
}
QmakeManager::~QmakeManager()
2008-12-02 12:01:29 +01:00
{
}
void QmakeManager::registerProject(QmakeProject *project)
{
m_projects.append(project);
}
void QmakeManager::unregisterProject(QmakeProject *project)
{
m_projects.removeOne(project);
}
void QmakeManager::notifyChanged(const QString &name)
{
foreach (QmakeProject *pro, m_projects)
pro->notifyChanged(name);
}
QString QmakeManager::mimeType() const
2008-12-02 12:01:29 +01:00
{
return QLatin1String(QmakeProjectManager::Constants::PROFILE_MIMETYPE);
2008-12-02 12:01:29 +01:00
}
ProjectExplorer::Project *QmakeManager::openProject(const QString &fileName, QString *errorString)
2008-12-02 12:01:29 +01:00
{
if (!QFileInfo(fileName).isFile()) {
if (errorString)
*errorString = tr("Failed opening project '%1': Project is not a file")
.arg(fileName);
2008-12-02 12:01:29 +01:00
return 0;
}
return new QmakeProject(this, fileName);
2008-12-02 12:01:29 +01:00
}
ProjectExplorer::Node *QmakeManager::contextNode() const
2008-12-02 12:01:29 +01:00
{
return m_contextNode;
}
void QmakeManager::setContextNode(ProjectExplorer::Node *node)
2008-12-02 12:01:29 +01:00
{
m_contextNode = node;
}
ProjectExplorer::Project *QmakeManager::contextProject() const
2008-12-02 12:01:29 +01:00
{
return m_contextProject;
2008-12-02 12:01:29 +01:00
}
void QmakeManager::setContextProject(ProjectExplorer::Project *project)
2008-12-02 12:01:29 +01:00
{
m_contextProject = project;
2008-12-02 12:01:29 +01:00
}
ProjectExplorer::FileNode *QmakeManager::contextFile() const
{
return m_contextFile;
}
void QmakeManager::setContextFile(ProjectExplorer::FileNode *file)
{
m_contextFile = file;
}
void QmakeManager::addLibrary()
{
ProFileEditorWidget *editor =
qobject_cast<ProFileEditorWidget*>(Core::EditorManager::currentEditor()->widget());
if (editor)
addLibrary(editor->editorDocument()->filePath(), editor);
}
void QmakeManager::addLibraryContextMenu()
{
ProjectExplorer::Node *node = ProjectExplorer::ProjectExplorerPlugin::instance()->currentNode();
if (qobject_cast<QmakeProFileNode *>(node))
addLibrary(node->path());
}
void QmakeManager::addLibrary(const QString &fileName, ProFileEditorWidget *editor)
{
AddLibraryWizard wizard(fileName, Core::EditorManager::instance());
if (wizard.exec() != QDialog::Accepted)
return;
TextEditor::BaseTextEditor *editable = 0;
if (editor) {
editable = editor->editor();
} else {
editable = qobject_cast<TextEditor::BaseTextEditor *>
(Core::EditorManager::openEditor(fileName, QmakeProjectManager::Constants::PROFILE_EDITOR_ID,
Core::EditorManager::DoNotMakeVisible));
}
if (!editable)
return;
const int endOfDoc = editable->position(TextEditor::ITextEditor::EndOfDoc);
editable->setCursorPosition(endOfDoc);
QString snippet = wizard.snippet();
// add extra \n in case the last line is not empty
int line, column;
editable->convertPosition(endOfDoc, &line, &column);
if (!editable->textDocument()->textAt(endOfDoc - column, column).simplified().isEmpty())
snippet = QLatin1Char('\n') + snippet;
editable->insert(snippet);
}
void QmakeManager::runQMake()
2008-12-02 12:01:29 +01:00
{
runQMake(SessionManager::startupProject(), 0);
2008-12-02 12:01:29 +01:00
}
void QmakeManager::runQMakeContextMenu()
2008-12-02 12:01:29 +01:00
{
runQMake(m_contextProject, m_contextNode);
2008-12-02 12:01:29 +01:00
}
void QmakeManager::runQMake(ProjectExplorer::Project *p, ProjectExplorer::Node *node)
2008-12-02 12:01:29 +01:00
{
if (!ProjectExplorer::ProjectExplorerPlugin::instance()->saveModifiedFiles())
return;
QmakeProject *qt4pro = qobject_cast<QmakeProject *>(p);
2009-12-08 13:09:16 +01:00
QTC_ASSERT(qt4pro, return);
if (!qt4pro->activeTarget() ||
!qt4pro->activeTarget()->activeBuildConfiguration())
return;
QmakeBuildConfiguration *bc = static_cast<QmakeBuildConfiguration *>(qt4pro->activeTarget()->activeBuildConfiguration());
QMakeStep *qs = bc->qmakeStep();
if (!qs)
return;
2008-12-02 12:01:29 +01:00
//found qmakeStep, now use it
qs->setForced(true);
if (node != 0 && node != qt4pro->rootProjectNode())
if (QmakeProFileNode *profile = qobject_cast<QmakeProFileNode *>(node))
bc->setSubNodeBuild(profile);
BuildManager::appendStep(qs, tr("QMake"));
bc->setSubNodeBuild(0);
}
void QmakeManager::buildSubDirContextMenu()
{
handleSubDirContextMenu(BUILD, false);
}
void QmakeManager::cleanSubDirContextMenu()
{
handleSubDirContextMenu(CLEAN, false);
}
void QmakeManager::rebuildSubDirContextMenu()
{
handleSubDirContextMenu(REBUILD, false);
}
void QmakeManager::buildFileContextMenu()
{
handleSubDirContextMenu(BUILD, true);
}
void QmakeManager::buildFile()
{
if (Core::IDocument *currentDocument= Core::EditorManager::currentDocument()) {
const QString file = currentDocument->filePath();
FileNode *node = qobject_cast<FileNode *>(SessionManager::nodeForFile(file));
Project *project = SessionManager::projectForFile(file);
if (project && node)
handleSubDirContextMenu(BUILD, true, project, node->projectNode(), node);
}
}
void QmakeManager::handleSubDirContextMenu(QmakeManager::Action action, bool isFileBuild)
{
handleSubDirContextMenu(action, isFileBuild, m_contextProject, m_contextNode, m_contextFile);
}
void QmakeManager::handleSubDirContextMenu(QmakeManager::Action action, bool isFileBuild,
ProjectExplorer::Project *contextProject,
ProjectExplorer::Node *contextNode,
ProjectExplorer::FileNode *contextFile)
{
QmakeProject *qt4pro = qobject_cast<QmakeProject *>(contextProject);
QTC_ASSERT(qt4pro, return);
if (!qt4pro->activeTarget() ||
!qt4pro->activeTarget()->activeBuildConfiguration())
return;
if (!contextNode || !contextFile)
isFileBuild = false;
QmakeBuildConfiguration *bc = qobject_cast<QmakeBuildConfiguration *>(qt4pro->activeTarget()->activeBuildConfiguration());
if (!bc)
return;
if (contextNode) {
if (QmakePriFileNode *prifile = qobject_cast<QmakePriFileNode *>(contextNode)) {
if (QmakeProFileNode *profile = prifile->proFileNode()) {
if (profile != qt4pro->rootProjectNode() || isFileBuild)
bc->setSubNodeBuild(profile);
}
}
}
if (isFileBuild)
bc->setFileNodeBuild(contextFile);
if (ProjectExplorerPlugin::instance()->saveModifiedFiles()) {
const Core::Id buildStep = ProjectExplorer::Constants::BUILDSTEPS_BUILD;
const Core::Id cleanStep = ProjectExplorer::Constants::BUILDSTEPS_CLEAN;
if (action == BUILD) {
const QString name = ProjectExplorerPlugin::displayNameForStepId(buildStep);
BuildManager::buildList(bc->stepList(buildStep), name);
} else if (action == CLEAN) {
const QString name = ProjectExplorerPlugin::displayNameForStepId(cleanStep);
BuildManager::buildList(bc->stepList(cleanStep), name);
} else if (action == REBUILD) {
QStringList names;
names << ProjectExplorerPlugin::displayNameForStepId(cleanStep)
<< ProjectExplorerPlugin::displayNameForStepId(buildStep);
QList<ProjectExplorer::BuildStepList *> stepLists;
stepLists << bc->stepList(cleanStep) << bc->stepList(buildStep);
BuildManager::buildLists(stepLists, names);
}
}
bc->setSubNodeBuild(0);
bc->setFileNodeBuild(0);
2008-12-02 12:01:29 +01:00
}
QString QmakeManager::fileTypeId(ProjectExplorer::FileType type)
2008-12-02 12:01:29 +01:00
{
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();
}