wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
ElementPorts.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include "App/Core/Common.h"
8#include "App/Wiring/Port.h"
9
10InputPort *ElementPorts::inputPort(const int index) const
11{
12 if (index < 0 || index >= m_inputPorts.size()) {
13 return nullptr;
14 }
15 return m_inputPorts.at(index);
16}
17
18OutputPort *ElementPorts::outputPort(const int index) const
19{
20 if (index < 0 || index >= m_outputPorts.size()) {
21 return nullptr;
22 }
23 return m_outputPorts.at(index);
24}
25
26QVector<Port *> ElementPorts::allPorts() const
27{
28 QVector<Port *> result;
29 result.reserve(m_inputPorts.size() + m_outputPorts.size());
30 for (auto *p : m_inputPorts) { result.append(p); }
31 for (auto *p : m_outputPorts) { result.append(p); }
32 return result;
33}
34
35void ElementPorts::setInputs(const QVector<InputPort *> &inputs)
36{
37 m_inputPorts = inputs;
38
39 // Keep index() == vector position: Connection::save() derives
40 // connection serial IDs from port->index() while GraphicElement::save()
41 // numbers ports by position. A caller that permutes the vector (the
42 // Display7 legacy pin remap) would otherwise cross-wire the pins on the
43 // next save/load round trip. Load-time connection resolution is pointer-
44 // keyed, so re-indexing here is safe.
45 for (int i = 0; i < m_inputPorts.size(); ++i) {
46 if (auto *port = m_inputPorts.at(i)) {
47 port->setIndex(i);
48 }
49 }
50}
51
52void ElementPorts::addPort(const QString &name, const bool isOutput)
53{
54 // No max-size guard here — setInputSize()/setOutputSize() already enforce
55 // min/max constraints before calling this method. The serializer also
56 // calls addPort() and needs to create whatever ports the file contains.
57 qCDebug(four) << "New port.";
58 Port *port = nullptr;
59
60 if (isOutput) {
61 m_outputPorts.push_back(new OutputPort(m_owner));
62 port = m_outputPorts.constLast();
63 port->setIndex(outputSize() - 1);
64 } else {
65 m_inputPorts.push_back(new InputPort(m_owner));
66 port = m_inputPorts.constLast();
67 port->setIndex(inputSize() - 1);
68 }
69
70 port->setGraphicElement(m_owner);
71 port->setName(name);
72 port->show();
73}
74
75void ElementPorts::resizeInputs(const int size)
76{
77 if (size > inputSize()) {
78 while (size > inputSize()) { addInputPort(); }
79 } else {
80 qDeleteAll(m_inputPorts.begin() + size, m_inputPorts.end());
81 m_inputPorts.resize(size);
82 }
83}
84
85void ElementPorts::resizeOutputs(const int size)
86{
87 if (size > outputSize()) {
88 while (size > outputSize()) { addOutputPort(); }
89 } else {
90 qDeleteAll(m_outputPorts.begin() + size, m_outputPorts.end());
91 m_outputPorts.resize(size);
92 }
93}
94
96{
97 if (m_inputPorts.isEmpty()) {
98 return nullptr;
99 }
100 auto *port = m_inputPorts.constLast();
101 m_inputPorts.removeLast();
102 return port;
103}
104
106{
107 if (m_outputPorts.isEmpty()) {
108 return nullptr;
109 }
110 auto *port = m_outputPorts.constLast();
111 m_outputPorts.removeLast();
112 return port;
113}
Common logging utilities, the Pandaception error type, and helper macros.
#define qCDebug(category)
Definition Common.h:29
Owns a GraphicElement's input/output port vectors and their creation/resize lifecycle.
Abstract base class for all graphical circuit elements.
Port classes: Port (base), InputPort, and OutputPort.
void addOutputPort(const QString &name={})
Appends a new output port with optional name.
void setInputs(const QVector< InputPort * > &inputs)
InputPort * takeLastInput()
QVector< Port * > allPorts() const
Returns a combined list of all input and output ports as Port pointers.
int outputSize() const
Returns the current number of output ports.
void addInputPort(const QString &name={})
Appends a new input port with optional name.
InputPort * inputPort(int index) const
Returns the input port at index, or nullptr if out of range.
int inputSize() const
Returns the current number of input ports.
void addPort(const QString &name, bool isOutput)
Appends a new port (parented to the owner) with name; isOutput selects direction.
const QVector< InputPort * > & inputs() const
Returns the input port vector.
OutputPort * takeLastOutput()
Removes and returns the last output port WITHOUT deleting it. Returns nullptr if empty.
void resizeOutputs(int size)
Grows or shrinks the output list to exactly size.
void resizeInputs(int size)
OutputPort * outputPort(int index) const
Returns the output port at index, or nullptr if out of range.
A port that receives a signal (the destination end of a wire).
Definition Port.h:229
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
void setGraphicElement(GraphicElement *graphicElement)
Binds this port to graphicElement.
Definition Port.cpp:208
void setName(const QString &name)
Sets the label text shown next to the port.
Definition Port.cpp:166
void setIndex(const int index)
Sets the port's visual index within the element.
Definition Port.cpp:143