18 .type = ElementType::Mux,
19 .group = ElementGroup::Mux,
24 .canChangeAppearance =
true,
26 static_assert(
validate(constraints));
31 meta.pixmapPath = []{
return QStringLiteral(
":/Components/Logic/mux.svg"); };
32 meta.titleText = QT_TRANSLATE_NOOP(
"Mux",
"MULTIPLEXER");
33 meta.translatedName = QT_TRANSLATE_NOOP(
"Mux",
"Mux");
34 meta.trContext =
"Mux";
35 meta.defaultAppearances = QStringList({
":/Components/Logic/mux.svg"});
55 const int numSelectLines = calculateSelectLines(
inputSize());
56 int numDataInputs =
inputSize() - numSelectLines;
61 int dataPortsSpan = (numDataInputs - 1) * step * 2;
62 int elementHeight = (std::max)(64, dataPortsSpan + step * 6);
64 elementHeight = ((elementHeight + 15) / 16) * 16;
65 int centerY = elementHeight / 2;
68 int y = centerY - (numDataInputs - 1) * step;
69 for (
int i = 0; i < numDataInputs; ++i) {
76 int selectY = elementHeight - step;
77 for (
int i = 0; i < numSelectLines; ++i) {
78 inputPort(numDataInputs + i)->setPos(32 + (i * step * 2), selectY);
94void Mux::generatePixmap()
100 QPixmap sizingPixmap(64, height);
101 sizingPixmap.fill(Qt::transparent);
106void Mux::drawBody(QPainter *painter)
109 painter->setRenderHint(QPainter::Antialiasing);
114 const int height =
pixmap().rect().height();
117 const int bodyTop = 8;
118 const int bodyBottom = height - 10;
119 const int rightInset = 8;
122 painter->setBrush(QColor(38, 38, 38));
123 painter->setPen(QPen(QColor(38, 38, 38), 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
126 outer << QPointF(22, bodyTop)
127 << QPointF(22, bodyBottom)
128 << QPointF(42, bodyBottom - rightInset)
129 << QPointF(42, bodyTop + rightInset);
130 painter->drawPolygon(outer);
133 painter->setBrush(QColor(252, 252, 252));
134 painter->setPen(QPen(QColor(252, 252, 252), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
136 const int fillInset = 4;
138 inner << QPointF(22 + fillInset, bodyTop + fillInset)
139 << QPointF(22 + fillInset, bodyBottom - fillInset)
140 << QPointF(42 - fillInset, bodyBottom - rightInset - fillInset + 2)
141 << QPointF(42 - fillInset, bodyTop + rightInset + fillInset - 2);
142 painter->drawPolygon(inner);
147void Mux::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
155 painter->setPen(QPen(
m_appearance.selectionPen(), 0.5, Qt::SolidLine));
171 const int numSelectLines = calculateSelectLines(
inputSize());
172 int numDataInputs =
inputSize() - numSelectLines;
175 for (
int i = 0; i < numSelectLines; i++) {
177 if (sel == Status::Unknown || sel == Status::Error) {
189 if (selectValue >= numDataInputs) {
197int Mux::calculateSelectLines(
int totalInputs)
200 while ((1 << k) < totalInputs - k) {
Shared numeric constants used across layers.
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
Graphic element for the Multiplexer (MUX).
Port classes: Port (base), InputPort, and OutputPort.
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...
InputPort * inputPort(const int index=0) const
Returns the input port at index (default 0).
OutputPort * outputPort(const int index=0) const
Returns the output port at index (default 0).
Graphical representation of a multiplexer (2^N-to-1).
void updatePortsProperties() override
Recalculates port positions for the current port count.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void updateLogic() override
Routes the data input selected by the select lines to the output.
Mux(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
QRectF boundingRect() const override
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 const bool registered
static constexpr ElementConstraints constraints
static ElementMetadata metadata()
Self-registering element information trait.