File: edge.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 (387 lines) | stat: -rwxr-xr-x 11,849 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
/***************************************************************************
 SocNetV: Social Networks Visualizer 
 version: 0.90
 Written in Qt 4.4

                        edge.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 <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <QtDebug>		//used for qDebug messages
#include <cmath>

#include "graphicswidget.h"
#include "edge.h"
#include "node.h"
#include "edgeweight.h"


static const double Pi = 3.14159265;
static double TwoPi = 2.0 * Pi;


Edge::Edge(  GraphicsWidget *gw, Node *from, Node *to, float weight, int nodeSize, QString color, bool reciprocal, bool drawArrows, bool bez): graphicsWidget(gw) {

	qDebug("Edge: Edge()");
	Q_UNUSED(nodeSize);
	graphicsWidget->scene()->addItem(this);  //Without this, edges don not appear on the screen...

 	from->addOutLink( this );	//adds this Edge to sourceNode
	to->addInLink( this );		//adds this Edge to targetNode

	source=from;			//saves the sourceNode
	target=to;			//Saves the targetNode
	m_color=color;
	m_drawArrows=drawArrows;
	m_reciprocal=reciprocal;
	m_startOffset=source->size();  //used to offset edge from the centre of node
	m_endOffset=target->size();  //used to offset edge from the centre of node	
	qDebug("Edge() m_startOffset %i",(int) m_startOffset);
	qDebug("Edge() m_endOffset %i",(int) m_endOffset);

	m_arrowSize=5;		//controls the width of the edge arrow

	eFrom = source->nodeNumber() ;
	eTo = target->nodeNumber() ;
	m_weight = weight ;
	m_Bezier = bez; 
	adjust();
}



void Edge::showArrows(bool drawArrows){
	prepareGeometryChange();
	m_drawArrows=drawArrows;
}



void Edge::removeRefs(){
	//FIXME Need to disconnect signals from node to this "erased" edge
	qDebug("Edge: removeRefs()");
	source->deleteOutLink(this);
	target->deleteInLink(this);
}

void Edge::remove(){
	qDebug("Edge: remove(), calling removeRefs()");
	removeRefs();
	this->hide();
	
}

void Edge::setColor( QString str) {
	m_color=str;
 	prepareGeometryChange();
}


QString Edge::color() { 
	return m_color; 
}

/*
	Called from MW when user wants to change an edge's weight.
	Updates both the width and the EdgeWeight
*/
void Edge::setWeight( float w) {
	m_weight = w;
	foreach (EdgeWeight *wgt, weightList) 		///update the edgeWeight of this edge as well.
		wgt->setPlainText (QString::number(w));

}

float Edge::weight() { 
	return m_weight; 
}


void Edge::setStartOffset(int offset){
	m_startOffset=offset;
}

void Edge::setEndOffset(int offset){
	m_endOffset=offset;
}


Node *Edge::sourceNode() const {
    return source;
}

void Edge::setSourceNode(Node *node) {
	source = node;
	adjust();
}


Node *Edge::targetNode() const {
	return target;
}


void Edge::setTargetNode(Node *node){
	target = node;
	adjust();
}


int Edge::sourceNodeNumber () { 
	return eFrom;
}

int Edge::targetNodeNumber() { 
	return eTo;
}


//Called from EdgeWeight objects to 'connect' them to this edge. 
void Edge::addWeight (EdgeWeight* canvasWeight  )  {
	weightList.push_back( canvasWeight) ; 
}


void Edge::clearWeightList(){
	weightList.clear();
}


/*
	-leaves some empty space (offset) from node
	-make the edge weight appear on the centre of the edge
*/
void Edge::adjust(){
	qDebug("Edge: adjust()");
	if (!source || !target)
		return;
	QLineF line(mapFromItem(source, 0, 0), mapFromItem(target, 0, 0));
	QPointF edgeOffset;
	if (source!=target) {
	 	qreal length = line.length();
		edgeOffset = QPointF((line.dx() * m_endOffset) / length, (line.dy() *m_endOffset) / length);		
	}
	else edgeOffset = QPointF(0, 0);  
	
 	prepareGeometryChange();
 	
	sourcePoint = line.p1() + edgeOffset;
	targetPoint = line.p2() - edgeOffset;
	qDebug()<<"----Edge: adjust() "<< sourcePoint.x()<< " "<<sourcePoint.y();
	foreach (EdgeWeight *wgt, weightList) 		//Move the weight of this edge
		wgt->setPos( (source->x()+target->x())/2.0, (source->y()+target->y())/2.0 );
}


