wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Connection.h
Go to the documentation of this file.
1// Copyright (c) 2012, STANISLAW ADASZEWSKI
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// Portions Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7// Originally derived from Stanislaw Adaszewski's Qt Node Editor (qneblock); since
8// re-authored for wiRedPanda. The BSD attribution above is retained as a license
9// obligation for the derived design.
10
14
15#pragma once
16
17#include <QGraphicsPathItem>
18#include <QPen>
19
20#include "App/Core/Enums.h"
21#include "App/Core/ItemWithId.h"
22
24class InputPort;
25class OutputPort;
26class Port;
27
37class Connection : public QGraphicsPathItem, public ItemWithId
38{
39public:
40 enum { Type = QGraphicsItem::UserType + 2 };
41 int type() const override { return Type; }
42
44 explicit Connection(QGraphicsItem *parent = nullptr);
45 ~Connection() override;
46
47 // --- Port / Endpoint Access ---
48
50 OutputPort *startPort() const;
52 InputPort *endPort() const;
54 Port *otherPort(const Port *port) const;
55
56 // --- Port Configuration ---
57
59 void setStartPort(OutputPort *port);
61 void setEndPort(InputPort *port);
63 void setStartPos(const QPointF point);
65 void setEndPos(const QPointF point);
66
67 // --- Status & Visualization ---
68
70 Status status() const;
72 void setStatus(const Status status);
74 bool highLight();
76 void setHighLight(const bool highLight);
78 QPen statusPen() const { return m_statusPen; }
79
80 // --- Geometric properties ---
81
83 QRectF boundingRect() const override;
88 QPainterPath shape() const override;
90 double angle();
92 void updatePath();
93
95 void updatePosFromPorts();
96
97 // --- Visual rendering ---
98
100 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
101
103 void updateTheme();
104
105 // --- Serialization ---
106
108 void load(QDataStream &stream, SerializationContext &context);
110 void save(QDataStream &stream) const;
111
112protected:
113 // --- Qt event overrides ---
114
116 QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
118 bool sceneEvent(QEvent *event) override;
119
120private:
121 Q_DISABLE_COPY_MOVE(Connection)
122
123
124 void changePortAttachment(Port *oldPort, Port *newPort);
125
127 void applyStatusPen();
128
129 // --- Members: Theme colors ---
130
131 QColor m_activeColor;
132 QColor m_inactiveColor;
133 QColor m_unknownColor;
134 QColor m_errorColor;
135 QColor m_selectedColor;
136
137 // --- Members: Ports & positions ---
138
139 OutputPort *m_startPort = nullptr;
140 InputPort *m_endPort = nullptr;
141 QPointF m_startPos;
142 QPointF m_endPos;
143
144 // --- Members: State ---
145
146 Status m_status = Status::Unknown;
147 bool m_highLight = false;
148
152 QPen m_statusPen;
153
155 mutable QPainterPath m_cachedShape;
156 mutable bool m_shapeDirty = true;
157};
Central enumeration types for element types, groups, and signal status.
Enums::Status Status
Definition Enums.h:106
Defines ItemWithId, the base class for all uniquely identified circuit items.
A bezier-curve wire connecting an output port to an input port in the scene.
Definition Connection.h:38
Connection(QGraphicsItem *parent=nullptr)
Constructs an unconnected wire.
QPen statusPen() const
Returns the pen paint() draws the wire with for its current status.
Definition Connection.h:78
OutputPort * startPort() const
Returns the output port this connection originates from.
void updateTheme()
Refreshes wire colours from the current ThemeManager palette.
void setHighLight(const bool highLight)
Enables or disables connection highlighting.
Status status() const
Returns the current four-state signal status (Active/Inactive/Unknown/Error).
void setEndPos(const QPointF point)
Sets the visual end position to point (used when dragging).
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void updatePath()
Recomputes the bezier control points from the current start/end positions.
InputPort * endPort() const
Returns the input port this connection leads to.
~Connection() override
QRectF boundingRect() const override
bool highLight()
Returns true if this connection is highlighted.
void setStatus(const Status status)
Sets the connection status and triggers a visual refresh.
double angle()
Returns the current angle of the bezier midpoint in radians.
void save(QDataStream &stream) const
Serializes the connection endpoints to stream.
int type() const override
Definition Connection.h:41
QPainterPath shape() const override
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
void updatePosFromPorts()
Moves the wire endpoints to match the current port positions.
void load(QDataStream &stream, SerializationContext &context)
Deserializes the connection from stream, resolving ports via context.
void setEndPort(InputPort *port)
Sets the input port this connection leads to.
Port * otherPort(const Port *port) const
Returns the port at the other end of this connection from port.
void setStartPort(OutputPort *port)
Sets the output port this connection originates from.
bool sceneEvent(QEvent *event) override
void setStartPos(const QPointF point)
Sets the visual start position to point (used when dragging).
A port that receives a signal (the destination end of a wire).
Definition Port.h:229
ItemWithId()=default
Constructs a new ItemWithId with an unassigned ID of -1.
A port that drives a signal (the source end of a wire).
Definition Port.h:261
Abstract base class for circuit element ports (connection endpoints).
Definition Port.h:39
Bundles all per-deserialization state so that load() overrides receive it through one explicit parame...