qmlprojectrunconfiguration.cpp 11.6 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
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
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.
**
Kai Koehne's avatar
Kai Koehne committed
28 29 30
**
**************************************************************************/

dt's avatar
dt committed
31
#include "qmlprojectrunconfiguration.h"
Kai Koehne's avatar
Kai Koehne committed
32 33
#include "qmlproject.h"
#include "qmlprojectmanagerconstants.h"
34
#include "qmlprojectrunconfigurationwidget.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>
40
#include <utils/qtcassert.h>
41
#include <utils/qtcprocess.h>
Tobias Hunger's avatar
Tobias Hunger committed
42
#include <qtsupport/qtprofileinformation.h>
43 44
#include <qtsupport/qtoutputformatter.h>
#include <qtsupport/qtsupportconstants.h>
Kai Koehne's avatar
Kai Koehne committed
45

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

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

using namespace QmlProjectManager::Internal;
Kai Koehne's avatar
Kai Koehne committed
55 56 57

namespace QmlProjectManager {

Kai Koehne's avatar
Kai Koehne committed
58
const char * const M_CURRENT_FILE = "CurrentFile";
Kai Koehne's avatar
Kai Koehne committed
59

Tobias Hunger's avatar
Tobias Hunger committed
60
QmlProjectRunConfiguration::QmlProjectRunConfiguration(ProjectExplorer::Target *parent) :
61
    ProjectExplorer::RunConfiguration(parent, Core::Id(Constants::QML_RC_ID)),
62
    m_scriptFile(M_CURRENT_FILE),
63
    m_isEnabled(false)
Kai Koehne's avatar
Kai Koehne committed
64 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) :
Kai Koehne's avatar
Kai Koehne committed
70
    ProjectExplorer::RunConfiguration(parent, source),
Kai Koehne's avatar
Kai Koehne committed
71
    m_scriptFile(source->m_scriptFile),
72
    m_qmlViewerArgs(source->m_qmlViewerArgs),
73 74
    m_isEnabled(source->m_isEnabled),
    m_userEnvironmentChanges(source->m_userEnvironmentChanges)
Kai Koehne's avatar
Kai Koehne committed
75
{
76
    ctor();
Kai Koehne's avatar
Kai Koehne committed
77 78
}

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

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

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

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

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

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

QmlProjectRunConfiguration::~QmlProjectRunConfiguration()
{
}

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

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

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

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

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

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

158
/* QtDeclarative checks explicitly that the capitalization for any URL / path
159
   is exactly like the capitalization on disk.*/
160 161 162 163
QString QmlProjectRunConfiguration::canonicalCapsPath(const QString &fileName)
{
    QString canonicalPath = QFileInfo(fileName).canonicalFilePath();

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

168 169 170 171
    return canonicalPath;
}


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

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

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

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

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

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

218
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
219 220
}

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

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

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

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

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

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

    return RunConfiguration::fromMap(map);
}

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

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

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

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

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

324 325 326

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

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

341 342 343
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
344 345
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    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
363
} // namespace QmlProjectManager