wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Port Class Referenceabstract

Abstract base class for circuit element ports (connection endpoints). More...

#include <Port.h>

Inheritance diagram for Port:
[legend]

Public Types

enum  { Type = QGraphicsItem::UserType + 1 }

Public Member Functions

void attachConnection (Connection *conn)
 Registers conn as a connection attached to this port.
QRectF boundingRect () const override
const QList< Connection * > & connections () const
 Returns the list of wires attached to this port.
QPen currentPen () const
 Returns the pen paint() draws the port outline with for its current status.
Status defaultValue () const
 Returns the default status applied when the port is unconnected.
void detachConnection (Connection *conn)
 Removes conn from this port's connection list.
int globalIndex () const
GraphicElementgraphicElement ()
 Returns the graphic element that owns this port.
const GraphicElementgraphicElement () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void hoverEnter ()
 Applies hover-enter visual feedback.
void hoverLeave ()
 Reverts hover-enter visual feedback.
int index () const
 Returns the port's visual/logical index within the element.
bool isConnected (Port *otherPort)
 Returns true if this port is connected to otherPort via any wire.
virtual bool isInput () const =0
 Returns true if this is an input port. Pure virtual.
virtual bool isOutput () const =0
 Returns true if this is an output port. Pure virtual.
bool isRequired () const
 Returns true if a connection to this port is mandatory.
virtual bool isValid () const =0
 Returns true if the port satisfies its connection requirement. Pure virtual.
QString name () const
 Returns the port's label text.
void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
 Port (QGraphicsItem *parent=nullptr)
 Constructs the port with optional parent item.
void setCurrentBrush (const QBrush &currentBrush)
 Sets the brush used to fill the port shape.
void setDefaultStatus (const Status defaultStatus)
 Sets the status applied when the port has no connection.
void setGraphicElement (GraphicElement *graphicElement)
 Binds this port to graphicElement.
void setIndex (const int index)
 Sets the port's visual index within the element.
void setName (const QString &name)
 Sets the label text shown next to the port.
void setRequired (const bool required)
 Marks whether a wire to this port is mandatory.
virtual void setStatus (Status status)=0
QPainterPath shape () const override
Status status () const
 Returns the current logical status (Active/Inactive/Unknown/Error).
int type () const override
void updateConnections ()
 Triggers a path update on all attached connections.
void updateTheme ()
 Reapplies the status-based pen and brush from the current ThemeManager palette.
virtual ~Port ()=default

Static Public Member Functions

static quint64 makeSerialId (quint64 elementBase, int globalIndex)

Protected Member Functions

void drainConnections (bool isInput)
 Drains all attached connections, breaking back-references before deletion.
QVariant itemChange (GraphicsItemChange change, const QVariant &value) override

Protected Attributes

QList< Connection * > m_connections
QBrush m_currentBrush
QPen m_currentPen
Status m_defaultStatus = Status::Unknown
GraphicElementm_graphicElement = nullptr
int m_index = 0
QString m_name
bool m_required = true
Status m_status = Status::Unknown

Static Protected Attributes

static constexpr int kRadius = 5

Detailed Description

Abstract base class for circuit element ports (connection endpoints).

A port is the small glyph drawn at the edge of a graphic element where wires attach — a circle for inputs, a right-pointing triangle for outputs. It stores a reference to the owning GraphicElement, all attached Connections, and its current logical status.

Concrete subclasses InputPort and OutputPort differentiate direction.

Definition at line 38 of file Port.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
Type 

Definition at line 43 of file Port.h.

Constructor & Destructor Documentation

◆ Port()

Port::Port ( QGraphicsItem * parent = nullptr)
explicit

Constructs the port with optional parent item.

Definition at line 16 of file Port.cpp.

Referenced by InputPort::InputPort(), isConnected(), and OutputPort::OutputPort().

◆ ~Port()

virtual Port::~Port ( )
virtualdefault

Member Function Documentation

◆ attachConnection()

void Port::attachConnection ( Connection * conn)

Registers conn as a connection attached to this port.

Definition at line 62 of file Port.cpp.

References m_connections, and updateConnections().

◆ boundingRect()

QRectF Port::boundingRect ( ) const
override
Reimplemented from base class. Pads the default pen-exact bound by a small margin so DeviceCoordinateCache
has room to antialias the stroke edge instead of clipping it (visible at high zoom).

Definition at line 35 of file Port.cpp.

◆ connections()

const QList< Connection * > & Port::connections ( ) const

Returns the list of wires attached to this port.

Definition at line 57 of file Port.cpp.

References m_connections.

Referenced by ConnectionManager::detach(), CommandUtils::drainPortConnections(), ChangePortSizeCommand::redo(), and OutputPort::setStatus().

◆ currentPen()

QPen Port::currentPen ( ) const
inline

Returns the pen paint() draws the port outline with for its current status.

Definition at line 147 of file Port.h.

References m_currentPen.

◆ defaultValue()

Status Port::defaultValue ( ) const
inline

