SILICON
Loading...
Searching...
No Matches
graphicalIO.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <QGraphicsItem>
21#include <QPainter>
22#include <QString>
23
24#include <core/component.hpp>
25#include <core/io.hpp>
26#include <core/wire.hpp>
28
29#include <string_view>
30
34
35
36namespace SILICON::ui {
37using namespace SILICON::core;
38
44 Q_OBJECT
45public:
46 explicit GraphicalConstant(QGraphicsItem* parent = nullptr);
47
48 int type() const override { return SiliconTypes::CONSTANT; }
49
50 void setComponent(const Component_ptr& component) override;
51
52private:
53 void setupCallbacks();
54 void updateLayout();
55 void updateLayout(const BusValue& value);
56};
57
63 Q_OBJECT
64protected:
73 GraphicalIO(ItemCategory category, const Component_ptr& component, QGraphicsItem* shape,
74 QGraphicsItem* parent = nullptr, bool scanShape = false);
75
80 [[nodiscard]] QString getComponentName() const;
81
84
87
89 void updatePortOrientation(std::string_view orientation);
90
92 [[nodiscard]] bool isPortOrientationUp() const;
93
95 [[nodiscard]] QRectF componentNameRect(const QString& name) const;
96
97public:
102 static inline const QFont UI_FONT{"NovaMono", 12};
103
105 [[nodiscard]] QPoint portPositionFor(PortSide side) const;
106
108 [[nodiscard]] QRectF collisionRectForPortSide(PortSide side) const;
109
111 void setPortOrientation(PortSide side);
112
116 virtual void handleSimulationClick() {}
117
121 virtual void applyStartValue() = 0;
122
126 virtual void resetSimulationState() = 0;
127
128 void setComponent(const Component_ptr& component) override;
129
130signals:
137 void inputToggled(Bus targetBus, BusValue value, Component_weakPtr source);
138};
139
145 Q_OBJECT
146public:
147 static constexpr std::string_view COMPONENT_TYPE = "Input";
148
149 explicit GraphicalInput(QGraphicsItem* parent = nullptr);
150
152 int type() const override { return SiliconTypes::SINGLE_INPUT; }
153
155 [[nodiscard]] std::string getTypeName() const override
156 {
157 return std::string(COMPONENT_TYPE);
158 }
159
164 State getState() const;
165
169 void toggle();
170
175 void setState(State state);
176
177 void handleSimulationClick() override;
178 void applyStartValue() override;
179 void resetSimulationState() override;
180
183
184 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
185 QWidget* widget) override;
186
187private:
189
190 static const QString& getOnShapePath()
191 {
192 static const QString path = ":/other_components/input_on.svg";
193 return path;
194 }
195
196 static const QString& getOffShapePath()
197 {
198 static const QString path = ":/other_components/input_off.svg";
199 return path;
200 }
201
202 QRectF boundingRect() const override;
203};
204
210 Q_OBJECT
211public:
212 static constexpr std::string_view COMPONENT_TYPE = "BusInput";
213
214 explicit GraphicalBusInput(QGraphicsItem* parent = nullptr);
215
216 int type() const override { return SiliconTypes::BUS_INPUT; }
217
218 [[nodiscard]] std::string getTypeName() const override
219 {
220 return std::string(COMPONENT_TYPE);
221 }
222
223 void setComponent(const Component_ptr& component) override;
224
229 void setValue(const BusValue& value);
230
234 void editValue();
235
240
241 void handleSimulationClick() override;
242 void applyStartValue() override;
243 void resetSimulationState() override;
245
246 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
247 QWidget* widget) override;
248
249private:
251
254 QRectF boundingRect() const override;
255};
256
262 Q_OBJECT
263public:
264 static constexpr std::string_view COMPONENT_TYPE = "Output";
265
266 explicit GraphicalOutputSingle(QGraphicsItem* parent = nullptr);
267
268 int type() const override { return SiliconTypes::SINGLE_OUTPUT; }
269
270 [[nodiscard]] std::string getTypeName() const override
271 {
272 return std::string(COMPONENT_TYPE);
273 }
274
279 void setState(State state);
280
281 void applyStartValue() override;
282 void resetSimulationState() override;
283
284 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
285 QWidget* widget) override;
286
287private:
288 static const QString& getUnknownShapePath()
289 {
290 static const QString path = ":/other_components/output_unknown.svg";
291 return path;
292 }
293 static const QString& getOnShapePath()
294 {
295 static const QString path = ":/other_components/output_on.svg";
296 return path;
297 }
298 static const QString& getOffShapePath()
299 {
300 static const QString path = ":/other_components/output_off.svg";
301 return path;
302 }
303
304 QRectF boundingRect() const override;
305};
306
312 Q_OBJECT
313public:
314 static constexpr std::string_view COMPONENT_TYPE = "BusOutput";
315
316 explicit GraphicalBusOutput(QGraphicsItem* parent = nullptr);
317
318 int type() const override { return SiliconTypes::BUS_OUTPUT; }
319
320 [[nodiscard]] std::string getTypeName() const override
321 {
322 return std::string(COMPONENT_TYPE);
323 }
324
325 void setComponent(const Component_ptr& component) override;
326
333 void setBusState(const Bus& bus);
334
336 void applyStartValue() override;
337 void resetSimulationState() override;
338
339 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
340 QWidget* widget) override;
341
342private:
344 QRectF boundingRect() const override;
345};
346
347} // namespace SILICON::ui
void applyStartValue() override
Applies the default starting value configured in the component's properties.
void editValue()
Opens a dialog prompting the user to type in a new bus value.
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
void resetSimulationState() override
Resets the visual state to its initial simulation state.
static constexpr std::string_view COMPONENT_TYPE
GraphicalBusInput(QGraphicsItem *parent=nullptr)
void handleSimulationClick() override
Called when the user clicks on the component during a live simulation.
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void refreshFromComponent()
Synchronizes the UI visuals with the underlying logic component's state.
std::string getTypeName() const override
Gets the component type name for serialization.
void setValue(const BusValue &value)
Directly sets the numerical value of the bus.
void setBusState(const Bus &bus)
Updates the display shape based on the state of the incoming bus. Handles edge cases like Error/Unkno...
void applyStartValue() override
Applies the default starting value configured in the component's properties.
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
void resetSimulationState() override
Resets the visual state to its initial simulation state.
static constexpr std::string_view COMPONENT_TYPE
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
std::string getTypeName() const override
Gets the component type name for serialization.
GraphicalBusOutput(QGraphicsItem *parent=nullptr)
void prepareSceneBoundsChange()
Prepares a geometry edit and checks its final bounds on the event loop.
bool scanShape
Whether to scan shape alpha for port placement.
int type() const override
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
GraphicalConstant(QGraphicsItem *parent=nullptr)
QRectF componentNameRect(const QString &name) const
Returns the label rectangle for the current orientation.
bool isPortOrientationUp() const
Returns true when the IO port is currently placed above the shape.
virtual void handleSimulationClick()
Called when the user clicks on the component during a live simulation.
virtual void resetSimulationState()=0
Resets the visual state to its initial simulation state.
void updatePortOrientation()
Rebuilds the single IO port based on the current orientation property.
virtual void applyStartValue()=0
Applies the default starting value configured in the component's properties.
void setComponent(const Component_ptr &component) override
Sets the associated logical component.
static const QFont UI_FONT
Shared, highly-optimized UI font for all IO components. Using an inline static font prevents allocati...
void inputToggled(Bus targetBus, BusValue value, Component_weakPtr source)
Emitted when an input component changes its state.
QString getComponentName() const
Safely retrieves the user-defined name of this component.
GraphicalIO(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
Constructs a GraphicalIO item.
QRectF collisionRectForPortSide(PortSide side) const
Returns local wire-obstacle bounds for a proposed port side.
void setPortOrientation(PortSide side)
Persists and applies a cardinal port orientation.
QPoint portPositionFor(PortSide side) const
Returns the local port position for a proposed cardinal side.
void installPortOrientationCallback()
Installs the callback that moves the IO port when orientation changes.
void triggerGeometryChange()
Notifies the Qt framework that the bounding rect has changed.
int type() const override
void setState(State state)
Explicitly sets the input state and updates visuals/simulation logic.
void applyStartValue() override
Applies the default starting value configured in the component's properties.
GraphicalInput(QGraphicsItem *parent=nullptr)
static const QString & getOnShapePath()
State getState() const
Retrieves the current state of the input.
void resetSimulationState() override
Resets the visual state to its initial simulation state.
static constexpr std::string_view COMPONENT_TYPE
void handleSimulationClick() override
Called when the user clicks on the component during a live simulation.
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
void toggle()
Toggles the visual and logical state between HIGH and LOW.
std::string getTypeName() const override
static const QString & getOffShapePath()
GraphicalLogicComponent(ItemCategory category, const Component_ptr &component, QGraphicsItem *shape, QGraphicsItem *parent, bool scanShape=false)
GraphicalOutputSingle(QGraphicsItem *parent=nullptr)
void setState(State state)
Updates the visual representation based on the wire state.
void applyStartValue() override
Applies the default starting value configured in the component's properties.
static const QString & getOnShapePath()
void resetSimulationState() override
Resets the visual state to its initial simulation state.
static constexpr std::string_view COMPONENT_TYPE
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
std::string getTypeName() const override
Gets the component type name for serialization.
static const QString & getOffShapePath()
static const QString & getUnknownShapePath()
std::shared_ptr< Component > Component_ptr
std::weak_ptr< Component > Component_weakPtr
Definition wire.hpp:52
std::vector< State > BusValue
Definition wire.hpp:120
@ SINGLE_INPUT
Definition enums.hpp:80
@ SINGLE_OUTPUT
Definition enums.hpp:81