qmlprojectrunconfiguration.cpp 12.2 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

Kai Koehne's avatar
Kai Koehne committed
49 50 51 52 53
using Core::EditorManager;
using Core::ICore;
using Core::IEditor;

using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
54 55 56

namespace QmlProjectManager {

Kai Koehne's avatar
Kai Koehne committed
57
const char * const M_CURRENT_FILE = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
58

59 60 61
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent,
                                                       Core::Id id) :
    ProjectExplorer::RunConfiguration(parent, id),
62
    m_scriptFile(QLatin1String(M_CURRENT_FILE)),
63
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
64 65
{
    ctor();
Kai Koehne's avatar
Kai Koehne committed
66
}
Kai Koehne's avatar
Kai Koehne committed
67

Tobias Hunger's avatar
Tobias Hunger committed
68
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent,
Kai Koehne's avatar
Kai Koehne committed
69
                                                       QmlProjectRunConfiguration *source) :
Kai Koehne's avatar
Kai Koehne committed
70
    ProjectExplorer::RunConfiguration(parent, source),
Kai Koehne's avatar
Kai Koehne committed
71
    m_scriptFile(source->m_scriptFile),
72
    m_qmlViewerArgs(source->m_qmlViewerArgs),
73 74
    m_isEnabled(source->m_isEnabled),
    m_userEnvironmentChanges(source->m_userEnvironmentChanges)
Kai Koehne's avatar
Kai Koehne committed
75
{
76
    ctor();
Kai Koehne's avatar
Kai Koehne committed
77 78
}

79
bool QmlProjectRunConfiguration::isEnabled() const
80
{
81
    return m_isEnabled;
82 83
}

dt_'s avatar
dt_ committed
84 85 86
QString QmlProjectRunConfiguration::disabledReason() const
{
    if (!m_isEnabled)
87
        return tr("No qmlviewer or qmlscene found.");
dt_'s avatar
dt_ committed
88 89 90
    return QString();
}

Kai Koehne's avatar
Kai Koehne committed
91 92
void QmlProjectRunConfiguration::ctor()
{
93
    // reset default settings in constructor
Kai Koehne's avatar
Kai Koehne committed
94
    EditorManager *em = Core::EditorManager::instance();
95 96 97
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(changeCurrentFile(Core::IEditor*)));

Tobias Hunger's avatar
Tobias Hunger committed
98
    connect(target(), SIGNAL(kitChanged()),
Tobias Hunger's avatar
Tobias Hunger committed
99
            this, SLOT(updateEnabled()));
100

101 102 103 104
    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
105

106
    addExtraAspect(new QmlProjectEnvironmentAspect(this));
Kai Koehne's avatar
Kai Koehne committed
107 108
}

109
QString QmlProjectRunConfiguration::viewerPath() const
110
{
111
    QtSupport::BaseQtVersion *version = qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
112
    if (!version)
113
        return QString();
114 115 116

    if (id() == Constants::QML_SCENE_RC_ID)
        return version->qmlsceneCommand();
Tobias Hunger's avatar
Tobias Hunger committed
117
    else
118
        return version->qmlviewerCommand();
119 120
}

121
QString QmlProjectRunConfiguration::observerPath() const
Kai Koehne's avatar
Kai Koehne committed
122
{
123
    QtSupport::BaseQtVersion *version = qtVersion();
124
    if (!version) {
125
        return QString();
126
    } else {
127 128
        if (id() == Constants::QML_SCENE_RC_ID)
            return version->qmlsceneCommand();
129 130
        if (!version->needsQmlDebuggingLibrary())
            return version->qmlviewerCommand();
131 132
        return version->qmlObserverTool();
    }
Kai Koehne's avatar
Kai Koehne committed
133 134
}

135
QString QmlProjectRunConfiguration::viewerArguments() const
Kai Koehne's avatar
Kai Koehne committed
136 137
{
    // arguments in .user file
138
    QString args = m_qmlViewerArgs;
Kai Koehne's avatar
Kai Koehne committed
139 140

    // arguments from .qmlproject file
141
    QmlProject *project = static_cast<QmlProject *>(target()->project());
142
    foreach (const QString &importPath, project->customImportPaths()) {
143
        Utils::QtcProcess::addArg(&args, QLatin1String("-I"));
144
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
145 146
    }

147 148 149
    QString s = mainScript();
    if (!s.isEmpty()) {
        s = canonicalCapsPath(s);
150
        Utils::QtcProcess::addArg(&args, s);
151
    }
Kai Koehne's avatar
Kai Koehne committed
152 153 154 155 156
    return args;
}

