From 84b82446e09191e64d3af5b4dc73a59a0d2adb3f Mon Sep 17 00:00:00 2001
From: Aurindam Jana <aurindam.jana@nokia.com>
Date: Tue, 31 Jan 2012 16:23:34 +0100
Subject: [PATCH] ScriptConsole: Enable for mixed debugging

Change-Id: I00f3ebd2d6fff34424b1c3c291e8bd715387acfb
Reviewed-by: Kai Koehne <kai.koehne@nokia.com>
---
 src/plugins/debugger/debuggerengine.cpp       |  3 +-
 .../debugger/qml/qmljsscriptconsole.cpp       | 81 ++++++++++---------
 src/plugins/debugger/qml/qmljsscriptconsole.h |  4 +-
 3 files changed, 47 insertions(+), 41 deletions(-)

diff --git a/src/plugins/debugger/debuggerengine.cpp b/src/plugins/debugger/debuggerengine.cpp
index 8e6d1fd61eb..aa2ce279136 100644
--- a/src/plugins/debugger/debuggerengine.cpp
+++ b/src/plugins/debugger/debuggerengine.cpp
@@ -1229,8 +1229,7 @@ void DebuggerEngine::setState(DebuggerState state, bool forced)
     showMessage(msg, LogDebug);
     updateViews();
 
-    if (isMasterEngine())
-        emit stateChanged(d->m_state);
+    emit stateChanged(d->m_state);
 
     if (isSlaveEngine())
         masterEngine()->slaveEngineStateChanged(this, state);
diff --git a/src/plugins/debugger/qml/qmljsscriptconsole.cpp b/src/plugins/debugger/qml/qmljsscriptconsole.cpp
index 6107fd06bcb..2325e80e6f6 100644
--- a/src/plugins/debugger/qml/qmljsscriptconsole.cpp
+++ b/src/plugins/debugger/qml/qmljsscriptconsole.cpp
@@ -75,7 +75,8 @@ public:
           prompt(_("> ")),
           startOfEditableArea(-1),
           lastKnownPosition(0),
-          inferiorStopped(false)
+          inferiorStopped(false),
+          hasContext(false)
     {
         scriptHistory.append(QString());
         scriptHistoryIndex = scriptHistory.count();
@@ -96,6 +97,7 @@ public:
     InteractiveInterpreter interpreter;
 
     bool inferiorStopped;
+    bool hasContext;
 
     QFlags<QmlJSScriptConsole::DebugLevelFlag> debugLevel;
 };
@@ -189,7 +191,7 @@ void QmlJSScriptConsoleWidget::setEngine(DebuggerEngine *engine)
 {
     if (m_console->engine())
         disconnect(m_console->engine(), SIGNAL(stateChanged(Debugger::DebuggerState)),
-                   this, SLOT(engineStateChanged(Debugger::DebuggerState)));
+                   this, SLOT(onEngineStateChanged(Debugger::DebuggerState)));
 
     QmlEngine *qmlEngine = qobject_cast<QmlEngine *>(engine);
     QmlCppEngine *qmlCppEngine = qobject_cast<QmlCppEngine *>(engine);
@@ -199,9 +201,9 @@ void QmlJSScriptConsoleWidget::setEngine(DebuggerEngine *engine)
     //Supports only QML Engine
     if (qmlEngine) {
         connect(qmlEngine, SIGNAL(stateChanged(Debugger::DebuggerState)),
-                this, SLOT(engineStateChanged(Debugger::DebuggerState)));
+                this, SLOT(onEngineStateChanged(Debugger::DebuggerState)));
 
-        engineStateChanged(qmlEngine->state());
+        onEngineStateChanged(qmlEngine->state());
     }
 
     m_console->setEngine(qmlEngine);
@@ -228,7 +230,7 @@ void QmlJSScriptConsoleWidget::setDebugLevel()
     m_console->setDebugLevel(level);
 }
 
