3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
12 #include <kiconloader.h> /* icons */
18 VarTree::VarTree(const QString
& name
, NameKind aKind
) :
22 m_valueChanged(false),
25 m_exprIndexUseGuard(false)
33 void VarTree::paintValue(QPainter
* p
)
36 int cellHeight
= height(p
->fontMetrics());
38 int textY
= (cellHeight
- p
->fontMetrics().height()) / 2 +
39 p
->fontMetrics().ascent();
44 // p->setBackgroundColor(cg.base());
45 p
->drawText(textX
, textY
, m_value
, m_value
.length());
49 int VarTree::valueWidth()
52 return owner
->fontMetrics().width(m_value
) + 4;
55 QString
VarTree::computeExpr() const
57 assert(getParent() != 0);
62 // top-level items are special
63 if (getParent()->getParent() == 0)
67 VarTree
* par
= static_cast<VarTree
*>(getParent());
68 QString parentExpr
= par
->computeExpr();
70 /* don't add this item's name if this is a base class sub-item */
71 if (m_nameKind
== NKtype
) {
74 /* augment by this item's text */
76 /* if this is an address, dereference it */
77 if (m_nameKind
== NKaddress
) {
78 ASSERT(par
->m_varKind
== VKpointer
);
79 result
= "*" + parentExpr
;
82 switch (par
->m_varKind
) {
85 QString index
= getText();
87 // skip past the index
88 while (index
[i
].isDigit())
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
;
102 result
= "(" + parentExpr
+ ")." + getText();
104 case VKsimple
: /* parent can't be simple */
105 case VKpointer
: /* handled in NKaddress */
106 case VKdummy
: /* can't occur at all */
108 result
= parentExpr
; /* paranoia */
114 bool VarTree::isToplevelExpr() const
116 return getParent() != 0 && getParent()->getParent() == 0;
119 bool VarTree::isAncestorEq(const VarTree
* child
) const
121 const KTreeViewItem
* c
= child
;
122 while (c
!= 0 && c
!= this) {
128 bool VarTree::updateValue(const QString
& newValue
)
130 // check whether the value changed
131 bool prevValueChanged
= m_valueChanged
;
132 m_valueChanged
= false;
133 if (m_value
!= newValue
) {
135 m_valueChanged
= true;
138 * We must repaint the cell if the value changed. If it did not change,
139 * we still must repaint the cell if the value changed previously,
140 * because the color of the display must be changed (from red to
143 return m_valueChanged
|| prevValueChanged
;
146 void VarTree::inferTypesOfChildren(ProgramTypeTable
& typeTable
)
149 * Type inference works like this: We use type information of those
150 * children that have a type name in their name (base classes) or in
151 * their value (pointers)
154 // first recurse children
155 VarTree
* child
= static_cast<VarTree
*>(getChild());
157 child
->inferTypesOfChildren(typeTable
);
158 child
= static_cast<VarTree
*>(child
->getSibling());
161 // if this is a pointer, get the type from the value (less the pointer)
162 if (m_varKind
== VKpointer
) {
163 #ifndef I_know_a_way_to_do_this_cleanly
166 const char* p
= m_value
.data();
167 const char* start
= p
;
168 // the type of the pointer shows up in the value (sometimes)
169 if (p
== 0 || *p
!= '(')
171 skipNested(p
, '(', ')');
173 * We only recognize pointers to data "(int *)" but not pointers
174 * to functions "(void (*)())".
176 if (p
-start
< 3 && /* at least 3 chars necessary: (*) */
177 p
[-2] != '*') /* skip back before the closing paren */
181 const QString
& typeName
=
182 FROM_LATIN1(start
+1, p
-start
-3) // minus 3 chars
184 m_type
= typeTable
.lookup(typeName
);
186 m_type
= TypeInfo::unknownType();
189 } else if (m_varKind
== VKstruct
) {
190 // check if this is a base class part
191 if (m_nameKind
== NKtype
) {
192 const QString
& typeName
=
193 text
.mid(1, text
.length()-2); // strip < and >
194 m_type
= typeTable
.lookup(typeName
);
196 /* if we don't have a type yet, get it from the base class */
198 m_type
= inferTypeFromBaseClass();
200 * If there is a known type now, it is the one from the
201 * first base class whose type we know.
206 * If we still don't have a type, the type is really unknown.
209 m_type
= TypeInfo::unknownType();
213 * This is not a base class part. We don't assign a type so
214 * that later we can ask gdb.
220 * Get the type of the first base class whose type we know.
222 TypeInfo
* VarTree::inferTypeFromBaseClass()
224 if (m_varKind
== VKstruct
) {
225 VarTree
* child
= static_cast<VarTree
*>(getChild());
227 // only check base class parts (i.e. type names)
228 child
->m_nameKind
== NKtype
)
230 if (child
->m_type
!= 0 &&
231 child
->m_type
!= TypeInfo::unknownType())
234 return child
->m_type
;
236 child
= static_cast<VarTree
*>(child
->getSibling());
243 ExprWnd::ExprWnd(QWidget
* parent
, const char* name
) :
244 KTreeView(parent
, name
),
249 connect(this, SIGNAL(expanded(int)), SLOT(slotExpandOrCollapse(int)));
250 connect(this, SIGNAL(collapsed(int)), SLOT(slotExpandOrCollapse(int)));
252 m_pixPointer
= BarIcon("pointer.xpm");
253 if (m_pixPointer
.isNull())
254 TRACE("Can't load pointer.xpm");
261 void ExprWnd::exprList(QStrList
& exprs
)
263 // ASSERT(exprs does deep-copies)
265 for (item
= itemAt(0); item
!= 0; item
= item
->getSibling()) {
266 exprs
.append(item
->getText());
270 void ExprWnd::insertExpr(VarTree
* expr
)
272 // append the expression
275 collectUnknownTypes(expr
);
280 void ExprWnd::updateExpr(VarTree
* expr
)
282 // search the root variable
283 QString p
= expr
->getText();
286 KTreeViewItem
* item
= itemAt(path
);
292 if (updateExprRec(static_cast<VarTree
*>(item
), expr
)) {
293 updateVisibleItems();
297 collectUnknownTypes(static_cast<VarTree
*>(item
));
300 void ExprWnd::updateExpr(VarTree
* display
, VarTree
* newValues
)
302 if (updateExprRec(display
, newValues
) && display
->isVisible()) {
303 updateVisibleItems();
307 collectUnknownTypes(display
);
311 * returns true if there's a visible change
313 bool ExprWnd::updateExprRec(VarTree
* display
, VarTree
* newValues
)
315 bool isExpanded
= display
->isExpanded();
318 * If we are updating a pointer without children by a dummy, we don't
319 * collapse it, but simply insert the new children. This happens when a
320 * pointer has just been expanded by the user.
322 if (display
->m_varKind
== VarTree::VKpointer
&&
323 display
->childCount() == 0 &&
324 newValues
->m_varKind
== VarTree::VKdummy
)
326 replaceChildren(display
, newValues
);
327 return isExpanded
; /* no visible change if not expanded */
331 * If the display and newValues have different kind or if their number
332 * of children is different, replace the whole sub-tree.
334 if (// the next two lines mean: not(m_varKind remains unchanged)
335 !(newValues
->m_varKind
== VarTree::VKdummy
||
336 display
->m_varKind
== newValues
->m_varKind
)
338 (display
->childCount() != newValues
->childCount() &&
340 * If this is a pointer and newValues doesn't have children, we
341 * don't replace the sub-tree; instead, below we mark this
342 * sub-tree for requiring an update.
344 (display
->m_varKind
!= VarTree::VKpointer
||
345 newValues
->childCount() != 0)))
348 collapseSubTree(display
, false);
351 // since children changed, it is likely that the type has also changed
352 display
->m_type
= 0; /* will re-evaluate the type */
354 // display the new value
355 updateSingleExpr(display
, newValues
);
356 replaceChildren(display
, newValues
);
358 // update the m_varKind
359 if (newValues
->m_varKind
!= VarTree::VKdummy
) {
360 display
->m_varKind
= newValues
->m_varKind
;
361 display
->setDelayedExpanding(newValues
->m_varKind
== VarTree::VKpointer
);
364 // (note that the new value might not have a sub-tree at all)
365 return display
->m_valueChanged
|| isExpanded
; /* no visible change if not expanded */
368 // display the new value
369 updateSingleExpr(display
, newValues
);
372 * If this is an expanded pointer, record it for being updated.
374 if (display
->m_varKind
== VarTree::VKpointer
) {
376 // if newValues is a dummy, we have already updated this pointer
377 newValues
->m_varKind
!= VarTree::VKdummy
)
379 m_updatePtrs
.append(display
);
382 * If the visible sub-tree has children, but newValues doesn't, we
385 if (newValues
->childCount() == 0) {
386 return display
->m_valueChanged
;
390 ASSERT(display
->childCount() == newValues
->childCount());
393 bool childChanged
= false;
395 VarTree
* vDisplay
= static_cast<VarTree
*>(display
->getChild());
396 VarTree
* vNew
= static_cast<VarTree
*>(newValues
->getChild());
397 while (vDisplay
!= 0) {
398 // check whether the names are the same
399 if (strcmp(vDisplay
->getText(), vNew
->getText()) != 0) {
401 vDisplay
->setText(vNew
->getText());
402 int i
= itemRow(vDisplay
);
404 updateCell(i
, 0, true);
409 if (updateExprRec(vDisplay
, vNew
)) {
412 vDisplay
= static_cast<VarTree
*>(vDisplay
->getSibling());
413 vNew
= static_cast<VarTree
*>(vNew
->getSibling());
416 // update of children propagates only if this node is expanded
417 return display
->m_valueChanged
|| (display
->isExpanded() && childChanged
);
420 void ExprWnd::updateSingleExpr(VarTree
* display
, VarTree
* newValue
)
423 * If newValues is a VKdummy, we are only interested in its children.
424 * No need to update anything here.
426 if (newValue
->m_varKind
== VarTree::VKdummy
) {
431 * If this node is a struct and we know its type then don't update its
432 * value now. This is a node for which we know how to find a nested
433 * value. So register the node for an update.
435 if (display
->m_varKind
== VarTree::VKstruct
&&
436 display
->m_type
!= 0 &&
437 display
->m_type
!= TypeInfo::unknownType())
439 ASSERT(newValue
->m_varKind
== VarTree::VKstruct
);
440 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
441 m_updateStruct
.append(display
);
445 if (display
->updateValue(newValue
->m_value
)) {
446 int i
= itemRow(display
);
448 updateCell(i
, 1, true);
454 void ExprWnd::updateStructValue(VarTree
* display
)
456 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
458 if (display
->updateValue(display
->m_partialValue
)) {
459 int i
= itemRow(display
);
462 updateCell(i
, 1, true);
466 display
->m_partialValue
= "";
467 display
->m_exprIndex
= -1;
470 void ExprWnd::replaceChildren(VarTree
* display
, VarTree
* newValues
)
472 ASSERT(display
->childCount() == 0 || display
->m_varKind
!= VarTree::VKsimple
);
474 // delete all children of display
476 while ((c
= display
->getChild()) != 0) {
477 display
->removeChild(c
);
479 // insert copies of the newValues
480 for (c
= newValues
->getChild(); c
!= 0; c
= c
->getSibling()) {
481 VarTree
* v
= static_cast<VarTree
*>(c
);
482 VarTree
* vNew
= new VarTree(v
->getText(), v
->m_nameKind
);
483 vNew
->m_varKind
= v
->m_varKind
;
484 vNew
->m_value
= v
->m_value
;
485 vNew
->m_type
= v
->m_type
;
486 vNew
->setDelayedExpanding(vNew
->m_varKind
== VarTree::VKpointer
);
487 vNew
->setExpanded(v
->isExpanded());
488 display
->appendChild(vNew
);
490 replaceChildren(vNew
, v
);
494 void ExprWnd::collectUnknownTypes(VarTree
* var
)
497 * forEveryItem does not scan the root item itself. So we must do it
500 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
501 if (var
->m_type
== 0 &&
502 var
->m_varKind
== VarTree::VKstruct
&&
503 var
->m_nameKind
!= VarTree::NKtype
)
505 /* this struct node doesn't have a type yet: register it */
506 m_updateType
.append(var
);
509 // add pointer pixmap to pointers
510 if (var
->m_varKind
== VarTree::VKpointer
) {
511 var
->setPixmap(m_pixPointer
);
514 forEveryItem(collectUnknownTypes
, this, var
);
517 bool ExprWnd::collectUnknownTypes(KTreeViewItem
* item
, void* user
)
519 VarTree
* var
= static_cast<VarTree
*>(item
);
520 ExprWnd
* tree
= static_cast<ExprWnd
*>(user
);
521 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
522 if (var
->m_type
== 0 &&
523 var
->m_varKind
== VarTree::VKstruct
&&
524 var
->m_nameKind
!= VarTree::NKtype
)
526 /* this struct node doesn't have a type yet: register it */
527 tree
->m_updateType
.append(var
);
529 // add pointer pixmap to pointers
530 if (var
->m_varKind
== VarTree::VKpointer
) {
531 var
->setPixmap(tree
->m_pixPointer
);
537 VarTree
* ExprWnd::topLevelExprByName(const char* name
)
542 KTreeViewItem
* item
= itemAt(path
);
545 return static_cast<VarTree
*>(item
);
548 void ExprWnd::removeExpr(VarTree
* item
)
550 // must remove any pointers scheduled for update from the list
551 sweepList(m_updatePtrs
, item
);
552 sweepList(m_updateType
, item
);
553 sweepList(m_updateStruct
, item
);
561 void ExprWnd::sweepList(QList
<VarTree
>& list
, VarTree
* subTree
)
566 VarTree
* checkItem
= list
.first();
567 while (checkItem
!= 0) {
568 if (!subTree
->isAncestorEq(checkItem
)) {
569 // checkItem is not an item from subTree
571 checkItem
= list
.next();
573 // checkItem is an item from subTree
575 * If checkItem is the last item in the list, we need a special
576 * treatment, because remove()ing it steps the current item of
577 * the list in the "wrong" direction.
579 if (checkItem
== list
.getLast()) { // does not set current item
581 /* we deleted the last element, so we've finished */
585 /* remove() advanced already */
586 checkItem
= list
.current();
592 QString
ExprWnd::exprStringAt(int index
)
594 KTreeViewItem
* item
= itemAt(index
);
595 if (item
== 0) return QString(); /* paranoia */
596 VarTree
* expr
= static_cast<VarTree
*>(item
);
597 return expr
->computeExpr();
600 void ExprWnd::clearPendingUpdates()
602 m_updatePtrs
.clear();
603 m_updateType
.clear();
604 m_updateStruct
.clear();
607 VarTree
* ExprWnd::nextUpdatePtr()
609 VarTree
* ptr
= m_updatePtrs
.first();
611 m_updatePtrs
.remove();
616 VarTree
* ExprWnd::nextUpdateType()
618 VarTree
* ptr
= m_updateType
.first();
620 m_updateType
.remove();
625 VarTree
* ExprWnd::nextUpdateStruct()
627 VarTree
* ptr
= m_updateStruct
.first();
629 m_updateStruct
.remove();
634 void ExprWnd::paintCell(QPainter
* painter
, int row
, int col
)
637 KTreeView::paintCell(painter
, row
, col
);
639 VarTree
* item
= static_cast<VarTree
*>(itemAt(row
));
641 item
->paintValue(painter
);
646 int ExprWnd::cellWidth(int col
) const
649 return KTreeView::cellWidth(col
);
651 return maxValueWidth
;
655 void ExprWnd::updateValuesWidth()
658 forEveryVisibleItem(static_cast<KForEveryFunc
>(&getMaxValueWidth
), &maxW
);
659 maxValueWidth
= maxW
;
663 // called by updateValuesWidth() for each item in the visible list
664 bool ExprWnd::getMaxValueWidth(KTreeViewItem
* item
, void* user
)
666 int *maxW
= (int *)user
;
667 VarTree
* v
= static_cast<VarTree
*>(item
);
668 int w
= v
->valueWidth();
674 void ExprWnd::slotExpandOrCollapse(int)