qmlprojectrunconfiguration.cpp 10.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Kai Koehne's avatar
Kai Koehne committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Kai Koehne's avatar
Kai Koehne committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
Kai Koehne's avatar
Kai Koehne committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
29

dt's avatar
dt committed
30
#include "qmlprojectrunconfiguration.h"
Kai Koehne's avatar
Kai Koehne committed
31 32
#include "qmlproject.h"
#include "qmlprojectmanagerconstants.h"
33
#include "qmlprojectrunconfigurationwidget.h"
34
#include "qmlprojectenvironmentaspect.h"
35
#include <coreplugin/mimedatabase.h>
Kai Koehne's avatar
Kai Koehne committed
36 37 38
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
Tobias Hunger's avatar
Tobias Hunger committed
39
#include <projectexplorer/target.h>
40
#include <utils/qtcprocess.h>
Tobias Hunger's avatar
Tobias Hunger committed
41
#include <qtsupport/qtkitinformation.h>
42 43
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtsupportconstants.h>
Kai Koehne's avatar
Kai Koehne committed
44

45
#ifdef Q_OS_WIN
46
#include <utils/winutils.h>
47 48
#endif

hjk's avatar
hjk committed
49
using namespace Core;
Kai Koehne's avatar
Kai Koehne committed
50
using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
51 52 53

namespace QmlProjectManager {

hjk's avatar
hjk committed
54
const char M_CURRENT_FILE[] = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
55

hjk's avatar
hjk committed
56
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent, Id id) :
57
    ProjectExplorer::LocalApplicationRunConfiguration(parent, id),
58
    m_scriptFile(QLatin1String(M_CURRENT_FILE)),
59
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
60 61
{
    ctor();
Kai Koehne's avatar
Kai Koehne committed
62
}
Kai Koehne's avatar
Kai Koehne committed
63

Tobias Hunger's avatar
Tobias Hunger committed
64
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent,
Kai Koehne's avatar
Kai Koehne committed
65
                                                       QmlProjectRunConfiguration *source) :
66
    ProjectExplorer::LocalApplicationRunConfiguration(parent, source),
Kai Koehne's avatar
Kai Koehne committed
67
    m_scriptFile(source->m_scriptFile),
68
    m_qmlViewerArgs(source->m_qmlViewerArgs),
69
    m_isEnabled(source->m_isEnabled)
Kai Koehne's avatar
Kai Koehne committed
70
{
71
    ctor();
Kai Koehne's avatar
Kai Koehne committed
72 73
}

74
bool QmlProjectRunConfiguration::isEnabled() const
75
{
76
    return m_isEnabled;
77 78
}

dt_'s avatar
dt_ committed
79 80 81
QString QmlProjectRunConfiguration::disabledReason() const
{
    if (!m_isEnabled)
82
        return tr("No qmlviewer or qmlscene found.");
dt_'s avatar
dt_ committed
83 84 85
    return QString();
}

Kai Koehne's avatar
Kai Koehne committed
86 87
void QmlProjectRunConfiguration::ctor()
{
88
    // reset default settings in constructor
hjk's avatar
hjk committed
89
    connect(EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
90 91
            this, SLOT(changeCurrentFile(Core::IEditor*)));

Tobias Hunger's avatar
Tobias Hunger committed
92
    connect(target(), SIGNAL(kitChanged()),
Tobias Hunger's avatar
Tobias Hunger committed
93
            this, SLOT(updateEnabled()));
94

95 96 97 98
    if (id() == Constants::QML_SCENE_RC_ID)
        setDisplayName(tr("QML Scene", "QMLRunConfiguration display name."));
    else
        setDisplayName(tr("QML Viewer", "QMLRunConfiguration display name."));
Kai Koehne's avatar
Kai Koehne committed
99

100
    addExtraAspect(new QmlProjectEnvironmentAspect(this));
Kai Koehne's avatar
Kai Koehne committed
101 102
}

