From c2c58fecd7adf6bf42f73559ef20e5f500b982e1 Mon Sep 17 00:00:00 2001
From: Aurindam Jana <aurindam.jana@digia.com>
Date: Fri, 5 Oct 2012 13:42:14 +0200
Subject: [PATCH] Inspector: Set the context of console

The current selected item in the inspector view
is set as the context of the console.

Task-number: QTCREATORBUG-7439

Change-Id: Ibc980218751ce4afacf714cf1ab34f0a36550b2c
Reviewed-by: hjk <qthjk@ovi.com>
Reviewed-by: Kai Koehne <kai.koehne@digia.com>
---
 src/plugins/debugger/basewindow.h             |  1 +
 .../debugger/localsandexpressionswindow.cpp   | 11 +++++-
 .../debugger/localsandexpressionswindow.h     |  1 +
 src/plugins/debugger/qml/qmlengine.cpp        | 38 ++++++++++++++++---
 src/plugins/debugger/qml/qmlengine.h          |  3 ++
 .../debugger/qml/qmlinspectoradapter.cpp      |  7 +---
 .../debugger/qml/qmlinspectoradapter.h        |  1 -
 src/plugins/debugger/watchwindow.cpp          |  5 +++
 src/plugins/debugger/watchwindow.h            |  4 ++
 9 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/src/plugins/debugger/basewindow.h b/src/plugins/debugger/basewindow.h
index 733d3ddd504..58987041781 100644
--- a/src/plugins/debugger/basewindow.h
+++ b/src/plugins/debugger/basewindow.h
@@ -53,6 +53,7 @@ public:
     void setModel(QAbstractItemModel *model) { m_treeView->setModel(model); }
     QHeaderView *header() const { return m_treeView->header(); }
     QAbstractItemModel *model() const { return m_treeView->model(); }
+    QTreeView *treeView() const { return m_treeView; }
 
 private:
     QTreeView *m_treeView;
diff --git a/src/plugins/debugger/localsandexpressionswindow.cpp b/src/plugins/debugger/localsandexpressionswindow.cpp
index 0211c3c6cad..5e1bf28006e 100644
--- a/src/plugins/debugger/localsandexpressionswindow.cpp
+++ b/src/plugins/debugger/localsandexpressionswindow.cpp
@@ -35,6 +35,9 @@
 #include <QSplitter>
 #include <QStackedWidget>
 
+const int LOCAL_WIDGET_INDEX = 0;
+const int INSPECTOR_WIDGET_INDEX = 1;
+
 namespace Debugger {
 namespace Internal {
 
@@ -79,7 +82,13 @@ void LocalsAndExpressionsWindow::setShowLocals(bool showLocals)
 
 void LocalsAndExpressionsWindow::showLocals()
 {
-    m_localsAndInspector->setCurrentIndex(m_showLocals ? 0 : 1);
+    m_localsAndInspector->setCurrentIndex(m_showLocals ? LOCAL_WIDGET_INDEX
+                                                       : INSPECTOR_WIDGET_INDEX);
+}
+
+QWidget *LocalsAndExpressionsWindow::inspectorWidget() const
+{
+    return m_localsAndInspector->widget(INSPECTOR_WIDGET_INDEX);
 }
 
 } // namespace Internal
diff --git a/src/plugins/debugger/localsandexpressionswindow.h b/src/plugins/debugger/localsandexpressionswindow.h
index bf59a2618fe..ec7df5dcfed 100644
--- a/src/plugins/debugger/localsandexpressionswindow.h
+++ b/src/plugins/debugger/localsandexpressionswindow.h
@@ -50,6 +50,7 @@ public:
             QWidget *returnWidget, QWidget *watchers, QWidget *parent = 0);
 
     void setShowLocals(bool showLocals);
+    QWidget *inspectorWidget() const;
 
 private slots:
     void showLocals();
diff --git a/src/plugins/debugger/qml/qmlengine.cpp b/src/plugins/debugger/qml/qmlengine.cpp
index d16c71c2a70..cb1c86e472f 100644
--- a/src/plugins/debugger/qml/qmlengine.cpp
+++ b/src/plugins/debugger/qml/qmlengine.cpp
@@ -41,6 +41,8 @@
 #include "debuggerrunner.h"
 #include "debuggerstringutils.h"
 #include "debuggertooltipmanager.h"
