qmlprojectruncontrol.cpp 7.83 KB
Newer Older
Kai Koehne's avatar
Kai Koehne committed
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
Kai Koehne's avatar
Kai Koehne committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.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
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
Kai Koehne's avatar
Kai Koehne committed
30
31
32
33
34
**
**************************************************************************/

#include "qmlprojectruncontrol.h"
#include "qmlprojectrunconfiguration.h"
35
#include "qmlprojectconstants.h"
Kai Koehne's avatar
Kai Koehne committed
36
37
38
39
#include <coreplugin/icore.h>
#include <coreplugin/modemanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/applicationlauncher.h>
40
41
#include <projectexplorer/target.h>
#include <projectexplorer/project.h>
42
#include <qtsupport/qtversionmanager.h>
43
#include <utils/environment.h>
Kai Koehne's avatar
Kai Koehne committed
44
#include <utils/qtcassert.h>
45
#include <utils/qtcprocess.h>
Kai Koehne's avatar
Kai Koehne committed
46

Lasse Holmstedt's avatar
Lasse Holmstedt committed
47
48
#include <debugger/debuggerrunner.h>
#include <debugger/debuggerplugin.h>
49
#include <debugger/debuggerconstants.h>
Lasse Holmstedt's avatar
Lasse Holmstedt committed
50
#include <debugger/debuggerengine.h>
Friedemann Kleint's avatar
Friedemann Kleint committed
51
#include <debugger/debuggerstartparameters.h>
52
#include <qmljsinspector/qmljsinspectorconstants.h>
53
#include <qtsupport/qmlobservertool.h>
54

55
#include <qmlprojectmanager/qmlprojectplugin.h>
56

Kai Koehne's avatar
Kai Koehne committed
57

58
using namespace ProjectExplorer;
Kai Koehne's avatar
Kai Koehne committed
59
60

