From dfd44a0ceb01f681965265d9b5340dbed217ae2c Mon Sep 17 00:00:00 2001
From: hjk <qtc-committer@nokia.com>
Date: Wed, 21 Jul 2010 13:11:03 +0200
Subject: [PATCH] debugger: fix display of correct thread in the thread
 combobox

---
 src/plugins/debugger/debuggerengine.cpp |  4 ++++
 src/plugins/debugger/debuggerplugin.cpp | 13 ++++++------
 src/plugins/debugger/gdb/gdbengine.cpp  |  4 +++-
 src/plugins/debugger/threadshandler.cpp | 27 +++++++------------------
 src/plugins/debugger/threadshandler.h   |  3 ++-
 tests/manual/gdbdebugger/simple/app.cpp |  5 ++++-
 6 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/src/plugins/debugger/debuggerengine.cpp b/src/plugins/debugger/debuggerengine.cpp
index cc1b5e45fef..c1a3b2d7672 100644
--- a/src/plugins/debugger/debuggerengine.cpp
+++ b/src/plugins/debugger/debuggerengine.cpp
@@ -1362,6 +1362,10 @@ void DebuggerEngine::setState(DebuggerState state, bool forced)
     if (!forced && !isAllowedTransition(oldState, state))
         qDebug() << "UNEXPECTED STATE TRANSITION: " << msg;
 
+    const bool running = d->m_state == InferiorRunOk;
+    if (running)
+        threadsHandler()->notifyRunning();
+
     showMessage(msg, LogDebug);
     plugin()->updateState(this);
 }
diff --git a/src/plugins/debugger/debuggerplugin.cpp b/src/plugins/debugger/debuggerplugin.cpp
index 1db8857ab97..654d7071d78 100644
--- a/src/plugins/debugger/debuggerplugin.cpp
+++ b/src/plugins/debugger/debuggerplugin.cpp
@@ -51,10 +51,11 @@
 #include "threadswindow.h"
 #include "watchwindow.h"
 
-#include "watchutils.h"
 #include "breakhandler.h"
-#include "snapshothandler.h"
 #include "sessionengine.h"
+#include "snapshothandler.h"
+#include "threadshandler.h"
+#include "watchutils.h"
 
 #ifdef Q_OS_WIN
 #  include "shared/peutils.h"
