File: graphicswidget.cpp

package info (click to toggle)
socnetv 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,028 kB
  • sloc: cpp: 12,953; makefile: 75
file content (903 lines) | stat: -rwxr-xr-x 24,978 bytes parent folder | download
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/***************************************************************************
 SocNetV: Social Networks Visualizer
 version: 0.90
 Written in Qt 4.4

                        graphicswidget.cpp description
                             -------------------
    copyright            : (C) 2005-2010 by Dimitris B. Kalamaras
    email                : dimitris.kalamaras@gmail.com
 ***************************************************************************/

/*******************************************************************************
*     This program is free software: you can redistribute it and/or modify     *
*     it under the terms of the GNU General Public License as published by     *
*     the Free Software Foundation, either version 3 of the License, or        *
*     (at your option) any later version.                                      *
*                                                                              *
*     This program is distributed in the hope that it will be useful,          *
*     but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
*     GNU General Public License for more details.                             *
*                                                                              *
*     You should have received a copy of the GNU General Public License        *
*     along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
********************************************************************************/

#include "graphicswidget.h"

#include <QGraphicsScene>
#include <QResizeEvent>
#include <QMouseEvent>
#include <QDebug>
#include <QWheelEvent>
#include <math.h>
#include "mainwindow.h"
#include "node.h"
#include "edge.h"
#include "nodenumber.h"
#include "nodelabel.h"
#include "backgrcircle.h"
#include "edgeweight.h"

/** 
	Constructor method. Called when a GraphicsWidget object is created in MW 
*/
GraphicsWidget::GraphicsWidget( QGraphicsScene *sc, MainWindow* par)  : QGraphicsView ( sc,par) {
	setScene(sc);
	secondDoubleClick=FALSE;
	dynamicMovement=FALSE;
	moving=0;
	timerId=0;
	layoutType=0;
	m_nodeLabel="";
	zoomIndex=3;
	m_currentScaleFactor = 1;
	m_currentRotationAngle = 0;
	markedNodeExists=false; //used in findNode()
}



/**
	http://thesmithfam.org/blog/2007/02/03/qt-improving-qgraphicsview-performance/#comment-7215
*/
void GraphicsWidget::paintEvent ( QPaintEvent * event ){
	QPaintEvent *newEvent=new QPaintEvent(event->region().boundingRect());
	QGraphicsView::paintEvent(newEvent);
	delete newEvent;
}



/** 
	Clears the scene 
*/
void GraphicsWidget::clear() {
	int i=0;
	nodeHash.clear();
	QList<QGraphicsItem *> allItems=scene()->items();
	foreach (QGraphicsItem *item, allItems ) {
		(item)->hide();
		scene()->removeItem (item);
		i++;
	}
}





/**	
	Adds a new node onto the scene 
	Called from Graph::createVertex method when:
		we load files or
		the user presses "Add Node" button or
		the user double clicks (mouseDoubleClickEvent() calls Graph::createVertex
*/
void GraphicsWidget::drawNode(
		int num, int size, QString nodeColor,
		QString numberColor, int numberSize,
		QString nodeLabel, QString labelColor, int labelSize,
		QPointF p,
		QString shape,
		bool showLabels, bool numberInsideNode, bool showNumbers
		) {
	qDebug()<< "GW: drawNode(): drawing new node at: " 
			<< p.x() << ", "<< p.y() ;

	if (numberInsideNode)
		size = size +3;
	
	Node *jim= new Node (
			this, num, size, nodeColor, shape,
			numberInsideNode, m_labelDistance, m_numberDistance,
			p
			);

	//Drawing node label - label will be moved by the node movement (see last code line in this method)
	NodeLabel *labelJim = new  NodeLabel (jim, labelSize, nodeLabel, scene() );
	labelJim -> setDefaultTextColor (labelColor);
	labelJim -> setTextInteractionFlags(Qt::TextEditorInteraction);
	
	if (showLabels) {
		//qDebug()<< "GW: drawNode: display label " <<  nodeLabel.toAscii() << " for node " << num;
	}
	else {
		//qDebug()<<"GW: drawNode: hiding label for node " << num;
		labelJim->hide();
	}

	// drawing node number - label will be moved by the node movement (see last code line in this method)
	if (numberInsideNode)
		numberSize = size-2;

	NodeNumber *numberJim = new  NodeNumber ( jim, numberSize, QString::number(num), scene() );
	numberJim -> setDefaultTextColor (numberColor);
	
	if (!showNumbers){
		numberJim->hide();
	}
	nodeHash.insert(num, jim);//add new node to a container to ease finding, edge creation etc
	jim -> setPos( p.x(), p.y());	//finally, move the node where it belongs!
}






