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

700 lines
28 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
2010-12-17 16:01:08 +01:00
**
****************************************************************************/
2008-12-02 16:19:05 +01:00
#include "qmakeprojectmanagerplugin.h"
2008-12-02 16:19:05 +01:00
#include "addlibrarywizard.h"
#include "profileeditor.h"
#include "qmakenodes.h"
#include "qmakesettings.h"
#include "qmakestep.h"
#include "qmakemakestep.h"
#include "qmakebuildconfiguration.h"
#include "wizards/simpleprojectwizard.h"
#include "wizards/subdirsprojectwizard.h"
#include "customwidgetwizard/customwidgetwizard.h"
#include "qmakeprojectmanagerconstants.h"
#include "qmakeproject.h"
#include "externaleditors.h"
#include "qmakekitinformation.h"
#include "profilehighlighter.h"
2009-05-26 15:31:29 +02:00
#include <coreplugin/icore.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/runcontrol.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <projectexplorer/projectexplorer.h>
#include <texteditor/texteditor.h>
2008-12-02 12:01:29 +01:00
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>
#include <utils/hostosinfo.h>
#include <utils/parameteraction.h>
2008-12-02 12:01:29 +01:00
#ifdef WITH_TESTS
# include <QTest>
2008-12-02 12:01:29 +01:00
#endif
using namespace Core;
using namespace ProjectExplorer;
using namespace TextEditor;
2008-12-02 12:01:29 +01:00
namespace QmakeProjectManager {
namespace Internal {
class QmakeProjectManagerPluginPrivate : public QObject
{
public:
~QmakeProjectManagerPluginPrivate() override;
void projectChanged();
void activeTargetChanged();
void updateActions();
void updateRunQMakeAction();
void updateContextActions();
void buildStateChanged(Project *pro);
void updateBuildFileAction();
void disableBuildFileMenus();
void enableBuildFileMenus(const Utils::FilePath &file);
Core::Context projectContext;
CustomWizardMetaFactory<CustomQmakeProjectWizard>
qmakeProjectWizard{"qmakeproject", IWizardFactory::ProjectWizard};
QMakeStepFactory qmakeStepFactory;
QmakeMakeStepFactory makeStepFactory;
QmakeBuildConfigurationFactory buildConfigFactory;
ProFileEditorFactory profileEditorFactory;
QmakeSettingsPage settingsPage;
ExternalQtEditor *m_designerEditor{ExternalQtEditor::createDesignerEditor()};
ExternalQtEditor *m_linguistEditor{ExternalQtEditor::createLinguistEditor()};
QmakeProject *m_previousStartupProject = nullptr;
Target *m_previousTarget = nullptr;
QAction *m_runQMakeAction = nullptr;
QAction *m_runQMakeActionContextMenu = nullptr;
Utils::ParameterAction *m_buildSubProjectContextMenu = nullptr;
QAction *m_subProjectRebuildSeparator = nullptr;
QAction *m_rebuildSubProjectContextMenu = nullptr;
QAction *m_cleanSubProjectContextMenu = nullptr;
QAction *m_buildFileContextMenu = nullptr;
Utils::ParameterAction *m_buildSubProjectAction = nullptr;
Utils::ParameterAction *m_rebuildSubProjectAction = nullptr;
Utils::ParameterAction *m_cleanSubProjectAction = nullptr;
Utils::ParameterAction *m_buildFileAction = nullptr;
QAction *m_addLibraryAction = nullptr;
QAction *m_addLibraryActionContextMenu = nullptr;
QmakeKitAspect qmakeKitAspect;
enum Action { BUILD, REBUILD, CLEAN };
void addLibrary();
void addLibraryContextMenu();
void runQMake();
void runQMakeContextMenu();
void buildSubDirContextMenu();
void rebuildSubDirContextMenu();
void cleanSubDirContextMenu();
void buildFileContextMenu();
void buildFile();
void handleSubDirContextMenu(Action action, bool isFileBuild);
static void handleSubDirContextMenu(Action action, bool isFileBuild,
Project *contextProject,
Node *contextProFileNode,
FileNode *buildableFile);
void addLibraryImpl(const QString &fileName, TextEditor::BaseTextEditor *editor);
void runQMakeImpl(Project *p, ProjectExplorer::Node *node);
};
QmakeProjectManagerPlugin::~QmakeProjectManagerPlugin()
{
delete d;
}
bool QmakeProjectManagerPlugin::initialize(const QStringList &arguments, QString *errorMessage)
2008-12-02 12:01:29 +01:00
{
Q_UNUSED(arguments)
Q_UNUSED(errorMessage)
const Context projectContext(QmakeProjectManager::Constants::QMAKEPROJECT_ID);
Context projectTreeContext(ProjectExplorer::Constants::C_PROJECT_TREE);
d = new QmakeProjectManagerPluginPrivate;
2008-12-02 12:01:29 +01:00
//create and register objects
ProjectManager::registerProjectType<QmakeProject>(QmakeProjectManager::Constants::PROFILE_MIMETYPE);
IWizardFactory::registerFactoryCreator([] {
return QList<IWizardFactory *> {
new SubdirsProjectWizard,
new CustomWidgetWizard,
new SimpleProjectWizard
};
});
2008-12-02 12:01:29 +01:00
//menus
ActionContainer *mbuild =
ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
ActionContainer *mproject =
ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
ActionContainer *msubproject =
ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
ActionContainer *mfile =
ActionManager::actionContainer(ProjectExplorer::Constants::M_FILECONTEXT);
2008-12-02 12:01:29 +01:00
//register actions
Command *command = nullptr;
2008-12-02 12:01:29 +01:00
d->m_buildSubProjectContextMenu = new Utils::ParameterAction(tr("Build"), tr("Build \"%1\""),
Utils::ParameterAction::AlwaysEnabled/*handled manually*/,
this);
command = ActionManager::registerAction(d->m_buildSubProjectContextMenu, Constants::BUILDSUBDIRCONTEXTMENU, projectContext);
command->setAttribute(Command::CA_Hide);
command->setAttribute(Command::CA_UpdateText);
command->setDescription(d->m_buildSubProjectContextMenu->text());
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(d->m_buildSubProjectContextMenu, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::buildSubDirContextMenu);
2008-12-02 12:01:29 +01:00
d->m_runQMakeActionContextMenu = new QAction(tr("Run qmake"), this);
command = ActionManager::registerAction(d->m_runQMakeActionContextMenu, Constants::RUNQMAKECONTEXTMENU, projectContext);
command->setAttribute(Command::CA_Hide);
2008-12-02 12:01:29 +01:00
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(d->m_runQMakeActionContextMenu, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::runQMakeContextMenu);
2008-12-02 12:01:29 +01:00
command = msubproject->addSeparator(projectContext, ProjectExplorer::Constants::G_PROJECT_BUILD,
&d->m_subProjectRebuildSeparator);
command->setAttribute(Command::CA_Hide);
d->m_rebuildSubProjectContextMenu = new QAction(tr("Rebuild"), this);
command = ActionManager::registerAction(
d->m_rebuildSubProjectContextMenu, Constants::REBUILDSUBDIRCONTEXTMENU, projectContext);
command->setAttribute(Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(d->m_rebuildSubProjectContextMenu, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::rebuildSubDirContextMenu);
d->m_cleanSubProjectContextMenu = new QAction(tr("Clean"), this);
command = ActionManager::registerAction(
d->m_cleanSubProjectContextMenu, Constants::CLEANSUBDIRCONTEXTMENU, projectContext);
command->setAttribute(Command::CA_Hide);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(d->m_cleanSubProjectContextMenu, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::cleanSubDirContextMenu);
d->m_buildFileContextMenu = new QAction(tr("Build"), this);
command = ActionManager::registerAction(d->m_buildFileContextMenu, Constants::BUILDFILECONTEXTMENU, projectContext);
command->setAttribute(Command::CA_Hide);
mfile->addAction(command, ProjectExplorer::Constants::G_FILE_OTHER);
connect(d->m_buildFileContextMenu, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::buildFileContextMenu);
d->m_buildSubProjectAction = new Utils::ParameterAction(tr("Build &Subproject"), tr("Build &Subproject \"%1\""),
Utils::ParameterAction::AlwaysEnabled, this);
command = ActionManager::registerAction(d->m_buildSubProjectAction, Constants::BUILDSUBDIR, projectContext);
command->setAttribute(Command::CA_Hide);
command->setAttribute(Command::CA_UpdateText);
command->setDescription(d->m_buildSubProjectAction->text());
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
connect(d->m_buildSubProjectAction, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::buildSubDirContextMenu);
d->m_runQMakeAction = new QAction(tr("Run qmake"), this);
const Context globalcontext(Core::Constants::C_GLOBAL);
command = ActionManager::registerAction(d->m_runQMakeAction, Constants::RUNQMAKE, globalcontext);
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
connect(d->m_runQMakeAction, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::runQMake);
d->m_rebuildSubProjectAction = new Utils::ParameterAction(tr("Rebuild Subproject"), tr("Rebuild Subproject \"%1\""),
Utils::ParameterAction::AlwaysEnabled, this);
command = ActionManager::registerAction(d->m_rebuildSubProjectAction, Constants::REBUILDSUBDIR, projectContext);
command->setAttribute(Command::CA_Hide);
command->setAttribute(Command::CA_UpdateText);
command->setDescription(d->m_rebuildSubProjectAction->text());
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_REBUILD);
connect(d->m_rebuildSubProjectAction, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::rebuildSubDirContextMenu);
d->m_cleanSubProjectAction = new Utils::ParameterAction(tr("Clean Subproject"), tr("Clean Subproject \"%1\""),
Utils::ParameterAction::AlwaysEnabled, this);
command = ActionManager::registerAction(d->m_cleanSubProjectAction, Constants::CLEANSUBDIR, projectContext);
command->setAttribute(Command::CA_Hide);
command->setAttribute(Command::CA_UpdateText);
command->setDescription(d->m_cleanSubProjectAction->text());
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_CLEAN);
connect(d->m_cleanSubProjectAction, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::cleanSubDirContextMenu);
d->m_buildFileAction = new Utils::ParameterAction(tr("Build File"), tr("Build File \"%1\""),
Utils::ParameterAction::AlwaysEnabled, this);
command = ActionManager::registerAction(d->m_buildFileAction, Constants::BUILDFILE, projectContext);
command->setAttribute(Command::CA_Hide);
command->setAttribute(Command::CA_UpdateText);
command->setDescription(d->m_buildFileAction->text());
command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Alt+B")));
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_BUILD);
connect(d->m_buildFileAction, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::buildFile);
connect(BuildManager::instance(), &BuildManager::buildStateChanged,
d, &QmakeProjectManagerPluginPrivate::buildStateChanged);
connect(SessionManager::instance(), &SessionManager::startupProjectChanged,
d, &QmakeProjectManagerPluginPrivate::projectChanged);
connect(ProjectTree::instance(), &ProjectTree::currentProjectChanged,
d, &QmakeProjectManagerPluginPrivate::projectChanged);
connect(ProjectTree::instance(), &ProjectTree::currentNodeChanged,
d, &QmakeProjectManagerPluginPrivate::updateContextActions);
2008-12-02 12:01:29 +01:00
ActionContainer *contextMenu = ActionManager::createMenu(QmakeProjectManager::Constants::M_CONTEXT);
Context proFileEditorContext = Context(QmakeProjectManager::Constants::PROFILE_EDITOR_ID);
command = ActionManager::command(TextEditor::Constants::JUMP_TO_FILE_UNDER_CURSOR);
contextMenu->addAction(command);
d->m_addLibraryAction = new QAction(tr("Add Library..."), this);
command = ActionManager::registerAction(d->m_addLibraryAction,
Constants::ADDLIBRARY, proFileEditorContext);
connect(d->m_addLibraryAction, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::addLibrary);
contextMenu->addAction(command);
d->m_addLibraryActionContextMenu = new QAction(tr("Add Library..."), this);
command = ActionManager::registerAction(d->m_addLibraryActionContextMenu,
Constants::ADDLIBRARY, projectTreeContext);
connect(d->m_addLibraryActionContextMenu, &QAction::triggered,
d, &QmakeProjectManagerPluginPrivate::addLibraryContextMenu);
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_FILES);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_FILES);
contextMenu->addSeparator(proFileEditorContext);
command = ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION);
contextMenu->addAction(command);
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
d, &QmakeProjectManagerPluginPrivate::updateBuildFileAction);
d->updateActions();
2008-12-02 12:01:29 +01:00
return true;
}
QmakeProjectManagerPluginPrivate::~QmakeProjectManagerPluginPrivate()
{
delete m_designerEditor;
delete m_linguistEditor;
}
2008-12-02 12:01:29 +01:00
void QmakeProjectManagerPluginPrivate::projectChanged()
{
if (m_previousStartupProject)
disconnect(m_previousStartupProject, &Project::activeTargetChanged,
this, &QmakeProjectManagerPluginPrivate::activeTargetChanged);
if (ProjectTree::currentProject())
m_previousStartupProject = qobject_cast<QmakeProject *>(ProjectTree::currentProject());
else
m_previousStartupProject = qobject_cast<QmakeProject *>(SessionManager::startupProject());
if (m_previousStartupProject) {
connect(m_previousStartupProject, &Project::activeTargetChanged,
this, &QmakeProjectManagerPluginPrivate::activeTargetChanged);
}
activeTargetChanged();
}
static QmakeProFileNode *buildableFileProFile(Node *node)
{
if (node) {
auto subPriFileNode = dynamic_cast<QmakePriFileNode *>(node);
if (!subPriFileNode)
subPriFileNode = dynamic_cast<QmakePriFileNode *>(node->parentProjectNode());
if (subPriFileNode)
return subPriFileNode->proFileNode();
}
return nullptr;
}
void QmakeProjectManagerPluginPrivate::addLibrary()
{
if (auto editor = qobject_cast<BaseTextEditor *>(Core::EditorManager::currentEditor()))
addLibraryImpl(editor->document()->filePath().toString(), editor);
}
void QmakeProjectManagerPluginPrivate::addLibraryContextMenu()
{
QString projectPath;
Node *node = ProjectTree::currentNode();
if (ContainerNode *cn = node->asContainerNode())
projectPath = cn->project()->projectFilePath().toString();
else if (dynamic_cast<QmakeProFileNode *>(node))
projectPath = node->filePath().toString();
addLibraryImpl(projectPath, nullptr);
}
void QmakeProjectManagerPluginPrivate::addLibraryImpl(const QString &fileName, BaseTextEditor *editor)
{
if (fileName.isEmpty())
return;
Internal::AddLibraryWizard wizard(fileName, Core::ICore::dialogParent());
if (wizard.exec() != QDialog::Accepted)
return;
if (!editor)
editor = qobject_cast<BaseTextEditor *>(Core::EditorManager::openEditor(fileName,
Constants::PROFILE_EDITOR_ID, Core::EditorManager::DoNotMakeVisible));
if (!editor)
return;
const int endOfDoc = editor->position(EndOfDocPosition);
editor->setCursorPosition(endOfDoc);
QString snippet = wizard.snippet();
// add extra \n in case the last line is not empty
int line, column;
editor->convertPosition(endOfDoc, &line, &column);
const int positionInBlock = column - 1;
if (!editor->textAt(endOfDoc - positionInBlock, positionInBlock).simplified().isEmpty())
snippet = QLatin1Char('\n') + snippet;
editor->insert(snippet);
}
void QmakeProjectManagerPluginPrivate::runQMake()
{
runQMakeImpl(SessionManager::startupProject(), nullptr);
}
void QmakeProjectManagerPluginPrivate::runQMakeContextMenu()
{
runQMakeImpl(ProjectTree::currentProject(), ProjectTree::currentNode());
}
void QmakeProjectManagerPluginPrivate::runQMakeImpl(Project *p, Node *node)
{
if (!ProjectExplorerPlugin::saveModifiedFiles())
return;
auto *qmakeProject = qobject_cast<QmakeProject *>(p);
QTC_ASSERT(qmakeProject, return);
if (!qmakeProject->activeTarget() || !qmakeProject->activeTarget()->activeBuildConfiguration())
return;
auto *bc = static_cast<QmakeBuildConfiguration *>(qmakeProject->activeTarget()->activeBuildConfiguration());
QMakeStep *qs = bc->qmakeStep();
if (!qs)
return;
//found qmakeStep, now use it
qs->setForced(true);
if (node && node != qmakeProject->rootProjectNode())
if (auto *profile = dynamic_cast<QmakeProFileNode *>(node))
bc->setSubNodeBuild(profile);
BuildManager::appendStep(qs, tr("QMake"));
bc->setSubNodeBuild(nullptr);
}
void QmakeProjectManagerPluginPrivate::buildSubDirContextMenu()
{
handleSubDirContextMenu(BUILD, false);
}
void QmakeProjectManagerPluginPrivate::cleanSubDirContextMenu()
{
handleSubDirContextMenu(CLEAN, false);
}
void QmakeProjectManagerPluginPrivate::rebuildSubDirContextMenu()
{
handleSubDirContextMenu(REBUILD, false);
}
void QmakeProjectManagerPluginPrivate::buildFileContextMenu()
{
handleSubDirContextMenu(BUILD, true);
}
void QmakeProjectManagerPluginPrivate::buildFile()
{
if (Core::IDocument *currentDocument= Core::EditorManager::currentDocument()) {
const Utils::FilePath file = currentDocument->filePath();
Node *n = ProjectTree::nodeForFile(file);
FileNode *node = n ? n->asFileNode() : nullptr;
Project *project = SessionManager::projectForFile(file);
if (project && node)
handleSubDirContextMenu(BUILD, true, project, node->parentProjectNode(), node);
}
}
void QmakeProjectManagerPlugin::buildProduct(Project *project, Node *proFileNode)
{
QmakeProjectManagerPluginPrivate::handleSubDirContextMenu(
QmakeProjectManagerPluginPrivate::BUILD, false, project, proFileNode, nullptr);
}
void QmakeProjectManagerPluginPrivate::handleSubDirContextMenu(Action action, bool isFileBuild)
{
Node *node = ProjectTree::currentNode();
QmakeProFileNode *subProjectNode = buildableFileProFile(node);
FileNode *fileNode = node ? node->asFileNode() : nullptr;
bool buildFilePossible = subProjectNode && fileNode && fileNode->fileType() == FileType::Source;
FileNode *buildableFileNode = buildFilePossible ? fileNode : nullptr;
handleSubDirContextMenu(action,
isFileBuild,
ProjectTree::currentProject(),
buildableFileProFile(ProjectTree::currentNode()),
buildableFileNode);
}
void QmakeProjectManagerPluginPrivate::handleSubDirContextMenu(Action action, bool isFileBuild,
Project *contextProject, Node *contextNode,
FileNode *buildableFile)
{
QTC_ASSERT(contextProject, return);
Target *target = contextProject->activeTarget();
if (!target)
return;
auto *bc = qobject_cast<QmakeBuildConfiguration *>(target->activeBuildConfiguration());
if (!bc)
return;
if (!contextNode || !buildableFile)
isFileBuild = false;
if (auto *prifile = dynamic_cast<QmakePriFileNode *>(contextNode)) {
if (QmakeProFileNode *profile = prifile->proFileNode()) {
if (profile != contextProject->rootProjectNode() || isFileBuild)
bc->setSubNodeBuild(profile->proFileNode());
}
}
if (isFileBuild)
bc->setFileNodeBuild(buildableFile);
if (ProjectExplorerPlugin::saveModifiedFiles()) {
const Core::Id buildStep = ProjectExplorer::Constants::BUILDSTEPS_BUILD;
const Core::Id cleanStep = ProjectExplorer::Constants::BUILDSTEPS_CLEAN;
if (action == BUILD) {
BuildManager::buildList(bc->buildSteps());
} else if (action == CLEAN) {
BuildManager::buildList(bc->cleanSteps());
} else if (action == REBUILD) {
QStringList names;
names << ProjectExplorerPlugin::displayNameForStepId(cleanStep)
<< ProjectExplorerPlugin::displayNameForStepId(buildStep);
BuildManager::buildLists({bc->cleanSteps(), bc->buildSteps()}, names);
}
}
bc->setSubNodeBuild(nullptr);
bc->setFileNodeBuild(nullptr);
}
void QmakeProjectManagerPluginPrivate::activeTargetChanged()
{
if (m_previousTarget)
disconnect(m_previousTarget, &Target::activeBuildConfigurationChanged,
this, &QmakeProjectManagerPluginPrivate::updateRunQMakeAction);
m_previousTarget = m_previousStartupProject ? m_previousStartupProject->activeTarget() : nullptr;
if (m_previousTarget) {
connect(m_previousTarget, &Target::activeBuildConfigurationChanged,
this, &QmakeProjectManagerPluginPrivate::updateRunQMakeAction);
connect(m_previousTarget, &Target::parsingFinished,
this, &QmakeProjectManagerPluginPrivate::updateActions);
}
updateRunQMakeAction();
}
void QmakeProjectManagerPluginPrivate::updateActions()
{
updateRunQMakeAction();
updateContextActions();
}
void QmakeProjectManagerPluginPrivate::updateRunQMakeAction()
2008-12-02 12:01:29 +01:00
{
bool enable = true;
if (BuildManager::isBuilding(m_previousStartupProject))
enable = false;
auto pro = qobject_cast<QmakeProject *>(m_previousStartupProject);
m_runQMakeAction->setVisible(pro);
if (!pro
|| !pro->rootProjectNode()
|| !pro->activeTarget()
|| !pro->activeTarget()->activeBuildConfiguration())
enable = false;
m_runQMakeAction->setEnabled(enable);
2008-12-02 12:01:29 +01:00
}
void QmakeProjectManagerPluginPrivate::updateContextActions()
{
const Node *node = ProjectTree::currentNode();
Project *project = ProjectTree::currentProject();
const ContainerNode *containerNode = node ? node->asContainerNode() : nullptr;
const auto *proFileNode = dynamic_cast<const QmakeProFileNode *>(containerNode ? containerNode->rootProjectNode() : node);
m_addLibraryActionContextMenu->setEnabled(proFileNode);
auto *qmakeProject = qobject_cast<QmakeProject *>(project);
QmakeProFileNode *subProjectNode = nullptr;
disableBuildFileMenus();
if (node) {
auto subPriFileNode = dynamic_cast<const QmakePriFileNode *>(node);
if (!subPriFileNode)
subPriFileNode = dynamic_cast<QmakePriFileNode *>(node->parentProjectNode());
subProjectNode = subPriFileNode ? subPriFileNode->proFileNode() : nullptr;
if (const FileNode *fileNode = node->asFileNode())
enableBuildFileMenus(fileNode->filePath());
}
bool subProjectActionsVisible = false;
if (qmakeProject && subProjectNode) {
if (ProjectNode *rootNode = qmakeProject->rootProjectNode())
subProjectActionsVisible = subProjectNode != rootNode;
}
QString subProjectName;
if (subProjectActionsVisible)
subProjectName = subProjectNode->displayName();
m_buildSubProjectAction->setParameter(subProjectName);
m_rebuildSubProjectAction->setParameter(subProjectName);
m_cleanSubProjectAction->setParameter(subProjectName);
m_buildSubProjectContextMenu->setParameter(proFileNode ? proFileNode->displayName() : QString());
auto buildConfiguration = (qmakeProject && qmakeProject->activeTarget()) ?
static_cast<QmakeBuildConfiguration *>(qmakeProject->activeTarget()->activeBuildConfiguration()) : nullptr;
bool isProjectNode = qmakeProject && proFileNode && buildConfiguration;
bool isBuilding = BuildManager::isBuilding(project);
bool enabled = subProjectActionsVisible && !isBuilding;
m_buildSubProjectAction->setVisible(subProjectActionsVisible);
m_rebuildSubProjectAction->setVisible(subProjectActionsVisible);
m_cleanSubProjectAction->setVisible(subProjectActionsVisible);
m_buildSubProjectContextMenu->setVisible(subProjectActionsVisible && isProjectNode);
m_subProjectRebuildSeparator->setVisible(subProjectActionsVisible && isProjectNode);
m_rebuildSubProjectContextMenu->setVisible(subProjectActionsVisible && isProjectNode);
m_cleanSubProjectContextMenu->setVisible(subProjectActionsVisible && isProjectNode);
m_buildSubProjectAction->setEnabled(enabled);
m_rebuildSubProjectAction->setEnabled(enabled);
m_cleanSubProjectAction->setEnabled(enabled);
m_buildSubProjectContextMenu->setEnabled(enabled && isProjectNode);
m_rebuildSubProjectContextMenu->setEnabled(enabled && isProjectNode);
m_cleanSubProjectContextMenu->setEnabled(enabled && isProjectNode);
m_runQMakeActionContextMenu->setEnabled(isProjectNode && !isBuilding
&& buildConfiguration->qmakeStep());
}
void QmakeProjectManagerPluginPrivate::buildStateChanged(Project *pro)
2008-12-02 12:01:29 +01:00
{
if (pro == ProjectTree::currentProject()) {
updateRunQMakeAction();
updateContextActions();
updateBuildFileAction();
}
}
void QmakeProjectManagerPluginPrivate::updateBuildFileAction()
{
disableBuildFileMenus();
if (IDocument *currentDocument = EditorManager::currentDocument())
enableBuildFileMenus(currentDocument->filePath());
}
void QmakeProjectManagerPluginPrivate::disableBuildFileMenus()
{
m_buildFileAction->setVisible(false);
m_buildFileAction->setEnabled(false);
m_buildFileAction->setParameter(QString());
m_buildFileContextMenu->setEnabled(false);
}
void QmakeProjectManagerPluginPrivate::enableBuildFileMenus(const Utils::FilePath &file)
{
bool visible = false;
bool enabled = false;
if (Node *node = ProjectTree::nodeForFile(file)) {
if (Project *project = SessionManager::projectForFile(file)) {
if (const FileNode *fileNode = node->asFileNode()) {
const FileType type = fileNode->fileType();
visible = qobject_cast<QmakeProject *>(project)
&& dynamic_cast<QmakePriFileNode *>(node->parentProjectNode())
&& (type == FileType::Source || type == FileType::Header);
enabled = !BuildManager::isBuilding(project);
m_buildFileAction->setParameter(file.fileName());
}
}
}
m_buildFileAction->setVisible(visible);
m_buildFileAction->setEnabled(enabled);
m_buildFileContextMenu->setEnabled(visible && enabled);
2008-12-02 12:01:29 +01:00
}
} // Internal
} // QmakeProjectManager