forked from qt-creator/qt-creator
The correct pattern is this: The actions in the build menu are supposed to be for the startup project. They should use the global context and be manually hidden/shown if the startup project changes. This fixes a crash on assigning keyboard shortcut to the edit files context menu action. The slot connected assumed that the action could only be triggered via the context menu. By using ProjectTree;:currentProject() the code now works even if the project tree is not actually focused. It also fixes that the "Run CMake" action was shown even in the build menu, even though a non cmake project was the startup project. Change-Id: I0bb8086d8b1078b4c71c3b5ba9d7f8596757e724 Task-number: QTCREATORBUG-14728 Task-number: QTCREATORBUG-14768 Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
185 lines
7.5 KiB
C++
185 lines
7.5 KiB
C++
/****************************************************************************
|
|
**
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
** Contact: http://www.qt.io/licensing
|
|
**
|
|
** This file is part of Qt Creator.
|
|
**
|
|
** 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 http://www.qt.io/terms-conditions. For further information
|
|
** use the contact form at http://www.qt.io/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 or version 3 as published by the Free
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
** following information to ensure the GNU Lesser General Public License
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
**
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
**
|
|
****************************************************************************/
|
|
|
|
#include "cmakeprojectmanager.h"
|
|
#include "cmakeopenprojectwizard.h"
|
|
#include "cmakeprojectconstants.h"
|
|
#include "cmakeproject.h"
|
|
#include "cmakesettingspage.h"
|
|
#include "cmaketoolmanager.h"
|
|
|
|
#include <coreplugin/icore.h>
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
#include <coreplugin/actionmanager/command.h>
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
#include <projectexplorer/buildmanager.h>
|
|
#include <projectexplorer/projectexplorer.h>
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
|
#include <projectexplorer/projecttree.h>
|
|
#include <projectexplorer/session.h>
|
|
|
|
#include <utils/synchronousprocess.h>
|
|
|
|
#include <QAction>
|
|
#include <QDateTime>
|
|
|
|
using namespace CMakeProjectManager::Internal;
|
|
|
|
CMakeManager::CMakeManager()
|
|
{
|
|
Core::ActionContainer *mbuild =
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
|
|
Core::ActionContainer *mproject =
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
|
|
Core::ActionContainer *msubproject =
|
|
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
|
|
|
|
const Core::Context projectContext(CMakeProjectManager::Constants::PROJECTCONTEXT);
|
|
const Core::Context globalcontext(Core::Constants::C_GLOBAL);
|
|
|
|
m_runCMakeAction = new QAction(QIcon(), tr("Run CMake"), this);
|
|
Core::Command *command = Core::ActionManager::registerAction(m_runCMakeAction,
|
|
Constants::RUNCMAKE, globalcontext);
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_DEPLOY);
|
|
connect(m_runCMakeAction, &QAction::triggered, [this]() {
|
|
runCMake(ProjectExplorer::SessionManager::startupProject());
|
|
});
|
|
|
|
m_runCMakeActionContextMenu = new QAction(QIcon(), tr("Run CMake"), this);
|
|
command = Core::ActionManager::registerAction(m_runCMakeActionContextMenu,
|
|
Constants::RUNCMAKECONTEXTMENU, projectContext);
|
|
command->setAttribute(Core::Command::CA_Hide);
|
|
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
|
|
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
|
|
connect(m_runCMakeActionContextMenu, &QAction::triggered, [this]() {
|
|
runCMake(ProjectExplorer::ProjectTree::currentProject());
|
|
});
|
|
|
|
connect(ProjectExplorer::SessionManager::instance(), &ProjectExplorer::SessionManager::startupProjectChanged,
|
|
this, &CMakeManager::updateRunCmakeAction);
|
|
connect(ProjectExplorer::BuildManager::instance(), &ProjectExplorer::BuildManager::buildStateChanged,
|
|
this, &CMakeManager::updateRunCmakeAction);
|
|
|
|
}
|
|
|
|
void CMakeManager::updateRunCmakeAction()
|
|
{
|
|
auto project = qobject_cast<CMakeProject *>(ProjectExplorer::SessionManager::startupProject());
|
|
m_runCMakeAction->setVisible(project && !ProjectExplorer::BuildManager::isBuilding(project));
|
|
}
|
|
|
|
void CMakeManager::runCMake(ProjectExplorer::Project *project)
|
|
{
|
|
if (!project)
|
|
return;
|
|
CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(project);
|
|
if (!cmakeProject || !cmakeProject->activeTarget() || !cmakeProject->activeTarget()->activeBuildConfiguration())
|
|
return;
|
|
|
|
if (!ProjectExplorer::ProjectExplorerPlugin::saveModifiedFiles())
|
|
return;
|
|
|
|
CMakeBuildConfiguration *bc
|
|
= static_cast<CMakeBuildConfiguration *>(cmakeProject->activeTarget()->activeBuildConfiguration());
|
|
|
|
CMakeBuildInfo info(bc);
|
|
|
|
CMakeOpenProjectWizard copw(Core::ICore::mainWindow(), this, CMakeOpenProjectWizard::WantToUpdate, &info);
|
|
if (copw.exec() == QDialog::Accepted)
|
|
cmakeProject->parseCMakeLists();
|
|
}
|
|
|
|
ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName, QString *errorString)
|
|
{
|
|
Utils::FileName file = Utils::FileName::fromString(fileName);
|
|
if (!file.toFileInfo().isFile()) {
|
|
if (errorString)
|
|
*errorString = tr("Failed opening project \"%1\": Project is not a file")
|
|
.arg(file.toUserOutput());
|
|
return 0;
|
|
}
|
|
|
|
return new CMakeProject(this, file);
|
|
}
|
|
|
|
QString CMakeManager::mimeType() const
|
|
{
|
|
return QLatin1String(Constants::CMAKEPROJECTMIMETYPE);
|
|
}
|
|
|
|
bool CMakeManager::preferNinja() const
|
|
{
|
|
return CMakeToolManager::preferNinja();
|
|
}
|
|
|
|
// need to refactor this out
|
|
// we probably want the process instead of this function
|
|
// cmakeproject then could even run the cmake process in the background, adding the files afterwards
|
|
// sounds like a plan
|
|
void CMakeManager::createXmlFile(Utils::QtcProcess *proc, const QString &executable, const QString &arguments,
|
|
const QString &sourceDirectory, const QDir &buildDirectory,
|
|
const Utils::Environment &env, const QString &generator)
|
|
{
|
|
QString buildDirectoryPath = buildDirectory.absolutePath();
|
|
buildDirectory.mkpath(buildDirectoryPath);
|
|
proc->setWorkingDirectory(buildDirectoryPath);
|
|
proc->setEnvironment(env);
|
|
|
|
const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ?
|
|
QString(QLatin1Char('.')) : sourceDirectory;
|
|
QString args;
|
|
Utils::QtcProcess::addArg(&args, srcdir);
|
|
Utils::QtcProcess::addArgs(&args, arguments);
|
|
Utils::QtcProcess::addArg(&args, generator);
|
|
proc->setCommand(executable, args);
|
|
proc->start();
|
|
}
|
|
|
|
QString CMakeManager::findCbpFile(const QDir &directory)
|
|
{
|
|
// Find the cbp file
|
|
// the cbp file is named like the project() command in the CMakeList.txt file
|
|
// so this function below could find the wrong cbp file, if the user changes the project()
|
|
// 2name
|
|
QDateTime t;
|
|
QString file;
|
|
foreach (const QString &cbpFile , directory.entryList()) {
|
|
if (cbpFile.endsWith(QLatin1String(".cbp"))) {
|
|
QFileInfo fi(directory.path() + QLatin1Char('/') + cbpFile);
|
|
if (t.isNull() || fi.lastModified() > t) {
|
|
file = directory.path() + QLatin1Char('/') + cbpFile;
|
|
t = fi.lastModified();
|
|
}
|
|
}
|
|
}
|
|
return file;
|
|
}
|