SILICON
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
21
22#include <vector>
23
24#include <QAction>
25#include <QSignalBlocker>
26#include <QUndoStack>
27#include <QWidget>
28
29#include <nlohmann/json.hpp>
30
41
42namespace SILICON::ui {
43using namespace SILICON::core;
44
56
58{
59 connect(actions.selectAll, &QAction::triggered, this,
61 connect(actions.rotate, &QAction::triggered, this,
63 connect(actions.autoPlace, &QAction::triggered, this,
65 connect(actions.centerView, &QAction::triggered, this,
67 connect(actions.setNormalMode, &QAction::triggered, this,
69 connect(actions.setPanMode, &QAction::triggered, this,
71 connect(actions.setWireCreationMode, &QAction::triggered, this,
73 connect(actions.setSimulationMode, &QAction::triggered, this,
75 connect(actions.setComponentPlacingMode, &QAction::triggered, this,
77 connect(actions.cancelInteraction, &QAction::triggered, this,
79 connect(actions.openComponentCatalog, &QAction::triggered, this,
81 connect(actions.editSubcircuitShape, &QAction::triggered, this,
83}
84
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()) {
94 item->setSelected(true);
95 }
96}
97
99{
100 circuit.scene()->cancelComponentResize();
101 std::vector<GraphicalComponent*> selectedComponents;
102 for (auto* item : circuit.scene()->selectedItems()) {
103 if (auto* 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
134{
135 circuit.scene()->autoPlaceCircuit();
136}
137
139{
140 circuit.view()->zoomToRect(circuit.scene()->itemsBoundingRect());
141}
142
144{
145 if (SILICON::project::documentTypeForPath(session.activeDocumentPath)
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) {
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
165{
166 circuit.scene()->setInteractionMode(InteractionMode::NORMAL_MODE);
167}
168
170{
171 circuit.scene()->setInteractionMode(InteractionMode::PAN_MODE);
172}
173
175{
176 circuit.scene()->setInteractionMode(InteractionMode::WIRE_CREATION_MODE);
177}
178
180{
181 const auto type = SILICON::project::documentTypeForPath(session.activeDocumentPath);
182 if (!type
185 return;
186
187 circuit.scene()->setInteractionMode(InteractionMode::SIMULATION_MODE);
188}
189
191{
192 circuit.scene()->setInteractionMode(InteractionMode::COMPONENT_PLACING_MODE);
193}
194
196{
197 catalog.setGeometry(circuit.view()->viewport()->rect());
198 catalog.open();
199}
200
202{
203 if (catalog.isVisible())
204 catalog.dismiss();
205 else
206 circuit.scene()->cancelCurrentInteraction();
207}
208
209} // namespace SILICON::ui
DiagramInteractionController(ProjectSession &session, CircuitEditor &circuit, ComponentCatalogOverlay &catalog, QUndoStack &undoStack, QWidget &window)
void bindActions(const CircuitActions &actions)
Connects the main-window diagram actions to their diagram commands.
void editActiveSubcircuitShape()
Opens the graphical shape editor for the active circuit document.
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< std::string > documentSlugForPath(const std::string_view path)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
void warning(QWidget *parent, const QString &title, const QString &text)
bool hasCategory(const QGraphicsItem *item, ItemCategory cat)
Definition enums.hpp:49
TargetType * category_cast(QGraphicsItem *item, ItemCategory expectedCategory)
Definition enums.hpp:60
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.