qmlprojectrunconfiguration.cpp 11.8 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
Tobias Hunger's avatar
Tobias Hunger committed
140 141 142 143
    QmlProject *project = qobject_cast<QmlProject *>(target()->project());
    if (!project)
        return args;
    foreach (const QString &importPath, project->importPaths()) {
144 145
        Utils::QtcProcess::addArg(&args, "-I");
        Utils::QtcProcess::addArg(&args, importPath);
Kai Koehne's avatar
Kai Koehne committed
146 147
    }

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

QString QmlProjectRunConfiguration::workingDirectory() const
{
Tobias Hunger's avatar
Tobias Hunger committed
158
    QFileInfo projectFile(target()->project()->document()->fileName());
159
    return canonicalCapsPath(projectFile.absolutePath());
Kai Koehne's avatar
Kai Koehne committed
160 161
}

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

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

172 173 174 175
    return canonicalPath;
}


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

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

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

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

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

221
    if (!m_mainScriptFilename.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
222
        return m_mainScriptFilename;
Kai Koehne's avatar
Kai Koehne committed
223

224
    return m_currentFileFilename;
Kai Koehne's avatar
Kai Koehne committed
225 226
}

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

246 247 248 249 250 251 252
Utils::Environment QmlProjectRunConfiguration::environment() const
{
    Utils::Environment env = baseEnvironment();
    env.modify(userEnvironmentChanges());
    return env;
}

253 254 255 256
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
257
                                ProjectExplorer::Abi::RuntimeQmlFormat, hostAbi.wordWidth());
258 259
}

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

bool QmlProjectRunConfiguration::fromMap(const QVariantMap &map)
{
    m_qmlViewerArgs = map.value(QLatin1String(Constants::QML_VIEWER_ARGUMENTS_KEY)).toString();
274
    m_scriptFile = map.value(QLatin1String(Constants::QML_MAINSCRIPT_KEY), M_CURRENT_FILE).toString();
275 276 277
    m_userEnvironmentChanges = Utils::EnvironmentItem::fromStringList(
                map.value(QLatin1String(Constants::USER_ENVIRONMENT_CHANGES_KEY)).toStringList());

Tobias Hunger's avatar
Tobias Hunger committed
278
    if (m_scriptFile == M_CURRENT_FILE)
Kai Koehne's avatar
Kai Koehne committed
279
        setScriptSource(FileInEditor);
Tobias Hunger's avatar
Tobias Hunger committed
280
    else if (m_scriptFile.isEmpty())
Kai Koehne's avatar
Kai Koehne committed
281
        setScriptSource(FileInProjectFile);
Tobias Hunger's avatar
Tobias Hunger committed
282
    else
Kai Koehne's avatar
Kai Koehne committed
283
        setScriptSource(FileInSettings, m_scriptFile);
Kai Koehne's avatar
Kai Koehne committed
284 285 286 287

    return RunConfiguration::fromMap(map);
}

288
void QmlProjectRunConfiguration::changeCurrentFile(Core::IEditor *editor)
289
{
Tobias Hunger's avatar
Tobias Hunger committed
290
    if (editor)
291
        m_currentFileFilename = editor->document()->fileName();
292 293 294 295
    updateEnabled();
}

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

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

            }
322
        }
323
    } else { // use default one
Kai Koehne's avatar
Kai Koehne committed
324
        qmlFileFound = !mainScript().isEmpty();
325 326
    }

327
    bool newValue = (QFileInfo(viewerPath()).exists()
Kai Koehne's avatar
Kai Koehne committed
328
                     || QFileInfo(observerPath()).exists()) && qmlFileFound;
329

330 331 332

    // Always emit change signal to force reevaluation of run/debug buttons
    m_isEnabled = newValue;
333
    emit enabledChanged();
334 335
}

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

347 348 349
Utils::Environment QmlProjectRunConfiguration::baseEnvironment() const
{
    Utils::Environment env;
350 351
    if (qtVersion())
        env = qtVersion()->qmlToolsEnvironment();
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    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
369
} // namespace QmlProjectManager