QString QmlProjectRunConfiguration::workingDirectory() const
{
Tobias Hunger's avatar
Tobias Hunger committed
157
    QFileInfo projectFile(target()->project()->document()->fileName());
158
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
159 160
}

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

167 168
#if defined(Q_OS_WIN)
    canonicalPath = Utils::normalizePathName(canonicalPath);
169
#endif
Kai Koehne's avatar
Kai Koehne committed
170

171 172 173 174
    return canonicalPath;
}


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

180
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
Kai Koehne's avatar
Kai Koehne committed
181
{
182 183 184
    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
185 186
}

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

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

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

218
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
219
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
220

221
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
222 223
}

Kai Koehne's avatar
Kai Koehne committed
224 225
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
226
{
227
    if (source == FileInEditor) {
228
        m_scriptFile = QLatin1String(M_CURRENT_FILE);
Kai Koehne's avatar
Kai Koehne committed
229 230 231 232 233 234 235
        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
236
                = target()->project()->projectDirectory() + QLatin1Char('/') + m_scriptFile;
237
    }
Kai Koehne's avatar
Kai Koehne committed
238 239 240
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
241 242
}

243 244 245 246 247 248 249
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

250 251 252 253
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
254
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
255 256
}

Kai Koehne's avatar
Kai Koehne committed
257 258 259 260 261 262
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);
263 264
    map.insert(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY),
               Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
Kai Koehne's avatar
Kai Koehne committed
265 266 267 268 269 270
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
271
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), QLatin1String(M_CURRENT_FILE)).toString();
272 273 274
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

275
    if (m_scriptFile == QLatin1String(M_CURRENT_FILE))
Kai Koehne's avatar
Kai Koehne committed
276
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
277
    else if (m_scriptFile.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
278
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
279
    else
Kai Koehne's avatar
Kai Koehne committed
280
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
281 282 283 284

    return RunConfiguration::fromMap(map);
}

285
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
286
{
Tobias Hunger's avatar
Tobias Hunger committed
287
    if (editor)
288
        m_currentFileFilename = editor->document()->fileName();
289 290 291 292
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
293
{
294
    bool qmlFileFound = false;
295
    if (mainScriptSource() == FileInEditor) {
hjk's avatar
hjk committed
296
        Core::IEditor *editor = Core::EditorManager::currentEditor();
hjk's avatar
hjk committed
297
        Core::MimeDatabase *db = ICore::mimeDatabase();
298
        if (editor) {
299
            m_currentFileFilename = editor->document()->fileName();
Kai Koehne's avatar
Kai Koehne committed
300
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
301
                qmlFileFound = true;
302 303
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
304
                || db->findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
305 306
            // 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
307
            foreach (const QString &filename, target()->project()->files(ProjectExplorer::Project::AllFiles)) {
308 309 310
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
Kai Koehne's avatar
Kai Koehne committed
311
                        && db->findByFile(fi).type() == QLatin1String("application/x-qml"))
312 313
                {
                    m_currentFileFilename = filename;
314
                    qmlFileFound = true;
315 316 317 318
                    break;
                }

            }
319
        }
320
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
321
        qmlFileFound = !mainScript().isEmpty();
322 323
    }

324
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
325
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
326

327 328 329

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
330
    emit enabledChanged();
331 332
}

333
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
334 335
{
    if (version
336 337
            && (version->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
                || version->type() == QLatin1String(QtSupport::Constants::SIMULATORQT))
338 339 340 341
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
342 343
}

344 345 346
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
347 348
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    return env;
}

void QmlProjectRunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
{
    if (m_userEnvironmentChanges != diff) {
        m_userEnvironmentChanges = diff;
        if (m_configurationWidget)
            m_configurationWidget.data()->userEnvironmentChangesChanged();
    }
}

QList<Utils::EnvironmentItem> QmlProjectRunConfiguration::userEnvironmentChanges() const
{
    return m_userEnvironmentChanges;
}

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