qmlprojectrunconfiguration.cpp 10.9 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 64
    addExtraAspect(new QmlProjectEnvironmentAspect(this));

Kai Koehne's avatar
Kai Koehne committed
65
    ctor();
Kai Koehne's avatar
Kai Koehne committed
66
}
Kai Koehne's avatar
Kai Koehne committed
67

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

78
bool QmlProjectRunConfiguration::isEnabled() const
79
{
80
    return m_isEnabled;
81 82
}

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

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

Tobias Hunger's avatar
Tobias Hunger committed
98
    connect(target(), SIGNAL(kitChanged()),
Tobias Hunger's avatar
Tobias Hunger committed
99
            this, SLOT(updateEnabled()));
100

101 102 103 104
    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
105 106
}

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

    if (id() == Constants::QML_SCENE_RC_ID)
        return version->qmlsceneCommand();
115
    return version->qmlviewerCommand();
116 117
}

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

Kai Koehne's avatar
Kai Koehne committed
216 217
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
218
{
219
    if (source == FileInEditor) {
220
        m_scriptFile = QLatin1String(M_CURRENT_FILE);
Kai Koehne's avatar
Kai Koehne committed
221 222 223 224 225 226 227
        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
228
                = target()->project()->projectDirectory() + QLatin1Char('/') + m_scriptFile;
229
    }
Kai Koehne's avatar
Kai Koehne committed
230 231 232
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
233 234
}

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

Kai Koehne's avatar
Kai Koehne committed
242 243 244 245 246 247 248 249 250 251 252 253
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();
254
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), QLatin1String(M_CURRENT_FILE)).toString();
255

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

    return RunConfiguration::fromMap(map);
}

hjk's avatar
hjk committed
266
void QmlProjectRunConfiguration::changeCurrentFile(IEditor *editor)
267
{
268 269 270
    if (!editor)
        editor = EditorManager::currentEditor();

Tobias Hunger's avatar
Tobias Hunger committed
271
    if (editor)
272
        m_currentFileFilename = editor->document()->filePath();
273 274 275 276
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
277
{
278
    bool qmlFileFound = false;
279
    if (mainScriptSource() == FileInEditor) {
hjk's avatar
hjk committed
280
        IDocument *document = EditorManager::currentDocument();
281 282
        if (document) {
            m_currentFileFilename = document->filePath();
hjk's avatar
hjk committed
283
            if (MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
284
                qmlFileFound = true;
285
        }
286
        if (!document
hjk's avatar
hjk committed
287
                || MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
288 289
            // 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
290
            foreach (const QString &filename, target()->project()->files(ProjectExplorer::Project::AllFiles)) {
291 292 293
                const QFileInfo fi(filename);

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

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

307
    bool newValue = QFileInfo(executable()).exists() && qmlFileFound;
308

309 310 311

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

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

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