103
QString QmlProjectRunConfiguration::executable() const
104
{
105
    QtSupport::BaseQtVersion *version = qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
106
    if (!version)
107
        return QString();
108 109 110

    if (id() == Constants::QML_SCENE_RC_ID)
        return version->qmlsceneCommand();
111
    if (!version->needsQmlDebuggingLibrary())
112
        return version->qmlviewerCommand();
113 114
    return version->qmlObserverTool();

115 116
}

117
ProjectExplorer::LocalApplicationRunConfiguration::RunMode QmlProjectRunConfiguration::runMode() const
Kai Koehne's avatar
Kai Koehne committed
118
{
119
    return Gui;
Kai Koehne's avatar
Kai Koehne committed
120 121
}

122
QString QmlProjectRunConfiguration::commandLineArguments() const
Kai Koehne's avatar
Kai Koehne committed
123 124
{
    // arguments in .user file
125
    QString args = m_qmlViewerArgs;
Kai Koehne's avatar
Kai Koehne committed
126 127

    // arguments from .qmlproject file
128
    QmlProject *project = static_cast<QmlProject *>(target()->project());
129
    foreach (const QString &importPath, project->customImportPaths()) {
130
        Utils::QtcProcess::addArg(&args, QLatin1String("-I"));
131
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
132 133
    }

134 135 136
    QString s = mainScript();
    if (!s.isEmpty()) {
        s = canonicalCapsPath(s);
137
        Utils::QtcProcess::addArg(&args, s);
138
    }
Kai Koehne's avatar
Kai Koehne committed
139 140 141
    return args;
}

142 143 144 145 146 147 148 149 150 151
QString QmlProjectRunConfiguration::dumperLibrary() const
{
    return QString();
}

QStringList QmlProjectRunConfiguration::dumperLibraryLocations() const
{
    return QStringList();
}

Kai Koehne's avatar
Kai Koehne committed
152 153
QString QmlProjectRunConfiguration::workingDirectory() const
{
154
    QFileInfo projectFile(target()->project()->projectFilePath());
155
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
156 157
}

158
/* QtDeclarative checks explicitly that the capitalization for any URL / path
159
   is exactly like the capitalization on disk.*/
160 161 162 163
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
    QString canonicalPath = QFileInfo(fileName).canonicalFilePath();

164 165
#if defined(Q_OS_WIN)
    canonicalPath = Utils::normalizePathName(canonicalPath);
166
#endif
Kai Koehne's avatar
Kai Koehne committed
167

168 169 170 171
    return canonicalPath;
}


172
QtSupport::BaseQtVersion *QmlProjectRunConfiguration::qtVersion() const
173
{
Tobias Hunger's avatar
Tobias Hunger committed
174
    return QtSupport::QtKitInformation::qtVersion(target()->kit());
175 176
}

177
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
Kai Koehne's avatar
Kai Koehne committed
178
{
179 180 181
    QTC_ASSERT(m_configurationWidget.isNull(), return m_configurationWidget.data());
    m_configurationWidget = new QmlProjectRunConfigurationWidget(this);
    return m_configurationWidget.data();
Kai Koehne's avatar
Kai Koehne committed
182 183
}

con's avatar
con committed
184
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
185
{
Tobias Hunger's avatar
Tobias Hunger committed
186
    return new QtSupport::QtOutputFormatter(target()->project());
187
}
188

Kai Koehne's avatar
Kai Koehne committed
189 190
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
191
    QmlProject *project = static_cast<QmlProject *>(target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
192
    if (!project->mainFile().isEmpty())
193
        return FileInProjectFile;
Tobias Hunger's avatar
Tobias Hunger committed
194
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
195
        return FileInSettings;
196
    return FileInEditor;
Kai Koehne's avatar
Kai Koehne committed
197 198 199 200 201
}

/**
  Returns absolute path to main script file.
  */
