summaryrefslogtreecommitdiffstats
path: root/src/nodegroup.cpp
blob: 3b23e232ee0c34f66cf3f0458ae29d4ead7549ac (plain)
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
/***************************************************************************
 *   Copyright (C) 2004-2005 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "icndocument.h"
#include "connector.h"
#include "conrouter.h"
#include "node.h"
#include "nodegroup.h"

#include <kdebug.h>
#include <assert.h>
#include <cmath>

NodeGroup::NodeGroup( ICNDocument *icnDocument, const char *name )
	: TQObject( icnDocument, name )
{
	p_icnDocument = icnDocument;
	b_visible = true;
}


NodeGroup::~NodeGroup()
{
	clearConList();
	
	m_extNodeList.remove( (Node*)0l );
	const NodeList::iterator xnEnd = m_extNodeList.end();
	for ( NodeList::iterator it = m_extNodeList.begin(); it != xnEnd; ++it )
		(*it)->setNodeGroup(0l);
	m_extNodeList.clear();
	
	m_nodeList.remove( (Node*)0l );
	const NodeList::iterator nEnd = m_nodeList.end();
	for ( NodeList::iterator it = m_nodeList.begin(); it != nEnd; ++it )
		(*it)->setNodeGroup(0l);
	m_nodeList.clear();
}


void NodeGroup::setVisible( bool visible )
{
	if ( b_visible == visible )
		return;
	
	b_visible = visible;
	
	m_nodeList.remove( (Node*)0l );
	const NodeList::iterator nEnd = m_nodeList.end();
	for ( NodeList::iterator it = m_nodeList.begin(); it != nEnd; ++it )
		(*it)->setVisible(visible);
}


void NodeGroup::addNode( Node *node, bool checkSurrouding )
{
	if ( !node || node->isChildNode() || m_nodeList.contains(node) ) {
		return;
	}
	
	m_nodeList.append(node);
	node->setNodeGroup(this);
	node->setVisible(b_visible);
	
	if (checkSurrouding)
	{
		ConnectorList con = node->inputConnectorList();
		ConnectorList::iterator end = con.end();
		for ( ConnectorList::iterator it = con.begin(); it != end; ++it )
		{
			if (*it) {
				addNode( (*it)->startNode(), true );
			}
		}
		
		con = node->outputConnectorList();
		end = con.end();
		for ( ConnectorList::iterator it = con.begin(); it != end; ++it )
		{
			if (*it) {
				addNode( (*it)->endNode(), true );
			}
		}
	}
}


void NodeGroup::translate( int dx, int dy )
{
	if ( (dx == 0) && (dy == 0) )
		return;
	
	m_conList.remove((Connector*)0l);
	m_nodeList.remove((Node*)0l);
	
	const ConnectorList::iterator conEnd = m_conList.end();
	for ( ConnectorList::iterator it = m_conList.begin(); it != conEnd; ++it )
	{
		(*it)->updateConnectorPoints(false);
		(*it)->translateRoute( dx, dy );
	}
	
	const NodeList::iterator nodeEnd = m_nodeList.end();
	for ( NodeList::iterator it = m_nodeList.begin(); it != nodeEnd; ++it )
		(*it)->moveBy( dx, dy );
}


void NodeGroup::updateRoutes()
{
	resetRoutedMap();
	
	// Basic algorithm used here: starting with the external nodes, find the
	// pair with the shortest distance between them. Route the connectors
	// between the two nodes appropriately. Remove that pair of nodes from the
	// list, and add the nodes along the connector route (which have been spaced
	// equally along the route). Repeat until all the nodes are connected.
	
	
	const ConnectorList::iterator conEnd = m_conList.end();
	for ( ConnectorList::iterator it = m_conList.begin(); it != conEnd; ++it )
	{
		if (*it) 
			(*it)->updateConnectorPoints(false);
	}
	
	Node *n1, *n2;
	NodeList currentList = m_extNodeList;
	while ( !currentList.isEmpty() )
	{
		findBestPair( &currentList, &n1, &n2 );
		if ( n1 == 0l || n2 == 0l ) {
			return;
		}
		NodeList route = findRoute( n1, n2 );
		currentList += route;
		
		ConRouter cr(p_icnDocument);
		cr.mapRoute( (int)n1->x(), (int)n1->y(), (int)n2->x(), (int)n2->y() );
		TQPointListList pl = cr.dividePoints( route.size()+1 );
		
		const NodeList::iterator routeEnd = route.end();
		const TQPointListList::iterator plEnd = pl.end();
		Node *prev = n1;
		NodeList::iterator routeIt = route.begin();
		for ( TQPointListList::iterator it = pl.begin(); it != plEnd; ++it )
		{
			Node *next = (routeIt == routeEnd) ? n2 : (Node*)*(routeIt++);
			removeRoutedNodes( &currentList, prev, next );
			TQPointList pointList = *it;
			if ( prev != n1 )
			{
				TQPoint first = pointList.first();
				prev->moveBy( first.x() - prev->x(), first.y() - prev->y() );
			}
			Connector *con = findCommonConnector( prev, next );
			if (con)
			{
				con->updateConnectorPoints(false);
				con->setRoutePoints( pointList, false, false );
				con->updateConnectorPoints(true);
// 				con->conRouter()->setPoints( &pointList, con->startNode() != prev );
// 				con->conRouter()->setPoints( &pointList, con->pointsAreReverse( &pointList ) );
// 				con->calcBoundingPoints();
			}
			prev = next;
		}
	}
}


NodeList NodeGroup::findRoute( Node *startNode, Node *endNode )
{
	NodeList nl;
	if ( !startNode || !endNode || startNode == endNode ) {
		return nl;
	}

	IntList temp;	
	IntList il = findRoute( temp, getNodePos(startNode), getNodePos(endNode) );
	
	const IntList::iterator end = il.end();
	for ( IntList::iterator it = il.begin(); it != end; ++it )
	{
		Node *node = getNodePtr(*it);
		if (node) {
			nl += node;
		}
	}
	
	nl.remove(startNode);
	nl.remove(endNode);
	
	return nl;
}


IntList NodeGroup::findRoute( IntList used, int currentNode, int endNode, bool *success )
{
	bool temp;
	if (!success) {
		success = &temp;
	}
	*success = false;
	
	if ( !used.contains(currentNode) ) {
		used.append(currentNode);
	}
	
	if ( currentNode == endNode ) {
		*success = true;
		return used;
	}
	
	const uint n = m_nodeList.size()+m_extNodeList.size();
	for ( uint i=0; i<n; ++i )
	{
		if ( b_routedMap[i*n+currentNode] && !used.contains(i) )
		{
			IntList il = findRoute( used, i, endNode, success );
			if (*success) {
				return il;
			}
		}
	}
	
	IntList il;
	return il;
}


Connector* NodeGroup::findCommonConnector( Node *n1, Node *n2 )
{
	if ( !n1 || !n2 || n1==n2 ) {
		return 0l;
	}
	
	ConnectorList n1Con = n1->inputConnectorList() + n1->outputConnectorList();
	ConnectorList n2Con = n2->inputConnectorList() + n2->outputConnectorList();
	
	const ConnectorList::iterator end = n1Con.end();
	for ( ConnectorList::iterator it = n1Con.begin(); it != end; ++it )
	{
		if ( n2Con.contains(*it) ) {
			return *it;
		}
	}
	return 0l;
}


void NodeGroup::findBestPair( NodeList *list, Node **n1, Node **n2 )
{
	*n1 = 0l;
	*n2 = 0l;
	
	if ( list->size() < 2 ) {
		return;
	}
	
	const NodeList::iterator end = list->end();
	int shortest = 1<<30;
	
	// Try and find any that are aligned horizontally
	for ( NodeList::iterator it1 = list->begin(); it1 != end; ++it1 )
	{
		NodeList::iterator it2 = it1;
		for ( ++it2; it2 != end; ++it2 )
		{
			if ( *it1 != *it2 && (*it1)->y() == (*it2)->y() && canRoute( *it1, *it2 ) )
			{
				const int distance = std::abs((double)( (*it1)->x()-(*it2)->x() ));
				if ( distance < shortest )
				{
					shortest = distance;
					*n1 = *it1;
					*n2 = *it2;	
				}
			}
		}
	}
	if (*n1) {
		return;
	}
	
	// Try and find any that are aligned vertically
	for ( NodeList::iterator it1 = list->begin(); it1 != end; ++it1 )
	{
		NodeList::iterator it2 = it1;
		for ( ++it2; it2 != end; ++it2 )
		{
			if ( *it1 != *it2 && (*it1)->x() == (*it2)->x() && canRoute( *it1, *it2 ) )
			{
				const int distance = std::abs((double)( (*it1)->y()-(*it2)->y() ));
				if ( distance < shortest )
				{
					shortest = distance;
					*n1 = *it1;
					*n2 = *it2;	
				}
			}
		}
	}
	if (*n1) {
		return;
	}
	
	// Now, lets just find the two closest nodes
	for ( NodeList::iterator it1 = list->begin(); it1 != end; ++it1 )
	{
		NodeList::iterator it2 = it1;
		for ( ++it2; it2 != end; ++it2 )
		{
			if ( *it1 != *it2 && canRoute( *it1, *it2 ) )
			{
				const int dx = (int)((*it1)->x()-(*it2)->x());
				const int dy = (int)((*it1)->y()-(*it2)->y());
				const int distance = std::abs((double)dx) + std::abs((double)dy);
				if ( distance < shortest )
				{
					shortest = distance;
					*n1 = *it1;
					*n2 = *it2;
				}
			}
		}
	}
	
	if (!*n1) {
		kdError() << "NodeGroup::findBestPair: Could not find a routable pair of nodes!"<<endl;
	}
}


bool NodeGroup::canRoute( Node *n1, Node *n2 )
{
	if ( !n1 || !n2 ) {
		return false;
	}
	
	IntList reachable;
	getReachable( &reachable, getNodePos(n1) );
	return reachable.contains(getNodePos(n2));
}


void NodeGroup::getReachable( IntList *reachable, int node )
{
	if ( !reachable || reachable->contains(node) ) {
		return;
	}
	reachable->append(node);
	
	const uint n = m_nodeList.size() + m_extNodeList.size();
	assert( node < int(n) );
	for ( uint i=0; i<n; ++i )
	{
		if ( b_routedMap[i*n + node] ) {
			getReachable(reachable,i);
		}
	}
}


void NodeGroup::resetRoutedMap()
{
	const uint n = m_nodeList.size() + m_extNodeList.size();
	
	b_routedMap.resize(0);
	b_routedMap.resize( n*n, false );
	
	const ConnectorList::iterator end = m_conList.end();
	for ( ConnectorList::iterator it = m_conList.begin(); it != end; ++it )
	{
		Connector *con = *it;
		if (con)
		{
			int n1 = getNodePos(con->startNode());
			int n2 = getNodePos(con->endNode());
			if ( n1 != -1 && n2 != -1 )
			{
				b_routedMap[n1*n + n2] = b_routedMap[n2*n + n1] = true;
			}
		}
	}
}


void NodeGroup::removeRoutedNodes( NodeList *nodes, Node *n1, Node *n2 )
{
	if (!nodes) {
		return;
	}
	
	// Lets get rid of any duplicate nodes in nodes (as a general cleaning operation)
	const NodeList::iterator end = nodes->end();
	for ( NodeList::iterator it = nodes->begin(); it != end; ++it )
	{
		if ( nodes->contains(*it) > 1 ) {
			*it = 0l;
		}
	}
	nodes->remove((Node*)0l);
	
	const int n1pos = getNodePos(n1);
	const int n2pos = getNodePos(n2);
	
	if ( n1pos == -1 || n2pos == -1 ) {
		return;
	}
	
	const uint n = m_nodeList.size() + m_extNodeList.size();
	
	b_routedMap[n1pos*n + n2pos] = b_routedMap[n2pos*n + n1pos] = false;
	
	bool n1HasCon = false;
	bool n2HasCon = false;
	
	for ( uint i=0; i<n; ++i )
	{
		n1HasCon |= b_routedMap[n1pos*n + i];
		n2HasCon |= b_routedMap[n2pos*n + i];
	}
	
	if (!n1HasCon) {
		nodes->remove(n1);
	}
	if (!n2HasCon) {
		nodes->remove(n2);
	}
}


int NodeGroup::getNodePos( Node *n )
{
	if (!n) {
		return -1;
	}
	int pos = m_nodeList.findIndex(n);
	if ( pos != -1 ) {
		return pos;
	}
	pos = m_extNodeList.findIndex(n);
	if ( pos != -1 ) {
		return pos+m_nodeList.size();
	}
	return -1;
}


Node* NodeGroup::getNodePtr( int n )
{
	if ( n<0 ) {
		return 0l;
	}
	const int a = m_nodeList.size();
	if (n<a) {
		return m_nodeList[n];
	}
	const int b = m_extNodeList.size();
	if (n<a+b) {
		return m_extNodeList[n-a];
	}
	return 0l;
}


void NodeGroup::clearConList()
{
	const ConnectorList::iterator end = m_conList.end();
	for ( ConnectorList::iterator it = m_conList.begin(); it != end; ++it )
	{
		Connector *con = *it;
		if (con)
		{
			con->setNodeGroup(0l);
			disconnect( con, TQ_SIGNAL(removed(Connector*)), this, TQ_SLOT(connectorRemoved(Connector*)) );
		}
	}
	m_conList.clear();
}


void NodeGroup::init()
{
	NodeList::iterator xnEnd = m_extNodeList.end();
	for ( NodeList::iterator it = m_extNodeList.begin(); it != xnEnd; ++it )
	{
		(*it)->setNodeGroup(0l);
	}
	m_extNodeList.clear();
	
	clearConList();
	
	// First, lets get all of our external nodes and internal connectors
	const NodeList::iterator nlEnd = m_nodeList.end();
	for ( NodeList::iterator nodeIt = m_nodeList.begin(); nodeIt != nlEnd; ++nodeIt )
	{
		ConnectorList inCon = (*nodeIt)->inputConnectorList();
		ConnectorList outCon = (*nodeIt)->outputConnectorList();
		ConnectorList::iterator conEnd;
		
		conEnd = inCon.end();
		for ( ConnectorList::iterator conIt = inCon.begin(); conIt != conEnd; ++conIt )
		{
			Connector *con = *conIt;
			addExtNode(con->startNode());
			if ( !m_conList.contains(con) ) {
				m_conList += con;
				con->setNodeGroup(this);
			}
			connect( con, TQ_SIGNAL(removed(Connector*)), this, TQ_SLOT(connectorRemoved(Connector*)) );
		}
		
		conEnd = outCon.end();
		for ( ConnectorList::iterator conIt = outCon.begin(); conIt != conEnd; ++conIt )
		{
			Connector *con = *conIt;
			addExtNode(con->endNode());
			if ( !m_conList.contains(con) ) {
				m_conList += con;
				con->setNodeGroup(this);
			}
			connect( con, TQ_SIGNAL(removed(Connector*)), this, TQ_SLOT(connectorRemoved(Connector*)) );
		}
		
		// Connect the node up to us
		connect( *nodeIt, TQ_SIGNAL(removed(Node*)), this, TQ_SLOT(nodeRemoved(Node*)) );
	}
	
	// And connect up our external nodes
	xnEnd = m_extNodeList.end();
	for ( NodeList::iterator it = m_extNodeList.begin(); it != xnEnd; ++it )
	{
// 		connect( *it, TQ_SIGNAL(moved(Node*)), this, TQ_SLOT(extNodeMoved()) );
		connect( *it, TQ_SIGNAL(removed(Node*)), this, TQ_SLOT(nodeRemoved(Node*)) );
	}
}


void NodeGroup::nodeRemoved( Node *node )
{
	// We are probably about to get deleted by ICNDocument anyway...so no point in doing anything
	m_nodeList.remove(node);
	node->setNodeGroup(0l);
	node->setVisible(true);
	m_extNodeList.remove(node);
}


void NodeGroup::connectorRemoved( Connector *connector )
{
	m_conList.remove(connector);
}


void NodeGroup::addExtNode( Node *node )
{
	if ( !m_extNodeList.contains(node) && !m_nodeList.contains(node) )
	{
		m_extNodeList.append(node);
		node->setNodeGroup(this);
	}
}

#include "nodegroup.moc"