SILICON
Toggle main menu visibility
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
22
namespace
SILICON::ui
{
23
using namespace
SILICON::core;
24
25
DiagramView::DiagramView
(QWidget* parent) : QGraphicsView(parent)
26
{
27
setMouseTracking(
true
);
28
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
29
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
30
}
31
32
void
DiagramView::setScene
(
DiagramScene
* scene)
33
{
34
if
(
auto
* previousScene = qobject_cast<DiagramScene*>(QGraphicsView::scene()))
35
disconnect(previousScene, &
DiagramScene::modeChanged
,
this
,
36
&
DiagramView::modeChanged
);
37
38
QGraphicsView::setScene(scene);
39
40
if
(!scene)
41
return
;
42
43
connect(scene, &
DiagramScene::modeChanged
,
this
, &
DiagramView::modeChanged
);
44
modeChanged
(scene->
getInteractionMode
());
45
}
46
47
void
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
85
void
DiagramView::zoomIn
()
86
{
87
zoom
(
zoomLevel
+ 20);
88
}
89
90
void
DiagramView::zoomOut
()
91
{
92
zoom
(
zoomLevel
- 20);
93
}
94
95
void
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
109
bool
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
121
void
DiagramView::updateZoom
()
122
{
123
const
qreal factor =
static_cast<
qreal
>
(
zoomLevel
) / 100.0;
124
setTransform(QTransform::fromScale(factor, factor));
125
}
126
127
void
DiagramView::centerCircuit
()
128
{
129
const
auto
* currentScene = scene();
130
centerOn(currentScene ? currentScene->itemsBoundingRect().center() : QPointF{});
131
}
132
133
void
DiagramView::modeChanged
(
InteractionMode
mode)
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
SILICON::ui::DiagramScene
Graphics scene for the circuit diagram editor.
Definition
diagramScene.hpp:118
SILICON::ui::DiagramScene::modeChanged
void modeChanged(InteractionMode mode)
Emitted when the interaction mode changes.
SILICON::ui::DiagramScene::getInteractionMode
InteractionMode getInteractionMode() const
Gets the current interaction mode.
Definition
diagramScene.hpp:180
SILICON::ui::DiagramView::zoom
bool zoom(int level)
Definition
diagramView.cpp:109
SILICON::ui::DiagramView::DiagramView
DiagramView(QWidget *parent=nullptr)
Definition
diagramView.cpp:25
SILICON::ui::DiagramView::MAX_ZOOM_LEVEL
static constexpr int MAX_ZOOM_LEVEL
Definition
diagramView.hpp:55
SILICON::ui::DiagramView::zoomLevel
int zoomLevel
Definition
diagramView.hpp:56
SILICON::ui::DiagramView::updateZoom
void updateZoom()
Definition
diagramView.cpp:121
SILICON::ui::DiagramView::modeChanged
void modeChanged(InteractionMode mode)
Definition
diagramView.cpp:133
SILICON::ui::DiagramView::zoomOut
void zoomOut()
Decreases the circuit zoom by one step.
Definition
diagramView.cpp:90
SILICON::ui::DiagramView::setScene
void setScene(DiagramScene *scene)
Definition
diagramView.cpp:32
SILICON::ui::DiagramView::zoomIn
void zoomIn()
Increases the circuit zoom by one step.
Definition
diagramView.cpp:85
SILICON::ui::DiagramView::wheelZoomRemainder
qreal wheelZoomRemainder
Definition
diagramView.hpp:57
SILICON::ui::DiagramView::MIN_ZOOM_LEVEL
static constexpr int MIN_ZOOM_LEVEL
Definition
diagramView.hpp:54
SILICON::ui::DiagramView::zoomToRect
void zoomToRect(const QRectF &rect)
Centers a scene rectangle and fits it within the existing zoom limits.
Definition
diagramView.cpp:95
SILICON::ui::DiagramView::wheelEvent
void wheelEvent(QWheelEvent *event) override
Definition
diagramView.cpp:47
SILICON::ui::DiagramView::centerCircuit
void centerCircuit()
Pans the viewport so the circuit's bounds are centered.
Definition
diagramView.cpp:127
diagramView.hpp
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::InteractionMode
DiagramScene::InteractionMode InteractionMode
Alias for DiagramScene::InteractionMode.
Definition
diagramScene.hpp:751
src
ui
circuit
diagram
diagramView.cpp
Generated by
1.17.0