qmlprojectrunconfiguration.cpp 13.6 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 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
Kai Koehne's avatar
Kai Koehne committed
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
Kai Koehne's avatar
Kai Koehne committed
15 16 17 18 19 20 21 22 23 24
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
Kai Koehne's avatar
Kai Koehne committed
31 32 33
**
**************************************************************************/

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

49
#ifdef Q_OS_WIN32
50
#include <utils/winutils.h>
51 52
#endif

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

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

namespace QmlProjectManager {

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

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

Kai Koehne's avatar
Kai Koehne committed
76 77
QmlProjectRunConfiguration::QmlProjectRunConfiguration(QmlProjectTarget *parent,
                                                       QmlProjectRunConfiguration *source) :
Kai Koehne's avatar
Kai Koehne committed
78
    ProjectExplorer::RunConfiguration(parent, source),
79
    m_qtVersionId(source->m_qtVersionId),
Kai Koehne's avatar
Kai Koehne committed
80
    m_scriptFile(source->m_scriptFile),
81
    m_qmlViewerArgs(source->m_qmlViewerArgs),
82
    m_projectTarget(parent),
Kai Koehne's avatar
Kai Koehne committed
83
    m_usingCurrentFile(source->m_usingCurrentFile),
84
    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 91 92 93
bool QmlProjectRunConfiguration::isEnabled(ProjectExplorer::BuildConfiguration *bc) const
{
    Q_UNUSED(bc);

94
    return m_isEnabled;
95 96
}

Kai Koehne's avatar
Kai Koehne committed
97 98
void QmlProjectRunConfiguration::ctor()
{
99 100 101 102
    // reset default settings in constructor
    setUseCppDebugger(false);
    setUseQmlDebugger(true);

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

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

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

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

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

122
QString QmlProjectRunConfiguration::viewerPath() const
123
{
124 125
    Qt4ProjectManager::QtVersion *version = qtVersion();
    if (!version) {
126
        return QString();
127 128 129
    } else {
        return version->qmlviewerCommand();
    }
130 131
}

132
QString QmlProjectRunConfiguration::observerPath() const
Kai Koehne's avatar
Kai Koehne committed
133
{
134 135
    Qt4ProjectManager::QtVersion *version = qtVersion();
    if (!version) {
136
        return QString();
137 138 139
    } else {
        return version->qmlObserverTool();
    }
Kai Koehne's avatar
Kai Koehne committed
140 141
}

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

    // arguments from .qmlproject file
148
    foreach (const QString &importPath, qmlTarget()->qmlProject()->importPaths()) {
149 150
        Utils::QtcProcess::addArg(&args, "-I");
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
151 152
    }

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

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

167 168 169 170 171
int QmlProjectRunConfiguration::qtVersionId() const
{
    return m_qtVersionId;
}

172 173 174 175 176 177 178
void QmlProjectRunConfiguration::setQtVersionId(int id)
{
    if (m_qtVersionId == id)
        return;

    m_qtVersionId = id;
    qmlTarget()->qmlProject()->refresh(QmlProject::Configuration);
179 180
    if (m_configurationWidget)
        m_configurationWidget.data()->updateQtVersionComboBox();
181 182
}

183 184 185 186 187 188 189 190
/* QtDeclarative checks explicitly that the capitalization for any URL / path
   is exactly like the capitalization on disk. This method is uses the same
   native Windows API's to get the exact canonical path. */
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
    QString canonicalPath = QFileInfo(fileName).canonicalFilePath();

#if defined(Q_OS_WIN32)
191 192 193 194 195 196 197 198
    QString error;
    // don't know whether the shortpath step is really needed,
    // but we do this in QtDeclarative too.
    QString path = Utils::getShortPathName(canonicalPath, &error);
    if (!path.isEmpty())
        path = Utils::getLongPathName(canonicalPath, &error);
    if (!path.isEmpty())
        canonicalPath = path;
199
#endif
Kai Koehne's avatar
Kai Koehne committed
200

201 202 203 204
    return canonicalPath;
}


205 206 207 208 209
Qt4ProjectManager::QtVersion *QmlProjectRunConfiguration::qtVersion() const
{
    if (m_qtVersionId == -1)
        return 0;

Kai Koehne's avatar
Kai Koehne committed
210
    QtVersionManager *versionManager = QtVersionManager::instance();
211 212 213 214 215 216
    Qt4ProjectManager::QtVersion *version = versionManager->version(m_qtVersionId);
    QTC_ASSERT(version, return 0);

    return version;
}

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

224 225 226 227
ProjectExplorer::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
{
    return new Qt4ProjectManager::QtOutputFormatter(qmlTarget()->qmlProject());
}
228

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

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

Kai Koehne's avatar
Kai Koehne committed
253 254 255 256 257 258
    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
259 260
}

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

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

290 291 292 293 294 295 296
ProjectExplorer::Abi QmlProjectRunConfiguration::abi() const
{
    ProjectExplorer::Abi hostAbi = ProjectExplorer::Abi::hostAbi();
    return ProjectExplorer::Abi(hostAbi.architecture(), hostAbi.os(), hostAbi.osFlavor(),
                                ProjectExplorer::Abi::Format_Runtime_QML, hostAbi.wordWidth());
}

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

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

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

317 318

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

    return RunConfiguration::fromMap(map);
}

330 331 332 333 334 335
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor * /*editor*/)
{
    updateEnabled();
}

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

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

            }
362
        }
363
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
364
        qmlFileFound = !mainScript().isEmpty();
365 366
    }

367
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
368
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
369

370 371 372 373

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

376
void QmlProjectRunConfiguration::updateQtVersions()
377
{
Kai Koehne's avatar
Kai Koehne committed
378
    QtVersionManager *qtVersions = QtVersionManager::instance();
379 380 381 382 383 384

    //
    // update m_qtVersionId
    //
    if (!qtVersions->isValidId(m_qtVersionId)
            || !isValidVersion(qtVersions->version(m_qtVersionId))) {
385
        int newVersionId = -1;
386 387 388
        // take first one you find
        foreach (Qt4ProjectManager::QtVersion *version, qtVersions->validVersions()) {
            if (isValidVersion(version)) {
389
                newVersionId = version->uniqueId();
390 391
                break;
            }
392
        }
393
        setQtVersionId(newVersionId);
394 395
    }

396 397 398 399 400 401
    updateEnabled();
}

bool QmlProjectRunConfiguration::isValidVersion(Qt4ProjectManager::QtVersion *version)
{
    if (version
402 403
            && (version->supportsTargetId(Qt4ProjectManager::Constants::DESKTOP_TARGET_ID)
                || version->supportsTargetId(Qt4ProjectManager::Constants::QT_SIMULATOR_TARGET_ID))
404 405 406 407
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
408 409
}

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