6#include <QGraphicsSceneMouseEvent>
22 .type = ElementType::TruthTable,
23 .group = ElementGroup::IC,
28 .canChangeAppearance =
true,
30 .hasTruthTable =
true,
32 static_assert(
validate(constraints));
37 meta.pixmapPath = []{
return QStringLiteral(
":/Components/Logic/truthtable-rotated.svg"); };
38 meta.titleText = QT_TRANSLATE_NOOP(
"TruthTable",
"TRUTH TABLE");
39 meta.translatedName = QT_TRANSLATE_NOOP(
"TruthTable",
"Truth Table");
40 meta.trContext =
"TruthTable";
41 meta.defaultAppearances = QStringList({
":/Components/Logic/truthtable-rotated.svg"});
75 int y = 32 - (
static_cast<int>(
inputs().size()) * step) + step;
77 for (
auto *port :
inputs()) {
88 port->setName(QChar::fromLatin1(
static_cast<char>(
'A' + index)));
97 int y = 32 - (
static_cast<int>(
outputs().size()) * step) + step;
102 port->setRotation(0);
110 port->setName(
"S" + QString::number(index));
124 static QSvgRenderer renderer(QStringLiteral(
":/Components/Logic/truthtable-rotated.svg"));
133void TruthTable::generatePixmap()
140 QPixmap sizingPixmap(size);
141 sizingPixmap.fill(Qt::transparent);
143 GraphicElement::update();
146void TruthTable::drawBody(QPainter *painter)
149 painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing,
true);
155 const QRectF bounds(
pixmap().rect());
159 painter->setBrush(QColor(126, 126, 126));
160 painter->setPen(QPen(QBrush(QColor(78, 78, 78)), 0.5, Qt::SolidLine));
161 const QRectF finalRect(QPointF(7, 0), QSizeF(bounds.width() - 14, bounds.height()));
162 painter->drawRoundedRect(finalRect, 3, 3);
169 const QSizeF logoSize = logo.defaultSize();
170 const QRectF logoRect(finalRect.center() - QPointF(logoSize.width() / 2, logoSize.height() / 2), logoSize);
172 painter->translate(logoRect.center());
175 painter->translate(-logoRect.center());
176 logo.render(painter, logoRect);
180 painter->setBrush(QColor(78, 78, 78));
181 painter->setPen(QPen(QBrush(QColor(78, 78, 78)), 0.5, Qt::SolidLine));
182 QRectF shadowRect(finalRect.bottomLeft(), finalRect.bottomRight());
183 shadowRect.adjust(0, -3, 0, 0);
184 painter->drawRoundedRect(shadowRect, 3, 3);
189void TruthTable::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
197 painter->setPen(QPen(
m_appearance.selectionPen(), 0.5, Qt::SolidLine));
231 QMap<QString, QVariant> map;
232 map.insert(
"key", m_key);
244 if (map.contains(
"key")) {
245 setkey(map.value(
"key").toBitArray());
258 if (s == Status::Unknown || s == Status::Error) {
271 pos = (pos << 1) | ((s == Status::Active) ? 1U : 0U);
275 const bool result = m_key.at(
static_cast<qsizetype
>(256 * i) +
static_cast<qsizetype
>(pos));
Common logging utilities, the Pandaception error type, and helper macros.
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
Port classes: Port (base), InputPort, and OutputPort.
Deserialization/serialization context structs passed through load()/save() call chains.
Circuit and waveform file serialization/deserialization utilities.
static QSvgRenderer & truthTableLogoRenderer()
Graphic element for a user-programmable truth table.
Named version predicates for file-format compatibility checks.
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.
ElementAppearance m_appearance
qreal rotation() const
Returns the current rotation angle of this element in degrees.
bool isFlippedY() const
Returns true if this element is mirrored along the Y axis (vertical flip).
void setLabelAnchor(const QPointF &pos)
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...
virtual void load(QDataStream &stream, SerializationContext &context)
Loads the graphic element through a binary data stream.
int outputSize() const
Returns the current number of output ports.
virtual void save(QDataStream &stream, SerializationOptions options) const
bool isFlippedX() const
Returns true if this element is mirrored along the X axis (horizontal flip).
const QVector< OutputPort * > & outputs() const
Returns a const reference to the vector of all output ports.
bool rotatesGraphic() const
const QVector< InputPort * > & inputs() const
Returns a const reference to the vector of all input ports.
static QMap< QString, QVariant > readBoundedMetadata(QDataStream &stream)
Reads the file-level metadata QMap<QString,QVariant> from stream without calling QList::reserve() wit...
Programmable truth-table element with configurable inputs and outputs.
void setkey(const QBitArray &key)
Sets the truth-table output bit-array to key.
void updatePortsProperties() override
Resizes the truth-table key when input or output count changes.
void requestOpenTruthTableEditor()
Emitted on double-click to request opening the truth table editor.
QBitArray & key()
Returns a reference to the truth-table output bit-array.
QRectF boundingRect() const override
void load(QDataStream &stream, SerializationContext &context) override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void save(QDataStream &stream, SerializationOptions options) const override
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
void updateLogic() override
Looks up the current input pattern in the truth table and drives outputs.
TruthTable(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
constexpr int gridSize
Scene grid unit in pixels (elements snap to gridSize/2).
bool hasTruthTableData(const QVersionNumber &v)
V4.2: TruthTable output data stored in file.
Compile-time-validatable subset of ElementMetadata.
static ElementMetadata metadata()
static constexpr ElementConstraints constraints
static const bool registered
Self-registering element information trait.
Bundles all per-deserialization state so that load() overrides receive it through one explicit parame...
QVersionNumber version
File-format version read from the stream header.
Options passed to GraphicElement::save() (and friends); the save-side counterpart of SerializationCon...