29#include <QTemporaryFile>
31#include <nlohmann/json.hpp>
54using namespace SILICON::core;
57 const SILICON::logging::Logger
uiLog(
"ui");
76 const QString displayFileName = QFileInfo(
session.currentFileName).fileName();
78 if (!displayFileName.isEmpty())
79 dialogParent->setWindowTitle(QString(
"SILICON - %1").arg(displayFileName));
97 session.currentProjectMetadata.reset();
102 document.setContents(
workspace.circuitEditor().scene()->serialize());
103 session.projectContext.setDocuments({std::move(document)});
104 documents.notifyActiveDocumentActivated();
108 const QByteArray& fileContent)
110 uiLog.info(std::format(
"Opening {}", fileName.toStdString()));
113 QTemporaryFile archive;
114 if (!archive.open() || archive.write(fileContent) != fileContent.size()
116 throw std::runtime_error(
"Cannot stage the selected project archive");
118 const QString archivePath = archive.fileName();
123 session.currentProjectMetadata = std::move(projectFile.metadata);
125 session.currentProjectInfo = std::move(projectFile.project);
126 session.projectContext.setDocuments(std::move(projectFile.documents));
127 const auto initialCircuit =
session.firstCircuitPath();
129 throw std::runtime_error(
"Project has no circuit document");
130 session.activeDocumentPath = *initialCircuit;
132 const auto* document =
133 session.projectContext.documents().find(
session.activeDocumentPath);
135 throw std::runtime_error(
"Initial circuit payload is missing");
138 documents.notifyActiveDocumentActivated();
142 }
catch (
const nlohmann::json::exception& e) {
145 tr(
"The project contains invalid JSON data:\n%1").arg(e.what()));
146 }
catch (
const std::exception& e) {
149 tr(
"Failed to load the project:\n%1").arg(e.what()));
157 dialogParent, tr(
"Open Project"), tr(
"SILICON Project (*.sil);;All Files (*)"),
158 [
this](
const QString& fileName,
const QByteArray& fileContent) {
169 }
catch (
const std::exception& e) {
172 tr(
"Failed to serialize the active circuit:\n%1").arg(e.what()));
176 QString destinationFileName =
session.currentFileName;
177#ifndef __EMSCRIPTEN__
178 if (destinationFileName.isEmpty()) {
179 destinationFileName =
180 QFileDialog::getSaveFileName(
dialogParent, tr(
"Save Project"), QString(),
181 tr(
"SILICON Project (*.sil);;All Files (*)"));
182 if (destinationFileName.isEmpty())
186 if (destinationFileName.isEmpty())
187 destinationFileName = QStringLiteral(
"project.sil");
194 metadata.siliconVersion = SILICON_VERSION;
199 project.name = QFileInfo(destinationFileName).baseName().toStdString();
207 QTemporaryFile archive;
209 throw std::runtime_error(
"Cannot create a temporary project archive");
210 const QString archivePath = archive.fileName();
215 QFile archiveFile(archivePath);
216 if (!archiveFile.open(QIODevice::ReadOnly))
217 throw std::runtime_error(
"Cannot read the temporary project archive");
221 tr(
"SILICON Project (*.sil);;All Files (*)"), archiveFile.readAll());
229 session.currentProjectMetadata = std::move(metadata);
234 }
catch (
const std::exception& e) {
237 tr(
"Failed to save the project:\n%1").arg(e.what()));
251 tr(
"The current project has unsaved changes. Do you want to save them?"),
252 tr(
"Save"), tr(
"Discard"),
253 [
this, continuation =
Selects and hosts editors while preserving detached document preparation.
void confirmSaveIfDirty(std::function< void()> continuation)
void setFileName(const QString &fileName)
EditorWorkspace & workspace
void loadProjectContent(const QString &fileName, const QByteArray &fileContent)
ProjectDocumentController & documents
ProjectFileController(ProjectSession &session, EditorWorkspace &workspace, ProjectDocumentController &documents, QUndoStack &undoStack, QWidget *dialogParent)
std::string currentUtcTimestamp()
Returns the current UTC timestamp formatted as YYYY-MM-DDTHH\:MM\:SSZ.
constexpr int FORMAT_VERSION
Current version of the Silicon project archive schema.
ProjectFile readProjectFile(const std::filesystem::path &path)
Reads and validates a Silicon .sil project archive.
void writeProjectFile(const std::filesystem::path &path, const ProjectFile &projectFile)
Writes a Silicon .sil project archive.
ProjectMetadata metadataForNewFile()
Creates default metadata for a new project file.
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)
SILICON::project::Document defaultCircuitDocument()
std::string defaultCircuitPath()
SILICON::project::ProjectInfo defaultProjectInfo(const QString &fileName)
void ensureProjectDocuments(SILICON::project::ProjectContext &context)
const SILICON::logging::Logger uiLog("ui")
In-memory representation of a .sil project archive.
Project-level data stored in project.json.