/** Draws an edge from source to target Node. 
	This is used when we do not have references to nodes - only nodeNumbers:
	a) when we load a network file (check = FALSE)
	b) when the user clicks on the AddLink button on the MW.
*/
void GraphicsWidget::drawEdge(int i, int j, float weight, bool reciprocal, bool drawArrows, QString color, bool bezier){
	qDebug()<<"GW: drawEdge ("<< i<< ","<< j<< ") with weight "<<weight << " - nodeHash reports "<< nodeHash.size()<<" nodes.";
	if (i == j ) {
		bezier = true;		
	}
	qDebug()<< "GW: drawEdge() drawing edge now!"<< " From node "
			<<  nodeHash.value(i)->nodeNumber()<< " to node "
			<<  nodeHash.value(j)->nodeNumber() << " weight "
			<< weight << " nodesize "
			<<  m_nodeSize << " edgecolor "<< color ;
	Edge *edge=new Edge (this, nodeHash.value(i), nodeHash.value(j), weight,
			     m_nodeSize, color, reciprocal, drawArrows, bezier);
	edge -> setZValue(253);		//Edges have lower z than nodes. Nodes always appear above edges.
	// Keep it here so that it doesnt interfere with dashed lines.
	edge->setBoundingRegionGranularity(0.05);	// Slows down the universe...Keep it 0.05...
	//edge->setCacheMode (QGraphicsItem::DeviceCoordinateCache);  //Also slows down the universe...

	QString edgeName = QString::number(i) + QString(">")+ QString::number(j);
	qDebug()<<"GW: drawEdge() - adding new edge between "<<i << " and "<< j<< " to edgesMap. Name: "<<edgeName.toAscii();
	edgesMap [edgeName] =  edge;

	qDebug()<< "GW: drawNode(): drawing edge weight number...";
	double x = ( (nodeHash.value(i))->x() + (nodeHash.value(j))->x() ) / 2.0;
	double y = ( (nodeHash.value(i))->y() + (nodeHash.value(j))->y() ) / 2.0;

	EdgeWeight *edgeWeight = new  EdgeWeight (edge, 7, QString::number(weight), scene() );
	edgeWeight-> setPos(x,y);
	edgeWeight-> setDefaultTextColor (color);
	edgeWeight-> hide();

	qDebug()<< "Scene items now: "<< scene()->items().size() << " - GW items now: "<< items().size();
}




/**
	Called from Graph to make an existing arc symmetric (reciprocal)
*/
void GraphicsWidget::drawEdgeReciprocal(int source, int target){
	qDebug("GW: drawEdgeReciprocal ()");
	QString edgeName = QString::number(source) + QString(">")+ QString::number(target);
	qDebug("GW: making existing edge between %i and %i reciprocal. Name: "+edgeName.toAscii(), source, target );
	edgesMap [edgeName]->makeReciprocal();
}



/**
	Called from Graph to unmake an existing symmetric (reciprocal) edge to one-directed only.
*/
void GraphicsWidget::unmakeEdgeReciprocal(int source, int target){
	qDebug("GW: unmakeEdgeReciprocal ()");
	QString edgeName = QString::number(source) + QString(">")+ QString::number(target);
	qDebug("GW: removing edge between %i and %i. Name: "+edgeName.toAscii(), source, target );
	edgesMap [edgeName]->unmakeReciprocal();
}



