qmlprojectrunconfiguration.cpp 13.3 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) 2012 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_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
69 70
{
    ctor();
71
    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
72
}
Kai Koehne's avatar
Kai Koehne committed
73

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

88
bool QmlProjectRunConfiguration::isEnabled() const
89
{
90
    return m_isEnabled;
91 92
}

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

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

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

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

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

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

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

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

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

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

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

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

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

172 173 174 175 176
int QmlProjectRunConfiguration::qtVersionId() const
{
    return m_qtVersionId;
}

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

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

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

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

198 199 200 201
    return canonicalPath;
}


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

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

    return version;
}

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

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

Kai Koehne's avatar
Kai Koehne committed
226 227
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
228 229
    if (!qmlTarget()->qmlProject()->mainFile().isEmpty())
        return FileInProjectFile;
Kai Koehne's avatar
Kai Koehne committed
230 231 232
    if (!m_mainScriptFilename.isEmpty()) {
        return FileInSettings;
    }
233
    return FileInEditor;
Kai Koehne's avatar
Kai Koehne committed
234 235 236 237 238
}

/**
  Returns absolute path to main script file.
  */
Kai Koehne's avatar
Kai Koehne committed
239 240
QString QmlProjectRunConfiguration::mainScript() const
{
241 242 243 244 245 246
    if (!qmlTarget()->qmlProject()->mainFile().isEmpty()) {
        const QString pathInProject = qmlTarget()->qmlProject()->mainFile();
        if (QFileInfo(pathInProject).isAbsolute())
            return pathInProject;
        else
            return qmlTarget()->qmlProject()->projectDir().absoluteFilePath(pathInProject);
Kai Koehne's avatar
Kai Koehne committed
247 248
    }

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

252
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
253 254
}

Kai Koehne's avatar
Kai Koehne committed
255 256
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
257
{
258 259
    if (source == FileInEditor) {
        m_scriptFile = M_CURRENT_FILE;
Kai Koehne's avatar
Kai Koehne committed
260 261 262 263 264 265 266 267
        m_mainScriptFilename.clear();
    } else if (source == FileInProjectFile) {
        m_scriptFile.clear();
        m_mainScriptFilename.clear();
    } else { // FileInSettings
        m_scriptFile = settingsPath;
        m_mainScriptFilename
                = qmlTarget()->qmlProject()->projectDir().absoluteFilePath(m_scriptFile);
268
    }
Kai Koehne's avatar
Kai Koehne committed
269 270 271
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
272 273
}

274 275 276 277 278 279 280
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

281 282 283 284
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
285
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
286 287
}

Kai Koehne's avatar
Kai Koehne committed
288 289 290 291
QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());

292
    map.insert(QLatin1String(Constants::QML_VIEWER_QT_KEY), m_qtVersionId);
Kai Koehne's avatar
Kai Koehne committed
293 294
    map.insert(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY), m_qmlViewerArgs);
    map.insert(QLatin1String(Constants::QML_MAINSCRIPT_KEY),  m_scriptFile);
295 296
    map.insert(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY),
               Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
Kai Koehne's avatar
Kai Koehne committed
297 298 299 300 301
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
302
    setQtVersionId(map.value(QLatin1String(Constants::QML_VIEWER_QT_KEY), -1).toInt());
Kai Koehne's avatar
Kai Koehne committed
303
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
304
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
305 306 307
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

308 309

    updateQtVersions();
Kai Koehne's avatar
Kai Koehne committed
310 311 312 313 314 315 316
    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
317 318 319 320

    return RunConfiguration::fromMap(map);
}

321
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
322
{
323
    if (editor) {
324
        m_currentFileFilename = editor->document()->fileName();
325
    }
326 327 328 329
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
330
{
331
    bool qmlFileFound = false;
332
    if (mainScriptSource() == FileInEditor) {
333
        Core::IEditor *editor = Core::EditorManager::instance()->currentEditor();
hjk's avatar
hjk committed
334
        Core::MimeDatabase *db = ICore::mimeDatabase();
335
        if (editor) {
336
            m_currentFileFilename = editor->document()->fileName();
Kai Koehne's avatar
Kai Koehne committed
337
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
338
                qmlFileFound = true;
339 340
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
341
                || db->findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
342 343
            // find a qml file with lowercase filename. This is slow, but only done
            // in initialization/other border cases.
344
            foreach(const QString &filename, m_projectTarget->qmlProject()->files()) {
345 346 347
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
Kai Koehne's avatar
Kai Koehne committed
348
                        && db->findByFile(fi).type() == QLatin1String("application/x-qml"))
349 350
                {
                    m_currentFileFilename = filename;
351
                    qmlFileFound = true;
352 353 354 355
                    break;
                }

            }
356
        }
357
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
358
        qmlFileFound = !mainScript().isEmpty();
359 360
    }

361
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
362
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
363

364 365 366 367

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

370
void QmlProjectRunConfiguration::updateQtVersions()
371
{
Kai Koehne's avatar
Kai Koehne committed
372
    QtVersionManager *qtVersions = QtVersionManager::instance();
373 374 375 376 377 378

    //
    // update m_qtVersionId
    //
    if (!qtVersions->isValidId(m_qtVersionId)
            || !isValidVersion(qtVersions->version(m_qtVersionId))) {
379
        int newVersionId = -1;
380
        // take first one you find
381
        foreach (QtSupport::BaseQtVersion *version, qtVersions->validVersions()) {
382
            if (isValidVersion(version)) {
383
                newVersionId = version->uniqueId();
384 385
                break;
            }
386
        }
387
        setQtVersionId(newVersionId);
388 389
    }

390 391 392
    updateEnabled();
}

393
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
394 395
{
    if (version
396 397
            && (version->type() == QtSupport::Constants::DESKTOPQT
                || version->type() == QtSupport::Constants::SIMULATORQT)
398 399 400 401
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
402 403
}

404 405 406
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
407 408
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    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
426
} // namespace QmlProjectManager