KDbg 2.5.6.
[kdbg.git] / kdbg / exprwnd.cpp
blob11ca30988d1c3f5cc92d94585f804bdf3aa835e5
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 "exprwnd.h"
8 #include "exprwnd.moc"
9 #include "typetable.h"
10 #include <QHeaderView>
11 #include <QStringList>
12 #include <QPainter>
13 #include <QPaintEvent>
14 #include <QFocusEvent>
15 #include <QKeyEvent>
16 #include <QScrollBar>
17 #include <kiconloader.h> /* icons */
18 #include <klocale.h> /* i18n */
19 #include "mydebug.h"
21 VarTree::VarTree(VarTree* parent, ExprValue* v) :
22 QTreeWidgetItem(parent),
23 m_varKind(v->m_varKind),
24 m_nameKind(v->m_nameKind),
25 m_type(0),
26 m_exprIndex(0),
27 m_exprIndexUseGuard(false),
28 m_baseValue(v->m_value),
29 m_baseChanged(false),
30 m_structChanged(false)
32 setText(v->m_name);
33 updateValueText();
34 if (v->m_child != 0 || m_varKind == VarTree::VKpointer)
35 setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
36 else
37 setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
38 setExpanded(v->m_initiallyExpanded);
41 VarTree::VarTree(ExprWnd* parent, ExprValue* v) :
42 QTreeWidgetItem(parent),
43 m_varKind(VKsimple),
44 m_nameKind(VarTree::NKplain),
45 m_type(0),
46 m_exprIndex(0),
47 m_exprIndexUseGuard(false),
48 m_baseValue(v->m_value),
49 m_baseChanged(false),
50 m_structChanged(false)
52 setText(v->m_name);
53 updateValueText();
56 VarTree::~VarTree()
60 QString VarTree::computeExpr() const
62 // top-level items are special
63 if (isToplevelExpr())
64 return getText();
66 // get parent expr
67 VarTree* par = static_cast<VarTree*>(parent());
68 QString parentExpr = par->computeExpr();
70 // skip this item's name if it is a base class or anonymous struct or union
71 if (m_nameKind == NKtype || m_nameKind == NKanonymous) {
72 return parentExpr;
74 /* augment by this item's text */
75 QString result;
76 /* if this is an address, dereference it */
77 if (m_nameKind == NKaddress) {
78 ASSERT(par->m_varKind == VKpointer);
79 result = "*" + parentExpr;
80 return result;
82 switch (par->m_varKind) {
83 case VKarray:
85 QString index = getText();
86 int i = 1;
87 // skip past the index
88 while (index[i].isDigit())
89 i++;
91 * Some array indices are actually ranges due to repeated array
92 * values. We use the first index in these cases.
94 if (index[i] != ']') {
95 // remove second index
96 index.remove(i, index.length()-i-1);
98 result = "(" + parentExpr + ")" + index;
100 break;
101 case VKstruct:
102 result = "(" + parentExpr + ")." + getText();
103 break;
104 case VKsimple: /* parent can't be simple */
105 case VKpointer: /* handled in NKaddress */
106 case VKdummy: /* can't occur at all */
107 ASSERT(false);
108 result = parentExpr; /* paranoia */
109 break;
111 return result;
114 bool VarTree::isToplevelExpr() const
116 return parent() == 0;
119 bool VarTree::isAncestorEq(const VarTree* child) const
121 const QTreeWidgetItem* c = child;
122 while (c != 0 && c != this) {
123 c = c->parent();
125 return c != 0;
128 bool VarTree::updateValue(const QString& newValue)
130 // check whether the value changed
131 bool prevValueChanged = m_baseChanged;
132 if ((m_baseChanged = m_baseValue != newValue)) {
133 m_baseValue = newValue;
134 updateValueText();
135 setForeground(1, QBrush(QColor(Qt::red)));
136 } else if (prevValueChanged) {
137 setForeground(1, treeWidget()->palette().text());
140 * We must repaint the cell if the value changed. If it did not change,
141 * we still must repaint the cell if the value changed previously,
142 * because the color of the display must be changed (from red to
143 * black).
145 return m_baseChanged || prevValueChanged;
148 bool VarTree::updateStructValue(const QString& newValue)
150 // check whether the value changed
151 bool prevValueChanged = m_structChanged;
152 if ((m_structChanged = m_structValue != newValue)) {
153 m_structValue = newValue;
154 updateValueText();
155 setForeground(1, QBrush(QColor(Qt::red)));
156 } else if (prevValueChanged) {
157 setForeground(1, treeWidget()->palette().text());
160 * We must repaint the cell if the value changed. If it did not change,
161 * we still must repaint the cell if the value changed previously,
162 * because the color of the display must be changed (from red to
163 * black).
165 return m_structChanged || prevValueChanged;
168 void VarTree::updateValueText()
170 if (m_baseValue.isEmpty()) {
171 setText(1, m_structValue);
172 } else if (m_structValue.isEmpty()) {
173 setText(1, m_baseValue);
174 } else {
175 setText(1, m_baseValue + " " + m_structValue);
179 void VarTree::inferTypesOfChildren(ProgramTypeTable& typeTable)
182 * Type inference works like this: We use type information of those
183 * children that have a type name in their name (base classes) or in
184 * their value (pointers)
187 // first recurse children
188 for (int i = 0; i < childCount(); i++)
190 child(i)->inferTypesOfChildren(typeTable);
193 // if this is a pointer, get the type from the value (less the pointer)
194 if (m_varKind == VKpointer) {
195 if (isWcharT())
198 * wchart_t pointers must be treated as struct, because the array
199 * of characters is printed similar to how QStrings are decoded.
201 m_varKind = VKstruct;
202 setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
204 // don't know how to do this cleanly
205 } else if (m_varKind == VKstruct) {
206 // check if this is a base class part
207 if (m_nameKind == NKtype) {
208 const QString& typeName =
209 getText().mid(1, getText().length()-2); // strip < and >
210 m_type = typeTable.lookup(typeName);
212 /* if we don't have a type yet, get it from the base class */
213 if (m_type == 0) {
214 m_type = inferTypeFromBaseClass();
216 * If there is a known type now, it is the one from the
217 * first base class whose type we know.
222 * If we still don't have a type, the type is really unknown.
224 if (m_type == 0) {
225 m_type = TypeInfo::unknownType();
227 } // else
229 * This is not a base class part. We don't assign a type so
230 * that later we can ask gdb.
235 // the value contains the pointer type in parenthesis
236 bool VarTree::isWcharT() const
238 return value().startsWith("(const wchar_t *)") ||
239 value().startsWith("(wchar_t *)");
243 * Get the type of the first base class whose type we know.
245 const TypeInfo* VarTree::inferTypeFromBaseClass()
247 if (m_varKind == VKstruct) {
248 for (int i = 0; i < childCount(); i++)
250 VarTree* child = VarTree::child(i);
251 // only check base class parts (i.e. type names)
252 if (child->m_nameKind != NKtype)
253 break;
254 if (child->m_type != 0 &&
255 child->m_type != TypeInfo::unknownType())
257 // got a type!
258 return child->m_type;
262 return 0;
265 QVariant VarTree::data(int column, int role) const
267 if (role != Qt::ToolTipRole || column != 1)
268 return QTreeWidgetItem::data(column, role);
269 return QVariant(value());
272 ExprValue::ExprValue(const QString& name, VarTree::NameKind aKind) :
273 m_name(name),
274 m_varKind(VarTree::VKsimple),
275 m_nameKind(aKind),
276 m_child(0),
277 m_next(0),
278 m_initiallyExpanded(false)
282 ExprValue::~ExprValue()
284 delete m_child;
285 delete m_next;
288 void ExprValue::appendChild(ExprValue* newChild)
290 if (m_child == 0) {
291 m_child = newChild;
292 } else {
293 // walk chain of children to find the last one
294 ExprValue* last = m_child;
295 while (last->m_next != 0)
296 last = last->m_next;
297 last->m_next = newChild;
299 newChild->m_next = 0; // just to be sure
302 int ExprValue::childCount() const
304 int i = 0;
305 ExprValue* c = m_child;
306 while (c) {
307 ++i;
308 c = c->m_next;
310 return i;
315 ExprWnd::ExprWnd(QWidget* parent, const QString& colHeader) :
316 QTreeWidget(parent),
317 m_edit(0)
319 QTreeWidgetItem* pHeaderItem = new QTreeWidgetItem();
320 pHeaderItem->setText(0, colHeader);
321 pHeaderItem->setText(1, i18n("Value"));
322 setHeaderItem(pHeaderItem);
323 header()->setResizeMode(0, QHeaderView::Interactive);
324 header()->setResizeMode(1, QHeaderView::Interactive);
326 setSortingEnabled(false); // do not sort items
327 setRootIsDecorated(true);
328 setAllColumnsShowFocus(true);
330 m_pixPointer = UserIcon("pointer.xpm");
331 if (m_pixPointer.isNull())
332 TRACE("Can't load pointer.xpm");
335 ExprWnd::~ExprWnd()
339 QStringList ExprWnd::exprList() const
341 QStringList exprs;
342 for (int i = 0; i < topLevelItemCount(); i++)
344 exprs.append(topLevelItem(i)->getText());
346 return exprs;
349 VarTree* ExprWnd::insertExpr(ExprValue* expr, ProgramTypeTable& typeTable)
351 // append a new dummy expression
352 VarTree* display = new VarTree(this, expr);
354 // replace it right away
355 updateExpr(display, expr, typeTable);
356 return display;
359 void ExprWnd::updateExpr(ExprValue* expr, ProgramTypeTable& typeTable)
361 // search the root variable
362 VarTree* item = 0;
363 for (int i = 0; i < topLevelItemCount(); i++)
365 if (topLevelItem(i)->getText() == expr->m_name) {
366 item = topLevelItem(i);
367 break;
370 if (item == 0) {
371 return;
373 // now update it
374 updateExprRec(item, expr, typeTable);
375 collectUnknownTypes(item);
378 void ExprWnd::updateExpr(VarTree* display, ExprValue* newValues, ProgramTypeTable& typeTable)
380 updateExprRec(display, newValues, typeTable);
381 collectUnknownTypes(display);
385 * returns true if there's a visible change
387 void ExprWnd::updateExprRec(VarTree* display, ExprValue* newValues, ProgramTypeTable& typeTable)
389 bool isExpanded = display->isExpanded();
392 * If we are updating a pointer without children by a dummy, we don't
393 * collapse it, but simply insert the new children. This happens when a
394 * pointer has just been expanded by the user.
396 if (display->m_varKind == VarTree::VKpointer &&
397 display->childCount() == 0 &&
398 newValues->m_varKind == VarTree::VKdummy)
400 replaceChildren(display, newValues);
401 return;
405 * If the display and newValues have different kind or if their number
406 * of children is different, replace the whole sub-tree.
408 if (// the next two lines mean: not(m_varKind remains unchanged)
409 !(newValues->m_varKind == VarTree::VKdummy ||
410 display->m_varKind == newValues->m_varKind)
412 (display->childCount() != newValues->childCount() &&
414 * If this is a pointer and newValues doesn't have children, we
415 * don't replace the sub-tree; instead, below we mark this
416 * sub-tree for requiring an update.
418 (display->m_varKind != VarTree::VKpointer ||
419 newValues->m_child != 0)))
421 if (isExpanded) {
422 display->setExpanded(false);
425 // since children changed, it is likely that the type has also changed
426 display->m_type = 0; /* will re-evaluate the type */
428 // display the new value
429 updateSingleExpr(display, newValues);
430 replaceChildren(display, newValues);
432 // update the m_varKind
433 if (newValues->m_varKind != VarTree::VKdummy) {
434 display->m_varKind = newValues->m_varKind;
435 if (newValues->m_child != 0 || newValues->m_varKind == VarTree::VKpointer)
436 display->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
437 else
438 display->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
441 // get some types (after the new m_varKind has been set!)
442 display->inferTypesOfChildren(typeTable);
444 // (note that the new value might not have a sub-tree at all)
445 return;
448 // display the new value
449 updateSingleExpr(display, newValues);
452 * If this is an expanded pointer, record it for being updated.
454 if (display->m_varKind == VarTree::VKpointer) {
455 if (isExpanded &&
456 // if newValues is a dummy, we have already updated this pointer
457 newValues->m_varKind != VarTree::VKdummy)
459 m_updatePtrs.push_back(display);
462 * If the visible sub-tree has children, but newValues doesn't, we
463 * can stop here.
465 if (newValues->m_child == 0) {
466 return;
470 ASSERT(display->childCount() == newValues->childCount());
472 // go for children
473 ExprValue* vNew = newValues->m_child;
474 for (int i = 0; i < display->childCount(); i++)
476 VarTree* vDisplay = display->child(i);
477 // check whether the names are the same
478 if (vDisplay->getText() != vNew->m_name) {
479 // set new name
480 vDisplay->setText(vNew->m_name);
482 // recurse
483 updateExprRec(vDisplay, vNew, typeTable);
485 vNew = vNew->m_next;
489 void ExprWnd::updateSingleExpr(VarTree* display, ExprValue* newValue)
492 * If newValues is a VKdummy, we are only interested in its children.
493 * No need to update anything here.
495 if (newValue->m_varKind == VarTree::VKdummy) {
496 return;
500 * If this node is a struct and we know its type then we know how to
501 * find a nested value. So register the node for an update.
503 * wchar_t types are also treated specially here: We consider them
504 * as struct (has been set in inferTypesOfChildren()).
506 if (display->m_varKind == VarTree::VKstruct &&
507 display->m_type != 0 &&
508 display->m_type != TypeInfo::unknownType())
510 ASSERT(newValue->m_varKind == VarTree::VKstruct);
511 if (display->m_type == TypeInfo::wchartType())
513 display->m_partialValue = "L";
515 else
516 display->m_partialValue = display->m_type->m_displayString[0];
517 m_updateStruct.push_back(display);
520 display->updateValue(newValue->m_value);
523 void ExprWnd::updateStructValue(VarTree* display)
525 ASSERT(display->m_varKind == VarTree::VKstruct);
527 display->updateStructValue(display->m_partialValue);
528 // reset the value
529 display->m_partialValue = "";
530 display->m_exprIndex = -1;
533 void ExprWnd::replaceChildren(VarTree* display, ExprValue* newValues)
535 ASSERT(display->childCount() == 0 || display->m_varKind != VarTree::VKsimple);
537 // delete all children of display
538 while (VarTree* c = display->child(0)) {
539 unhookSubtree(c);
540 delete c;
542 // insert copies of the newValues
543 for (ExprValue* v = newValues->m_child; v != 0; v = v->m_next)
545 VarTree* vNew = new VarTree(display, v);
546 // recurse
547 replaceChildren(vNew, v);
551 void ExprWnd::collectUnknownTypes(VarTree* var)
553 QTreeWidgetItemIterator i(var);
554 for (; *i; ++i)
556 checkUnknownType(static_cast<VarTree*>(*i));
560 void ExprWnd::checkUnknownType(VarTree* var)
562 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
563 if (var->m_type == 0 &&
564 var->m_varKind == VarTree::VKstruct &&
565 var->m_nameKind != VarTree::NKtype &&
566 var->m_nameKind != VarTree::NKanonymous)
568 if (!var->isWcharT())
570 /* this struct node doesn't have a type yet: register it */
571 m_updateType.push_back(var);
573 else
575 var->m_type = TypeInfo::wchartType();
576 var->m_partialValue = "L";
577 m_updateStruct.push_back(var);
580 // add pointer pixmap to pointers
581 if (var->m_varKind == VarTree::VKpointer) {
582 var->setPixmap(m_pixPointer);
586 QString ExprWnd::formatWCharPointer(QString value)
588 int pos = value.indexOf(") ");
589 if (pos > 0)
590 value = value.mid(pos+2);
591 return value + " L";
595 VarTree* ExprWnd::topLevelExprByName(const QString& name) const
597 for (int i = 0; i < topLevelItemCount(); i++)
599 if (topLevelItem(i)->getText() == name)
600 return topLevelItem(i);
602 return 0;
605 VarTree* ExprWnd::ptrMemberByName(VarTree* v, const QString& name)
607 // v must be a pointer variable, must have children
608 if (v->m_varKind != VarTree::VKpointer || v->childCount() == 0)
609 return 0;
611 // the only child of v is the pointer value that represents the struct
612 VarTree* item = v->child(0);
613 return memberByName(item, name);
616 VarTree* ExprWnd::memberByName(VarTree* v, const QString& name)
618 // search immediate children for name
619 for (int i = 0; i < v->childCount(); i++)
621 if (v->child(i)->getText() == name)
622 return v->child(i);
625 // try in base classes and members that are anonymous structs or unions
626 for (int i = 0; i < v->childCount(); i++)
628 VarTree* item = v->child(i);
629 if (item->m_nameKind == VarTree::NKtype ||
630 item->m_nameKind == VarTree::NKanonymous)
632 item = memberByName(item, name);
633 if (item != 0)
634 return item;
637 return 0;
640 void ExprWnd::removeExpr(VarTree* item)
642 unhookSubtree(item);
644 delete item;
647 void ExprWnd::unhookSubtree(VarTree* subTree)
649 // must remove any pointers scheduled for update from the list
650 unhookSubtree(m_updatePtrs, subTree);
651 unhookSubtree(m_updateType, subTree);
652 unhookSubtree(m_updateStruct, subTree);
653 emit removingItem(subTree);
656 void ExprWnd::unhookSubtree(std::list<VarTree*>& list, VarTree* subTree)
658 if (subTree == 0)
659 return;
661 std::list<VarTree*>::iterator i = list.begin();
662 while (i != list.end()) {
663 std::list<VarTree*>::iterator checkItem = i;
664 ++i;
665 if (subTree->isAncestorEq(*checkItem)) {
666 // checkItem is an item from subTree
667 list.erase(checkItem);
672 void ExprWnd::clearPendingUpdates()
674 m_updatePtrs.clear();
675 m_updateType.clear();
676 m_updateStruct.clear();
679 VarTree* ExprWnd::nextUpdatePtr()
681 VarTree* ptr = 0;
682 if (!m_updatePtrs.empty()) {
683 ptr = m_updatePtrs.front();
684 m_updatePtrs.pop_front();
686 return ptr;
689 VarTree* ExprWnd::nextUpdateType()
691 VarTree* ptr = 0;
692 if (!m_updateType.empty()) {
693 ptr = m_updateType.front();
694 m_updateType.pop_front();
696 return ptr;
699 VarTree* ExprWnd::nextUpdateStruct()
701 VarTree* ptr = 0;
702 if (!m_updateStruct.empty()) {
703 ptr = m_updateStruct.front();
704 m_updateStruct.pop_front();
706 return ptr;
710 void ExprWnd::editValue(VarTree* item, const QString& text)
712 if (m_edit == 0)
713 m_edit = new ValueEdit(this);
715 QRect r = visualItemRect(item);
716 int x = columnViewportPosition(1);
717 int y = r.y();
718 int w = columnWidth(1);
719 int h = r.height();
722 * Make the edit widget at least 5 characters wide (but not wider than
723 * this widget). If less than half of this widget is used to display
724 * the text, scroll this widget so that half of it shows the text (or
725 * less than half of it if the text is shorter).
727 QFontMetrics metr = m_edit->font();
728 int wMin = metr.width("88888");
729 if (w < wMin)
730 w = wMin;
731 int wThis = viewport()->width();
732 if (x >= wThis/2 && // less than half the width displays text
733 x+w > wThis) // not all text is visible
735 // scroll so that more text is visible
736 QScrollBar* pScrollBar = horizontalScrollBar();
737 int wScroll = qMin(x-wThis/2, x+w-wThis);
738 pScrollBar->setValue(pScrollBar->value() + wScroll);
739 x -= wScroll;
741 else if (x < 0)
743 // don't let the edit move out at the left
744 x = 0;
747 // make the edit box as wide as the visible column
748 QRect rect(x,y, wThis-x,h);
749 m_edit->setText(text);
750 m_edit->selectAll();
752 m_edit->setGeometry(rect);
753 m_edit->m_finished = false;
754 m_edit->m_item = item;
755 m_edit->show();
756 m_edit->setFocus();
759 bool ExprWnd::isEditing() const
761 return m_edit != 0 && m_edit->isVisible();
765 ValueEdit::ValueEdit(ExprWnd* parent) :
766 QLineEdit(parent->viewport())
768 setFrame(false);
769 hide();
770 lower(); // lower the window below scrollbars
771 connect(parent, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
772 SLOT(slotSelectionChanged()));
773 connect(parent, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
774 SLOT(slotSelectionChanged()));
775 connect(parent, SIGNAL(itemExpanded(QTreeWidgetItem*)),
776 SLOT(slotSelectionChanged()));
777 connect(parent, SIGNAL(itemCollapsed(QTreeWidgetItem*)),
778 SLOT(slotSelectionChanged()));
779 connect(this, SIGNAL(done(VarTree*, const QString&)),
780 parent, SIGNAL(editValueCommitted(VarTree*, const QString&)));
783 ValueEdit::~ValueEdit()
787 void ValueEdit::terminate(bool commit)
789 TRACE(commit?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
790 if (!m_finished)
792 m_finished = true;
793 hide(); // will call focusOutEvent, that's why we need m_finished
794 if (commit) {
795 emit done(m_item, text());
800 void ValueEdit::keyPressEvent(QKeyEvent *e)
802 if(e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
803 terminate(true);
804 else if(e->key() == Qt::Key_Escape)
805 terminate(false);
806 else
807 QLineEdit::keyPressEvent(e);
810 void ValueEdit::paintEvent(QPaintEvent* e)
812 QLineEdit::paintEvent(e);
814 QPainter p(this);
815 p.drawRect(rect());
818 void ValueEdit::focusOutEvent(QFocusEvent* ev)
820 TRACE("ValueEdit::focusOutEvent");
821 QFocusEvent* focusEv = static_cast<QFocusEvent*>(ev);
822 if (focusEv->reason() == Qt::ActiveWindowFocusReason)
824 // Switching to a different window should terminate the edit,
825 // because if the window with this variable display is floating
826 // then that different window could be the main window, where
827 // the user had clicked one of the Execute buttons. This in turn
828 // may pull the item away that we are editing here.
829 terminate(false);
831 // Don't let a RMB close the editor
832 else if (focusEv->reason() != Qt::PopupFocusReason)
834 terminate(true);
838 void ValueEdit::slotSelectionChanged()
840 TRACE("ValueEdit::slotSelectionChanged");
841 terminate(false);