/** 	
	Called when the user middle-clicks on two nodes consecutively. .
	It saves the source & target nodes that were clicked 
	On the second middle-click, it emits the userMiddleClicked() signal to MW, 
	which will notify activeGraph, which in turn will signal back to drawEdge()...
*/
void GraphicsWidget::startEdge(Node *node){
	if (secondDoubleClick){
		qDebug("GW: startEdge(): this is the second double click. Emitting userMiddleClicked() to create edge");
		secondNode=node;
		emit userMiddleClicked(firstNode->nodeNumber(), secondNode->nodeNumber(), 1.0);
		( (MainWindow*)parent() )->setCursor(Qt::ArrowCursor);
		secondDoubleClick=FALSE;
	}
	else{
		qDebug("GW: startEdge(): this is the first double click.");
		firstNode=node;
		secondDoubleClick=TRUE;
		( (MainWindow*)parent() )->setCursor( Qt::PointingHandCursor); 
	}
}



/** 
	This is called from each node when the user clicks on it.
	It emits the selectedNode signal to MW which is used to
	- display node info on the status bar
	- notify context menus for the clicked node.
*/
void GraphicsWidget::nodeClicked(Node *node){
	qDebug ("GW: Emitting selectedNode()");
	emit selectedNode(node);
}



/** 
	This is called from each edge when the user clicks on it.
	It emits the selectedEdge signal to MW which is used to
	- display edge info on the status bar
	- notify context menus for the clicked edge.
*/
void GraphicsWidget::edgeClicked(Edge *edge){
	qDebug ("GW: Emitting selectedEdge()");
	emit selectedEdge(edge);
}




/** 
	Called from each node when they move.
	Updates 
	- node coordinates in activeGraph (via updateNodeCoords() signal)

*/
void GraphicsWidget::nodeMoved(int number, int x, int y){
	//qDebug ("GW: nodeMoved() for %i with %i, %i. Emitting updateNodeCoords() signal", number, x,y);
	emit updateNodeCoords(number, x, y);
}



/** 
	Called from activeGraph to update node coordinates 
	on the canvas  
*/
void GraphicsWidget::moveNode(int number, int x, int y){
	qDebug ("GW: updateNode() %i with %i, %i", number, x,y);
	nodeHash.value(number)->setPos(x,y);
}


/**
	Called from Graph signal eraseNode(int) 
*/
void GraphicsWidget::eraseNode(long int doomedJim){
	qDebug() << "GW: Deleting node "<< doomedJim;
	QList<QGraphicsItem *> list=scene()->items();
	qDebug("GW: Scene items= %i - View items : %i",scene()->items().size(), items().size());
	for (QList<QGraphicsItem *>::iterator it=list.begin(); it!=list.end(); it++) {
		if ( (*it)->type()==TypeNode) {
			Node *jim=(Node*) (*it);
			if ( jim->nodeNumber()==doomedJim)	{
				qDebug() << "GW: found doomedJim " <<  jim->nodeNumber() << " Demanding node->die() :)" ;
				jim->die();
				delete *it;
				break;
			}
		}
	}
	qDebug("GW: Scene items now= %i - View items now= %i ", scene()->items().size(), items().size() );
}



/**
	Called from MainWindow when erasing edges using vertex numbers
*/
void GraphicsWidget::eraseEdge(int sourceNode, int targetNode){
	qDebug("GW: Scene items= %i - View items : %i",scene()->items().size(), items().size());
	QList<QGraphicsItem *>  list=scene()->items();
	for (QList<QGraphicsItem *>::iterator it=list.begin(); it!= list.end() ; it++){
		if ( (*it)->type()==TypeEdge ) {
			Edge *edge=(Edge*) (*it);
			if ( edge->sourceNodeNumber()==sourceNode && edge->targetNodeNumber()==targetNode ) {
				removeItem(edge);
				break;
			}
		}
	}
	qDebug("GW: Scene items now= %i - View items now= %i ", scene()->items().size(), items().size() );
}








/** 
	Called from Node::die() to removeItem from nodeHash...
*/
void GraphicsWidget::removeItem( Node *node){
	vector<Node*>::iterator it;
	long int i=node->nodeNumber();
	foreach ( Node *candidate, nodeHash) {
		if ( candidate->nodeNumber() == i )
			nodeHash.remove( i );
	}
	node->deleteLater ();
	qDebug() << "GW items now:  " << items().size();
}




