qmlprojectrunconfiguration.cpp 12.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** 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 <coreplugin/mimedatabase.h>
Kai Koehne's avatar
Kai Koehne committed
35 36 37
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
Tobias Hunger's avatar
Tobias Hunger committed
38
#include <projectexplorer/target.h>
39
#include <utils/qtcassert.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(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
94 95
    debuggerAspect()->setUseCppDebugger(false);
    debuggerAspect()->setUseQmlDebugger(true);
96

Kai Koehne's avatar
Kai Koehne committed
97
    EditorManager *em = Core::EditorManager::instance();
98 99 100
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(changeCurrentFile(Core::IEditor*)));

Tobias Hunger's avatar
Tobias Hunger committed
101
    connect(target(), SIGNAL(kitChanged()),
Tobias Hunger's avatar
Tobias Hunger committed
102
            this, SLOT(updateEnabled()));
103

104 105 106 107
    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
108 109 110 111 112 113
}

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

114
QString QmlProjectRunConfiguration::viewerPath() const
115
{
116
    QtSupport::BaseQtVersion *version = qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
117
    if (!version)
118
        return QString();
119 120 121

    if (id() == Constants::QML_SCENE_RC_ID)
        return version->qmlsceneCommand();
Tobias Hunger's avatar
Tobias Hunger committed
122
    else
123
        return version->qmlviewerCommand();
124 125
}

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

140
QString QmlProjectRunConfiguration::viewerArguments() const
Kai Koehne's avatar
Kai Koehne committed
141 142
{
    // arguments in .user file
143
    QString args = m_qmlViewerArgs;
Kai Koehne's avatar
Kai Koehne committed
144 145

    // arguments from .qmlproject file
146
    QmlProject *project = static_cast<QmlProject *>(target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
147
    foreach (const QString &importPath, project->importPaths()) {
148 149
        Utils::QtcProcess::addArg(&args, "-I");
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
150 151
    }

152 153 154
    QString s = mainScript();
    if (!s.isEmpty()) {
        s = canonicalCapsPath(s);
155
        Utils::QtcProcess::addArg(&args, s);
156
    }
Kai Koehne's avatar
Kai Koehne committed
157 158 159 160 161
    return args;
}

QString QmlProjectRunConfiguration::workingDirectory() const
{
Tobias Hunger's avatar
Tobias Hunger committed
162
    QFileInfo projectFile(target()->project()->document()->fileName());
163
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
164 165
}

166
/* QtDeclarative checks explicitly that the capitalization for any URL / path
167
   is exactly like the capitalization on disk.*/
168 169 170 171
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
    QString canonicalPath = QFileInfo(fileName).canonicalFilePath();

172 173
#if defined(Q_OS_WIN)
    canonicalPath = Utils::normalizePathName(canonicalPath);
174
#endif
Kai Koehne's avatar
Kai Koehne committed
175

176 177 178 179
    return canonicalPath;
}


180
QtSupport::BaseQtVersion *QmlProjectRunConfiguration::qtVersion() const
181
{
Tobias Hunger's avatar
Tobias Hunger committed
182
    return QtSupport::QtKitInformation::qtVersion(target()->kit());
183 184
}

185
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
Kai Koehne's avatar
Kai Koehne committed
186
{
187 188 189
    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
190 191
}

con's avatar
con committed
192
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
193
{
Tobias Hunger's avatar
Tobias Hunger committed
194
    return new QtSupport::QtOutputFormatter(target()->project());
195
}
196

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

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

223
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
224
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
225

226
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
227 228
}

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

248 249 250 251 252 253 254
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

255 256 257 258
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
259
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
260 261
}

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

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
276
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
277 278 279
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

Tobias Hunger's avatar
Tobias Hunger committed
280
    if (m_scriptFile == M_CURRENT_FILE)
Kai Koehne's avatar
Kai Koehne committed
281
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
282
    else if (m_scriptFile.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
283
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
284
    else
Kai Koehne's avatar
Kai Koehne committed
285
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
286 287 288 289

    return RunConfiguration::fromMap(map);
}

290
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
291
{
Tobias Hunger's avatar
Tobias Hunger committed
292
    if (editor)
293
        m_currentFileFilename = editor->document()->fileName();
294 295 296 297
    updateEnabled();
}

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

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

            }
324
        }
325
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
326
        qmlFileFound = !mainScript().isEmpty();
327 328
    }

329
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
330
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
331

332 333 334

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
335
    emit enabledChanged();
336 337
}

338
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
339 340
{
    if (version
341 342
            && (version->type() == QtSupport::Constants::DESKTOPQT
                || version->type() == QtSupport::Constants::SIMULATORQT)
343 344 345 346
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
347 348
}

349 350 351
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
352 353
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    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
371
} // namespace QmlProjectManager