SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
projectDocumentPolicy.cpp
Go to the documentation of this file.
1
#include "
projectDocumentPolicy.hpp
"
2
3
#include <cctype>
4
#include <format>
5
#include <stdexcept>
6
7
#include <QFileInfo>
8
#include <nlohmann/json.hpp>
9
10
#include <
ui/circuit/components/subcircuit/metadata.hpp
>
11
12
namespace
SILICON::ui::projectDocumentPolicy
{
13
14
std::string
defaultCircuitPath
()
15
{
16
return
std::string(
SILICON::project::DEFAULT_CIRCUIT_PATH
);
17
}
18
19
SILICON::project::Document
defaultCircuitDocument
()
20
{
21
return
{
defaultCircuitPath
(),
""
};
22
}
23
24
SILICON::project::ProjectInfo
defaultProjectInfo
(
const
QString& fileName)
25
{
26
const
QString baseName = QFileInfo(fileName).baseName();
27
return
{.name = (baseName.isEmpty() ? QStringLiteral(
"Untitled Project"
) : baseName)
28
.toStdString(),
29
.description =
""
};
30
}
31
32
void
ensureProjectDocuments
(
SILICON::project::ProjectContext
& context)
33
{
34
if
(!context.
documents
().
contains
(
SILICON::project::DocumentType::Circuit
)) {
35
context.
upsertDocument
(
36
{
defaultCircuitPath
(),
37
emptyGraphicalDocumentJson
(
SILICON::project::DocumentType::Circuit
)});
38
}
39
}
40
41
std::string
emptyGraphicalDocumentJson
(
const
SILICON::project::DocumentType
type)
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
49
if
(type ==
SILICON::project::DocumentType::Circuit
) {
50
scene[
"graphicalComponent"
] =
51
graphicalSubcircuitMetadataToJson
(
GraphicalSubcircuitMetadata
{});
52
}
53
54
return
scene.dump(2);
55
}
56
57
std::string
uniqueDocumentPath
(
const
SILICON::project::ProjectContext
& context,
58
const
SILICON::project::DocumentType
type,
59
const
QString& requestedName)
60
{
61
if
(
SILICON::project::categoryOf
(type) !=
SILICON::project::DocumentCategory::Diagram
)
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
SILICON::project::DocumentStore::contains
bool contains(std::string_view documentPath) const noexcept
Definition
projectDocument.cpp:267
SILICON::project::Document
Definition
projectDocument.hpp:109
SILICON::project::ProjectContext
Definition
projectContext.hpp:30
SILICON::project::ProjectContext::upsertDocument
void upsertDocument(Document document)
Definition
projectContext.cpp:115
SILICON::project::ProjectContext::documents
const DocumentStore & documents() const noexcept
Definition
projectContext.cpp:96
metadata.hpp
SILICON::project::documentPathForSlug
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
Definition
projectDocument.cpp:191
SILICON::project::DocumentCategory::Diagram
@ Diagram
Definition
projectDocument.hpp:39
SILICON::project::categoryOf
constexpr DocumentCategory categoryOf(const DocumentType type)
Definition
projectDocument.hpp:81
SILICON::project::DocumentType
DocumentType
Definition
projectDocument.hpp:37
SILICON::project::DocumentType::Circuit
@ Circuit
Definition
projectDocument.hpp:37
SILICON::project::DEFAULT_CIRCUIT_PATH
const std::string DEFAULT_CIRCUIT_PATH
Default path of the initial circuit JSON file.
Definition
projectFile.hpp:60
SILICON::ui::projectDocumentPolicy
Definition
projectDocumentPolicy.cpp:12
SILICON::ui::projectDocumentPolicy::defaultCircuitDocument
SILICON::project::Document defaultCircuitDocument()
Definition
projectDocumentPolicy.cpp:19
SILICON::ui::projectDocumentPolicy::defaultCircuitPath
std::string defaultCircuitPath()
Definition
projectDocumentPolicy.cpp:14
SILICON::ui::projectDocumentPolicy::defaultProjectInfo
SILICON::project::ProjectInfo defaultProjectInfo(const QString &fileName)
Definition
projectDocumentPolicy.cpp:24
SILICON::ui::projectDocumentPolicy::uniqueDocumentPath
std::string uniqueDocumentPath(const SILICON::project::ProjectContext &context, const SILICON::project::DocumentType type, const QString &requestedName)
Definition
projectDocumentPolicy.cpp:57
SILICON::ui::projectDocumentPolicy::emptyGraphicalDocumentJson
std::string emptyGraphicalDocumentJson(const SILICON::project::DocumentType type)
Definition
projectDocumentPolicy.cpp:41
SILICON::ui::projectDocumentPolicy::ensureProjectDocuments
void ensureProjectDocuments(SILICON::project::ProjectContext &context)
Definition
projectDocumentPolicy.cpp:32
SILICON::ui::graphicalSubcircuitMetadataToJson
nlohmann::ordered_json graphicalSubcircuitMetadataToJson(const GraphicalSubcircuitMetadata &metadata)
Serializes graphical metadata to the JSON shape/port object.
Definition
metadata.cpp:324
projectDocumentPolicy.hpp
SILICON::project::ProjectInfo
Project-level data stored in project.json.
Definition
projectFile.hpp:80
SILICON::ui::GraphicalSubcircuitMetadata
Visual metadata used to render a subcircuit as a reusable component.
Definition
metadata.hpp:74
src
ui
project
projectDocumentPolicy.cpp
Generated by
1.17.0