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 */
19 VarTree::VarTree(const QString
& name
, NameKind aKind
) :
23 m_valueChanged(false),
26 m_exprIndexUseGuard(false)
34 void VarTree::paintValue(QPainter
* p
)
37 int cellHeight
= height(p
->fontMetrics());
39 int textY
= (cellHeight
- p
->fontMetrics().height()) / 2 +
40 p
->fontMetrics().ascent();
45 // p->setBackgroundColor(cg.base());
46 p
->drawText(textX
, textY
, m_value
, m_value
.length());
50 int VarTree::valueWidth()
53 return owner
->fontMetrics().width(m_value
) + 4;
56 QString
VarTree::computeExpr() const
58 assert(getParent() != 0);
63 // top-level items are special
64 if (getParent()->getParent() == 0)
68 VarTree
* par
= static_cast<VarTree
*>(getParent());
69 QString parentExpr
= par
->computeExpr();
71 /* don't add this item's name if this is a base class sub-item */
72 if (m_nameKind
== NKtype
) {
75 /* augment by this item's text */
77 /* if this is an address, dereference it */
78 if (m_nameKind
== NKaddress
) {
79 ASSERT(par
->m_varKind
== VKpointer
);
80 result
= "*" + parentExpr
;
83 switch (par
->m_varKind
) {
86 QString index
= getText();
88 // skip past the index
89 while (index
[i
].isDigit())
92 * Some array indices are actually ranges due to repeated array
93 * values. We use the first index in these cases.
95 if (index
[i
] != ']') {
96 // remove second index
97 index
.remove(i
, index
.length()-i
-1);
99 result
= "(" + parentExpr
+ ")" + index
;
103 result
= "(" + parentExpr
+ ")." + getText();
105 case VKsimple
: /* parent can't be simple */
106 case VKpointer
: /* handled in NKaddress */
107 case VKdummy
: /* can't occur at all */
109 result
= parentExpr
; /* paranoia */
115 bool VarTree::isToplevelExpr() const
117 return getParent() != 0 && getParent()->getParent() == 0;
120 bool VarTree::isAncestorEq(const VarTree
* child
) const
122 const KTreeViewItem
* c
= child
;
123 while (c
!= 0 && c
!= this) {
129 bool VarTree::updateValue(const QString
& newValue
)
131 // check whether the value changed
132 bool prevValueChanged
= m_valueChanged
;
133 m_valueChanged
= false;
134 if (m_value
!= newValue
) {
136 m_valueChanged
= true;
139 * We must repaint the cell if the value changed. If it did not change,
140 * we still must repaint the cell if the value changed previously,
141 * because the color of the display must be changed (from red to
144 return m_valueChanged
|| prevValueChanged
;
147 void VarTree::inferTypesOfChildren(ProgramTypeTable
& typeTable
)
150 * Type inference works like this: We use type information of those
151 * children that have a type name in their name (base classes) or in
152 * their value (pointers)
155 // first recurse children
156 VarTree
* child
= static_cast<VarTree
*>(getChild());
158 child
->inferTypesOfChildren(typeTable
);
159 child
= static_cast<VarTree
*>(child
->getSibling());
162 // if this is a pointer, get the type from the value (less the pointer)
163 if (m_varKind
== VKpointer
) {
164 #ifndef I_know_a_way_to_do_this_cleanly
167 const char* p
= m_value
.data();
168 const char* start
= p
;
169 // the type of the pointer shows up in the value (sometimes)
170 if (p
== 0 || *p
!= '(')
172 skipNested(p
, '(', ')');
174 * We only recognize pointers to data "(int *)" but not pointers
175 * to functions "(void (*)())".
177 if (p
-start
< 3 && /* at least 3 chars necessary: (*) */
178 p
[-2] != '*') /* skip back before the closing paren */
182 const QString
& typeName
=
183 FROM_LATIN1(start
+1, p
-start
-3) // minus 3 chars
185 m_type
= typeTable
.lookup(typeName
);
187 m_type
= TypeInfo::unknownType();
190 } else if (m_varKind
== VKstruct
) {
191 // check if this is a base class part
192 if (m_nameKind
== NKtype
) {
193 const QString
& typeName
=
194 text
.mid(1, text
.length()-2); // strip < and >
195 m_type
= typeTable
.lookup(typeName
);
197 /* if we don't have a type yet, get it from the base class */
199 m_type
= inferTypeFromBaseClass();
201 * If there is a known type now, it is the one from the
202 * first base class whose type we know.
207 * If we still don't have a type, the type is really unknown.
210 m_type
= TypeInfo::unknownType();
214 * This is not a base class part. We don't assign a type so
215 * that later we can ask gdb.
221 * Get the type of the first base class whose type we know.
223 TypeInfo
* VarTree::inferTypeFromBaseClass()
225 if (m_varKind
== VKstruct
) {
226 VarTree
* child
= static_cast<VarTree
*>(getChild());
228 // only check base class parts (i.e. type names)
229 child
->m_nameKind
== NKtype
)
231 if (child
->m_type
!= 0 &&
232 child
->m_type
!= TypeInfo::unknownType())
235 return child
->m_type
;
237 child
= static_cast<VarTree
*>(child
->getSibling());
244 ExprWnd::ExprWnd(QWidget
* parent
, const char* name
) :
245 KTreeView(parent
, name
),
251 connect(this, SIGNAL(expanded(int)), SLOT(slotExpandOrCollapse(int)));
252 connect(this, SIGNAL(collapsed(int)), SLOT(slotExpandOrCollapse(int)));
254 m_pixPointer
= UserIcon("pointer.xpm");
255 if (m_pixPointer
.isNull())
256 TRACE("Can't load pointer.xpm");
263 void ExprWnd::exprList(QStrList
& exprs
)
265 // ASSERT(exprs does deep-copies)
267 for (item
= itemAt(0); item
!= 0; item
= item
->getSibling()) {
268 exprs
.append(item
->getText());
272 void ExprWnd::insertExpr(VarTree
* expr
)
274 // append the expression
277 collectUnknownTypes(expr
);
282 void ExprWnd::updateExpr(VarTree
* expr
)
284 // search the root variable
285 QString p
= expr
->getText();
288 KTreeViewItem
* item
= itemAt(path
);
294 if (updateExprRec(static_cast<VarTree
*>(item
), expr
)) {
295 updateVisibleItems();
299 collectUnknownTypes(static_cast<VarTree
*>(item
));
302 void ExprWnd::updateExpr(VarTree
* display
, VarTree
* newValues
)
304 if (updateExprRec(display
, newValues
) && display
->isVisible()) {
305 updateVisibleItems();
309 collectUnknownTypes(display
);
313 * returns true if there's a visible change
315 bool ExprWnd::updateExprRec(VarTree
* display
, VarTree
* newValues
)
317 bool isExpanded
= display
->isExpanded();
320 * If we are updating a pointer without children by a dummy, we don't
321 * collapse it, but simply insert the new children. This happens when a
322 * pointer has just been expanded by the user.
324 if (display
->m_varKind
== VarTree::VKpointer
&&
325 display
->childCount() == 0 &&
326 newValues
->m_varKind
== VarTree::VKdummy
)
328 replaceChildren(display
, newValues
);
329 return isExpanded
; /* no visible change if not expanded */
333 * If the display and newValues have different kind or if their number
334 * of children is different, replace the whole sub-tree.
336 if (// the next two lines mean: not(m_varKind remains unchanged)
337 !(newValues
->m_varKind
== VarTree::VKdummy
||
338 display
->m_varKind
== newValues
->m_varKind
)
340 (display
->childCount() != newValues
->childCount() &&
342 * If this is a pointer and newValues doesn't have children, we
343 * don't replace the sub-tree; instead, below we mark this
344 * sub-tree for requiring an update.
346 (display
->m_varKind
!= VarTree::VKpointer
||
347 newValues
->childCount() != 0)))
350 collapseSubTree(display
, false);
353 // since children changed, it is likely that the type has also changed
354 display
->m_type
= 0; /* will re-evaluate the type */
356 // display the new value
357 updateSingleExpr(display
, newValues
);
358 replaceChildren(display
, newValues
);
360 // update the m_varKind
361 if (newValues
->m_varKind
!= VarTree::VKdummy
) {
362 display
->m_varKind
= newValues
->m_varKind
;
363 display
->setDelayedExpanding(newValues
->m_varKind
== VarTree::VKpointer
);
366 // (note that the new value might not have a sub-tree at all)
367 return display
->m_valueChanged
|| isExpanded
; /* no visible change if not expanded */
370 // display the new value
371 updateSingleExpr(display
, newValues
);
374 * If this is an expanded pointer, record it for being updated.
376 if (display
->m_varKind
== VarTree::VKpointer
) {
378 // if newValues is a dummy, we have already updated this pointer
379 newValues
->m_varKind
!= VarTree::VKdummy
)
381 m_updatePtrs
.append(display
);
384 * If the visible sub-tree has children, but newValues doesn't, we
387 if (newValues
->childCount() == 0) {
388 return display
->m_valueChanged
;
392 ASSERT(display
->childCount() == newValues
->childCount());
395 bool childChanged
= false;
397 VarTree
* vDisplay
= static_cast<VarTree
*>(display
->getChild());
398 VarTree
* vNew
= static_cast<VarTree
*>(newValues
->getChild());
399 while (vDisplay
!= 0) {
400 // check whether the names are the same
401 if (strcmp(vDisplay
->getText(), vNew
->getText()) != 0) {
403 vDisplay
->setText(vNew
->getText());
404 int i
= itemRow(vDisplay
);
406 updateCell(i
, 0, true);
411 if (updateExprRec(vDisplay
, vNew
)) {
414 vDisplay
= static_cast<VarTree
*>(vDisplay
->getSibling());
415 vNew
= static_cast<VarTree
*>(vNew
->getSibling());
418 // update of children propagates only if this node is expanded
419 return display
->m_valueChanged
|| (display
->isExpanded() && childChanged
);
422 void ExprWnd::updateSingleExpr(VarTree
* display
, VarTree
* newValue
)
425 * If newValues is a VKdummy, we are only interested in its children.
426 * No need to update anything here.
428 if (newValue
->m_varKind
== VarTree::VKdummy
) {
433 * If this node is a struct and we know its type then don't update its
434 * value now. This is a node for which we know how to find a nested
435 * value. So register the node for an update.
437 if (display
->m_varKind
== VarTree::VKstruct
&&
438 display
->m_type
!= 0 &&
439 display
->m_type
!= TypeInfo::unknownType())
441 ASSERT(newValue
->m_varKind
== VarTree::VKstruct
);
442 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
443 m_updateStruct
.append(display
);
447 if (display
->updateValue(newValue
->m_value
)) {
448 int i
= itemRow(display
);
450 updateCell(i
, 1, true);
456 void ExprWnd::updateStructValue(VarTree
* display
)
458 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
460 if (display
->updateValue(display
->m_partialValue
)) {
461 int i
= itemRow(display
);
464 updateCell(i
, 1, true);
468 display
->m_partialValue
= "";
469 display
->m_exprIndex
= -1;
472 void ExprWnd::replaceChildren(VarTree
* display
, VarTree
* newValues
)
474 ASSERT(display
->childCount() == 0 || display
->m_varKind
!= VarTree::VKsimple
);
476 // delete all children of display
478 while ((c
= display
->getChild()) != 0) {
479 display
->removeChild(c
);
481 // insert copies of the newValues
482 for (c
= newValues
->getChild(); c
!= 0; c
= c
->getSibling()) {
483 VarTree
* v
= static_cast<VarTree
*>(c
);
484 VarTree
* vNew
= new VarTree(v
->getText(), v
->m_nameKind
);
485 vNew
->m_varKind
= v
->m_varKind
;
486 vNew
->m_value
= v
->m_value
;
487 vNew
->m_type
= v
->m_type
;
488 vNew
->setDelayedExpanding(vNew
->m_varKind
== VarTree::VKpointer
);
489 vNew
->setExpanded(v
->isExpanded());
490 display
->appendChild(vNew
);
492 replaceChildren(vNew
, v
);
496 void ExprWnd::collectUnknownTypes(VarTree
* var
)
499 * forEveryItem does not scan the root item itself. So we must do it
502 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
503 if (var
->m_type
== 0 &&
504 var
->m_varKind
== VarTree::VKstruct
&&
505 var
->m_nameKind
!= VarTree::NKtype
)
507 /* this struct node doesn't have a type yet: register it */
508 m_updateType
.append(var
);
511 // add pointer pixmap to pointers
512 if (var
->m_varKind
== VarTree::VKpointer
) {
513 var
->setPixmap(m_pixPointer
);
516 forEveryItem(collectUnknownTypes
, this, var
);
519 bool ExprWnd::collectUnknownTypes(KTreeViewItem
* item
, void* user
)
521 VarTree
* var
= static_cast<VarTree
*>(item
);
522 ExprWnd
* tree
= static_cast<ExprWnd
*>(user
);
523 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
524 if (var
->m_type
== 0 &&
525 var
->m_varKind
== VarTree::VKstruct
&&
526 var
->m_nameKind
!= VarTree::NKtype
)
528 /* this struct node doesn't have a type yet: register it */
529 tree
->m_updateType
.append(var
);
531 // add pointer pixmap to pointers
532 if (var
->m_varKind
== VarTree::VKpointer
) {
533 var
->setPixmap(tree
->m_pixPointer
);
539 VarTree
* ExprWnd::topLevelExprByName(const char* name
)
544 KTreeViewItem
* item
= itemAt(path
);
547 return static_cast<VarTree
*>(item
);
550 void ExprWnd::removeExpr(VarTree
* item
)
552 // must remove any pointers scheduled for update from the list
553 sweepList(m_updatePtrs
, item
);
554 sweepList(m_updateType
, item
);
555 sweepList(m_updateStruct
, item
);
563 void ExprWnd::sweepList(QList
<VarTree
>& list
, VarTree
* subTree
)
568 VarTree
* checkItem
= list
.first();
569 while (checkItem
!= 0) {
570 if (!subTree
->isAncestorEq(checkItem
)) {
571 // checkItem is not an item from subTree
573 checkItem
= list
.next();
575 // checkItem is an item from subTree
577 * If checkItem is the last item in the list, we need a special
578 * treatment, because remove()ing it steps the current item of
579 * the list in the "wrong" direction.
581 if (checkItem
== list
.getLast()) { // does not set current item
583 /* we deleted the last element, so we've finished */
587 /* remove() advanced already */
588 checkItem
= list
.current();
594 QString
ExprWnd::exprStringAt(int index
)
596 KTreeViewItem
* item
= itemAt(index
);
597 if (item
== 0) return QString(); /* paranoia */
598 VarTree
* expr
= static_cast<VarTree
*>(item
);
599 return expr
->computeExpr();
602 void ExprWnd::clearPendingUpdates()
604 m_updatePtrs
.clear();
605 m_updateType
.clear();
606 m_updateStruct
.clear();
609 VarTree
* ExprWnd::nextUpdatePtr()
611 VarTree
* ptr
= m_updatePtrs
.first();
613 m_updatePtrs
.remove();
618 VarTree
* ExprWnd::nextUpdateType()
620 VarTree
* ptr
= m_updateType
.first();
622 m_updateType
.remove();
627 VarTree
* ExprWnd::nextUpdateStruct()
629 VarTree
* ptr
= m_updateStruct
.first();
631 m_updateStruct
.remove();
636 void ExprWnd::paintCell(QPainter
* painter
, int row
, int col
)
639 KTreeView::paintCell(painter
, row
, col
);
641 VarTree
* item
= static_cast<VarTree
*>(itemAt(row
));
643 item
->paintValue(painter
);
648 int ExprWnd::cellWidth(int col
) const
651 return KTreeView::cellWidth(col
);
653 return maxValueWidth
;
657 void ExprWnd::updateValuesWidth()
660 forEveryVisibleItem(static_cast<KForEveryFunc
>(&getMaxValueWidth
), &maxW
);
661 maxValueWidth
= maxW
;
665 // called by updateValuesWidth() for each item in the visible list
666 bool ExprWnd::getMaxValueWidth(KTreeViewItem
* item
, void* user
)
668 int *maxW
= (int *)user
;
669 VarTree
* v
= static_cast<VarTree
*>(item
);
670 int w
= v
->valueWidth();
676 void ExprWnd::slotExpandOrCollapse(int)
681 void ExprWnd::editValue(int row
, const QString
& text
)
687 int w
= cellWidth(1);
688 int h
= cellHeight(row
);
689 QScrollBar
* sbV
= static_cast<QScrollBar
*>(child("table_sbV"));
692 * Make the edit widget at least 5 characters wide (but not wider than
693 * this widget). If less than half of this widget is used to display
694 * the text, scroll this widget so that half of it shows the text (or
695 * less than half of it if the text is shorter).
697 QFontMetrics metr
= m_edit
.font();
698 int wMin
= metr
.width("88888");
702 if (sbV
->isVisible()) // subtract width of scrollbar
703 wThis
-= sbV
->width();
704 if (x
>= wThis
/2 && // less than half the width displays text
705 x
+w
> wThis
) // not all text is visible
707 // scroll so that more text is visible
708 int wScroll
= QMIN(x
-wThis
/2, x
+w
-wThis
);
709 sbHor(xOffset()+wScroll
);
714 // don't let the edit move out at the left
718 // make the edit box as wide as the visible column
719 QRect
rect(x
,y
, wThis
-x
,h
);
720 m_edit
.setText(text
);
723 m_edit
.setGeometry(rect
);
724 m_edit
.m_finished
= false;
730 bool ExprWnd::isEditing() const
732 return m_edit
.isVisible();
736 ValueEdit::ValueEdit(ExprWnd
* parent
) :
737 QLineEdit(parent
, "valueedit")
741 lower(); // lower the window below scrollbars
742 connect(parent
, SIGNAL(selected(int)), SLOT(slotSelectionChanged()));
743 connect(parent
, SIGNAL(collapsed(int)), SLOT(slotSelectionChanged()));
744 connect(parent
, SIGNAL(expanded(int)), SLOT(slotSelectionChanged()));
745 connect(this, SIGNAL(done(int, const QString
&)),
746 parent
, SIGNAL(editValueCommitted(int, const QString
&)));
749 ValueEdit::~ValueEdit()
753 void ValueEdit::terminate(bool commit
)
755 TRACE(commit
?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
759 hide(); // will call focusOutEvent, that's why we need m_finished
761 emit
done(m_row
, text());
766 void ValueEdit::keyPressEvent(QKeyEvent
*e
)
768 if(e
->key() == Qt::Key_Return
|| e
->key() == Qt::Key_Enter
)
770 else if(e
->key() == Qt::Key_Escape
)
773 QLineEdit::keyPressEvent(e
);
776 void ValueEdit::paintEvent(QPaintEvent
* e
)
778 QLineEdit::paintEvent(e
);
784 void ValueEdit::focusOutEvent(QFocusEvent
* ev
)
786 TRACE("ValueEdit::focusOutEvent");
787 QFocusEvent
* focusEv
= static_cast<QFocusEvent
*>(ev
);
788 // Don't let a RMB close the editor
789 if (focusEv
->reason() != QFocusEvent::Popup
&&
790 focusEv
->reason() != QFocusEvent::ActiveWindow
)
796 void ValueEdit::slotSelectionChanged()
798 TRACE("ValueEdit::slotSelectionChanged");