From 200a03ed20fc1774b457cb6e4f116dd4fc1a5e29 Mon Sep 17 00:00:00 2001
From: Bea Lam <bea.lam@nokia.com>
Date: Fri, 18 Dec 2009 15:32:36 +1000
Subject: [PATCH] Fix QmlInspector to work properly now that QmlProjectManager
 no longer uses LocalAppRunConfiguration. QmlInspector now uses the qmlviewer
 started by QmlProjectManager instead of starting its own, and clicking the
 Start Debug button within a QML project will switch to QML Inspect mode and
 start the inspector.

---
 src/plugins/qmlinspector/qmlinspector.pro     |   6 +-
 src/plugins/qmlinspector/qmlinspectormode.cpp |  73 +++-----
 src/plugins/qmlinspector/qmlinspectormode.h   |  15 --
 .../qmlinspector/qmlinspectorplugin.cpp       |  59 +------
 src/plugins/qmlinspector/qmlinspectorplugin.h |  12 +-
 src/plugins/qmlinspector/runcontrol.cpp       | 162 ------------------
 src/plugins/qmlinspector/runcontrol.h         |  94 ----------
 src/plugins/qmlprojectmanager/qmlproject.cpp  |  16 +-
 src/plugins/qmlprojectmanager/qmlproject.h    |   2 +
 9 files changed, 54 insertions(+), 385 deletions(-)
 delete mode 100644 src/plugins/qmlinspector/runcontrol.cpp
 delete mode 100644 src/plugins/qmlinspector/runcontrol.h

diff --git a/src/plugins/qmlinspector/qmlinspector.pro b/src/plugins/qmlinspector/qmlinspector.pro
index 7e9a4f1214f..55becbd1b6e 100644
--- a/src/plugins/qmlinspector/qmlinspector.pro
+++ b/src/plugins/qmlinspector/qmlinspector.pro
@@ -9,13 +9,11 @@ include(components/qmldebugger.pri)
 HEADERS += qmlinspectorplugin.h \
            qmlinspector.h \
            qmlinspectormode.h \
-           inspectoroutputpane.h \
-           runcontrol.h
+           inspectoroutputpane.h
 
 SOURCES += qmlinspectorplugin.cpp \
            qmlinspectormode.cpp \
-           inspectoroutputpane.cpp \
-           runcontrol.cpp 
+           inspectoroutputpane.cpp
 
 OTHER_FILES += QmlInspector.pluginspec
 RESOURCES += qmlinspector.qrc
diff --git a/src/plugins/qmlinspector/qmlinspectormode.cpp b/src/plugins/qmlinspector/qmlinspectormode.cpp
index 69da5c2a054..7af3a4fdb91 100644
--- a/src/plugins/qmlinspector/qmlinspectormode.cpp
+++ b/src/plugins/qmlinspector/qmlinspectormode.cpp
@@ -75,6 +75,8 @@
 #include <QtGui/qlabel.h>
 #include <QtGui/qspinbox.h>
 
+#include <QtNetwork/QHostAddress>
+
 
 QT_BEGIN_NAMESPACE
 
@@ -156,17 +158,11 @@ QmlInspectorMode::QmlInspectorMode(QObject *parent)
 {    
     m_watchTableModel = new WatchTableModel(0, this);
 
-    initActions();
     setWidget(createModeWindow());
 
     setName(tr("QML Inspect"));
     setIcon(QIcon(":/qmlinspector/images/logo.png"));
-    setUniqueModeName("QML_INSPECT_MODE");    
-}
-
-quint16 QmlInspectorMode::viewerPort() const
-{
-    return m_portSpinBox->value();
+    setUniqueModeName("QML_INSPECT_MODE");
 }
 
 void QmlInspectorMode::connectToViewer()
@@ -181,12 +177,30 @@ void QmlInspectorMode::connectToViewer()
         delete m_conn;
     }
 
