SILICON
Loading...
Searching...
No Matches
graphicalSubcircuit.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19
21
22#include <algorithm>
23#include <memory>
24#include <utility>
25
26#include <QScopedValueRollback>
27
29
30namespace SILICON::ui {
31 using namespace SILICON::core;
32
33 namespace {
34
35 [[nodiscard]] std::vector<PortPair>
36 portPairs(const std::vector<GraphicalSubcircuitPortMetadata>& ports)
37 {
38 std::vector<PortPair> result;
39 result.reserve(ports.size());
40 for (const auto& port : ports)
41 result.emplace_back(QString::fromStdString(port.name), port.position);
42 return result;
43 }
44
45 } // namespace
46
48 QGraphicsItem* parent, const SILICON::project::DocumentStore* documents,
49 const CircuitResolver* resolver)
51 std::make_shared<SubcircuitComponent>(resolver),
52 new SubcircuitRectShape(GraphicalSubcircuitMetadata{}.widthHeight), parent,
53 false),
55 {
56 printPortNames = true;
57
60 }
61
63 {
64 if (!documents || registryListenerId != 0)
65 return;
67 documents->addListener([this](const SILICON::project::DocumentChange& change) {
68 const auto slug = currentSlug();
69 const bool affectsConfiguredDocument =
71 && *change.path
75 || affectsConfiguredDocument) {
76 {
77 const QScopedValueRollback guard(refreshSuppressed, true);
78 if (const auto subcircuit =
79 std::dynamic_pointer_cast<SubcircuitComponent>(associatedComponent))
80 subcircuit->reloadFromResolver();
81 }
83 }
84 });
85 }
86
93
95 std::string slug, QGraphicsItem* parent,
97 : GraphicalSubcircuitComponent(parent, documents, resolver)
98 {
99 if (!slug.empty())
100 applyProperty("slug", std::move(slug));
101 }
102
107
109 {
110 {
111 const QScopedValueRollback guard(refreshSuppressed, true);
113 }
115 }
116
118 const SILICON::project::DocumentStore* newDocuments,
119 const CircuitResolver* newResolver)
120 {
121 {
122 const QScopedValueRollback guard(refreshSuppressed, true);
123 if (documents != newDocuments) {
125 documents = newDocuments;
127 }
128 if (const auto subcircuit =
129 std::dynamic_pointer_cast<SubcircuitComponent>(associatedComponent))
130 subcircuit->setCircuitResolver(newResolver);
131 }
133 }
134
136 const PropertyValue& value)
137 {
138 {
139 const QScopedValueRollback guard(refreshSuppressed, true);
141 }
142 if (key == "slug")
144 }
145
147 {
149 return {};
150 return associatedComponent->getPropertyValue<std::string>("slug").value_or(
151 std::string());
152 }
153
154 void
166
167 void
169 {
170 const QScopedValueRollback guard(refreshSuppressed, true);
171 prepareGeometryChange();
173 if (metadata.inputs.empty() && metadata.outputs.empty())
174 clearPorts();
175 else
176 setPorts(portPairs(metadata.inputs), portPairs(metadata.outputs));
178 update();
179 }
180
182 {
184 return;
185
186 const auto& componentInputs = associatedComponent->inputBuses();
187 const auto& componentOutputs = associatedComponent->outputBuses();
188
189 if (inputPorts.size() != componentInputs.size()
190 || outputPorts.size() != componentOutputs.size()) {
192 return;
193 }
194
196 }
197
199 {
201 return;
202 const QScopedValueRollback guard(refreshSuppressed, true);
203 const auto slug = currentSlug();
204 if (slug.empty() || !documents) {
205 applyMetadata({});
206 return;
207 }
208
209 const auto* document = documents->find(SILICON::project::documentPathForSlug(
211 if (!document) {
213 return;
214 }
215
216 const auto json = nlohmann::json::parse(document->getContents(), nullptr, false);
217 const auto subcircuit =
218 std::dynamic_pointer_cast<SubcircuitComponent>(associatedComponent);
220 json,
222 subcircuit ? subcircuit->circuitResolver() : nullptr);
223
225 const auto& componentInputs = associatedComponent->inputBuses();
226 const auto& componentOutputs = associatedComponent->outputBuses();
227
228 if (metadata.inputs.size() != componentInputs.size())
229 metadata.inputs =
230 synchronizePortsWithBuses(metadata.inputs, componentInputs, metadata, true);
231 if (metadata.outputs.size() != componentOutputs.size())
232 metadata.outputs = synchronizePortsWithBuses(metadata.outputs, componentOutputs,
233 metadata, false);
234 }
235
236 applyMetadata(metadata);
237 }
238
240 {
243 metadata.widthHeight =
245 associatedComponent->outputBuses().size());
246 metadata.inputs = synchronizePortsWithBuses({}, associatedComponent->inputBuses(),
247 metadata, true);
248 metadata.outputs = synchronizePortsWithBuses({}, associatedComponent->outputBuses(),
249 metadata, false);
250
251 if (const auto imported =
252 std::dynamic_pointer_cast<SubcircuitComponent>(associatedComponent)) {
253 const auto& inputNames = imported->importedInputNames();
254 for (std::size_t i = 0; i < std::min(metadata.inputs.size(), inputNames.size());
255 ++i)
256 metadata.inputs[i].name = inputNames[i];
257 const auto& outputNames = imported->importedOutputNames();
258 for (std::size_t i = 0; i < std::min(metadata.outputs.size(), outputNames.size());
259 ++i)
260 metadata.outputs[i].name = outputNames[i];
261 }
262 }
263 applyMetadata(metadata);
264 }
265
266} // namespace SILICON::ui
void clearPorts()
Removes existing input/output ports and their line items.
std::vector< Port * > outputPorts
Output connection ports.
bool printPortNames
Whether to print port names.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
Handles double-clicks not consumed by a component-specific action.
bool openDocumentOnDoubleClick(QGraphicsSceneMouseEvent *event, const std::string &path)
Queues document navigation for a left double-click in normal mode.
std::vector< Port * > inputPorts
Input connection ports.
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
virtual void updatePortSizes()
Copies associated component bus sizes into graphical ports.
void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs) override
Sets ports and validates against component I/O.
void applyProperty(std::string_view key, const PropertyValue &value) override
Applies a component property while notifying Qt about possible geometry changes.
GraphicalLogicComponent(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent, bool scanShape=false)
virtual void setComponent(const Component_ptr &component)
Sets the associated logical component.
Component_ptr associatedComponent
The logical component this graphical component represents.
void setProjectContext(const SILICON::project::DocumentStore *documents, const CircuitResolver *resolver)
Attaches project metadata and resolver in one refresh.
~GraphicalSubcircuitComponent() override
Unregisters the subcircuit registry listener.
const SILICON::project::DocumentStore * documents
void applyProperty(std::string_view key, const PropertyValue &value) override
Applies a component property and refreshes metadata when the slug changes.
void useAttachedInterfaceMetadata()
Uses default metadata derived only from the attached imported buses.
void applyMetadata(const GraphicalSubcircuitMetadata &metadata)
Applies parsed metadata to the rendered shape and ports.
void setComponent(const Component_ptr &component) override
Replaces the associated core subcircuit component.
void refreshFromMetadata()
Reloads shape and ports from the active registry document.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
void updatePortSizes() override
Rebuilds visible ports when subcircuit I/O shape changes.
GraphicalSubcircuitComponent(QGraphicsItem *parent=nullptr, const SILICON::project::DocumentStore *documents=nullptr, const CircuitResolver *resolver=nullptr)
Creates a graphical subcircuit with an empty slug.
std::string currentSlug() const
Returns the slug stored by the associated core component.
RTLIL::SigSpec port(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key, int expected)
std::shared_ptr< Component > Component_ptr
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
bool isValidDocumentSlug(const std::string_view slug)
QSize defaultGraphicalSubcircuitSize(std::size_t inputCount, std::size_t outputCount)
Default square dimensions for the boundary port counts, in pixels.
Definition metadata.cpp:215
std::optional< GraphicalSubcircuitMetadata > parseGraphicalSubcircuitMetadata(const nlohmann::json &document)
Parses graphical subcircuit metadata from a scene document.
Definition metadata.cpp:224
GraphicalSubcircuitMetadata synchronizeGraphicalSubcircuitMetadata(const nlohmann::json &document, const GraphicalSubcircuitMetadata &fallbackMetadata, const CircuitResolver *resolver)
Reconciles saved graphical metadata with the current circuit document.
Definition metadata.cpp:276
std::vector< GraphicalSubcircuitPortMetadata > synchronizePortsWithBuses(const std::vector< GraphicalSubcircuitPortMetadata > &fallbackPorts, const std::vector< Bus > &buses, const GraphicalSubcircuitMetadata &metadata, const bool inputPort)
Reconciles metadata when a loaded subcircuit component has a different bus count than its saved graph...
Definition metadata.cpp:298
std::optional< std::string > path
Visual metadata used to render a subcircuit as a reusable component.
Definition metadata.hpp:74
std::vector< GraphicalSubcircuitPortMetadata > inputs
Input boundary ports in core interface order.
Definition metadata.hpp:83
std::vector< GraphicalSubcircuitPortMetadata > outputs
Output boundary ports in core interface order.
Definition metadata.hpp:85
QSize widthHeight
Rectangle dimensions in pixels.
Definition metadata.hpp:76