/** Called from Node::die() to remove Edge edge ... */
void GraphicsWidget::removeItem( Edge * edge){
	edge->remove();
	delete (edge);
}


void GraphicsWidget::removeItem( NodeLabel *nodeLabel){
	qDebug() << "GW items now:  " << items().size();
	delete (nodeLabel);
	qDebug() << "GW items now:  " << items().size();
}



void GraphicsWidget::removeItem( NodeNumber *nodeNumber){
	delete (nodeNumber);
}






/** 
	Accepts initial node color from MW.
	It is called from MW on startup and when user changes it.
*/
void GraphicsWidget::setInitNodeColor(QString color){
	qDebug("GW setting initNodeColor");
	m_nodeColor=color;
}



/** 
	Sets initial edge color.
	Called from MW on startup and when user changes it.
*/
void GraphicsWidget::setInitLinkColor(QString color){
	qDebug("GW setting initLinkColor");
	m_linkColor=color;
}



/** 
	Sets the color of an node.
	Called from MW when the user changes the color of a node (right-clicking).
*/
bool GraphicsWidget::setNodeColor(long int nodeNumber, QString color){
	QList<QGraphicsItem *> list=scene()->items();
	for (QList<QGraphicsItem *>::iterator it=list.begin(); it!= list.end() ; it++){
		if ( (*it)->type()==TypeNode) {
			Node *node=(Node*) (*it);
			if ( node->nodeNumber()==nodeNumber ) {
				node->setColor(color);
				return true;
			}
		}
	}
	return false;
}


/** 
	Makes node label appear inside node.
	Called from MW on user request.
*/
void   GraphicsWidget::setNumbersInsideNodes(bool numIn){
	qDebug("GW setting initNumberDistance");
	foreach ( Node *m_node, nodeHash) {
		m_node->setNumberInside(numIn);
		if (numIn)
			this->setInitNodeSize(m_nodeSize+2);
		else
			this->setInitNodeSize(m_nodeSize-2);

	}
}



/** 
	Changes/Sets the color of an edge.
	Called from MW when the user changes the color of an edge (right-clicking).
*/
bool GraphicsWidget::setEdgeColor(int source, int target, QString color){
	QList<QGraphicsItem *> list=scene()->items();
	for (QList<QGraphicsItem *>::iterator it=list.begin(); it!= list.end() ; it++){
		if ( (*it)->type()==TypeEdge) {
			Edge *edge=(Edge*) (*it);
			if ( edge->sourceNodeNumber()==source && edge->targetNodeNumber()==target ) {
				edge->setColor(color);
				return true;
			}
		}
	}
	return false;
}




/** 
	Changes/Sets the weight of an edge.
	Called from MW when the user changes the weight of an edge (right-clicking).
*/
bool GraphicsWidget::setEdgeWeight(int source, int target, float weight){
	QList<QGraphicsItem *> list=scene()->items();
	for (QList<QGraphicsItem *>::iterator it=list.begin(); it!= list.end() ; it++){
		if ( (*it)->type()==TypeEdge) {
			Edge *edge=(Edge*) (*it);
			if ( edge->sourceNodeNumber()==source && edge->targetNodeNumber()==target ) {
				edge->setWeight(weight);
				edge->update();
				return true;
			}
		}
	}
	return false;
}



/** 
	Sets initial node size from MW.
	It is Called from MW on startup and when user changes it.
*/
void GraphicsWidget::setInitNodeSize(int size){
	qDebug("GW setting initNodeSize");
	m_nodeSize=size;
}




/** 
	Sets initial number distance from node 
	Called from MW on startup and when user changes it.
*/
void GraphicsWidget::setInitNumberDistance(int numberDistance){
	qDebug("GW setting initNumberDistance");
	m_numberDistance=numberDistance;
}


/** 
	Passes initial label distance from node 
	It is called from MW on startup and when user changes it.
*/
void GraphicsWidget::setInitLabelDistance(int labelDistance){
	qDebug("GW setting initLabelDistance");
	m_labelDistance=labelDistance;
}






