Mercurial > MadButterfly
annotate pyink/FSM_window.py @ 1469:c1e70540541c
Drawing functions for states and transitions
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 24 Apr 2011 12:30:47 +0800 |
parents | 6927debad4ee |
children | 055845649807 |
rev | line source |
---|---|
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
1 import gtk |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
2 import os |
1458
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
3 import data_monitor |
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
4 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
5 class FSM_window_base(object): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
6 def __init__(self): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
7 super(FSM_window_base, self).__init__() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
8 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
9 dirname = os.path.dirname(__file__) |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
10 fname = os.path.join(dirname, 'FSM_window.glade') |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
11 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
12 builder = gtk.Builder() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
13 builder.add_from_file(fname) |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
14 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
15 main_win = builder.get_object("FSM_main_win") |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
16 view_box = builder.get_object("view_box") |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
17 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
18 builder.connect_signals(self) |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
19 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
20 self._builder = builder |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
21 self._main_win = main_win |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
22 self._view_box = view_box |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
23 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
24 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
25 def show(self): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
26 self._main_win.show() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
27 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
28 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
29 def hide(self): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
30 self._main_win.hide() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
31 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
32 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
33 def on_start_state_activate(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
34 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
35 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
36 def on_rename_state_activate(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
37 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
38 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
39 def on_remove_state_activate(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
40 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
41 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
42 def on_zoom_out_clicked(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
43 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
44 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
45 def on_zoom_in_clicked(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
46 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
47 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
48 def on_move_state_toggled(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
49 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
50 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
51 def on_add_state_toggled(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
52 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
53 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
54 def on_close_window_activate(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
55 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
56 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
57 def on_FSM_main_win_destroy_event(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
58 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
59 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
60 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
61 class FSM_window(FSM_window_base): |
1458
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
62 __metaclass__ = data_monitor.data_monitor |
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
63 __data_monitor_prefix__ = 'on_' |
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
64 |
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
65 def __init__(self, domview_ui, close_cb, destroy_cb): |
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
66 super(FSM_window, self).__init__() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
67 |
1458
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
68 self._locker = domview_ui |
1467
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
69 |
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
70 self._domview = domview_ui |
1469
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
71 self._state_nodes = {} |
1458
59b90d7fcf57
Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents:
1457
diff
changeset
|
72 |
1457
416a18409603
Show an empty document in SVG viewer widget
Thinker K.F. Li <thinker@codemud.net>
parents:
1456
diff
changeset
|
73 self._close_cb = close_cb # callback to close editor window (hide) |
416a18409603
Show an empty document in SVG viewer widget
Thinker K.F. Li <thinker@codemud.net>
parents:
1456
diff
changeset
|
74 self._destroy_cb = destroy_cb # callback to destroy editor window |
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
75 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
76 |
1469
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
77 def _doc(self): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
78 view_widget = self._view_widget |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
79 view = view_widget.view |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
80 doc = view.doc().rdoc |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
81 return doc |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
82 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
83 def _root(self): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
84 doc = self._doc() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
85 root = doc.root() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
86 return root |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
87 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
88 def _clear_view(self): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
89 root = self._root() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
90 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
91 children = [child for child in root.childList() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
92 if child.name() == 'svg:g'] |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
93 for child in children: |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
94 root.removeChild(child) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
95 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
96 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
97 self._state_nodes = {} |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
98 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
99 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
100 def _draw_transition_real(self, state_g, path): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
101 import math |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
102 doc = self._doc() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
103 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
104 path_node = doc.createElement('svg:path') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
105 path_txt = 'M %f,%f C %f,%f %f,%f %f,%f' % tuple(path) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
106 path_node.setAttribute('d', path_txt) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
107 path_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
108 'fill: none') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
109 state_g.appendChild(path_node) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
110 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
111 # c0 c1 c2 c3 of cubic curve |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
112 c3 = (path[6], path[7]) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
113 c2 = (path[4], path[5]) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
114 c23_v = (c3[0] - c2[0], c3[1] - c2[1]) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
115 c23_len = math.sqrt(c23_v[0] * c23_v[0] + c23_v[1] * c23_v[1]) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
116 adir = (c23_v[0] / c23_len, c23_v[1] / c23_len) # arrow direction |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
117 odir = (-adir[1], adir[0]) # othogonal direction |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
118 arrow_pts = (c3[0], c3[1], |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
119 -adir[0] * 10 + odir[0] * 4, -adir[1] * 10 + odir[1] * 4, |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
120 -odir[0] * 8, -odir[1] * 8) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
121 arrow_txt = 'M %f,%f l %f,%f l %f,%f z' % arrow_pts |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
122 arrow_node = doc.createElement('svg:path') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
123 arrow_node.setAttribute('d', arrow_txt) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
124 arrow_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
125 'fill: #000000') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
126 state_g.appendChild(arrow_node) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
127 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
128 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
129 def _draw_state_real(self, state_name, r, x, y): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
130 doc = self._doc() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
131 root = self._root() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
132 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
133 state_g = doc.createElement('svg:g') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
134 state_g.setAttribute('inkscape:groupmode', 'layer') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
135 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
136 circle = doc.createElement('svg:circle') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
137 circle.setAttribute('r', str(r)) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
138 circle.setAttribute('cx', str(x)) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
139 circle.setAttribute('cy', str(y)) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
140 circle.setAttribute('style', 'stroke: #000000; stroke-width: 1; ' |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
141 'fill: #ffffff') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
142 state_g.appendChild(circle) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
143 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
144 text = doc.createElement('svg:text') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
145 text_content = doc.createTextNode(state_name) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
146 text.appendChild(text_content) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
147 text.setAttribute('font-size', '16') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
148 text.setAttribute('style', 'stroke: #000000; fill: #000000') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
149 state_g.appendChild(text) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
150 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
151 root.appendChild(state_g) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
152 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
153 tx, ty, tw, th = text.getBBox() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
154 text.setAttribute('x', str(x - tw / 2)) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
155 text.setAttribute('y', str(y + th / 2)) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
156 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
157 return state_g |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
158 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
159 def _draw_state(self, state_name): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
160 domview = self._domview |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
161 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
162 r = domview.get_state_r(state_name) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
163 x, y = domview.get_state_xy(state_name) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
164 state_g = self._draw_state_real(state_name, r, x, y) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
165 self._state_nodes[state_name] = state_g |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
166 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
167 transitions = [domview.get_transition(state_name, trn_name)[3] |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
168 for trn_name in domview.all_transitions()] |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
169 for trn in transitions: |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
170 self._draw_transition_real(state_g, trn) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
171 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
172 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
173 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
174 def _update_view(self): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
175 self._clear_view() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
176 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
177 domview = self._domview |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
178 state_names = domview.all_state_names() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
179 for state_name in state_names: |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
180 self._draw_state(state_name) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
181 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
182 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
183 |
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
184 def set_svg_view(self, view): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
185 self._view_box.add(view) |
1469
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
186 self._view_widget = view |
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
187 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
188 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
189 def on_close_window_activate(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
190 self._close_cb() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
191 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
192 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
193 def on_FSM_main_win_destroy_event(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
194 self._destroy_cb() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
195 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
196 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
197 def on_FSM_main_win_delete_event(self, *args): |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
198 self._destroy_cb() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
199 pass |
1467
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
200 |
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
201 def on_add_state_toggled(self, *args): |
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
202 domview = self._domview |
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
203 domview.add_state('test0') |
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
204 domview.add_state('test1') |
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
205 domview.add_transition('test0', 'event1', 'test1') |
1469
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
206 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
207 view = self._view_widget.view |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
208 doc = view.doc() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
209 rdoc = doc.rdoc |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
210 root_node = doc.root().repr |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
211 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
212 line_node = rdoc.createElement('svg:line') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
213 line_node.setAttribute('x1', '10') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
214 line_node.setAttribute('y1', '10') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
215 line_node.setAttribute('x2', '100') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
216 line_node.setAttribute('y2', '100') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
217 line_node.setAttribute('style', 'stroke: #000000; stroke-width:2') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
218 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
219 print root_node.name() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
220 print root_node.childList()[-1].name() |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
221 root_node.setAttribute('inkscape:groupmode', 'layer') |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
222 root_node.appendChild(line_node) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
223 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
224 def show_msg(*args, **kws): |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
225 print 'mouse_event' |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
226 print args |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
227 pass |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
228 print 'before connect' |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
229 hdl_id = line_node.spitem.connect('mouse-event', show_msg) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
230 print hdl_id |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
231 |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
232 state_g = self._draw_state_real('test1', 40, 100, 50) |
c1e70540541c
Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents:
1467
diff
changeset
|
233 self._draw_transition_real(state_g, (100, 100, 140, 120, 160, 120, 200, 100)) |
1467
6927debad4ee
Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents:
1458
diff
changeset
|
234 pass |
1456
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
235 pass |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
236 |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
237 if __name__ == '__main__': |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
238 win = FSM_window() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
239 win._main_win.connect('destroy', gtk.main_quit) |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
240 win.show() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
241 gtk.main() |
894a4bf35fe6
Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff
changeset
|
242 pass |