qmlprojectrunconfiguration.cpp 10.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Kai Koehne's avatar
Kai Koehne committed
2
**
3
** Copyright (C) 2014 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),
71 72
    m_currentFileFilename(source->m_currentFileFilename),
    m_mainScriptFilename(source->m_mainScriptFilename),
73
    m_scriptFile(source->m_scriptFile),
74
    m_qmlViewerArgs(source->m_qmlViewerArgs),
75
    m_isEnabled(source->m_isEnabled)
Kai Koehne's avatar
Kai Koehne committed
76
{
77
    ctor();
Kai Koehne's avatar
Kai Koehne committed
78 79
}

80
bool QmlProjectRunConfiguration::isEnabled() const
81
{
82
    return m_isEnabled;
83 84
}

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

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

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

103 104 105 106
    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
107 108
}

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

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

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

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

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

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

QString QmlProjectRunConfiguration::workingDirectory() const
{
147
    QFileInfo projectFile(target()->project()->projectFilePath());
148
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
149 150
}

151
/* QtDeclarative checks explicitly that the capitalization for any URL / path
152
   is exactly like the capitalization on disk.*/
153 154
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
Orgad Shaneh's avatar
Orgad Shaneh committed
155
    return Utils::FileUtils::normalizePathName(QFileInfo(fileName).canonicalFilePath());
156 157 158
}


159
QtSupport::BaseQtVersion *QmlProjectRunConfiguration::qtVersion() const
160
{
Tobias Hunger's avatar
Tobias Hunger committed
161
    return QtSupport::QtKitInformation::qtVersion(target()->kit());
162 163
}

164
QWidget *QmlProjectRunConfiguration::createConfigurationWidget()
Kai Koehne's avatar
Kai Koehne committed
165
{
166
    return new QmlProjectRunConfigurationWidget(this);
Kai Koehne's avatar
Kai Koehne committed
167 168
}

con's avatar
con committed
169
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
170
{
Tobias Hunger's avatar
Tobias Hunger committed
171
    return new QtSupport::QtOutputFormatter(target()->project());
172
}
173

174 175
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
176
    QmlProject *project = static_cast<QmlProject *>(target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
177
    if (!project->mainFile().isEmpty())
178
        return FileInProjectFile;
Tobias Hunger's avatar
Tobias Hunger committed
179
    if (!m_mainScriptFilename.isEmpty())
180
        return FileInSettings;
181
    return FileInEditor;
182 183 184 185 186
}

/**
  Returns absolute path to main script file.
  */
Kai Koehne's avatar
Kai Koehne committed
187 188
QString QmlProjectRunConfiguration::mainScript() const
{
Tobias Hunger's avatar
Tobias Hunger committed
189 190 191 192 193
    QmlProject *project = qobject_cast<QmlProject *>(target()->project());
    if (!project)
        return m_currentFileFilename;
    if (!project->mainFile().isEmpty()) {
        const QString pathInProject = project->mainFile();
194 195 196
        if (QFileInfo(pathInProject).isAbsolute())
            return pathInProject;
        else
Tobias Hunger's avatar
Tobias Hunger committed
197
            return project->projectDir().absoluteFilePath(pathInProject);
198 199
    }

200
    if (!m_mainScriptFilename.isEmpty())
201
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
202

203
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
204 205
}

206 207
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
208
{
209
    if (source == FileInEditor) {
210
        m_scriptFile = QLatin1String(M_CURRENT_FILE);
211 212 213 214 215 216 217
        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
218
                = target()->project()->projectDirectory() + QLatin1Char('/') + m_scriptFile;
219
    }
220
    updateEnabled();
221 222

    emit scriptSourceChanged();
Kai Koehne's avatar
Kai Koehne committed
223 224
}

225 226 227 228
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
229
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
230 231
}

Kai Koehne's avatar
Kai Koehne committed
232 233 234 235 236 237 238 239 240 241 242 243
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();
244
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), QLatin1String(M_CURRENT_FILE)).toString();
245

246
    if (m_scriptFile == QLatin1String(M_CURRENT_FILE))
247
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
248
    else if (m_scriptFile.isEmpty())
249
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
250
    else
251
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
252 253 254 255

    return RunConfiguration::fromMap(map);
}

hjk's avatar
hjk committed
256
void QmlProjectRunConfiguration::changeCurrentFile(IEditor *editor)
257
{
258 259 260
    if (!editor)
        editor = EditorManager::currentEditor();

Tobias Hunger's avatar
Tobias Hunger committed
261
    if (editor)
262
        m_currentFileFilename = editor->document()->filePath();
263 264 265 266
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
267
{
268
    bool qmlFileFound = false;
269
    if (mainScriptSource() == FileInEditor) {
hjk's avatar
hjk committed
270
        IDocument *document = EditorManager::currentDocument();
271 272
        if (document) {
            m_currentFileFilename = document->filePath();
hjk's avatar
hjk committed
273
            if (MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
274
                qmlFileFound = true;
275
        }
276
        if (!document
hjk's avatar
hjk committed
277
                || MimeDatabase::findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
278 279
            // 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
280
            foreach (const QString &filename, target()->project()->files(ProjectExplorer::Project::AllFiles)) {
281 282 283
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
hjk's avatar
hjk committed
284
                        && MimeDatabase::findByFile(fi).type() == QLatin1String("application/x-qml"))
285 286
                {
                    m_currentFileFilename = filename;
287
                    qmlFileFound = true;
288 289 290 291
                    break;
                }

            }
292
        }
293
    } else { // use default one
294
        qmlFileFound = !mainScript().isEmpty();
295 296
    }

297
    bool newValue = QFileInfo(executable()).exists() && qmlFileFound;
298

299 300 301

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
302
    emit enabledChanged();
303 304
}

305
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
306 307
{
    if (version
308 309
            && (version->type() == QLatin1String(QtSupport::Constants::DESKTOPQT)
                || version->type() == QLatin1String(QtSupport::Constants::SIMULATORQT))
310 311 312 313
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
314 315
}

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