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),
27 m_baseValue(v
->m_value
),
29 m_structChanged(false)
31 QListViewItem::setText(0, v
->m_name
);
33 setExpandable(m_varKind
== VarTree::VKpointer
);
34 setOpen(v
->m_initiallyExpanded
);
37 VarTree::VarTree(ExprWnd
* parent
, QListViewItem
* after
, const QString
& name
) :
38 QListViewItem(parent
, after
),
40 m_nameKind(VarTree::NKplain
),
43 m_exprIndexUseGuard(false),
45 m_structChanged(false)
47 QListViewItem::setText(0, name
);
54 void VarTree::paintCell(QPainter
* p
, const QColorGroup
& cg
, int column
, int width
, int align
)
56 if (column
== 1 && (m_baseChanged
|| m_structChanged
)) {
57 QColorGroup cgChg
= cg
;
58 cgChg
.setColor(QColorGroup::Text
, Qt::red
);
59 QListViewItem::paintCell(p
, cgChg
, column
, width
, align
);
61 QListViewItem::paintCell(p
, cg
, column
, width
, align
);
65 QString
VarTree::computeExpr() const
67 // top-level items are special
72 VarTree
* par
= static_cast<VarTree
*>(parent());
73 QString parentExpr
= par
->computeExpr();
75 // skip this item's name if it is a base class or anonymous struct or union
76 if (m_nameKind
== NKtype
|| m_nameKind
== NKanonymous
) {
79 /* augment by this item's text */
81 /* if this is an address, dereference it */
82 if (m_nameKind
== NKaddress
) {
83 ASSERT(par
->m_varKind
== VKpointer
);
84 result
= "*" + parentExpr
;
87 switch (par
->m_varKind
) {
90 QString index
= getText();
92 // skip past the index
93 while (index
[i
].isDigit())
96 * Some array indices are actually ranges due to repeated array
97 * values. We use the first index in these cases.
99 if (index
[i
] != ']') {
100 // remove second index
101 index
.remove(i
, index
.length()-i
-1);
103 result
= "(" + parentExpr
+ ")" + index
;
107 result
= "(" + parentExpr
+ ")." + getText();
109 case VKsimple
: /* parent can't be simple */
110 case VKpointer
: /* handled in NKaddress */
111 case VKdummy
: /* can't occur at all */
113 result
= parentExpr
; /* paranoia */
119 bool VarTree::isToplevelExpr() const
121 return parent() == 0;
124 bool VarTree::isAncestorEq(const VarTree
* child
) const
126 const QListViewItem
* c
= child
;
127 while (c
!= 0 && c
!= this) {
133 bool VarTree::updateValue(const QString
& newValue
)
135 // check whether the value changed
136 bool prevValueChanged
= m_baseChanged
;
137 if ((m_baseChanged
= m_baseValue
!= newValue
)) {
138 m_baseValue
= newValue
;
142 * We must repaint the cell if the value changed. If it did not change,
143 * we still must repaint the cell if the value changed previously,
144 * because the color of the display must be changed (from red to
147 return m_baseChanged
|| prevValueChanged
;
150 bool VarTree::updateStructValue(const QString
& newValue
)
152 // check whether the value changed
153 bool prevValueChanged
= m_structChanged
;
154 if ((m_structChanged
= m_structValue
!= newValue
)) {
155 m_structValue
= newValue
;
159 * We must repaint the cell if the value changed. If it did not change,
160 * we still must repaint the cell if the value changed previously,
161 * because the color of the display must be changed (from red to
164 return m_structChanged
|| prevValueChanged
;
167 void VarTree::updateValueText()
169 if (m_baseValue
.isEmpty()) {
170 QListViewItem::setText(1, m_structValue
);
171 } else if (m_structValue
.isEmpty()) {
172 QListViewItem::setText(1, m_baseValue
);
174 QListViewItem::setText(1, m_baseValue
+ " " + m_structValue
);
178 void VarTree::inferTypesOfChildren(ProgramTypeTable
& typeTable
)
181 * Type inference works like this: We use type information of those
182 * children that have a type name in their name (base classes) or in
183 * their value (pointers)
186 // first recurse children
187 VarTree
* child
= firstChild();
189 child
->inferTypesOfChildren(typeTable
);
190 child
= child
->nextSibling();
193 // if this is a pointer, get the type from the value (less the pointer)
194 if (m_varKind
== VKpointer
) {
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 setExpandable(false);
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 */
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.
225 m_type
= TypeInfo::unknownType();
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 TypeInfo
* VarTree::inferTypeFromBaseClass()
247 if (m_varKind
== VKstruct
) {
248 VarTree
* child
= firstChild();
250 // only check base class parts (i.e. type names)
251 child
->m_nameKind
== NKtype
)
253 if (child
->m_type
!= 0 &&
254 child
->m_type
!= TypeInfo::unknownType())
257 return child
->m_type
;
259 child
= child
->nextSibling();
265 ExprValue::ExprValue(const QString
& name
, VarTree::NameKind aKind
) :
267 m_varKind(VarTree::VKsimple
),
271 m_initiallyExpanded(false)
275 ExprValue::~ExprValue()
281 void ExprValue::appendChild(ExprValue
* newChild
)
286 // walk chain of children to find the last one
287 ExprValue
* last
= m_child
;
288 while (last
->m_next
!= 0)
290 last
->m_next
= newChild
;
292 newChild
->m_next
= 0; // just to be sure
295 int ExprValue::childCount() const
298 ExprValue
* c
= m_child
;
308 ExprWnd::ExprWnd(QWidget
* parent
, const QString
& colHeader
, const char* name
) :
309 QListView(parent
, name
),
312 addColumn(colHeader
);
313 addColumn(i18n("Value"));
314 setSorting(-1); // do not sort items
315 setColumnWidthMode(0, Manual
);
316 setColumnWidthMode(1, Maximum
);
317 setRootIsDecorated(true);
318 setAllColumnsShowFocus(true);
320 m_pixPointer
= UserIcon("pointer.xpm");
321 if (m_pixPointer
.isNull())
322 TRACE("Can't load pointer.xpm");
329 void ExprWnd::exprList(QStrList
& exprs
)
331 // ASSERT(exprs does deep-copies)
333 for (item
= firstChild(); item
!= 0; item
= item
->nextSibling()) {
334 exprs
.append(item
->getText());
338 VarTree
* ExprWnd::insertExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
340 // append a new dummy expression
341 VarTree
* last
= 0; // last top-level item
342 for (VarTree
* i
= firstChild(); i
!= 0; i
= i
->nextSibling()) {
345 VarTree
* display
= new VarTree(this, last
, expr
->m_name
);
347 // replace it right away
348 updateExpr(display
, expr
, typeTable
);
352 void ExprWnd::updateExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
354 // search the root variable
355 VarTree
* item
= firstChild();
356 while (item
!= 0 && item
->getText() != expr
->m_name
)
357 item
= item
->nextSibling();
362 updateExprRec(item
, expr
, typeTable
);
363 collectUnknownTypes(item
);
366 void ExprWnd::updateExpr(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
368 updateExprRec(display
, newValues
, typeTable
);
369 collectUnknownTypes(display
);
373 * returns true if there's a visible change
375 void ExprWnd::updateExprRec(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
377 bool isExpanded
= display
->isOpen();
380 * If we are updating a pointer without children by a dummy, we don't
381 * collapse it, but simply insert the new children. This happens when a
382 * pointer has just been expanded by the user.
384 if (display
->m_varKind
== VarTree::VKpointer
&&
385 display
->childCount() == 0 &&
386 newValues
->m_varKind
== VarTree::VKdummy
)
388 replaceChildren(display
, newValues
);
393 * If the display and newValues have different kind or if their number
394 * of children is different, replace the whole sub-tree.
396 if (// the next two lines mean: not(m_varKind remains unchanged)
397 !(newValues
->m_varKind
== VarTree::VKdummy
||
398 display
->m_varKind
== newValues
->m_varKind
)
400 (display
->childCount() != newValues
->childCount() &&
402 * If this is a pointer and newValues doesn't have children, we
403 * don't replace the sub-tree; instead, below we mark this
404 * sub-tree for requiring an update.
406 (display
->m_varKind
!= VarTree::VKpointer
||
407 newValues
->m_child
!= 0)))
410 display
->setOpen(false);
413 // since children changed, it is likely that the type has also changed
414 display
->m_type
= 0; /* will re-evaluate the type */
416 // display the new value
417 updateSingleExpr(display
, newValues
);
418 replaceChildren(display
, newValues
);
420 // update the m_varKind
421 if (newValues
->m_varKind
!= VarTree::VKdummy
) {
422 display
->m_varKind
= newValues
->m_varKind
;
423 display
->setExpandable(newValues
->m_varKind
== VarTree::VKpointer
);
426 // get some types (after the new m_varKind has been set!)
427 display
->inferTypesOfChildren(typeTable
);
429 // (note that the new value might not have a sub-tree at all)
433 // display the new value
434 updateSingleExpr(display
, newValues
);
437 * If this is an expanded pointer, record it for being updated.
439 if (display
->m_varKind
== VarTree::VKpointer
) {
441 // if newValues is a dummy, we have already updated this pointer
442 newValues
->m_varKind
!= VarTree::VKdummy
)
444 m_updatePtrs
.append(display
);
447 * If the visible sub-tree has children, but newValues doesn't, we
450 if (newValues
->m_child
== 0) {
455 ASSERT(display
->childCount() == newValues
->childCount());
458 VarTree
* vDisplay
= display
->firstChild();
459 ExprValue
* vNew
= newValues
->m_child
;
460 while (vDisplay
!= 0) {
461 // check whether the names are the same
462 if (vDisplay
->getText() != vNew
->m_name
) {
464 vDisplay
->setText(vNew
->m_name
);
467 updateExprRec(vDisplay
, vNew
, typeTable
);
469 vDisplay
= vDisplay
->nextSibling();
474 void ExprWnd::updateSingleExpr(VarTree
* display
, ExprValue
* newValue
)
477 * If newValues is a VKdummy, we are only interested in its children.
478 * No need to update anything here.
480 if (newValue
->m_varKind
== VarTree::VKdummy
) {
485 * If this node is a struct and we know its type then we know how to
486 * find a nested value. So register the node for an update.
488 * wchar_t types are also treated specially here: We consider them
489 * as struct (has been set in inferTypesOfChildren()).
491 if (display
->m_varKind
== VarTree::VKstruct
&&
492 display
->m_type
!= 0 &&
493 display
->m_type
!= TypeInfo::unknownType())
495 ASSERT(newValue
->m_varKind
== VarTree::VKstruct
);
496 if (display
->m_type
== TypeInfo::wchartType())
498 display
->m_partialValue
= "L";
501 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
502 m_updateStruct
.append(display
);
505 if (display
->updateValue(newValue
->m_value
)) {
510 void ExprWnd::updateStructValue(VarTree
* display
)
512 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
514 if (display
->updateStructValue(display
->m_partialValue
)) {
518 display
->m_partialValue
= "";
519 display
->m_exprIndex
= -1;
522 void ExprWnd::replaceChildren(VarTree
* display
, ExprValue
* newValues
)
524 ASSERT(display
->childCount() == 0 || display
->m_varKind
!= VarTree::VKsimple
);
526 // delete all children of display
527 while (VarTree
* c
= display
->firstChild()) {
531 // insert copies of the newValues
533 for (ExprValue
* v
= newValues
->m_child
; v
!= 0; v
= v
->m_next
)
535 vNew
= new VarTree(display
, vNew
, v
);
537 replaceChildren(vNew
, v
);
541 void ExprWnd::collectUnknownTypes(VarTree
* var
)
543 QListViewItemIterator
i(var
);
544 for (; i
.current(); ++i
)
546 checkUnknownType(static_cast<VarTree
*>(i
.current()));
550 void ExprWnd::checkUnknownType(VarTree
* var
)
552 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
553 if (var
->m_type
== 0 &&
554 var
->m_varKind
== VarTree::VKstruct
&&
555 var
->m_nameKind
!= VarTree::NKtype
&&
556 var
->m_nameKind
!= VarTree::NKanonymous
)
558 if (!var
->isWcharT())
560 /* this struct node doesn't have a type yet: register it */
561 m_updateType
.append(var
);
565 var
->m_type
= TypeInfo::wchartType();
566 var
->m_partialValue
= "L";
567 m_updateStruct
.append(var
);
570 // add pointer pixmap to pointers
571 if (var
->m_varKind
== VarTree::VKpointer
) {
572 var
->setPixmap(m_pixPointer
);
576 QString
ExprWnd::formatWCharPointer(QString value
)
578 int pos
= value
.find(") ");
580 value
= value
.mid(pos
+2);
585 VarTree
* ExprWnd::topLevelExprByName(const char* name
)
587 VarTree
* item
= firstChild();
588 while (item
!= 0 && item
->getText() != name
)
589 item
= item
->nextSibling();
594 VarTree
* ExprWnd::ptrMemberByName(VarTree
* v
, const QString
& name
)
596 // v must be a pointer variable, must have children
597 if (v
->m_varKind
!= VarTree::VKpointer
|| v
->childCount() == 0)
600 // the only child of v is the pointer value that represents the struct
601 VarTree
* item
= v
->firstChild();
602 return memberByName(item
, name
);
605 VarTree
* ExprWnd::memberByName(VarTree
* v
, const QString
& name
)
607 // search immediate children for name
608 VarTree
* item
= v
->firstChild();
609 while (item
!= 0 && item
->getText() != name
)
610 item
= item
->nextSibling();
615 // try in base classes and members that are anonymous structs or unions
616 item
= v
->firstChild();
619 if (item
->m_nameKind
== VarTree::NKtype
||
620 item
->m_nameKind
== VarTree::NKanonymous
)
622 v
= memberByName(item
, name
);
626 item
= item
->nextSibling();
631 void ExprWnd::removeExpr(VarTree
* item
)
638 void ExprWnd::unhookSubtree(VarTree
* subTree
)
640 // must remove any pointers scheduled for update from the list
641 unhookSubtree(m_updatePtrs
, subTree
);
642 unhookSubtree(m_updateType
, subTree
);
643 unhookSubtree(m_updateStruct
, subTree
);
644 emit
removingItem(subTree
);
647 void ExprWnd::unhookSubtree(QList
<VarTree
>& list
, VarTree
* subTree
)
652 VarTree
* checkItem
= list
.first();
653 while (checkItem
!= 0) {
654 if (!subTree
->isAncestorEq(checkItem
)) {
655 // checkItem is not an item from subTree
657 checkItem
= list
.next();
659 // checkItem is an item from subTree
661 * If checkItem is the last item in the list, we need a special
662 * treatment, because remove()ing it steps the current item of
663 * the list in the "wrong" direction.
665 if (checkItem
== list
.getLast()) { // does not set current item
667 /* we deleted the last element, so we've finished */
671 /* remove() advanced already */
672 checkItem
= list
.current();
678 void ExprWnd::clearPendingUpdates()
680 m_updatePtrs
.clear();
681 m_updateType
.clear();
682 m_updateStruct
.clear();
685 VarTree
* ExprWnd::nextUpdatePtr()
687 VarTree
* ptr
= m_updatePtrs
.first();
689 m_updatePtrs
.remove();
694 VarTree
* ExprWnd::nextUpdateType()
696 VarTree
* ptr
= m_updateType
.first();
698 m_updateType
.remove();
703 VarTree
* ExprWnd::nextUpdateStruct()
705 VarTree
* ptr
= m_updateStruct
.first();
707 m_updateStruct
.remove();
713 void ExprWnd::editValue(VarTree
* item
, const QString
& text
)
716 m_edit
= new ValueEdit(this);
718 QRect r
= itemRect(item
);
719 int x
= r
.x()+columnWidth(0);
721 int w
= columnWidth(1);
723 QListView
* lv
= item
->listView();
726 * Make the edit widget at least 5 characters wide (but not wider than
727 * this widget). If less than half of this widget is used to display
728 * the text, scroll this widget so that half of it shows the text (or
729 * less than half of it if the text is shorter).
731 QFontMetrics metr
= m_edit
->font();
732 int wMin
= metr
.width("88888");
735 int wThis
= lv
->visibleWidth();
736 if (x
>= wThis
/2 && // less than half the width displays text
737 x
+w
> wThis
) // not all text is visible
739 // scroll so that more text is visible
740 int wScroll
= QMIN(x
-wThis
/2, x
+w
-wThis
);
741 lv
->scrollBy(wScroll
, 0);
746 // don't let the edit move out at the left
750 // make the edit box as wide as the visible column
751 QRect
rect(x
,y
, wThis
-x
,h
);
752 m_edit
->setText(text
);
755 m_edit
->setGeometry(rect
);
756 m_edit
->m_finished
= false;
757 m_edit
->m_item
= item
;
762 bool ExprWnd::isEditing() const
764 return m_edit
!= 0 && m_edit
->isVisible();
768 ValueEdit::ValueEdit(ExprWnd
* parent
) :
769 QLineEdit(parent
->viewport(), "valueedit")
773 lower(); // lower the window below scrollbars
774 connect(parent
, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged()));
775 connect(parent
, SIGNAL(currentChanged(QListViewItem
*)), SLOT(slotSelectionChanged()));
776 connect(parent
, SIGNAL(expanded(QListViewItem
*)), SLOT(slotSelectionChanged()));
777 connect(parent
, SIGNAL(collapsed(QListViewItem
*)), SLOT(slotSelectionChanged()));
778 connect(this, SIGNAL(done(VarTree
*, const QString
&)),
779 parent
, SIGNAL(editValueCommitted(VarTree
*, const QString
&)));
782 ValueEdit::~ValueEdit()
786 void ValueEdit::terminate(bool commit
)
788 TRACE(commit
?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
792 hide(); // will call focusOutEvent, that's why we need m_finished
794 emit
done(m_item
, text());
799 void ValueEdit::keyPressEvent(QKeyEvent
*e
)
801 if(e
->key() == Qt::Key_Return
|| e
->key() == Qt::Key_Enter
)
803 else if(e
->key() == Qt::Key_Escape
)
806 QLineEdit::keyPressEvent(e
);
809 void ValueEdit::paintEvent(QPaintEvent
* e
)
811 QLineEdit::paintEvent(e
);
817 void ValueEdit::focusOutEvent(QFocusEvent
* ev
)
819 TRACE("ValueEdit::focusOutEvent");
820 QFocusEvent
* focusEv
= static_cast<QFocusEvent
*>(ev
);
821 if (focusEv
->reason() == QFocusEvent::ActiveWindow
)
823 // Switching to a different window should terminate the edit,
824 // because if the window with this variable display is floating
825 // then that different window could be the main window, where
826 // the user had clicked one of the Execute buttons. This in turn
827 // may pull the item away that we are editing here.
830 // Don't let a RMB close the editor
831 else if (focusEv
->reason() != QFocusEvent::Popup
)
837 void ValueEdit::slotSelectionChanged()
839 TRACE("ValueEdit::slotSelectionChanged");