/**
*	Changes the visibility of an GraphicsView edge (number, label, edge, etc)
*/
void GraphicsWidget::setEdgeVisibility(int source, int target, bool visible){
	QString edgeName = QString::number( source ) + QString(">")+ QString::number( target );
	
	if (visible) {
		if  ( edgesMap.contains (edgeName) ) {
			qDebug()<<"GW: setEdgeVisibility(). Making edge between "<< source  << " and "<< target 
				<< " VISIBLE.";
			edgesMap [edgeName] -> show();			
		}
		else {
			
		}
	}
	else {
		if  ( edgesMap.contains (edgeName) ) {
			qDebug()<<"GW: setEdgeVisibility(). Making edge between "<< source  << " and "<< target 
				<< " NOT VISIBLE.";
			edgesMap [edgeName] -> hide();
		}
	}
}



/**
*	Changes the visibility of a  Node
*/
void GraphicsWidget::setNodeVisibility(long int number, bool visible){
    qDebug() << "GW: setNodeVisibility() for "<< number;
    QList<QGraphicsItem *> list = scene()->items();
    for (QList<QGraphicsItem *>::iterator item=list.begin();item!=list.end(); item++) {
	if ( (*item)->type() == TypeNode){
	    if ( Node *node = qgraphicsitem_cast<Node *>(*item) )
		if ( node->nodeNumber() == number ) {
		    if (visible){
			qDebug() << "GW: setNodeVisibility(): Node numbered " << number << " found! Will be visible now...";
			(*item)->show();
		    }
		    else {
			qDebug() << "GW: setNodeVisibility(): Node numbered " << number << " found! Invisible now...";
			(*item)->hide();
		    }
		}
	    }
    }

//	qDebug() << "GW: setNodeVisibility() for "<< number;
//	foreach ( Node *candidate, nodeHash) {
//		if (candidate->nodeNumber() == number ) {
//		    if (visible) {
//			qDebug() << "GW: setNodeVisibility(): Node numbered " << number << " found! Will be visible now...";
//			candidate->show();
//			candidate->update();
//		    }
//		    else{
//			qDebug() << "GW: setNodeVisibility(): Node numbered " << number << " found! Invisible now...";
//			candidate->hide();
//		    }
//
//			break;
//		}
//
//	}
}




/*
 * Used by findNode. 
 * Returns, if found, the node with label or number 'text'
 */
Node* GraphicsWidget::hasNode( QString text ){
	vector<Node*>::iterator it;
	bool ok = false;
	foreach ( Node *candidate, nodeHash) {
		if ( 	candidate->nodeNumber()==text.toInt(&ok, 10)  ||
			candidate->labelText().contains (text, Qt::CaseInsensitive)
			) {
			qDebug() << "GW: hasNode(): Node " << text << " found!";
			markedNodeExists=true;
			return candidate;
			break;
		}

	}
	return markedNode;	//dummy return. We check markedNodeExists flag first...
}



/**
	 Marks (by double-sizing and highlighting) or unmarks a node, given its number or label.
	 Called by MW:slotFindNode()
*/
bool GraphicsWidget::setMarkedNode(QString nodeText){
	qDebug ("GW: setMarkedNode()");
	if (markedNodeExists) {
		markedNode->setSelected(false);		//unselect it, so that it restores its color
		markedNode->setSize(originalNodeSize);	//restore its size
		markedNodeExists=false;
		return true;
	}
	
	markedNode = hasNode (nodeText);
	if (!markedNodeExists)
		return false;
	
	markedNode->setSelected(true);		//select it, so that it changes color
	originalNodeSize=markedNode->size(); // save its original size
	markedNode->setSize(2*originalNodeSize-1);	//now, make it larger 
	return true;	
}




/**
*	Changes the visibility of all items of certain type (i.e. number, label, edge, etc)
*/
void GraphicsWidget::setAllItemsVisibility(int type, bool visible){
	QList<QGraphicsItem *> list = scene()->items();
	for (QList<QGraphicsItem *>::iterator item=list.begin();item!=list.end(); item++) {
		if ( (*item)->type() == type){
			if (visible)	(*item)->show();
			else	(*item)->hide();
		}
	}
}



void GraphicsWidget::addBackgrCircle( int x0, int y0, int radius){
	BackgrCircle *circ=new BackgrCircle (this, x0, y0, radius);
	circ->show();

}


