summaryrefslogtreecommitdiffstats
path: root/src/flowparts/flowpart.h
blob: 3523b4e77c369564e554b9cbe4bcafb0500a5cf6 (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
/***************************************************************************
 *   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.                                   *
 ***************************************************************************/

#ifndef FLOWPART_H
#define FLOWPART_H

#include "cnitem.h"

class ICNDocument;
class Node;
class FlowCode;
class FlowCodeDocument;
class FlowPart;
class FPNode;
class TQPixmap;
class TQSize;

typedef TQValueList<FlowPart*> FlowPartList;

/**
All flow parts (eg 'CallSub', 'Read from Port' ,etc) should inherit from this class.
It provides basic functionality for creating commonly used nodes, as well as a virtual function
that you should reinherit for generating the assembly code.
@short Base class for all FlowParts
@author David Saxton
*/
class FlowPart : public CNItem
{
Q_OBJECT
  
public:
	enum FlowSymbol
	{
		ps_process, // Process - Plain rectangular box
		ps_call, // Call - Rectangular box with double vertical lines at either end
		ps_io, // I/O - Slanter rectangular box
		ps_round, // Start/End - Rounded rectangular box
		ps_decision, // Decision - Diamond shape
		ps_other // Custom shape, which is ignored by FlowPart
	};
	FlowPart( ICNDocument *icnDocument, bool newItem, const TQString &id );
	virtual ~FlowPart();
	
	virtual void generateMicrobe( FlowCode */*code*/ ) = 0;
	/**
	 * Set a preset "orientation" of this item - 0 through 7
	 */
	void setOrientation( uint orientation );
	uint orientation() const { return m_orientation; }
	/**
	 * The allowed orientations, as bit positions of 0 through 7
	 */
	uint allowedOrientations() const;
	virtual ItemData itemData() const;
	virtual void restoreFromItemData( const ItemData &itemData );
	/**
	 * Sets the caption displayed in the flowpart, resizes the item as necessary
	 */
	virtual void setCaption( const TQString &caption );
	/**
	 * Traces the FlowCode document route from the nodes with the given internal
	 * ids, and returns the FlowPart to which:
	 * @li all the routes from the given nodes are eventually connected to downwards
	 * @li their exists one (possibly internally branched) route for each node to that part
	 * @param ids The list of internal ids of the nodes for the paths to begin from - if empty,
	 *			  all nodes internal nodes are used
	 * @param previousParts A list of parts in the calling tree. This avoids infinite recursion.
	 * @returns The first FlowPart satisfying these conditions, or NULL if no such part exists
	 */
	FlowPart* endPart( TQStringList ids, FlowPartList *previousParts = 0l );
	/**
	 * Handles the addition of a if-else statement to the given FlowCode. This will
	 * order the code as necessary, adding the branches in the appropriate places
	 * @param code The FlowCode where the if-else will be added
	 * @param case1Statement The statement (e.g. "PORTA.0 is high") used for the first case
	 * @param case2Statement The logically opposite statement (e.g. "PORTA.0 is low") (note
	 						 that only one of the two statements will be used.
	 * @param case1 The internal node id for case1
	 * @param case2 The internal node id for case2
	 */
	void handleIfElse( FlowCode *code, const TQString &case1Statement, const TQString &case2Statement,
					   const TQString &case1, const TQString &case2 );
	/**
	 * Returns a pointer to the FlowPart that is connected to the node with the
	 * given internal id, or NULL if no such node / no connected part
	 */
	FlowPart* outputPart( const TQString& internalNodeId );
	/**
	 * Returns the FlowParts connected to the given node
	 * @see outputPart
	 */
	FlowPartList inputParts( const TQString& id );
	/**
	 * Returns a list of parts that are connected to *all* input parts
	 */
	FlowPartList inputParts();
	/**
	 * Returns a list of parts that are connected to *all* output parts. Note that if
	 * the same FlowPart is connected to more than one output, that flowpart will appear
	 * in the FlowPartList the number of times it is connected.
	 */
	FlowPartList outputParts();
	/**
	 * Draw the picture of the flowpart in the given orientation onto the pixmap
	 */
	void orientationPixmap( uint orientation, TQPixmap & pm ) const;
	virtual Variant * createProperty( const TQString & id, Variant::Type::Value type );
	
public slots:
	/**
	 * Called when variable name data for MicroSettings changes, and so our
	 * data needs updating
	 */
	void updateVarNames();
	/**
	 * Called when a variable name has changed (from an entry box)
	 */
	void varNameChanged( TQVariant newValue, TQVariant oldValue );
	/**
	 * Called when some of the FlowPart-specific variables (e.g. Pin or
	 * SevenSegment) requiring updating, such as when the PIC type changes
	 * or the list of Pin Maps changes.
	 */
	void slotUpdateFlowPartVariables();

protected:
	virtual void updateAttachedPositioning();
	/**
	 * Removes the node ids that shouldn't be used for finding the end part
	 */
	virtual void filterEndPartIDs( TQStringList *ids ) { Q_UNUSED(ids); }
	/**
	 * Normally just passes the paint request onto CNItem::drawShape,
	 * although in the case of some FlowSymbols (e.g. decision), it will handle
	 * the drawing itself
	 */
	virtual void drawShape( TQPainter &p );
	/**
	 * Returns the goto instruction that will goto the FlowPart that is connected
	 * to the node with the given internal id.
	 * For example, gotoCode("stdOutput") might return "goto delay__13"
	 */
	TQString gotoCode( const TQString& internalNodeId );
	/**
	 * Creates a FPNode with an internal id of "stdinput".
	 * The node is positioned half-way along the top of the FlowPart,
	 * as determined by width(), height(), x() and y()
	 */
	void createStdInput();
	/**
	 * Creates a FPNode with an internal id of "stdoutput".
	 * The node is positioned half-way along the bottom of the FlowPart,
	 * as determined by width(), height(), x() and y()
	 */
	void createStdOutput();
	/**
	 * Creates a FPNode with an internal id of "altoutput".
	 * The node is positioned half-way along the right of the FlowPart,
	 * as determined by width(), height(), x() and y()
	 */
	void createAltOutput();
	/**
	 * Initialises a symbol, by calling setItemPoints with the shape
	 */
	void initSymbol( FlowPart::FlowSymbol symbol, int width = 48 );
	
	void initProcessSymbol() { initSymbol( FlowPart::ps_process ); }
	void initCallSymbol() { initSymbol( FlowPart::ps_call ); }
	void initIOSymbol() { initSymbol( FlowPart::ps_io ); }
	void initRoundedRectSymbol() { initSymbol( FlowPart::ps_round ); }
	void initDecisionSymbol() { initSymbol( FlowPart::ps_decision ); }

	TQString m_caption;
	uint m_orientation;
	FPNode *m_stdInput;
	FPNode *m_stdOutput;
	FPNode *m_altOutput;
	TQGuardedPtr<FlowCodeDocument> m_pFlowCodeDocument;
	
	virtual void postResize();
	void updateNodePositions();
	
private:
	FlowSymbol m_flowSymbol;
};
typedef TQValueList<FlowPart*> FlowPartList;

#endif