wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
InputButton.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 <QGraphicsSceneMouseEvent>
7
12#include "App/IO/VersionInfo.h"
13
14template<>
17 .type = ElementType::InputButton,
18 .group = ElementGroup::Input,
19 .minOutputSize = 1,
20 .maxOutputSize = 1,
21 .canChangeAppearance = true,
22 .hasTrigger = true,
23 .hasLabel = true,
24 .rotatesGraphic = false,
25 };
26 static_assert(validate(constraints));
27
29 {
31 meta.pixmapPath = []{ return QStringLiteral(":/Components/Input/buttonOff.svg"); };
32 meta.titleText = QT_TRANSLATE_NOOP("InputButton", "PUSH BUTTON");
33 meta.translatedName = QT_TRANSLATE_NOOP("InputButton", "Push Button");
34 meta.trContext = "InputButton";
35 meta.defaultAppearances = QStringList({
36 ":/Components/Input/buttonOff.svg",
37 ":/Components/Input/buttonOn.svg",
38 });
39 return meta;
40 }
41
42 static inline const bool registered = []() {
44 ElementFactory::registerCreator(constraints.type, [] { return new InputButton(); });
45 return true;
46 }();
47};
48
54
55void InputButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
56{
57 // Momentary action: button goes HIGH on press, LOW on release (unlike InputSwitch which latches)
58 if (!m_locked && (event->button() == Qt::LeftButton)) {
59 setOn();
60 event->accept();
61 }
62
63 QGraphicsItem::mousePressEvent(event);
64}
65
66void InputButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
67{
68 if (!m_locked && (event->button() == Qt::LeftButton)) {
69 setOff();
70 event->accept();
71 }
72
73 QGraphicsItem::mouseReleaseEvent(event);
74}
75
76void InputButton::save(QDataStream &stream, SerializationOptions options) const
77{
78 GraphicElement::save(stream, options);
79
80 QMap<QString, QVariant> map;
81 // PortableFile streams omit the default; fresh-loaded elements start there
82 // anyway. Snapshots write unconditionally (reloaded into live elements).
84 map.insert("locked", m_locked);
85 }
86
87 stream << map;
88}
89
90void InputButton::load(QDataStream &stream, SerializationContext &context)
91{
92 GraphicElement::load(stream, context);
93
95 // v3.1–4.0 stored the locked flag as a bare bool
96 stream >> m_locked;
97 }
98
100 // v4.1+ uses a key-value map
101 QMap<QString, QVariant> map = Serialization::readBoundedMetadata(stream);
102
103 if (map.contains("locked")) {
104 m_locked = map.value("locked").toBool();
105 }
106 }
107}
108
109bool InputButton::isOn(const int port) const
110{
111 Q_UNUSED(port)
112 return m_isOn;
113}
114
116{
117 InputButton::setOn(false);
118}
119
121{
122 InputButton::setOn(true);
123}
124
125QList<std::pair<int, QString>> InputButton::appearanceStates() const
126{
127 return {{0, tr("Released")}, {1, tr("Pressed")}};
128}
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.
Definition ElementInfo.h:80
constexpr bool validate(const ElementConstraints &c)
Validates element constraints at compile time.
Definition ElementInfo.h:48
Enums::ElementType ElementType
Definition Enums.h:107
Graphic element for a momentary push-button input.
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().
static void registerMetadata(const ElementMetadata &meta)
Registers meta in the global map (called once per element type at startup).
GraphicElementInput(ElementType type, QGraphicsItem *parent=nullptr)
virtual void load(QDataStream &stream, SerializationContext &context)
Loads the graphic element through a binary data stream.
virtual void save(QDataStream &stream, SerializationOptions options) const
Momentary push-button input element that is active only while pressed.
Definition InputButton.h:20
void setOff() override
Drives all output ports logic-low (releases the button).
void load(QDataStream &stream, SerializationContext &context) override
InputButton(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
void save(QDataStream &stream, SerializationOptions options) const override
QList< std::pair< int, QString > > appearanceStates() const override
bool isOn(const int port=0) const override
Returns true if output port port is driven logic-high.
void setOn() override
Presses the button (logic-1).
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
static QMap< QString, QVariant > readBoundedMetadata(QDataStream &stream)
Reads the file-level metadata QMap<QString,QVariant> from stream without calling QList::reserve() wit...
bool hasLockState(const QVersionNumber &v)
V3.1: Lock state for input elements; color for some display/button types.
Definition VersionInfo.h:51
bool hasQMapFormat(const QVersionNumber &v)
V4.1: Format changed from flat binary to keyed QMap; port serial IDs; rotation fix.
Definition VersionInfo.h:60
Compile-time-validatable subset of ElementMetadata.
Definition ElementInfo.h:21
static ElementMetadata metadata()
static constexpr ElementConstraints constraints
static const bool registered
Self-registering element information trait.
Compile-time-registered properties for one element type.
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.