20#include <QAbstractItemView>
23#include <QSignalBlocker>
24#include <QTreeWidgetItemIterator>
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;
37 [[nodiscard]] QString circuitDisplayName(
const project::Document& document)
39 return QString::fromStdString(
49 return ProjectTree::tr(
"ISA Architectures");
55 [[nodiscard]] QString documentLabel(
const project::Document& document)
60 return QFileInfo(QString::fromStdString(document.getPath())).fileName();
61 switch (document.getType()) {
65 .value_or(document.getPath()));
73 item->setData(0, ITEM_KIND_ROLE,
static_cast<int>(kind));
78 item->setData(0, DOCUMENT_TYPE_ROLE,
static_cast<int>(type));
81 void setDocumentCategory(QTreeWidgetItem* item,
84 item->setData(0, DOCUMENT_CATEGORY_ROLE,
static_cast<int>(category));
91 setHeaderHidden(
true);
92 setRootIsDecorated(
true);
93 setSelectionMode(QAbstractItemView::SingleSelection);
94 setContextMenuPolicy(Qt::CustomContextMenu);
98 const std::span<const project::Document> documents,
99 const std::string_view activeDocumentPath)
101 const QSignalBlocker blocker(
this);
104 auto* projectItem =
new QTreeWidgetItem(
this);
105 projectItem->setText(0, QString::fromStdString(projectInfo.
name));
107 projectItem->setExpanded(
true);
117 if (!activeDocumentPath.empty())
123 const QSignalBlocker blocker(
this);
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)
132 (*it)->setSelected(
true);
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)
145 (*it)->setSelected(
true);
155 const QSignalBlocker blocker(
this);
161 const auto items = selectedItems();
162 return items.empty() ?
nullptr : items.front();
187std::optional<project::DocumentType>
190 const auto value = item->data(0, DOCUMENT_TYPE_ROLE);
191 if (!value.isValid())
196std::optional<project::DocumentCategory>
199 const auto value = item->data(0, DOCUMENT_CATEGORY_ROLE);
200 if (!value.isValid())
207 return item->data(0, PATH_ROLE).toString().toStdString();
214 return item->text(0).toStdString();
219 const std::span<const project::Document> documents)
221 bool hasDocuments =
false;
222 for (
const auto& document : documents) {
232 auto* section =
new QTreeWidgetItem(projectItem);
233 section->setText(0, sectionTitle(category));
235 setDocumentCategory(section, category);
236 section->setExpanded(
true);
238 for (
const auto& document : documents) {
246 for (
int i = 0; i < section->childCount(); ++i)
247 found |= section->child(i)->text(0) == QString::fromStdString(*name);
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()));
264 auto* item =
new QTreeWidgetItem(parent);
265 item->setText(0, documentLabel(document));
268 setDocumentType(item, document.
getType());
269 item->setData(0, PATH_ROLE, QString::fromStdString(document.
getPath()));
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)
void clearDocumentSelection()
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)
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.