qmlprojectrunconfiguration.cpp 12.2 KB
Newer Older
Kai Koehne's avatar
Kai Koehne committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
Kai Koehne's avatar
Kai Koehne committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

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>
38
#include <utils/qtcassert.h>
39
#include <utils/qtcprocess.h>
40
#include <qt4projectmanager/qtversionmanager.h>
41
#include <qt4projectmanager/qtoutputformatter.h>
42
#include <qt4projectmanager/qt4projectmanagerconstants.h>
Kai Koehne's avatar
Kai Koehne committed
43 44 45 46 47 48 49

using Core::EditorManager;
using Core::ICore;
using Core::IEditor;
using Qt4ProjectManager::QtVersionManager;

using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
50 51 52

namespace QmlProjectManager {

Kai Koehne's avatar
Kai Koehne committed
53
const char * const M_CURRENT_FILE = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
54 55

QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent) :
Kai Koehne's avatar
Kai Koehne committed
56
    ProjectExplorer::RunConfiguration(parent, QLatin1String(Constants::QML_RC_ID)),
57
    m_qtVersionId(-1),
58
    m_projectTarget(parent),
59
    m_usingCurrentFile(true),
60
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
61 62
{
    ctor();
63
    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
64 65
}

Kai Koehne's avatar
Kai Koehne committed
66 67
QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent,
                                                       QmlProjectRunConfiguration *source) :
Kai Koehne's avatar
Kai Koehne committed
68
    ProjectExplorer::RunConfiguration(parent, source),
69
    m_qtVersionId(source->m_qtVersionId),
Kai Koehne's avatar
Kai Koehne committed
70
    m_scriptFile(source->m_scriptFile),
71
    m_qmlViewerArgs(source->m_qmlViewerArgs),
72
    m_projectTarget(parent),
Kai Koehne's avatar
Kai Koehne committed
73
    m_usingCurrentFile(source->m_usingCurrentFile),
74
    m_userEnvironmentChanges(source->m_userEnvironmentChanges)
Kai Koehne's avatar
Kai Koehne committed
75
{
76
    ctor();
77
    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
78 79
}

80 81 82 83
bool QmlProjectRunConfiguration::isEnabled(ProjectExplorer::BuildConfiguration *bc) const
{
    Q_UNUSED(bc);

84
    return m_isEnabled;
85 86
}

Kai Koehne's avatar
Kai Koehne committed
87 88
void QmlProjectRunConfiguration::ctor()
{
89 90 91 92
    // reset default settings in constructor
    setUseCppDebugger(false);
    setUseQmlDebugger(true);

Kai Koehne's avatar
Kai Koehne committed
93
    EditorManager *em = Core::EditorManager::instance();
94 95 96
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(changeCurrentFile(Core::IEditor*)));

Kai Koehne's avatar
Kai Koehne committed
97
    QtVersionManager *qtVersions = QtVersionManager::instance();
98
    connect(qtVersions, SIGNAL(qtVersionsChanged(QList<int>)), this, SLOT(updateQtVersions()));
99

100
    setDisplayName(tr("QML Viewer", "QMLRunConfiguration display name."));
Kai Koehne's avatar
Kai Koehne committed
101 102 103 104 105 106
}

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

Kai Koehne's avatar
Kai Koehne committed
107
QmlProjectTarget *QmlProjectRunConfiguration::qmlTarget() const
Kai Koehne's avatar
Kai Koehne committed
108
{
Kai Koehne's avatar
Kai Koehne committed
109
    return static_cast<QmlProjectTarget *>(target());
Kai Koehne's avatar
Kai Koehne committed
110 111
}

112
QString QmlProjectRunConfiguration::viewerPath() const
113
{
114 115
    Qt4ProjectManager::QtVersion *version = qtVersion();
    if (!version) {
116
        return QString();
117 118 119
    } else {
        return version->qmlviewerCommand();
    }
120 121
}

122
QString QmlProjectRunConfiguration::observerPath() const
Kai Koehne's avatar
Kai Koehne committed
123
{
124 125
    Qt4ProjectManager::QtVersion *version = qtVersion();
    if (!version) {
126
        return QString();
127 128 129
    } else {
        return version->qmlObserverTool();
    }
Kai Koehne's avatar
Kai Koehne committed
130 131
}

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

    // arguments from .qmlproject file
138
    foreach (const QString &importPath, qmlTarget()->qmlProject()->importPaths()) {
139 140
        Utils::QtcProcess::addArg(&args, "-I");
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
141 142
    }

143 144 145
    const QString &s = mainScript();
    if (!s.isEmpty())
        Utils::QtcProcess::addArg(&args, s);
Kai Koehne's avatar
Kai Koehne committed
146 147 148 149 150 151 152 153 154
    return args;
}

QString QmlProjectRunConfiguration::workingDirectory() const
{
    QFileInfo projectFile(qmlTarget()->qmlProject()->file()->fileName());
    return projectFile.absolutePath();
}

155 156 157 158 159
int QmlProjectRunConfiguration::qtVersionId() const
{
    return m_qtVersionId;
}

160 161 162 163 164 165 166
void QmlProjectRunConfiguration::setQtVersionId(int id)
{
    if (m_qtVersionId == id)
        return;

    m_qtVersionId = id;
    qmlTarget()->qmlProject()->refresh(QmlProject::Configuration);
167 168
    if (m_configurationWidget)
        m_configurationWidget.data()->updateQtVersionComboBox();
169 170
}

171 172 173 174 175
Qt4ProjectManager::QtVersion *QmlProjectRunConfiguration::qtVersion() const
{
    if (m_qtVersionId == -1)
        return 0;

Kai Koehne's avatar
Kai Koehne committed
176
    QtVersionManager *versionManager = QtVersionManager::instance();
177 178 179 180 181 182
    Qt4ProjectManager::QtVersion *version = versionManager->version(m_qtVersionId);
    QTC_ASSERT(version, return 0);

    return version;
}

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