-void QmlJSScriptConsoleWidget::engineStateChanged(Debugger::DebuggerState state)
+void QmlJSScriptConsoleWidget::onEngineStateChanged(Debugger::DebuggerState state)
 {
     if (state == InferiorRunOk || state == InferiorStopOk) {
         setEnabled(true);
@@ -301,11 +303,10 @@ void QmlJSScriptConsole::setEngine(QmlEngine *eng)
     clear();
 }
 
-DebuggerEngine *QmlJSScriptConsole::engine()
+DebuggerEngine *QmlJSScriptConsole::engine() const
 {
-    if (d->adapter) {
+    if (d->adapter)
         return d->adapter->debuggerEngine();
-    }
     return 0;
 }
 
@@ -364,9 +365,11 @@ void QmlJSScriptConsole::onSelectionChanged()
 {
     if (d->adapter) {
         const QString context = d->inferiorStopped ?
-            engine()->stackHandler()->currentFrame().function :
-            d->adapter->currentSelectedDisplayName();
-        emit updateStatusMessage(tr("Context: %1").arg(context), 0);
+                    d->adapter->debuggerEngine()->stackHandler()->currentFrame().function :
+                    d->adapter->currentSelectedDisplayName();
+
+        d->hasContext = !context.isEmpty();
+        emit updateStatusMessage(tr("Context: ").append(context), 0);
     }
 }
 
@@ -562,22 +565,21 @@ void QmlJSScriptConsole::displayPrompt()
 void QmlJSScriptConsole::handleReturnKey()
 {
     QString currentScript = getCurrentScript();
-    bool scriptEvaluated = false;
+    bool showPrompt = true;
+    bool showInvalidContextError = false;
 
     //Check if string is only white spaces
-    if (currentScript.trimmed().isEmpty()) {
-        scriptEvaluated = true;
-    }
-
-    if (!scriptEvaluated) {
-        //check if it can be evaluated
-        if (d->canEvaluateScript(currentScript)) {
-
-            //Select the engine for evaluation based on
-            //inferior state
-            if (!d->inferiorStopped) {
-
-                if (d->adapter) {
+    if (!currentScript.trimmed().isEmpty()) {
+        //Check for a valid context
+        if (d->hasContext) {
+            //check if it can be evaluated
+            if (d->canEvaluateScript(currentScript)) {
+                //Evaluate expression based on engine state
+                //When engine->state() == InferiorStopOk, the expression
+                //is sent to V8DebugService. In all other cases, the
+                //expression is evaluated by QDeclarativeEngine.
+                if (!d->inferiorStopped) {
+                    QTC_ASSERT(d->adapter, return);
                     QDeclarativeEngineDebug *engineDebug = d->adapter->engineDebugClient();
                     int id = d->adapter->currentSelectedDebugId();
                     if (engineDebug && id != -1) {
@@ -585,26 +587,28 @@ void QmlJSScriptConsole::handleReturnKey()
                                 engineDebug->queryExpressionResult(id, currentScript, this);
                         connect(query, SIGNAL(stateChanged(QmlJsDebugClient::QDeclarativeDebugQuery::State)),
                                 this, SLOT(onStateChanged(QmlJsDebugClient::QDeclarativeDebugQuery::State)));
-                        scriptEvaluated = true;
                     }
+                } else {
+                    emit evaluateExpression(currentScript);
                 }
-            }
-
-            if (!scriptEvaluated) {
-                emit evaluateExpression(currentScript);
-                scriptEvaluated = true;
-            }
 
-            if (scriptEvaluated) {
                 d->appendToHistory(currentScript);
+            } else {
+                //The expression is not complete, wait for more input
+                //Move to next line and do not show prompt
+                QPlainTextEdit::appendPlainText(QString());
+                moveCursor(QTextCursor::EndOfLine);
+                showPrompt = false;
             }
+        } else {
+            //Incase of invalid context, append the expression to history
+            //and show Error message
+            d->appendToHistory(currentScript);
+            showInvalidContextError = true;
         }
     }
 
-    if (!scriptEvaluated) {
-        QPlainTextEdit::appendPlainText(QString());
-        moveCursor(QTextCursor::EndOfLine);
-    } else {
+    if (showPrompt) {
         QTextCursor cur = textCursor();
         cur.movePosition(QTextCursor::End);
         cur.insertText(_("\n"));
@@ -612,6 +616,9 @@ void QmlJSScriptConsole::handleReturnKey()
         displayPrompt();
     }
 
+    //Show an error message
+    if (showInvalidContextError)
+        appendResult(QLatin1String("Cannot evaluate without a valid QML/JS Context"));
 }
 
 void QmlJSScriptConsole::handleUpKey()
diff --git a/src/plugins/debugger/qml/qmljsscriptconsole.h b/src/plugins/debugger/qml/qmljsscriptconsole.h
index c1f563587e9..c42852d3380 100644
--- a/src/plugins/debugger/qml/qmljsscriptconsole.h
+++ b/src/plugins/debugger/qml/qmljsscriptconsole.h
@@ -72,7 +72,7 @@ signals:
 
 private slots:
     void setDebugLevel();
-    void engineStateChanged(Debugger::DebuggerState state);
+    void onEngineStateChanged(Debugger::DebuggerState state);
 
 private:
     QmlJSScriptConsole *m_console;
@@ -110,7 +110,7 @@ public:
     void setInferiorStopped(bool inferiorStopped);
 
     void setEngine(QmlEngine *engine);
-    DebuggerEngine *engine();
+    DebuggerEngine *engine() const;
 
     void appendResult(const QString &message, const QColor &color = QColor(Qt::darkGray));
 
-- 
GitLab