File indexing completed on 2024-04-28 05:48:34

0001 //
0002 // Description: Widget that local variables of the gdb inferior
0003 //
0004 // SPDX-FileCopyrightText: 2010 Kåre Särs <kare.sars@iki.fi>
0005 //
0006 //  SPDX-License-Identifier: LGPL-2.0-only
0007 
0008 #include "localsview.h"
0009 #include <KLocalizedString>
0010 #include <QDebug>
0011 #include <QLabel>
0012 
0013 LocalsView::LocalsView(QWidget *parent)
0014     : QTreeWidget(parent)
0015 {
0016     QStringList headers;
0017     headers << i18n("Symbol");
0018     headers << i18n("Value");
0019     setHeaderLabels(headers);
0020     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
0021 }
0022 
0023 LocalsView::~LocalsView()
0024 {
0025 }
0026 
0027 void LocalsView::showEvent(QShowEvent *)
0028 {
0029     Q_EMIT localsVisible(true);
0030 }
0031 
0032 void LocalsView::hideEvent(QHideEvent *)
0033 {
0034     Q_EMIT localsVisible(false);
0035 }
0036 
0037 QString nameTip(const dap::Variable &variable)
0038 {
0039     QString text = QStringLiteral("<qt>%1<qt>").arg(variable.name);
0040     if (variable.type && !variable.type->isEmpty()) {
0041         text += QStringLiteral("<em>%1</em>: %2").arg(i18n("type")).arg(variable.type.value());
0042     }
0043     return text;
0044 }
0045 
0046 QString valueTip(const dap::Variable &variable)
0047 {
0048     QString text;
0049     if (variable.indexedVariables.value_or(0) > 0) {
0050         text + QStringLiteral("<em>%1</em>: %2").arg(i18n("indexed items")).arg(variable.indexedVariables.value());
0051     }
0052     if (variable.namedVariables.value_or(0) > 0) {
0053         text + QStringLiteral("<em>%1</em>: %2").arg(i18n("named items")).arg(variable.namedVariables.value());
0054     }
0055     text += QStringLiteral("<qt>%1<qt>").arg(variable.value);
0056     return text;
0057 }
0058 
0059 static void formatName(QTreeWidgetItem &item, const dap::Variable &variable)
0060 {
0061     QFont font = item.font(0);
0062     font.setBold(variable.valueChanged.value_or(false));
0063     item.setFont(0, font);
0064 }
0065 
0066 QTreeWidgetItem *LocalsView::createWrappedItem(QTreeWidgetItem *parent, const dap::Variable &variable)
0067 {
0068     QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(variable.name));
0069     formatName(*item, variable);
0070     QLabel *label = new QLabel(variable.value);
0071     label->setWordWrap(true);
0072     setItemWidget(item, 1, label);
0073     item->setData(1, Qt::UserRole, variable.value);
0074     item->setToolTip(0, nameTip(variable));
0075     item->setToolTip(1, valueTip(variable));
0076 
0077     return item;
0078 }
0079 
0080 QTreeWidgetItem *LocalsView::createWrappedItem(QTreeWidget *parent, const dap::Variable &variable)
0081 {
0082     QTreeWidgetItem *item = new QTreeWidgetItem(parent, QStringList(variable.name));
0083     formatName(*item, variable);
0084     QLabel *label = new QLabel(variable.value);
0085     label->setWordWrap(true);
0086     setItemWidget(item, 1, label);
0087     item->setToolTip(0, nameTip(variable));
0088     item->setToolTip(1, valueTip(variable));
0089 
0090     return item;
0091 }
0092 
0093 void LocalsView::openVariableScope()
0094 {
0095     clear();
0096     m_variables.clear();
0097 }
0098 
0099 void LocalsView::closeVariableScope()
0100 {
0101     if (m_variables.count() == 1) {
0102         // Auto-expand if there is a single item
0103         QTreeWidgetItem *item = m_variables.begin().value();
0104         item->setExpanded(true);
0105     }
0106 }
0107 
0108 void LocalsView::addVariableLevel(int parentId, const dap::Variable &variable)
0109 {
0110     QTreeWidgetItem *item = nullptr;
0111 
0112     if (parentId == 0) {
0113         item = createWrappedItem(this, variable);
0114     } else {
0115         if (!m_variables.contains(parentId)) {
0116             qDebug() << "unknown variable reference:" << parentId;
0117             return;
0118         }
0119         item = createWrappedItem(m_variables[parentId], variable);
0120     }
0121 
0122     if (variable.variablesReference > 0) {
0123         m_variables[variable.variablesReference] = item;
0124     }
0125 }
0126 
0127 #include "moc_localsview.cpp"