Returns the default status applied when the port is unconnected.

Definition at line 76 of file Port.h.

References m_defaultStatus.

Referenced by updateConnections().

◆ detachConnection()

void Port::detachConnection ( Connection * conn)

Removes conn from this port's connection list.

Parameters
connWire to detach.

Definition at line 77 of file Port.cpp.

References Connection::endPort(), m_connections, Connection::setEndPort(), Connection::setStartPort(), Connection::startPort(), and updateConnections().

◆ drainConnections()

void Port::drainConnections ( bool isInput)
protected

Drains all attached connections, breaking back-references before deletion.

Parameters
isInputTrue if this is an input port (sets endPort to nullptr); false for output.

Called from the InputPort and OutputPort destructors to avoid re-entrant disconnect() calls into a partially destroyed port.

Definition at line 260 of file Port.cpp.

References isInput(), and m_connections.

Referenced by InputPort::~InputPort(), and OutputPort::~OutputPort().

◆ globalIndex()

int Port::globalIndex ( ) const

Returns this port's index within the element's combined input+output port sequence: index() for input ports, index() + graphicElement()->inputSize() for output ports. The single source of truth for the port-ordering half of the serial-ID formula.

Definition at line 148 of file Port.cpp.

References isOutput(), m_graphicElement, and m_index.

Referenced by makeSerialId().

◆ graphicElement() [1/2]

GraphicElement * Port::graphicElement ( )
inline

◆ graphicElement() [2/2]

const GraphicElement * Port::graphicElement ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 68 of file Port.h.

References m_graphicElement.

◆ hoverEnter()

void Port::hoverEnter ( )

Applies hover-enter visual feedback.

Definition at line 218 of file Port.cpp.

References ThemeManager::attributes().

Referenced by Connection::itemChange().

◆ hoverLeave()

void Port::hoverLeave ( )

Reverts hover-enter visual feedback.

Definition at line 213 of file Port.cpp.

Referenced by Connection::itemChange().

◆ index()

int Port::index ( ) const

Returns the port's visual/logical index within the element.

Definition at line 138 of file Port.cpp.

References m_index.

Referenced by UpdateBlobCommand::captureConnections(), and setIndex().

◆ isConnected()

bool Port::isConnected ( Port * otherPort)

Returns true if this port is connected to otherPort via any wire.

Parameters
otherPortPort to check for an existing connection.

Definition at line 94 of file Port.cpp.

References m_connections, and Port().

Referenced by ConnectionManager::connectionRejectionReason().

◆ isInput()

virtual bool Port::isInput ( ) const
pure virtual

Returns true if this is an input port. Pure virtual.

Implemented in InputPort, and OutputPort.

Referenced by drainConnections(), ConnectionSerializer::load(), and updateConnections().

◆ isOutput()

virtual bool Port::isOutput ( ) const
pure virtual

Returns true if this is an output port. Pure virtual.

Implemented in InputPort, and OutputPort.

Referenced by Connection::angle(), globalIndex(), ConnectionSerializer::load(), and ConnectionManager::updateHover().

◆ isRequired()

bool Port::isRequired ( ) const

Returns true if a connection to this port is mandatory.

Definition at line 194 of file Port.cpp.

References m_required.

Referenced by InputPort::isValid().

◆ isValid()

virtual bool Port::isValid ( ) const
pure virtual

Returns true if the port satisfies its connection requirement. Pure virtual.

Implemented in InputPort, and OutputPort.

Referenced by updateConnections().

◆ itemChange()

QVariant Port::itemChange ( GraphicsItemChange change,
const QVariant & value )
overrideprotected
Reimplemented from base class.

Definition at line 129 of file Port.cpp.

References updateConnections().

◆ makeSerialId()

quint64 Port::makeSerialId ( quint64 elementBase,
int globalIndex )
static

Packs elementBase and globalIndex into a serial ID: (elementBase << 16) | (globalIndex & 0xFFFF). The single source of truth for the serial-ID format used to resolve connection endpoints across save/load.

Definition at line 156 of file Port.cpp.

References globalIndex().

Referenced by ConnectionSerializer::save(), and GraphicElementSerializer::save().

◆ name()

QString Port::name ( ) const

Returns the port's label text.

Definition at line 161 of file Port.cpp.

References m_name.

Referenced by setName().

◆ paint()

void Port::paint ( QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget )
override
Reimplemented from base class. Draws with m_currentPen instead of the item's own pen() – see updateTheme()
for why – otherwise identical to the default QGraphicsPathItem paint.

Definition at line 44 of file Port.cpp.

References m_currentPen.

◆ setCurrentBrush()

void Port::setCurrentBrush ( const QBrush & currentBrush)

Sets the brush used to fill the port shape.

Parameters
currentBrushNew brush.

Definition at line 183 of file Port.cpp.

References m_currentBrush.

Referenced by updateTheme().

◆ setDefaultStatus()

void Port::setDefaultStatus ( const Status defaultStatus)

Sets the status applied when the port has no connection.

Parameters
defaultStatusDefault status value.

