From 70bcccd646d2aeac3aabf9bc6c81ed42c563aeb2 Mon Sep 17 00:00:00 2001
From: Aurindam Jana <aurindam.jana@nokia.com>
Date: Mon, 16 Jan 2012 12:38:33 +0100
Subject: [PATCH] ScriptConsole: Show current context

Show the current context in the script console. The expression
in the script console is evaluated within this context.

Change-Id: Ieb4cfc3e0892b150301f4ad79220cd878dee3ce3
Reviewed-by: hjk <qthjk@ovi.com>
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
---
 src/plugins/debugger/qml/qmljsscriptconsole.cpp | 17 +++++++++++------
 src/plugins/debugger/stackhandler.cpp           | 11 +++++++----
 src/plugins/debugger/stackhandler.h             |  1 +
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/plugins/debugger/qml/qmljsscriptconsole.cpp b/src/plugins/debugger/qml/qmljsscriptconsole.cpp
index 21fb2281ae5..5098dcd8ec6 100644
--- a/src/plugins/debugger/qml/qmljsscriptconsole.cpp
+++ b/src/plugins/debugger/qml/qmljsscriptconsole.cpp
@@ -49,6 +49,8 @@
 #include <qmljsdebugclient/qdebugmessageclient.h>
 #include <debugger/qml/qmlcppengine.h>
 #include <debugger/qml/qmlengine.h>
+#include <debugger/stackhandler.h>
+#include <debugger/stackframe.h>
 
 #include <QtGui/QMenu>
 #include <QtGui/QTextBlock>
@@ -289,17 +291,19 @@ void QmlJSScriptConsole::setInferiorStopped(bool inferiorStopped)
     onSelectionChanged();
 }
 
-void QmlJSScriptConsole::setEngine(QmlEngine *engine)
+void QmlJSScriptConsole::setEngine(QmlEngine *eng)
 {
     if (d->adapter) {
+        disconnect(engine()->stackHandler(), SIGNAL(currentIndexChanged()), this, SLOT(onSelectionChanged()));
         disconnect(d->adapter, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
         disconnect(d->adapter->messageClient(), SIGNAL(message(QtMsgType,QString)),
                 this, SLOT(insertDebugOutput(QtMsgType,QString)));
         d->adapter = 0;
     }
 
-    if (engine) {
-        d->adapter = engine->adapter();
+    if (eng) {
+        d->adapter = eng->adapter();
+        connect(eng->stackHandler(), SIGNAL(currentIndexChanged()), this, SLOT(onSelectionChanged()));
         connect(d->adapter, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
         connect(d->adapter->messageClient(), SIGNAL(message(QtMsgType,QString)),
                 this, SLOT(insertDebugOutput(QtMsgType,QString)));
@@ -308,7 +312,7 @@ void QmlJSScriptConsole::setEngine(QmlEngine *engine)
     clear();
 }
 
-DebuggerEngine * QmlJSScriptConsole::engine()
+DebuggerEngine *QmlJSScriptConsole::engine()
 {
     if (d->adapter) {
         return d->adapter->debuggerEngine();
@@ -372,10 +376,11 @@ void QmlJSScriptConsole::onStateChanged(QmlJsDebugClient::QDeclarativeDebugQuery
 void QmlJSScriptConsole::onSelectionChanged()
 {
     if (d->adapter) {
-        QString status;
+        QString status(tr("Context: "));
         if (!d->inferiorStopped) {
-            status.append(tr("Current Selected Object: "));
             status.append(d->adapter->currentSelectedDisplayName());
+        } else {
+            status.append(engine()->stackHandler()->currentFrame().function);
         }
         emit updateStatusMessage(status, 0);
     }
diff --git a/src/plugins/debugger/stackhandler.cpp b/src/plugins/debugger/stackhandler.cpp
index 37eb25d16af..d88acc41c1b 100644
--- a/src/plugins/debugger/stackhandler.cpp
+++ b/src/plugins/debugger/stackhandler.cpp
@@ -62,7 +62,7 @@ StackHandler::StackHandler()
 {
     m_resetLocationScheduled = false;
     m_contentsValid = false;
-    m_currentIndex = 0;
+    m_currentIndex = -1;
     m_canExpand = false;
     connect(debuggerCore()->action(OperateByInstruction), SIGNAL(triggered()),
         this, SLOT(resetModel()));
@@ -160,6 +160,8 @@ Qt::ItemFlags StackHandler::flags(const QModelIndex &index) const
 
 StackFrame StackHandler::currentFrame() const
 {
+    if (m_currentIndex == -1)
+        return StackFrame();
     QTC_ASSERT(m_currentIndex >= 0, return StackFrame());
     QTC_ASSERT(m_currentIndex < m_stackFrames.size(), return StackFrame());
     return m_stackFrames.at(m_currentIndex);
@@ -167,7 +169,7 @@ StackFrame StackHandler::currentFrame() const
 
 void StackHandler::setCurrentIndex(int level)
 {
-    if (level == m_currentIndex)
+    if (level == -1 || level == m_currentIndex)
         return;
 
     // Emit changed for previous frame
@@ -175,6 +177,7 @@ void StackHandler::setCurrentIndex(int level)
     emit dataChanged(i, i);
 
     m_currentIndex = level;
+    emit currentIndexChanged();
 
     // Emit changed for new frame
     i = index(m_currentIndex, 0);
@@ -184,7 +187,7 @@ void StackHandler::setCurrentIndex(int level)
 void StackHandler::removeAll()
 {
     m_stackFrames.clear();
-    m_currentIndex = 0;
+    setCurrentIndex(-1);
     reset();
 }
 
@@ -195,7 +198,7 @@ void StackHandler::setFrames(const StackFrames &frames, bool canExpand)
     m_canExpand = canExpand;
     m_stackFrames = frames;
     if (m_currentIndex >= m_stackFrames.size())
-        m_currentIndex = m_stackFrames.size() - 1;
+        setCurrentIndex(m_stackFrames.size() - 1);
     reset();
     emit stackChanged();
 }
diff --git a/src/plugins/debugger/stackhandler.h b/src/plugins/debugger/stackhandler.h
index 2a29c523d37..afbe1410dfe 100644
--- a/src/plugins/debugger/stackhandler.h
+++ b/src/plugins/debugger/stackhandler.h
@@ -87,6 +87,7 @@ public:
 
 signals:
     void stackChanged();
+    void currentIndexChanged();
 
 private:
     // QAbstractTableModel
-- 
GitLab