qmlprojectrunconfiguration.cpp 10.5 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),
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 143 144
    return args;
}

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

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


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

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

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

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

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

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

201
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
202 203
}

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

    emit scriptSourceChanged();
Kai Koehne's avatar
Kai Koehne committed
221 222
}

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

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

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

    return RunConfiguration::fromMap(map);
}

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

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

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

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

            }
290
        }
291
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
292
        qmlFileFound = !mainScript().isEmpty();
293 294
    }

295
    bool newValue = QFileInfo(executable()).exists() && qmlFileFound;
296

297 298 299

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

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

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