Kai Koehne's avatar
Kai Koehne committed
202 203
QString QmlProjectRunConfiguration::mainScript() const
{
Tobias Hunger's avatar
Tobias Hunger committed
204 205 206 207 208
    QmlProject *project = qobject_cast<QmlProject *>(target()->project());
    if (!project)
        return m_currentFileFilename;
    if (!project->mainFile().isEmpty()) {
        const QString pathInProject = project->mainFile();
209 210 211
        if (QFileInfo(pathInProject).isAbsolute())
            return pathInProject;
        else
Tobias Hunger's avatar
Tobias Hunger committed
212
            return project->projectDir().absoluteFilePath(pathInProject);
Kai Koehne's avatar
Kai Koehne committed
213 214
    }

215
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
216
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
217

218
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
219 220
}

Kai Koehne's avatar
Kai Koehne committed
221 222
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
223
{
224
    if (source == FileInEditor) {
225
        m_scriptFile = QLatin1String(M_CURRENT_FILE);
Kai Koehne's avatar
Kai Koehne committed
226 227 228 229 230 231 232
        m_mainScriptFilename.clear();
    } else if (source == FileInProjectFile) {
        m_scriptFile.clear();
        m_mainScriptFilename.clear();
    } else { // FileInSettings
        m_scriptFile = settingsPath;
        m_mainScriptFilename
Tobias Hunger's avatar
Tobias Hunger committed
233
                = target()->project()->projectDirectory() + QLatin1Char('/') + m_scriptFile;
234
    }
Kai Koehne's avatar
Kai Koehne committed
235 236 237
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
238 239
}

240 241 242 243
ProjectExplorer::Abi QmlProjectRunConfiguration::abi() const
{
    ProjectExplorer::Abi hostAbi = ProjectExplorer::Abi::hostAbi();
    return ProjectExplorer::Abi(hostAbi.architecture(), hostAbi.os(), hostAbi.osFlavor(),
hjk's avatar
hjk committed
244
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
245 246
}

Kai Koehne's avatar
Kai Koehne committed
247 248 249 250 251 252 253 254 255 256 257 258
QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::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();
259
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), QLatin1String(M_CURRENT_FILE)).toString();
260

261
    if (m_scriptFile == QLatin1String(M_CURRENT_FILE))
Kai Koehne's avatar
Kai Koehne committed
262
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
263
    else if (m_scriptFile.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
264
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
265
    else
Kai Koehne's avatar
Kai Koehne committed
266
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
267 268 269 270

    return RunConfiguration::fromMap(map);
}

hjk's avatar
hjk committed
271
void QmlProjectRunConfiguration::changeCurrentFile(IEditor *editor)
272
{
Tobias Hunger's avatar
Tobias Hunger committed
273
    if (editor)
274
        m_currentFileFilename = editor->document()->filePath();
275 276 277 278
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
279
{
280
    bool qmlFileFound = false;
281
    if (mainScriptSource() == FileInEditor) {
hjk's avatar
hjk committed
282
        IDocument *document = EditorManager::currentDocument();
283 284
        if (document) {
            m_currentFileFilename = document->filePath();
hjk's avatar
hjk committed
285
            if (MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
286
                qmlFileFound = true;
287
        }
288
        if (!document
hjk's avatar
hjk committed
289
                || MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
290 291
            // find a qml file with lowercase filename. This is slow, but only done
            // in initialization/other border cases.
Tobias Hunger's avatar
Tobias Hunger committed
292
            foreach (const QString &filename, target()->project()->files(ProjectExplorer::Project::AllFiles)) {
293 294 295
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
hjk's avatar
hjk committed
296
                        && MimeDatabase::findByFile(fi).type() == QLatin1String("application/x-qml"))
297 298
                {
                    m_currentFileFilename = filename;
299
                    qmlFileFound = true;
300 301 302 303
                    break;
                }

            }
304
        }
305
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
306
        qmlFileFound = !mainScript().isEmpty();
307 308
    }

309
    bool newValue = QFileInfo(executable()).exists() && qmlFileFound;
310

311 312 313

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
314
    emit enabledChanged();
315 316
}

317
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
318 319
{
    if (version
320 321
            && (version->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
                || version->type() == QLatin1String(QtSupport::Constants::SIMULATORQT))
322 323 324 325
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
326 327
}

Kai Koehne's avatar
Kai Koehne committed
328
} // namespace QmlProjectManager