35#include <QDialogButtonBox>
53using namespace SILICON::core;
58 const auto path = document.
getPath();
60 auto addDocument = [
this, document] {
63 }
catch (
const std::exception&) {
68 auto removeDocumentCommand = [
this, path] { this->
removeDocument(path); };
77 tr(
"Supported Documents (*.json *.v *.sisl *.bin);;All Files (*)"),
78 [
this](
const QString& fileName,
const QByteArray& fileContent) {
81 fileName.toStdString(),
82 std::string(fileContent.constData(),
83 static_cast<std::size_t
>(fileContent.size())));
86 const auto sourcePath =
session.activeDocumentPath;
87 const auto importPath = document.getPath();
89 tr(
"Import Document"), tr(
"Document Import Error"));
90 }
catch (
const std::exception& error) {
93 tr(
"Failed to import the document:\n%1").arg(error.what()));
106 if (selection->path ==
session.activeDocumentPath)
109 const auto* document =
session.projectContext.documents().find(selection->path);
111 throw std::runtime_error(
"The selected document no longer exists");
113 const auto& contents = document->getContents();
118 QByteArray(contents.data(),
static_cast<qsizetype
>(contents.size())));
119 }
catch (
const std::exception& error) {
122 tr(
"Failed to export the document:\n%1").arg(error.what()));
134 dialog->setAttribute(Qt::WA_DeleteOnClose);
135 dialog->setWindowTitle(tr(
"New Code File"));
136 dialog->setModal(
true);
138 auto* form =
new QFormLayout(dialog);
139 auto* nameEdit =
new QLineEdit(tr(
"untitled"), dialog);
140 auto* typeBox =
new QComboBox(dialog);
143 typeBox->addItem(QString::fromUtf8(info.displayName),
static_cast<int>(info.type));
146 form->addRow(tr(
"Name"), nameEdit);
147 form->addRow(tr(
"File type"), typeBox);
150 new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dialog);
151 form->addRow(buttons);
152 connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
154 buttons, &QDialogButtonBox::accepted, dialog, [
this, dialog, nameEdit, typeBox] {
155 const auto index = typeBox->currentIndex();
161 const auto slug = nameEdit->text().trimmed().toStdString();
166 tr(
"The name must be non-empty and cannot contain path separators."));
170 if (
session.projectContext.documents().contains(path)) {
172 tr(
"A code file named '%1' already exists.")
173 .arg(QString::fromStdString(path)));
181 nameEdit->selectAll();
182 nameEdit->setFocus();
188 constexpr int MAXIMUM_BINARY_SIZE = 256 * 1024 * 1024;
191 dialog->setAttribute(Qt::WA_DeleteOnClose);
192 dialog->setWindowTitle(tr(
"New Binary File"));
193 dialog->setModal(
true);
195 auto* form =
new QFormLayout(dialog);
196 auto* nameEdit =
new QLineEdit(tr(
"untitled"), dialog);
197 auto* sizeEdit =
new QSpinBox(dialog);
198 sizeEdit->setRange(1, MAXIMUM_BINARY_SIZE);
199 sizeEdit->setValue(256);
200 sizeEdit->setSuffix(tr(
" bytes"));
202 form->addRow(tr(
"Name"), nameEdit);
203 form->addRow(tr(
"Size"), sizeEdit);
206 new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dialog);
207 form->addRow(buttons);
208 connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
210 buttons, &QDialogButtonBox::accepted, dialog, [
this, dialog, nameEdit, sizeEdit] {
211 const auto slug = nameEdit->text().trimmed().toStdString();
215 tr(
"The name must be non-empty and cannot contain path separators."));
221 if (
session.projectContext.documents().contains(path)) {
223 tr(
"A binary file named '%1' already exists.")
224 .arg(QString::fromStdString(path)));
228 const auto roundedSize =
229 std::bit_ceil(
static_cast<unsigned int>(sizeEdit->value()));
231 {path, std::string(
static_cast<std::size_t
>(roundedSize),
'\0')},
232 tr(
"Create Binary File"));
236 nameEdit->selectAll();
237 nameEdit->setFocus();
244 throw std::invalid_argument(
"createDocument requires a graphical document type");
248 dialogParent, tr(
"New %1").arg(noun), tr(
"%1 name").arg(noun), noun,
249 [
this, type, noun](
const QString& requestedName) {
250 const auto trimmed = requestedName.trimmed();
251 const auto displayName = trimmed.isEmpty() ? noun : trimmed;
253 session.projectContext, type, displayName);
264 QString commandText, std::vector<SILICON::project::Document> before,
265 std::string beforeActive, std::vector<SILICON::project::Document> after,
266 std::string afterActive)
269 std::move(commandText),
270 [
this, before = std::move(before), beforeActive = std::move(beforeActive)] {
273 [
this, after = std::move(after), afterActive = std::move(afterActive)] {
293 const auto title = tr(
"Rename %1").arg(noun);
295 dialogParent, title, tr(
"Name"), QString::fromStdString(*slug),
296 [
this, selection = *selection, noun, title](
const QString& requestedName) {
297 const auto newSlug = requestedName.trimmed().toStdString();
301 tr(
"The name must be non-empty and cannot contain path separators."));
306 if (newPath == selection.path)
308 if (
session.projectContext.documents().contains(newPath)) {
311 tr(
"A %1 named '%2' already exists.")
312 .arg(noun.toLower(), QString::fromStdString(newPath)));
318 const auto before =
session.projectContext.documents().getDocuments();
319 const auto beforeActive =
session.activeDocumentPath;
323 const auto afterActive =
324 beforeActive == selection.path ? newPath : beforeActive;
327 }
catch (
const std::exception& error) {
335 const std::vector<std::string>& paths,
const QString& commandText)
339 const auto before =
session.projectContext.documents().getDocuments();
341 std::erase_if(after, [&paths](
const auto& document) {
342 return std::ranges::find(paths, document.getPath()) != paths.end();
344 if (after.size() == before.size())
347 const auto beforeActive =
session.activeDocumentPath;
348 auto afterActive = beforeActive;
349 if (std::ranges::find(paths, beforeActive) != paths.end()) {
351 const auto architecture =
357 const auto prefix = architecture
360 const auto nextComponent =
362 ? std::ranges::find_if(after,
363 [&](
const auto& document) {
364 return document.getPath().starts_with(prefix);
367 if (nextComponent != after.end())
368 afterActive = nextComponent->getPath();
369 else if (
const auto circuit = std::ranges::find_if(
371 [](
const auto& document) {
374 circuit != after.end())
375 afterActive = circuit->getPath();
380 }
catch (
const std::exception& error) {
389 if (!selection || !item)
398 const auto circuitCount = std::ranges::count_if(
399 session.projectContext.documents().getDocuments(), [](
const auto& document) {
400 return document.getType() == SILICON::project::DocumentType::Circuit;
402 if (circuitCount <= 1)
407 const auto title = tr(
"Delete %1").arg(noun);
411 }
catch (
const std::exception& e) {
414 tr(
"Failed to save the active document before deleting it:\n%1").arg(e.what()));
419 const auto dependents =
420 session.projectContext.circuitDependencies().dependentsOf(selection->path);
421 if (!dependents.empty()) {
423 for (
const auto& dependent : dependents)
424 names.push_back(QString::fromStdString(dependent));
428 tr(
"This subcircuit is still used by:\n%1").arg(names.join(
'\n')));
434 dialogParent, title, tr(
"Delete %1 \"%2\"?").arg(noun.toLower(), item->text(0)),
435 [
this, path = selection->path, title] {
436 const auto& store = session.projectContext.documents();
437 const auto* storedDocument = store.find(path);
438 const auto storedIndex = store.indexOf(path);
439 if (!storedDocument || !storedIndex)
442 const auto document = *storedDocument;
443 const auto index = static_cast<std::ptrdiff_t>(*storedIndex);
445 auto removeStored = [this, path] { this->removeDocument(path); };
446 auto restoreDocument = [
this, document, index] {
447 this->insertDocument(document, index, true);
456 std::vector<SILICON::project::Document> documents,
const std::string& sourcePath,
457 const std::string& activatePath,
const QString& commandText,
458 const QString& errorTitle)
460 QStringList conflicts;
461 for (
const auto& document : documents) {
462 if (
session.projectContext.documents().contains(document.getPath()))
463 conflicts.push_back(QString::fromStdString(document.getPath()));
466 auto commit = [
this, documents = std::move(documents), sourcePath, activatePath,
467 commandText, errorTitle]()
mutable {
469 const auto& store =
session.projectContext.documents();
470 auto beforeDocuments = store.getDocuments();
471 auto afterDocuments = beforeDocuments;
473 for (
auto& document : documents) {
474 const auto existing = std::ranges::find(afterDocuments, document.getPath(),
476 if (existing == afterDocuments.end())
477 afterDocuments.push_back(std::move(document));
479 *existing = std::move(document);
486 std::move(afterDocuments), activatePath);
487 }
catch (
const std::exception& error) {
490 tr(
"Failed to update the project documents:\n%1").arg(error.what()));
494 if (!conflicts.empty()) {
497 tr(
"The following documents already exist and will be replaced:\n\n%1")
498 .arg(conflicts.join(
'\n')),
499 std::move(commit),
this);
Runtime-only graph of Circuit-to-Circuit instantiation dependencies.
void rebuildFromProject(const std::vector< Document > &documents)
Rebuilds the graph from a full project description.
const std::vector< Document > & getDocuments() const noexcept
const std::string & getPath() const noexcept
void renameDocument(std::string_view oldPath, std::string_view newPath)
void setDocuments(std::vector< Document > documents)
const DocumentStore & documents() const noexcept
void exportSelectedDocument()
void renameSelectedDocument()
void importProjectDocument()
void pushCreateDocumentCommand(SILICON::project::Document document, const QString &commandText)
void removeDocument(const std::string &path)
EditorWorkspace & workspace
void insertDocument(SILICON::project::Document document, std::optional< std::ptrdiff_t > insertAt, bool activate)
void pushDocumentSnapshotCommand(QString commandText, std::vector< SILICON::project::Document > before, std::string beforeActive, std::vector< SILICON::project::Document > after, std::string afterActive)
void renameArchitecture(const std::string &name)
void createDocument(SILICON::project::DocumentType type)
void commitDocumentChanges(std::vector< SILICON::project::Document > documents, const std::string &sourcePath, const std::string &activatePath, const QString &commandText, const QString &errorTitle)
ProjectTree * projectTree
void deleteSelectedDocument()
void restoreProjectDocuments(const std::vector< SILICON::project::Document > &documents, const std::string &activePath)
void deleteArchitecture(const std::string &name)
void removeProjectDocuments(const std::vector< std::string > &paths, const QString &commandText)
static std::string architectureName(const QTreeWidgetItem *item)
static ProjectTreeItemKind itemKind(const QTreeWidgetItem *item)
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
std::string documentFileName(const Document &document)
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< std::string > documentSlugForPath(const std::string_view path)
Document importDocument(const std::string_view sourcePath, std::string contents)
bool isValidDocumentSlug(const std::string_view slug)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
std::string architectureDirectory(const std::string_view name)
std::optional< QString > saveFileContent(QWidget *parent, const QString &caption, const QString &suggestedFileName, const QString &filter, const QByteArray &content)
void openFileContent(QWidget *parent, const QString &caption, const QString &filter, OpenContentCallback callback, QObject *context)
std::string uniqueDocumentPath(const SILICON::project::ProjectContext &context, const SILICON::project::DocumentType type, const QString &requestedName)
std::string emptyGraphicalDocumentJson(const SILICON::project::DocumentType type)
std::span< const CodeFilePresentation > codeFilePresentations()
QString documentTypeName(SILICON::project::DocumentType type)