qmlprojectrunconfiguration.cpp 11.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 54 55
const char * const M_CURRENT_FILE  = "CurrentFile";

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),
70
    m_qmlViewerArgs(source->m_qmlViewerArgs),
71
    m_projectTarget(parent),
72
    m_userEnvironmentChanges(source->m_userEnvironmentChanges)
Kai Koehne's avatar
Kai Koehne committed
73
{
74 75
    ctor();
    setMainScript(source->m_scriptFile);
76
    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
77 78
}

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

83
    return m_isEnabled;
84 85
}

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

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

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

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

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

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

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

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

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

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

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

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

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

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

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

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

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

    return version;
}

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

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

Kai Koehne's avatar
Kai Koehne committed
194 195
QString QmlProjectRunConfiguration::mainScript() const
{
196 197
    if (m_usingCurrentFile)
        return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
198

199
    return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
200 201 202 203 204
}

void QmlProjectRunConfiguration::setMainScript(const QString &scriptFile)
{
    m_scriptFile = scriptFile;
205 206 207
    // replace with locale-agnostic string
    if (m_scriptFile == CURRENT_FILE)
        m_scriptFile = M_CURRENT_FILE;
208

209
    if (m_scriptFile.isEmpty() || m_scriptFile == M_CURRENT_FILE) {
210 211 212 213 214
        m_usingCurrentFile = true;
        changeCurrentFile(Core::EditorManager::instance()->currentEditor());
    } else {
        m_usingCurrentFile = false;
        m_mainScriptFilename = qmlTarget()->qmlProject()->projectDir().absoluteFilePath(scriptFile);
215
        updateEnabled();
216
    }
Kai Koehne's avatar
Kai Koehne committed
217 218
}

219 220 221 222 223 224 225
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

Kai Koehne's avatar
Kai Koehne committed
226 227 228 229
QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());

230
    map.insert(QLatin1String(Constants::QML_VIEWER_QT_KEY), m_qtVersionId);
Kai Koehne's avatar
Kai Koehne committed
231 232
    map.insert(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY), m_qmlViewerArgs);
    map.insert(QLatin1String(Constants::QML_MAINSCRIPT_KEY),  m_scriptFile);
233 234
    map.insert(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY),
               Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
Kai Koehne's avatar
Kai Koehne committed
235 236 237 238 239
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
240
    setQtVersionId(map.value(QLatin1String(Constants::QML_VIEWER_QT_KEY), -1).toInt());
Kai Koehne's avatar
Kai Koehne committed
241
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
242
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
243 244 245
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

246 247

    updateQtVersions();
248
    setMainScript(m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
249 250 251 252

    return RunConfiguration::fromMap(map);
}

253 254 255 256 257 258
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor * /*editor*/)
{
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
259
{
260
    bool qmlFileFound = false;
261
    if (m_usingCurrentFile) {
262
        Core::IEditor *editor = Core::EditorManager::instance()->currentEditor();
Kai Koehne's avatar
Kai Koehne committed
263
        Core::MimeDatabase *db = ICore::instance()->mimeDatabase();
264 265
        if (editor) {
            m_currentFileFilename = editor->file()->fileName();
Kai Koehne's avatar
Kai Koehne committed
266
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
267
                qmlFileFound = true;
268 269
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
270 271 272
            || 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.
273
            foreach(const QString &filename, m_projectTarget->qmlProject()->files()) {
274 275 276
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
Kai Koehne's avatar
Kai Koehne committed
277
                    && db->findByFile(fi).type() == QLatin1String("application/x-qml"))
278 279
                {
                    m_currentFileFilename = filename;
280
                    qmlFileFound = true;
281 282 283 284
                    break;
                }

            }
285
        }
286 287 288 289
    } else { // use default one
        qmlFileFound = !m_mainScriptFilename.isEmpty();
    }

290 291
    bool newValue = (QFileInfo(viewerPath()).exists()
                    || QFileInfo(observerPath()).exists()) && qmlFileFound;
292

293 294 295 296

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

299
void QmlProjectRunConfiguration::updateQtVersions()
300
{
Kai Koehne's avatar
Kai Koehne committed
301
    QtVersionManager *qtVersions = QtVersionManager::instance();
302 303 304 305 306 307

    //
    // update m_qtVersionId
    //
    if (!qtVersions->isValidId(m_qtVersionId)
            || !isValidVersion(qtVersions->version(m_qtVersionId))) {
308
        int newVersionId = -1;
309 310 311
        // take first one you find
        foreach (Qt4ProjectManager::QtVersion *version, qtVersions->validVersions()) {
            if (isValidVersion(version)) {
312
                newVersionId = version->uniqueId();
313 314
                break;
            }
315
        }
316
        setQtVersionId(newVersionId);
317 318
    }

319 320 321 322 323 324
    updateEnabled();
}

bool QmlProjectRunConfiguration::isValidVersion(Qt4ProjectManager::QtVersion *version)
{
    if (version
325 326
            && (version->supportsTargetId(Qt4ProjectManager::Constants::DESKTOP_TARGET_ID)
                || version->supportsTargetId(Qt4ProjectManager::Constants::QT_SIMULATOR_TARGET_ID))
327 328 329 330
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
331 332
}

333 334 335
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
336 337
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    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
356
} // namespace QmlProjectManager