-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.h
491 lines (485 loc) · 19.4 KB
/
node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include<QGraphicsItem>
#include<QGraphicsScene>
#include<QPainter>
#include<unordered_map>
#include<QGraphicsSceneMouseEvent>
#include<QDebug>
#include<QMenu>
#include"imagegraphicsview.h"
#include<unordered_set>
using namespace std;
enum inputConnection{noInput,oneInput,twoInput};
enum outputConnection{noOutput,oneOutput};
enum connectorState{hoverEnter,hoverExit,connected};
//hoverEnter - hover enter when not connected
//hovetExit - hover exit when not connected
//connected - when an edge is connected
class PropertiesWindow:public QWidget{
Q_OBJECT
public:
PropertiesWindow(QString title){
setWindowTitle(title);
}
};
class connector:public QGraphicsItem{
int width,height;
connectorState state;
void hoverEnterEvent(QGraphicsSceneHoverEvent *event){
if(state!=connected) state=hoverEnter;
update();
QGraphicsItem::hoverEnterEvent(event);
};
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event){
if(state!=connected) state=hoverExit;
update();
QGraphicsItem::hoverLeaveEvent(event);
};
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr){
QRectF rect=boundingRect();
QPainterPath path;
path.addRoundedRect(rect,2,2);
QPen pen(Qt::white);
painter->setPen(pen);
painter->drawPath(path);
if(state==hoverEnter) painter->fillRect(rect,QBrush(QColor(200,200,200)));
else if(state==connected) painter->fillRect(rect,QBrush(Qt::white));
};
public:
connector(QGraphicsItem*parent=NULL,int width=10,int height=10):QGraphicsItem(parent),width(width),height(height){
setAcceptHoverEvents(true);
state=hoverExit;
setToolTip("Right-click for creating edges\nMiddle-button for deleting edges");
}
QRectF boundingRect() const{
return QRectF(0,0,width,height);
}
int getWidth(){
return width;
}
int getHeight(){
return height;
}
void setState(connectorState state){
this->state=state;
update();
}
};
class node:public QObject,public QGraphicsItem{
Q_OBJECT
protected:
QGraphicsScene*scene;
int width,height;
inputConnection iType;
outputConnection oType;
node*input1,*input2;
QGraphicsPathItem*inputLine1,*inputLine2;
unordered_multimap<node*,QGraphicsPathItem*>output;
connector *ci1,*ci2,*co;
QString name;
PropertiesWindow*propW;
QString propertiesText;
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event){};
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr){
QRectF rect=boundingRect();
QPainterPath path;
path.addRoundedRect(rect,15,15);
QLinearGradient grad(0,0,0,height);
grad.setColorAt(0,QColor(184,15,10));
grad.setColorAt(0.5,QColor(240,36,0));
grad.setColorAt(1,QColor(184,15,10));
painter->fillPath(path,grad);
QPen pen;
pen.setWidth(2);
painter->setPen(pen);
painter->drawPath(path);
QFont font;
font.setPixelSize(20);
painter->setFont(font);
painter->drawText(rect,Qt::AlignCenter,name);
font.setPixelSize(10);
painter->setFont(font);
if(iType==oneInput){
QRectF rect1(0,ci1->getHeight()/2-1,width/2-ci1->getWidth()/2-5,15);
painter->drawText(rect1,Qt::AlignRight,"input");
}else if(iType==twoInput){
QRectF rect1(0,ci1->getHeight()/2-1,width/4-ci1->getWidth()/2-2,15);
painter->drawText(rect1,Qt::AlignRight,"A");
rect1.setWidth(3*width/4-ci2->getWidth()/2-2);
painter->drawText(rect1,Qt::AlignRight,"B");
}
if(oType==oneOutput){
QRectF rect1(0,0,width/2-co->getWidth()/2-5,height-co->getHeight()/2+2);
painter->drawText(rect1,Qt::AlignRight|Qt::AlignBottom,"output");
}
};
QVariant itemChange(GraphicsItemChange change, const QVariant &value){
if(change==ItemPositionChange){
QPointF p=value.toPointF();
//inputLine
if(iType==oneInput){
if(input1!=NULL){
QPainterPath path;
path.moveTo(input1->x()+input1->getWidth()/2,input1->y()+input1->getHeight()-input1->getCO()->getHeight());
path.cubicTo(input1->x()+input1->getWidth()/2,input1->y()+input1->getHeight()-input1->getCO()->getHeight()+40,
p.x()+width/2,p.y()+ci1->getHeight()-40,p.x()+width/2,p.y()+ci1->getHeight());
inputLine1->setPath(path);
}
}
else if(iType==twoInput){
if(input1!=NULL){
QPainterPath path;
path.moveTo(input1->x()+input1->getWidth()/2,input1->y()+input1->getHeight()-input1->getCO()->getHeight());
path.cubicTo(input1->x()+input1->getWidth()/2,input1->y()+input1->getHeight()-input1->getCO()->getHeight()+40,
p.x()+width/4,p.y()+ci1->getHeight()-40,p.x()+width/4,p.y()+ci1->getHeight());
inputLine1->setPath(path);
}
if(input2!=NULL){
QPainterPath path;
path.moveTo(input2->x()+input2->getWidth()/2,input2->y()+input2->getHeight()-input2->getCO()->getHeight());
path.cubicTo(input2->x()+input2->getWidth()/2,input2->y()+input2->getHeight()-input2->getCO()->getHeight()+40,
p.x()+3*width/4,p.y()+ci2->getHeight()-40,p.x()+3*width/4,p.y()+ci2->getHeight());
inputLine2->setPath(path);
}
}
//outputLine
for(auto it=output.begin();it!=output.end();it++){
node*n=it->first;
QGraphicsPathItem*pitem=it->second;
if(n->iType==oneInput){
QPainterPath path;
path.moveTo(x()+width/2,y()+height-co->getHeight());
path.cubicTo(x()+width/2,y()+height-co->getHeight()+40,n->x()+n->width/2,n->y()+n->ci1->getHeight()-40,
n->x()+n->width/2,n->y()+n->getCI1()->getHeight());
pitem->setPath(path);
}else if(n->iType==twoInput){
if(n->inputLine1==pitem){
QPainterPath path;
path.moveTo(x()+width/2,y()+height-co->getHeight());
path.cubicTo(x()+width/2,y()+height-co->getHeight()+40,n->x()+n->width/4,n->y()+n->ci1->getHeight()-40,
n->x()+n->width/4,n->y()+n->getCI1()->getHeight());
pitem->setPath(path);
}else{
QPainterPath path;
path.moveTo(x()+width/2,y()+height-co->getHeight());
path.cubicTo(x()+width/2,y()+height-co->getHeight()+40,n->x()+3*n->width/4,n->y()+n->ci1->getHeight()-40,
n->x()+3*n->width/4,n->y()+n->getCI1()->getHeight());
pitem->setPath(path);
}
}
}
}
return QGraphicsItem::itemChange(change,value);
};
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
QPointF rel=event->scenePos();
rel.setX(rel.x()-x());
rel.setY(rel.y()-y());
QString type;
bool clicked=false;
if(iType==oneInput){
if(rel.x()>=(width-ci1->getWidth())/2 && rel.x()<=(width+ci1->getWidth())/2
&& rel.y()>=ci1->getHeight()/2 && rel.y()<=3*ci1->getHeight()/2)
{
qDebug()<<"ci1";
type="ci1";
clicked=true;
}
}else if(iType==twoInput){
if(rel.x()>=(width-2*ci1->getWidth())/4 && rel.x()<=(width+2*ci1->getWidth())/4
&& rel.y()>=ci1->getHeight()/2 && rel.y()<=3*ci1->getHeight()/2)
{
qDebug()<<"ci1";
type="ci1";
clicked=true;
}
else if(rel.x()>=(3*width-2*ci1->getWidth())/4 && rel.x()<=(3*width+2*ci1->getWidth())/4
&& rel.y()>=ci1->getHeight()/2 && rel.y()<=3*ci1->getHeight()/2)
{
qDebug()<<"ci2";
type="ci2";
clicked=true;
}
}
if(oType==oneOutput){
if(rel.x()>=(width-co->getWidth())/2 && rel.x()<=(width+co->getWidth())/2
&& rel.y()<=height-(co->getHeight()/2) && rel.y()>=height-(3*co->getHeight()/2))
{
qDebug()<<"co";
type="co";
clicked=true;
}
}
if(!clicked && event->button()==Qt::RightButton){
QMenu menu;
QAction*deleteNode=menu.addAction("Delete Node");
QAction*properties;
if(propW!=NULL) properties=menu.addAction(propertiesText);
QAction*current=menu.exec(event->screenPos());
if(current==deleteNode){
if(input1!=NULL) removeInput1();
if(input2!=NULL) removeInput2();
if(oType==oneOutput){
unordered_set<node*> set;
for(auto it=output.begin();it!=output.end();it++) set.insert(it->first);
removeAllOutputs();
for(auto it=set.begin();it!=set.end();it++) (*it)->refresh();
}
delete this;
}
if(current==properties){
if(!(propW->isVisible())) propW->show();
else propW->activateWindow();
}
}
else if(clicked && event->button()==Qt::MiddleButton){
if(connection.first.first==NULL){
if(type=="ci1" && input1!=NULL){
removeInput1();
refresh();
}
else if(type=="ci2" && input2!=NULL){
removeInput2();
refresh();
}
else if(type=="co"){
unordered_set<node*> set;
for(auto it=output.begin();it!=output.end();it++) set.insert(it->first);
removeAllOutputs();
for(auto it=set.begin();it!=set.end();it++) (*it)->refresh();
}
}
}
else if(clicked && event->button()==Qt::RightButton){
if(connection.first.first==NULL){
connection.first={this,type};
connection.second=new QGraphicsPathItem;
QPen pen(Qt::white);
pen.setWidth(2);
connection.second->setPen(pen);
scene->addItem(connection.second);
connection.second->setZValue(-1);
if(type=="ci1") ci1->setState(connected);
else if(type=="ci2") ci2->setState(connected);
else co->setState(connected);
}else{
node*&n=connection.first.first;
QString&ctype=connection.first.second;
QGraphicsPathItem*&pitem=connection.second;
if((connection.first.second=="ci1" || connection.first.second=="ci2") && type=="co" && connection.first.first!=this){
qDebug()<<"valid";
output.insert({n,pitem});
if(n->iType==oneInput){
if(n->input1!=NULL) n->removeInput1();
n->input1=this;
n->inputLine1=pitem;
n->getCI1()->setState(connected);
}else if(n->iType==twoInput){
if(ctype=="ci1"){
if(n->input1!=NULL) n->removeInput1();
n->input1=this;
n->inputLine1=pitem;
n->getCI1()->setState(connected);
}else{
if(n->input2!=NULL) n->removeInput2();
n->input2=this;
n->inputLine2=pitem;
n->getCI2()->setState(connected);
}
}
n->refresh();
co->setState(connected);
n=NULL;
pitem=NULL;
ctype="";
}else if(connection.first.second=="co" && (type=="ci1" || type=="ci2") && connection.first.first!=this){
qDebug()<<"valid";
n->output.insert({this,pitem});
if(iType==oneInput){
if(input1!=NULL) removeInput1();
input1=n;
inputLine1=pitem;
ci1->setState(connected);
}else if(iType==twoInput){
if(type=="ci1"){
if(input1!=NULL) removeInput1();
input1=n;
inputLine1=pitem;
ci1->setState(connected);
}else if(type=="ci2"){
if(input2!=NULL) removeInput2();
input2=n;
inputLine2=pitem;
ci2->setState(connected);
}
}
refresh();
n=NULL;
pitem=NULL;
ctype="";
}else{
qDebug()<<"invalid";
if(ctype=="ci1") n->getCI1()->setState(hoverExit);
else if(ctype=="ci2") n->getCI2()->setState(hoverExit);
else n->getCO()->setState(hoverExit);
n=NULL;
scene->removeItem(pitem);
ctype="";
}
}
}
QGraphicsItem::mouseReleaseEvent(event);
};
void hoverMoveEvent(QGraphicsSceneHoverEvent *event){
pair<node*,QString>&p=node::connection.first;
node*&n=p.first;
QString&type=p.second;
QGraphicsPathItem*&gpath=node::connection.second;
if(n!=NULL){
QPainterPath path;
if(n->getIType()==oneInput){
if(type=="ci1"){
path.moveTo(n->x()+n->getWidth()/2,n->y()+n->getCI1()->getHeight());
path.cubicTo(n->x()+n->getWidth()/2,n->y()+n->getCI1()->getHeight()-40,
event->scenePos().x(),event->scenePos().y()+40,event->scenePos().x(),event->scenePos().y());
}
}else if(n->getIType()==twoInput){
if(type=="ci1"){
path.moveTo(n->x()+n->getWidth()/4,n->y()+n->getCI1()->getHeight());
path.cubicTo(n->x()+n->getWidth()/4,n->y()+n->getCI1()->getHeight()-40,
event->scenePos().x(),event->scenePos().y()+40,event->scenePos().x(),event->scenePos().y());
}else if(type=="ci2"){
path.moveTo(n->x()+3*n->getWidth()/4,n->y()+n->getCI1()->getHeight());
path.cubicTo(n->x()+3*n->getWidth()/4,n->y()+n->getCI1()->getHeight()-40,
event->scenePos().x(),event->scenePos().y()+40,event->scenePos().x(),event->scenePos().y());
}
}
if(n->getOType()==oneOutput){
if(type=="co"){
path.moveTo(n->x()+n->getWidth()/2,n->y()+n->getHeight()-n->getCO()->getHeight());
path.cubicTo(n->x()+n->getWidth()/2,n->y()+n->getHeight()-n->getCO()->getHeight()+40,
event->scenePos().x(),event->scenePos().y()-40,event->scenePos().x(),event->scenePos().y());
}
}
gpath->setPath(path);
}
QGraphicsItem::hoverMoveEvent(event);
};
public:
node(QGraphicsScene*scene,inputConnection iType=oneInput,outputConnection oType=oneOutput,QString name="node"+QString::number(lastIndex++),int width=200,int height=75)
:scene(scene),width(width),height(height),iType(iType),oType(oType),input1(NULL),input2(NULL),inputLine1(NULL),inputLine2(NULL),name(name)
{
scene->addItem(this);
setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsScenePositionChanges);
setAcceptHoverEvents(true);
if(iType==oneInput){
ci1=new connector(this);
ci1->setPos((width-ci1->getWidth())/2,ci1->getHeight()/2);
}else if(iType==twoInput){
ci1=new connector(this);
ci1->setPos((width-2*ci1->getWidth())/4,ci1->getHeight()/2);
ci2=new connector(this);
ci2->setPos((3*width-2*ci1->getWidth())/4,ci1->getHeight()/2);
}
if(oType==oneOutput){
co=new connector(this);
co->setPos((width-co->getHeight())/2,height-(3*co->getHeight()/2));
}
propW=NULL;
setToolTip("Right-click for context menu");
propertiesText="Properties";
}
~node(){
if(propW!=NULL) delete propW;
}
QRectF boundingRect() const{
return QRectF(0,0,width,height);
};
void removeInput1(){
auto p=input1->output.equal_range(this);
for(auto it=p.first;it!=p.second;){
if(it->second==inputLine1){
input1->output.erase(it);
break;
}
it++;
}
if(input1->output.empty()) input1->getCO()->setState(hoverExit);
input1=NULL;
scene->removeItem(inputLine1);
delete inputLine1;
inputLine1=NULL;
ci1->setState(hoverExit);
}
void removeInput2(){
auto p=input2->output.equal_range(this);
for(auto it=p.first;it!=p.second;){
if(it->second==inputLine2){
input2->output.erase(it);
break;
}
it++;
}
if(input2->output.empty()) input2->getCO()->setState(hoverExit);
input2=NULL;
scene->removeItem(inputLine2);
delete inputLine2;
inputLine2=NULL;
ci2->setState(hoverExit);
}
void removeAllOutputs(){
for(auto it=output.begin();it!=output.end();){
if(it->second==it->first->inputLine1) it->first->removeInput1();
else it->first->removeInput2();
it++;
}
}
int getWidth(){
return width;
}
int getHeight(){
return height;
}
inputConnection getIType(){
return iType;
}
outputConnection getOType(){
return oType;
}
connector* getCI1(){
return ci1;
}
connector* getCI2(){
return ci2;
}
connector* getCO(){
return co;
}
unordered_multimap<node*,QGraphicsPathItem*>* getOutput(){
return &output;
}
node* getInput1(){
return input1;
}
node* getInput2(){
return input2;
}
virtual bool imageCalculate(QImage&image){
return false;
}
float clampF(float i){
if(i>1) return 1;
else if(i<0) return 0;
else return i;
}
int clamp(int i){
if(i>255) return 255;
else if(i<0) return 0;
else return i;
}
static pair<pair<node*,QString>,QGraphicsPathItem*> connection;
static int lastIndex;
public slots:
virtual void refresh(){
for(auto it=output.begin();it!=output.end();it++)it->first->refresh();
}
};