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_child
!= 0 || m_varKind
== VarTree::VKpointer
)
35 setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator
);
37 setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
38 setExpanded(v
->m_initiallyExpanded
);
41 VarTree::VarTree(ExprWnd
* parent
, ExprValue
* v
) :
42 QTreeWidgetItem(parent
),
44 m_nameKind(VarTree::NKplain
),
47 m_exprIndexUseGuard(false),
48 m_baseValue(v
->m_value
),
50 m_structChanged(false)
60 QString
VarTree::computeExpr() const
62 // top-level items are special
67 VarTree
* par
= static_cast<VarTree
*>(parent());
68 QString parentExpr
= par
->computeExpr();
70 // skip this item's name if it is a base class or anonymous struct or union
71 if (m_nameKind
== NKtype
|| m_nameKind
== NKanonymous
) {
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 parent() == 0;
119 bool VarTree::isAncestorEq(const VarTree
* child
) const
121 const QTreeWidgetItem
* 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_baseChanged
;
132 if ((m_baseChanged
= m_baseValue
!= newValue
)) {
133 m_baseValue
= newValue
;
135 setForeground(1, QBrush(QColor(Qt::red
)));
136 } else if (prevValueChanged
) {
137 setForeground(1, treeWidget()->palette().text());
140 * We must repaint the cell if the value changed. If it did not change,
141 * we still must repaint the cell if the value changed previously,
142 * because the color of the display must be changed (from red to
145 return m_baseChanged
|| prevValueChanged
;
148 bool VarTree::updateStructValue(const QString
& newValue
)
150 // check whether the value changed
151 bool prevValueChanged
= m_structChanged
;
152 if ((m_structChanged
= m_structValue
!= newValue
)) {
153 m_structValue
= newValue
;
155 setForeground(1, QBrush(QColor(Qt::red
)));
156 } else if (prevValueChanged
) {
157 setForeground(1, treeWidget()->palette().text());
160 * We must repaint the cell if the value changed. If it did not change,
161 * we still must repaint the cell if the value changed previously,
162 * because the color of the display must be changed (from red to
165 return m_structChanged
|| prevValueChanged
;
168 void VarTree::updateValueText()
170 if (m_baseValue
.isEmpty()) {
171 setText(1, m_structValue
);
172 } else if (m_structValue
.isEmpty()) {
173 setText(1, m_baseValue
);
175 setText(1, m_baseValue
+ " " + m_structValue
);
179 void VarTree::inferTypesOfChildren(ProgramTypeTable
& typeTable
)
182 * Type inference works like this: We use type information of those
183 * children that have a type name in their name (base classes) or in
184 * their value (pointers)
187 // first recurse children
188 for (int i
= 0; i
< childCount(); i
++)
190 child(i
)->inferTypesOfChildren(typeTable
);
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 setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
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 const TypeInfo
* VarTree::inferTypeFromBaseClass()
247 if (m_varKind
== VKstruct
) {
248 for (int i
= 0; i
< childCount(); i
++)
250 VarTree
* child
= VarTree::child(i
);
251 // only check base class parts (i.e. type names)
252 if (child
->m_nameKind
!= NKtype
)
254 if (child
->m_type
!= 0 &&
255 child
->m_type
!= TypeInfo::unknownType())
258 return child
->m_type
;
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
) :
312 QTreeWidgetItem
* pHeaderItem
= new QTreeWidgetItem();
313 pHeaderItem
->setText(0, colHeader
);
314 pHeaderItem
->setText(1, i18n("Value"));
315 setHeaderItem(pHeaderItem
);
316 header()->setResizeMode(0, QHeaderView::Interactive
);
317 header()->setResizeMode(1, QHeaderView::Interactive
);
319 setSortingEnabled(false); // do not sort items
320 setRootIsDecorated(true);
321 setAllColumnsShowFocus(true);
323 m_pixPointer
= UserIcon("pointer.xpm");
324 if (m_pixPointer
.isNull())
325 TRACE("Can't load pointer.xpm");
332 QStringList
ExprWnd::exprList() const
335 for (int i
= 0; i
< topLevelItemCount(); i
++)
337 exprs
.append(topLevelItem(i
)->getText());
342 VarTree
* ExprWnd::insertExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
344 // append a new dummy expression
345 VarTree
* display
= new VarTree(this, expr
);
347 // replace it right away
348 updateExpr(display
, expr
, typeTable
);
352 void ExprWnd::updateExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
354 // search the root variable
356 for (int i
= 0; i
< topLevelItemCount(); i
++)
358 if (topLevelItem(i
)->getText() == expr
->m_name
) {
359 item
= topLevelItem(i
);
367 updateExprRec(item
, expr
, typeTable
);
368 collectUnknownTypes(item
);
371 void ExprWnd::updateExpr(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
373 updateExprRec(display
, newValues
, typeTable
);
374 collectUnknownTypes(display
);
378 * returns true if there's a visible change
380 void ExprWnd::updateExprRec(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
382 bool isExpanded
= display
->isExpanded();
385 * If we are updating a pointer without children by a dummy, we don't
386 * collapse it, but simply insert the new children. This happens when a
387 * pointer has just been expanded by the user.
389 if (display
->m_varKind
== VarTree::VKpointer
&&
390 display
->childCount() == 0 &&
391 newValues
->m_varKind
== VarTree::VKdummy
)
393 replaceChildren(display
, newValues
);
398 * If the display and newValues have different kind or if their number
399 * of children is different, replace the whole sub-tree.
401 if (// the next two lines mean: not(m_varKind remains unchanged)
402 !(newValues
->m_varKind
== VarTree::VKdummy
||
403 display
->m_varKind
== newValues
->m_varKind
)
405 (display
->childCount() != newValues
->childCount() &&
407 * If this is a pointer and newValues doesn't have children, we
408 * don't replace the sub-tree; instead, below we mark this
409 * sub-tree for requiring an update.
411 (display
->m_varKind
!= VarTree::VKpointer
||
412 newValues
->m_child
!= 0)))
415 display
->setExpanded(false);
418 // since children changed, it is likely that the type has also changed
419 display
->m_type
= 0; /* will re-evaluate the type */
421 // display the new value
422 updateSingleExpr(display
, newValues
);
423 replaceChildren(display
, newValues
);
425 // update the m_varKind
426 if (newValues
->m_varKind
!= VarTree::VKdummy
) {
427 display
->m_varKind
= newValues
->m_varKind
;
428 if (newValues
->m_child
!= 0 || newValues
->m_varKind
== VarTree::VKpointer
)
429 display
->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator
);
431 display
->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
434 // get some types (after the new m_varKind has been set!)
435 display
->inferTypesOfChildren(typeTable
);
437 // (note that the new value might not have a sub-tree at all)
441 // display the new value
442 updateSingleExpr(display
, newValues
);
445 * If this is an expanded pointer, record it for being updated.
447 if (display
->m_varKind
== VarTree::VKpointer
) {
449 // if newValues is a dummy, we have already updated this pointer
450 newValues
->m_varKind
!= VarTree::VKdummy
)
452 m_updatePtrs
.push_back(display
);
455 * If the visible sub-tree has children, but newValues doesn't, we
458 if (newValues
->m_child
== 0) {
463 ASSERT(display
->childCount() == newValues
->childCount());
466 ExprValue
* vNew
= newValues
->m_child
;
467 for (int i
= 0; i
< display
->childCount(); i
++)
469 VarTree
* vDisplay
= display
->child(i
);
470 // check whether the names are the same
471 if (vDisplay
->getText() != vNew
->m_name
) {
473 vDisplay
->setText(vNew
->m_name
);
476 updateExprRec(vDisplay
, vNew
, typeTable
);
482 void ExprWnd::updateSingleExpr(VarTree
* display
, ExprValue
* newValue
)
485 * If newValues is a VKdummy, we are only interested in its children.
486 * No need to update anything here.
488 if (newValue
->m_varKind
== VarTree::VKdummy
) {
493 * If this node is a struct and we know its type then we know how to
494 * find a nested value. So register the node for an update.
496 * wchar_t types are also treated specially here: We consider them
497 * as struct (has been set in inferTypesOfChildren()).
499 if (display
->m_varKind
== VarTree::VKstruct
&&
500 display
->m_type
!= 0 &&
501 display
->m_type
!= TypeInfo::unknownType())
503 ASSERT(newValue
->m_varKind
== VarTree::VKstruct
);
504 if (display
->m_type
== TypeInfo::wchartType())
506 display
->m_partialValue
= "L";
509 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
510 m_updateStruct
.push_back(display
);
513 display
->updateValue(newValue
->m_value
);
516 void ExprWnd::updateStructValue(VarTree
* display
)
518 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
520 display
->updateStructValue(display
->m_partialValue
);
522 display
->m_partialValue
= "";
523 display
->m_exprIndex
= -1;
526 void ExprWnd::replaceChildren(VarTree
* display
, ExprValue
* newValues
)
528 ASSERT(display
->childCount() == 0 || display
->m_varKind
!= VarTree::VKsimple
);
530 // delete all children of display
531 while (VarTree
* c
= display
->child(0)) {
535 // insert copies of the newValues
536 for (ExprValue
* v
= newValues
->m_child
; v
!= 0; v
= v
->m_next
)
538 VarTree
* vNew
= new VarTree(display
, v
);
540 replaceChildren(vNew
, v
);
544 void ExprWnd::collectUnknownTypes(VarTree
* var
)
546 QTreeWidgetItemIterator
i(var
);
549 checkUnknownType(static_cast<VarTree
*>(*i
));
553 void ExprWnd::checkUnknownType(VarTree
* var
)
555 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
556 if (var
->m_type
== 0 &&
557 var
->m_varKind
== VarTree::VKstruct
&&
558 var
->m_nameKind
!= VarTree::NKtype
&&
559 var
->m_nameKind
!= VarTree::NKanonymous
)
561 if (!var
->isWcharT())
563 /* this struct node doesn't have a type yet: register it */
564 m_updateType
.push_back(var
);
568 var
->m_type
= TypeInfo::wchartType();
569 var
->m_partialValue
= "L";
570 m_updateStruct
.push_back(var
);
573 // add pointer pixmap to pointers
574 if (var
->m_varKind
== VarTree::VKpointer
) {
575 var
->setPixmap(m_pixPointer
);
579 QString
ExprWnd::formatWCharPointer(QString value
)
581 int pos
= value
.indexOf(") ");
583 value
= value
.mid(pos
+2);
588 VarTree
* ExprWnd::topLevelExprByName(const QString
& name
) const
590 for (int i
= 0; i
< topLevelItemCount(); i
++)
592 if (topLevelItem(i
)->getText() == name
)
593 return topLevelItem(i
);
598 VarTree
* ExprWnd::ptrMemberByName(VarTree
* v
, const QString
& name
)
600 // v must be a pointer variable, must have children
601 if (v
->m_varKind
!= VarTree::VKpointer
|| v
->childCount() == 0)
604 // the only child of v is the pointer value that represents the struct
605 VarTree
* item
= v
->child(0);
606 return memberByName(item
, name
);
609 VarTree
* ExprWnd::memberByName(VarTree
* v
, const QString
& name
)
611 // search immediate children for name
612 for (int i
= 0; i
< v
->childCount(); i
++)
614 if (v
->child(i
)->getText() == name
)
618 // try in base classes and members that are anonymous structs or unions
619 for (int i
= 0; i
< v
->childCount(); i
++)
621 VarTree
* item
= v
->child(i
);
622 if (item
->m_nameKind
== VarTree::NKtype
||
623 item
->m_nameKind
== VarTree::NKanonymous
)
625 item
= memberByName(item
, name
);
633 void ExprWnd::removeExpr(VarTree
* item
)
640 void ExprWnd::unhookSubtree(VarTree
* subTree
)
642 // must remove any pointers scheduled for update from the list
643 unhookSubtree(m_updatePtrs
, subTree
);
644 unhookSubtree(m_updateType
, subTree
);
645 unhookSubtree(m_updateStruct
, subTree
);
646 emit
removingItem(subTree
);
649 void ExprWnd::unhookSubtree(std::list
<VarTree
*>& list
, VarTree
* subTree
)
654 std::list
<VarTree
*>::iterator i
= list
.begin();
655 while (i
!= list
.end()) {
656 std::list
<VarTree
*>::iterator checkItem
= i
;
658 if (subTree
->isAncestorEq(*checkItem
)) {
659 // checkItem is an item from subTree
660 list
.erase(checkItem
);
665 void ExprWnd::clearPendingUpdates()
667 m_updatePtrs
.clear();
668 m_updateType
.clear();
669 m_updateStruct
.clear();
672 VarTree
* ExprWnd::nextUpdatePtr()
675 if (!m_updatePtrs
.empty()) {
676 ptr
= m_updatePtrs
.front();
677 m_updatePtrs
.pop_front();
682 VarTree
* ExprWnd::nextUpdateType()
685 if (!m_updateType
.empty()) {
686 ptr
= m_updateType
.front();
687 m_updateType
.pop_front();
692 VarTree
* ExprWnd::nextUpdateStruct()
695 if (!m_updateStruct
.empty()) {
696 ptr
= m_updateStruct
.front();
697 m_updateStruct
.pop_front();
703 void ExprWnd::editValue(VarTree
* item
, const QString
& text
)
706 m_edit
= new ValueEdit(this);
708 QRect r
= visualItemRect(item
);
709 int x
= columnViewportPosition(1);
711 int w
= columnWidth(1);
715 * Make the edit widget at least 5 characters wide (but not wider than
716 * this widget). If less than half of this widget is used to display
717 * the text, scroll this widget so that half of it shows the text (or
718 * less than half of it if the text is shorter).
720 QFontMetrics metr
= m_edit
->font();
721 int wMin
= metr
.width("88888");
724 int wThis
= viewport()->width();
725 if (x
>= wThis
/2 && // less than half the width displays text
726 x
+w
> wThis
) // not all text is visible
728 // scroll so that more text is visible
729 QScrollBar
* pScrollBar
= horizontalScrollBar();
730 int wScroll
= qMin(x
-wThis
/2, x
+w
-wThis
);
731 pScrollBar
->setValue(pScrollBar
->value() + wScroll
);
736 // don't let the edit move out at the left
740 // make the edit box as wide as the visible column
741 QRect
rect(x
,y
, wThis
-x
,h
);
742 m_edit
->setText(text
);
745 m_edit
->setGeometry(rect
);
746 m_edit
->m_finished
= false;
747 m_edit
->m_item
= item
;
752 bool ExprWnd::isEditing() const
754 return m_edit
!= 0 && m_edit
->isVisible();
758 ValueEdit::ValueEdit(ExprWnd
* parent
) :
759 QLineEdit(parent
->viewport())
763 lower(); // lower the window below scrollbars
764 connect(parent
, SIGNAL(itemActivated(QTreeWidgetItem
*,int)),
765 SLOT(slotSelectionChanged()));
766 connect(parent
, SIGNAL(currentItemChanged(QTreeWidgetItem
*,QTreeWidgetItem
*)),
767 SLOT(slotSelectionChanged()));
768 connect(parent
, SIGNAL(itemExpanded(QTreeWidgetItem
*)),
769 SLOT(slotSelectionChanged()));
770 connect(parent
, SIGNAL(itemCollapsed(QTreeWidgetItem
*)),
771 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 if (focusEv
->reason() == Qt::ActiveWindowFocusReason
)
817 // Switching to a different window should terminate the edit,
818 // because if the window with this variable display is floating
819 // then that different window could be the main window, where
820 // the user had clicked one of the Execute buttons. This in turn
821 // may pull the item away that we are editing here.
824 // Don't let a RMB close the editor
825 else if (focusEv
->reason() != Qt::PopupFocusReason
)
831 void ValueEdit::slotSelectionChanged()
833 TRACE("ValueEdit::slotSelectionChanged");