qmlprojectrunconfiguration.cpp 11.7 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) 2012 Nokia Corporation and/or its subsidiary(-ies).
Kai Koehne's avatar
Kai Koehne committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
Kai Koehne's avatar
Kai Koehne committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Kai Koehne's avatar
Kai Koehne committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Kai Koehne's avatar
Kai Koehne committed
30 31 32
**
**************************************************************************/

dt's avatar
dt committed
33
#include "qmlprojectrunconfiguration.h"
Kai Koehne's avatar
Kai Koehne committed
34 35
#include "qmlproject.h"
#include "qmlprojectmanagerconstants.h"
36
#include "qmlprojectrunconfigurationwidget.h"
37
#include <coreplugin/mimedatabase.h>
Kai Koehne's avatar
Kai Koehne committed
38 39 40
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icore.h>
Tobias Hunger's avatar
Tobias Hunger committed
41
#include <projectexplorer/target.h>
42
#include <utils/qtcassert.h>
43
#include <utils/qtcprocess.h>
Tobias Hunger's avatar
Tobias Hunger committed
44
#include <qtsupport/qtprofileinformation.h>
45 46
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtsupportconstants.h>
Kai Koehne's avatar
Kai Koehne committed
47

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

Kai Koehne's avatar
Kai Koehne committed
52 53 54 55 56
using Core::EditorManager;
using Core::ICore;
using Core::IEditor;

using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
57 58 59

namespace QmlProjectManager {

Kai Koehne's avatar
Kai Koehne committed
60
const char * const M_CURRENT_FILE = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
61

Tobias Hunger's avatar
Tobias Hunger committed
62
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent) :
63
    ProjectExplorer::RunConfiguration(parent, Core::Id(Constants::QML_RC_ID)),
64
    m_scriptFile(M_CURRENT_FILE),
65
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
66 67
{
    ctor();
Kai Koehne's avatar
Kai Koehne committed
68
}
Kai Koehne's avatar
Kai Koehne committed
69

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

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

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

Kai Koehne's avatar
Kai Koehne committed
93 94
void QmlProjectRunConfiguration::ctor()
{
95
    // reset default settings in constructor
96 97
    debuggerAspect()->setUseCppDebugger(false);
    debuggerAspect()->setUseQmlDebugger(true);
98

Kai Koehne's avatar
Kai Koehne committed
99
    EditorManager *em = Core::EditorManager::instance();
100 101 102
    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
            this, SLOT(changeCurrentFile(Core::IEditor*)));

Tobias Hunger's avatar
Tobias Hunger committed
103 104
    connect(target(), SIGNAL(profileChanged()),
            this, SLOT(updateEnabled()));
105

106
    setDisplayName(tr("QML Viewer", "QMLRunConfiguration display name."));
Kai Koehne's avatar
Kai Koehne committed
107 108 109 110 111 112
}

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

113
QString QmlProjectRunConfiguration::viewerPath() const
114
{
115
    QtSupport::BaseQtVersion *version = qtVersion();
Tobias Hunger's avatar
Tobias Hunger committed
116
    if (!version)
117
        return QString();
Tobias Hunger's avatar
Tobias Hunger committed
118
    else
119
        return version->qmlviewerCommand();
120 121
}

122
QString QmlProjectRunConfiguration::observerPath() const
Kai Koehne's avatar
Kai Koehne committed
123
{
124
    QtSupport::BaseQtVersion *version = qtVersion();
125
    if (!version) {
126
        return QString();
127
    } else {
128 129
        if (!version->needsQmlDebuggingLibrary())
            return version->qmlviewerCommand();
130 131
        return version->qmlObserverTool();
    }
Kai Koehne's avatar
Kai Koehne committed
132 133
}

134
QString QmlProjectRunConfiguration::viewerArguments() const
Kai Koehne's avatar
Kai Koehne committed
135 136
{
    // arguments in .user file
137
    QString args = m_qmlViewerArgs;
Kai Koehne's avatar
Kai Koehne committed
138 139

    // arguments from .qmlproject file
140
    QmlProject *project = static_cast<QmlProject *>(target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
141
    foreach (const QString &importPath, project->importPaths()) {
142 143
        Utils::QtcProcess::addArg(&args, "-I");
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
144 145
    }

146 147 148
    QString s = mainScript();
    if (!s.isEmpty()) {
        s = canonicalCapsPath(s);
149
        Utils::QtcProcess::addArg(&args, s);
150
    }
Kai Koehne's avatar
Kai Koehne committed
151 152 153 154 155
    return args;
}

QString QmlProjectRunConfiguration::workingDirectory() const
{
Tobias Hunger's avatar
Tobias Hunger committed
156
    QFileInfo projectFile(target()->project()->document()->fileName());
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 164 165
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
    QString canonicalPath = QFileInfo(fileName).canonicalFilePath();

166 167
#if defined(Q_OS_WIN)
    canonicalPath = Utils::normalizePathName(canonicalPath);
168
#endif
Kai Koehne's avatar
Kai Koehne committed
169

170 171 172 173
    return canonicalPath;
}


174
QtSupport::BaseQtVersion *QmlProjectRunConfiguration::qtVersion() const
175
{
Tobias Hunger's avatar
Tobias Hunger committed
176
    return QtSupport::QtProfileInformation::qtVersion(target()->profile());
177 178
}

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

con's avatar
con committed
186
Utils::OutputFormatter *QmlProjectRunConfiguration::createOutputFormatter() const
187
{
Tobias Hunger's avatar
Tobias Hunger committed
188
    return new QtSupport::QtOutputFormatter(target()->project());
189
}
190

Kai Koehne's avatar
Kai Koehne committed
191 192
QmlProjectRunConfiguration::MainScriptSource QmlProjectRunConfiguration::mainScriptSource() const
{
193
    QmlProject *project = static_cast<QmlProject *>(target()->project());
Tobias Hunger's avatar
Tobias Hunger committed
194
    if (!project->mainFile().isEmpty())
195
        return FileInProjectFile;
Tobias Hunger's avatar
Tobias Hunger committed
196
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
197
        return FileInSettings;
198
    return FileInEditor;
Kai Koehne's avatar
Kai Koehne committed
199 200 201 202 203
}

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

217
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
218
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
219

220
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
221 222
}

