Mercurial > MadButterfly
comparison pyink/FSM_window.py @ 1473:e807ad5aeb91
Extract update_graph for state and transition
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 24 Apr 2011 15:54:11 +0800 |
parents | 7cb7abb5063b |
children | 697ebfa9dc47 |
comparison
equal
deleted
inserted
replaced
1472:7cb7abb5063b | 1473:e807ad5aeb91 |
---|---|
62 _doc = None | 62 _doc = None |
63 _domview = None | 63 _domview = None |
64 _state = None | 64 _state = None |
65 trn_cond = None | 65 trn_cond = None |
66 trn_g = None | 66 trn_g = None |
67 _arrow_node = None | |
68 _path_node = None | |
67 | 69 |
68 def __init__(self, trn_cond): | 70 def __init__(self, trn_cond): |
69 self.trn_cond = trn_cond | 71 self.trn_cond = trn_cond |
70 pass | 72 pass |
71 | 73 |
72 def init(self, doc, domview, state): | 74 def init(self, doc, domview, state): |
73 self._doc = doc | 75 self._doc = doc |
74 self._domview = domview | 76 self._domview = domview |
75 self._state = state | 77 self._state = state |
76 pass | 78 pass |
77 | 79 |
78 def _draw_transition_real(self, parent, path): | 80 @staticmethod |
81 def _update_graph(path, arrow_node, path_node): | |
79 import math | 82 import math |
80 doc = self._doc | 83 |
81 | |
82 trn_g = doc.createElement('svg:g') | |
83 | |
84 path_node = doc.createElement('svg:path') | |
85 path_txt = 'M %f,%f C %f,%f %f,%f %f,%f' % tuple(path) | 84 path_txt = 'M %f,%f C %f,%f %f,%f %f,%f' % tuple(path) |
86 path_node.setAttribute('d', path_txt) | 85 path_node.setAttribute('d', path_txt) |
87 path_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' | 86 path_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' |
88 'fill: none') | 87 'fill: none') |
89 trn_g.appendChild(path_node) | |
90 | 88 |
91 # c0 c1 c2 c3 of cubic curve | 89 # c0 c1 c2 c3 of cubic curve |
92 c3 = (path[6], path[7]) | 90 c3 = (path[6], path[7]) |
93 c2 = (path[4], path[5]) | 91 c2 = (path[4], path[5]) |
94 c23_v = (c3[0] - c2[0], c3[1] - c2[1]) | 92 c23_v = (c3[0] - c2[0], c3[1] - c2[1]) |
97 odir = (-adir[1], adir[0]) # othogonal direction | 95 odir = (-adir[1], adir[0]) # othogonal direction |
98 arrow_pts = (c3[0], c3[1], | 96 arrow_pts = (c3[0], c3[1], |
99 -adir[0] * 10 + odir[0] * 4, -adir[1] * 10 + odir[1] * 4, | 97 -adir[0] * 10 + odir[0] * 4, -adir[1] * 10 + odir[1] * 4, |
100 -odir[0] * 8, -odir[1] * 8) | 98 -odir[0] * 8, -odir[1] * 8) |
101 arrow_txt = 'M %f,%f l %f,%f l %f,%f z' % arrow_pts | 99 arrow_txt = 'M %f,%f l %f,%f l %f,%f z' % arrow_pts |
102 arrow_node = doc.createElement('svg:path') | |
103 arrow_node.setAttribute('d', arrow_txt) | 100 arrow_node.setAttribute('d', arrow_txt) |
104 arrow_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' | 101 arrow_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' |
105 'fill: #000000') | 102 'fill: #000000') |
103 pass | |
104 | |
105 def _draw_transition_real(self, parent, path): | |
106 doc = self._doc | |
107 | |
108 trn_g = doc.createElement('svg:g') | |
109 | |
110 path_node = doc.createElement('svg:path') | |
111 arrow_node = doc.createElement('svg:path') | |
112 self._update_graph(path, arrow_node, path_node) | |
113 | |
114 trn_g.appendChild(path_node) | |
106 trn_g.appendChild(arrow_node) | 115 trn_g.appendChild(arrow_node) |
107 | 116 |
108 parent.appendChild(trn_g) | 117 parent.appendChild(trn_g) |
109 | 118 |
110 self.trn_g = trn_g | 119 return trn_g, path_node, arrow_node |
111 pass | |
112 | 120 |
113 @property | 121 @property |
114 def path(self): | 122 def path(self): |
115 domview = self._domview | 123 domview = self._domview |
116 state_name = self._state.state_name | 124 state_name = self._state.state_name |
118 trn = domview.get_transition(state_name, trn_cond) | 126 trn = domview.get_transition(state_name, trn_cond) |
119 return trn[3] | 127 return trn[3] |
120 | 128 |
121 def draw(self, parent): | 129 def draw(self, parent): |
122 path = self.path | 130 path = self.path |
123 self._draw_transition_real(parent, path) | 131 trn_g, arrow_node, path_node = self._draw_transition_real(parent, path) |
132 self.trn_g = trn_g | |
133 self._arrow_node = arrow_node | |
134 self._path_node = path_node | |
135 pass | |
136 | |
137 def clear(self): | |
138 trn_g = self.trn_g | |
139 parent = trn_g.parent() | |
140 parent.removeChild(trn_g) | |
124 pass | 141 pass |
125 pass | 142 pass |
126 | 143 |
127 class FSM_state(object): | 144 class FSM_state(object): |
128 _doc = None | 145 _doc = None |
129 _domview = None | 146 _domview = None |
130 state_name = None | 147 state_name = None |
131 state_g = None | 148 state_g = None |
149 _text_node = None | |
150 _circle_node = None | |
132 transitions = None | 151 transitions = None |
133 | 152 |
134 def __init__(self, state_name): | 153 def __init__(self, state_name): |
135 self.state_name = state_name | 154 self.state_name = state_name |
136 self.transitions = {} | 155 self.transitions = {} |
138 | 157 |
139 def init(self, doc, domview): | 158 def init(self, doc, domview): |
140 self._doc = doc | 159 self._doc = doc |
141 self._domview = domview | 160 self._domview = domview |
142 pass | 161 pass |
162 | |
163 @staticmethod | |
164 def _update_graph(text_node, text_content, circle_node, | |
165 state_name, r, x, y): | |
166 circle_node.setAttribute('r', str(r)) | |
167 circle_node.setAttribute('cx', str(x)) | |
168 circle_node.setAttribute('cy', str(y)) | |
169 circle_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' | |
170 'fill: #ffffff') | |
171 | |
172 text_node.setAttribute('font-size', '16') | |
173 text_node.setAttribute('style', 'stroke: #000000; fill: #000000') | |
174 | |
175 text_content.setContent(state_name) | |
176 | |
177 tx, ty, tw, th = text_node.getBBox() | |
178 text_node.setAttribute('x', str(x - tw / 2)) | |
179 text_node.setAttribute('y', str(y + th / 2)) | |
180 pass | |
143 | 181 |
144 def _draw_state_real(self, parent, state_name, r, x, y): | 182 def _draw_state_real(self, parent, state_name, r, x, y): |
145 doc = self._doc | 183 doc = self._doc |
146 | 184 |
147 state_g = doc.createElement('svg:g') | 185 state_g = doc.createElement('svg:g') |
148 state_g.setAttribute('inkscape:groupmode', 'layer') | 186 |
149 | 187 text = doc.createElement('svg:text') |
150 circle = doc.createElement('svg:circle') | 188 circle = doc.createElement('svg:circle') |
151 circle.setAttribute('r', str(r)) | 189 text_content = doc.createTextNode(state_name) |
152 circle.setAttribute('cx', str(x)) | 190 |
153 circle.setAttribute('cy', str(y)) | 191 text.appendChild(text_content) |
154 circle.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' | |
155 'fill: #ffffff') | |
156 state_g.appendChild(circle) | 192 state_g.appendChild(circle) |
157 | |
158 text = doc.createElement('svg:text') | |
159 text_content = doc.createTextNode(state_name) | |
160 text.appendChild(text_content) | |
161 text.setAttribute('font-size', '16') | |
162 text.setAttribute('style', 'stroke: #000000; fill: #000000') | |
163 state_g.appendChild(text) | 193 state_g.appendChild(text) |
164 | |
165 parent.appendChild(state_g) | 194 parent.appendChild(state_g) |
166 | 195 |
167 tx, ty, tw, th = text.getBBox() | 196 self._update_graph(text, text_content, circle, state_name, r, x, y) |
168 text.setAttribute('x', str(x - tw / 2)) | 197 |
169 text.setAttribute('y', str(y + th / 2)) | 198 return state_g, text, text_content, circle |
170 | |
171 return state_g | |
172 | 199 |
173 @property | 200 @property |
174 def r(self): | 201 def r(self): |
175 domview = self._domview | 202 domview = self._domview |
176 state_name = self.state_name | 203 state_name = self.state_name |
195 domview = self._domview | 222 domview = self._domview |
196 state_name = self.state_name | 223 state_name = self.state_name |
197 | 224 |
198 r = self.r | 225 r = self.r |
199 x, y = self.xy | 226 x, y = self.xy |
200 state_g = self._draw_state_real(parent, state_name, r, x, y) | 227 state_g, text_node, text_content, circle_node = \ |
228 self._draw_state_real(parent, state_name, r, x, y) | |
201 self.state_g = state_g | 229 self.state_g = state_g |
230 self._text_node = text_node | |
231 self._text_content = text_content | |
232 self._circle_node = circle_node | |
202 | 233 |
203 for trn_cond in self.all_transitions: | 234 for trn_cond in self.all_transitions: |
204 trn = FSM_transition(trn_cond) | 235 trn = FSM_transition(trn_cond) |
205 trn.init(self._doc, domview, self) | 236 trn.init(self._doc, domview, self) |
206 trn.draw(parent) | 237 trn.draw(parent) |
207 self.transitions[trn_cond] = trn | 238 self.transitions[trn_cond] = trn |
208 pass | 239 pass |
240 pass | |
241 | |
242 def clear(self): | |
243 state_g = self.state_g | |
244 parent = state_g.parent() | |
245 parent.removeChild(state_g) | |
209 pass | 246 pass |
210 pass | 247 pass |
211 | 248 |
212 class FSM_window(FSM_window_base): | 249 class FSM_window(FSM_window_base): |
213 __metaclass__ = data_monitor.data_monitor | 250 __metaclass__ = data_monitor.data_monitor |