93492daab854686f517a28a4a4ae0ad70e6ff569
[kdbg.git] / kdbg / exprwnd.cpp
blob93492daab854686f517a28a4a4ae0ad70e6ff569
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "exprwnd.h"
7 #include "exprwnd.moc"
8 #include "typetable.h"
9 #include <qstrlist.h>
10 #include <qpainter.h>
11 #include <qscrollbar.h>
12 #include <kapp.h>
13 #include <kiconloader.h> /* icons */
14 #include <klocale.h> /* i18n */
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 #include "mydebug.h"
20 VarTree::VarTree(VarTree* parent, QListViewItem* after, ExprValue* v) :
21 QListViewItem(parent, after),
22 m_varKind(v->m_varKind),
23 m_nameKind(v->m_nameKind),
24 m_type(0),
25 m_exprIndex(0),
26 m_exprIndexUseGuard(false),
27 m_baseChanged(false),
28 m_structChanged(false)
30 QListViewItem::setText(0, v->m_name);
31 QListViewItem::setText(1, v->m_value);
32 setExpandable(m_varKind == VarTree::VKpointer);
33 setOpen(v->m_initiallyExpanded);
36 VarTree::VarTree(ExprWnd* parent, QListViewItem* after, const QString& name) :
37 QListViewItem(parent, after),
38 m_varKind(VKsimple),
39 m_nameKind(VarTree::NKplain),
40 m_type(0),
41 m_exprIndex(0),
42 m_exprIndexUseGuard(false),
43 m_baseChanged(false),
44 m_structChanged(false)
46 QListViewItem::setText(0, name);
49 VarTree::~VarTree()
53 void VarTree::paintCell(QPainter* p, const QColorGroup& cg, int column, int width, int align)
55 if (column == 1 && (m_baseChanged || m_structChanged)) {
56 QColorGroup cgChg = cg;
57 cgChg.setColor(QColorGroup::Text, Qt::red);
58 QListViewItem::paintCell(p, cgChg, column, width, align);
59 } else {
60 QListViewItem::paintCell(p, cg, column, width, align);
64 QString VarTree::computeExpr() const
66 // top-level items are special
67 if (isToplevelExpr())
68 return getText();
70 // get parent expr
71 VarTree* par = static_cast<VarTree*>(parent());
72 QString parentExpr = par->computeExpr();
74 /* don't add this item's name if this is a base class sub-item */
75 if (m_nameKind == NKtype) {
76 return parentExpr;
78 /* augment by this item's text */
79 QString result;
80 /* if this is an address, dereference it */
81 if (m_nameKind == NKaddress) {
82 ASSERT(par->m_varKind == VKpointer);
83 result = "*" + parentExpr;
84 return result;
86 switch (par->m_varKind) {
87 case VKarray:
89 QString index = getText();
90 int i = 1;
91 // skip past the index
92 while (index[i].isDigit())
93 i++;
95 * Some array indices are actually ranges due to repeated array
96 * values. We use the first index in these cases.
98 if (index[i] != ']') {
99 // remove second index
100 index.remove(i, index.length()-i-1);
102 result = "(" + parentExpr + ")" + index;
104 break;
105 case VKstruct:
106 result = "(" + parentExpr + ")." + getText();
107 break;
108 case VKsimple: /* parent can't be simple */
109 case VKpointer: /* handled in NKaddress */
110 case VKdummy: /* can't occur at all */
111 ASSERT(false);
112 result = parentExpr; /* paranoia */
113 break;
115 return result;
118 bool VarTree::isToplevelExpr() const
120 return parent() == 0;
123 bool VarTree::isAncestorEq(const VarTree* child) const
125 const QListViewItem* c = child;
126 while (c != 0 && c != this) {
127 c = c->parent();
129 return c != 0;
132 bool VarTree::updateValue(const QString& newValue)
134 // check whether the value changed
135 bool prevValueChanged = m_baseChanged;
136 if ((m_baseChanged = m_baseValue != newValue)) {
137 m_baseValue = newValue;
138 updateValueText();
141 * We must repaint the cell if the value changed. If it did not change,
142 * we still must repaint the cell if the value changed previously,
143 * because the color of the display must be changed (from red to
144 * black).
146 return m_baseChanged || prevValueChanged;
149 bool VarTree::updateStructValue(const QString& newValue)
151 // check whether the value changed
152 bool prevValueChanged = m_structChanged;
153 if ((m_structChanged = m_structValue != newValue)) {
154 m_structValue = newValue;
155 updateValueText();
158 * We must repaint the cell if the value changed. If it did not change,
159 * we still must repaint the cell if the value changed previously,
160 * because the color of the display must be changed (from red to
161 * black).
163 return m_structChanged || prevValueChanged;
166 void VarTree::updateValueText()
168 if (m_baseValue.isEmpty()) {
169 QListViewItem::setText(1, m_structValue);
170 } else if (m_structValue.isEmpty()) {
171 QListViewItem::setText(1, m_baseValue);
172 } else {
173 QListViewItem::setText(1, m_baseValue + " " + m_structValue);
177 void VarTree::inferTypesOfChildren(ProgramTypeTable& typeTable)
180 * Type inference works like this: We use type information of those
181 * children that have a type name in their name (base classes) or in
182 * their value (pointers)
185 // first recurse children
186 VarTree* child = firstChild();
187 while (child != 0) {
188 child->inferTypesOfChildren(typeTable);
189 child = child->nextSibling();
192 // if this is a pointer, get the type from the value (less the pointer)
193 if (m_varKind == VKpointer) {
194 if (isWcharT())
197 * wchart_t pointers must be treated as struct, because the array
198 * of characters is printed similar to how QStrings are decoded.
200 m_varKind = VKstruct;
201 setExpandable(false);
203 // don't know how to do this cleanly
204 } else if (m_varKind == VKstruct) {
205 // check if this is a base class part
206 if (m_nameKind == NKtype) {
207 const QString& typeName =
208 getText().mid(1, getText().length()-2); // strip < and >
209 m_type = typeTable.lookup(typeName);
211 /* if we don't have a type yet, get it from the base class */
212 if (m_type == 0) {
213 m_type = inferTypeFromBaseClass();
215 * If there is a known type now, it is the one from the
216 * first base class whose type we know.
221 * If we still don't have a type, the type is really unknown.
223 if (m_type == 0) {
224 m_type = TypeInfo::unknownType();
226 } // else
228 * This is not a base class part. We don't assign a type so
229 * that later we can ask gdb.
234 // the value contains the pointer type in parenthesis
235 bool VarTree::isWcharT() const
237 return value().startsWith("(const wchar_t *)") ||
238 value().startsWith("(wchar_t *)");
242 * Get the type of the first base class whose type we know.
244 TypeInfo* VarTree::inferTypeFromBaseClass()
246 if (m_varKind == VKstruct) {
247 VarTree* child = firstChild();
248 while (child != 0 &&
249 // only check base class parts (i.e. type names)
250 child->m_nameKind == NKtype)
252 if (child->m_type != 0 &&
253 child->m_type != TypeInfo::unknownType())
255 // got a type!
256 return child->m_type;
258 child = child->nextSibling();
261 return 0;
264 ExprValue::ExprValue(const QString& name, VarTree::NameKind aKind) :
265 m_name(name),
266 m_varKind(VarTree::VKsimple),
267 m_nameKind(aKind),
268 m_child(0),
269 m_next(0),
270 m_initiallyExpanded(false)
274 ExprValue::~ExprValue()
276 delete m_child;
277 delete m_next;
280 void ExprValue::appendChild(ExprValue* newChild)
282 if (m_child == 0) {
283 m_child = newChild;
284 } else {
285 // walk chain of children to find the last one
286 ExprValue* last = m_child;
287 while (last->m_next != 0)
288 last = last->m_next;
289 last->m_next = newChild;
291 newChild->m_next = 0; // just to be sure
294 int ExprValue::childCount() const
296 int i = 0;
297 ExprValue* c = m_child;
298 while (c) {
299 ++i;
300 c = c->m_next;
302 return i;
307 ExprWnd::ExprWnd(QWidget* parent, const QString& colHeader, const char* name) :
308 QListView(parent, name),
309 m_edit(0)
311 addColumn(colHeader);
312 addColumn(i18n("Value"));
313 setSorting(-1); // do not sort items
314 setColumnWidthMode(0, Manual);
315 setColumnWidthMode(1, Maximum);
316 setRootIsDecorated(true);
318 m_pixPointer = UserIcon("pointer.xpm");
319 if (m_pixPointer.isNull())
320 TRACE("Can't load pointer.xpm");
323 ExprWnd::~ExprWnd()
327 void ExprWnd::exprList(QStrList& exprs)
329 // ASSERT(exprs does deep-copies)
330 VarTree* item;
331 for (item = firstChild(); item != 0; item = item->nextSibling()) {
332 exprs.append(item->getText());
336 VarTree* ExprWnd::insertExpr(ExprValue* expr, ProgramTypeTable& typeTable)
338 // append a new dummy expression
339 VarTree* last = 0; // last top-level item
340 for (VarTree* i = firstChild(); i != 0; i = i->nextSibling()) {
341 last = i;
343 VarTree* display = new VarTree(this, last, expr->m_name);
345 // replace it right away
346 updateExpr(display, expr, typeTable);
347 return display;
350 void ExprWnd::updateExpr(ExprValue* expr, ProgramTypeTable& typeTable)
352 // search the root variable
353 VarTree* item = firstChild();
354 while (item != 0 && item->getText() != expr->m_name)
355 item = item->nextSibling();
356 if (item == 0) {
357 return;
359 // now update it
360 updateExprRec(item, expr, typeTable);
361 collectUnknownTypes(item);
364 void ExprWnd::updateExpr(VarTree* display, ExprValue* newValues, ProgramTypeTable& typeTable)
366 updateExprRec(display, newValues, typeTable);
367 collectUnknownTypes(display);
371 * returns true if there's a visible change
373 void ExprWnd::updateExprRec(VarTree* display, ExprValue* newValues, ProgramTypeTable& typeTable)
375 bool isExpanded = display->isOpen();
378 * If we are updating a pointer without children by a dummy, we don't
379 * collapse it, but simply insert the new children. This happens when a
380 * pointer has just been expanded by the user.
382 if (display->m_varKind == VarTree::VKpointer &&
383 display->childCount() == 0 &&
384 newValues->m_varKind == VarTree::VKdummy)
386 replaceChildren(display, newValues);
387 return;
391 * If the display and newValues have different kind or if their number
392 * of children is different, replace the whole sub-tree.
394 if (// the next two lines mean: not(m_varKind remains unchanged)
395 !(newValues->m_varKind == VarTree::VKdummy ||
396 display->m_varKind == newValues->m_varKind)
398 (display->childCount() != newValues->childCount() &&
400 * If this is a pointer and newValues doesn't have children, we
401 * don't replace the sub-tree; instead, below we mark this
402 * sub-tree for requiring an update.
404 (display->m_varKind != VarTree::VKpointer ||
405 newValues->m_child != 0)))
407 if (isExpanded) {
408 display->setOpen(false);
411 // since children changed, it is likely that the type has also changed
412 display->m_type = 0; /* will re-evaluate the type */
414 // display the new value
415 updateSingleExpr(display, newValues);
416 replaceChildren(display, newValues);
418 // update the m_varKind
419 if (newValues->m_varKind != VarTree::VKdummy) {
420 display->m_varKind = newValues->m_varKind;
421 display->setExpandable(newValues->m_varKind == VarTree::VKpointer);
424 // get some types (after the new m_varKind has been set!)
425 display->inferTypesOfChildren(typeTable);
427 // (note that the new value might not have a sub-tree at all)
428 return;
431 // display the new value
432 updateSingleExpr(display, newValues);
435 * If this is an expanded pointer, record it for being updated.
437 if (display->m_varKind == VarTree::VKpointer) {
438 if (isExpanded &&
439 // if newValues is a dummy, we have already updated this pointer
440 newValues->m_varKind != VarTree::VKdummy)
442 m_updatePtrs.append(display);
445 * If the visible sub-tree has children, but newValues doesn't, we
446 * can stop here.
448 if (newValues->m_child == 0) {
449 return;
453 ASSERT(display->childCount() == newValues->childCount());
455 // go for children
456 VarTree* vDisplay = display->firstChild();
457 ExprValue* vNew = newValues->m_child;
458 while (vDisplay != 0) {
459 // check whether the names are the same
460 if (vDisplay->getText() != vNew->m_name) {
461 // set new name
462 vDisplay->setText(vNew->m_name);
464 // recurse
465 updateExprRec(vDisplay, vNew, typeTable);
467 vDisplay = vDisplay->nextSibling();
468 vNew = vNew->m_next;
472 void ExprWnd::updateSingleExpr(VarTree* display, ExprValue* newValue)
475 * If newValues is a VKdummy, we are only interested in its children.
476 * No need to update anything here.
478 if (newValue->m_varKind == VarTree::VKdummy) {
479 return;
483 * If this node is a struct and we know its type then we know how to
484 * find a nested value. So register the node for an update.
486 * wchar_t types are also treated specially here: We consider them
487 * as struct (has been set in inferTypesOfChildren()).
489 if (display->m_varKind == VarTree::VKstruct &&
490 display->m_type != 0 &&
491 display->m_type != TypeInfo::unknownType())
493 ASSERT(newValue->m_varKind == VarTree::VKstruct);
494 if (display->m_type == TypeInfo::wchartType())
496 display->m_partialValue = "L";
498 else
499 display->m_partialValue = display->m_type->m_displayString[0];
500 m_updateStruct.append(display);
503 if (display->updateValue(newValue->m_value)) {
504 triggerUpdate();
508 void ExprWnd::updateStructValue(VarTree* display)
510 ASSERT(display->m_varKind == VarTree::VKstruct);
512 if (display->updateStructValue(display->m_partialValue)) {
513 triggerUpdate();
515 // reset the value
516 display->m_partialValue = "";
517 display->m_exprIndex = -1;
520 void ExprWnd::replaceChildren(VarTree* display, ExprValue* newValues)
522 ASSERT(display->childCount() == 0 || display->m_varKind != VarTree::VKsimple);
524 // delete all children of display
525 while (VarTree* c = display->firstChild()) {
526 unhookSubtree(c);
527 delete c;
529 // insert copies of the newValues
530 VarTree* vNew = 0;
531 for (ExprValue* v = newValues->m_child; v != 0; v = v->m_next)
533 vNew = new VarTree(display, vNew, v);
534 // recurse
535 replaceChildren(vNew, v);
539 void ExprWnd::collectUnknownTypes(VarTree* var)
541 QListViewItemIterator i(var);
542 for (; i.current(); ++i)
544 checkUnknownType(static_cast<VarTree*>(i.current()));
548 void ExprWnd::checkUnknownType(VarTree* var)
550 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
551 if (var->m_type == 0 &&
552 var->m_varKind == VarTree::VKstruct &&
553 var->m_nameKind != VarTree::NKtype)
555 if (!var->isWcharT())
557 /* this struct node doesn't have a type yet: register it */
558 m_updateType.append(var);
560 else
562 var->m_type = TypeInfo::wchartType();
563 var->m_partialValue = "L";
564 m_updateStruct.append(var);
567 // add pointer pixmap to pointers
568 if (var->m_varKind == VarTree::VKpointer) {
569 var->setPixmap(m_pixPointer);
573 QString ExprWnd::formatWCharPointer(QString value)
575 int pos = value.find(") ");
576 if (pos > 0)
577 value = value.mid(pos+2);
578 return value + " L";
582 VarTree* ExprWnd::topLevelExprByName(const char* name)
584 VarTree* item = firstChild();
585 while (item != 0 && item->getText() != name)
586 item = item->nextSibling();
588 return item;
591 VarTree* ExprWnd::ptrMemberByName(VarTree* v, const QString& name)
593 // v must be a pointer variable, must have children
594 if (v->m_varKind != VarTree::VKpointer || v->childCount() == 0)
595 return 0;
597 // the only child of v is the pointer value that represents the struct
598 VarTree* item = v->firstChild();
599 return memberByName(item, name);
602 VarTree* ExprWnd::memberByName(VarTree* v, const QString& name)
604 // search immediate children for name
605 VarTree* item = v->firstChild();
606 while (item != 0 && item->getText() != name)
607 item = item->nextSibling();
609 if (item != 0)
610 return item;
612 // try in base classes
613 item = v->firstChild();
614 while (item != 0 &&
615 item->m_nameKind == VarTree::NKtype)
617 v = memberByName(item, name);
618 if (v != 0)
619 return v;
620 item = item->nextSibling();
622 return 0;
625 void ExprWnd::removeExpr(VarTree* item)
627 unhookSubtree(item);
629 delete item;
632 void ExprWnd::unhookSubtree(VarTree* subTree)
634 // must remove any pointers scheduled for update from the list
635 unhookSubtree(m_updatePtrs, subTree);
636 unhookSubtree(m_updateType, subTree);
637 unhookSubtree(m_updateStruct, subTree);
638 emit removingItem(subTree);
641 void ExprWnd::unhookSubtree(QList<VarTree>& list, VarTree* subTree)
643 if (subTree == 0)
644 return;
646 VarTree* checkItem = list.first();
647 while (checkItem != 0) {
648 if (!subTree->isAncestorEq(checkItem)) {
649 // checkItem is not an item from subTree
650 // advance
651 checkItem = list.next();
652 } else {
653 // checkItem is an item from subTree
655 * If checkItem is the last item in the list, we need a special
656 * treatment, because remove()ing it steps the current item of
657 * the list in the "wrong" direction.
659 if (checkItem == list.getLast()) { // does not set current item
660 list.remove();
661 /* we deleted the last element, so we've finished */
662 checkItem = 0;
663 } else {
664 list.remove();
665 /* remove() advanced already */
666 checkItem = list.current();
672 void ExprWnd::clearPendingUpdates()
674 m_updatePtrs.clear();
675 m_updateType.clear();
676 m_updateStruct.clear();
679 VarTree* ExprWnd::nextUpdatePtr()
681 VarTree* ptr = m_updatePtrs.first();
682 if (ptr != 0) {
683 m_updatePtrs.remove();
685 return ptr;
688 VarTree* ExprWnd::nextUpdateType()
690 VarTree* ptr = m_updateType.first();
691 if (ptr != 0) {
692 m_updateType.remove();
694 return ptr;
697 VarTree* ExprWnd::nextUpdateStruct()
699 VarTree* ptr = m_updateStruct.first();
700 if (ptr != 0) {
701 m_updateStruct.remove();
703 return ptr;
707 void ExprWnd::editValue(VarTree* item, const QString& text)
709 if (m_edit == 0)
710 m_edit = new ValueEdit(this);
712 QRect r = itemRect(item);
713 int x = r.x()+columnWidth(0);
714 int y = r.y();
715 int w = columnWidth(1);
716 int h = r.height();
717 QListView* lv = item->listView();
720 * Make the edit widget at least 5 characters wide (but not wider than
721 * this widget). If less than half of this widget is used to display
722 * the text, scroll this widget so that half of it shows the text (or
723 * less than half of it if the text is shorter).
725 QFontMetrics metr = m_edit->font();
726 int wMin = metr.width("88888");
727 if (w < wMin)
728 w = wMin;
729 int wThis = lv->visibleWidth();
730 if (x >= wThis/2 && // less than half the width displays text
731 x+w > wThis) // not all text is visible
733 // scroll so that more text is visible
734 int wScroll = QMIN(x-wThis/2, x+w-wThis);
735 lv->scrollBy(wScroll, 0);
736 x -= wScroll;
738 else if (x < 0)
740 // don't let the edit move out at the left
741 x = 0;
744 // make the edit box as wide as the visible column
745 QRect rect(x,y, wThis-x,h);
746 m_edit->setText(text);
747 m_edit->selectAll();
749 m_edit->setGeometry(rect);
750 m_edit->m_finished = false;
751 m_edit->m_item = item;
752 m_edit->show();
753 m_edit->setFocus();
756 bool ExprWnd::isEditing() const
758 return m_edit != 0 && m_edit->isVisible();
762 ValueEdit::ValueEdit(ExprWnd* parent) :
763 QLineEdit(parent->viewport(), "valueedit")
765 setFrame(false);
766 hide();
767 lower(); // lower the window below scrollbars
768 connect(parent, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()));
769 connect(parent, SIGNAL(currentChanged(QListViewItem*)), SLOT(slotSelectionChanged()));
770 connect(parent, SIGNAL(expanded(QListViewItem*)), SLOT(slotSelectionChanged()));
771 connect(parent, SIGNAL(collapsed(QListViewItem*)), 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 // Don't let a RMB close the editor
816 if (focusEv->reason() != QFocusEvent::Popup &&
817 focusEv->reason() != QFocusEvent::ActiveWindow)
819 terminate(true);
823 void ValueEdit::slotSelectionChanged()
825 TRACE("ValueEdit::slotSelectionChanged");
826 terminate(false);