Files
qt-creator/src/plugins/qmlprojectmanager/qmlprojectrunconfiguration.cpp

289 lines
10 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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 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
**
****************************************************************************/
#include "qmlprojectrunconfiguration.h"
#include "qmlproject.h"
#include "qmlprojectmanagerconstants.h"
#include "qmlprojectrunconfigurationwidget.h"
#include "qmlprojectenvironmentaspect.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <projectexplorer/target.h>
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtsupportconstants.h>
#include <qtsupport/desktopqtversion.h>
2010-11-29 09:45:30 +01:00
#include <utils/fileutils.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcprocess.h>
#include <utils/winutils.h>
#include <qmljstools/qmljstoolsconstants.h>
using namespace Core;
using namespace ProjectExplorer;
using namespace QtSupport;
2010-11-29 09:45:30 +01:00
using namespace QmlProjectManager::Internal;
namespace QmlProjectManager {
const char M_CURRENT_FILE[] = "CurrentFile";
2010-11-29 09:45:30 +01:00
QmlProjectRunConfiguration::QmlProjectRunConfiguration(Target *target)
ProjectExplorer/all: Re-organize BuildSteps/{Deploy,Build}Config setup This follow the rough pattern of recent *RunConfigurationFactory changes for build and deploy configurations. - Collapse the two lines of constructors similar to what 890c1906e6fb2ec did for RunConfigurations * Deploy* was purely mechanical * Build* ctors are split in connects() in the ctor body to create "empty shell for clone" etc and build step additions in initialize() functions which are only used in the create() case. -- Allows to collapse the shared 'ctor()' functions, too. - Move FooBuildConfigurationFactory::create() implementations to FooBuildConfiguration() constructor. That was a strange and unneeded ping-pong between factories and objects, and furthermore allows one level less of indirection (and for a later, left out here, some reduction of the FooBuildConfiguration interfaces that were only used to accommodate the *Factory::create() functions. - Most {Build,Deploy}Configuration{,Factory} classes had a canHandle(), but there wasn't one in the base classses. Have one there. - Most canHandle() functions were checking simple restrictions on e.g. project or target types, specify those by setters in the constructors instead and check them in the base canHandle() - clone() is generally replaced by a creation of a "shell object" and a fromMap(source->toMap()), implemented in the base, there are two cases left for Android and Qbs that needed(?) some extra polish - generally use canHandle() in base implementation, instead of doing that in all Derived::canFoo() - as a result, canCreate/create/canClone/clone reimplementations are not needed anymore, keep the base implementation for now (could be inlined into their only users later), but de-virtualize them. - Combine Ios{Preset,DSym}BuildStepFactory. There was only one 'dsym' build step they could create. - Split the 'mangled' id into the ProjectConfiguration subtype specific constant identifier, and a QString extraId() bit. Only maintain the mangled id in saved settings. - Make ProjectConfiguration::m_id a constant member, adapt all constructors of derived classe. Not done in this patch: - Finish possible cosmetic changes on top - Add a way to specify restrictions to supported Qt versions (used in Android/Ios), as the base implementation does not depend on the qtsupport plugin - Combine the QList<X> availableFoo() + createFoo(X) function pairs to somthing like a direct QList<struct { X; std::function<X()>; }> fooCreators() to avoid e.g. the baseId.withSuffix() <-> id.suffixAfter(base) pingpong - Remove the *Factories from the global object pool - Do something about priority(). Falling back to plain qmake in android+qmake setup is not helpful. Change-Id: I2be7d88d554c5aa8b7db8edf5b93278e1ae0112a Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
2017-11-29 12:28:40 +01:00
: RunConfiguration(target, Constants::QML_SCENE_RC_ID)
{
addExtraAspect(new QmlProjectEnvironmentAspect(this));
// reset default settings in constructor
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
this, &QmlProjectRunConfiguration::changeCurrentFile);
connect(EditorManager::instance(), &EditorManager::currentDocumentStateChanged,
this, [this] { changeCurrentFile(); });
connect(target, &Target::kitChanged,
this, &QmlProjectRunConfiguration::updateEnabledState);
m_scriptFile = M_CURRENT_FILE;
setDisplayName(tr("QML Scene", "QMLRunConfiguration display name."));
updateEnabledState();
}
Runnable QmlProjectRunConfiguration::runnable() const
{
StandardRunnable r;
r.executable = executable();
r.commandLineArguments = commandLineArguments();
r.runMode = ApplicationLauncher::Gui;
r.environment = extraAspect<QmlProjectEnvironmentAspect>()->environment();
r.workingDirectory = canonicalCapsPath(target()->project()->projectFilePath()
.toFileInfo().absolutePath());
return r;
}
QString QmlProjectRunConfiguration::disabledReason() const
{
if (mainScript().isEmpty())
return tr("No script file to execute.");
if (!QFileInfo::exists(executable()))
return tr("No qmlscene found.");
return RunConfiguration::disabledReason();
}
QString QmlProjectRunConfiguration::executable() const
{
BaseQtVersion *version = QtKitInformation::qtVersion(target()->kit());
if (!version)
return QString();
QTC_ASSERT(version->type() == QLatin1String(QtSupport::Constants::DESKTOPQT), return QString());
return static_cast<QtSupport::DesktopQtVersion *>(version)->qmlsceneCommand();
}
QString QmlProjectRunConfiguration::commandLineArguments() const
{
// arguments in .user file
QString args = m_qmlViewerArgs;
// arguments from .qmlproject file
QmlProject *project = static_cast<QmlProject *>(target()->project());
foreach (const QString &importPath, project->customImportPaths()) {
Utils::QtcProcess::addArg(&args, QLatin1String("-I"));
Utils::QtcProcess::addArg(&args, importPath);
}
QString s = mainScript();
if (!s.isEmpty()) {
s = canonicalCapsPath(s);
Utils::QtcProcess::addArg(&args, s);
}
return args;
}
/* QtDeclarative checks explicitly that the capitalization for any URL / path
is exactly like the capitalization on disk.*/
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
return Utils::FileUtils::normalizePathName(QFileInfo(fileName).canonicalFilePath());
}
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
{
return new QmlProjectRunConfigurationWidget(this);
}
2011-04-15 12:59:44 +02:00
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
{
return new QtSupport::QtOutputFormatter(target()->project());
}
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
QmlProject *project = static_cast<QmlProject *>(target()->project());
if (!project->mainFile().isEmpty())
return FileInProjectFile;
if (!m_mainScriptFilename.isEmpty())
return FileInSettings;
return FileInEditor;
}
/**
Returns absolute path to main script file.
*/
QString QmlProjectRunConfiguration::mainScript() const
{
QmlProject *project = qobject_cast<QmlProject *>(target()->project());
if (!project)
return m_currentFileFilename;
if (!project->mainFile().isEmpty()) {
const QString pathInProject = project->mainFile();
if (QFileInfo(pathInProject).isAbsolute())
return pathInProject;
else
return project->projectDir().absoluteFilePath(pathInProject);
}
if (!m_mainScriptFilename.isEmpty())
return m_mainScriptFilename;
return m_currentFileFilename;
}
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
const QString &settingsPath)
{
if (source == FileInEditor) {
m_scriptFile = QLatin1String(M_CURRENT_FILE);
m_mainScriptFilename.clear();
} else if (source == FileInProjectFile) {
m_scriptFile.clear();
m_mainScriptFilename.clear();
} else { // FileInSettings
m_scriptFile = settingsPath;
m_mainScriptFilename
= target()->project()->projectDirectory().toString() + QLatin1Char('/') + m_scriptFile;
}
updateEnabledState();
emit scriptSourceChanged();
}
Abi QmlProjectRunConfiguration::abi() const
{
Abi hostAbi = Abi::hostAbi();
return Abi(hostAbi.architecture(), hostAbi.os(), hostAbi.osFlavor(),
Abi::RuntimeQmlFormat, hostAbi.wordWidth());
}
QVariantMap QmlProjectRunConfiguration::toMap() const
{
QVariantMap map(RunConfiguration::toMap());
map.insert(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY), m_qmlViewerArgs);
map.insert(QLatin1String(Constants::QML_MAINSCRIPT_KEY), m_scriptFile);
return map;
}
bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), QLatin1String(M_CURRENT_FILE)).toString();
if (m_scriptFile == QLatin1String(M_CURRENT_FILE))
setScriptSource(FileInEditor);
else if (m_scriptFile.isEmpty())
setScriptSource(FileInProjectFile);
else
setScriptSource(FileInSettings, m_scriptFile);
return RunConfiguration::fromMap(map);
}
void QmlProjectRunConfiguration::changeCurrentFile(IEditor *editor)
{
if (!editor)
editor = EditorManager::currentEditor();
if (editor)
m_currentFileFilename = editor->document()->filePath().toString();
updateEnabledState();
}
void QmlProjectRunConfiguration::updateEnabledState()
{
bool qmlFileFound = false;
if (mainScriptSource() == FileInEditor) {
IDocument *document = EditorManager::currentDocument();
Utils::MimeType mainScriptMimeType = Utils::mimeTypeForFile(mainScript());
if (document) {
m_currentFileFilename = document->filePath().toString();
if (mainScriptMimeType.matchesName(
QLatin1String(ProjectExplorer::Constants::QML_MIMETYPE))
|| mainScriptMimeType.matchesName(
QLatin1String(ProjectExplorer::Constants::QMLUI_MIMETYPE))) {
qmlFileFound = true;
}
}
if (!document
|| mainScriptMimeType.matchesName(QLatin1String(QmlJSTools::Constants::QMLPROJECT_MIMETYPE))) {
2010-11-29 09:45:30 +01:00
// find a qml file with lowercase filename. This is slow, but only done
// in initialization/other border cases.
foreach (const Utils::FileName &filename, target()->project()->files(Project::AllFiles)) {
const QFileInfo fi = filename.toFileInfo();
if (!filename.isEmpty() && fi.baseName()[0].isLower()) {
Utils::MimeType type = Utils::mimeTypeForFile(fi);
if (type.matchesName(QLatin1String(ProjectExplorer::Constants::QML_MIMETYPE))
|| type.matchesName(
QLatin1String(ProjectExplorer::Constants::QMLUI_MIMETYPE))) {
m_currentFileFilename = filename.toString();
qmlFileFound = true;
break;
}
}
}
}
} else { // use default one
qmlFileFound = !mainScript().isEmpty();
}
if (QFileInfo::exists(executable()) && qmlFileFound)
RunConfiguration::updateEnabledState();
else
setEnabled(false);
}
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
{
return version
&& version->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
&& !static_cast<QtSupport::DesktopQtVersion *>(version)->qmlsceneCommand().isEmpty();
}
} // namespace QmlProjectManager