SILICON
Loading...
Searching...
No Matches
projectDocumentPolicy.cpp
Go to the documentation of this file.
2
3#include <cctype>
4#include <format>
5#include <stdexcept>
6
7#include <QFileInfo>
8#include <nlohmann/json.hpp>
9
11
13
14std::string defaultCircuitPath()
15{
17}
18
23
25{
26 const QString baseName = QFileInfo(fileName).baseName();
27 return {.name = (baseName.isEmpty() ? QStringLiteral("Untitled Project") : baseName)
28 .toStdString(),
29 .description = ""};
30}
31
40
42{
43 nlohmann::ordered_json scene;
44 scene["circuit"] = nlohmann::ordered_json{
45 {"version", SILICON_VERSION}, {"components", nlohmann::ordered_json::array()}};
46 scene["visual"]["components"] = nlohmann::ordered_json::array();
47 scene["visual"]["wires"] = nlohmann::ordered_json::array();
48
50 scene["graphicalComponent"] =
52 }
53
54 return scene.dump(2);
55}
56
59 const QString& requestedName)
60{
62 throw std::invalid_argument("Only graphical documents use generated slugs");
63
64 const auto fallback = std::string("circuit");
65 auto slug = requestedName.trimmed().toStdString();
66 if (slug.empty())
67 slug = fallback;
68
69 for (char& ch : slug) {
70 const auto byte = static_cast<unsigned char>(ch);
71 if (std::isalnum(byte))
72 ch = static_cast<char>(std::tolower(byte));
73 else if (ch != '-' && ch != '_')
74 ch = '_';
75 }
76
77 if (const auto first = slug.find_first_not_of('_'); first == std::string::npos) {
78 slug = fallback;
79 } else {
80 slug = slug.substr(first, slug.find_last_not_of('_') - first + 1);
81 }
82
83 const auto& store = context.documents();
84 auto candidate = SILICON::project::documentPathForSlug(type, slug);
85 for (int suffix = 2; store.contains(candidate); ++suffix)
86 candidate =
87 SILICON::project::documentPathForSlug(type, std::format("{}-{}", slug, suffix));
88
89 return candidate;
90}
91
92} // namespace SILICON::ui::projectDocumentPolicy
bool contains(std::string_view documentPath) const noexcept
void upsertDocument(Document document)
const DocumentStore & documents() const noexcept
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
constexpr DocumentCategory categoryOf(const DocumentType type)
const std::string DEFAULT_CIRCUIT_PATH
Default path of the initial circuit JSON file.
SILICON::project::Document defaultCircuitDocument()
SILICON::project::ProjectInfo defaultProjectInfo(const QString &fileName)
std::string uniqueDocumentPath(const SILICON::project::ProjectContext &context, const SILICON::project::DocumentType type, const QString &requestedName)
std::string emptyGraphicalDocumentJson(const SILICON::project::DocumentType type)
void ensureProjectDocuments(SILICON::project::ProjectContext &context)
nlohmann::ordered_json graphicalSubcircuitMetadataToJson(const GraphicalSubcircuitMetadata &metadata)
Serializes graphical metadata to the JSON shape/port object.
Definition metadata.cpp:324
Project-level data stored in project.json.
Visual metadata used to render a subcircuit as a reusable component.
Definition metadata.hpp:74