SILICON
Loading...
Searching...
No Matches
GraphicalComponent Class Reference

Base class for graphical circuit components. More...

#include <graphicalComponent.hpp>

Inheritance diagram for GraphicalComponent:
Collaboration diagram for GraphicalComponent:

Public Member Functions

virtual const PropertyMap & getProperties () const
 Properties exposed by graphical and simulation components alike.
std::optional< PropertyValue > getProperty (std::string_view key) const
virtual std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions (std::string_view key) const
virtual bool isMultilineProperty (std::string_view key) const
bool isPropertyHidden (std::string_view key) const
 Whether a property is omitted from the properties panel.
virtual void applyProperty (std::string_view key, const PropertyValue &value)
virtual bool participatesInCollisions () const
 Annotations remain selectable but do not obstruct placement or routing.
virtual std::span< const ResizeHandle > resizeHandles () const
virtual QRectF resizeBounds () const
virtual PropertyMap resizedProperties (QSizeF size, ResizeHandle handle, const PropertyMap &initial)
std::optional< ResizeHandle > resizeHandleAt (QPointF localPos) const
bool resizeHandlesVisible () const
ComponentResizeState resizeState () const
void applyResizeState (const ComponentResizeState &state)
void modeChanged (InteractionMode mode) override
 Called when the interaction mode changes.
void prepareSceneBoundsChange ()
 Prepares a geometry edit and checks its final bounds on the event loop.
QRectF collisionRectForWires () const
 Gets the collision rectangle extended for wire connections.
 GraphicalComponent (QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
void rotate ()
 Rotates the component by 90 degrees.
virtual void setPorts (const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs)
 Sets input and output ports.
const std::vector< Port * > & getInputPorts () const
 Borrows input ports; copy before replacing the component ports.
const std::vector< Port * > & getOutputPorts () const
 Borrows output ports; copy before replacing the component ports.
auto getPorts () const
 Borrows every port, inputs first, without copying or deduplicating.
virtual std::string getTypeName () const
 Gets the component type name for serialization.
virtual nlohmann::ordered_json serialize () const
 Serializes the component to JSON.
virtual void loadSerializedState (const nlohmann::json &j)
 Loads subclass-specific serialized state.
Public Member Functions inherited from GraphicalItem
 GraphicalItem (ItemCategory category=ItemCategory::GraphicalItem, QGraphicsItem *parent=nullptr)
 ~GraphicalItem () override
void setCollidingStatus (CollidingStatus newStatus)
 Sets the collision status.
QVariant itemChange (GraphicsItemChange change, const QVariant &value) override
 Handles item change events.
int type () const override
 Returns the item type identifier.
bool isColliding () const
 Checks if the item is colliding.
QPointF getInitialPosition () const
 Gets the original position of the item before it started moving.
qreal getInitialRotation () const
uint64_t getUiId () const
void setInitialPosition ()
 Sets the initialPosition of the item to its current position (should be used when placed).
void setInitialRotation ()
void setUiId (uint64_t newUiId)
void setOwningScene (DiagramScene *scene)
void setGeometryPosition (QPointF position)
virtual void updateTopology ()

Static Public Member Functions

static std::unique_ptr< GraphicalComponent > deserialize (const nlohmann::json &j, GUIComponentFactory &factory)
 Deserializes a component from JSON using the factory.
Static Public Member Functions inherited from GraphicalItem
static uint64_t generateUiId ()

Protected Member Functions

void setItemShape (QGraphicsItem *shape)
 Sets the component shape graphics item.
QGraphicsItem * getItemShape () const
 Gets the component shape.
QRectF boundingRect () const override
 Gets bounding rectangle with selection margin.
QRectF boundingRectWithoutMargins () const
 Gets bounding rectangle without margins.
void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
 Paints the component (selection outline).
QRectF getCollisionRect () const override
 Gets physical body and port line bounds for drag/drop.
bool canRotate () const override
 Components can rotate by default.
QVariant itemChange (GraphicsItemChange change, const QVariant &value) override
void mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event) override
 Handles double-clicks not consumed by a component-specific action.
void hoverMoveEvent (QGraphicsSceneHoverEvent *event) override
void hoverLeaveEvent (QGraphicsSceneHoverEvent *event) override
bool openDocumentOnDoubleClick (QGraphicsSceneMouseEvent *event, const std::string &path)
 Queues document navigation for a left double-click in normal mode.
