summaryrefslogtreecommitdiffstats
path: root/src/node.cpp
blob: 9a3531d1f3f4d9136aa0a6d2c2f6cdcc83f6b898 (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
/***************************************************************************
 *   Copyright (C) 2003-2005 by David Saxton                               *
 *   [email protected]                                                    *
 *                                                                         *
 *   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 "cnitem.h"
#include "icndocument.h"
#include "connector.h"
#include "itemdocumentdata.h"
#include "node.h"

#include <kdebug.h>

#include <qpainter.h>

Node::Node( ICNDocument *icnDocument, Node::node_type type, node_dir dir, const QPoint &pos, QString *id )
	: QObject(), QCanvasPolygon( icnDocument->canvas() )
{
	p_nodeGroup = 0l;
	p_parentItem = 0L;
	b_deleted = false;
	m_dir = dir;
	m_type = type;
	p_icnDocument = icnDocument;
	m_level = 0;
	m_selectedColor = QColor( 101, 134, 192 );
	
	if (id)
	{
		m_id = *id;
		if ( !p_icnDocument->registerUID(*id) )
			kdError() << k_funcinfo << "Could not register id " << *id << endl;
	}
	else m_id = p_icnDocument->generateUID("node"+QString::number(type));
	
	initPoints();
	move( pos.x(), pos.y() );
	setBrush( Qt::black );
	setPen( Qt::black );
	show();
	
	emit (moved(this));
}


Node::~Node()
{
	p_icnDocument->unregisterUID( id() );
}


int Node::rtti() const
{
	return ICNDocument::RTTI::Node;
}


void Node::setLevel( const int level )
{
	m_level = level;
}


bool Node::acceptInput() const
{
	return type() != fp_out;
}


bool Node::acceptOutput() const
{
	return type() != fp_in;
}


void Node::setVisible( bool yes )
{
	if ( isVisible() == yes )
		return;
	
	QCanvasPolygon::setVisible(yes);
	
	const ConnectorList::iterator inputEnd = m_inputConnectorList.end();
	for ( ConnectorList::iterator it = m_inputConnectorList.begin(); it != inputEnd; ++it )
	{
		Connector *connector = *it;
		if (connector)
		{
			if ( isVisible() )
				connector->setVisible(true);
			
			else
			{
				Node *node = connector->startNode();
				connector->setVisible( node && node->isVisible() );
			}
		}
	}
	
	const ConnectorList::iterator outputEnd = m_outputConnectorList.end();
	for ( ConnectorList::iterator it = m_outputConnectorList.begin(); it != outputEnd; ++it )
	{
		Connector *connector = *it;
		if (connector)
		{
			if ( isVisible() )
				connector->setVisible(true);
			
			else
			{
				Node *node = connector->endNode();
				connector->setVisible( node && node->isVisible() );
			}
		}
	}
}


bool Node::isConnected( Node *node, NodeList *checkedNodes )
{
	if ( this == node )
		return true;
		
	bool firstNode = !checkedNodes;
	if (firstNode)
		checkedNodes = new NodeList();
		
	else if ( checkedNodes->contains(this) )
		return false;
	
	
	checkedNodes->append(this);
	
	const ConnectorList::const_iterator inputEnd = m_inputConnectorList.end();
	for ( ConnectorList::const_iterator it = m_inputConnectorList.begin(); it != inputEnd; ++it )
	{
		Connector *connector = *it;
		if (connector)
		{
			Node *startNode = connector->startNode();
			if ( startNode && startNode->isConnected( node, checkedNodes ) ) {
				if (firstNode) {
					delete checkedNodes;
				}
				return true;
			}
		}
	}
	
	const ConnectorList::const_iterator outputEnd = m_outputConnectorList.end();
	for ( ConnectorList::const_iterator it = m_outputConnectorList.begin(); it != outputEnd; ++it )
	{
		Connector *connector = *it;
		if (connector)
		{
			Node *endNode = connector->endNode();
			if ( endNode && endNode->isConnected( node, checkedNodes ) ) {
				if (firstNode) {
					delete checkedNodes;
				}
				return true;
			}
		}
	}
	
	if (firstNode) {
		delete checkedNodes;
	}
	
	return false;
}


void Node::setOrientation( node_dir dir )
{
	if ( m_dir == dir )
		return;
	
	if ( dir != Node::dir_up &&
		 dir != Node::dir_right &&
		 dir != Node::dir_down &&
		 dir != Node::dir_left )
	{
		kdDebug() << "Node::setOrientation: Unknown node direction "<<dir<<endl;
		return;
	}
	else
		m_dir = dir;
	initPoints();
}


void Node::initPoints()
{
	if ( type() == ec_junction )
	{
		setPoints( QPointArray( QRect( -4, -4, 8, 8 ) ) );
		return;
	}
	
	if ( type() == fp_junction )
	{
		setPoints( QPointArray( QRect( -4, -4, 9, 9 ) ) );
		return;
	}
	
	const int length = ( type() == ec_pin ) ? 8 : -8;
	
	// Bounding rectangle, facing right
	QPointArray pa( QRect( 0, -8, length, 16 ) );
	
	double angle;
	if		( m_dir == Node::dir_up ) angle = -90.;
	else if ( m_dir == Node::dir_right ) angle = 0.;
	else if ( m_dir == Node::dir_down ) angle = 90.;
	else if ( m_dir == Node::dir_left ) angle = 180.;
	else
	{
		kdError() << "Node::initPoints: unknown m_dir = "<<m_dir<<endl;
		return;
	}
	
	QWMatrix m;
	m.rotate(angle);
	pa = m.map(pa);
	setPoints(pa);
}


QPoint Node::findConnectorDivergePoint( bool * found )
{
	bool temp;
	if (!found)
		found = &temp;
	*found = false;
	
	if ( numCon( false, false ) != 2 )
		return QPoint(0,0);
	
	QPointList p1;
	QPointList p2;
	
	int inSize = m_inputConnectorList.count();
	
	const ConnectorList connectors = m_inputConnectorList + m_outputConnectorList;
	const ConnectorList::const_iterator end = connectors.end();
	bool gotP1 = false;
	bool gotP2 = false;
	int at = -1;
	for ( ConnectorList::const_iterator it = connectors.begin(); it != end && !gotP2; ++it )
	{
		at++;
		if ( !(*it) || !(*it)->canvas() )
			continue;
		
		if (gotP1)
		{
			p2 = (*it)->connectorPoints( at < inSize );
			gotP2 = true;
		}
		else
		{
			p1 = (*it)->connectorPoints( at < inSize );
			gotP1 = true;
		}
	}
	if ( !gotP1 || !gotP2 )
		return QPoint(0,0);
	
	unsigned maxLength = p1.size() > p2.size() ? p1.size() : p2.size();
	
	for ( unsigned i = 1; i < maxLength; ++i )
	{
		if ( p1[i] != p2[i] )
		{
			*found = true;
			return p1[i-1];
		}
	}
	return QPoint(0, 0);
}


void Node::setParentItem( CNItem *parentItem )
{
	if (!parentItem)
	{
		kdError() << k_funcinfo << "no parent item" << endl;
		return;
	}
	
	p_parentItem = parentItem;
	
	setLevel(p_parentItem->level());
	
	connect( p_parentItem, SIGNAL(movedBy(double, double )), this, SLOT(moveBy(double, double)) );
	connect( p_parentItem, SIGNAL(removed(Item*)), this, SLOT(removeNode(Item*)) );
}


void Node::removeNode()
{
	if (b_deleted)
		return;
	b_deleted = true;
	
	emit removed(this);
	p_icnDocument->appendDeleteList(this);
}


void Node::moveBy( double dx, double dy )
{
	if ( dx == 0 && dy == 0 ) return;
	QCanvasPolygon::moveBy( dx, dy );
	emit moved(this);
}


int Node::numCon( bool includeParentItem, bool includeHiddenConnectors ) const
{
	unsigned count = 0;
	
	const ConnectorList connectors[2] = { m_inputConnectorList, m_outputConnectorList };
	
	for ( unsigned i = 0; i < 2; i++ )
	{
		ConnectorList::const_iterator end = connectors[i].end();
		for ( ConnectorList::const_iterator it = connectors[i].begin(); it != end; ++it )
		{
			if ( *it && (includeHiddenConnectors || (*it)->canvas()) )
				count++;
		}
	}
	
	if ( isChildNode() && includeParentItem )
		count++;
	
	return count;
}


void Node::addOutputConnector( Connector * const connector )
{
	if ( type() == fp_in || !handleNewConnector(connector) )
		return;
	
	m_outputConnectorList.append(connector);
	
	if ( type() == fp_out || type() == fp_junction )
	{
		// We can only have one output connector, so remove the others. Note
		// that this code has to come *after* adding the new output connector,
		// as this node will delete itself if it's an fp_junction and there are
		// no output connectors.
		
		const ConnectorList connectors = m_outputConnectorList;
		const ConnectorList::const_iterator end = connectors.end();
		for ( ConnectorList::const_iterator it = connectors.begin(); it != end; ++it )
		{
			Connector * con = *it;
			if ( con && con != connector )
				con->removeConnector();
		}
	}
	
	m_outputConnectorList.remove((Connector*)0l);
}


void Node::addInputConnector( Connector * const connector )
{
	if ( type() == fp_out || !handleNewConnector(connector) )
		return;
	
	m_inputConnectorList.append(connector);
}


bool Node::handleNewConnector( Connector * connector )
{
	if (!connector)
		return false;
	
	if ( m_inputConnectorList.contains(connector) || m_outputConnectorList.contains(connector) )
	{
		kdWarning() << k_funcinfo << " Already have connector = " << connector << endl;
		return false;
	}
	
	connect( this, SIGNAL(removed(Node*)), connector, SLOT(removeConnector(Node*)) );
	connect( connector, SIGNAL(removed(Connector*)), this, SLOT(checkForRemoval(Connector*)) );
	connect( connector, SIGNAL(selected(bool)), this, SLOT(setNodeSelected(bool)) );
	
	if ( !isChildNode() )
		p_icnDocument->slotRequestAssignNG();
	
	return true;
}


Connector* Node::createInputConnector( Node * startNode )
{
	if ( type() == fp_out || !startNode )
		return 0l;
	
	Connector *connector = new Connector( startNode, this, p_icnDocument );
	addInputConnector(connector);
	
	return connector;
}


void Node::removeConnector( Connector *connector )
{
	if (!connector)
		return;
	
	ConnectorList::iterator it;
	
	it = m_inputConnectorList.find(connector);
	if ( it != m_inputConnectorList.end() )
	{
		(*it)->removeConnector();
		(*it) = 0L;
	}
	
	it = m_outputConnectorList.find(connector);
	if ( it != m_outputConnectorList.end() )
	{
		(*it)->removeConnector();
		(*it) = 0L;
	}
}

void Node::checkForRemoval( Connector *connector )
{
	removeConnector(connector);
	setNodeSelected(false);
	
	removeNullConnectors();
	
	if (!p_parentItem)
	{
		int conCount = m_inputConnectorList.count() + m_outputConnectorList.count();
		if ( conCount < 2 )
			removeNode();
	}
	
	if ( type() == Node::fp_junction && m_outputConnectorList.isEmpty() )
		removeNode();
}

void Node::removeNullConnectors()
{
	m_inputConnectorList.remove((Connector*)0L);
	m_outputConnectorList.remove((Connector*)0L);
}


NodeData Node::nodeData() const
{
	NodeData data;
	data.x = x();
	data.y = y();
	return data;
}


void Node::setNodeSelected( bool yes )
{
	if ( isSelected() == yes )
		return;
	
	QCanvasItem::setSelected(yes);
	
	setPen(   yes ? m_selectedColor : Qt::black );
	setBrush( yes ? m_selectedColor : Qt::black );
}

#include "node.moc"