SILICON
Loading...
Searching...
No Matches
graphicalComponent.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 <array>
21#include <ranges>
22#include <span>
23#include <unordered_set>
24
25#include <core/component.hpp>
26#include <nlohmann/json.hpp>
27
33
34#include <QPoint>
35#include <QRect>
36#include <QLineF>
37
38
39namespace SILICON::ui {
40using namespace SILICON::core;
41
42using PortPair = std::pair<QString, QPoint>;
43
45
55class Port : public QGraphicsItem {
56 friend class GraphicalComponent;
57
58private:
60 unsigned int index;
61
63 QPoint position;
64
66 QGraphicsLineItem* line = nullptr;
67
69 QString name;
70
72 unsigned int size = 1;
73
76
78 bool printName = false;
79
82
83 [[nodiscard]] QLineF connectionLine() const;
84 [[nodiscard]] QTransform labelTransform(const QRectF& rect, bool insideShape) const;
86
87public:
96 Port(unsigned int index, QPoint position, QString name, bool printName = false,
97 QGraphicsItem* parent = nullptr);
98
100 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
101 QWidget* widget) override;
102
104 QRectF boundingRect() const override;
105
107 unsigned int getIndex() const { return this->index; }
108
110 QPoint getPosition() const { return this->position; }
111
113 [[nodiscard]] QString getName() const { return this->name; }
114
116 unsigned int getSize() const { return this->size; }
117
119 PortSide getDirection() const { return this->side; }
120
122 void setSize(unsigned int newSize);
123
125 void setInputAssignmentError(bool failed);
126
131 void setLine(QGraphicsLineItem* line);
132
137 [[nodiscard]] QRectF collisionRect() const;
138};
139
155 Q_OBJECT
156 friend class Port;
157public:
159 [[nodiscard]] virtual const PropertyMap& getProperties() const;
160 [[nodiscard]] std::optional<PropertyValue> getProperty(std::string_view key) const;
161 [[nodiscard]] virtual std::optional<std::reference_wrapper<const StringPropertyOptions>>
162 getStringPropertyOptions(std::string_view key) const;
163 [[nodiscard]] virtual bool isMultilineProperty(std::string_view key) const;
165 [[nodiscard]] bool isPropertyHidden(std::string_view key) const;
166 virtual void applyProperty(std::string_view key, const PropertyValue& value);
167
169 [[nodiscard]] virtual bool participatesInCollisions() const { return true; }
170
172 [[nodiscard]] virtual std::span<const ResizeHandle> resizeHandles() const { return {}; }
173 [[nodiscard]] virtual QRectF resizeBounds() const
174 {
176 }
177 [[nodiscard]] virtual PropertyMap resizedProperties(QSizeF size, ResizeHandle handle,
178 const PropertyMap& initial);
179 [[nodiscard]] std::optional<ResizeHandle> resizeHandleAt(QPointF localPos) const;
180 [[nodiscard]] bool resizeHandlesVisible() const;
181 [[nodiscard]] ComponentResizeState resizeState() const;
182 void applyResizeState(const ComponentResizeState& state);
183 void modeChanged(InteractionMode mode) override;
184
187
192 [[nodiscard]] QRectF collisionRectForWires() const;
193
194protected:
198 std::unordered_set<std::string, TransparentStringHash, std::equal_to<>>
200
205 void setItemShape(QGraphicsItem* shape);
206
208 [[nodiscard]] QGraphicsItem* getItemShape() const { return itemShape; }
209
211 [[nodiscard]] QRectF boundingRect() const override;
212
214 [[nodiscard]] QRectF boundingRectWithoutMargins() const;
215
219 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
220 QWidget* widget) override;
221
223 [[nodiscard]] QRectF getCollisionRect() const override;
224
226 [[nodiscard]] bool canRotate() const override { return true; }
227
228 QVariant itemChange(GraphicsItemChange change, const QVariant& value) override;
229
233 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override;
234 void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
235 void hoverLeaveEvent(QGraphicsSceneHoverEvent* event) override;
236
238 bool openDocumentOnDoubleClick(QGraphicsSceneMouseEvent* event,
239 const std::string& path);
240
242 std::vector<Port*> inputPorts;
243
245 std::vector<Port*> outputPorts;
246
251 void setPortLine(Port* port);
252
254
256 bool scanShape = false;
257
259 bool printPortNames = false;
260
262 void clearPorts();
263
264protected:
265 explicit GraphicalComponent(ItemCategory category, QGraphicsItem* shape,
266 QGraphicsItem* parent = nullptr, bool scanShape = false);
267
268public:
269 explicit GraphicalComponent(QGraphicsItem* shape, QGraphicsItem* parent = nullptr,
270 bool scanShape = false);
271
275 void rotate();
276
283 virtual void setPorts(const std::vector<PortPair>& busToPortInputs,
284 const std::vector<PortPair>& busToPortOutputs);
285
287 [[nodiscard]] const std::vector<Port*>& getInputPorts() const { return inputPorts; };
288
290 [[nodiscard]] const std::vector<Port*>& getOutputPorts() const { return outputPorts; };
291
296 [[nodiscard]] auto getPorts() const
297 {
298 return std::array{std::span<Port* const>(inputPorts),
299 std::span<Port* const>(outputPorts)}
300 | std::views::join;
301 }
302
307 [[nodiscard]] virtual std::string getTypeName() const;
308
313 [[nodiscard]] virtual nlohmann::ordered_json serialize() const;
314
319 virtual void loadSerializedState(const nlohmann::json& j);
320
327 static std::unique_ptr<GraphicalComponent> deserialize(const nlohmann::json& j,
328 GUIComponentFactory& factory);
329
330private:
332 void setPortLine(Port* port, QImage& shapeImage);
333
342 QPoint scanImage(const QImage& image, PortSide side, QPoint initialPoint) const;
343
345 QGraphicsItem* itemShape = nullptr;
346};
347
348} // namespace SILICON::ui
Factory for creating graphical circuit components.
QRectF getCollisionRect() const override
Gets physical body and port line bounds for drag/drop.
virtual PropertyMap resizedProperties(QSizeF size, ResizeHandle handle, const PropertyMap &initial)
ComponentResizeState resizeState() const
void setPortLine(Port *port)
Creates the connecting line from port to shape.
void clearPorts()
Removes existing input/output ports and their line items.
void rotate()
Rotates the component by 90 degrees.
void prepareSceneBoundsChange()
Prepares a geometry edit and checks its final bounds on the event loop.
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override
std::optional< PropertyValue > getProperty(std::string_view key) const
void applyResizeState(const ComponentResizeState &state)
GraphicalComponent(ItemCategory category, QGraphicsItem *shape, QGraphicsItem *parent=nullptr, bool scanShape=false)
virtual void setPorts(const std::vector< PortPair > &busToPortInputs, const std::vector< PortPair > &busToPortOutputs)
Sets input and output ports.
virtual bool participatesInCollisions() const
Annotations remain selectable but do not obstruct placement or routing.
const std::vector< Port * > & getOutputPorts() const
Borrows output ports; copy before replacing the component ports.
std::vector< Port * > outputPorts
Output connection ports.
virtual nlohmann::ordered_json serialize() const
Serializes the component to JSON.
virtual const PropertyMap & getProperties() const
Properties exposed by graphical and simulation components alike.
QRectF boundingRectWithoutMargins() const
Gets bounding rectangle without margins.
bool canRotate() const override
Components can rotate by default.
QGraphicsItem * itemShape
The visual shape item.
std::optional< ResizeHandle > resizeHandleAt(QPointF localPos) const
virtual void applyProperty(std::string_view key, const PropertyValue &value)
QPoint scanImage(const QImage &image, PortSide side, QPoint initialPoint) const
Scans image for first non-transparent pixel.
virtual std::string getTypeName() const
Gets the component type name for serialization.
std::unordered_set< std::string, TransparentStringHash, std::equal_to<> > hiddenProperties
Property names hidden from the panel, still accessible and serialized. Add names in subclasses with h...
QRectF collisionRectForWires() const
Gets the collision rectangle extended for wire connections.
const std::vector< Port * > & getInputPorts() const
Borrows input ports; copy before replacing the component ports.
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override
QGraphicsItem * getItemShape() const
Gets the component shape.
bool printPortNames
Whether to print port names.
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
Handles double-clicks not consumed by a component-specific action.
void modeChanged(InteractionMode mode) override
Called when the interaction mode changes.
virtual std::span< const ResizeHandle > resizeHandles() const
bool isPropertyHidden(std::string_view key) const
Whether a property is omitted from the properties panel.
virtual bool isMultilineProperty(std::string_view key) const
auto getPorts() const
Borrows every port, inputs first, without copying or deduplicating.
QRectF boundingRect() const override
Gets bounding rectangle with selection margin.
virtual void loadSerializedState(const nlohmann::json &j)
Loads subclass-specific serialized state.
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Paints the component (selection outline).
virtual std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions(std::string_view key) const
bool openDocumentOnDoubleClick(QGraphicsSceneMouseEvent *event, const std::string &path)
Queues document navigation for a left double-click in normal mode.
static std::unique_ptr< GraphicalComponent > deserialize(const nlohmann::json &j, GUIComponentFactory &factory)
Deserializes a component from JSON using the factory.
std::vector< Port * > inputPorts
Input connection ports.
bool scanShape
Whether to scan shape alpha for port placement.
void setItemShape(QGraphicsItem *shape)
Sets the component shape graphics item.
GraphicalItem(ItemCategory category=ItemCategory::GraphicalItem, QGraphicsItem *parent=nullptr)
Represents a connection point on a component.
bool printName
Whether to write the port name on the component.
QPoint getPosition() const
Gets the port position.
Port(unsigned int index, QPoint position, QString name, bool printName=false, QGraphicsItem *parent=nullptr)
Constructs a port.
QTransform labelTransform(const QRectF &rect, bool insideShape) const
QGraphicsLineItem * line
Visual line connecting to component.
bool inputAssignmentError
Whether the net associated with this port has an error.
unsigned int index
Port index in the port list.
PortSide side
Direction of the connecting line from port to component.
PortSide getDirection() const
Gets the port line direction.
unsigned int getIndex() const
Gets the port index.
QPoint position
Position relative to component center.
QString getName() const
Gets the port name.
friend class GraphicalComponent
unsigned int size
Number of wires represented by this port.
QRectF collisionRect() const
Gets the collision rectangle for wire detection.
QString name
Port name.
QRectF boundingRect() const override
Gets visual bounds, including port labels and stroked markers.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Paints port names, bus markers, and input assignment errors.
void setInputAssignmentError(bool failed)
Marks whether this port failed input assignment during simulation setup.
QLineF connectionLine() const
void setLine(QGraphicsLineItem *line)
Sets the connecting line.
void setSize(unsigned int newSize)
Sets the port bus size.
unsigned int getSize() const
Gets the port bus size.
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::unordered_map< std::string, PropertyValue, TransparentStringHash, TransparentStringEqual > PropertyMap
Map from property names to their values.
std::pair< QString, QPoint > PortPair
DiagramScene::InteractionMode InteractionMode
Alias for DiagramScene::InteractionMode.