Kai Koehne's avatar
Kai Koehne committed
223 224
void QmlProjectRunConfiguration::setScriptSource(MainScriptSource source,
                                                 const QString &settingsPath)
Kai Koehne's avatar
Kai Koehne committed
225
{
226 227
    if (source == FileInEditor) {
        m_scriptFile = M_CURRENT_FILE;
Kai Koehne's avatar
Kai Koehne committed
228 229 230 231 232 233 234
        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
235
                = target()->project()->projectDirectory() + QLatin1Char('/') + m_scriptFile;
236
    }
Kai Koehne's avatar
Kai Koehne committed
237 238 239
    updateEnabled();
    if (m_configurationWidget)
        m_configurationWidget.data()->updateFileComboBox();
Kai Koehne's avatar
Kai Koehne committed
240 241
}

242 243 244 245 246 247 248
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

249 250 251 252
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
253
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
254 255
}

Kai Koehne's avatar
Kai Koehne committed
256 257 258 259 260 261
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);
262 263
    map.insert(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY),
               Utils::EnvironmentItem::toStringList(m_userEnvironmentChanges));
Kai Koehne's avatar
Kai Koehne committed
264 265 266 267 268 269
    return map;
}

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
270
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
271 272 273
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

Tobias Hunger's avatar
Tobias Hunger committed
274
    if (m_scriptFile == M_CURRENT_FILE)
Kai Koehne's avatar
Kai Koehne committed
275
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
276
    else if (m_scriptFile.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
277
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
278
    else
Kai Koehne's avatar
Kai Koehne committed
279
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
280 281 282 283

    return RunConfiguration::fromMap(map);
}

284
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
285
{
Tobias Hunger's avatar
Tobias Hunger committed
286
    if (editor)
287
        m_currentFileFilename = editor->document()->fileName();
288 289 290 291
    updateEnabled();
}

void QmlProjectRunConfiguration::updateEnabled()
292
{
293
    bool qmlFileFound = false;
294
    if (mainScriptSource() == FileInEditor) {
hjk's avatar
hjk committed
295
        Core::IEditor *editor = Core::EditorManager::currentEditor();
hjk's avatar
hjk committed
296
        Core::MimeDatabase *db = ICore::mimeDatabase();
297
        if (editor) {
298
            m_currentFileFilename = editor->document()->fileName();
Kai Koehne's avatar
Kai Koehne committed
299
            if (db->findByFile(mainScript()).type() == QLatin1String("application/x-qml"))
300
                qmlFileFound = true;
301 302
        }
        if (!editor
Kai Koehne's avatar
Kai Koehne committed
303
                || db->findByFile(mainScript()).type() == QLatin1String("application/x-qmlproject")) {
Kai Koehne's avatar
Kai Koehne committed
304 305
            // 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
306
            foreach (const QString &filename, target()->project()->files(ProjectExplorer::Project::AllFiles)) {
307 308 309
                const QFileInfo fi(filename);

                if (!filename.isEmpty() && fi.baseName()[0].isLower()
Kai Koehne's avatar
Kai Koehne committed
310
                        && db->findByFile(fi).type() == QLatin1String("application/x-qml"))
311 312
                {
                    m_currentFileFilename = filename;
313
                    qmlFileFound = true;
314 315 316 317
                    break;
                }

            }
318
        }
319
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
320
        qmlFileFound = !mainScript().isEmpty();
321 322
    }

323
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
324
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
325

326 327 328

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
329
    emit enabledChanged();
330 331
}

332
bool QmlProjectRunConfiguration::isValidVersion(QtSupport::BaseQtVersion *version)
333 334
{
    if (version
335 336
            && (version->type() == QtSupport::Constants::DESKTOPQT
                || version->type() == QtSupport::Constants::SIMULATORQT)
337 338 339 340
            && !version->qmlviewerCommand().isEmpty()) {
        return true;
    }
    return false;
341 342
}

343 344 345
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
346 347
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    return env;
}

void QmlProjectRunConfiguration::setUserEnvironmentChanges(const QList<Utils::EnvironmentItem> &diff)
{
    if (m_userEnvironmentChanges != diff) {
        m_userEnvironmentChanges = diff;
        if (m_configurationWidget)
            m_configurationWidget.data()->userEnvironmentChangesChanged();
    }
}

QList<Utils::EnvironmentItem> QmlProjectRunConfiguration::userEnvironmentChanges() const
{
    return m_userEnvironmentChanges;
}

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