ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
AstModel.h
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
25#pragma once
26
27#include "core/AstNode.h"
28#include <QAbstractItemModel>
29#include <QString>
30#include <vector>
31
32namespace acav {
33
41class AstModel : public QAbstractItemModel {
42 Q_OBJECT
43
44public:
45 explicit AstModel(QObject *parent = nullptr);
46 ~AstModel() override;
47
49 void setEmptyMessage(const QString &message);
50
54 void setRootNode(AstViewNode *root);
55
57 void setTotalNodeCount(std::size_t count) { totalNodeCount_ = count; }
58
60 void clear();
61
63 int visibleNodeCount() const;
64
65 // QAbstractItemModel interface
66 QModelIndex index(int row, int column,
67 const QModelIndex &parent = QModelIndex()) const override;
68 QModelIndex parent(const QModelIndex &child) const override;
69 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
70 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
71 QVariant data(const QModelIndex &index,
72 int role = Qt::DisplayRole) const override;
73 QVariant headerData(int section, Qt::Orientation orientation,
74 int role = Qt::DisplayRole) const override;
75 Qt::ItemFlags flags(const QModelIndex &index) const override;
76
77 // Custom roles
78 enum CustomRoles {
79 NodePtrRole = Qt::UserRole + 1 // Returns AstViewNode*
80 };
81
85 QModelIndex selectNode(AstViewNode *node);
86
89 void updateSelectionFromIndex(const QModelIndex &index);
90
92 AstViewNode *selectedNode() const { return selectedNode_; }
93
95 bool hasNodes() const { return root_ != nullptr; }
96
97signals:
100
101private:
102 AstViewNode *getNodeFromIndex(const QModelIndex &index) const;
103
105 QModelIndex findNodeIndex(AstViewNode *node) const;
106
108 const std::vector<AstViewNode *> &
109 visibleChildrenFor(AstViewNode *parent) const;
110
112 int visibleRow(AstViewNode *node) const;
113
114 AstViewNode *root_ = nullptr; // Not owned - AstContext owns all nodes
115 AstViewNode *selectedNode_ = nullptr;
116 std::size_t totalNodeCount_ = 0;
117 QString emptyMessage_;
118};
119
120} // namespace acav
AST data structures and memory management.
void updateSelectionFromIndex(const QModelIndex &index)
Update selection from a QModelIndex (e.g., when user clicks directly).
Definition AstModel.cpp:448
bool hasNodes() const
Check if model has any nodes loaded.
Definition AstModel.h:95
int visibleNodeCount() const
Number of nodes currently visible.
Definition AstModel.cpp:204
void setTotalNodeCount(std::size_t count)
Set total node count for display.
Definition AstModel.h:57
void clear()
Clear model and delete AST.
Definition AstModel.cpp:196
void setRootNode(AstViewNode *root)
Set the root node of AST.
Definition AstModel.cpp:189
AstViewNode * selectedNode() const
Get currently selected node.
Definition AstModel.h:92
QModelIndex selectNode(AstViewNode *node)
Programmatically select node and get its model index.
Definition AstModel.cpp:418
void nodeSelected(AstViewNode *node)
Emitted when node is selected.
void setEmptyMessage(const QString &message)
Message shown when the model has no AST loaded.
Definition AstModel.cpp:180
Represents node in AST tree hierarchy.
Definition AstNode.h:195