@@ -2136,6 +2137,9 @@ void DebuggerPluginPrivate::setInitialState()
 
 void DebuggerPluginPrivate::updateState(DebuggerEngine *engine)
 {
+    //m_threadBox->setModel(engine->threadsModel());
+    //m_threadBox->setModel(engine->threadsModel());
+    m_threadBox->setCurrentIndex(engine->threadsHandler()->currentThread());
     m_watchersWindow->setVisible(
         m_watchersWindow->model()->rowCount(QModelIndex()) > 0);
     m_returnWindow->setVisible(
@@ -2192,16 +2196,11 @@ void DebuggerPluginPrivate::updateState(DebuggerEngine *engine)
         || m_state == InferiorUnrunnable;
 
     const bool running = m_state == InferiorRunOk;
-// FIXME ABC
-//    if (running)
-//        threadsHandler()->notifyRunning();
     const bool stopped = m_state == InferiorStopOk;
 
     if (stopped)
         QApplication::alert(mainWindow(), 3000);
 
-    //qDebug() << "FLAGS: " << stoppable << running << stopped;
-
     m_actions.watchAction1->setEnabled(true);
     m_actions.watchAction2->setEnabled(true);
     m_actions.breakAction->setEnabled(true);
diff --git a/src/plugins/debugger/gdb/gdbengine.cpp b/src/plugins/debugger/gdb/gdbengine.cpp
index c222351100c..197b7db047c 100644
--- a/src/plugins/debugger/gdb/gdbengine.cpp
+++ b/src/plugins/debugger/gdb/gdbengine.cpp
@@ -2913,8 +2913,10 @@ void GdbEngine::handleThreadInfo(const GdbResponse &response)
             threads.append(thread);
         }
         threadsHandler()->setThreads(threads);
-        const int currentThreadId = response.data.findChild("current-thread-id").data().toInt();
+        const int currentThreadId =
+            response.data.findChild("current-thread-id").data().toInt();
         threadsHandler()->setCurrentThreadId(currentThreadId);
+        plugin()->updateState(this); // Adjust Threads combobox.
     } else {
         // Fall back for older versions: Try to get at least a list
         // of running threads.
diff --git a/src/plugins/debugger/threadshandler.cpp b/src/plugins/debugger/threadshandler.cpp
index 8eedbbf60e0..9092e099a4a 100644
--- a/src/plugins/debugger/threadshandler.cpp
+++ b/src/plugins/debugger/threadshandler.cpp
@@ -32,6 +32,7 @@
 #include "debuggerconstants.h"
 #include "debuggerengine.h"
 
+#include <QtCore/QDebug>
 #include <QtCore/QTextStream>
 
 namespace Debugger {
@@ -59,21 +60,6 @@ void ThreadData::notifyRunning()
     lineNumber = -1;
 }
 
-
-int id;
-QString targetId;
-QString core;
-
-// State information when stopped
-void notifyRunning(); // Clear state information
-
-int frameLevel;
-quint64 address;
-QString function;
-QString fileName;
-QString state;
-int lineNumber;
-
 static inline QString threadToolTip(const ThreadData &thread)
 {
     const char tableRowStartC[] = "<tr><td>";
@@ -94,7 +80,7 @@ static inline QString threadToolTip(const ThreadData &thread)
         str << tableRowStartC << ThreadsHandler::tr("Core:")
         << tableRowSeparatorC << thread.core << tableRowEndC;
 
-    if (thread.address) {        
+    if (thread.address) {
         str << tableRowStartC << ThreadsHandler::tr("Stopped&nbsp;at:")
                 << tableRowSeparatorC;
         if (!thread.function.isEmpty())
@@ -166,7 +152,8 @@ QVariant ThreadsHandler::data(const QModelIndex &index, int role) const
         }
     case Qt::ToolTipRole:
         return threadToolTip(thread);
-    case Qt::DecorationRole: // Return icon that indicates whether this is the active stack frame
+    case Qt::DecorationRole:
+        // Return icon that indicates whether this is the active stack frame
         if (index.column() == 0)
             return (index.row() == m_currentIndex) ? m_positionIcon : m_emptyIcon;
         break;
@@ -256,8 +243,8 @@ void ThreadsHandler::setThreads(const Threads &threads)
 {
     m_threads = threads;
     if (m_currentIndex >= m_threads.size())
-        m_currentIndex = m_threads.size() - 1;
-    reset();
+        m_currentIndex = -1;
+    layoutChanged();
 }
 
 Threads ThreadsHandler::threads() const
@@ -269,7 +256,7 @@ void ThreadsHandler::removeAll()
 {
     m_threads.clear();
     m_currentIndex = 0;
-    reset();
+    layoutChanged();
 }
 
 void ThreadsHandler::notifyRunning()
diff --git a/src/plugins/debugger/threadshandler.h b/src/plugins/debugger/threadshandler.h
index eaed8e1f339..c237c1ccd56 100644
--- a/src/plugins/debugger/threadshandler.h
+++ b/src/plugins/debugger/threadshandler.h
@@ -96,9 +96,10 @@ class ThreadsHandler : public QAbstractTableModel
 public:
     explicit ThreadsHandler(DebuggerEngine *engine);
 
+    int currentThread() const { return m_currentIndex; }
+    void setCurrentThread(int index);
     int currentThreadId() const;
     void setCurrentThreadId(int id);
-    void setCurrentThread(int index);
     int indexOf(int threadId) const;
 
     void selectThread(int index);
diff --git a/tests/manual/gdbdebugger/simple/app.cpp b/tests/manual/gdbdebugger/simple/app.cpp
index 939f34a4887..1a4675d8247 100644
--- a/tests/manual/gdbdebugger/simple/app.cpp
+++ b/tests/manual/gdbdebugger/simple/app.cpp
@@ -1347,6 +1347,9 @@ public:
             //sleep(1);
             std::cerr << m_id;
         }
+        if (m_id == 2) {
+            ++j;
+        }
         std::cerr << j;
     }
 
@@ -1937,7 +1940,7 @@ int main(int argc, char *argv[])
 #    endif
     testQStringList();
     testStruct();
-    // testQThread();
+    //testQThread();
     testQVariant1();
     testQVariant2();
     testQVariant3();
-- 
GitLab