qmlprojectrunconfiguration.cpp 13.4 KB
Newer Older
Kai Koehne's avatar
Kai Koehne committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
Kai Koehne's avatar
Kai Koehne committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Kai Koehne's avatar
Kai Koehne committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Kai Koehne's avatar
Kai Koehne committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Kai Koehne's avatar
Kai Koehne committed
30 31 32
**
**************************************************************************/

dt's avatar
dt committed
33
#include "qmlprojectrunconfiguration.h"
Kai Koehne's avatar
Kai Koehne committed
34 35
#include "qmlproject.h"
#include "qmlprojectmanagerconstants.h"
Friedemann Kleint's avatar
Friedemann Kleint committed
36
#include "qmlprojecttarget.h"
37
#include "qmlprojectrunconfigurationwidget.h"
38
#include <coreplugin/mimedatabase.h>
Kai Koehne's avatar
Kai Koehne committed
39 40 41
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
42
#include <utils/qtcassert.h>
43
#include <utils/qtcprocess.h>
44 45 46
#include <qtsupport/qtversionmanager.h>
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtsupportconstants.h>
Kai Koehne's avatar
Kai Koehne committed
47

48
#ifdef Q_OS_WIN
49
#include <utils/winutils.h>
50 51
#endif

Kai Koehne's avatar
Kai Koehne committed
52 53 54
using Core::EditorManager;
using Core::ICore;
using Core::IEditor;
55
using QtSupport::QtVersionManager;
Kai Koehne's avatar
Kai Koehne committed
56 57

using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
58 59 60

namespace QmlProjectManager {

Kai Koehne's avatar
Kai Koehne committed
61
const char * const M_CURRENT_FILE = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
62 63

QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent) :
Kai Koehne's avatar
Kai Koehne committed
64
    ProjectExplorer::RunConfiguration(parent, QLatin1String(Constants::QML_RC_ID)),
65
    m_qtVersionId(-1),
66
    m_scriptFile(M_CURRENT_FILE),
67
    m_projectTarget(parent),
68
    m_usingCurrentFile(true),
69
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
70 71
{
    ctor();
72
    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
73
}
Kai Koehne's avatar
Kai Koehne committed
74

Kai Koehne's avatar
Kai Koehne committed
75 76
QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent,
                                                       QmlProjectRunConfiguration *source) :
Kai Koehne's avatar
Kai Koehne committed
77
    ProjectExplorer::RunConfiguration(parent, source),
78
    m_qtVersionId(source->m_qtVersionId),
Kai Koehne's avatar
Kai Koehne committed
79
    m_scriptFile(source->m_scriptFile),
80
    m_qmlViewerArgs(source->m_qmlViewerArgs),
81
    m_projectTarget(parent),
Kai Koehne's avatar
Kai Koehne committed
82
    m_usingCurrentFile(source->m_usingCurrentFile),
83 84
    m_isEnabled(source->m_isEnabled),
    m_userEnvironmentChanges(source->m_userEnvironmentChanges)
Kai Koehne's avatar
Kai Koehne committed
85
{
86
    ctor();
87
    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
88 89
}

90
bool QmlProjectRunConfiguration::isEnabled() const
91
{
92
    return m_isEnabled;
93 94
}

dt_'s avatar
dt_ committed
95 96 97 98 99 100 101
QString QmlProjectRunConfiguration::disabledReason() const
{
    if (!m_isEnabled)
        return tr("No qmlviewer or qmlobserver found.");
    return QString();
}

Kai Koehne's avatar
Kai Koehne committed
102 103
void QmlProjectRunConfiguration::ctor()
{
104 105 106 107
    // reset default settings in constructor
    setUseCppDebugger(false);
    setUseQmlDebugger(true);

Kai Koehne's avatar
Kai Koehne committed
108
    EditorManager *em = Core::EditorManager::instance();
109 110 111
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(changeCurrentFile(Core::IEditor*)));

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

115
    setDisplayName(tr("QML Viewer", "QMLRunConfiguration display name."));
Kai Koehne's avatar
Kai Koehne committed
116 117 118 119 120 121
}

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