void setPortLine (Port *port)
 Creates the connecting line from port to shape.
void clearPorts ()
 Removes existing input/output ports and their line items.
 GraphicalComponent (ItemCategory category, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
Protected Member Functions inherited from GraphicalItem
virtual void onPositionChanged (QPointF offset)
 Called when the item position changes.
CollidingStatus getCollidingStatus () const
 Gets the current collision status.
void mouseReleaseEvent (QGraphicsSceneMouseEvent *event) override
void mousePressEvent (QGraphicsSceneMouseEvent *event) override

Protected Attributes

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 hiddenProperties.insert("propertyName").
std::vector< Port * > inputPorts
 Input connection ports.
std::vector< Port * > outputPorts
 Output connection ports.
bool boundsUpdatePending = false
bool scanShape = false
 Whether to scan shape alpha for port placement.
bool printPortNames = false
 Whether to print port names.

Private Member Functions

void setPortLine (Port *port, QImage &shapeImage)
 Reuses a shape image while building a batch of port lines.
QPoint scanImage (const QImage &image, PortSide side, QPoint initialPoint) const
 Scans image for first non-transparent pixel.

Private Attributes

QGraphicsItem * itemShape = nullptr
 The visual shape item.

Friends

class Port

Detailed Description

Base class for graphical circuit components.

GraphicalComponent is the base class for all visually rendered circuit components in the diagram. It manages the component shape, input/output ports, rotation, and property editing.

Ports are positioned relative to the component and can be automatically detected via alpha-scanning the shape image.

See also
GraphicalLogicComponent
Port

Definition at line 154 of file graphicalComponent.hpp.

Constructor & Destructor Documentation

◆ GraphicalComponent() [1/2]

GraphicalComponent ( ItemCategory category,
QGraphicsItem * shape,
QGraphicsItem * parent = nullptr,
bool scanShape = false )
explicitprotected

Definition at line 209 of file graphicalComponent.cpp.

◆ GraphicalComponent() [2/2]

GraphicalComponent ( QGraphicsItem * shape,
QGraphicsItem * parent = nullptr,
bool scanShape = false )
explicit

Definition at line 165 of file graphicalComponent.cpp.

Member Function Documentation

◆ applyProperty()

void applyProperty ( std::string_view key,
const PropertyValue & value )
virtual

◆ applyResizeState()

void applyResizeState ( const ComponentResizeState & state)

Definition at line 309 of file graphicalComponent.cpp.

◆ boundingRect()

QRectF boundingRect ( ) const
nodiscardoverrideprotected

Gets bounding rectangle with selection margin.

Definition at line 263 of file graphicalComponent.cpp.

◆ boundingRectWithoutMargins()

QRectF boundingRectWithoutMargins ( ) const
nodiscardprotected

Gets bounding rectangle without margins.

Definition at line 355 of file graphicalComponent.cpp.

◆ canRotate()

bool canRotate ( ) const
inlinenodiscardoverrideprotectedvirtual

Components can rotate by default.

Reimplemented from GraphicalItem.

Definition at line 226 of file graphicalComponent.hpp.

◆ clearPorts()

void clearPorts ( )
protected

Removes existing input/output ports and their line items.

Definition at line 482 of file graphicalComponent.cpp.

◆ collisionRectForWires()

QRectF collisionRectForWires ( ) const
nodiscard

Gets the collision rectangle extended for wire connections.

Returns
Collision rectangle including ports

Definition at line 383 of file graphicalComponent.cpp.

◆ deserialize()

std::unique_ptr< GraphicalComponent > deserialize ( const nlohmann::json & j,
GUIComponentFactory & factory )
static

Deserializes a component from JSON using the factory.

Parameters
jJSON object
factoryComponent factory for creating instances
Returns
Unique pointer to deserialized component

Definition at line 825 of file graphicalComponent.cpp.

◆ getCollisionRect()

QRectF getCollisionRect ( ) const
nodiscardoverrideprotectedvirtual

Gets physical body and port line bounds for drag/drop.

Implements GraphicalItem.

Definition at line 415 of file graphicalComponent.cpp.

◆ getInputPorts()

const std::vector< Port * > & getInputPorts ( ) const
inlinenodiscard

Borrows input ports; copy before replacing the component ports.

Definition at line 287 of file graphicalComponent.hpp.

◆ getItemShape()

QGraphicsItem * getItemShape ( ) const
inlinenodiscardprotected

Gets the component shape.

Definition at line 208 of file graphicalComponent.hpp.

◆ getOutputPorts()

const std::vector< Port * > & getOutputPorts ( ) const
inlinenodiscard

Borrows output ports; copy before replacing the component ports.

Definition at line 290 of file graphicalComponent.hpp.

◆ getPorts()

auto getPorts ( ) const
inlinenodiscard

Borrows every port, inputs first, without copying or deduplicating.

Note
Replacing ports invalidates the view. The component must outlive it.

Definition at line 296 of file graphicalComponent.hpp.

◆ getProperties()

const PropertyMap & getProperties ( ) const
nodiscardvirtual

Properties exposed by graphical and simulation components alike.

Reimplemented in GraphicalLogicComponent, and GraphicalText.

Definition at line 171 of file graphicalComponent.cpp.

◆ getProperty()

std::optional< PropertyValue > getProperty ( std::string_view key) const
nodiscard

Definition at line 177 of file graphicalComponent.cpp.

◆ getStringPropertyOptions()

std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions ( std::string_view key) const
nodiscardvirtual

Reimplemented in GraphicalLogicComponent, and GraphicalText.

Definition at line 186 of file graphicalComponent.cpp.

◆ getTypeName()

std::string getTypeName ( ) const
nodiscardvirtual

Gets the component type name for serialization.

Returns
String representing the component type

Reimplemented in GraphicalBusInput, GraphicalBusOutput, GraphicalInput, GraphicalLogicComponent, GraphicalOutputSingle, and GraphicalText.

Definition at line 811 of file graphicalComponent.cpp.

◆ hoverLeaveEvent()

void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event)
overrideprotected

