SILICON
Loading...
Searching...
No Matches
graphicalWire.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 <unordered_set>
21#include <vector>
22
23#include <QDebug>
24#include <QGraphicsItem>
25#include <QGraphicsScene>
26#include <QPainter>
27#include <QPainterPath>
28#include <QPoint>
29#include <QRect>
30
31#include <core/wire.hpp>
34
35#include <nlohmann/json.hpp>
36
37
38namespace SILICON::ui {
39using namespace SILICON::core;
40
42class WireManager;
43
59public:
62 ~GraphicalWire() = default;
63
68 explicit GraphicalWire(unsigned int busSize);
70 [[nodiscard]] uint64_t getRouteId() const { return routeId; }
75 void restoreRouteId(uint64_t id);
76
81 void setConnectionError(std::optional<QString> reason);
82
84 [[nodiscard]] std::optional<QString> getConnectionError() const
85 { return connectionError; }
86
91 void addSegment(GraphicalWireSegment* segment);
92
98
103 [[nodiscard]] const std::unordered_set<GraphicalWireSegment*>& getSegments() const
104 {
105 return segments;
106 }
107
111 [[nodiscard]] bool empty() const { return segments.empty(); }
112
117 void setBus(Bus bus) { this->bus = bus; }
118
123 void setBusSize(unsigned int size);
124
129 [[nodiscard]] Bus getBus() const { return bus; }
130
135 [[nodiscard]] size_t getBusSize() const { return bus.size(); }
136
142 void clearBusState();
143
144 // --- Topology ------------------------------------------------------------------------
145
154 [[nodiscard]] std::vector<QPointF> getVertices() const;
155
161 [[nodiscard]] QPainterPath shape() const;
162
163 // ---
164
169 QColor getColor() const;
170
176 static QColor getColor(const GraphicalWire* w);
177
182 void setManager(WireManager* manager) { this->manager = manager; }
183
188 WireManager* getManager() const { return manager; }
189
190private:
191 uint64_t routeId;
192 std::optional<QString> connectionError;
195
197 std::unordered_set<GraphicalWireSegment*> segments;
198
201};
202
217public:
223 explicit GraphicalWireSegment(QPointF firstPoint, QGraphicsItem* parent = nullptr);
224
226 int type() const override { return SiliconTypes::WIRE_SEGMENT; }
227
231 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
232 QWidget* widget) override;
233
235 [[nodiscard]] QPainterPath shape() const override;
236
238 [[nodiscard]] QPainterPath centerlineShape() const;
239
241 [[nodiscard]] QRectF boundingRect() const override;
242
244 [[nodiscard]] QRectF getCollisionRect() const override { return boundingRect(); }
245
251 bool isPointOnPath(QPointF point) const;
252
259 void addPoints();
260
265 void setShowPoints(const std::vector<QPointF>& newShowPoints);
266
271 std::vector<QPointF> getShowPoints() { return showPoints; }
272
274 [[nodiscard]] const std::vector<QPointF>& getPoints() const { return points; }
275
277 [[nodiscard]] std::vector<QPointF> getScenePoints() const;
278
280 [[nodiscard]] QPointF lastPoint() const { return points[points.size() - 1]; }
281
283 [[nodiscard]] QPointF firstPoint() const { return points[0]; }
284
286 [[nodiscard]] QPointF lastShowPoint() const
287 {
288 return showPoints[showPoints.size() - 1];
289 }
290
292 [[nodiscard]] bool hasShowPoints() const { return !showPoints.empty(); }
293
295 [[nodiscard]] bool empty() const { return points.size() == 1; }
296
304 void setPoints(std::vector<QPointF> newPoints);
305
312 [[nodiscard]] std::optional<size_t> pointIndexAt(QPointF localPos) const;
318 [[nodiscard]] std::optional<size_t> edgeIndexAt(QPointF localPos) const;
319
321 [[nodiscard]] bool isFirstPointJunction() const { return firstJunction; }
322
324 [[nodiscard]] bool isLastPointJunction() const { return lastJunction; }
325
327 void setFirstPointJunction(bool v);
328
330 void setLastPointJunction(bool v);
331
334
339 void setGraphicalWire(GraphicalWire* newWire);
340
347 void detachFromWire() { graphicalWire = nullptr; }
348
349 void updateTopology() override;
351 void modeChanged(InteractionMode mode) override;
352
353 ~GraphicalWireSegment() override;
354
362 [[nodiscard]] bool isAlignedWith(const GraphicalWireSegment* other) const;
363
368 [[nodiscard]] nlohmann::ordered_json serialize() const;
369
370private:
372 QPainterPath path;
373
375 QPainterPath showPath;
376
378 std::vector<QPointF> points;
379
381 std::vector<QPointF> showPoints;
382
385
387 bool firstJunction = false;
388
390 bool lastJunction = false;
391
393 std::optional<size_t> hoveredPointIndex = std::nullopt;
394
396 void updatePath();
397
399 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
400
402 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
403
405 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
406
408 void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
409
411 static constexpr int INTERVAL = 60;
412
414 static constexpr int SLASH_LENGTH = 20;
415
417 static constexpr int SLASH_ANGLE = 135;
418
420 static constexpr int BOX_HEIGHT = 20;
421
423 static constexpr int BOX_WIDTH = INTERVAL * 0.6;
424
426 static constexpr double POINT_RADIUS = 4.0;
427
429 static constexpr double GRAB_RADIUS = 8.0;
430};
431
432} // namespace SILICON::ui
GraphicalItem(ItemCategory category=ItemCategory::GraphicalItem, QGraphicsItem *parent=nullptr)
The visible, selectable polyline wire on the diagram scene.
std::vector< QPointF > showPoints
Temporary points while drawing.
void setLastPointJunction(bool v)
Sets last endpoint junction status.
void setPoints(std::vector< QPointF > newPoints)
Replaces all points at once.
void updatePath()
Updates the internal path shapes.
QRectF getCollisionRect() const override
Gets the collision rectangle.
QPainterPath shape() const override
Gets the shape for hit testing.
QPainterPath showPath
The temporary path while drawing.
GraphicalWire * getGraphicalWire() const
Gets the logical wire.
QPainterPath centerlineShape() const
Narrow painted centerline used by component collision checks.
bool lastJunction
Junction flag for last endpoint.
bool isFirstPointJunction() const
Checks if first endpoint is a junction.
void setFirstPointJunction(bool v)
Sets first endpoint junction status.
const std::vector< QPointF > & getPoints() const
Gets all points of the segment.
int type() const override
Returns the item type identifier.
static constexpr int BOX_WIDTH
Width of bus size label box.
QPointF firstPoint() const
Gets the first point.
nlohmann::ordered_json serialize() const
Serializes the wire segment to JSON.
static constexpr int SLASH_ANGLE
Angle for slash decorations.
std::optional< size_t > edgeIndexAt(QPointF localPos) const
Finds the first edge within the grab radius of a local position.
static constexpr int INTERVAL
Interval for bus size decorations.
static constexpr int BOX_HEIGHT
Height of bus size label box.
bool empty() const
Checks if segment has only one point.
static constexpr double POINT_RADIUS
Radius of draggable points.
void setGraphicalWire(GraphicalWire *newWire)
Sets the logical wire, optionally propagating to touching siblings.
GraphicalWireSegment(QPointF firstPoint, QGraphicsItem *parent=nullptr)
Constructs a segment starting at the given point.
std::vector< QPointF > points
The segment points.
std::vector< QPointF > getShowPoints()
Gets temporary points shown while drawing.
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override
Handles hover for point highlighting.
void detachFromWire()
Detaches from the logical wire without side effects.
QPointF lastPoint() const
Gets the last point.
static constexpr int SLASH_LENGTH
Length of slash decorations.
GraphicalWire * graphicalWire
The logical wire this segment belongs to.
std::optional< size_t > hoveredPointIndex
Currently hovered point index.
std::vector< QPointF > getScenePoints() const
Gets all points mapped into scene coordinates.
std::optional< size_t > pointIndexAt(QPointF localPos) const
Gets the index of the point nearest to localPos.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse release.
void modeChanged(InteractionMode mode) override
Clears the drag cursor and resets hover highlighting outside normal mode.
void setShowPoints(const std::vector< QPointF > &newShowPoints)
Sets temporary points shown while drawing.
bool isLastPointJunction() const
Checks if last endpoint is a junction.
bool isPointOnPath(QPointF point) const
Checks if a point is on the wire path.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse press for point dragging.
void addPoints()
Confirms the show points as actual wire points.
QPointF lastShowPoint() const
Gets the last show point.
static constexpr double GRAB_RADIUS
Grab radius for point selection.
QRectF boundingRect() const override
Gets the bounding rectangle.
bool firstJunction
Junction flag for first endpoint.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Paints the wire segment.
bool isAlignedWith(const GraphicalWireSegment *other) const
Checks whether another segment is a straight continuation.
QPainterPath path
The main path.
bool hasShowPoints() const
Whether any temporary preview points are being shown.
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
Handles mouse move for point dragging.
Logical wire entity that groups segments sharing the same bus.
void addSegment(GraphicalWireSegment *segment)
Adds a segment to this wire.
size_t getBusSize() const
Gets the bus size.
WireManager * manager
Wire manager for this wire.
QColor getColor() const
Gets the color for this wire based on bus size.
void removeSegment(GraphicalWireSegment *segment)
Removes a segment from this wire.
WireManager * getManager() const
Gets the wire manager.
GraphicalWire()
Creates a wire with a unique route identifier.
void restoreRouteId(uint64_t id)
Restores a saved identifier and advances the identifier allocator.
bool empty() const
Checks if this wire has no segments.
uint64_t getRouteId() const
Returns the persistent identifier of this logical wire group.
void setBus(Bus bus)
Sets the logical bus for this wire.
QPainterPath shape() const
Gets the combined shape of all segments.
const std::unordered_set< GraphicalWireSegment * > & getSegments() const
Gets all segments belonging to this wire.
void setBusSize(unsigned int size)
Sets the bus size.
std::optional< QString > connectionError
void setConnectionError(std::optional< QString > reason)
Updates the connection error and refreshes segment tooltips and painting.
void setManager(WireManager *manager)
Sets the wire manager.
std::optional< QString > getConnectionError() const
Returns the connection error, or no value when the net has no error.
void clearBusState()
Clears the state of all wires in the bus.
std::vector< QPointF > getVertices() const
Gets vertices at wire endpoints that are not junctions.
std::unordered_set< GraphicalWireSegment * > segments
Set of segments belonging to this wire.
Bus getBus() const
Gets the logical bus.
@ WIRE_SEGMENT
Definition enums.hpp:75
DiagramScene::InteractionMode InteractionMode
Alias for DiagramScene::InteractionMode.