summaryrefslogtreecommitdiffstats
path: root/src/fpnode.cpp
blob: b62f0ae1669be9e6b6c78a9ecce5848b1850ae3c (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
/***************************************************************************
 *   Copyright (C) 2003-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 "flowpart.h"
#include "fpnode.h"

#include <kdebug.h>
#include <tqpainter.h>

FPNode::FPNode( ICNDocument *icnDocument, Node::node_type type, node_dir dir, const TQPoint &pos, TQString *id )
	: Node( icnDocument, type, dir, pos, id )
{
	icnDocument->registerItem(this);
}


FPNode::~FPNode()
{
}


FlowPart *FPNode::outputFlowPart() const
{
	FlowPart *flowPart = dynamic_cast<FlowPart*>(parentItem());
	
	if ( type() == fp_in )
		return flowPart;
	
	if ( m_outputConnectorList.size() > 1 )
		kdError() << "FpNode::outputFlowPart(): outputConnectorList() size is greater than 1"<<endl;
	
	else if ( m_outputConnectorList.size() < 1 )
		return 0l;
	
	ConnectorList::const_iterator it = m_outputConnectorList.begin();
	if ( it == m_outputConnectorList.end() || !*it || !(*it)->endNode()  )
		return 0L;
	
	return (dynamic_cast<FPNode*>((*it)->endNode()))->outputFlowPart();
}


FlowPartList FPNode::inputFlowParts() const
{
	FlowPartList list;
	FlowPart *flowPart = dynamic_cast<FlowPart*>(parentItem());
	if ( type() != fp_in && flowPart )
	{
		list.append(flowPart);
		return list;
	}
	const ConnectorList::const_iterator end = m_inputConnectorList.end();
	for ( ConnectorList::const_iterator it = m_inputConnectorList.begin(); it != end; ++it )
	{
		if (*it)
		{
			Node *startNode = (*it)->startNode();
			FlowPart *flowPart = startNode ? dynamic_cast<FlowPart*>(startNode->parentItem()) : 0l;
			if (flowPart)
				list.append(flowPart);
		}
	}
	return list;
}


inline TQPointArray arrowPoints( Node::node_dir dir )
{
	TQPointArray pa(3);
	switch (dir)
	{
		case Node::dir_right:
			pa[0] = TQPoint( 3, 0 );
			pa[1] = TQPoint( 0, 2 );
			pa[2] = TQPoint( 0, -2 );
			break;
		case Node::dir_left:
			pa[0] = TQPoint( -3, 0 );
			pa[1] = TQPoint( 0, 2 );
			pa[2] = TQPoint( 0, -2 );
			break;
		case Node::dir_down:
			pa[0] = TQPoint( 2, 0 );
			pa[1] = TQPoint( -2, 0 );
			pa[2] = TQPoint( 0, 3 );
			break;
		case Node::dir_up:
			pa[0] = TQPoint( 2, 0 );
			pa[1] = TQPoint( -2, 0 );
			pa[2] = TQPoint( 0, -3 );
			break;
	};
	return pa;
}


void FPNode::drawShape( TQPainter &p )
{
	const int _x = (int)x();
	const int _y = (int)y();
	
	if ( type() == fp_junction && !m_inputConnectorList.isEmpty() )
	{
		const ConnectorList::iterator end = m_inputConnectorList.end();
		for ( ConnectorList::iterator it = m_inputConnectorList.begin(); it != end; ++ it)
		{
			Connector * connector = *it;
			if (!connector)
				continue;
		
			// Work out the direction of the connector
			const TQPointList points = connector->connectorPoints(false);
		
			const int count = points.size();
			if ( count < 2 )
				continue;
		
			TQPoint end_0 = points[count-1];
			TQPoint end_1 = points[count-2];
		
			TQPointArray pa;
			if ( end_0.x() < end_1.x() )
			{
				pa = arrowPoints( Node::dir_left );
				pa.translate( 4, 0 );
			}
			else if ( end_0.x() > end_1.x() )
			{
				pa = arrowPoints( Node::dir_right );
				pa.translate( -4, 0 );
			}
			else if ( end_0.y() < end_1.y() )
			{
				pa = arrowPoints( Node::dir_up );
				pa.translate( 0, 4 );
			}
			else if ( end_0.y() > end_1.y() )
			{
				pa = arrowPoints( Node::dir_down );
				pa.translate( 0, -4 );
			}
			else
				continue;
			
			pa.translate( _x, _y );
			p.setPen( connector->isSelected() ? m_selectedColor : TQt::black );
			p.drawPolygon(pa);
		}
		return;
	}
	
	if		( m_dir == Node::dir_right )	p.drawLine( _x, _y, _x-8, _y );
	else if ( m_dir == Node::dir_down )		p.drawLine( _x, _y, _x, _y-8 );
	else if ( m_dir == Node::dir_left )		p.drawLine( _x, _y, _x+8, _y );
	else if ( m_dir == Node::dir_up )		p.drawLine( _x, _y, _x, _y+8 );
	
	TQPointArray pa(3);	
	
	// Right facing arrow
	if ( (type() == fp_out && m_dir == Node::dir_right) ||
			 (type() == fp_in && m_dir == Node::dir_left ) )
		pa = arrowPoints( Node::dir_right );
	
	// Left facing arrow
	else if ( (type() == fp_out && m_dir == Node::dir_left) ||
				  (type() == fp_in && m_dir == Node::dir_right) )
		pa = arrowPoints( Node::dir_left );
	
	// Down facing arrow
	else if ( (type() == fp_out && m_dir == Node::dir_down) ||
				  (type() == fp_in && m_dir == Node::dir_up) )
		pa = arrowPoints( Node::dir_down );
	
	// Up facing arrow
	else
		pa = arrowPoints( Node::dir_up );
	
	
	// Note: I have not tested the positioning of the arrows for all combinations.
	// In fact, most almost definitely do not work. So feel free to change the code
	// as you see fit if necessary.
	
	if ( type() == fp_out )
	{
		if		( m_dir == Node::dir_right ) pa.translate( -5, 0 );
		else if ( m_dir == Node::dir_down ) pa.translate( 0, -5 );
		else if ( m_dir == Node::dir_left ) pa.translate( 5, 0 );
		else if ( m_dir == Node::dir_up ) pa.translate( 0, 5 );
	}
	else if ( type() == fp_in )
	{
		if		( m_dir == Node::dir_right );
		else if ( m_dir == Node::dir_down );
		else if ( m_dir == Node::dir_left ) pa.translate( 3, 0 );
		else if ( m_dir == Node::dir_up ) pa.translate( 0, 3 );
	}
	else return;
	
	pa.translate( _x, _y );
	p.drawPolygon(pa);
}

#include "fpnode.moc"