Definition at line 342 of file graphicalComponent.cpp.

◆ hoverMoveEvent()

void hoverMoveEvent ( QGraphicsSceneHoverEvent * event)
overrideprotected

Definition at line 318 of file graphicalComponent.cpp.

◆ isMultilineProperty()

bool isMultilineProperty ( std::string_view key) const
nodiscardvirtual

Reimplemented in GraphicalText.

Definition at line 192 of file graphicalComponent.cpp.

◆ isPropertyHidden()

bool isPropertyHidden ( std::string_view key) const
nodiscard

Whether a property is omitted from the properties panel.

Definition at line 198 of file graphicalComponent.cpp.

◆ itemChange()

QVariant itemChange ( GraphicsItemChange change,
const QVariant & value )
overrideprotected

Definition at line 400 of file graphicalComponent.cpp.

◆ loadSerializedState()

void loadSerializedState ( const nlohmann::json & j)
virtual

Loads subclass-specific serialized state.

Parameters
jJSON object previously produced by serialize()

Reimplemented in GraphicalText.

Definition at line 821 of file graphicalComponent.cpp.

◆ modeChanged()

void modeChanged ( InteractionMode mode)
overridevirtual

Called when the interaction mode changes.

Parameters
modeThe new interaction mode

Reimplemented from GraphicalItem.

Definition at line 348 of file graphicalComponent.cpp.

◆ mouseDoubleClickEvent()

void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event)
overrideprotected

Handles double-clicks not consumed by a component-specific action.

Definition at line 426 of file graphicalComponent.cpp.

◆ openDocumentOnDoubleClick()

bool openDocumentOnDoubleClick ( QGraphicsSceneMouseEvent * event,
const std::string & path )
protected

Queues document navigation for a left double-click in normal mode.

Definition at line 431 of file graphicalComponent.cpp.

◆ paint()

void paint ( QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget )
overrideprotected

Paints the component (selection outline).

Definition at line 360 of file graphicalComponent.cpp.

◆ participatesInCollisions()

virtual bool participatesInCollisions ( ) const
inlinenodiscardvirtual

Annotations remain selectable but do not obstruct placement or routing.

Reimplemented in GraphicalText.

Definition at line 169 of file graphicalComponent.hpp.

◆ prepareSceneBoundsChange()

void prepareSceneBoundsChange ( )

Prepares a geometry edit and checks its final bounds on the event loop.

Definition at line 224 of file graphicalComponent.cpp.

