18 .type = ElementType::Demux,
19 .group = ElementGroup::Mux,
24 .canChangeAppearance =
true,
26 static_assert(
validate(constraints));
31 meta.pixmapPath = []{
return QStringLiteral(
":/Components/Logic/demux.svg"); };
32 meta.titleText = QT_TRANSLATE_NOOP(
"Demux",
"DEMULTIPLEXER");
33 meta.translatedName = QT_TRANSLATE_NOOP(
"Demux",
"Demux");
34 meta.trContext =
"Demux";
35 meta.defaultAppearances = QStringList({
":/Components/Logic/demux.svg"});
52void Demux::setInputSize(
const int size)
68 const int numSelectLines = calculateSelectLines(size);
69 int requiredInputs = 1 + numSelectLines;
84 const int numSelectLines = calculateSelectLines(
outputSize());
89 int outputPortsSpan = (
outputSize() - 1) * step * 2;
90 int elementHeight = (std::max)(64, outputPortsSpan + step * 6);
92 elementHeight = ((elementHeight + 15) / 16) * 16;
93 int centerY = elementHeight / 2;
100 int selectY = elementHeight - step;
101 for (
int i = 0; i < numSelectLines; ++i) {
103 inputPort(1 + i)->setPos(32 + (i * step * 2), selectY);
126void Demux::generatePixmap()
132 QPixmap sizingPixmap(64, height);
133 sizingPixmap.fill(Qt::transparent);
138void Demux::drawBody(QPainter *painter)
141 painter->setRenderHint(QPainter::Antialiasing);
146 const int height =
pixmap().rect().height();
149 const int bodyTop = 8;
150 const int bodyBottom = height - 10;
151 const int leftInset = 8;
154 painter->setBrush(QColor(38, 38, 38));
155 painter->setPen(QPen(QColor(38, 38, 38), 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
158 outer << QPointF(22, bodyTop + leftInset)
159 << QPointF(22, bodyBottom - leftInset)
160 << QPointF(42, bodyBottom)
161 << QPointF(42, bodyTop);
162 painter->drawPolygon(outer);
165 painter->setBrush(QColor(252, 252, 252));
166 painter->setPen(QPen(QColor(252, 252, 252), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
168 const int fillInset = 4;
170 inner << QPointF(22 + fillInset, bodyTop + leftInset + fillInset - 2)
171 << QPointF(22 + fillInset, bodyBottom - leftInset - fillInset + 2)
172 << QPointF(42 - fillInset, bodyBottom - fillInset)
173 << QPointF(42 - fillInset, bodyTop + fillInset);
174 painter->drawPolygon(inner);
179void Demux::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
187 painter->setPen(QPen(
m_appearance.selectionPen(), 0.5, Qt::SolidLine));
205 const int numSelectLines = calculateSelectLines(
outputSize());
208 for (
int i = 0; i < numSelectLines; i++) {
210 if (sel == Status::Unknown || sel == Status::Error) {
235int Demux::calculateSelectLines(
int dataCount)
238 while ((1 << k) < dataCount) {
Shared numeric constants used across layers.
Graphic element for the Demultiplexer (DEMUX).
Singleton factory for all circuit element types.
Self-registering element trait template and compile-time constraint validation.
ElementMetadata metadataFromConstraints(const ElementConstraints &c)
Converts ElementConstraints to an ElementMetadata with all constraint-derived fields set.
constexpr bool validate(const ElementConstraints &c)
Validates element constraints at compile time.
Enums::ElementType ElementType
Port classes: Port (base), InputPort, and OutputPort.
Graphical representation of a demultiplexer (1-to-2^N).
void updateLogic() override
Routes the data input to the output selected by the select lines.
QRectF boundingRect() const override
void updatePortsProperties() override
Recalculates port positions for the current port count.
void setOutputSize(const int size) override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Demux(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
void setRenderPixmap(const QPixmap &pixmap)
static void registerCreator(ElementType type, std::function< GraphicElement *()> creator)
Registers a creator lambda for type, used by buildElement().
GraphicElement(ElementType type, QGraphicsItem *parent=nullptr)
Constructs a graphic element of the given type, fetching all properties from the metadata registry.
const QVector< Status > & simInputs() const
Read-only view of the cached simulation input values.
int decodeSelectValue(int offset, int count) const
Decodes count select-line statuses from simInputs() into a binary index.
ElementAppearance m_appearance
int inputSize() const
Returns the current number of input ports.
QPixmap pixmap() const
Returns the pixmap currently displayed by this element.
bool simUpdateInputsAllowUnknown()
Like simUpdateInputs(), but allows Unknown/Error values through.
void setOutputValue(const int index, const Status value)
Sets simulation output port index to value.
QRectF renderBodyBounds() const
Footprint of a "procedural render body" (IC/Mux/Demux/TruthTable): the nominal 64x64 body unioned wit...
int maxOutputSize() const
Returns the maximum allowed number of output ports.
InputPort * inputPort(const int index=0) const
Returns the input port at index (default 0).
virtual void setInputSize(const int size)
Adjusts the number of input ports to size, adding or removing ports as needed.
int outputSize() const
Returns the current number of output ports.
OutputPort * outputPort(const int index=0) const
Returns the output port at index (default 0).
int minOutputSize() const
Returns the minimum allowed number of output ports.
virtual void setOutputSize(const int size)
Adjusts the number of output ports to size, adding or removing ports as needed.
void setName(const QString &name)
Sets the label text shown next to the port.
constexpr int gridSize
Scene grid unit in pixels (elements snap to gridSize/2).
Compile-time-validatable subset of ElementMetadata.
static constexpr ElementConstraints constraints
static ElementMetadata metadata()
static const bool registered
Self-registering element information trait.