SILICON
Loading...
Searching...
No Matches
projectTree.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "projectTree.hpp"
19
20#include <QAbstractItemView>
21#include <QFileInfo>
22#include <QFont>
23#include <QSignalBlocker>
24#include <QTreeWidgetItemIterator>
25
26#include <ui/shell/icons.hpp>
27#include <ui/shell/uiUtils.hpp>
28
29namespace SILICON::ui {
30namespace {
31
32 constexpr int ITEM_KIND_ROLE = Qt::UserRole;
33 constexpr int DOCUMENT_TYPE_ROLE = Qt::UserRole + 1;
34 constexpr int PATH_ROLE = Qt::UserRole + 2;
35 constexpr int DOCUMENT_CATEGORY_ROLE = Qt::UserRole + 3;
36
37 [[nodiscard]] QString circuitDisplayName(const project::Document& document)
38 {
39 return QString::fromStdString(
40 project::documentSlugForPath(document.getPath()).value_or(document.getPath()));
41 }
42
43 [[nodiscard]] QString sectionTitle(const project::DocumentCategory category)
44 {
45 switch (category) {
46 case project::DocumentCategory::Diagram: return ProjectTree::tr("Circuits");
47 case project::DocumentCategory::Code: return ProjectTree::tr("Code");
49 return ProjectTree::tr("ISA Architectures");
50 case project::DocumentCategory::Binary: return ProjectTree::tr("Binaries");
51 }
52 return {};
53 }
54
55 [[nodiscard]] QString documentLabel(const project::Document& document)
56 {
57 const auto category = project::categoryOf(document.getType());
60 return QFileInfo(QString::fromStdString(document.getPath())).fileName();
61 switch (document.getType()) {
62 case project::DocumentType::Circuit: return circuitDisplayName(document);
64 return QString::fromStdString(project::documentSlugForPath(document.getPath())
65 .value_or(document.getPath()));
66 default: break;
67 }
68 return {};
69 }
70
71 void setKind(QTreeWidgetItem* item, const ProjectTreeItemKind kind)
72 {
73 item->setData(0, ITEM_KIND_ROLE, static_cast<int>(kind));
74 }
75
76 void setDocumentType(QTreeWidgetItem* item, const project::DocumentType type)
77 {
78 item->setData(0, DOCUMENT_TYPE_ROLE, static_cast<int>(type));
79 }
80
81 void setDocumentCategory(QTreeWidgetItem* item,
82 const project::DocumentCategory category)
83 {
84 item->setData(0, DOCUMENT_CATEGORY_ROLE, static_cast<int>(category));
85 }
86
87} // namespace
88
89ProjectTree::ProjectTree(QWidget* parent) : QTreeWidget(parent)
90{
91 setHeaderHidden(true);
92 setRootIsDecorated(true);
93 setSelectionMode(QAbstractItemView::SingleSelection);
94 setContextMenuPolicy(Qt::CustomContextMenu);
95}
96
98 const std::span<const project::Document> documents,
99 const std::string_view activeDocumentPath)
100{
101 const QSignalBlocker blocker(this);
102 clear();
103
104 auto* projectItem = new QTreeWidgetItem(this);
105 projectItem->setText(0, QString::fromStdString(projectInfo.name));
106 setKind(projectItem, ProjectTreeItemKind::Project);
107 projectItem->setExpanded(true);
108
109 for (const auto category : {project::DocumentCategory::Diagram,
113 addSection(projectItem, category, documents);
114
115 expandAll();
116
117 if (!activeDocumentPath.empty())
118 selectDocument(activeDocumentPath);
119}
120
121void ProjectTree::selectDocument(const std::string_view path)
122{
123 const QSignalBlocker blocker(this);
124 clearSelection();
125
126 const auto targetPath =
127 QString::fromUtf8(path.data(), static_cast<qsizetype>(path.size()));
128 for (QTreeWidgetItemIterator it(this); *it; ++it) {
129 if ((*it)->data(0, PATH_ROLE).toString() != targetPath)
130 continue;
131
132 (*it)->setSelected(true);
133 setCurrentItem(*it);
134 return;
135 }
136
137 if (path.starts_with("isa/")) {
138 const auto separator = path.find('/', 4);
139 if (separator != std::string_view::npos) {
140 const auto name = QString::fromUtf8(path.substr(4, separator - 4));
141 for (QTreeWidgetItemIterator it(this); *it; ++it) {
143 || (*it)->text(0) != name)
144 continue;
145 (*it)->setSelected(true);
146 setCurrentItem(*it);
147 return;
148 }
149 }
150 }
151}
152
154{
155 const QSignalBlocker blocker(this);
156 clearSelection();
157}
158
159QTreeWidgetItem* ProjectTree::selectedProjectItem() const
160{
161 const auto items = selectedItems();
162 return items.empty() ? nullptr : items.front();
163}
164
165std::optional<ProjectTreeDocumentSelection> ProjectTree::selectedDocument() const
166{
167 const auto* item = selectedProjectItem();
168 if (!item || (itemKind(item) != ProjectTreeItemKind::Document
170 return std::nullopt;
171
172 const auto type = itemDocumentType(item);
173 if (!type)
174 return std::nullopt;
175
177 .type = *type,
178 .path = documentPath(item),
179 };
180}
181
182ProjectTreeItemKind ProjectTree::itemKind(const QTreeWidgetItem* item)
183{
184 return static_cast<ProjectTreeItemKind>(item->data(0, ITEM_KIND_ROLE).toInt());
185}
186
187std::optional<project::DocumentType>
188ProjectTree::itemDocumentType(const QTreeWidgetItem* item)
189{
190 const auto value = item->data(0, DOCUMENT_TYPE_ROLE);
191 if (!value.isValid())
192 return std::nullopt;
193 return static_cast<project::DocumentType>(value.toInt());
194}
195
196std::optional<project::DocumentCategory>
197ProjectTree::itemDocumentCategory(const QTreeWidgetItem* item)
198{
199 const auto value = item->data(0, DOCUMENT_CATEGORY_ROLE);
200 if (!value.isValid())
201 return std::nullopt;
202 return static_cast<project::DocumentCategory>(value.toInt());
203}
204
205std::string ProjectTree::documentPath(const QTreeWidgetItem* item)
206{
207 return item->data(0, PATH_ROLE).toString().toStdString();
208}
209
210std::string ProjectTree::architectureName(const QTreeWidgetItem* item)
211{
212 if (!item || itemKind(item) != ProjectTreeItemKind::Architecture)
213 return {};
214 return item->text(0).toStdString();
215}
216
217void ProjectTree::addSection(QTreeWidgetItem* projectItem,
218 const project::DocumentCategory category,
219 const std::span<const project::Document> documents)
220{
221 bool hasDocuments = false;
222 for (const auto& document : documents) {
223 if (project::categoryOf(document.getType()) == category) {
224 hasDocuments = true;
225 break;
226 }
227 }
228
229 if (!hasDocuments)
230 return;
231
232 auto* section = new QTreeWidgetItem(projectItem);
233 section->setText(0, sectionTitle(category));
234 setKind(section, ProjectTreeItemKind::Section);
235 setDocumentCategory(section, category);
236 section->setExpanded(true);
237
238 for (const auto& document : documents) {
239 if (project::categoryOf(document.getType()) != category)
240 continue;
242 const auto name = project::documentSlugForPath(document.getPath());
243 if (!name)
244 continue;
245 bool found = false;
246 for (int i = 0; i < section->childCount(); ++i)
247 found |= section->child(i)->text(0) == QString::fromStdString(*name);
248 if (found)
249 continue;
250 auto* item = new QTreeWidgetItem(section);
251 item->setText(0, QString::fromStdString(*name));
252 item->setIcon(0, Icon("cpu"));
254 setDocumentType(item, document.getType());
255 item->setData(0, PATH_ROLE, QString::fromStdString(document.getPath()));
256 } else {
257 addDocument(section, document);
258 }
259 }
260}
261
262void ProjectTree::addDocument(QTreeWidgetItem* parent, const project::Document& document)
263{
264 auto* item = new QTreeWidgetItem(parent);
265 item->setText(0, documentLabel(document));
266 item->setIcon(0, categoryIcon(document.getType()));
267 setKind(item, ProjectTreeItemKind::Document);
268 setDocumentType(item, document.getType());
269 item->setData(0, PATH_ROLE, QString::fromStdString(document.getPath()));
270}
271
272} // namespace SILICON::ui
DocumentType getType() const noexcept
const std::string & getPath() const noexcept
static std::optional< project::DocumentType > itemDocumentType(const QTreeWidgetItem *item)
static std::string architectureName(const QTreeWidgetItem *item)
void addSection(QTreeWidgetItem *projectItem, project::DocumentCategory category, std::span< const project::Document > documents)
static ProjectTreeItemKind itemKind(const QTreeWidgetItem *item)
void rebuild(const project::ProjectInfo &project, std::span< const project::Document > documents, std::string_view activeDocumentPath={})
static std::string documentPath(const QTreeWidgetItem *item)
void selectDocument(std::string_view path)
QTreeWidgetItem * selectedProjectItem() const
void addDocument(QTreeWidgetItem *parent, const project::Document &document)
static std::optional< project::DocumentCategory > itemDocumentCategory(const QTreeWidgetItem *item)
ProjectTree(QWidget *parent=nullptr)
std::optional< ProjectTreeDocumentSelection > selectedDocument() const
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< std::string > documentSlugForPath(const std::string_view path)
QIcon categoryIcon(const SILICON::project::DocumentType type)
Definition uiUtils.cpp:26
ProjectTreeItemKind
Semantic role of an item shown in the project tree.
Project-level data stored in project.json.
std::string name
Human-readable project name.