/*
	Returns the shape of this edge as a QPainterPath in local coordinates. 
	The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.
	The default implementation calls boundingRect() to return a simple rectangular shape,
	 but subclasses can reimplement this function to return a more accurate shape for non-rectangular items.
	For example, a round item may choose to return an elliptic shape for better collision detection
*/
QPainterPath Edge::shape () const {
	//qDebug()<<"Edge::shape()";		//too many debug messages...
	QPainterPath path;
	//path.addRegion(boundingRegion(QTransform()));
	path.addRect(boundingRect());
	return path;
} 


/*
Defines the outer bounds of the edge as a rectangle; 
All painting will be restricted to inside the edge's bounding rect. 
Qt uses this bounding rect to determine whether the edge requires redrawing.
*/
QRectF Edge::boundingRect() const {

	if (!source || !target)
		return QRectF();
	
	qreal penWidth = 1;
	qreal extra = ( penWidth + m_arrowSize) / 2.0;
	QRectF a = QRectF (
			sourcePoint, 
			QSizeF(
			targetPoint.x() - sourcePoint.x(), targetPoint.y() - sourcePoint.y())
			).normalized().adjusted(-extra, -extra, extra, extra);
	//qDebug()<<"Edge::boundingRect() extra = " << extra << "QSizeF width "<< a.width() << " QSizeF height "<< a.height();
	if (source==target) {		//self-edge has different bounding rect.
			return QRectF (
			sourcePoint-QPointF(30,30), 
			QSizeF(60,30)).normalized().adjusted(-extra, -extra, extra, extra);
	}
	return a;
}



void Edge::makeReciprocal(){
	qDebug("Edge::makeReciprocal()");
 	prepareGeometryChange();
	m_reciprocal= true;
}



void Edge::unmakeReciprocal(){
	qDebug("Edge::unmakeReciprocal()");
 	prepareGeometryChange();
	m_reciprocal= false;
}