Kai Koehne's avatar
Kai Koehne committed
122
QmlProjectTarget *QmlProjectRunConfiguration::qmlTarget() const
Kai Koehne's avatar
Kai Koehne committed
123
{
Kai Koehne's avatar
Kai Koehne committed
124
    return static_cast<QmlProjectTarget *>(target());
Kai Koehne's avatar
Kai Koehne committed
125 126
}

127
QString QmlProjectRunConfiguration::viewerPath() const
128
{
129
    QtSupport::BaseQtVersion *version = qtVersion();
130
    if (!version) {
131
        return QString();
132 133 134
    } else {
        return version->qmlviewerCommand();
    }
135 136
}

137
QString QmlProjectRunConfiguration::observerPath() const
Kai Koehne's avatar
Kai Koehne committed
138
{
139
    QtSupport::BaseQtVersion *version = qtVersion();
140
    if (!version) {
141
        return QString();
142
    } else {
143 144
        if (!version->needsQmlDebuggingLibrary())
            return version->qmlviewerCommand();
145 146
        return version->qmlObserverTool();
    }
Kai Koehne's avatar
Kai Koehne committed
147 148
}

149
QString QmlProjectRunConfiguration::viewerArguments() const
Kai Koehne's avatar
Kai Koehne committed
150 151
{
    // arguments in .user file
152
    QString args = m_qmlViewerArgs;
Kai Koehne's avatar
Kai Koehne committed
153 154

    // arguments from .qmlproject file
155
    foreach (const QString &importPath, qmlTarget()->qmlProject()->importPaths()) {
156 157
        Utils::QtcProcess::addArg(&args, "-I");
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
158 159
    }

160 161 162
    QString s = mainScript();
    if (!s.isEmpty()) {
        s = canonicalCapsPath(s);
163
        Utils::QtcProcess::addArg(&args, s);
164
    }
Kai Koehne's avatar
Kai Koehne committed
165 166 167 168 169 170
    return args;
}

QString QmlProjectRunConfiguration::workingDirectory() const
{
    QFileInfo projectFile(qmlTarget()->qmlProject()->file()->fileName());
171
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
172 173
}

174 175 176 177 178
int QmlProjectRunConfiguration::qtVersionId() const
{
    return m_qtVersionId;
}

179 180 181 182 183 184 185
void QmlProjectRunConfiguration::setQtVersionId(int id)
{
    if (m_qtVersionId == id)
        return;

    m_qtVersionId = id;
    qmlTarget()->qmlProject()->refresh(QmlProject::Configuration);
186 187
    if (m_configurationWidget)
        m_configurationWidget.data()->updateQtVersionComboBox();
188 189
}

190
/* QtDeclarative checks explicitly that the capitalization for any URL / path
191
   is exactly like the capitalization on disk.*/
192 193 194 195
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
    QString canonicalPath = QFileInfo(fileName).canonicalFilePath();

196 197
#if defined(Q_OS_WIN)
    canonicalPath = Utils::normalizePathName(canonicalPath);
198
#endif
Kai Koehne's avatar
Kai Koehne committed
199

200 201 202 203
    return canonicalPath;
}


204
QtSupport::BaseQtVersion *QmlProjectRunConfiguration::qtVersion() const
205 206 207 208
{
    if (m_qtVersionId == -1)
        return 0;

209 210
    QtSupport::QtVersionManager *versionManager = QtSupport::QtVersionManager::instance();
    QtSupport::BaseQtVersion *version = versionManager->version(m_qtVersionId);
211 212 213 214 215
    QTC_ASSERT(version, return 0);

    return version;
}

216
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
Kai Koehne's avatar
Kai Koehne committed
217
{
218 219 220
    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
221 222
}

con's avatar
con committed
223
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
224
{
225
    return new QtSupport::QtOutputFormatter(qmlTarget()->qmlProject());
226
}
227

Kai Koehne's avatar
Kai Koehne committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241
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
242 243
QString QmlProjectRunConfiguration::mainScript() const
{
Kai Koehne's avatar
Kai Koehne committed
244
    if (m_usingCurrentFile) {
245
        return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
246 247 248 249 250
    }

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

252 253 254 255
    const QString path = qmlTarget()->qmlProject()->mainFile();
    if (path.isEmpty()) {
        return m_currentFileFilename;
    }
Kai Koehne's avatar
Kai Koehne committed
256 257 258 259 260
    if (QFileInfo(path).isAbsolute()) {
        return path;
    } else {
        return qmlTarget()->qmlProject()->projectDir().absoluteFilePath(path);
    }
Kai Koehne's avatar
Kai Koehne committed
261 262
}