+    ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->currentProject();
+    if (!project) {
+        emit statusMessage(tr("No active project, debugging canceled."));
+        return;
+    }
+
+    ProjectExplorer::RunConfiguration* config = project->activeRunConfiguration();
+    if (!config) {
+        emit statusMessage(tr("Cannot find project run configuration, debugging canceled."));
+        return;
+    }
+
+    // TODO load from QmlProject settings!!
+    QHostAddress host = QHostAddress::LocalHost;
+    quint16 port = 3768;
+
     m_conn = new QmlDebugConnection(this);
     connect(m_conn, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
             SLOT(connectionStateChanged()));
     connect(m_conn, SIGNAL(error(QAbstractSocket::SocketError)),
             SLOT(connectionError()));
-    m_conn->connectToHost(m_addressEdit->text(), m_portSpinBox->value());
+
+    emit statusMessage(tr("[Inspector] set to connect to debug server %1:%2").arg(host.toString()).arg(port));
+    m_conn->connectToHost(host, port);
 }
 
 void QmlInspectorMode::disconnectFromViewer()
@@ -201,8 +215,6 @@ void QmlInspectorMode::connectionStateChanged()
         case QAbstractSocket::UnconnectedState:
         {
             emit statusMessage(tr("[Inspector] disconnected.\n\n"));
-            m_addressEdit->setEnabled(true);
-            m_portSpinBox->setEnabled(true);
 
             delete m_engineQuery;
             m_engineQuery = 0;
@@ -219,8 +231,6 @@ void QmlInspectorMode::connectionStateChanged()
         case QAbstractSocket::ConnectedState:
         {
             emit statusMessage(tr("[Inspector] connected.\n"));
-            m_addressEdit->setEnabled(false);
-            m_portSpinBox->setEnabled(false);
 
             if (!m_client) {
                 m_client = new QmlEngineDebug(m_conn, this);
@@ -251,29 +261,6 @@ void QmlInspectorMode::connectionError()
             .arg(m_conn->error()).arg(m_conn->errorString()));
 }
 
-void QmlInspectorMode::initActions()
-{
-    m_actions.startAction = new QAction(tr("Start Inspector"), this);
-    m_actions.startAction->setIcon(QIcon(ProjectExplorer::Constants::ICON_RUN));
-    
-    m_actions.stopAction = new QAction(tr("Stop Inspector"), this);
-    m_actions.stopAction->setIcon(QIcon(ProjectExplorer::Constants::ICON_STOP));
-
-    Core::ICore *core = Core::ICore::instance();
-    Core::ActionManager *am = core->actionManager();
-    Core::UniqueIDManager *uidm = core->uniqueIDManager();
-
-    QList<int> context;
-    context << uidm->uniqueIdentifier(QmlInspector::Constants::C_INSPECTOR);
-
-    am->registerAction(m_actions.startAction, QmlInspector::Constants::RUN, context);
-    connect(m_actions.startAction, SIGNAL(triggered()), SIGNAL(startViewer()));
-    
-    am->registerAction(m_actions.stopAction, QmlInspector::Constants::STOP, context);
-    connect(m_actions.stopAction, SIGNAL(triggered()), SIGNAL(stopViewer()));
-}    
-
-
 QToolButton *QmlInspectorMode::createToolButton(QAction *action)
 {
     QToolButton *button = new QToolButton;
@@ -329,10 +316,9 @@ QWidget *QmlInspectorMode::createMainView()
     
     Core::ICore *core = Core::ICore::instance();
     Core::ActionManager *am = core->actionManager();    
-    configBarLayout->addWidget(createToolButton(am->command(QmlInspector::Constants::RUN)->action()));
-    configBarLayout->addWidget(createToolButton(am->command(QmlInspector::Constants::STOP)->action()));
-    configBarLayout->addWidget(m_addressEdit);
-    configBarLayout->addWidget(m_portSpinBox);
+    configBarLayout->addWidget(createToolButton(am->command(ProjectExplorer::Constants::DEBUG)->action()));
+    configBarLayout->addWidget(createToolButton(am->command(ProjectExplorer::Constants::STOP)->action()));
+
     configBarLayout->addStretch();
        
     QWidget *widgetAboveTabs = new QWidget;
@@ -450,15 +436,6 @@ void QmlInspectorMode::initWidgets()
     connect(m_objectTreeWidget, SIGNAL(currentObjectChanged(QmlDebugObjectReference)),
             m_expressionWidget, SLOT(setCurrentObject(QmlDebugObjectReference)));
 
-    m_addressEdit = new QLineEdit;
-    m_addressEdit->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
-    m_addressEdit->setText("127.0.0.1");
-
-    m_portSpinBox = new QSpinBox;
-    m_portSpinBox->setMinimum(1024);
-    m_portSpinBox->setMaximum(20000);
-    m_portSpinBox->setValue(3768);
-
     m_engineSpinBox = new EngineSpinBox;
     m_engineSpinBox->setEnabled(false);
     connect(m_engineSpinBox, SIGNAL(valueChanged(int)),
diff --git a/src/plugins/qmlinspector/qmlinspectormode.h b/src/plugins/qmlinspector/qmlinspectormode.h
index 00f136dba2e..764fde4ba3c 100644
--- a/src/plugins/qmlinspector/qmlinspectormode.h
+++ b/src/plugins/qmlinspector/qmlinspectormode.h
@@ -62,12 +62,7 @@ class QmlInspectorMode : public Core::BaseMode
 public:
     QmlInspectorMode(QObject *parent = 0);
     
-    
-    quint16 viewerPort() const;
-    
 signals:
-    void startViewer();
-    void stopViewer();
     void statusMessage(const QString &text);
     
 public slots: 
@@ -84,20 +79,12 @@ private slots:
     void treeObjectActivated(const QmlDebugObjectReference &obj);
 
 private:
-    struct Actions {
-        QAction *startAction;
-        QAction *stopAction;
-    };
-    
-    void initActions();
     QWidget *createModeWindow();
     QWidget *createMainView();
     void initWidgets();
     QWidget *createBottomWindow();
     QToolButton *createToolButton(QAction *action);
     
-    Actions m_actions;
-    
     QmlDebugConnection *m_conn;
     QmlEngineDebug *m_client;
 
@@ -111,8 +98,6 @@ private:
     CanvasFrameRate *m_frameRateWidget;
     ExpressionQueryWidget *m_expressionWidget;
 
-    QLineEdit *m_addressEdit;
-    QSpinBox *m_portSpinBox;
     EngineSpinBox *m_engineSpinBox;
 };
 
diff --git a/src/plugins/qmlinspector/qmlinspectorplugin.cpp b/src/plugins/qmlinspector/qmlinspectorplugin.cpp
index a81184b379f..2ca3f0c5416 100644
--- a/src/plugins/qmlinspector/qmlinspectorplugin.cpp
+++ b/src/plugins/qmlinspector/qmlinspectorplugin.cpp
@@ -26,7 +26,6 @@
 ** contact the sales department at http://qt.nokia.com/contact.
 **
 **************************************************************************/
-#include "runcontrol.h"
 #include "qmlinspector.h"
 #include "qmlinspectormode.h"
 #include "inspectoroutputpane.h"
@@ -36,6 +35,7 @@
 #include <private/qmldebugclient_p.h>
 
 #include <coreplugin/icore.h>
+#include <coreplugin/modemanager.h>
 
 #include <projectexplorer/runconfiguration.h>
 #include <projectexplorer/projectexplorer.h>
@@ -55,8 +55,7 @@ QT_BEGIN_NAMESPACE
 
 
 QmlInspectorPlugin::QmlInspectorPlugin()
-    : m_inspectMode(0),
-      m_runControl(0)
+    : m_inspectMode(0)
 {
 }
 
@@ -89,8 +88,6 @@ bool QmlInspectorPlugin::initialize(const QStringList &arguments, QString *error
     context.append(uidm->uniqueIdentifier(Core::Constants::C_NAVIGATION_PANE));
 
     m_inspectMode = new QmlInspectorMode(this);
-    connect(m_inspectMode, SIGNAL(startViewer()), SLOT(startViewer()));
-    connect(m_inspectMode, SIGNAL(stopViewer()), SLOT(stopViewer()));
     m_inspectMode->setContext(context);
     addObject(m_inspectMode);
 
@@ -100,9 +97,9 @@ bool QmlInspectorPlugin::initialize(const QStringList &arguments, QString *error
     connect(m_inspectMode, SIGNAL(statusMessage(QString)),
             m_outputPane, SLOT(addInspectorStatus(QString)));
 
-    m_runControlFactory = new QmlInspectorRunControlFactory(this);
-    addAutoReleasedObject(m_runControlFactory);
-    
+    connect(core->modeManager(), SIGNAL(currentModeChanged(Core::IMode*)),
+            SLOT(currentModeChanged(Core::IMode*)));
+
     return true;
 }
 
@@ -110,50 +107,10 @@ void QmlInspectorPlugin::extensionsInitialized()
 {
 }
 
-void QmlInspectorPlugin::startViewer()
-{
-    stopViewer();
-    
-    ProjectExplorer::Project *project = 0;
-    ProjectExplorer::ProjectExplorerPlugin *plugin = ProjectExplorer::ProjectExplorerPlugin::instance();
-    if (plugin)
-        project = plugin->currentProject();
-    if (!project) {
-        qDebug() << "No project loaded"; // TODO should this just run the debugger without a viewer?
-        return;
-    }
-        
-    ProjectExplorer::RunConfiguration *rc = project->activeRunConfiguration();
-
-    QmlInspector::StartParameters sp;
-    sp.port = m_inspectMode->viewerPort();
-
-    m_runControl = m_runControlFactory->create(rc, ProjectExplorer::Constants::RUNMODE, sp);
-
-    if (m_runControl) {
-        connect(m_runControl, SIGNAL(started()), m_inspectMode, SLOT(connectToViewer()));
-        connect(m_runControl, SIGNAL(finished()), m_inspectMode, SLOT(disconnectFromViewer()));
-
-        connect(m_runControl, SIGNAL(addToOutputWindow(RunControl*,QString)),
-                m_outputPane, SLOT(addOutput(RunControl*,QString)));
-        connect(m_runControl, SIGNAL(addToOutputWindowInline(RunControl*,QString)),
-                m_outputPane, SLOT(addOutputInline(RunControl*,QString)));
-        connect(m_runControl, SIGNAL(error(RunControl*,QString)),
-                m_outputPane, SLOT(addErrorOutput(RunControl*,QString)));
- 
-        m_runControl->start();
-        m_outputPane->popup(false);
-    }
-    
-}
-
-void QmlInspectorPlugin::stopViewer()
+void QmlInspectorPlugin::currentModeChanged(Core::IMode *mode)
 {
-    if (m_runControl) {
-        m_runControl->stop();
-        m_runControl->deleteLater();
-        m_runControl = 0;
-    }
+    if (mode == m_inspectMode) 
+        m_inspectMode->connectToViewer();
 }
 
 
diff --git a/src/plugins/qmlinspector/qmlinspectorplugin.h b/src/plugins/qmlinspector/qmlinspectorplugin.h
index a1735d4c3bf..ccec2f9b7c5 100644
--- a/src/plugins/qmlinspector/qmlinspectorplugin.h
+++ b/src/plugins/qmlinspector/qmlinspectorplugin.h
@@ -38,14 +38,12 @@ QT_BEGIN_NAMESPACE
 
 class QStringList;
 
-
-class QmlInspectorRunControlFactory;
 class QmlInspectorMode;
 class InspectorOutputPane;
 
-namespace ProjectExplorer
+namespace Core
 {
-    class RunControl;
+    class IMode;
 }
 
 class QmlInspectorPlugin : public ExtensionSystem::IPlugin
@@ -61,15 +59,11 @@ public:
     virtual void shutdown();
 
 private slots:
-    void startViewer();
-    void stopViewer();
+    void currentModeChanged(Core::IMode *mode);
 
 private:
     QmlInspectorMode *m_inspectMode;
     InspectorOutputPane *m_outputPane;
-    
-    QmlInspectorRunControlFactory *m_runControlFactory;
-    QPointer<ProjectExplorer::RunControl> m_runControl;
 };
 
 
diff --git a/src/plugins/qmlinspector/runcontrol.cpp b/src/plugins/qmlinspector/runcontrol.cpp
deleted file mode 100644
index 72254310594..00000000000
--- a/src/plugins/qmlinspector/runcontrol.cpp
+++ /dev/null
@@ -1,162 +0,0 @@
-/**************************************************************************
-**
-** This file is part of Qt Creator
-**
-** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
-**
-** Contact: Nokia Corporation (qt-info@nokia.com)
-**
-** Commercial Usage
-**
-** Licensees holding valid Qt Commercial licenses may use this file in
-** accordance with the Qt Commercial License Agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and Nokia.
-**
-** GNU Lesser General Public License Usage
-**
-** 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.
-**
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at http://qt.nokia.com/contact.
-**
-**************************************************************************/
-#include "runcontrol.h"
-
-#include <projectexplorer/applicationlauncher.h>
-#include <projectexplorer/applicationrunconfiguration.h>
-#include <projectexplorer/projectexplorerconstants.h>
-
-#include <QtCore/qdebug.h>
-#include <QtCore/qtimer.h>
-
-using namespace ProjectExplorer;
-
-
-QmlInspectorRunControlFactory::QmlInspectorRunControlFactory(QObject *parent)
-    : ProjectExplorer::IRunControlFactory(parent)
-{
-}
-
-bool QmlInspectorRunControlFactory::canRun(RunConfiguration *runConfiguration, const QString &mode) const
-{
-    Q_UNUSED(runConfiguration);
-    if (mode != ProjectExplorer::Constants::RUNMODE)
-        return false;
-    return true;
-}
-
-ProjectExplorer::RunControl *QmlInspectorRunControlFactory::create(RunConfiguration *runConfiguration, const QString &mode)
-{
-    Q_UNUSED(mode);
-    return new QmlInspectorRunControl(runConfiguration);
-}
-
-ProjectExplorer::RunControl *QmlInspectorRunControlFactory::create(ProjectExplorer::RunConfiguration *runConfiguration,
-const QString &mode, const QmlInspector::StartParameters &sp)
-{
-    Q_UNUSED(mode);
-    return new QmlInspectorRunControl(runConfiguration, sp);
-}
-                
-QString QmlInspectorRunControlFactory::displayName() const
-{
-    return tr("Qml Inspector");
-}
-
-QWidget *QmlInspectorRunControlFactory::configurationWidget(RunConfiguration *runConfiguration)
-{
-    Q_UNUSED(runConfiguration);
-    return 0;
-}
-
-
-
-QmlInspectorRunControl::QmlInspectorRunControl(ProjectExplorer::RunConfiguration *runConfiguration,
-const QmlInspector::StartParameters &sp)
-    : ProjectExplorer::RunControl(runConfiguration),
-      m_configuration(runConfiguration),
-      m_running(false),
-      m_viewerLauncher(0),
-      m_startParams(sp)
-{
-}
-
-QmlInspectorRunControl::~QmlInspectorRunControl()
-{
-}
-
-void QmlInspectorRunControl::start()
-{
-    if (m_running || m_viewerLauncher)
-        return;
-
-    m_viewerLauncher = new ProjectExplorer::ApplicationLauncher(this);
-    connect(m_viewerLauncher, SIGNAL(applicationError(QString)), SLOT(applicationError(QString)));
-    connect(m_viewerLauncher, SIGNAL(processExited(int)), SLOT(viewerExited()));
-    connect(m_viewerLauncher, SIGNAL(appendOutput(QString)), SLOT(appendOutput(QString)));
-    connect(m_viewerLauncher, SIGNAL(bringToForegroundRequested(qint64)),
-            this, SLOT(appStarted()));
-
-    LocalApplicationRunConfiguration *rc = qobject_cast<LocalApplicationRunConfiguration *>(m_configuration);
-    if (!rc)
-        return;
-
-    ProjectExplorer::Environment env = rc->environment();
-    env.set("QML_DEBUG_SERVER_PORT", QString::number(m_startParams.port));
-
-    QStringList arguments = rc->commandLineArguments();
-    arguments << QLatin1String("-stayontop");
-
-    m_viewerLauncher->setEnvironment(env.toStringList());
-    m_viewerLauncher->setWorkingDirectory(rc->workingDirectory());
-
-    m_running = true;
-
-    m_viewerLauncher->start(static_cast<ApplicationLauncher::Mode>(rc->runMode()),
-                            rc->executable(), arguments);
-}
-
-void QmlInspectorRunControl::stop()
-{
-    if (m_viewerLauncher->isRunning())
-        m_viewerLauncher->stop();
-}
-
-bool QmlInspectorRunControl::isRunning() const
-{
-    return m_running;
-}
-
-void QmlInspectorRunControl::appStarted()
-{
-    QTimer::singleShot(500, this, SLOT(delayedStart()));
-}
-
-void QmlInspectorRunControl::appendOutput(const QString &s)
-{
-    emit addToOutputWindow(this, s);
-}
-
-void QmlInspectorRunControl::delayedStart()
-{
-    emit started();
-}
-
-void QmlInspectorRunControl::viewerExited()
-{
-    m_running = false;
-    emit finished();
-    
-    deleteLater();
-}
-
-void QmlInspectorRunControl::applicationError(const QString &s)
-{
-    emit error(this, s);
-}
diff --git a/src/plugins/qmlinspector/runcontrol.h b/src/plugins/qmlinspector/runcontrol.h
deleted file mode 100644
index b2523b5347a..00000000000
--- a/src/plugins/qmlinspector/runcontrol.h
+++ /dev/null
@@ -1,94 +0,0 @@
-/**************************************************************************
-**
-** This file is part of Qt Creator
-**
-** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
-**
-** Contact: Nokia Corporation (qt-info@nokia.com)
-**
-** Commercial Usage
-**
-** Licensees holding valid Qt Commercial licenses may use this file in
-** accordance with the Qt Commercial License Agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and Nokia.
-**
-** GNU Lesser General Public License Usage
-**
-** 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.
-**
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at http://qt.nokia.com/contact.
-**
-**************************************************************************/
-#ifndef QMLINSPECTORRUNCONTROL_H
-#define QMLINSPECTORRUNCONTROL_H
-
-#include "qmlinspector.h"
-
-#include <projectexplorer/runconfiguration.h>
-
-#include <QtCore/qobject.h>
-
-namespace ProjectExplorer {
-    class ApplicationLauncher;
-}
-
-class QmlInspectorRunControlFactory : public ProjectExplorer::IRunControlFactory
-{
-    Q_OBJECT
-
-public:
-    explicit QmlInspectorRunControlFactory(QObject *parent);
-
-    virtual bool canRun(
-                ProjectExplorer::RunConfiguration *runConfiguration,
-                const QString &mode) const;
-
-    virtual ProjectExplorer::RunControl *create(
-                ProjectExplorer::RunConfiguration *runConfiguration,
-                const QString &mode);
-
-    ProjectExplorer::RunControl *create(
-                ProjectExplorer::RunConfiguration *runConfiguration,
-                const QString &mode,
-                const QmlInspector::StartParameters &sp);
-                
-    virtual QString displayName() const;
-
-    virtual QWidget *configurationWidget(ProjectExplorer::RunConfiguration *runConfiguration);
-};
-
-class QmlInspectorRunControl : public ProjectExplorer::RunControl
-{
-    Q_OBJECT
-
-public:
-    explicit QmlInspectorRunControl(ProjectExplorer::RunConfiguration *runConfiguration,
-                                    const QmlInspector::StartParameters &sp = QmlInspector::StartParameters());
-    ~QmlInspectorRunControl();
-    
-    virtual void start();
-    virtual void stop();
-    virtual bool isRunning() const;
-
-private slots:
-    void appendOutput(const QString &s);
-    void appStarted();
-    void delayedStart();
-    void viewerExited();
-    void applicationError(const QString &error);
-
-private:
-    ProjectExplorer::RunConfiguration *m_configuration;
-    bool m_running;
-    ProjectExplorer::ApplicationLauncher *m_viewerLauncher;
-    QmlInspector::StartParameters m_startParams;
-};
-
-#endif
diff --git a/src/plugins/qmlprojectmanager/qmlproject.cpp b/src/plugins/qmlprojectmanager/qmlproject.cpp
index 1efd5af8d93..8725e4fb788 100644
--- a/src/plugins/qmlprojectmanager/qmlproject.cpp
+++ b/src/plugins/qmlprojectmanager/qmlproject.cpp
@@ -39,6 +39,7 @@
 #include <coreplugin/icore.h>
 #include <coreplugin/editormanager/editormanager.h>
 #include <coreplugin/editormanager/ieditor.h>
+#include <coreplugin/modemanager.h>
 
 #include <qmleditor/qmlmodelmanagerinterface.h>
 
@@ -59,6 +60,7 @@
 #include <QtGui/QLabel>
 #include <QtGui/QSpinBox>
 
+
 using namespace QmlProjectManager;
 using namespace QmlProjectManager::Internal;
 using namespace ProjectExplorer;
@@ -508,7 +510,7 @@ ProjectExplorer::RunConfiguration *QmlRunConfigurationFactory::create(ProjectExp
 }
 
 QmlRunControl::QmlRunControl(QmlRunConfiguration *runConfiguration, bool debugMode)
-    : RunControl(runConfiguration)
+    : RunControl(runConfiguration), m_debugMode(debugMode)
 {
     Environment environment = ProjectExplorer::Environment::systemEnvironment();
     if (debugMode)
@@ -527,7 +529,7 @@ QmlRunControl::QmlRunControl(QmlRunConfiguration *runConfiguration, bool debugMo
     connect(&m_applicationLauncher, SIGNAL(processExited(int)),
             this, SLOT(processExited(int)));
     connect(&m_applicationLauncher, SIGNAL(bringToForegroundRequested(qint64)),
-            this, SLOT(bringApplicationToForeground(qint64)));
+            this, SLOT(slotBringApplicationToForeground(qint64)));
 }
 
 QmlRunControl::~QmlRunControl()
@@ -551,6 +553,16 @@ bool QmlRunControl::isRunning() const
     return m_applicationLauncher.isRunning();
 }
 
+void QmlRunControl::slotBringApplicationToForeground(qint64 pid)
+{
+    if (m_debugMode) {
+        Core::ICore *core = Core::ICore::instance();
+        core->modeManager()->activateMode(QLatin1String("QML_INSPECT_MODE"));
+    }
+
+    bringApplicationToForeground(pid);
+}
+
 void QmlRunControl::slotError(const QString &err)
 {
     emit error(this, err);
diff --git a/src/plugins/qmlprojectmanager/qmlproject.h b/src/plugins/qmlprojectmanager/qmlproject.h
index 5c89239dcca..454981b9d53 100644
--- a/src/plugins/qmlprojectmanager/qmlproject.h
+++ b/src/plugins/qmlprojectmanager/qmlproject.h
@@ -209,6 +209,7 @@ public:
 
 private slots:
     void processExited(int exitCode);
+    void slotBringApplicationToForeground(qint64 pid);
     void slotAddToOutputWindow(const QString &line);
     void slotError(const QString & error);
 
@@ -217,6 +218,7 @@ private:
 
     QString m_executable;
     QStringList m_commandLineArguments;
+    bool m_debugMode;
 };
 
 class QmlRunControlFactory : public ProjectExplorer::IRunControlFactory {
-- 
GitLab