+#include "localsandexpressionswindow.h"
+#include "watchwindow.h"
 
 #include "breakhandler.h"
 #include "moduleshandler.h"
@@ -82,6 +84,8 @@
 #include <QTcpSocket>
 #include <QHostAddress>
 
+#include <QDockWidget>
+
 #define DEBUG_QML 1
 #if DEBUG_QML
 #   define SDEBUG(s) qDebug() << s
@@ -292,7 +296,7 @@ QmlEngine::QmlEngine(const DebuggerStartParameters &startParameters)
             SLOT(updateCurrentContext()));
     connect(this->stackHandler(), SIGNAL(currentIndexChanged()),
             SLOT(updateCurrentContext()));
-    connect(&m_inspectorAdapter, SIGNAL(selectionChanged()),
+    connect(inspectorTreeView(), SIGNAL(currentIndexChanged(QModelIndex)),
             SLOT(updateCurrentContext()));
     connect(m_inspectorAdapter.agent(), SIGNAL(
                 expressionResult(quint32,QVariant)),
@@ -1137,9 +1141,23 @@ void QmlEngine::documentUpdated(QmlJS::Document::Ptr doc)
 
 void QmlEngine::updateCurrentContext()
 {
-    const QString context = state() == InferiorStopOk ?
-                stackHandler()->currentFrame().function
-              : m_inspectorAdapter.currentSelectedDisplayName();
+    QString context;
+    if (state() == InferiorStopOk) {
+        context = stackHandler()->currentFrame().function;
+    } else {
+        QModelIndex currentIndex = inspectorTreeView()->currentIndex();
+        const WatchData *currentData = watchHandler()->watchData(currentIndex);
+        const WatchData *parentData = watchHandler()->watchData(currentIndex.parent());
+        const WatchData *grandParentData = watchHandler()->watchData(
+                    currentIndex.parent().parent());
+        if (currentData->id != parentData->id)
+            context = currentData->name;
+        else if (parentData->id != grandParentData->id)
+            context = parentData->name;
+        else
+            context = grandParentData->name;
+    }
+
     QmlJS::ConsoleManagerInterface *consoleManager = qmlConsoleManager();
     if (consoleManager)
         consoleManager->setContext(tr("Context: ").append(context));
@@ -1187,8 +1205,9 @@ bool QmlEngine::evaluateScript(const QString &expression)
     // Evaluate expression based on engine state
     // When engine->state() == InferiorStopOk, the expression is sent to debuggerClient.
     if (state() != InferiorStopOk) {
+        QModelIndex currentIndex = inspectorTreeView()->currentIndex();
         QmlInspectorAgent *agent = m_inspectorAdapter.agent();
-        quint32 queryId = agent->queryExpressionResult(m_inspectorAdapter.currentSelectedDebugId(),
+        quint32 queryId = agent->queryExpressionResult(watchHandler()->watchData(currentIndex)->id,
                                                        expression);
         if (queryId) {
             queryIds << queryId;
@@ -1333,6 +1352,15 @@ bool QmlEngine::adjustBreakpointLineAndColumn(
     return success;
 }
 
+WatchTreeView *QmlEngine::inspectorTreeView() const
+{
+    DebuggerMainWindow *dw = qobject_cast<DebuggerMainWindow *>(debuggerCore()->mainWindow());
+    LocalsAndExpressionsWindow *leW = qobject_cast<LocalsAndExpressionsWindow *>(
+                dw->dockWidget(QLatin1String(Constants::DOCKWIDGET_WATCHERS))->widget());
+    WatchWindow *inspectorWindow = qobject_cast<WatchWindow *>(leW->inspectorWidget());
+    return qobject_cast<WatchTreeView *>(inspectorWindow->treeView());
+}
+
 DebuggerEngine *createQmlEngine(const DebuggerStartParameters &sp)
 {
     return new QmlEngine(sp);
diff --git a/src/plugins/debugger/qml/qmlengine.h b/src/plugins/debugger/qml/qmlengine.h
index 7f515403332..fa1f3123838 100644
--- a/src/plugins/debugger/qml/qmlengine.h
+++ b/src/plugins/debugger/qml/qmlengine.h
@@ -53,6 +53,7 @@ namespace Debugger {
 namespace Internal {
 
 class QmlAdapter;
+class WatchTreeView;
 
 class QmlEngine : public DebuggerEngine, QmlJS::IScriptEvaluator
 {
@@ -183,6 +184,8 @@ private:
     bool adjustBreakpointLineAndColumn(const QString &filePath, quint32 *line,
                                        quint32 *column, bool *valid);
 
+    WatchTreeView *inspectorTreeView() const;
+
     QmlAdapter m_adapter;
     QmlInspectorAdapter m_inspectorAdapter;
     ProjectExplorer::ApplicationLauncher m_applicationLauncher;
diff --git a/src/plugins/debugger/qml/qmlinspectoradapter.cpp b/src/plugins/debugger/qml/qmlinspectoradapter.cpp
index 8a4f2b064ac..c2e51c76a04 100644
--- a/src/plugins/debugger/qml/qmlinspectoradapter.cpp
+++ b/src/plugins/debugger/qml/qmlinspectoradapter.cpp
@@ -509,12 +509,7 @@ void QmlInspectorAdapter::selectObject(const ObjectReference &obj,
     if (target == EditorTarget)
         gotoObjectReferenceDefinition(obj.source());
 
-    if (!agent()->selectObjectInTree(obj.debugId()))
-        return;
-
-    m_currentSelectedDebugId = obj.debugId();
-    m_currentSelectedDebugName = agent()->displayName(obj.debugId());
-    emit selectionChanged();
+    agent()->selectObjectInTree(obj.debugId());
 }
 
 void QmlInspectorAdapter::deletePreviews()
diff --git a/src/plugins/debugger/qml/qmlinspectoradapter.h b/src/plugins/debugger/qml/qmlinspectoradapter.h
index dc5272d7828..11833e31732 100644
--- a/src/plugins/debugger/qml/qmlinspectoradapter.h
+++ b/src/plugins/debugger/qml/qmlinspectoradapter.h
@@ -75,7 +75,6 @@ public:
 
 signals:
     void expressionResult();
-    void selectionChanged();
 
 private slots:
     void clientStatusChanged(QmlDebug::ClientStatus status);
diff --git a/src/plugins/debugger/watchwindow.cpp b/src/plugins/debugger/watchwindow.cpp
index 9a199d72bea..331e5a8758a 100644
--- a/src/plugins/debugger/watchwindow.cpp
+++ b/src/plugins/debugger/watchwindow.cpp
@@ -972,6 +972,11 @@ bool WatchTreeView::event(QEvent *ev)
     return BaseTreeView::event(ev);
 }
 
+void WatchTreeView::currentChanged(const QModelIndex &current, const QModelIndex &)
+{
+    emit currentIndexChanged(current);
+}
+
 void WatchTreeView::editItem(const QModelIndex &idx)
 {
     Q_UNUSED(idx) // FIXME
diff --git a/src/plugins/debugger/watchwindow.h b/src/plugins/debugger/watchwindow.h
index 72912cd2809..16aaf5c9e2c 100644
--- a/src/plugins/debugger/watchwindow.h
+++ b/src/plugins/debugger/watchwindow.h
@@ -58,6 +58,9 @@ public slots:
     void watchExpression(const QString &exp, const QString &name);
     void handleItemIsExpanded(const QModelIndex &idx);
 
+signals:
+    void currentIndexChanged(const QModelIndex &currentIndex);
+
 private:
     Q_SLOT void resetHelper();
     Q_SLOT void expandNode(const QModelIndex &idx);
@@ -70,6 +73,7 @@ private:
     void dragMoveEvent(QDragMoveEvent *ev);
     void mouseDoubleClickEvent(QMouseEvent *ev);
     bool event(QEvent *ev);
+    void currentChanged(const QModelIndex &current, const QModelIndex &previous);
 
     void editItem(const QModelIndex &idx);
     void resetHelper(const QModelIndex &idx);
-- 
GitLab