18 .type = ElementType::Led,
19 .group = ElementGroup::Output,
22 .canChangeAppearance =
true,
25 .rotatesGraphic =
false,
27 static_assert(
validate(constraints));
32 meta.pixmapPath = []{
return QStringLiteral(
":/Components/Output/Led/LedOff.svg"); };
33 meta.titleText = QT_TRANSLATE_NOOP(
"Led",
"LED");
34 meta.translatedName = QT_TRANSLATE_NOOP(
"Led",
"LED");
35 meta.trContext =
"Led";
36 meta.defaultAppearances = QStringList({
38 ":/Components/Output/Led/LedOff.svg",
39 ":/Components/Output/Led/WhiteLed.svg",
40 ":/Components/Output/Led/LedOff.svg",
41 ":/Components/Output/Led/RedLed.svg",
42 ":/Components/Output/Led/LedOff.svg",
43 ":/Components/Output/Led/GreenLed.svg",
44 ":/Components/Output/Led/LedOff.svg",
45 ":/Components/Output/Led/BlueLed.svg",
46 ":/Components/Output/Led/LedOff.svg",
47 ":/Components/Output/Led/PurpleLed.svg",
49 ":/Components/Output/Led//BlackLed.png",
50 ":/Components/Output/Led//NavyBlueLed.png",
51 ":/Components/Output/Led//GreenLed.png",
52 ":/Components/Output/Led//TealLed.png",
53 ":/Components/Output/Led//DarkRedLed.png",
54 ":/Components/Output/Led//MagentaLed.png",
55 ":/Components/Output/Led//OrangeLed.png",
56 ":/Components/Output/Led//LightGrayLed.png",
58 ":/Components/Output/Led/LedOff.svg",
59 ":/Components/Output/Led/RoyalLed.png",
60 ":/Components/Output/Led/LimeGreenLed.png",
61 ":/Components/Output/Led/AquaLightLed.png",
62 ":/Components/Output/Led/RedLed.png",
63 ":/Components/Output/Led/HotPinkLed.png",
64 ":/Components/Output/Led/YellowLed.png",
65 ":/Components/Output/Led/WhiteLed.png",
122 std::bitset<4> indexBit;
125 indexBit[
static_cast<std::size_t
>(i)] = (
inputPort(i)->status() == Status::Active);
128 const int index =
static_cast<int>(indexBit.to_ulong());
138 case 1: index2 = m_colorIndex + index;
break;
139 case 2: index2 = (index == 3) ? 25 : 18 + index;
break;
140 case 3: index2 = 18 + index;
break;
141 case 4: index2 = 10 + index;
break;
174 QMap<QString, QVariant> map;
178 map.insert(
"color",
color());
198 if (map.contains(
"color")) {
199 setColor(map.value(
"color").toString());
215 for (
auto *port :
inputs()) {
216 port->setName(QString::number(
inputs().indexOf(port) + 1));
217 port->setRequired(
false);
218 port->setDefaultStatus(Status::Inactive);
226 const int index = colorIndex();
228 if (useDefaultAppearance) {
233 m_appearance.setAlternativeAppearanceAt(index, fileName);
236 m_appearance.setUsingDefaultAppearance(useDefaultAppearance);
242 QList<std::pair<int, QString>> states;
246 auto portStateLabel = [](
const int numPorts,
const int code) {
248 for (
int port = 0; port < numPorts; ++port) {
249 parts << tr(
"Port %1=%2").arg(port + 1).arg((code >> port) & 1);
251 return parts.join(QStringLiteral(
", "));
256 states.append({m_colorIndex, tr(
"Off")});
257 states.append({m_colorIndex + 1, tr(
"On")});
261 for (
int i = 0; i < 4; ++i) {
262 const int listIndex = (i == 3) ? 25 : 18 + i;
263 states.append({listIndex, portStateLabel(2, i)});
268 for (
int i = 0; i < 8; ++i) {
269 states.append({18 + i, portStateLabel(3, i)});
274 for (
int i = 0; i < 16; ++i) {
275 states.append({10 + i, portStateLabel(4, i)});
280 states.append({0, tr(
"Default")});
292 for (
int i = 0; i <
simInputs().size(); ++i) {
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 LED output indicator.
Port classes: Port (base), InputPort, and OutputPort.
Deserialization/serialization context structs passed through load()/save() call chains.
Circuit and waveform file serialization/deserialization utilities.
Named version predicates for file-format compatibility checks.
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.
void setHasColors(const bool hasColors)
Sets whether this element type supports color selection.
ElementAppearance m_appearance
int inputSize() const
Returns the current number of input ports.
void setOutputValue(const int index, const Status value)
Sets simulation output port index to value.
virtual void load(QDataStream &stream, SerializationContext &context)
Loads the graphic element through a binary data stream.
InputPort * inputPort(const int index=0) const
Returns the input port at index (default 0).
static int colorNameToIndex(const QString &color)
Maps a color name ("White","Red","Green","Blue","Purple") to indices 0–4.
bool simUpdateInputs()
Snapshots each predecessor's output into the simulation input cache.
virtual void save(QDataStream &stream, SerializationOptions options) const
void setPixmap(const QString &pixmapPath)
Loads and applies the pixmap located at pixmapPath.
bool isValid()
Returns true if the element is fully initialised and connected correctly.
virtual void updatePortsProperties()
Repositions and reconfigures all ports after the port count changes.
const QVector< InputPort * > & inputs() const
Returns a const reference to the vector of all input ports.
LED output element that lights up when its input is logic-1.
Led(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
QString color() const override
Returns the current LED color name.
void updateLogic() override
Propagates input values to matching outputs.
QList< std::pair< int, QString > > appearanceStates() const override
void setColor(const QString &color) override
Sets the LED color to color.
void load(QDataStream &stream, SerializationContext &context) override
void updatePortsProperties() override
Updates port positions when input count changes.
void save(QDataStream &stream, SerializationOptions options) const override
QString genericProperties() override
Returns a string describing the current color for the element editor.
void setAppearance(const bool useDefaultAppearance, const QString &fileName) override
void refresh() override
Refreshes the visual appearance based on current state.
static constexpr const char * kDefaultColor
static QString readBoundedString(QDataStream &stream)
Reads a QString from stream, refusing to allocate more bytes than remain in the stream (prevents OOM ...
static QMap< QString, QVariant > readBoundedMetadata(QDataStream &stream)
Reads the file-level metadata QMap<QString,QVariant> from stream without calling QList::reserve() wit...
bool hasClock(const QVersionNumber &v)
V1.1: Clock element added.
bool hasQMapFormat(const QVersionNumber &v)
V4.1: Format changed from flat binary to keyed QMap; port serial IDs; rotation fix.
Compile-time-validatable subset of ElementMetadata.
static const bool registered
static ElementMetadata metadata()
static constexpr ElementConstraints constraints
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...
SerializationPurpose purpose
What this serialization is for; see SerializationPurpose. Mandatory, no default.