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 friend class TestConnection;
122
123 Q_DISABLE_COPY_MOVE(Connection)
124
125
126 void changePortAttachment(Port *oldPort, Port *newPort);
127
129 void applyStatusPen();
130
131 // --- Members: Theme colors ---
132
133 QColor m_activeColor;
134 QColor m_inactiveColor;
135 QColor m_unknownColor;
136 QColor m_errorColor;
137 QColor m_selectedColor;
138
139 // --- Members: Ports & positions ---
140
141 OutputPort *m_startPort = nullptr;
142 InputPort *m_endPort = nullptr;
143 QPointF m_startPos;
144 QPointF m_endPos;
145
146 // --- Members: State ---
147
148 Status m_status = Status::Unknown;
149 bool m_highLight = false;
150
154 QPen m_statusPen;
155
157 mutable QPainterPath m_cachedShape;
158 mutable bool m_shapeDirty = true;
159};
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.
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.
friend class TestConnection
Definition Connection.h:121
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:234
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:266
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...