void GraphicsWidget::addBackgrHLine( int y0){
	BackgrCircle *circ=new BackgrCircle (this, y0, 	this->width());
	circ->show();
}


/**
*	Removes all items of certain type (i.e. number, label, edge, etc)
*/
void GraphicsWidget::removeAllItems(int type){
	qDebug()<< "GW: removeAllItems";
	QList<QGraphicsItem *> list = scene()->items();
	for (QList<QGraphicsItem *>::iterator item=list.begin();item!=list.end(); item++) {
		if ( (*item)->type() == type){
			BackgrCircle *bgcircle = qgraphicsitem_cast<BackgrCircle *>  (*item);
			qDebug()<< "GW: removeAllItems - located element";
			bgcircle->die();
			bgcircle->deleteLater ();
			delete *item;
		}
	}
}



void GraphicsWidget::clearBackgrCircles(){
	qDebug()<< "GW: clearBackgrCircles";
	this->removeAllItems(TypeBackgrCircle);
}





/** 	
	Starts a new node when the user double-clicks somewhere
	Emits userDoubleClicked to MW slot addNodeWithMouse() which
		- displays node info on MW status bar and 
		- calls Graph::createVertex(), which in turn calls this->drawNode()...
		Yes, we make a full circle! :) 
*/
void GraphicsWidget::mouseDoubleClickEvent ( QMouseEvent * e ) {

	if ( QGraphicsItem *item= itemAt(e->pos() ) ) {
		if (Node *node = qgraphicsitem_cast<Node *>(item)) {
			Q_UNUSED(node);
			qDebug() << "GW: mouseDoubleClickEvent() double click on a node detected!"
					<< " Cant create new one!";
			return;
		}
	}

	QPointF p = mapToScene(e->pos());
	qDebug()<< " GW: mouseDoubleClickEvent():double click detected on empty space. "
		  << " Emitting signal to MW to create a new vertex in graph. e->pos() "
		  << e->pos().x() << ", "<< e->pos().y() << ", "<< p.x() << ", " <<p.y();
	emit userDoubleClicked(-1, p);
	qDebug("GW: mouseDoubleClickEvent(): Scene and GW items now: %i and %i. Emitting Changed() signal... ", scene()->items().size(), items().size());
}



void GraphicsWidget::mousePressEvent( QMouseEvent * e ) {
	QPointF p = mapToScene(e->pos());
	if ( QGraphicsItem *item= itemAt(e->pos() ) ) {
		if (Node *node = qgraphicsitem_cast<Node *>(item)) {
			Q_UNUSED(node);
			qDebug() << "GW: mousePressEvent() single click detected on a node at pos  "
					<< e->pos().x() << ","<< e->pos().y() << " or "<<  p.x() << ","<<p.y();
			QGraphicsView::mousePressEvent(e);
			return;
		}
		if (Edge *edge= qgraphicsitem_cast<Edge *>(item)) {
			Q_UNUSED(edge);
			qDebug() << "GW: mousePressEvent() single click detected on an edge at pos  "
					<< e->pos().x() << ","<< e->pos().y() << " or "<<  p.x() << ","<<p.y();

			QGraphicsView::mousePressEvent(e);
			return;
		}
	}
	else{
		qDebug() << "GW: mousePressEvent(). No item here. "
				<< "Starting a new selection rectangle or a new node";
		this->scene()->clearSelection();
		QGraphicsView::mousePressEvent(e);
	} 

}




//void GraphicsWidget::mouseReleaseEvent( QMouseEvent * e ) {
	//QPointF p = mapToScene(e->pos());
	//qDebug() << "GW: mouseReleaseEvent() detected at " 
		//<< e->pos().x() << ","<< e->pos().y() << " or "<<  p.x() << ","<<p.y();
	//if ( QGraphicsItem *item= itemAt(e->pos() ) ) {
		//if (Node *node = qgraphicsitem_cast<Node *>(item)) {
			//Q_UNUSED(node);
			//QGraphicsView::mouseReleaseEvent(e);
			//return;
		//}
	//}
	//else{
		//qDebug() << "GW: mouseReleaseEvent(). No item here. Maybe end a selection rectangle? " 
				//<< "I will save this pos";
		//endPoint =  
	//} 

