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

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

82
    return m_isEnabled;
83 84
}

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

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

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

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

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

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

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

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

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

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

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

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

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

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

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

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

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

    return version;
}

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

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

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

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

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

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

QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());

222
    map.insert(QLatin1String(Constants::QML_VIEWER_QT_KEY), m_qtVersionId);
Kai Koehne's avatar
Kai Koehne committed
223 224 225 226 227 228 229
    map.insert(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY), m_qmlViewerArgs);
    map.insert(QLatin1String(Constants::QML_MAINSCRIPT_KEY),  m_scriptFile);
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
230
    setQtVersionId(map.value(QLatin1String(Constants::QML_VIEWER_QT_KEY), -1).toInt());
Kai Koehne's avatar
Kai Koehne committed
231
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
232
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
233 234

    updateQtVersions();
235
    setMainScript(m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
236 237 238 239

    return RunConfiguration::fromMap(map);
}

240 241 242 243 244 245
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor * /*editor*/)
{
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
246
{
247
    bool qmlFileFound = false;
248
    if (m_usingCurrentFile) {
249
        Core::IEditor *editor = Core::EditorManager::instance()->currentEditor();
Kai Koehne's avatar
Kai Koehne committed
250
        Core::MimeDatabase *db = ICore::instance()->mimeDatabase();
251 252
        if (editor) {
            m_currentFileFilename = editor->file()->fileName();
Kai Koehne's avatar
Kai Koehne committed
253
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
254
                qmlFileFound = true;
255 256
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
257 258 259
            || 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.
260
            foreach(const QString &filename, m_projectTarget->qmlProject()->files()) {
261 262 263
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
Kai Koehne's avatar
Kai Koehne committed
264
                    && db->findByFile(fi).type() == QLatin1String("application/x-qml"))
265 266
                {
                    m_currentFileFilename = filename;
267
                    qmlFileFound = true;
268 269 270 271
                    break;
                }

            }
272
        }
273 274 275 276
    } else { // use default one
        qmlFileFound = !m_mainScriptFilename.isEmpty();
    }

277 278
    bool newValue = (QFileInfo(viewerPath()).exists()
                    || QFileInfo(observerPath()).exists()) && qmlFileFound;
279

280 281 282 283

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

286
void QmlProjectRunConfiguration::updateQtVersions()
287
{
Kai Koehne's avatar
Kai Koehne committed
288
    QtVersionManager *qtVersions = QtVersionManager::instance();
289 290 291 292 293 294

    //
    // update m_qtVersionId
    //
    if (!qtVersions->isValidId(m_qtVersionId)
            || !isValidVersion(qtVersions->version(m_qtVersionId))) {
295
        int newVersionId = -1;
296 297 298
        // take first one you find
        foreach (Qt4ProjectManager::QtVersion *version, qtVersions->validVersions()) {
            if (isValidVersion(version)) {
299
                newVersionId = version->uniqueId();
300 301
                break;
            }
302
        }
303
        setQtVersionId(newVersionId);
304 305
    }

306 307 308 309 310 311
    updateEnabled();
}

bool QmlProjectRunConfiguration::isValidVersion(Qt4ProjectManager::QtVersion *version)
{
    if (version
312 313
            && (version->supportsTargetId(Qt4ProjectManager::Constants::DESKTOP_TARGET_ID)
                || version->supportsTargetId(Qt4ProjectManager::Constants::QT_SIMULATOR_TARGET_ID))
314 315 316 317
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
318 319
}

Kai Koehne's avatar
Kai Koehne committed
320
} // namespace QmlProjectManager