wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
ElementFactory.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 <QCoreApplication>
7#include <QMetaEnum>
8
9#include "App/Core/Common.h"
12
14{
15 // QMetaEnum provides compile-time-safe string<->int conversion for Q_ENUM
16 // types; it returns -1 on failure, which is NOT a valid ElementType —
17 // map it to Unknown explicitly so callers' `== ElementType::Unknown`
18 // checks catch bad input (Unknown is 0, not -1).
19 qCDebug(four) << text;
20 const int value = QMetaEnum::fromType<ElementType>().keyToValue(text.toLatin1());
21 return (value == -1) ? ElementType::Unknown : static_cast<ElementType>(value);
22}
23
25{
26 qCDebug(four) << type;
27
28 if (type == ElementType::Unknown) {
29 return "UNKNOWN";
30 }
31
32 // QVariant leverages the Q_ENUM registration to produce the enum key name.
33 return QVariant::fromValue(type).toString();
34}
35
37{
38 qCDebug(four) << type;
39
40 if (type == ElementType::Unknown) {
41 return tr("MULTIPLE TYPES");
42 }
43
44 const auto &meta = ElementMetadataRegistry::metadata(type);
45 return QCoreApplication::translate(meta.trContext, meta.titleText);
46}
47
49{
50 if (type == ElementType::Unknown) {
51 return tr("Unknown");
52 }
53
54 const auto &meta = ElementMetadataRegistry::metadata(type);
55 return QCoreApplication::translate(meta.trContext, meta.translatedName);
56}
57
59{
60 switch (type) {
61 case ElementType::InputVcc: return tr("Constant logic HIGH (1) source.");
62 case ElementType::InputGnd: return tr("Constant logic LOW (0) source.");
63 case ElementType::InputButton: return tr("Push button: outputs HIGH only while pressed.");
64 case ElementType::InputSwitch: return tr("Toggle switch: click to flip between 0 and 1.");
65 case ElementType::InputRotary: return tr("Rotary switch with several selectable positions.");
66 case ElementType::Clock: return tr("Clock: a square wave at a configurable frequency.");
67 case ElementType::Led: return tr("LED: lights up while its input is HIGH.");
68 case ElementType::Display7: return tr("7-segment display for a single digit.");
69 case ElementType::Display14: return tr("14-segment alphanumeric display.");
70 case ElementType::Display16: return tr("16-segment alphanumeric display.");
71 case ElementType::Buzzer: return tr("Buzzer: plays a tone while its input is HIGH.");
72 case ElementType::AudioBox: return tr("Audio box: plays an audio file while its input is HIGH.");
73 case ElementType::And: return tr("AND gate: output is HIGH only when every input is HIGH.");
74 case ElementType::Or: return tr("OR gate: output is HIGH when any input is HIGH.");
75 case ElementType::Not: return tr("NOT gate: inverts its input.");
76 case ElementType::Nand: return tr("NAND gate: LOW only when every input is HIGH.");
77 case ElementType::Nor: return tr("NOR gate: HIGH only when every input is LOW.");
78 case ElementType::Xor: return tr("XOR gate: HIGH when an odd number of inputs are HIGH.");
79 case ElementType::Xnor: return tr("XNOR gate: HIGH when the inputs match.");
80 case ElementType::Node: return tr("Node: a wire junction / branch point.");
81 case ElementType::TruthTable: return tr("Truth table: custom logic you define, output by output.");
82 case ElementType::Mux: return tr("Multiplexer: routes the selected input to the output.");
83 case ElementType::Demux: return tr("Demultiplexer: routes the input to the selected output.");
84 case ElementType::DLatch: return tr("D latch: stores the D input while Enable is HIGH.");
85 case ElementType::SRLatch: return tr("SR latch: a Set/Reset storage element.");
86 case ElementType::DFlipFlop: return tr("D flip-flop: stores D on the active clock edge.");
87 case ElementType::JKFlipFlop: return tr("JK flip-flop: set, reset or toggle on the active clock edge.");
88 case ElementType::TFlipFlop: return tr("T flip-flop: toggles on the active clock edge when T is HIGH.");
89 case ElementType::Text: return tr("Text: a free-text annotation on the canvas.");
90 case ElementType::Line: return tr("Line: a decorative annotation on the canvas.");
91 default: return {};
92 }
93}
94
96{
97 if (type == ElementType::Unknown) {
98 return {};
99 }
100
101 return QPixmap{ElementMetadataRegistry::metadata(type).pixmapPath()};
102}
103
105{
106 qCDebug(four) << type;
107
108 if (type == ElementType::Unknown) {
109 throw PANDACEPTION("Unknown element type: %1", typeToText(type));
110 }
111
112 const auto it = instance().m_creatorMap.constFind(type);
113
114 if (it == instance().m_creatorMap.constEnd()) {
115 throw PANDACEPTION("Unknown type: %1", typeToText(type));
116 }
117
118 return it.value()();
119}
120
121void ElementFactory::registerCreator(ElementType type, std::function<GraphicElement *()> creator)
122{
123 instance().m_creatorMap[type] = std::move(creator);
124}
125
127{
128 return instance().m_creatorMap.contains(type);
129}
Common logging utilities, the Pandaception error type, and helper macros.
#define PANDACEPTION(msg,...)
Definition Common.h:98
#define qCDebug(category)
Definition Common.h:29
Singleton factory for all circuit element types.
Element metadata struct and global registry for compile-time element registration.
Enums::ElementType ElementType
Definition Enums.h:107
Abstract base class for all graphical circuit elements.
static QString translatedName(const ElementType type)
Returns the translated human-readable name for type.
static bool hasCreator(ElementType type)
Returns true if a creator function is registered for type.
static QPixmap pixmap(const ElementType type)
Returns the pixmap icon for the given element type.
static void registerCreator(ElementType type, std::function< GraphicElement *()> creator)
Registers a creator lambda for type, used by buildElement().
static QString typeToText(const ElementType type)
Converts type to its internal name string.
static QString description(const ElementType type)
static QString typeToTitleText(const ElementType type)
Returns the display title string for type.
static ElementFactory & instance()
Returns the singleton ElementFactory instance.
static ElementType textToType(const QString &text)
Converts an element type name string to its ElementType enum value.
static GraphicElement * buildElement(const ElementType type)
Constructs and returns a new graphic element of the given type.
static const ElementMetadata & metadata(ElementType type)
Returns the metadata for type. Asserts if type is not registered.
Abstract base class for all graphical circuit elements in wiRedPanda.
std::function< QString()> pixmapPath
Callable returning the default pixmap resource path (lazily evaluated).