93492daab854686f517a28a4a4ae0ad70e6ff569
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
11 #include <qscrollbar.h>
13 #include <kiconloader.h> /* icons */
14 #include <klocale.h> /* i18n */
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
),
26 m_exprIndexUseGuard(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
),
39 m_nameKind(VarTree::NKplain
),
42 m_exprIndexUseGuard(false),
44 m_structChanged(false)
46 QListViewItem::setText(0, name
);
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
);
60 QListViewItem::paintCell(p
, cg
, column
, width
, align
);
64 QString
VarTree::computeExpr() const
66 // top-level items are special
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
) {
78 /* augment by this item's text */
80 /* if this is an address, dereference it */
81 if (m_nameKind
== NKaddress
) {
82 ASSERT(par
->m_varKind
== VKpointer
);
83 result
= "*" + parentExpr
;
86 switch (par
->m_varKind
) {
89 QString index
= getText();
91 // skip past the index
92 while (index
[i
].isDigit())
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
;
106 result
= "(" + parentExpr
+ ")." + getText();
108 case VKsimple
: /* parent can't be simple */
109 case VKpointer
: /* handled in NKaddress */
110 case VKdummy
: /* can't occur at all */
112 result
= parentExpr
; /* paranoia */
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) {
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
;
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
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
;
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
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
);
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();
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
) {
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 */
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.
224 m_type
= TypeInfo::unknownType();
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();
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())
256 return child
->m_type
;
258 child
= child
->nextSibling();
264 ExprValue::ExprValue(const QString
& name
, VarTree::NameKind aKind
) :
266 m_varKind(VarTree::VKsimple
),
270 m_initiallyExpanded(false)
274 ExprValue::~ExprValue()
280 void ExprValue::appendChild(ExprValue
* newChild
)
285 // walk chain of children to find the last one
286 ExprValue
* last
= m_child
;
287 while (last
->m_next
!= 0)
289 last
->m_next
= newChild
;
291 newChild
->m_next
= 0; // just to be sure
294 int ExprValue::childCount() const
297 ExprValue
* c
= m_child
;
307 ExprWnd::ExprWnd(QWidget
* parent
, const QString
& colHeader
, const char* name
) :
308 QListView(parent
, name
),
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");
327 void ExprWnd::exprList(QStrList
& exprs
)
329 // ASSERT(exprs does deep-copies)
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()) {
343 VarTree
* display
= new VarTree(this, last
, expr
->m_name
);
345 // replace it right away
346 updateExpr(display
, expr
, typeTable
);
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();
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
);
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)))
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)
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
) {
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
448 if (newValues
->m_child
== 0) {
453 ASSERT(display
->childCount() == newValues
->childCount());
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
) {
462 vDisplay
->setText(vNew
->m_name
);
465 updateExprRec(vDisplay
, vNew
, typeTable
);
467 vDisplay
= vDisplay
->nextSibling();
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
) {
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";
499 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
500 m_updateStruct
.append(display
);
503 if (display
->updateValue(newValue
->m_value
)) {
508 void ExprWnd::updateStructValue(VarTree
* display
)
510 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
512 if (display
->updateStructValue(display
->m_partialValue
)) {
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()) {
529 // insert copies of the newValues
531 for (ExprValue
* v
= newValues
->m_child
; v
!= 0; v
= v
->m_next
)
533 vNew
= new VarTree(display
, vNew
, v
);
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
);
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(") ");
577 value
= value
.mid(pos
+2);
582 VarTree
* ExprWnd::topLevelExprByName(const char* name
)
584 VarTree
* item
= firstChild();
585 while (item
!= 0 && item
->getText() != name
)
586 item
= item
->nextSibling();
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)
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();
612 // try in base classes
613 item
= v
->firstChild();
615 item
->m_nameKind
== VarTree::NKtype
)
617 v
= memberByName(item
, name
);
620 item
= item
->nextSibling();
625 void ExprWnd::removeExpr(VarTree
* 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
)
646 VarTree
* checkItem
= list
.first();
647 while (checkItem
!= 0) {
648 if (!subTree
->isAncestorEq(checkItem
)) {
649 // checkItem is not an item from subTree
651 checkItem
= list
.next();
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
661 /* we deleted the last element, so we've finished */
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();
683 m_updatePtrs
.remove();
688 VarTree
* ExprWnd::nextUpdateType()
690 VarTree
* ptr
= m_updateType
.first();
692 m_updateType
.remove();
697 VarTree
* ExprWnd::nextUpdateStruct()
699 VarTree
* ptr
= m_updateStruct
.first();
701 m_updateStruct
.remove();
707 void ExprWnd::editValue(VarTree
* item
, const QString
& text
)
710 m_edit
= new ValueEdit(this);
712 QRect r
= itemRect(item
);
713 int x
= r
.x()+columnWidth(0);
715 int w
= columnWidth(1);
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");
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);
740 // don't let the edit move out at the left
744 // make the edit box as wide as the visible column
745 QRect
rect(x
,y
, wThis
-x
,h
);
746 m_edit
->setText(text
);
749 m_edit
->setGeometry(rect
);
750 m_edit
->m_finished
= false;
751 m_edit
->m_item
= item
;
756 bool ExprWnd::isEditing() const
758 return m_edit
!= 0 && m_edit
->isVisible();
762 ValueEdit::ValueEdit(ExprWnd
* parent
) :
763 QLineEdit(parent
->viewport(), "valueedit")
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)");
786 hide(); // will call focusOutEvent, that's why we need m_finished
788 emit
done(m_item
, text());
793 void ValueEdit::keyPressEvent(QKeyEvent
*e
)
795 if(e
->key() == Qt::Key_Return
|| e
->key() == Qt::Key_Enter
)
797 else if(e
->key() == Qt::Key_Escape
)
800 QLineEdit::keyPressEvent(e
);
803 void ValueEdit::paintEvent(QPaintEvent
* e
)
805 QLineEdit::paintEvent(e
);
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
)
823 void ValueEdit::slotSelectionChanged()
825 TRACE("ValueEdit::slotSelectionChanged");