Extend copyright to 2018.
[kdbg.git] / kdbg / threadlist.cpp
blob3315928bdc1ee8b0369d25a12292a0035f4ba76f
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include "threadlist.h"
8 #include "dbgdriver.h"
9 #include <klocalizedstring.h>
10 #include <kiconloader.h>
11 #include <QBitmap>
12 #include <QHeaderView>
13 #include <QPainter>
16 class ThreadEntry : public QTreeWidgetItem, public ThreadInfo
18 public:
19 ThreadEntry(QTreeWidget* parent, const ThreadInfo& thread);
20 void setFunction(const QString& func);
22 bool m_delete; /* used for updating the list */
25 ThreadEntry::ThreadEntry(QTreeWidget* parent, const ThreadInfo& thread) :
26 QTreeWidgetItem(parent, QStringList() << thread.threadName << thread.function),
27 ThreadInfo(thread),
28 m_delete(false)
32 void ThreadEntry::setFunction(const QString& func)
34 function = func;
35 setText(1, function);
39 ThreadList::ThreadList(QWidget* parent) :
40 QTreeWidget(parent)
42 setHeaderLabels(QStringList() << i18n("Thread ID") << i18n("Location"));
43 header()->setSectionResizeMode(1, QHeaderView::Interactive);
44 setRootIsDecorated(false);
46 // load pixmaps
47 m_focusIcon = UserIcon("pcinner");
48 makeNoFocusIcon();
50 connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
51 this, SLOT(slotCurrentChanged(QTreeWidgetItem*)));
54 ThreadList::~ThreadList()
58 void ThreadList::updateThreads(const std::list<ThreadInfo>& threads)
60 // reset flag in all items
61 for (QTreeWidgetItemIterator i(this); *i; ++i)
62 static_cast<ThreadEntry*>(*i)->m_delete = true;
64 for (std::list<ThreadInfo>::const_iterator i = threads.begin(); i != threads.end(); ++i)
66 // look up this thread by id
67 ThreadEntry* te = threadById(i->id);
68 if (te == 0) {
69 te = new ThreadEntry(this, *i);
70 } else {
71 te->m_delete = false;
72 te->setFunction(i->function);
74 // set focus icon
75 te->hasFocus = i->hasFocus;
76 te->setIcon(0, i->hasFocus ? QIcon(m_focusIcon) : QIcon(m_noFocusIcon));
79 // delete all entries that have not been seen
80 for (QTreeWidgetItemIterator i(this); *i;)
82 ThreadEntry* te = static_cast<ThreadEntry*>(*i);
83 ++i; // step ahead before deleting it ;-)
84 if (te->m_delete) {
85 delete te;
90 ThreadEntry* ThreadList::threadById(int id)
92 for (QTreeWidgetItemIterator i(this); *i; ++i)
94 ThreadEntry* te = static_cast<ThreadEntry*>(*i);
95 if (te->id == id) {
96 return te;
99 return 0;
103 * Creates an icon of the same size as the m_focusIcon, but which is
104 * totally transparent.
106 void ThreadList::makeNoFocusIcon()
108 m_noFocusIcon = m_focusIcon;
110 QPainter p(&m_noFocusIcon);
111 p.fillRect(0,0, m_noFocusIcon.width(),m_noFocusIcon.height(), QColor(Qt::white));
113 m_noFocusIcon.setMask(m_noFocusIcon.createHeuristicMask());
116 void ThreadList::slotCurrentChanged(QTreeWidgetItem* newItem)
118 if (newItem == 0)
119 return;
121 ThreadEntry* te = static_cast<ThreadEntry*>(newItem);
123 // change the focused thread
124 if (te->hasFocus)
125 return;
127 emit setThread(te->id);
131 #include "threadlist.moc"