YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
builderscene.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "builderscene.h"
7#include <QDebug>
8#include <QMimeData>
9#include "moduleitem.h"
10#include "sourceportitem.h"
11#include "destinationportitem.h"
12#include <QCursor>
13#include "arrow.h"
14#include <QGraphicsView>
15#include <QGraphicsSceneWheelEvent>
16
18 QGraphicsScene(parent),
19 currentLine(nullptr),
20 startConnectionItem(nullptr),
21 endConnectionItem(nullptr),
22 snap(false),
23 editingMode(false)
24{
25 //connect(this,SIGNAL(changed(QList<QRectF>)),this,SLOT(onSceneChanged(QList<QRectF>)));
26 setStickyFocus(true);
27}
28
29
30
31void BuilderScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
32{
33 qDebug() << "Enter Drag";
34 QString itemType = event->mimeData()->text();
35 if(itemType == "module" ||
36 itemType == "application"){
37 event->setAccepted(true);
38 qDebug() << "Enter Drag ACCEPTED";
39 }else{
40 event->setAccepted(false);
41 qDebug() << "Enter Drag REJECTED";
42 }
43
44
45
46}
47
48void BuilderScene::dropEvent(QGraphicsSceneDragDropEvent *event)
49{
50 qDebug() << "dropEvent";
51 qlonglong pointer = event->mimeData()->data("pointer").toLongLong();
52 QString itemType = event->mimeData()->text();
53
54 // Deselect all
55 foreach (QGraphicsItem *it, selectedItems()) {
56 it->setSelected(false);
57 }
58
59 if(itemType == "module" ){
60 auto* mod = (yarp::manager::Module*)pointer;
61 emit addedModule((void*)mod,event->scenePos());
62 }
63 if(itemType == "application" ){
64 auto* app = (yarp::manager::Application*)pointer;
65 emit addedApplication((void*)app,event->scenePos());
66 }
67
68}
69
70void BuilderScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
71{
72 if(!editingMode){
73 event->setAccepted(false);
74 return;
75 }
76 event->setAccepted(true);
77}
78
79void BuilderScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
80{
81 QPointF p = event->scenePos();
82 QGraphicsItem *it = itemAt(p,QTransform());
83 if(currentLine && !it){
84 removeItem(currentLine);
85 delete currentLine;
86 currentLine = nullptr;
87 }
88
89 QGraphicsScene::mousePressEvent(event);
90
91
92 //startConnectionItem = NULL;
93}
94
95//void BuilderScene::wheelEvent(QGraphicsSceneWheelEvent *event)
96//{
97// qDebug() << event->pos();
98
99// if(event->modifiers() == Qt::CTRL){
100// if(event->delta() > 0){
101// views().first()->scale(1.1,1.1);
102// }else{
103// views().first()->scale(0.9,0.9);
104// }
105
106// views().first()->centerOn(event->pos());
107
108// }
109//}
110
111void BuilderScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
112{
113 QGraphicsScene::mouseReleaseEvent(event);
114}
115
116void BuilderScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
117{
118 //qDebug() << "mouseMoveEvent";
119 if(currentLine){
120 //qDebug() << startingPoint.x() << " || " << startingPoint.y() << " || " << QCursor::pos().x() << " || " << QCursor::pos().y();
121 currentLine->setLine(startingPoint.x(),startingPoint.y(),event->scenePos().x()-1,event->scenePos().y()-1);
122 }
123 QGraphicsScene::mouseMoveEvent(event);
124}
125
126void BuilderScene::onNewConnectionRequested(QPointF p,QGraphicsItem *item)
127{
128
129 startConnectionItem = nullptr;
130 if(!editingMode || !((BuilderItem*)item)->allowOutputConnections()){
131 return;
132 }
133
134 if(!currentLine){
135 startConnectionItem = item;
136 startingPoint = item->mapToScene(p);
137 currentLine = new QGraphicsLineItem();
138 addItem(currentLine);
139 }else{
140 removeItem(currentLine);
141 delete currentLine;
142 currentLine = nullptr;
143 }
144
145
146
147}
148
149void BuilderScene::onNewConnectionAdded(QPointF p,QGraphicsItem *item)
150{
151 qDebug() << "onNewConnectionAdded";
152 if(!editingMode || !item){
153 return;
154 }
155 if(startConnectionItem){
156 if(currentLine){
157 removeItem(currentLine);
158 delete currentLine;
159 currentLine = nullptr;
160 }
161
162 auto* startItem = (BuilderItem*)startConnectionItem;
163 auto* endItem = (BuilderItem*)item;
164
165 if(!startItem || !endItem){
166 return;
167 }
168
169 if(startItem->type() == QGraphicsItem::UserType + (int)ModuleItemType || endItem->type() == QGraphicsItem::UserType + (int)ModuleItemType){
170 return;
171 }
172
173 // controllo di non cambiare una applicazione nested.
174 if(startItem->isNestedInApp() && endItem->isNestedInApp()){
175
176 BuilderItem *startParent;
177 BuilderItem *endParent;
178
179 if(startItem->type() == QGraphicsItem::UserType + (int)ModulePortItemType){
180 startParent = (BuilderItem *)startItem->parentItem()->parentItem();
181 }else{
182 startParent = (BuilderItem *)startItem->parentItem();
183 }
184
185 if(endItem->type() == QGraphicsItem::UserType + (int)ModulePortItemType){
186 endParent = (BuilderItem *)endItem->parentItem()->parentItem();
187 }else{
188 endParent = (BuilderItem *)endItem->parentItem();
189 }
190
191 if(startParent && endParent &&
192 startParent->type() == QGraphicsItem::UserType + (int)ApplicationItemType &&
193 endParent->type() == QGraphicsItem::UserType + (int)ApplicationItemType){
194 if(startParent == endParent){
195 return;
196 }
197 }
198 }
199
200
201
202 if (startItem->allowOutputConnections() &&
203 endItem->allowInputConnections()) {
204 if(!startItem->arrowAlreadyPresent(endItem)){
205 emit addNewConnection(startItem,endItem);
206 }
207 }
208
209 startConnectionItem = nullptr;
210 }
211
212}
213
214void BuilderScene::onSceneChanged(QList<QRectF> rects)
215{
216 foreach (QRectF r, rects) {
217 QList<QGraphicsItem *> startItems = items(r);
218
219 for(int i=0;i<startItems.count();i++){
220 QGraphicsItem *gIt = startItems.at(i);
221 if(gIt){
222 if(gIt == currentLine){
223 return;
224 }
225 if(gIt->type() == QGraphicsItem::UserType + (int) ConnectionItemType){
226
227 return;
228
229 }
230 auto* it = (BuilderItem*)gIt;
231 it->updateConnections();
232 //qDebug() << "UPDATE";
233 }
234 }
235 }
236}
237
239{
240 this->snap = snap;
241 foreach (QGraphicsItem *it, items()) {
242 if(it->type() == QGraphicsItem::UserType + (int)HandleItemType ||
243 it->type() == QGraphicsItem::UserType + (int)ArrowLabelItemType){
244 continue;
245 }
246
247 ((BuilderItem*)it)->snapToGrid(snap);
248 }
249}
int type() const override=0
void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override
void onNewConnectionRequested(QPointF, QGraphicsItem *item)
void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override
void addedModule(void *mod, QPointF)
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
void dropEvent(QGraphicsSceneDragDropEvent *event) override
void addNewConnection(void *start, void *end)
void snapToGrid(bool snap)
void onSceneChanged(QList< QRectF > rects)
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
void onNewConnectionAdded(QPointF, QGraphicsItem *item)
BuilderScene(QObject *parent=0)
void addedApplication(void *app, QPointF)
Class Application.
Class Module.
Definition module.h:99
@ ApplicationItemType
Definition commons.h:17
@ ConnectionItemType
Definition commons.h:17
@ ModulePortItemType
Definition commons.h:17
@ ModuleItemType
Definition commons.h:17
@ HandleItemType
Definition commons.h:17
@ ArrowLabelItemType
Definition commons.h:17