void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *){
	if (!source || !target)
		return;
	Q_UNUSED(option); //	painter->setClipRect( option->exposedRect );
	
	qDebug()<<endl <<"@@@ Edge::paint() edge from "<< sourceNodeNumber() << " at (" <<(sourceNode())->x() <<","<< (sourceNode())->y() << ") to node "<<targetNodeNumber() << " at ("<<(targetNode())->x() <<","<< (targetNode())->y() << ") of weight "<< m_weight;

	//Define the path upon which we' ll draw the line 
	QPainterPath line(sourcePoint);
	
	//Construct the path
	if (source!=target) {
		if ( !m_Bezier){
			qDebug("*** Edge::paint(). Constructing a line");
			line.lineTo(targetPoint);
		}
		else {
			qDebug("*** Edge::paint(). Constructing a bezier curve");
		}
	}
	else { //self-link
		QPointF c1 = QPointF( targetPoint.x() -30,  targetPoint.y() -30 );
		QPointF c2 = QPointF( targetPoint.x() +30,  targetPoint.y() -30 );
		qDebug()<<"*** Edge::paint(). Constructing a bezier self curve c1 "<<c1.x()<<","<<c1.y()
						<< " and c2 "<<c2.x()<<","<<c2.y();
		line.cubicTo( c1, c2, targetPoint);
	}

	//Prepare the pen
	qDebug()<<"*** Edge::paint(). Preparing the pen with width "<< width();
	if (m_weight > 0)
			painter->setPen(QPen(QColor(m_color), width(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
	else 
			painter->setPen(QPen(QColor(m_color), width(), Qt::DashLine, Qt::RoundCap, Qt::RoundJoin));
			

	//Draw the arrows only if we have different nodes.
	if (m_drawArrows && source!=target) {
		angle = 0;
		line_length = line.length();
		line_dx = targetPoint.x()-sourcePoint.x();
		line_dy = targetPoint.y()-sourcePoint.y();
		if ( line.length() >0 )
			angle = ::acos( line_dx / line_length );
		qDebug() << " acos() " << ::acos( line_dx  / line_length ) ;

		if ( line_dy  >= 0)
			angle = TwoPi - angle;

		qDebug() << "*** Edge::paint(). Constructing arrows. First Arrow at target node" 
					<< "target-source: " << line_dx
					<< " length: " << line_length
					<< " angle: "<< angle;

		QPointF destArrowP1 = targetPoint + QPointF(sin(angle - Pi / 3) * m_arrowSize,
								cos(angle - Pi / 3) * m_arrowSize);
		QPointF destArrowP2 = targetPoint + QPointF(sin(angle - Pi + Pi / 3) * m_arrowSize,
								cos(angle - Pi + Pi / 3) * m_arrowSize);
		qDebug() << "*** Edge::paint() destArrowP1 " <<  destArrowP1.x() << "," << destArrowP1.y() 
								<< "  destArrowP2 " <<  destArrowP2.x() << "," << destArrowP2.y();
		painter->setBrush(QColor(m_color));
		QPolygonF destP;
		destP << targetPoint << destArrowP1 << destArrowP2;
		line.addPolygon ( destP);
		//painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
		if (m_reciprocal) { 
			qDebug("**** Edge::paint() This edge is SYMMETRIC! So, we need to create Arrow at src node as well");
			QPointF srcArrowP1 = sourcePoint + QPointF(sin(angle +Pi / 3) * m_arrowSize,
									cos(angle +Pi / 3) * m_arrowSize);
			QPointF srcArrowP2 = sourcePoint + QPointF(sin(angle +Pi - Pi  / 3) * m_arrowSize,
									cos(angle +Pi - Pi / 3) * m_arrowSize);
			qDebug() << "*** Edge::paint() srcArrowP1 " <<  srcArrowP1.x() << "," << srcArrowP1.y() 
									<< "  srcArrowP2 " <<  srcArrowP2.x() << "," << srcArrowP2.y();
			QPolygonF srcP;
			srcP << sourcePoint<< srcArrowP1<< srcArrowP2;
			line.addPolygon ( srcP);

//			painter->drawPolygon(QPolygonF() << line.p1() << srcArrowP1 << srcArrowP2);
		}
		else qDebug("*** Edge::paint() This edge is not symmetric. Therefore, I do not have anything else to do...");
	}
	else {
		qDebug()<< "*** Edge::paint(). This edge is self-link - CONTINUE!";
	}
	qDebug()<< "### Edge::paint(). DrawPath now....";
	painter->drawPath(line);
}


/** 
	Controls the width of the edge; is a function of edge weight
*/
float Edge::width(){
	if ( fabs(m_weight) > 0 && fabs(m_weight) <=5) {
		qDebug()<< "Edge::width() will return "<< fabs(m_weight);
		return  fabs(m_weight);
	}
	else if (fabs(m_weight)  > 5 && fabs(m_weight) <=10) {
		 return 6;
	}
	else if (fabs(m_weight) >10 && fabs(m_weight) <=20) {
		return 7;
	}
	else if (fabs(m_weight) >20 && fabs(m_weight)<=30) {
		return 8;
	}
	else if ( fabs(m_weight) > 30 ) return 9;
	
	return 1;	//	Default, if  m_weight in (-1, 1) space  
}




/** handles the events of a click on an edge*/
void Edge::mousePressEvent(QGraphicsSceneMouseEvent *event) {  
	qDebug("Edge: pressEvent() emitting edgeClicked");
	graphicsWidget->edgeClicked(this);
	if ( event->button()==Qt::LeftButton ) {
		qDebug("Edge: edge pressEvent() left click > ");
	//	graphicsWidget->startNodeMovement(0);
	}
	if ( event->button()==Qt::RightButton ) {
		qDebug("Edge: Right-click on an edge,  at %i, %i", event->screenPos().x(), event->screenPos().y()); 
		graphicsWidget->openEdgeContextMenu();
	}
}