qmlprojectrunconfiguration.cpp 10.8 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Kai Koehne's avatar
Kai Koehne committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Kai Koehne's avatar
Kai Koehne committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Kai Koehne's avatar
Kai Koehne committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Kai Koehne's avatar
Kai Koehne committed
29

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 "qmlprojectenvironmentaspect.h"
35
#include <coreplugin/mimedatabase.h>
Kai Koehne's avatar
Kai Koehne committed
36 37 38
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
Tobias Hunger's avatar
Tobias Hunger committed
39
#include <projectexplorer/target.h>
Tobias Hunger's avatar
Tobias Hunger committed
40
#include <qtsupport/qtkitinformation.h>
41 42
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtsupportconstants.h>
Kai Koehne's avatar
Kai Koehne committed
43

Orgad Shaneh's avatar
Orgad Shaneh committed
44 45 46
#include <utils/fileutils.h>
#include <utils/qtcprocess.h>

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

hjk's avatar
hjk committed
51
using namespace Core;
Kai Koehne's avatar
Kai Koehne committed
52
using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
53 54 55

namespace QmlProjectManager {

hjk's avatar
hjk committed
56
const char M_CURRENT_FILE[] = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
57

hjk's avatar
hjk committed
58
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent, Id id) :
59
    ProjectExplorer::LocalApplicationRunConfiguration(parent, id),
60
    m_scriptFile(QLatin1String(M_CURRENT_FILE)),
61
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
62 63
{
    ctor();
Kai Koehne's avatar
Kai Koehne committed
64
}
Kai Koehne's avatar
Kai Koehne committed
65

Tobias Hunger's avatar
Tobias Hunger committed
66
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent,
Kai Koehne's avatar
Kai Koehne committed
67
                                                       QmlProjectRunConfiguration *source) :
68
    ProjectExplorer::LocalApplicationRunConfiguration(parent, source),
Kai Koehne's avatar
Kai Koehne committed
69
    m_scriptFile(source->m_scriptFile),
70
    m_qmlViewerArgs(source->m_qmlViewerArgs),
71
    m_isEnabled(source->m_isEnabled)
Kai Koehne's avatar
Kai Koehne committed
72
{
73
    ctor();
Kai Koehne's avatar
Kai Koehne committed
74 75
}

76
bool QmlProjectRunConfiguration::isEnabled() const
77
{
78
    return m_isEnabled;
79 80
}

dt_'s avatar
dt_ committed
81 82 83
QString QmlProjectRunConfiguration::disabledReason() const
{
    if (!m_isEnabled)
84
        return tr("No qmlviewer or qmlscene found.");
dt_'s avatar
dt_ committed
85 86 87
    return QString();
}

Kai Koehne's avatar
Kai Koehne committed
88 89
void QmlProjectRunConfiguration::ctor()
{
90
    // reset default settings in constructor
hjk's avatar
hjk committed
91
    connect(EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
92 93
            this, SLOT(changeCurrentFile(Core::IEditor*)));

Tobias Hunger's avatar
Tobias Hunger committed
94
    connect(target(), SIGNAL(kitChanged()),
Tobias Hunger's avatar
Tobias Hunger committed
95
            this, SLOT(updateEnabled()));
96

97 98 99 100
    if (id() == Constants::QML_SCENE_RC_ID)
        setDisplayName(tr("QML Scene", "QMLRunConfiguration display name."));
    else
        setDisplayName(tr("QML Viewer", "QMLRunConfiguration display name."));
Kai Koehne's avatar
Kai Koehne committed
101

102
    addExtraAspect(new QmlProjectEnvironmentAspect(this));
Kai Koehne's avatar
Kai Koehne committed
103 104
}

105
QString QmlProjectRunConfiguration::executable() const
106
{
107
    QtSupport::BaseQtVersion *version = qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
108
    if (!version)
109
        return QString();
110 111 112

    if (id() == Constants::QML_SCENE_RC_ID)
        return version->qmlsceneCommand();
113
    if (!version->needsQmlDebuggingLibrary())
114
        return version->qmlviewerCommand();
115 116
    return version->qmlObserverTool();

117 118
}

119
ProjectExplorer::LocalApplicationRunConfiguration::RunMode QmlProjectRunConfiguration::runMode() const
Kai Koehne's avatar
Kai Koehne committed
120
{
121
    return Gui;
Kai Koehne's avatar
Kai Koehne committed
122 123
}

124
QString QmlProjectRunConfiguration::commandLineArguments() const
Kai Koehne's avatar
Kai Koehne committed
125 126
{
    // arguments in .user file
127
    QString args = m_qmlViewerArgs;
Kai Koehne's avatar
Kai Koehne committed
128 129

    // arguments from .qmlproject file
130
    QmlProject *project = static_cast<QmlProject *>(target()->project());
131
    foreach (const QString &importPath, project->customImportPaths()) {
132
        Utils::QtcProcess::addArg(&args, QLatin1String("-I"));
133
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
134 135
    }

136 137 138
    QString s = mainScript();
    if (!s.isEmpty()) {
        s = canonicalCapsPath(s);
139
        Utils::QtcProcess::addArg(&args, s);
140
    }
Kai Koehne's avatar
Kai Koehne committed
141 142 143
    return args;
}

144 145 146 147 148 149 150 151 152 153
QString QmlProjectRunConfiguration::dumperLibrary() const
{
    return QString();
}

QStringList QmlProjectRunConfiguration::dumperLibraryLocations() const
{
    return QStringList();
}