namespace QmlProjectManager {
61

Kai Koehne's avatar
Kai Koehne committed
62
63
namespace Internal {

64
QmlProjectRunControl::QmlProjectRunControl(QmlProjectRunConfiguration *runConfiguration, QString mode)
dt's avatar
dt committed
65
    : RunControl(runConfiguration, mode)
Kai Koehne's avatar
Kai Koehne committed
66
{
67
    m_applicationLauncher.setEnvironment(runConfiguration->environment());
Kai Koehne's avatar
Kai Koehne committed
68
69
    m_applicationLauncher.setWorkingDirectory(runConfiguration->workingDirectory());

70
71
72
73
74
    if (mode == ProjectExplorer::Constants::RUNMODE) {
        m_executable = runConfiguration->viewerPath();
    } else {
        m_executable = runConfiguration->observerPath();
    }
Kai Koehne's avatar
Kai Koehne committed
75
76
    m_commandLineArguments = runConfiguration->viewerArguments();

con's avatar
con committed
77
78
    connect(&m_applicationLauncher, SIGNAL(appendMessage(QString,Utils::OutputFormat)),
            this, SLOT(slotAppendMessage(QString, Utils::OutputFormat)));
Kai Koehne's avatar
Kai Koehne committed
79
80
81
82
83
84
    connect(&m_applicationLauncher, SIGNAL(processExited(int)),
            this, SLOT(processExited(int)));
    connect(&m_applicationLauncher, SIGNAL(bringToForegroundRequested(qint64)),
            this, SLOT(slotBringApplicationToForeground(qint64)));
}

85
QmlProjectRunControl::~QmlProjectRunControl()
Kai Koehne's avatar
Kai Koehne committed
86
{
87
    stop();
Kai Koehne's avatar
Kai Koehne committed
88
89
}

90
void QmlProjectRunControl::start()
Kai Koehne's avatar
Kai Koehne committed
91
{
92
    m_applicationLauncher.start(ApplicationLauncher::Gui, m_executable,
Kai Koehne's avatar
Kai Koehne committed
93
                                m_commandLineArguments);
94
    setApplicationProcessHandle(ProcessHandle(m_applicationLauncher.applicationPID()));
Kai Koehne's avatar
Kai Koehne committed
95
    emit started();
dt's avatar
dt committed
96
    QString msg = tr("Starting %1 %2\n")
97
        .arg(QDir::toNativeSeparators(m_executable), m_commandLineArguments);
con's avatar
con committed
98
    appendMessage(msg, Utils::NormalMessageFormat);
Kai Koehne's avatar
Kai Koehne committed
99
100
}

101
RunControl::StopResult QmlProjectRunControl::stop()
Kai Koehne's avatar
Kai Koehne committed
102
103
{
    m_applicationLauncher.stop();
104
    return StoppedSynchronously;
Kai Koehne's avatar
Kai Koehne committed
105
106
}

107
bool QmlProjectRunControl::isRunning() const
Kai Koehne's avatar
Kai Koehne committed
108
109
110
111
{
    return m_applicationLauncher.isRunning();
}

112
QIcon QmlProjectRunControl::icon() const
113
114
115
116
{
    return QIcon(ProjectExplorer::Constants::ICON_RUN_SMALL);
}

117
void QmlProjectRunControl::slotBringApplicationToForeground(qint64 pid)
Kai Koehne's avatar
Kai Koehne committed
118
119
120
121
{
    bringApplicationToForeground(pid);
}

122
void QmlProjectRunControl::slotAppendMessage(const QString &line, Utils::OutputFormat format)
Kai Koehne's avatar
Kai Koehne committed
123
{
124
    appendMessage(line, format);
Kai Koehne's avatar
Kai Koehne committed
125
126
}

127
void QmlProjectRunControl::processExited(int exitCode)
Kai Koehne's avatar
Kai Koehne committed
128
{
dt's avatar
dt committed
129
    QString msg = tr("%1 exited with code %2\n")
130
        .arg(QDir::toNativeSeparators(m_executable)).arg(exitCode);
con's avatar
con committed
131
    appendMessage(msg, exitCode ? Utils::ErrorMessageFormat : Utils::NormalMessageFormat);
Kai Koehne's avatar
Kai Koehne committed
132
133
134
    emit finished();
}

135
QmlProjectRunControlFactory::QmlProjectRunControlFactory(QObject *parent)
Kai Koehne's avatar
Kai Koehne committed
136
137
138
139
    : IRunControlFactory(parent)
{
}

140
QmlProjectRunControlFactory::~QmlProjectRunControlFactory()
Kai Koehne's avatar
Kai Koehne committed
141
142
143
{
}

144
bool QmlProjectRunControlFactory::canRun(RunConfiguration *runConfiguration,
Kai Koehne's avatar
Kai Koehne committed
145
146
                                  const QString &mode) const
{
147
148
    QmlProjectRunConfiguration *config =
        qobject_cast<QmlProjectRunConfiguration*>(runConfiguration);
149
150
    if (!config)
        return false;
151
    if (mode == ProjectExplorer::Constants::RUNMODE)
152
153
        return !config->viewerPath().isEmpty();
    if (mode != Debugger::Constants::DEBUGMODE)
154
        return false;
155

156
157
    if (!Debugger::DebuggerPlugin::isActiveDebugLanguage(Debugger::QmlLanguage))
        return false;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
158

159
160
161
162
163
164
165
166
    if (!config->observerPath().isEmpty())
        return true;
    if (!config->qtVersion())
        return false;
    if (!config->qtVersion()->needsQmlDebuggingLibrary())
        return true;
    if (QtSupport::QmlObserverTool::canBuild(config->qtVersion()))
        return true;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
167
    return false;
Kai Koehne's avatar
Kai Koehne committed
168
169
}

170
RunControl *QmlProjectRunControlFactory::create(RunConfiguration *runConfiguration,
Kai Koehne's avatar
Kai Koehne committed
171
172
173
                                         const QString &mode)
{
    QTC_ASSERT(canRun(runConfiguration, mode), return 0);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
174
175
176

    QmlProjectRunConfiguration *config = qobject_cast<QmlProjectRunConfiguration *>(runConfiguration);
    RunControl *runControl = 0;
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
177
    if (mode == ProjectExplorer::Constants::RUNMODE)
178
        runControl = new QmlProjectRunControl(config, mode);
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
179
    else if (mode == Debugger::Constants::DEBUGMODE)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
180
181
        runControl = createDebugRunControl(config);
    return runControl;
Kai Koehne's avatar
Kai Koehne committed
182
183
}

184
QString QmlProjectRunControlFactory::displayName() const
Kai Koehne's avatar
Kai Koehne committed
185
186
187
188
{
    return tr("Run");
}

189
ProjectExplorer::RunConfigWidget *QmlProjectRunControlFactory::createConfigurationWidget(RunConfiguration *runConfiguration)
Kai Koehne's avatar
Kai Koehne committed
190
191
{
    Q_UNUSED(runConfiguration)
192
    return 0;
Kai Koehne's avatar
Kai Koehne committed
193
194
}

195
RunControl *QmlProjectRunControlFactory::createDebugRunControl(QmlProjectRunConfiguration *runConfig)
Lasse Holmstedt's avatar
Lasse Holmstedt committed
196
197
198
{
    Debugger::DebuggerStartParameters params;
    params.startMode = Debugger::StartInternal;
199
    params.executable = runConfig->observerPath();
200
201
    params.qmlServerAddress = "127.0.0.1";
    params.qmlServerPort = runConfig->qmlDebugServerPort();
202
    params.processArgs = QString("-qmljsdebugger=port:%1,block").arg(runConfig->qmlDebugServerPort());
203
    params.processArgs += QLatin1Char(' ') + runConfig->viewerArguments();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
204
    params.workingDirectory = runConfig->workingDirectory();
205
    params.environment = runConfig->environment();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
206
    params.displayName = runConfig->displayName();
207
208
    params.projectSourceDirectory = runConfig->target()->project()->projectDirectory();
    params.projectSourceFiles = runConfig->target()->project()->files(Project::ExcludeGeneratedFiles);
209
210
    if (!runConfig->qtVersion()->qtAbis().isEmpty())
        params.toolChainAbi = runConfig->qtVersion()->qtAbis().first();
Lasse Holmstedt's avatar
Lasse Holmstedt committed
211

212
213
214
215
216
217
218
    // Makes sure that all bindings go through the JavaScript engine, so that
    // breakpoints are actually hit!
    const QString optimizerKey = QLatin1String("QML_DISABLE_OPTIMIZER");
    if (!params.environment.hasKey(optimizerKey)) {
        params.environment.set(optimizerKey, QLatin1String("1"));
    }

219
    if (params.executable.isEmpty()) {
220
        QmlProjectPlugin::showQmlObserverToolWarning();
221
222
223
        return 0;
    }

224
    return Debugger::DebuggerPlugin::createDebugger(params, runConfig);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
225
226
}

Kai Koehne's avatar
Kai Koehne committed
227
228
} // namespace Internal
} // namespace QmlProjectManager