Kai Koehne's avatar
Kai Koehne committed
263 264
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
265
{
266 267
    if (source == FileInEditor) {
        m_scriptFile = M_CURRENT_FILE;
Kai Koehne's avatar
Kai Koehne committed
268
        m_mainScriptFilename.clear();
269
        m_usingCurrentFile = true;
Kai Koehne's avatar
Kai Koehne committed
270 271 272 273 274 275 276 277
    } 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);
278 279
        m_usingCurrentFile = false;
    }
Kai Koehne's avatar
Kai Koehne committed
280 281 282
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
283 284
}

285 286 287 288 289 290 291
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

292 293 294 295
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
296
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
297 298
}

Kai Koehne's avatar
Kai Koehne committed
299 300 301 302
QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());

303
    map.insert(QLatin1String(Constants::QML_VIEWER_QT_KEY), m_qtVersionId);
Kai Koehne's avatar
Kai Koehne committed
304 305
    map.insert(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY), m_qmlViewerArgs);
    map.insert(QLatin1String(Constants::QML_MAINSCRIPT_KEY),  m_scriptFile);
306 307
    map.insert(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY),
               Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
Kai Koehne's avatar
Kai Koehne committed
308 309 310 311 312
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
313
    setQtVersionId(map.value(QLatin1String(Constants::QML_VIEWER_QT_KEY), -1).toInt());
Kai Koehne's avatar
Kai Koehne committed
314
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
315
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
316 317 318
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

319 320

    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
321 322 323 324 325 326 327
    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
328 329 330 331

    return RunConfiguration::fromMap(map);
}

332
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
333
{
334 335 336
    if (editor) {
        m_currentFileFilename = editor->file()->fileName();
    }
337 338 339 340
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
341
{
342
    bool qmlFileFound = false;
343
    if (m_usingCurrentFile) {
344
        Core::IEditor *editor = Core::EditorManager::instance()->currentEditor();
Kai Koehne's avatar
Kai Koehne committed
345
        Core::MimeDatabase *db = ICore::instance()->mimeDatabase();
346 347
        if (editor) {
            m_currentFileFilename = editor->file()->fileName();
Kai Koehne's avatar
Kai Koehne committed
348
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
349
                qmlFileFound = true;
350 351
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
352
                || db->findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
353 354
            // find a qml file with lowercase filename. This is slow, but only done
            // in initialization/other border cases.
355
            foreach(const QString &filename, m_projectTarget->qmlProject()->files()) {
356 357 358
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
Kai Koehne's avatar
Kai Koehne committed
359
                        && db->findByFile(fi).type() == QLatin1String("application/x-qml"))
360 361
                {
                    m_currentFileFilename = filename;
362
                    qmlFileFound = true;
363 364 365 366
                    break;
                }

            }
367
        }
368
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
369
        qmlFileFound = !mainScript().isEmpty();
370 371
    }

372
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
373
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
374

375 376 377 378

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

381
void QmlProjectRunConfiguration::updateQtVersions()
382
{
Kai Koehne's avatar
Kai Koehne committed
383
    QtVersionManager *qtVersions = QtVersionManager::instance();
384 385 386 387 388 389

    //
    // update m_qtVersionId
    //
    if (!qtVersions->isValidId(m_qtVersionId)
            || !isValidVersion(qtVersions->version(m_qtVersionId))) {
390
        int newVersionId = -1;
391
        // take first one you find
392
        foreach (QtSupport::BaseQtVersion *version, qtVersions->validVersions()) {
393
            if (isValidVersion(version)) {
394
                newVersionId = version->uniqueId();
395 396
                break;
            }
397
        }
398
        setQtVersionId(newVersionId);
399 400
    }

401 402 403
    updateEnabled();
}

404
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
405 406
{
    if (version
407 408
            && (version->type() == QtSupport::Constants::DESKTOPQT
                || version->type() == QtSupport::Constants::SIMULATORQT)
409 410 411 412
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
413 414
}

415 416 417
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
418 419
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    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
437
} // namespace QmlProjectManager