Kai Koehne's avatar
Kai Koehne committed
154 155
QString QmlProjectRunConfiguration::workingDirectory() const
{
156
    QFileInfo projectFile(target()->project()->projectFilePath());
157
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
158 159
}

160
/* QtDeclarative checks explicitly that the capitalization for any URL / path
161
   is exactly like the capitalization on disk.*/
162 163
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
Orgad Shaneh's avatar
Orgad Shaneh committed
164
    return Utils::FileUtils::normalizePathName(QFileInfo(fileName).canonicalFilePath());
165 166 167
}


168
QtSupport::BaseQtVersion *QmlProjectRunConfiguration::qtVersion() const
169
{
Tobias Hunger's avatar
Tobias Hunger committed
170
    return QtSupport::QtKitInformation::qtVersion(target()->kit());
171 172
}

173
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
Kai Koehne's avatar
Kai Koehne committed
174
{
175 176 177
    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
178 179
}

con's avatar
con committed
180
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
181
{
Tobias Hunger's avatar
Tobias Hunger committed
182
    return new QtSupport::QtOutputFormatter(target()->project());
183
}
184

Kai Koehne's avatar
Kai Koehne committed
185 186
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
187
    QmlProject *project = static_cast<QmlProject *>(target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
188
    if (!project->mainFile().isEmpty())
189
        return FileInProjectFile;
Tobias Hunger's avatar
Tobias Hunger committed
190
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
191
        return FileInSettings;
192
    return FileInEditor;
Kai Koehne's avatar
Kai Koehne committed
193 194 195 196 197
}

/**
  Returns absolute path to main script file.
  */
Kai Koehne's avatar
Kai Koehne committed
198 199
QString QmlProjectRunConfiguration::mainScript() const
{
Tobias Hunger's avatar
Tobias Hunger committed
200 201 202 203 204
    QmlProject *project = qobject_cast<QmlProject *>(target()->project());
    if (!project)
        return m_currentFileFilename;
    if (!project->mainFile().isEmpty()) {
        const QString pathInProject = project->mainFile();
205 206 207
        if (QFileInfo(pathInProject).isAbsolute())
            return pathInProject;
        else
Tobias Hunger's avatar
Tobias Hunger committed
208
            return project->projectDir().absoluteFilePath(pathInProject);
Kai Koehne's avatar
Kai Koehne committed
209 210
    }

211
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
212
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
213

214
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
215 216
}

Kai Koehne's avatar
Kai Koehne committed
217 218
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
219
{
220
    if (source == FileInEditor) {
221
        m_scriptFile = QLatin1String(M_CURRENT_FILE);
Kai Koehne's avatar
Kai Koehne committed
222 223 224 225 226 227 228
        m_mainScriptFilename.clear();
    } else if (source == FileInProjectFile) {
        m_scriptFile.clear();
        m_mainScriptFilename.clear();
    } else { // FileInSettings
        m_scriptFile = settingsPath;
        m_mainScriptFilename
Tobias Hunger's avatar
Tobias Hunger committed
229
                = target()->project()->projectDirectory() + QLatin1Char('/') + m_scriptFile;
230
    }
Kai Koehne's avatar
Kai Koehne committed
231 232 233
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
234 235
}

236 237 238 239
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
240
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
241 242
}

Kai Koehne's avatar
Kai Koehne committed
243 244 245 246 247 248 249 250 251 252 253 254
QVariantMap QmlProjectRunConfiguration::toMap() const
{
    QVariantMap map(ProjectExplorer::RunConfiguration::toMap());

    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)
{
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
255
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), QLatin1String(M_CURRENT_FILE)).toString();
256

257
    if (m_scriptFile == QLatin1String(M_CURRENT_FILE))
Kai Koehne's avatar
Kai Koehne committed
258
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
259
    else if (m_scriptFile.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
260
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
261
    else
Kai Koehne's avatar
Kai Koehne committed
262
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
263 264 265 266

    return RunConfiguration::fromMap(map);
}

hjk's avatar
hjk committed
267
void QmlProjectRunConfiguration::changeCurrentFile(IEditor *editor)
268
{
Tobias Hunger's avatar
Tobias Hunger committed
269
    if (editor)
270
        m_currentFileFilename = editor->document()->filePath();
271 272 273 274
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
275
{
276
    bool qmlFileFound = false;
277
    if (mainScriptSource() == FileInEditor) {
hjk's avatar
hjk committed
278
        IDocument *document = EditorManager::currentDocument();
279 280
        if (document) {
            m_currentFileFilename = document->filePath();
hjk's avatar
hjk committed
281
            if (MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
282
                qmlFileFound = true;
283
        }
284
        if (!document
hjk's avatar
hjk committed
285
                || MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
286 287
            // find a qml file with lowercase filename. This is slow, but only done
            // in initialization/other border cases.
Tobias Hunger's avatar
Tobias Hunger committed
288
            foreach (const QString &filename, target()->project()->files(ProjectExplorer::Project::AllFiles)) {
289 290 291
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
hjk's avatar
hjk committed
292
                        && MimeDatabase::findByFile(fi).type() == QLatin1String("application/x-qml"))
293 294
                {
                    m_currentFileFilename = filename;
295
                    qmlFileFound = true;
296 297 298 299
                    break;
                }

            }
300
        }
301
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
302
        qmlFileFound = !mainScript().isEmpty();
303 304
    }

305
    bool newValue = QFileInfo(executable()).exists() && qmlFileFound;
306

307 308 309

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
310
    emit enabledChanged();
311 312
}

313
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
314 315
{
    if (version
316 317
            && (version->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
                || version->type() == QLatin1String(QtSupport::Constants::SIMULATORQT))
318 319 320 321
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
322 323
}

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