SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
diagramInteractionController.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 "
diagramInteractionController.hpp
"
19
#include <
ui/circuit/editor/circuitEditor.hpp
>
20
#include <
ui/project/projectSession.hpp
>
21
22
#include <vector>
23
24
#include <QAction>
25
#include <QSignalBlocker>
26
#include <QUndoStack>
27
#include <QWidget>
28
29
#include <nlohmann/json.hpp>
30
31
#include <
core/serialization/component_registry.hpp
>
32
#include <
ui/circuit/components/graphicalLogicComponent.hpp
>
33
#include <
ui/circuit/components/subcircuit/componentShapeEditor.hpp
>
34
#include <
ui/circuit/components/subcircuit/utils.hpp
>
35
#include <
ui/circuit/diagram/diagramView.hpp
>
36
#include <
ui/circuit/diagram/scene/diagramScene.hpp
>
37
#include <
ui/circuit/diagram/undoCommands.hpp
>
38
#include <
ui/circuit/editor/componentCatalogOverlay.hpp
>
39
#include <
ui/serialization/gui_component_factory.hpp
>
40
#include <
ui/shell/inputDialogUtils.hpp
>
41
42
namespace
SILICON::ui
{
43
using namespace
SILICON::core;
44
45
DiagramInteractionController::DiagramInteractionController
(
46
ProjectSession
&
session
,
CircuitEditor
&
circuit
,
ComponentCatalogOverlay
&
catalog
,
47
QUndoStack& history, QWidget&
window
)
48
: QObject(&
window
),
49
session
(
session
),
50
circuit
(
circuit
),
51
catalog
(
catalog
),
52
undoStack
(history),
53
window
(
window
)
54
{
55
}
56
57
void
DiagramInteractionController::bindActions
(
const
CircuitActions
& actions)
58
{
59
connect(actions.
selectAll
, &QAction::triggered,
this
,
60
&
DiagramInteractionController::selectAll
);
61
connect(actions.
rotate
, &QAction::triggered,
this
,
62
&
DiagramInteractionController::rotate
);
63
connect(actions.
autoPlace
, &QAction::triggered,
this
,
64
&
DiagramInteractionController::autoPlace
);
65
connect(actions.
centerView
, &QAction::triggered,
this
,
66
&
DiagramInteractionController::centerView
);
67
connect(actions.
setNormalMode
, &QAction::triggered,
this
,
68
&
DiagramInteractionController::setNormalMode
);
69
connect(actions.
setPanMode
, &QAction::triggered,
this
,
70
&
DiagramInteractionController::setPanMode
);
71
connect(actions.
setWireCreationMode
, &QAction::triggered,
this
,
72
&
DiagramInteractionController::setWireCreationMode
);
73
connect(actions.
setSimulationMode
, &QAction::triggered,
this
,
74
&
DiagramInteractionController::setSimulationMode
);
75
connect(actions.
setComponentPlacingMode
, &QAction::triggered,
this
,
76
&
DiagramInteractionController::setComponentPlacingMode
);
77
connect(actions.
cancelInteraction
, &QAction::triggered,
this
,
78
&
DiagramInteractionController::cancelCurrentInteraction
);
79
connect(actions.
openComponentCatalog
, &QAction::triggered,
this
,
80
&
DiagramInteractionController::showComponentCatalog
);
81
connect(actions.
editSubcircuitShape
, &QAction::triggered,
this
,
82
&
DiagramInteractionController::editActiveSubcircuitShape
);
83
}
84
85
void
DiagramInteractionController::selectAll
()
86
{
87
auto
* scene =
circuit
.scene();
88
scene->setInteractionMode(InteractionMode::NORMAL_MODE);
89
if
(scene->getInteractionMode() != InteractionMode::NORMAL_MODE)
90
return
;
91
for
(
auto
* item : scene->items()) {
92
if
(
hasCategory
(item,
ItemCategory::Component
)
93
||
hasCategory
(item,
ItemCategory::WireSegment
))
94
item->setSelected(
true
);
95
}
96
}
97
98
void
DiagramInteractionController::rotate
()
99
{
100
circuit
.scene()->cancelComponentResize();
101
std::vector<GraphicalComponent*> selectedComponents;
102
for
(
auto
* item :
circuit
.scene()->selectedItems()) {
103
if
(
auto
* component =
104
category_cast<GraphicalComponent>
(item,
ItemCategory::Component
)) {
105
selectedComponents.push_back(component);
106
}
107
}
108
109
switch
(
circuit
.scene()->getInteractionMode()) {
110
case
InteractionMode::NORMAL_MODE: {
111
if
(selectedComponents.size() != 1)
112
return
;
113
114
auto
* component = selectedComponents.front();
115
auto
oldRotation = component->rotation();
116
component->rotate();
117
circuit
.scene()->updateSceneAfterEdit();
118
auto
newRotation = component->rotation();
119
component->setInitialRotation();
120
auto
rotateCmd =
new
RotateItemCommand
(component, oldRotation, newRotation);
121
undoStack
.push(rotateCmd);
122
break
;
123
}
124
case
InteractionMode::COMPONENT_PLACING_MODE: {
125
circuit
.scene()->getComponentToBeDrawn()->rotate();
126
break
;
127
}
128
129
default
:
return
;
130
}
131
}
132
133
void
DiagramInteractionController::autoPlace
()
134
{
135
circuit
.scene()->autoPlaceCircuit();
136
}
137
138
void
DiagramInteractionController::centerView
()
139
{
140
circuit
.view()->zoomToRect(
circuit
.scene()->itemsBoundingRect());
141
}
142
143
void
DiagramInteractionController::editActiveSubcircuitShape
()
144
{
145
if
(
SILICON::project::documentTypeForPath
(
session
.activeDocumentPath)
146
!=
SILICON::project::DocumentType::Circuit
)
147
return
;
148
149
const
auto
slug =
SILICON::project::documentSlugForPath
(
session
.activeDocumentPath);
150
if
(!slug)
151
return
;
152
153
try
{
154
circuit
.flush(
session
.activeDocumentPath);
155
editGraphicalSubcircuitShape
(*slug,
session
.projectContext, &
undoStack
, &
window
);
156
}
catch
(
const
std::exception& e) {
157
inputDialog::warning
(
158
&
window
,
window
.tr(
"Edit shape"
),
159
window
.tr(
"Failed to save the active circuit before editing its shape:\n%1"
)
160
.arg(e.what()));
161
}
162
}
163
164
void
DiagramInteractionController::setNormalMode
()
165
{
166
circuit
.scene()->setInteractionMode(InteractionMode::NORMAL_MODE);
167
}
168
169
void
DiagramInteractionController::setPanMode
()
170
{
171
circuit
.scene()->setInteractionMode(InteractionMode::PAN_MODE);
172
}
173
174
void
DiagramInteractionController::setWireCreationMode
()
175
{
176
circuit
.scene()->setInteractionMode(InteractionMode::WIRE_CREATION_MODE);
177
}
178
179
void
DiagramInteractionController::setSimulationMode
()
180
{
181
const
auto
type =
SILICON::project::documentTypeForPath
(
session
.activeDocumentPath);
182
if
(!type
183
||
SILICON::project::categoryOf
(*type)
184
!=
SILICON::project::DocumentCategory::Diagram
)
185
return
;
186
187
circuit
.scene()->setInteractionMode(InteractionMode::SIMULATION_MODE);
188
}
189
190
void
DiagramInteractionController::setComponentPlacingMode
()
191
{
192
circuit
.scene()->setInteractionMode(InteractionMode::COMPONENT_PLACING_MODE);
193
}
194
195
void
DiagramInteractionController::showComponentCatalog
()
196
{
197
catalog
.setGeometry(
circuit
.view()->viewport()->rect());
198
catalog
.open();
199
}
200
201
void
DiagramInteractionController::cancelCurrentInteraction
()
202
{
203
if
(
catalog
.isVisible())
204
catalog
.dismiss();
205
else
206
circuit
.scene()->cancelCurrentInteraction();
207
}
208
209
}
// namespace SILICON::ui
circuitEditor.hpp
SILICON::ui::CircuitEditor
Definition
circuitEditor.hpp:22
SILICON::ui::ComponentCatalogOverlay
Definition
componentCatalogOverlay.hpp:46
SILICON::ui::DiagramInteractionController::catalog
ComponentCatalogOverlay & catalog
Definition
diagramInteractionController.hpp:63
SILICON::ui::DiagramInteractionController::DiagramInteractionController
DiagramInteractionController(ProjectSession &session, CircuitEditor &circuit, ComponentCatalogOverlay &catalog, QUndoStack &undoStack, QWidget &window)
Definition
diagramInteractionController.cpp:45
SILICON::ui::DiagramInteractionController::setPanMode
void setPanMode()
Definition
diagramInteractionController.cpp:169
SILICON::ui::DiagramInteractionController::rotate
void rotate()
Definition
diagramInteractionController.cpp:98
SILICON::ui::DiagramInteractionController::session
ProjectSession & session
Definition
diagramInteractionController.hpp:61
SILICON::ui::DiagramInteractionController::setSimulationMode
void setSimulationMode()
Definition
diagramInteractionController.cpp:179
SILICON::ui::DiagramInteractionController::setNormalMode
void setNormalMode()
Definition
diagramInteractionController.cpp:164
SILICON::ui::DiagramInteractionController::autoPlace
void autoPlace()
Definition
diagramInteractionController.cpp:133
SILICON::ui::DiagramInteractionController::centerView
void centerView()
Definition
diagramInteractionController.cpp:138
SILICON::ui::DiagramInteractionController::cancelCurrentInteraction
void cancelCurrentInteraction()
Definition
diagramInteractionController.cpp:201
SILICON::ui::DiagramInteractionController::undoStack
QUndoStack & undoStack
Definition
diagramInteractionController.hpp:64
SILICON::ui::DiagramInteractionController::bindActions
void bindActions(const CircuitActions &actions)
Connects the main-window diagram actions to their diagram commands.
Definition
diagramInteractionController.cpp:57
SILICON::ui::DiagramInteractionController::setWireCreationMode
void setWireCreationMode()
Definition
diagramInteractionController.cpp:174
SILICON::ui::DiagramInteractionController::showComponentCatalog
void showComponentCatalog()
Definition
diagramInteractionController.cpp:195
SILICON::ui::DiagramInteractionController::setComponentPlacingMode
void setComponentPlacingMode()
Definition
diagramInteractionController.cpp:190
SILICON::ui::DiagramInteractionController::circuit
CircuitEditor & circuit
Definition
diagramInteractionController.hpp:62
SILICON::ui::DiagramInteractionController::editActiveSubcircuitShape
void editActiveSubcircuitShape()
Opens the graphical shape editor for the active circuit document.
Definition
diagramInteractionController.cpp:143
SILICON::ui::DiagramInteractionController::selectAll
void selectAll()
Definition
diagramInteractionController.cpp:85
SILICON::ui::DiagramInteractionController::window
QWidget & window
Definition
diagramInteractionController.hpp:65
SILICON::ui::RotateItemCommand
Definition
undoCommands.hpp:172
componentCatalogOverlay.hpp
componentShapeEditor.hpp
component_registry.hpp
diagramInteractionController.hpp
diagramScene.hpp
diagramView.hpp
graphicalLogicComponent.hpp
gui_component_factory.hpp
inputDialogUtils.hpp
SILICON::project::DocumentCategory::Diagram
@ Diagram
Definition
projectDocument.hpp:39
SILICON::project::categoryOf
constexpr DocumentCategory categoryOf(const DocumentType type)
Definition
projectDocument.hpp:81
SILICON::project::documentSlugForPath
std::optional< std::string > documentSlugForPath(const std::string_view path)
Definition
projectDocument.cpp:174
SILICON::project::documentTypeForPath
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
Definition
projectDocument.cpp:165
SILICON::project::DocumentType::Circuit
@ Circuit
Definition
projectDocument.hpp:37
SILICON::ui::inputDialog::warning
void warning(QWidget *parent, const QString &title, const QString &text)
Definition
inputDialogUtils.cpp:98
SILICON::ui
Definition
actionGroups.hpp:14
SILICON::ui::hasCategory
bool hasCategory(const QGraphicsItem *item, ItemCategory cat)
Definition
enums.hpp:49
SILICON::ui::category_cast
TargetType * category_cast(QGraphicsItem *item, ItemCategory expectedCategory)
Definition
enums.hpp:60
SILICON::ui::ItemCategory::Component
@ Component
Definition
enums.hpp:32
SILICON::ui::ItemCategory::WireSegment
@ WireSegment
Definition
enums.hpp:31
SILICON::ui::editGraphicalSubcircuitShape
void editGraphicalSubcircuitShape(const std::string &slug, SILICON::project::ProjectContext &project, QUndoStack *undoStack, QWidget *parent)
Opens the modal editor for a project subcircuit's graphical shape.
Definition
componentShapeEditor.cpp:439
projectSession.hpp
SILICON::ui::CircuitActions
Definition
actionGroups.hpp:47
SILICON::ui::CircuitActions::selectAll
QAction * selectAll
Definition
actionGroups.hpp:48
SILICON::ui::CircuitActions::autoPlace
QAction * autoPlace
Definition
actionGroups.hpp:50
SILICON::ui::CircuitActions::editSubcircuitShape
QAction * editSubcircuitShape
Definition
actionGroups.hpp:59
SILICON::ui::CircuitActions::centerView
QAction * centerView
Definition
actionGroups.hpp:51
SILICON::ui::CircuitActions::rotate
QAction * rotate
Definition
actionGroups.hpp:49
SILICON::ui::CircuitActions::openComponentCatalog
QAction * openComponentCatalog
Definition
actionGroups.hpp:58
SILICON::ui::CircuitActions::setPanMode
QAction * setPanMode
Definition
actionGroups.hpp:53
SILICON::ui::CircuitActions::setSimulationMode
QAction * setSimulationMode
Definition
actionGroups.hpp:55
SILICON::ui::CircuitActions::cancelInteraction
QAction * cancelInteraction
Definition
actionGroups.hpp:57
SILICON::ui::CircuitActions::setComponentPlacingMode
QAction * setComponentPlacingMode
Definition
actionGroups.hpp:56
SILICON::ui::CircuitActions::setWireCreationMode
QAction * setWireCreationMode
Definition
actionGroups.hpp:54
SILICON::ui::CircuitActions::setNormalMode
QAction * setNormalMode
Definition
actionGroups.hpp:52
SILICON::ui::ProjectSession
Definition
projectSession.hpp:26
utils.hpp
undoCommands.hpp
src
ui
circuit
editor
diagramInteractionController.cpp
Generated by
1.17.0