2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
10 #include <QHeaderView>
11 #include <QStringList>
13 #include <QPaintEvent>
14 #include <QFocusEvent>
17 #include <kiconloader.h> /* icons */
18 #include <klocale.h> /* i18n */
21 VarTree::VarTree(VarTree
* parent
, ExprValue
* v
) :
22 QTreeWidgetItem(parent
),
23 m_varKind(v
->m_varKind
),
24 m_nameKind(v
->m_nameKind
),
27 m_exprIndexUseGuard(false),
28 m_baseValue(v
->m_value
),
30 m_structChanged(false)
34 if (v
->m_initiallyExpanded
|| m_varKind
== VarTree::VKpointer
)
35 setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator
);
37 setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
38 setExpanded(v
->m_initiallyExpanded
);
41 VarTree::VarTree(ExprWnd
* parent
, const QString
& name
) :
42 QTreeWidgetItem(parent
),
44 m_nameKind(VarTree::NKplain
),
47 m_exprIndexUseGuard(false),
49 m_structChanged(false)
58 QString
VarTree::computeExpr() const
60 // top-level items are special
65 VarTree
* par
= static_cast<VarTree
*>(parent());
66 QString parentExpr
= par
->computeExpr();
68 // skip this item's name if it is a base class or anonymous struct or union
69 if (m_nameKind
== NKtype
|| m_nameKind
== NKanonymous
) {
72 /* augment by this item's text */
74 /* if this is an address, dereference it */
75 if (m_nameKind
== NKaddress
) {
76 ASSERT(par
->m_varKind
== VKpointer
);
77 result
= "*" + parentExpr
;
80 switch (par
->m_varKind
) {
83 QString index
= getText();
85 // skip past the index
86 while (index
[i
].isDigit())
89 * Some array indices are actually ranges due to repeated array
90 * values. We use the first index in these cases.
92 if (index
[i
] != ']') {
93 // remove second index
94 index
.remove(i
, index
.length()-i
-1);
96 result
= "(" + parentExpr
+ ")" + index
;
100 result
= "(" + parentExpr
+ ")." + getText();
102 case VKsimple
: /* parent can't be simple */
103 case VKpointer
: /* handled in NKaddress */
104 case VKdummy
: /* can't occur at all */
106 result
= parentExpr
; /* paranoia */
112 bool VarTree::isToplevelExpr() const
114 return parent() == 0;
117 bool VarTree::isAncestorEq(const VarTree
* child
) const
119 const QTreeWidgetItem
* c
= child
;
120 while (c
!= 0 && c
!= this) {
126 bool VarTree::updateValue(const QString
& newValue
)
128 // check whether the value changed
129 bool prevValueChanged
= m_baseChanged
;
130 if ((m_baseChanged
= m_baseValue
!= newValue
)) {
131 m_baseValue
= newValue
;
133 setForeground(1, QBrush(QColor(Qt::red
)));
134 } else if (prevValueChanged
) {
135 setForeground(1, treeWidget()->palette().text());
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_baseChanged
|| prevValueChanged
;
146 bool VarTree::updateStructValue(const QString
& newValue
)
148 // check whether the value changed
149 bool prevValueChanged
= m_structChanged
;
150 if ((m_structChanged
= m_structValue
!= newValue
)) {
151 m_structValue
= newValue
;
153 setForeground(1, QBrush(QColor(Qt::red
)));
154 } else if (prevValueChanged
) {
155 setForeground(1, treeWidget()->palette().text());
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 setText(1, m_structValue
);
170 } else if (m_structValue
.isEmpty()) {
171 setText(1, m_baseValue
);
173 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 for (int i
= 0; i
< childCount(); i
++)
188 child(i
)->inferTypesOfChildren(typeTable
);
191 // if this is a pointer, get the type from the value (less the pointer)
192 if (m_varKind
== VKpointer
) {
196 * wchart_t pointers must be treated as struct, because the array
197 * of characters is printed similar to how QStrings are decoded.
199 m_varKind
= VKstruct
;
200 setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
202 // don't know how to do this cleanly
203 } else if (m_varKind
== VKstruct
) {
204 // check if this is a base class part
205 if (m_nameKind
== NKtype
) {
206 const QString
& typeName
=
207 getText().mid(1, getText().length()-2); // strip < and >
208 m_type
= typeTable
.lookup(typeName
);
210 /* if we don't have a type yet, get it from the base class */
212 m_type
= inferTypeFromBaseClass();
214 * If there is a known type now, it is the one from the
215 * first base class whose type we know.
220 * If we still don't have a type, the type is really unknown.
223 m_type
= TypeInfo::unknownType();
227 * This is not a base class part. We don't assign a type so
228 * that later we can ask gdb.
233 // the value contains the pointer type in parenthesis
234 bool VarTree::isWcharT() const
236 return value().startsWith("(const wchar_t *)") ||
237 value().startsWith("(wchar_t *)");
241 * Get the type of the first base class whose type we know.
243 const TypeInfo
* VarTree::inferTypeFromBaseClass()
245 if (m_varKind
== VKstruct
) {
246 for (int i
= 0; i
< childCount(); i
++)
248 VarTree
* child
= VarTree::child(i
);
249 // only check base class parts (i.e. type names)
250 if (child
->m_nameKind
!= NKtype
)
252 if (child
->m_type
!= 0 &&
253 child
->m_type
!= TypeInfo::unknownType())
256 return child
->m_type
;
263 ExprValue::ExprValue(const QString
& name
, VarTree::NameKind aKind
) :
265 m_varKind(VarTree::VKsimple
),
269 m_initiallyExpanded(false)
273 ExprValue::~ExprValue()
279 void ExprValue::appendChild(ExprValue
* newChild
)
284 // walk chain of children to find the last one
285 ExprValue
* last
= m_child
;
286 while (last
->m_next
!= 0)
288 last
->m_next
= newChild
;
290 newChild
->m_next
= 0; // just to be sure
293 int ExprValue::childCount() const
296 ExprValue
* c
= m_child
;
306 ExprWnd::ExprWnd(QWidget
* parent
, const QString
& colHeader
) :
310 QTreeWidgetItem
* pHeaderItem
= new QTreeWidgetItem();
311 pHeaderItem
->setText(0, colHeader
);
312 pHeaderItem
->setText(1, i18n("Value"));
313 setHeaderItem(pHeaderItem
);
314 header()->setResizeMode(0, QHeaderView::Interactive
);
315 header()->setResizeMode(1, QHeaderView::Interactive
);
317 setSortingEnabled(false); // do not sort items
318 setRootIsDecorated(true);
319 setAllColumnsShowFocus(true);
321 m_pixPointer
= UserIcon("pointer.xpm");
322 if (m_pixPointer
.isNull())
323 TRACE("Can't load pointer.xpm");
330 QStringList
ExprWnd::exprList() const
333 for (int i
= 0; i
< topLevelItemCount(); i
++)
335 exprs
.append(topLevelItem(i
)->getText());
340 VarTree
* ExprWnd::insertExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
342 // append a new dummy expression
343 VarTree
* display
= new VarTree(this, 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
354 for (int i
= 0; i
< topLevelItemCount(); i
++)
356 if (topLevelItem(i
)->getText() == expr
->m_name
) {
357 item
= topLevelItem(i
);
365 updateExprRec(item
, expr
, typeTable
);
366 collectUnknownTypes(item
);
369 void ExprWnd::updateExpr(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
371 updateExprRec(display
, newValues
, typeTable
);
372 collectUnknownTypes(display
);
376 * returns true if there's a visible change
378 void ExprWnd::updateExprRec(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
380 bool isExpanded
= display
->isExpanded();
383 * If we are updating a pointer without children by a dummy, we don't
384 * collapse it, but simply insert the new children. This happens when a
385 * pointer has just been expanded by the user.
387 if (display
->m_varKind
== VarTree::VKpointer
&&
388 display
->childCount() == 0 &&
389 newValues
->m_varKind
== VarTree::VKdummy
)
391 replaceChildren(display
, newValues
);
396 * If the display and newValues have different kind or if their number
397 * of children is different, replace the whole sub-tree.
399 if (// the next two lines mean: not(m_varKind remains unchanged)
400 !(newValues
->m_varKind
== VarTree::VKdummy
||
401 display
->m_varKind
== newValues
->m_varKind
)
403 (display
->childCount() != newValues
->childCount() &&
405 * If this is a pointer and newValues doesn't have children, we
406 * don't replace the sub-tree; instead, below we mark this
407 * sub-tree for requiring an update.
409 (display
->m_varKind
!= VarTree::VKpointer
||
410 newValues
->m_child
!= 0)))
413 display
->setExpanded(false);
416 // since children changed, it is likely that the type has also changed
417 display
->m_type
= 0; /* will re-evaluate the type */
419 // display the new value
420 updateSingleExpr(display
, newValues
);
421 replaceChildren(display
, newValues
);
423 // update the m_varKind
424 if (newValues
->m_varKind
!= VarTree::VKdummy
) {
425 display
->m_varKind
= newValues
->m_varKind
;
426 switch (display
->m_varKind
) {
427 case VarTree::VKpointer
:
428 case VarTree::VKstruct
:
429 display
->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator
);
432 display
->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
437 // get some types (after the new m_varKind has been set!)
438 display
->inferTypesOfChildren(typeTable
);
440 // (note that the new value might not have a sub-tree at all)
444 // display the new value
445 updateSingleExpr(display
, newValues
);
448 * If this is an expanded pointer, record it for being updated.
450 if (display
->m_varKind
== VarTree::VKpointer
) {
452 // if newValues is a dummy, we have already updated this pointer
453 newValues
->m_varKind
!= VarTree::VKdummy
)
455 m_updatePtrs
.push_back(display
);
458 * If the visible sub-tree has children, but newValues doesn't, we
461 if (newValues
->m_child
== 0) {
466 ASSERT(display
->childCount() == newValues
->childCount());
469 ExprValue
* vNew
= newValues
->m_child
;
470 for (int i
= 0; i
< display
->childCount(); i
++)
472 VarTree
* vDisplay
= display
->child(i
);
473 // check whether the names are the same
474 if (vDisplay
->getText() != vNew
->m_name
) {
476 vDisplay
->setText(vNew
->m_name
);
479 updateExprRec(vDisplay
, vNew
, typeTable
);
485 void ExprWnd::updateSingleExpr(VarTree
* display
, ExprValue
* newValue
)
488 * If newValues is a VKdummy, we are only interested in its children.
489 * No need to update anything here.
491 if (newValue
->m_varKind
== VarTree::VKdummy
) {
496 * If this node is a struct and we know its type then we know how to
497 * find a nested value. So register the node for an update.
499 * wchar_t types are also treated specially here: We consider them
500 * as struct (has been set in inferTypesOfChildren()).
502 if (display
->m_varKind
== VarTree::VKstruct
&&
503 display
->m_type
!= 0 &&
504 display
->m_type
!= TypeInfo::unknownType())
506 ASSERT(newValue
->m_varKind
== VarTree::VKstruct
);
507 if (display
->m_type
== TypeInfo::wchartType())
509 display
->m_partialValue
= "L";
512 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
513 m_updateStruct
.push_back(display
);
516 display
->updateValue(newValue
->m_value
);
519 void ExprWnd::updateStructValue(VarTree
* display
)
521 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
523 display
->updateStructValue(display
->m_partialValue
);
525 display
->m_partialValue
= "";
526 display
->m_exprIndex
= -1;
529 void ExprWnd::replaceChildren(VarTree
* display
, ExprValue
* newValues
)
531 ASSERT(display
->childCount() == 0 || display
->m_varKind
!= VarTree::VKsimple
);
533 // delete all children of display
534 while (VarTree
* c
= display
->child(0)) {
538 // insert copies of the newValues
539 for (ExprValue
* v
= newValues
->m_child
; v
!= 0; v
= v
->m_next
)
541 VarTree
* vNew
= new VarTree(display
, v
);
543 replaceChildren(vNew
, v
);
547 void ExprWnd::collectUnknownTypes(VarTree
* var
)
549 QTreeWidgetItemIterator
i(var
);
552 checkUnknownType(static_cast<VarTree
*>(*i
));
556 void ExprWnd::checkUnknownType(VarTree
* var
)
558 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
559 if (var
->m_type
== 0 &&
560 var
->m_varKind
== VarTree::VKstruct
&&
561 var
->m_nameKind
!= VarTree::NKtype
&&
562 var
->m_nameKind
!= VarTree::NKanonymous
)
564 if (!var
->isWcharT())
566 /* this struct node doesn't have a type yet: register it */
567 m_updateType
.push_back(var
);
571 var
->m_type
= TypeInfo::wchartType();
572 var
->m_partialValue
= "L";
573 m_updateStruct
.push_back(var
);
576 // add pointer pixmap to pointers
577 if (var
->m_varKind
== VarTree::VKpointer
) {
578 var
->setPixmap(m_pixPointer
);
582 QString
ExprWnd::formatWCharPointer(QString value
)
584 int pos
= value
.find(") ");
586 value
= value
.mid(pos
+2);
591 VarTree
* ExprWnd::topLevelExprByName(const QString
& name
) const
593 for (int i
= 0; i
< topLevelItemCount(); i
++)
595 if (topLevelItem(i
)->getText() == name
)
596 return topLevelItem(i
);
601 VarTree
* ExprWnd::ptrMemberByName(VarTree
* v
, const QString
& name
)
603 // v must be a pointer variable, must have children
604 if (v
->m_varKind
!= VarTree::VKpointer
|| v
->childCount() == 0)
607 // the only child of v is the pointer value that represents the struct
608 VarTree
* item
= v
->child(0);
609 return memberByName(item
, name
);
612 VarTree
* ExprWnd::memberByName(VarTree
* v
, const QString
& name
)
614 // search immediate children for name
615 for (int i
= 0; i
< v
->childCount(); i
++)
617 if (v
->child(i
)->getText() == name
)
621 // try in base classes and members that are anonymous structs or unions
622 for (int i
= 0; i
< v
->childCount(); i
++)
624 VarTree
* item
= v
->child(i
);
625 if (item
->m_nameKind
== VarTree::NKtype
||
626 item
->m_nameKind
== VarTree::NKanonymous
)
628 item
= memberByName(item
, name
);
636 void ExprWnd::removeExpr(VarTree
* item
)
643 void ExprWnd::unhookSubtree(VarTree
* subTree
)
645 // must remove any pointers scheduled for update from the list
646 unhookSubtree(m_updatePtrs
, subTree
);
647 unhookSubtree(m_updateType
, subTree
);
648 unhookSubtree(m_updateStruct
, subTree
);
649 emit
removingItem(subTree
);
652 void ExprWnd::unhookSubtree(std::list
<VarTree
*>& list
, VarTree
* subTree
)
657 std::list
<VarTree
*>::iterator i
= list
.begin();
658 while (i
!= list
.end()) {
659 std::list
<VarTree
*>::iterator checkItem
= i
;
661 if (subTree
->isAncestorEq(*checkItem
)) {
662 // checkItem is an item from subTree
663 list
.erase(checkItem
);
668 void ExprWnd::clearPendingUpdates()
670 m_updatePtrs
.clear();
671 m_updateType
.clear();
672 m_updateStruct
.clear();
675 VarTree
* ExprWnd::nextUpdatePtr()
678 if (!m_updatePtrs
.empty()) {
679 ptr
= m_updatePtrs
.front();
680 m_updatePtrs
.pop_front();
685 VarTree
* ExprWnd::nextUpdateType()
688 if (!m_updateType
.empty()) {
689 ptr
= m_updateType
.front();
690 m_updateType
.pop_front();
695 VarTree
* ExprWnd::nextUpdateStruct()
698 if (!m_updateStruct
.empty()) {
699 ptr
= m_updateStruct
.front();
700 m_updateStruct
.pop_front();
706 void ExprWnd::editValue(VarTree
* item
, const QString
& text
)
709 m_edit
= new ValueEdit(this);
711 QRect r
= visualItemRect(item
);
712 int x
= columnViewportPosition(1);
714 int w
= columnWidth(1);
718 * Make the edit widget at least 5 characters wide (but not wider than
719 * this widget). If less than half of this widget is used to display
720 * the text, scroll this widget so that half of it shows the text (or
721 * less than half of it if the text is shorter).
723 QFontMetrics metr
= m_edit
->font();
724 int wMin
= metr
.width("88888");
727 int wThis
= viewport()->width();
728 if (x
>= wThis
/2 && // less than half the width displays text
729 x
+w
> wThis
) // not all text is visible
731 // scroll so that more text is visible
732 QScrollBar
* pScrollBar
= horizontalScrollBar();
733 int wScroll
= QMIN(x
-wThis
/2, x
+w
-wThis
);
734 pScrollBar
->setValue(pScrollBar
->value() + wScroll
);
739 // don't let the edit move out at the left
743 // make the edit box as wide as the visible column
744 QRect
rect(x
,y
, wThis
-x
,h
);
745 m_edit
->setText(text
);
748 m_edit
->setGeometry(rect
);
749 m_edit
->m_finished
= false;
750 m_edit
->m_item
= item
;
755 bool ExprWnd::isEditing() const
757 return m_edit
!= 0 && m_edit
->isVisible();
761 ValueEdit::ValueEdit(ExprWnd
* parent
) :
762 QLineEdit(parent
->viewport(), "valueedit")
766 lower(); // lower the window below scrollbars
767 connect(parent
, SIGNAL(itemActivated(QTreeWidgetItem
*,int)),
768 SLOT(slotSelectionChanged()));
769 connect(parent
, SIGNAL(currentItemChanged(QTreeWidgetItem
*,QTreeWidgetItem
*)),
770 SLOT(slotSelectionChanged()));
771 connect(parent
, SIGNAL(itemExpanded(QTreeWidgetItem
*)),
772 SLOT(slotSelectionChanged()));
773 connect(parent
, SIGNAL(itemCollapsed(QTreeWidgetItem
*)),
774 SLOT(slotSelectionChanged()));
775 connect(this, SIGNAL(done(VarTree
*, const QString
&)),
776 parent
, SIGNAL(editValueCommitted(VarTree
*, const QString
&)));
779 ValueEdit::~ValueEdit()
783 void ValueEdit::terminate(bool commit
)
785 TRACE(commit
?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
789 hide(); // will call focusOutEvent, that's why we need m_finished
791 emit
done(m_item
, text());
796 void ValueEdit::keyPressEvent(QKeyEvent
*e
)
798 if(e
->key() == Qt::Key_Return
|| e
->key() == Qt::Key_Enter
)
800 else if(e
->key() == Qt::Key_Escape
)
803 QLineEdit::keyPressEvent(e
);
806 void ValueEdit::paintEvent(QPaintEvent
* e
)
808 QLineEdit::paintEvent(e
);
814 void ValueEdit::focusOutEvent(QFocusEvent
* ev
)
816 TRACE("ValueEdit::focusOutEvent");
817 QFocusEvent
* focusEv
= static_cast<QFocusEvent
*>(ev
);
818 if (focusEv
->reason() == Qt::ActiveWindowFocusReason
)
820 // Switching to a different window should terminate the edit,
821 // because if the window with this variable display is floating
822 // then that different window could be the main window, where
823 // the user had clicked one of the Execute buttons. This in turn
824 // may pull the item away that we are editing here.
827 // Don't let a RMB close the editor
828 else if (focusEv
->reason() != Qt::PopupFocusReason
)
834 void ValueEdit::slotSelectionChanged()
836 TRACE("ValueEdit::slotSelectionChanged");