Definition at line 172 of file Port.cpp.

References m_defaultStatus, and setStatus().

Referenced by DFlipFlop::updatePortsProperties(), DLatch::updatePortsProperties(), JKFlipFlop::updatePortsProperties(), SRFlipFlop::updatePortsProperties(), SRLatch::updatePortsProperties(), and TFlipFlop::updatePortsProperties().

◆ setGraphicElement()

void Port::setGraphicElement ( GraphicElement * graphicElement)

Binds this port to graphicElement.

Parameters
graphicElementOwning graphic element.

Definition at line 208 of file Port.cpp.

References graphicElement(), and m_graphicElement.

Referenced by ElementPorts::addPort().

◆ setIndex()

void Port::setIndex ( const int index)

Sets the port's visual index within the element.

Parameters
indexNew index.

Definition at line 143 of file Port.cpp.

References index(), and m_index.

Referenced by ElementPorts::addPort().

◆ setName()

◆ setRequired()

void Port::setRequired ( const bool required)

Marks whether a wire to this port is mandatory.

Parameters
requiredtrue if a connection is required for valid simulation.
Note
Does not affect the port's default status — call setDefaultStatus() separately.

Definition at line 199 of file Port.cpp.

References m_required, and updateConnections().

Referenced by Node::Node(), Node::setWirelessMode(), DFlipFlop::updatePortsProperties(), JKFlipFlop::updatePortsProperties(), SRFlipFlop::updatePortsProperties(), and TFlipFlop::updatePortsProperties().

◆ setStatus()

virtual void Port::setStatus ( Status status)
pure virtual

Implemented in InputPort, and OutputPort.

References status().

Referenced by setDefaultStatus(), and updateConnections().

◆ shape()

QPainterPath Port::shape ( ) const
override
Reimplemented from base class. Hit-testing uses the full ±kRadius square regardless of the painted glyph.

Definition at line 25 of file Port.cpp.

References kRadius.

◆ status()

Status Port::status ( ) const
inline

Returns the current logical status (Active/Inactive/Unknown/Error).

Definition at line 79 of file Port.h.

References m_status.

Referenced by Display7::paint(), Connection::setStartPort(), InputPort::setStatus(), OutputPort::setStatus(), and setStatus().

◆ type()

int Port::type ( ) const
inlineoverride

Definition at line 44 of file Port.h.

References Type.

◆ updateConnections()

void Port::updateConnections ( )

Triggers a path update on all attached connections.

Definition at line 100 of file Port.cpp.

References defaultValue(), isInput(), isValid(), m_connections, and setStatus().

Referenced by attachConnection(), detachConnection(), itemChange(), ElementOrientation::orientPort(), and setRequired().

◆ updateTheme()

void Port::updateTheme ( )

Reapplies the status-based pen and brush from the current ThemeManager palette.

Definition at line 223 of file Port.cpp.

References ThemeManager::attributes(), m_currentPen, m_status, and setCurrentBrush().

Referenced by InputPort::InputPort(), OutputPort::OutputPort(), InputPort::setStatus(), and OutputPort::setStatus().

Member Data Documentation

◆ kRadius

int Port::kRadius = 5
staticconstexprprotected

Hit-area / glyph half-size in pixels. ±kRadius gives a 10×10 px clickable square, large enough to hit reliably without obscuring nearby elements.

Definition at line 199 of file Port.h.

Referenced by InputPort::InputPort(), OutputPort::OutputPort(), and shape().

◆ m_connections

QList<Connection *> Port::m_connections
protected

◆ m_currentBrush

QBrush Port::m_currentBrush
protected

Definition at line 204 of file Port.h.

Referenced by setCurrentBrush().

◆ m_currentPen

QPen Port::m_currentPen
protected

Pen paint() actually draws the outline with, kept separate from the item's own QGraphicsPathItem pen – see updateTheme() for why (avoids an unneeded BSP-tree re-index on every status colour change; all four status pens share the same width, so unlike Connection this never needs to fall back to the item's real pen).

Definition at line 209 of file Port.h.

Referenced by currentPen(), paint(), and updateTheme().

◆ m_defaultStatus

Status Port::m_defaultStatus = Status::Unknown
protected

Definition at line 212 of file Port.h.

Referenced by defaultValue(), and setDefaultStatus().

◆ m_graphicElement

GraphicElement* Port::m_graphicElement = nullptr
protected

Definition at line 203 of file Port.h.

Referenced by globalIndex(), graphicElement(), graphicElement(), and setGraphicElement().

◆ m_index

int Port::m_index = 0
protected

Definition at line 215 of file Port.h.

Referenced by globalIndex(), index(), and setIndex().

◆ m_name

QString Port::m_name
protected

Definition at line 211 of file Port.h.

Referenced by name(), and setName().

◆ m_required

bool Port::m_required = true
protected

Definition at line 214 of file Port.h.

Referenced by isRequired(), and setRequired().

◆ m_status

Status Port::m_status = Status::Unknown
protected

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