190 191 192 193
ProjectExplorer::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
{
    return new Qt4ProjectManager::QtOutputFormatter(qmlTarget()->qmlProject());
}
194

Kai Koehne's avatar
Kai Koehne committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
    if (m_usingCurrentFile) {
        return FileInEditor;
    }
    if (!m_mainScriptFilename.isEmpty()) {
        return FileInSettings;
    }
    return FileInProjectFile;
}

/**
  Returns absolute path to main script file.
  */
Kai Koehne's avatar
Kai Koehne committed
209 210
QString QmlProjectRunConfiguration::mainScript() const
{
Kai Koehne's avatar
Kai Koehne committed
211
    if (m_usingCurrentFile) {
212
        return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
213 214 215 216 217
    }

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

Kai Koehne's avatar
Kai Koehne committed
219 220 221 222 223 224
    QString path = qmlTarget()->qmlProject()->mainFile();
    if (QFileInfo(path).isAbsolute()) {
        return path;
    } else {
        return qmlTarget()->qmlProject()->projectDir().absoluteFilePath(path);
    }
Kai Koehne's avatar
Kai Koehne committed
225 226
}

Kai Koehne's avatar
Kai Koehne committed
227 228
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
229
{
Kai Koehne's avatar
Kai Koehne committed
230 231
    if (source == FileInEditor) { m_scriptFile.clear();
        m_mainScriptFilename.clear();
232
        m_usingCurrentFile = true;
Kai Koehne's avatar
Kai Koehne committed
233 234 235 236 237 238 239 240
    } else if (source == FileInProjectFile) {
        m_scriptFile.clear();
        m_mainScriptFilename.clear();
        m_usingCurrentFile = false;
    } else { // FileInSettings
        m_scriptFile = settingsPath;
        m_mainScriptFilename
                = qmlTarget()->qmlProject()->projectDir().absoluteFilePath(m_scriptFile);
241 242
        m_usingCurrentFile = false;
    }
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;
}

Kai Koehne's avatar
Kai Koehne committed
255 256 257 258
QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());

259
    map.insert(QLatin1String(Constants::QML_VIEWER_QT_KEY), m_qtVersionId);
Kai Koehne's avatar
Kai Koehne committed
260 261
    map.insert(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY), m_qmlViewerArgs);
    map.insert(QLatin1String(Constants::QML_MAINSCRIPT_KEY),  m_scriptFile);
262 263
    map.insert(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY),
               Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
Kai Koehne's avatar
Kai Koehne committed
264 265 266 267 268
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
269
    setQtVersionId(map.value(QLatin1String(Constants::QML_VIEWER_QT_KEY), -1).toInt());
Kai Koehne's avatar
Kai Koehne committed
270
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
271
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
272 273 274
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

275 276

    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
277 278 279 280 281 282 283
    if (m_scriptFile == M_CURRENT_FILE) {
        setScriptSource(FileInEditor);
    } else if (m_scriptFile.isEmpty()) {
        setScriptSource(FileInProjectFile);
    } else {
        setScriptSource(FileInSettings, m_scriptFile);
    }
Kai Koehne's avatar
Kai Koehne committed
284 285 286 287

    return RunConfiguration::fromMap(map);
}

288 289 290 291 292 293
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor * /*editor*/)
{
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
294
{
295
    bool qmlFileFound = false;
296
    if (m_usingCurrentFile) {
297
        Core::IEditor *editor = Core::EditorManager::instance()->currentEditor();
Kai Koehne's avatar
Kai Koehne committed
298
        Core::MimeDatabase *db = ICore::instance()->mimeDatabase();
299 300
        if (editor) {
            m_currentFileFilename = editor->file()->fileName();
Kai Koehne's avatar
Kai Koehne committed
301
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
302
                qmlFileFound = true;
303 304
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
305 306 307
            || db->findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
            // find a qml file with lowercase filename. This is slow, but only done
            // in initialization/other border cases.
308
            foreach(const QString &filename, m_projectTarget->qmlProject()->files()) {
309 310 311
                const QFileInfo fi(filename);

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

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

325 326
    bool newValue = (QFileInfo(viewerPath()).exists()
                    || QFileInfo(observerPath()).exists()) && qmlFileFound;
327

328 329 330 331

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

334
void QmlProjectRunConfiguration::updateQtVersions()
335
{
Kai Koehne's avatar
Kai Koehne committed
336
    QtVersionManager *qtVersions = QtVersionManager::instance();
337 338 339 340 341 342

    //
    // update m_qtVersionId
    //
    if (!qtVersions->isValidId(m_qtVersionId)
            || !isValidVersion(qtVersions->version(m_qtVersionId))) {
343
        int newVersionId = -1;
344 345 346
        // take first one you find
        foreach (Qt4ProjectManager::QtVersion *version, qtVersions->validVersions()) {
            if (isValidVersion(version)) {
347
                newVersionId = version->uniqueId();
348 349
                break;
            }
350
        }
351
        setQtVersionId(newVersionId);
352 353
    }

354 355 356 357 358 359
    updateEnabled();
}

bool QmlProjectRunConfiguration::isValidVersion(Qt4ProjectManager::QtVersion *version)
{
    if (version
360 361
            && (version->supportsTargetId(Qt4ProjectManager::Constants::DESKTOP_TARGET_ID)
                || version->supportsTargetId(Qt4ProjectManager::Constants::QT_SIMULATOR_TARGET_ID))
362 363 364 365
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
366 367
}

368 369 370
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
371 372
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    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
391
} // namespace QmlProjectManager