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 QVariant
VarTree::data(int column
, int role
) const
267 if (role
!= Qt::ToolTipRole
|| column
!= 1)
268 return QTreeWidgetItem::data(column
, role
);
269 return QVariant(value());
272 ExprValue::ExprValue(const QString
& name
, VarTree::NameKind aKind
) :
274 m_varKind(VarTree::VKsimple
),
278 m_initiallyExpanded(false)
282 ExprValue::~ExprValue()
288 void ExprValue::appendChild(ExprValue
* newChild
)
293 // walk chain of children to find the last one
294 ExprValue
* last
= m_child
;
295 while (last
->m_next
!= 0)
297 last
->m_next
= newChild
;
299 newChild
->m_next
= 0; // just to be sure
302 int ExprValue::childCount() const
305 ExprValue
* c
= m_child
;
315 ExprWnd::ExprWnd(QWidget
* parent
, const QString
& colHeader
) :
319 QTreeWidgetItem
* pHeaderItem
= new QTreeWidgetItem();
320 pHeaderItem
->setText(0, colHeader
);
321 pHeaderItem
->setText(1, i18n("Value"));
322 setHeaderItem(pHeaderItem
);
323 header()->setSectionResizeMode(0, QHeaderView::Interactive
);
324 header()->setSectionResizeMode(1, QHeaderView::Interactive
);
326 setSortingEnabled(false); // do not sort items
327 setRootIsDecorated(true);
328 setAllColumnsShowFocus(true);
330 m_pixPointer
= UserIcon("pointer.xpm");
331 if (m_pixPointer
.isNull())
332 TRACE("Can't load pointer.xpm");
339 QStringList
ExprWnd::exprList() const
342 for (int i
= 0; i
< topLevelItemCount(); i
++)
344 exprs
.append(topLevelItem(i
)->getText());
349 VarTree
* ExprWnd::insertExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
351 // append a new dummy expression
352 VarTree
* display
= new VarTree(this, expr
);
354 // replace it right away
355 updateExpr(display
, expr
, typeTable
);
359 void ExprWnd::updateExpr(ExprValue
* expr
, ProgramTypeTable
& typeTable
)
361 // search the root variable
363 for (int i
= 0; i
< topLevelItemCount(); i
++)
365 if (topLevelItem(i
)->getText() == expr
->m_name
) {
366 item
= topLevelItem(i
);
374 updateExprRec(item
, expr
, typeTable
);
375 collectUnknownTypes(item
);
378 void ExprWnd::updateExpr(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
380 updateExprRec(display
, newValues
, typeTable
);
381 collectUnknownTypes(display
);
385 * returns true if there's a visible change
387 void ExprWnd::updateExprRec(VarTree
* display
, ExprValue
* newValues
, ProgramTypeTable
& typeTable
)
389 bool isExpanded
= display
->isExpanded();
392 * If we are updating a pointer without children by a dummy, we don't
393 * collapse it, but simply insert the new children. This happens when a
394 * pointer has just been expanded by the user.
396 if (display
->m_varKind
== VarTree::VKpointer
&&
397 display
->childCount() == 0 &&
398 newValues
->m_varKind
== VarTree::VKdummy
)
400 replaceChildren(display
, newValues
);
405 * If the display and newValues have different kind or if their number
406 * of children is different, replace the whole sub-tree.
408 if (// the next two lines mean: not(m_varKind remains unchanged)
409 !(newValues
->m_varKind
== VarTree::VKdummy
||
410 display
->m_varKind
== newValues
->m_varKind
)
412 (display
->childCount() != newValues
->childCount() &&
414 * If this is a pointer and newValues doesn't have children, we
415 * don't replace the sub-tree; instead, below we mark this
416 * sub-tree for requiring an update.
418 (display
->m_varKind
!= VarTree::VKpointer
||
419 newValues
->m_child
!= 0)))
422 display
->setExpanded(false);
425 // since children changed, it is likely that the type has also changed
426 display
->m_type
= 0; /* will re-evaluate the type */
428 // display the new value
429 updateSingleExpr(display
, newValues
);
430 replaceChildren(display
, newValues
);
432 // update the m_varKind
433 if (newValues
->m_varKind
!= VarTree::VKdummy
) {
434 display
->m_varKind
= newValues
->m_varKind
;
435 if (newValues
->m_child
!= 0 || newValues
->m_varKind
== VarTree::VKpointer
)
436 display
->setChildIndicatorPolicy(QTreeWidgetItem::ShowIndicator
);
438 display
->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator
);
441 // get some types (after the new m_varKind has been set!)
442 display
->inferTypesOfChildren(typeTable
);
444 // (note that the new value might not have a sub-tree at all)
448 // display the new value
449 updateSingleExpr(display
, newValues
);
452 * If this is an expanded pointer, record it for being updated.
454 if (display
->m_varKind
== VarTree::VKpointer
) {
456 // if newValues is a dummy, we have already updated this pointer
457 newValues
->m_varKind
!= VarTree::VKdummy
)
459 m_updatePtrs
.push_back(display
);
462 * If the visible sub-tree has children, but newValues doesn't, we
465 if (newValues
->m_child
== 0) {
470 ASSERT(display
->childCount() == newValues
->childCount());
473 ExprValue
* vNew
= newValues
->m_child
;
474 for (int i
= 0; i
< display
->childCount(); i
++)
476 VarTree
* vDisplay
= display
->child(i
);
477 // check whether the names are the same
478 if (vDisplay
->getText() != vNew
->m_name
) {
480 vDisplay
->setText(vNew
->m_name
);
483 updateExprRec(vDisplay
, vNew
, typeTable
);
489 void ExprWnd::updateSingleExpr(VarTree
* display
, ExprValue
* newValue
)
492 * If newValues is a VKdummy, we are only interested in its children.
493 * No need to update anything here.
495 if (newValue
->m_varKind
== VarTree::VKdummy
) {
500 * If this node is a struct and we know its type then we know how to
501 * find a nested value. So register the node for an update.
503 * wchar_t types are also treated specially here: We consider them
504 * as struct (has been set in inferTypesOfChildren()).
506 if (display
->m_varKind
== VarTree::VKstruct
&&
507 display
->m_type
!= 0 &&
508 display
->m_type
!= TypeInfo::unknownType())
510 ASSERT(newValue
->m_varKind
== VarTree::VKstruct
);
511 if (display
->m_type
== TypeInfo::wchartType())
513 display
->m_partialValue
= "L";
516 display
->m_partialValue
= display
->m_type
->m_displayString
[0];
517 m_updateStruct
.push_back(display
);
520 display
->updateValue(newValue
->m_value
);
523 void ExprWnd::updateStructValue(VarTree
* display
)
525 ASSERT(display
->m_varKind
== VarTree::VKstruct
);
527 display
->updateStructValue(display
->m_partialValue
);
529 display
->m_partialValue
= "";
530 display
->m_exprIndex
= -1;
533 void ExprWnd::replaceChildren(VarTree
* display
, ExprValue
* newValues
)
535 ASSERT(display
->childCount() == 0 || display
->m_varKind
!= VarTree::VKsimple
);
537 // delete all children of display
538 while (VarTree
* c
= display
->child(0)) {
542 // insert copies of the newValues
543 for (ExprValue
* v
= newValues
->m_child
; v
!= 0; v
= v
->m_next
)
545 VarTree
* vNew
= new VarTree(display
, v
);
547 replaceChildren(vNew
, v
);
551 void ExprWnd::collectUnknownTypes(VarTree
* var
)
553 QTreeWidgetItemIterator
i(var
);
556 checkUnknownType(static_cast<VarTree
*>(*i
));
560 void ExprWnd::checkUnknownType(VarTree
* var
)
562 ASSERT(var
->m_varKind
!= VarTree::VKpointer
|| var
->m_nameKind
!= VarTree::NKtype
);
563 if (var
->m_type
== 0 &&
564 var
->m_varKind
== VarTree::VKstruct
&&
565 var
->m_nameKind
!= VarTree::NKtype
&&
566 var
->m_nameKind
!= VarTree::NKanonymous
)
568 if (!var
->isWcharT())
570 /* this struct node doesn't have a type yet: register it */
571 m_updateType
.push_back(var
);
575 var
->m_type
= TypeInfo::wchartType();
576 var
->m_partialValue
= "L";
577 m_updateStruct
.push_back(var
);
580 // add pointer pixmap to pointers
581 if (var
->m_varKind
== VarTree::VKpointer
) {
582 var
->setPixmap(m_pixPointer
);
586 QString
ExprWnd::formatWCharPointer(QString value
)
588 int pos
= value
.indexOf(") ");
590 value
= value
.mid(pos
+2);
595 VarTree
* ExprWnd::topLevelExprByName(const QString
& name
) const
597 for (int i
= 0; i
< topLevelItemCount(); i
++)
599 if (topLevelItem(i
)->getText() == name
)
600 return topLevelItem(i
);
605 VarTree
* ExprWnd::ptrMemberByName(VarTree
* v
, const QString
& name
)
607 // v must be a pointer variable, must have children
608 if (v
->m_varKind
!= VarTree::VKpointer
|| v
->childCount() == 0)
611 // the only child of v is the pointer value that represents the struct
612 VarTree
* item
= v
->child(0);
613 return memberByName(item
, name
);
616 VarTree
* ExprWnd::memberByName(VarTree
* v
, const QString
& name
)
618 // search immediate children for name
619 for (int i
= 0; i
< v
->childCount(); i
++)
621 if (v
->child(i
)->getText() == name
)
625 // try in base classes and members that are anonymous structs or unions
626 for (int i
= 0; i
< v
->childCount(); i
++)
628 VarTree
* item
= v
->child(i
);
629 if (item
->m_nameKind
== VarTree::NKtype
||
630 item
->m_nameKind
== VarTree::NKanonymous
)
632 item
= memberByName(item
, name
);
640 void ExprWnd::removeExpr(VarTree
* item
)
647 void ExprWnd::unhookSubtree(VarTree
* subTree
)
649 // must remove any pointers scheduled for update from the list
650 unhookSubtree(m_updatePtrs
, subTree
);
651 unhookSubtree(m_updateType
, subTree
);
652 unhookSubtree(m_updateStruct
, subTree
);
653 emit
removingItem(subTree
);
656 void ExprWnd::unhookSubtree(std::list
<VarTree
*>& list
, VarTree
* subTree
)
661 std::list
<VarTree
*>::iterator i
= list
.begin();
662 while (i
!= list
.end()) {
663 std::list
<VarTree
*>::iterator checkItem
= i
;
665 if (subTree
->isAncestorEq(*checkItem
)) {
666 // checkItem is an item from subTree
667 list
.erase(checkItem
);
672 void ExprWnd::clearPendingUpdates()
674 m_updatePtrs
.clear();
675 m_updateType
.clear();
676 m_updateStruct
.clear();
679 VarTree
* ExprWnd::nextUpdatePtr()
682 if (!m_updatePtrs
.empty()) {
683 ptr
= m_updatePtrs
.front();
684 m_updatePtrs
.pop_front();
689 VarTree
* ExprWnd::nextUpdateType()
692 if (!m_updateType
.empty()) {
693 ptr
= m_updateType
.front();
694 m_updateType
.pop_front();
699 VarTree
* ExprWnd::nextUpdateStruct()
702 if (!m_updateStruct
.empty()) {
703 ptr
= m_updateStruct
.front();
704 m_updateStruct
.pop_front();
710 void ExprWnd::editValue(VarTree
* item
, const QString
& text
)
713 m_edit
= new ValueEdit(this);
715 QRect r
= visualItemRect(item
);
716 int x
= columnViewportPosition(1);
718 int w
= columnWidth(1);
722 * Make the edit widget at least 5 characters wide (but not wider than
723 * this widget). If less than half of this widget is used to display
724 * the text, scroll this widget so that half of it shows the text (or
725 * less than half of it if the text is shorter).
727 QFontMetrics
metr(m_edit
->font());
728 int wMin
= metr
.width("88888");
731 int wThis
= viewport()->width();
732 if (x
>= wThis
/2 && // less than half the width displays text
733 x
+w
> wThis
) // not all text is visible
735 // scroll so that more text is visible
736 QScrollBar
* pScrollBar
= horizontalScrollBar();
737 int wScroll
= qMin(x
-wThis
/2, x
+w
-wThis
);
738 pScrollBar
->setValue(pScrollBar
->value() + wScroll
);
743 // don't let the edit move out at the left
747 // make the edit box as wide as the visible column
748 QRect
rect(x
,y
, wThis
-x
,h
);
749 m_edit
->setText(text
);
752 m_edit
->setGeometry(rect
);
753 m_edit
->m_finished
= false;
754 m_edit
->m_item
= item
;
759 bool ExprWnd::isEditing() const
761 return m_edit
!= 0 && m_edit
->isVisible();
765 ValueEdit::ValueEdit(ExprWnd
* parent
) :
766 QLineEdit(parent
->viewport())
770 lower(); // lower the window below scrollbars
771 connect(parent
, SIGNAL(itemActivated(QTreeWidgetItem
*,int)),
772 SLOT(slotSelectionChanged()));
773 connect(parent
, SIGNAL(currentItemChanged(QTreeWidgetItem
*,QTreeWidgetItem
*)),
774 SLOT(slotSelectionChanged()));
775 connect(parent
, SIGNAL(itemExpanded(QTreeWidgetItem
*)),
776 SLOT(slotSelectionChanged()));
777 connect(parent
, SIGNAL(itemCollapsed(QTreeWidgetItem
*)),
778 SLOT(slotSelectionChanged()));
779 connect(this, SIGNAL(done(VarTree
*, const QString
&)),
780 parent
, SIGNAL(editValueCommitted(VarTree
*, const QString
&)));
783 ValueEdit::~ValueEdit()
787 void ValueEdit::terminate(bool commit
)
789 TRACE(commit
?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
793 hide(); // will call focusOutEvent, that's why we need m_finished
795 emit
done(m_item
, text());
800 void ValueEdit::keyPressEvent(QKeyEvent
*e
)
802 if(e
->key() == Qt::Key_Return
|| e
->key() == Qt::Key_Enter
)
804 else if(e
->key() == Qt::Key_Escape
)
807 QLineEdit::keyPressEvent(e
);
810 void ValueEdit::paintEvent(QPaintEvent
* e
)
812 QLineEdit::paintEvent(e
);
818 void ValueEdit::focusOutEvent(QFocusEvent
* ev
)
820 TRACE("ValueEdit::focusOutEvent");
821 QFocusEvent
* focusEv
= static_cast<QFocusEvent
*>(ev
);
822 if (focusEv
->reason() == Qt::ActiveWindowFocusReason
)
824 // Switching to a different window should terminate the edit,
825 // because if the window with this variable display is floating
826 // then that different window could be the main window, where
827 // the user had clicked one of the Execute buttons. This in turn
828 // may pull the item away that we are editing here.
831 // Don't let a RMB close the editor
832 else if (focusEv
->reason() != Qt::PopupFocusReason
)
838 void ValueEdit::slotSelectionChanged()
840 TRACE("ValueEdit::slotSelectionChanged");