ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
NodeDetailsDialog.cpp
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
26
27#include <QHeaderView>
28#include <QVBoxLayout>
29
30namespace acav {
31
33 const QString &windowTitle,
34 QWidget *parent)
35 : QDialog(parent), properties_(std::move(properties)) {
36 setWindowTitle(windowTitle);
37 setupUI();
38}
39
40void NodeDetailsDialog::setupUI() {
41 setMinimumSize(400, 300);
42 resize(600, 500);
43
44 auto *layout = new QVBoxLayout(this);
45
46 treeWidget_ = new QTreeWidget(this);
47 treeWidget_->setHeaderLabels({tr("Property"), tr("Value")});
48 treeWidget_->setRootIsDecorated(true);
49 treeWidget_->setAnimated(false);
50 treeWidget_->setUniformRowHeights(true);
51 treeWidget_->setAlternatingRowColors(true);
52 treeWidget_->setIndentation(20);
53 treeWidget_->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
54 treeWidget_->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
55 treeWidget_->header()->setStretchLastSection(true);
56 treeWidget_->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
57
58 layout->addWidget(treeWidget_);
59
60 // Populate tree with data
61 populateTree(nullptr, QString(), properties_);
62 treeWidget_->expandToDepth(1);
63}
64
65void NodeDetailsDialog::populateTree(QTreeWidgetItem *parent, const QString &key,
66 const AcavJson &value) {
67 QTreeWidgetItem *item = nullptr;
68
69 if (value.is_object()) {
70 QString label = key.isEmpty() ? tr("(root)") : key;
71 item = new QTreeWidgetItem({label, tr("(object)")});
72
73 for (auto it = value.begin(); it != value.end(); ++it) {
74 QString childKey = QString::fromStdString(it.key().str());
75 populateTree(item, childKey, it.value());
76 }
77 } else if (value.is_array()) {
78 item = new QTreeWidgetItem({key, tr("[%1 items]").arg(value.size())});
79
80 int index = 0;
81 for (const auto &elem : value) {
82 QString indexKey = QStringLiteral("[%1]").arg(index++);
83 populateTree(item, indexKey, elem);
84 }
85 } else {
86 QString displayValue = valueToDisplayString(value, key);
87 item = new QTreeWidgetItem({key, displayValue});
88
89 if (value.is_boolean()) {
90 item->setForeground(1, QColor(0, 100, 0));
91 } else if (value.is_number()) {
92 item->setForeground(1, QColor(0, 0, 150));
93 } else if (value.is_null()) {
94 item->setForeground(1, QColor(128, 128, 128));
95 }
96 }
97
98 if (parent) {
99 parent->addChild(item);
100 } else {
101 treeWidget_->addTopLevelItem(item);
102 }
103}
104
105QString NodeDetailsDialog::valueToDisplayString(const AcavJson &value,
106 const QString &key) {
107 if (value.is_boolean()) {
108 return value.get<bool>() ? QStringLiteral("true") : QStringLiteral("false");
109 }
110 if (value.is_number_integer()) {
111 int64_t num = value.get<int64_t>();
112 // Format "nodePtr" fields as hex
113 if (key == QStringLiteral("nodePtr")) {
114 return QStringLiteral("0x%1").arg(static_cast<quint64>(num), 0, 16);
115 }
116 return QString::number(num);
117 }
118 if (value.is_number_unsigned()) {
119 uint64_t num = value.get<uint64_t>();
120 // Format "nodePtr" fields as hex
121 if (key == QStringLiteral("nodePtr")) {
122 return QStringLiteral("0x%1").arg(num, 0, 16);
123 }
124 return QString::number(num);
125 }
126 if (value.is_number_float()) {
127 return QString::number(value.get<double>());
128 }
129 if (value.is_string()) {
130 return QString::fromStdString(value.get<InternedString>().str());
131 }
132 if (value.is_null()) {
133 return QStringLiteral("null");
134 }
135 return QStringLiteral("(unknown)");
136}
137
138} // namespace acav
nlohmann::basic_json< std::map, std::vector, InternedString, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer, std::vector< uint8_t > > AcavJson
Custom JSON type using InternedString for automatic string deduplication.
Definition AstNode.h:51
Non-modal dialog displaying AST node properties in a tree view.
NodeDetailsDialog(AcavJson properties, const QString &windowTitle, QWidget *parent=nullptr)
Create a node details dialog.