//}


/** 	
	On the event of a right-click on a node, the node calls this function
	to emit a signal to MW to open a context menu at the mouse position.
	Node is already passed with selectedNode(Node *) signal
	The position of the menu is determined by QMouse:pos()...
*/
void GraphicsWidget::openNodeContextMenu(){
	qDebug("GW: emitting openNodeMenu()");
	emit openNodeMenu();
}


/** 	
	On the event of a right-click on an edge, the edge calls this function
	to emit a signal to MW to open a context menu at the mouse position.
	Edge is already passed with selectedNode(Node *) signal
	The position of the menu is determined by QMouse:pos()...
*/
void GraphicsWidget::openEdgeContextMenu(){
	qDebug("GW: emitting openEdgeMenu()");
	emit openEdgeMenu();
}












/** 
	Calls the scaleView() when the user spins the mouse wheel
	It passes delta as new m_scale
*/
void GraphicsWidget::wheelEvent(QWheelEvent *e) {
	qDebug("GW: Mouse wheel event");
	qDebug() << "GW: delta = " << e->delta();
  	float m_scale = e->delta() / qreal(600);
	qDebug("GW: m_scale = %f", m_scale);	
	if ( m_scale > 0)
		zoomIn();
	else  if ( m_scale < 0) 
		zoomOut();
	else m_scale=1;
}


/** 
	Called from MW magnifier widgets
*/
void GraphicsWidget::zoomOut (){
	if (zoomIndex > 0) {
		zoomIndex--;
		changeZoom(zoomIndex);
	}
	qDebug("GW: ZoomOut() index %i", zoomIndex);
    	emit zoomChanged(zoomIndex);
}	


/** 
	Called from MW magnifier widgets
*/
void GraphicsWidget::zoomIn(){
	qDebug("GW: ZoomIn()");
	if (zoomIndex < 6) {
		zoomIndex++;
		changeZoom(zoomIndex);
	}
	qDebug("GW: ZoomIn() index %i", zoomIndex);
    	emit zoomChanged(zoomIndex);
}


/**
      Initiated from MW zoomCombo widget to zoom in or out.
*/
void GraphicsWidget::changeZoom(int value) {
	double scaleFactor = 0.25;
	scaleFactor *= (value + 1);
	m_currentScaleFactor = scaleFactor;
	QMatrix oldMatrix = matrix();
	resetMatrix();
//	translate(oldMatrix.dx(), oldMatrix.dy());
	this->scale(scaleFactor, scaleFactor);
	rotate(m_currentRotationAngle);
}



void GraphicsWidget::rot(int angle){
	qDebug("rotating");
	m_currentRotationAngle = angle;
	resetMatrix();
	scale(m_currentScaleFactor, m_currentScaleFactor);
	rotate(angle);

}

/** Resizing the view causes a repositioning of the nodes maintaining the same pattern*/
void GraphicsWidget::resizeEvent( QResizeEvent *e ) {
	Q_UNUSED(e);
// 	qDebug ("GraphicsWidget: resizeEvent");
// 	int w=e->size().width();
// 	int h=e->size().height();
// 	int w0=e->oldSize().width();
// 	int h0=e->oldSize().height();
// 	qreal fX=  (double)(w)/(double)(w0);
// 	qreal fY= (double)(h)/(double)(h0);
// 	foreach (QGraphicsItem *item, scene()->items()) {
// 		qDebug ("item will move by %f, %f", fX, fY);
// 		if (Node *node = qgraphicsitem_cast<Node *>(item) ) {
// 			qDebug("Node original position %f, %f", item->x(), item->y());
// 			qDebug("Node will move to %f, %f",item->x()*fX, item->y()*fY);
// 			node->setPos(mapToScene(item->x()*fX, item->y()*fY));
// 		}	
// 		else 	item->setPos(mapToScene(item->x()*fX, item->y()*fY));
// 	}
// 	emit windowResized(w, h);

}



/** 
	Destructor.
*/
GraphicsWidget::~GraphicsWidget(){
}