wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
Text.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 <QFont>
7
11
12template<>
15 .type = ElementType::Text,
16 .group = ElementGroup::Other,
17 .hasLabel = true,
18 };
19 static_assert(validate(constraints));
20
22 {
24 meta.pixmapPath = []{ return QStringLiteral(":/Components/Misc/text.png"); };
25 meta.titleText = QT_TRANSLATE_NOOP("Text", "TEXT");
26 meta.translatedName = QT_TRANSLATE_NOOP("Text", "Text");
27 meta.trContext = "Text";
28 // Text has two built-in appearance assets, but only index 0 (the transparent
29 // placeholder) is ever actually shown -- refresh() always reloads index 0, and nothing
30 // switches to index 1. The empty-state hint (see labelContentChanged()) is a separate,
31 // lighter-weight mechanism layered on top rather than an attempt to wire up this swap.
32 meta.defaultAppearances = QStringList({
33 ":/Components/Misc/no_text.png",
34 ":/Components/Misc/text.png",
35 });
36 // Text is decorative and excluded from simulation.
37 return meta;
38 }
39
40 static inline const bool registered = []() {
42 ElementFactory::registerCreator(constraints.type, [] { return new Text(); });
43 return true;
44 }();
45};
46
47// Text is a purely decorative annotation element with no ports and no simulation role.
48Text::Text(QGraphicsItem *parent)
50{
51 QFont hintFont = m_emptyHint->font();
52 hintFont.setItalic(true);
53 m_emptyHint->setFont(hintFont);
54 // Matches the default label anchor (GraphicElement's own constructor, since Text never
55 // calls setLabelAnchor() to override it) -- the hint stands in for the label until there's
56 // real content to show.
57 m_emptyHint->setPos(0, 64);
58 m_emptyHint->setText(tr("Double-click to add text"));
60 labelContentChanged(); // a fresh Text starts with an empty label -- show the hint explicitly
61 // rather than relying on QGraphicsSimpleTextItem's default visibility
62}
63
64QRectF Text::boundingRect() const
65{
66 return GraphicElement::boundingRect().united(childrenBoundingRect());
67}
68
70{
72
73 // A faded, italic variant of the label color -- distinguishable from real content at a
74 // glance, but not so loud it reads as an actual (empty) label.
76 hintColor.setAlpha(140);
77 m_emptyHint->setBrush(hintColor);
78}
79
81{
82 m_emptyHint->setVisible(label().isEmpty());
83}
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 free-text annotation label (no simulation).
Theme management types and singleton ThemeManager.
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).
GraphicElement(ElementType type, QGraphicsItem *parent=nullptr)
Constructs a graphic element of the given type, fetching all properties from the metadata registry.
virtual void updateTheme()
Updates the element's visual theme according to the current dark/light palette.
QString label() const
Returns the user-visible label text for this element.
QRectF boundingRect() const override
Returns the bounding rectangle of this element in local coordinates.
Free-text annotation element used for labelling circuit diagrams.
Definition Text.h:19
Text(QGraphicsItem *parent=nullptr)
Constructs a Text element.
Definition Text.cpp:48
void updateTheme() override
Refreshes the empty-state hint's color alongside the base label/port theming.
Definition Text.cpp:69
void labelContentChanged() override
Definition Text.cpp:80
QRectF boundingRect() const override
Returns a bounding rect that includes the label text, not just the pixmap.
Definition Text.cpp:64
QColor m_graphicElementLabelColor
static const ThemeAttributes & attributes()
Returns the current ThemeAttributes color set.
Compile-time-validatable subset of ElementMetadata.
Definition ElementInfo.h:21
static ElementMetadata metadata()
Definition Text.cpp:21
static constexpr ElementConstraints constraints
Definition Text.cpp:14
static const bool registered
Definition Text.cpp:40
Self-registering element information trait.
Compile-time-registered properties for one element type.