23#include <unordered_set>
26#include <nlohmann/json.hpp>
40using namespace SILICON::core;
55class Port :
public QGraphicsItem {
66 QGraphicsLineItem*
line =
nullptr;
84 [[nodiscard]] QTransform
labelTransform(
const QRectF& rect,
bool insideShape)
const;
97 QGraphicsItem* parent =
nullptr);
100 void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
101 QWidget* widget)
override;
107 unsigned int getIndex()
const {
return this->index; }
113 [[nodiscard]] QString
getName()
const {
return this->name; }
116 unsigned int getSize()
const {
return this->size; }
122 void setSize(
unsigned int newSize);
160 [[nodiscard]] std::optional<PropertyValue>
getProperty(std::string_view key)
const;
161 [[nodiscard]]
virtual std::optional<std::reference_wrapper<const StringPropertyOptions>>
172 [[nodiscard]]
virtual std::span<const ResizeHandle>
resizeHandles()
const {
return {}; }
179 [[nodiscard]] std::optional<ResizeHandle>
resizeHandleAt(QPointF localPos)
const;
198 std::unordered_set<std::string, TransparentStringHash, std::equal_to<>>
219 void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
220 QWidget* widget)
override;
226 [[nodiscard]]
bool canRotate()
const override {
return true; }
228 QVariant
itemChange(GraphicsItemChange change,
const QVariant& value)
override;
239 const std::string& path);
266 QGraphicsItem* parent =
nullptr,
bool scanShape =
false);
283 virtual void setPorts(
const std::vector<PortPair>& busToPortInputs,
284 const std::vector<PortPair>& busToPortOutputs);
298 return std::array{std::span<Port* const>(
inputPorts),
307 [[nodiscard]]
virtual std::string
getTypeName()
const;
313 [[nodiscard]]
virtual nlohmann::ordered_json
serialize()
const;
327 static std::unique_ptr<GraphicalComponent>
deserialize(
const nlohmann::json& j,
Factory for creating graphical circuit components.
QRectF getCollisionRect() const override
Gets physical body and port line bounds for drag/drop.
virtual PropertyMap resizedProperties(QSizeF size, ResizeHandle handle, const PropertyMap &initial)
ComponentResizeState resizeState() const
void setPortLine(Port *port)
Creates the connecting line from port to shape.
void clearPorts()
Removes existing input/output ports and their line items.
void rotate()
Rotates the component by 90 degrees.
void prepareSceneBoundsChange()
Prepares a geometry edit and checks its final bounds on the event loop.
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
std::optional< PropertyValue > getProperty(std::string_view key) const
void applyResizeState(const ComponentResizeState &state)
GraphicalComponent(ItemCategory category, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
virtual void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs)
Sets input and output ports.
virtual bool participatesInCollisions() const
Annotations remain selectable but do not obstruct placement or routing.
const std::vector< Port * > & getOutputPorts() const
Borrows output ports; copy before replacing the component ports.
std::vector< Port * > outputPorts
Output connection ports.
virtual nlohmann::ordered_json serialize() const
Serializes the component to JSON.
virtual const PropertyMap & getProperties() const
Properties exposed by graphical and simulation components alike.
QRectF boundingRectWithoutMargins() const
Gets bounding rectangle without margins.
bool canRotate() const override
Components can rotate by default.
QGraphicsItem * itemShape
The visual shape item.
std::optional< ResizeHandle > resizeHandleAt(QPointF localPos) const
bool resizeHandlesVisible() const
virtual void applyProperty(std::string_view key, const PropertyValue &value)
QPoint scanImage(const QImage &image, PortSide side, QPoint initialPoint) const
Scans image for first non-transparent pixel.
virtual std::string getTypeName() const
Gets the component type name for serialization.
std::unordered_set< std::string, TransparentStringHash, std::equal_to<> > hiddenProperties
Property names hidden from the panel, still accessible and serialized. Add names in subclasses with h...
QRectF collisionRectForWires() const
Gets the collision rectangle extended for wire connections.
const std::vector< Port * > & getInputPorts() const
Borrows input ports; copy before replacing the component ports.
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override
QGraphicsItem * getItemShape() const
Gets the component shape.
bool printPortNames
Whether to print port names.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
Handles double-clicks not consumed by a component-specific action.
void modeChanged(InteractionMode mode) override
Called when the interaction mode changes.
virtual std::span< const ResizeHandle > resizeHandles() const
bool isPropertyHidden(std::string_view key) const
Whether a property is omitted from the properties panel.
virtual bool isMultilineProperty(std::string_view key) const
auto getPorts() const
Borrows every port, inputs first, without copying or deduplicating.
QRectF boundingRect() const override
Gets bounding rectangle with selection margin.
virtual void loadSerializedState(const nlohmann::json &j)
Loads subclass-specific serialized state.
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Paints the component (selection outline).
virtual std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions(std::string_view key) const
virtual QRectF resizeBounds() const
bool openDocumentOnDoubleClick(QGraphicsSceneMouseEvent *event, const std::string &path)
Queues document navigation for a left double-click in normal mode.
static std::unique_ptr< GraphicalComponent > deserialize(const nlohmann::json &j, GUIComponentFactory &factory)
Deserializes a component from JSON using the factory.
std::vector< Port * > inputPorts
Input connection ports.
bool scanShape
Whether to scan shape alpha for port placement.
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
GraphicalItem(ItemCategory category=ItemCategory::GraphicalItem, QGraphicsItem *parent=nullptr)
Represents a connection point on a component.
bool printName
Whether to write the port name on the component.
QPoint getPosition() const
Gets the port position.
Port(unsigned int index, QPoint position, QString name, bool printName=false, QGraphicsItem *parent=nullptr)
Constructs a port.
QTransform labelTransform(const QRectF &rect, bool insideShape) const
QGraphicsLineItem * line
Visual line connecting to component.
void prepareBoundsChange()
bool inputAssignmentError
Whether the net associated with this port has an error.
unsigned int index
Port index in the port list.
PortSide side
Direction of the connecting line from port to component.
PortSide getDirection() const
Gets the port line direction.
unsigned int getIndex() const
Gets the port index.
QPoint position
Position relative to component center.
QString getName() const
Gets the port name.
friend class GraphicalComponent
unsigned int size
Number of wires represented by this port.
QRectF collisionRect() const
Gets the collision rectangle for wire detection.
QRectF boundingRect() const override
Gets visual bounds, including port labels and stroked markers.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Paints port names, bus markers, and input assignment errors.
void setInputAssignmentError(bool failed)
Marks whether this port failed input assignment during simulation setup.
QLineF connectionLine() const
void setLine(QGraphicsLineItem *line)
Sets the connecting line.
void setSize(unsigned int newSize)
Sets the port bus size.
unsigned int getSize() const
Gets the port bus size.
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::unordered_map< std::string, PropertyValue, TransparentStringHash, TransparentStringEqual > PropertyMap
Map from property names to their values.
std::pair< QString, QPoint > PortPair
DiagramScene::InteractionMode InteractionMode
Alias for DiagramScene::InteractionMode.