KDbg 2.5.5.
[kdbg.git] / kdbg / exprwnd.cpp
blob59957c76bbcc5c539eb1d21a09e4da4057bb95d7
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 ExprValue::ExprValue(const QString& name, VarTree::NameKind aKind) :
266 m_name(name),
267 m_varKind(VarTree::VKsimple),
268 m_nameKind(aKind),
269 m_child(0),
270 m_next(0),
271 m_initiallyExpanded(false)
275 ExprValue::~ExprValue()
277 delete m_child;
278 delete m_next;
281 void ExprValue::appendChild(ExprValue* newChild)
283 if (m_child == 0) {
284 m_child = newChild;
285 } else {
286 // walk chain of children to find the last one
287 ExprValue* last = m_child;
288 while (last->m_next != 0)
289 last = last->m_next;
290 last->m_next = newChild;
292 newChild->m_next = 0; // just to be sure
295 int ExprValue::childCount() const
297 int i = 0;
298 ExprValue* c = m_child;
299 while (c) {
300 ++i;
301 c = c->m_next;
303 return i;
308 ExprWnd::ExprWnd(QWidget* parent, const QString& colHeader) :
309 QTreeWidget(parent),
310 m_edit(0)
312 QTreeWidgetItem* pHeaderItem = new QTreeWidgetItem();
313 pHeaderItem->setText(0, colHeader);
314 pHeaderItem->setText(1, i18n("Value"));
315 setHeaderItem(pHeaderItem);
316 header()->setResizeMode(0, QHeaderView::Interactive);
317 header()->setResizeMode(1, QHeaderView::Interactive);
319 setSortingEnabled(false); // do not sort items
320 setRootIsDecorated(true);
321 setAllColumnsShowFocus(true);
323 m_pixPointer = UserIcon("pointer.xpm");
324 if (m_pixPointer.isNull())
325 TRACE("Can't load pointer.xpm");
328 ExprWnd::~ExprWnd()
332 QStringList ExprWnd::exprList() const
334 QStringList exprs;
335 for (int i = 0; i < topLevelItemCount(); i++)
337 exprs.append(topLevelItem(i)->getText());
339 return exprs;
342 VarTree* ExprWnd::insertExpr(ExprValue* expr, ProgramTypeTable& typeTable)
344 // append a new dummy expression
345 VarTree* display = new VarTree(this, expr);
347 // replace it right away
348 updateExpr(display, expr, typeTable);
349 return display;
352 void ExprWnd::updateExpr(ExprValue* expr, ProgramTypeTable& typeTable)
354 // search the root variable
355 VarTree* item = 0;
356 for (int i = 0; i < topLevelItemCount(); i++)
358 if (topLevelItem(i)->getText() == expr->m_name) {
359 item = topLevelItem(i);
360 break;
363 if (item == 0) {
364 return;
366 // now update it
367 updateExprRec(item, expr, typeTable);
368 collectUnknownTypes(item);
371 void ExprWnd::updateExpr(VarTree* display, ExprValue* newValues, ProgramTypeTable& typeTable)
373 updateExprRec(display, newValues, typeTable);
374 collectUnknownTypes(display);
378 * returns true if there's a visible change
380 void ExprWnd::updateExprRec(VarTree* display, ExprValue* newValues, ProgramTypeTable& typeTable)
382 bool isExpanded = display->isExpanded();
385 * If we are updating a pointer without children by a dummy, we don't
386 * collapse it, but simply insert the new children. This happens when a
387 * pointer has just been expanded by the user.
389 if (display->m_varKind == VarTree::VKpointer &&
390 display->childCount() == 0 &&
391 newValues->m_varKind == VarTree::VKdummy)
393 replaceChildren(display, newValues);
394 return;
398 * If the display and newValues have different kind or if their number
399 * of children is different, replace the whole sub-tree.
401 if (// the next two lines mean: not(m_varKind remains unchanged)
402 !(newValues->m_varKind == VarTree::VKdummy ||
403 display->m_varKind == newValues->m_varKind)
405 (display->childCount() != newValues->childCount() &&
407 * If this is a pointer and newValues doesn't have children, we
408 * don't replace the sub-tree; instead, below we mark this
409 * sub-tree for requiring an update.
411 (display->m_varKind != VarTree::VKpointer ||
412 newValues->m_child != 0)))
414 if (isExpanded) {
415 display->setExpanded(false);
418 // since children changed, it is likely that the type has also changed
419 display->m_type = 0; /* will re-evaluate the type */
421 // display the new value
422 updateSingleExpr(display, newValues);
423 replaceChildren(display, newValues);
425 // update the m_varKind
426 if (newValues->m_varKind != VarTree::VKdummy) {
427 display->m_varKind = newValues->m_varKind;
428 if (newValues->m_child != 0 || newValues->m_varKind == VarTree::VKpointer)
429 display->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator);
430 else
431 display->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
434 // get some types (after the new m_varKind has been set!)
435 display->inferTypesOfChildren(typeTable);
437 // (note that the new value might not have a sub-tree at all)
438 return;
441 // display the new value
442 updateSingleExpr(display, newValues);
445 * If this is an expanded pointer, record it for being updated.
447 if (display->m_varKind == VarTree::VKpointer) {
448 if (isExpanded &&
449 // if newValues is a dummy, we have already updated this pointer
450 newValues->m_varKind != VarTree::VKdummy)
452 m_updatePtrs.push_back(display);
455 * If the visible sub-tree has children, but newValues doesn't, we
456 * can stop here.
458 if (newValues->m_child == 0) {
459 return;
463 ASSERT(display->childCount() == newValues->childCount());
465 // go for children
466 ExprValue* vNew = newValues->m_child;
467 for (int i = 0; i < display->childCount(); i++)
469 VarTree* vDisplay = display->child(i);
470 // check whether the names are the same
471 if (vDisplay->getText() != vNew->m_name) {
472 // set new name
473 vDisplay->setText(vNew->m_name);
475 // recurse
476 updateExprRec(vDisplay, vNew, typeTable);
478 vNew = vNew->m_next;
482 void ExprWnd::updateSingleExpr(VarTree* display, ExprValue* newValue)
485 * If newValues is a VKdummy, we are only interested in its children.
486 * No need to update anything here.
488 if (newValue->m_varKind == VarTree::VKdummy) {
489 return;
493 * If this node is a struct and we know its type then we know how to
494 * find a nested value. So register the node for an update.
496 * wchar_t types are also treated specially here: We consider them
497 * as struct (has been set in inferTypesOfChildren()).
499 if (display->m_varKind == VarTree::VKstruct &&
500 display->m_type != 0 &&
501 display->m_type != TypeInfo::unknownType())
503 ASSERT(newValue->m_varKind == VarTree::VKstruct);
504 if (display->m_type == TypeInfo::wchartType())
506 display->m_partialValue = "L";
508 else
509 display->m_partialValue = display->m_type->m_displayString[0];
510 m_updateStruct.push_back(display);
513 display->updateValue(newValue->m_value);
516 void ExprWnd::updateStructValue(VarTree* display)
518 ASSERT(display->m_varKind == VarTree::VKstruct);
520 display->updateStructValue(display->m_partialValue);
521 // reset the value
522 display->m_partialValue = "";
523 display->m_exprIndex = -1;
526 void ExprWnd::replaceChildren(VarTree* display, ExprValue* newValues)
528 ASSERT(display->childCount() == 0 || display->m_varKind != VarTree::VKsimple);
530 // delete all children of display
531 while (VarTree* c = display->child(0)) {
532 unhookSubtree(c);
533 delete c;
535 // insert copies of the newValues
536 for (ExprValue* v = newValues->m_child; v != 0; v = v->m_next)
538 VarTree* vNew = new VarTree(display, v);
539 // recurse
540 replaceChildren(vNew, v);
544 void ExprWnd::collectUnknownTypes(VarTree* var)
546 QTreeWidgetItemIterator i(var);
547 for (; *i; ++i)
549 checkUnknownType(static_cast<VarTree*>(*i));
553 void ExprWnd::checkUnknownType(VarTree* var)
555 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
556 if (var->m_type == 0 &&
557 var->m_varKind == VarTree::VKstruct &&
558 var->m_nameKind != VarTree::NKtype &&
559 var->m_nameKind != VarTree::NKanonymous)
561 if (!var->isWcharT())
563 /* this struct node doesn't have a type yet: register it */
564 m_updateType.push_back(var);
566 else
568 var->m_type = TypeInfo::wchartType();
569 var->m_partialValue = "L";
570 m_updateStruct.push_back(var);
573 // add pointer pixmap to pointers
574 if (var->m_varKind == VarTree::VKpointer) {
575 var->setPixmap(m_pixPointer);
579 QString ExprWnd::formatWCharPointer(QString value)
581 int pos = value.indexOf(") ");
582 if (pos > 0)
583 value = value.mid(pos+2);
584 return value + " L";
588 VarTree* ExprWnd::topLevelExprByName(const QString& name) const
590 for (int i = 0; i < topLevelItemCount(); i++)
592 if (topLevelItem(i)->getText() == name)
593 return topLevelItem(i);
595 return 0;
598 VarTree* ExprWnd::ptrMemberByName(VarTree* v, const QString& name)
600 // v must be a pointer variable, must have children
601 if (v->m_varKind != VarTree::VKpointer || v->childCount() == 0)
602 return 0;
604 // the only child of v is the pointer value that represents the struct
605 VarTree* item = v->child(0);
606 return memberByName(item, name);
609 VarTree* ExprWnd::memberByName(VarTree* v, const QString& name)
611 // search immediate children for name
612 for (int i = 0; i < v->childCount(); i++)
614 if (v->child(i)->getText() == name)
615 return v->child(i);
618 // try in base classes and members that are anonymous structs or unions
619 for (int i = 0; i < v->childCount(); i++)
621 VarTree* item = v->child(i);
622 if (item->m_nameKind == VarTree::NKtype ||
623 item->m_nameKind == VarTree::NKanonymous)
625 item = memberByName(item, name);
626 if (item != 0)
627 return item;
630 return 0;
633 void ExprWnd::removeExpr(VarTree* item)
635 unhookSubtree(item);
637 delete item;
640 void ExprWnd::unhookSubtree(VarTree* subTree)
642 // must remove any pointers scheduled for update from the list
643 unhookSubtree(m_updatePtrs, subTree);
644 unhookSubtree(m_updateType, subTree);
645 unhookSubtree(m_updateStruct, subTree);
646 emit removingItem(subTree);
649 void ExprWnd::unhookSubtree(std::list<VarTree*>& list, VarTree* subTree)
651 if (subTree == 0)
652 return;
654 std::list<VarTree*>::iterator i = list.begin();
655 while (i != list.end()) {
656 std::list<VarTree*>::iterator checkItem = i;
657 ++i;
658 if (subTree->isAncestorEq(*checkItem)) {
659 // checkItem is an item from subTree
660 list.erase(checkItem);
665 void ExprWnd::clearPendingUpdates()
667 m_updatePtrs.clear();
668 m_updateType.clear();
669 m_updateStruct.clear();
672 VarTree* ExprWnd::nextUpdatePtr()
674 VarTree* ptr = 0;
675 if (!m_updatePtrs.empty()) {
676 ptr = m_updatePtrs.front();
677 m_updatePtrs.pop_front();
679 return ptr;
682 VarTree* ExprWnd::nextUpdateType()
684 VarTree* ptr = 0;
685 if (!m_updateType.empty()) {
686 ptr = m_updateType.front();
687 m_updateType.pop_front();
689 return ptr;
692 VarTree* ExprWnd::nextUpdateStruct()
694 VarTree* ptr = 0;
695 if (!m_updateStruct.empty()) {
696 ptr = m_updateStruct.front();
697 m_updateStruct.pop_front();
699 return ptr;
703 void ExprWnd::editValue(VarTree* item, const QString& text)
705 if (m_edit == 0)
706 m_edit = new ValueEdit(this);
708 QRect r = visualItemRect(item);
709 int x = columnViewportPosition(1);
710 int y = r.y();
711 int w = columnWidth(1);
712 int h = r.height();
715 * Make the edit widget at least 5 characters wide (but not wider than
716 * this widget). If less than half of this widget is used to display
717 * the text, scroll this widget so that half of it shows the text (or
718 * less than half of it if the text is shorter).
720 QFontMetrics metr = m_edit->font();
721 int wMin = metr.width("88888");
722 if (w < wMin)
723 w = wMin;
724 int wThis = viewport()->width();
725 if (x >= wThis/2 && // less than half the width displays text
726 x+w > wThis) // not all text is visible
728 // scroll so that more text is visible
729 QScrollBar* pScrollBar = horizontalScrollBar();
730 int wScroll = qMin(x-wThis/2, x+w-wThis);
731 pScrollBar->setValue(pScrollBar->value() + wScroll);
732 x -= wScroll;
734 else if (x < 0)
736 // don't let the edit move out at the left
737 x = 0;
740 // make the edit box as wide as the visible column
741 QRect rect(x,y, wThis-x,h);
742 m_edit->setText(text);
743 m_edit->selectAll();
745 m_edit->setGeometry(rect);
746 m_edit->m_finished = false;
747 m_edit->m_item = item;
748 m_edit->show();
749 m_edit->setFocus();
752 bool ExprWnd::isEditing() const
754 return m_edit != 0 && m_edit->isVisible();
758 ValueEdit::ValueEdit(ExprWnd* parent) :
759 QLineEdit(parent->viewport())
761 setFrame(false);
762 hide();
763 lower(); // lower the window below scrollbars
764 connect(parent, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
765 SLOT(slotSelectionChanged()));
766 connect(parent, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
767 SLOT(slotSelectionChanged()));
768 connect(parent, SIGNAL(itemExpanded(QTreeWidgetItem*)),
769 SLOT(slotSelectionChanged()));
770 connect(parent, SIGNAL(itemCollapsed(QTreeWidgetItem*)),
771 SLOT(slotSelectionChanged()));
772 connect(this, SIGNAL(done(VarTree*, const QString&)),
773 parent, SIGNAL(editValueCommitted(VarTree*, const QString&)));
776 ValueEdit::~ValueEdit()
780 void ValueEdit::terminate(bool commit)
782 TRACE(commit?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
783 if (!m_finished)
785 m_finished = true;
786 hide(); // will call focusOutEvent, that's why we need m_finished
787 if (commit) {
788 emit done(m_item, text());
793 void ValueEdit::keyPressEvent(QKeyEvent *e)
795 if(e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
796 terminate(true);
797 else if(e->key() == Qt::Key_Escape)
798 terminate(false);
799 else
800 QLineEdit::keyPressEvent(e);
803 void ValueEdit::paintEvent(QPaintEvent* e)
805 QLineEdit::paintEvent(e);
807 QPainter p(this);
808 p.drawRect(rect());
811 void ValueEdit::focusOutEvent(QFocusEvent* ev)
813 TRACE("ValueEdit::focusOutEvent");
814 QFocusEvent* focusEv = static_cast<QFocusEvent*>(ev);
815 if (focusEv->reason() == Qt::ActiveWindowFocusReason)
817 // Switching to a different window should terminate the edit,
818 // because if the window with this variable display is floating
819 // then that different window could be the main window, where
820 // the user had clicked one of the Execute buttons. This in turn
821 // may pull the item away that we are editing here.
822 terminate(false);
824 // Don't let a RMB close the editor
825 else if (focusEv->reason() != Qt::PopupFocusReason)
827 terminate(true);
831 void ValueEdit::slotSelectionChanged()
833 TRACE("ValueEdit::slotSelectionChanged");
834 terminate(false);