◆ resizeBounds()

virtual QRectF resizeBounds ( ) const
inlinenodiscardvirtual

Definition at line 173 of file graphicalComponent.hpp.

◆ resizedProperties()

PropertyMap resizedProperties ( QSizeF size,
ResizeHandle handle,
const PropertyMap & initial )
nodiscardvirtual

Reimplemented in GraphicalText.

Definition at line 269 of file graphicalComponent.cpp.

◆ resizeHandleAt()

std::optional< ResizeHandle > resizeHandleAt ( QPointF localPos) const
nodiscard

Definition at line 283 of file graphicalComponent.cpp.

◆ resizeHandles()

virtual std::span< const ResizeHandle > resizeHandles ( ) const
inlinenodiscardvirtual

Opt in to shared handles and translate a requested local size into properties.

Reimplemented in GraphicalText.

Definition at line 172 of file graphicalComponent.hpp.

◆ resizeHandlesVisible()

bool resizeHandlesVisible ( ) const
nodiscard

Definition at line 275 of file graphicalComponent.cpp.

◆ resizeState()

ComponentResizeState resizeState ( ) const
nodiscard

Definition at line 304 of file graphicalComponent.cpp.

◆ rotate()

void rotate ( )

Rotates the component by 90 degrees.

Definition at line 394 of file graphicalComponent.cpp.

◆ scanImage()

QPoint scanImage ( const QImage & image,
PortSide side,
QPoint initialPoint ) const
private

Scans image for first non-transparent pixel.

Parameters
imageImage to scan
side
initialPointStarting point
Returns
First non-transparent point

Definition at line 500 of file graphicalComponent.cpp.

◆ serialize()

nlohmann::ordered_json serialize ( ) const
nodiscardvirtual

Serializes the component to JSON.

Returns
JSON object with position, rotation, and type

Reimplemented in GraphicalText.

Definition at line 814 of file graphicalComponent.cpp.

◆ setItemShape()

void setItemShape ( QGraphicsItem * shape)
protected

Sets the component shape graphics item.

Parameters
shapeThe shape item

Definition at line 246 of file graphicalComponent.cpp.

◆ setPortLine() [1/2]

void setPortLine ( Port * port)
protected

Creates the connecting line from port to shape.

Parameters
portThe port to connect

Definition at line 530 of file graphicalComponent.cpp.

◆ setPortLine() [2/2]

void setPortLine ( Port * port,
QImage & shapeImage )
private

Reuses a shape image while building a batch of port lines.

Definition at line 536 of file graphicalComponent.cpp.

◆ setPorts()

void setPorts ( const std::vector< PortPair > & busToPortInputs,
const std::vector< PortPair > & busToPortOutputs )
virtual

Sets input and output ports.

Parameters
busToPortInputsVector of (name, position) pairs for inputs
busToPortOutputsVector of (name, position) pairs for outputs

Reimplemented in GraphicalLogicComponent.

Definition at line 454 of file graphicalComponent.cpp.

◆ Port

friend class Port
friend

Definition at line 156 of file graphicalComponent.hpp.

Member Data Documentation

◆ boundsUpdatePending

bool boundsUpdatePending = false
protected

Definition at line 253 of file graphicalComponent.hpp.

◆ hiddenProperties

std::unordered_set<std::string, TransparentStringHash, std::equal_to<> > hiddenProperties
protected

Property names hidden from the panel, still accessible and serialized. Add names in subclasses with hiddenProperties.insert("propertyName").

Definition at line 199 of file graphicalComponent.hpp.

◆ inputPorts

std::vector<Port*> inputPorts
protected

Input connection ports.

Definition at line 242 of file graphicalComponent.hpp.

◆ itemShape

QGraphicsItem* itemShape = nullptr
private

The visual shape item.

Definition at line 345 of file graphicalComponent.hpp.

◆ outputPorts

std::vector<Port*> outputPorts
protected

Output connection ports.

Definition at line 245 of file graphicalComponent.hpp.

◆ printPortNames

bool printPortNames = false
protected

Whether to print port names.

Definition at line 259 of file graphicalComponent.hpp.

◆ scanShape

bool scanShape = false
protected

Whether to scan shape alpha for port placement.

Definition at line 256 of file graphicalComponent.hpp.


The documentation for this class was generated from the following files: