SILICON
Loading...
Searching...
No Matches
diagramView.cpp
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#include "diagramView.hpp"
19
20#include <stdexcept>
21
22namespace SILICON::ui {
23using namespace SILICON::core;
24
25DiagramView::DiagramView(QWidget* parent) : QGraphicsView(parent)
26{
27 setMouseTracking(true);
28 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
29 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
30}
31
33{
34 if (auto* previousScene = qobject_cast<DiagramScene*>(QGraphicsView::scene()))
35 disconnect(previousScene, &DiagramScene::modeChanged, this,
37
38 QGraphicsView::setScene(scene);
39
40 if (!scene)
41 return;
42
45}
46
47void DiagramView::wheelEvent(QWheelEvent* event)
48{
49 const auto* dg = qobject_cast<DiagramScene*>(scene());
50
51 if (!dg) {
52 QGraphicsView::wheelEvent(event);
53 return;
54 }
55
56 if (dg->getInteractionMode() == InteractionMode::WIRE_CREATION_MODE
57 && dg->isWireCreationInProgress()) {
58 event->ignore();
59 return;
60 }
61
62 const bool isPixelDelta = !event->pixelDelta().isNull();
63 const int delta = isPixelDelta ? event->pixelDelta().y() : event->angleDelta().y();
64
65 constexpr qreal ZOOM_STEP = 20.0;
66 constexpr qreal ANGLE_STEP = 120.0;
67 constexpr qreal PIXEL_STEP = 100.0;
68
69 wheelZoomRemainder += delta * ZOOM_STEP / (isPixelDelta ? PIXEL_STEP : ANGLE_STEP);
70
71 const int increment = static_cast<int>(wheelZoomRemainder);
72 wheelZoomRemainder -= increment;
73
74 const auto cursor = event->position().toPoint();
75 const auto before = mapToScene(cursor);
76
77 if (increment != 0 && zoom(zoomLevel + increment)) {
78 const auto after = mapToScene(cursor);
79 centerOn(mapToScene(viewport()->rect().center()) + before - after);
80 }
81
82 event->accept();
83}
84
86{
87 zoom(zoomLevel + 20);
88}
89
91{
92 zoom(zoomLevel - 20);
93}
94
95void DiagramView::zoomToRect(const QRectF& rect)
96{
97 const auto bounds = rect.normalized();
98 const auto available = viewport()->rect().adjusted(2, 2, -2, -2);
99 if (bounds.isEmpty() || available.isEmpty())
100 return;
101
102 const qreal factor = std::min(available.width() / bounds.width(),
103 available.height() / bounds.height());
104 zoom(static_cast<int>(std::clamp(factor * 100.0,
105 qreal(MIN_ZOOM_LEVEL), qreal(MAX_ZOOM_LEVEL))));
106 centerOn(bounds.center());
107}
108
109bool DiagramView::zoom(int level)
110{
111 level = std::clamp(level, MIN_ZOOM_LEVEL, MAX_ZOOM_LEVEL);
112
113 if (level == zoomLevel)
114 return false;
115
116 zoomLevel = level;
117 updateZoom();
118 return true;
119}
120
122{
123 const qreal factor = static_cast<qreal>(zoomLevel) / 100.0;
124 setTransform(QTransform::fromScale(factor, factor));
125}
126
128{
129 const auto* currentScene = scene();
130 centerOn(currentScene ? currentScene->itemsBoundingRect().center() : QPointF{});
131}
132
134{
135 // TODO: Set items flags using this->items()
136
137 setCursor(Qt::ArrowCursor);
138 setDragMode(QGraphicsView::NoDrag);
139
140 switch (mode) {
141 case InteractionMode::NORMAL_MODE: setDragMode(QGraphicsView::RubberBandDrag); break;
142 case InteractionMode::WIRE_CREATION_MODE: setCursor(Qt::CrossCursor); break;
143 case InteractionMode::PAN_MODE:
144 setDragMode(QGraphicsView::ScrollHandDrag);
145 setCursor(Qt::SizeAllCursor);
146 break;
147 case InteractionMode::COMPONENT_PLACING_MODE:
148 case InteractionMode::SIMULATION_MODE: break;
149 default: throw std::logic_error("Unhandled InteractionMode in modeChanged");
150 }
151}
152
153} // namespace SILICON::ui
Graphics scene for the circuit diagram editor.
void modeChanged(InteractionMode mode)
Emitted when the interaction mode changes.
InteractionMode getInteractionMode() const
Gets the current interaction mode.
DiagramView(QWidget *parent=nullptr)
static constexpr int MAX_ZOOM_LEVEL
void modeChanged(InteractionMode mode)
void zoomOut()
Decreases the circuit zoom by one step.
void setScene(DiagramScene *scene)
void zoomIn()
Increases the circuit zoom by one step.
static constexpr int MIN_ZOOM_LEVEL
void zoomToRect(const QRectF &rect)
Centers a scene rectangle and fits it within the existing zoom limits.
void wheelEvent(QWheelEvent *event) override
void centerCircuit()
Pans the viewport so the circuit's bounds are centered.
DiagramScene::InteractionMode InteractionMode
Alias for DiagramScene::InteractionMode.