annotate pyink/FSM_window.py @ 1481:28ab64f8581e

Moving state
author Thinker K.F. Li <thinker@codemud.net>
date Fri, 29 Apr 2011 01:27:47 +0800
parents e11ffd5fd609
children 3a671e79429a
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):
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
6 _state_editor = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
7 _state_name = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
8 _state_radius = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
9
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
10 _error_dialog = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
11 _error_dialog_label = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
12
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13 def __init__(self):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14 super(FSM_window_base, self).__init__()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16 dirname = os.path.dirname(__file__)
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17 fname = os.path.join(dirname, 'FSM_window.glade')
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 builder = gtk.Builder()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20 builder.add_from_file(fname)
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
22 main_win = builder.get_object("FSM_main_win")
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
23 view_box = builder.get_object("view_box")
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
24
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
25 state_editor = builder.get_object("state_editor")
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
26 state_name = builder.get_object('state_name')
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
27 state_radius = builder.get_object('state_radius')
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
28
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
29 error_dialog = builder.get_object('error_dialog')
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
30 error_dialog_label = builder.get_object('error_dialog_label')
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
31
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
32 builder.connect_signals(self)
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
33
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 self._builder = builder
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 self._main_win = main_win
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
36 self._view_box = view_box
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
37
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
38 self._state_editor = state_editor
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
39 self._state_name = state_name
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
40 self._state_radius = state_radius
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
41
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
42 self._error_dialog = error_dialog
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
43 self._error_dialog_label = error_dialog_label
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
44 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
45
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
46 def show_error(self, msg):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
47 error_dialog = self._error_dialog
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
48 error_dialog_label = self._error_dialog_label
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
49
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
50 error_dialog_label.set_text(msg)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
51 error_dialog.show()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
52 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
53
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
54 def hide_error(self):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
55 error_dialog = self._error_dialog
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
56 error_dialog.hide()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
57 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
58
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
59 def show_state_editor(self, state_name=''):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
60 state_name_inp = self._state_name
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
61 state_radius_inp = self._state_radius
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
62 state_editor = self._state_editor
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
63
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
64 state_name_inp.set_text(state_name)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
65 state_radius_inp.set_text('30')
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
66 state_editor.show()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
67 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
68
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
69 def hide_state_editor(self):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
70 state_editor = self._state_editor
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
71 state_editor.hide()
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
72 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
73
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
74 def show(self):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
75 self._main_win.show()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
76 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
77
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
78 def hide(self):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
79 self._main_win.hide()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
80 pass
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
81
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
82 def gtk_widget_hide(self, widget, event, *data):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
83 widget.hide()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
84 return True
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
85
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
86 def on_start_state_activate(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
87 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
89 def on_rename_state_activate(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
90 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
91
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
92 def on_remove_state_activate(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
93 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
94
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95 def on_zoom_out_clicked(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
96 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
97
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
98 def on_zoom_in_clicked(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
99 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
100
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
101 def on_move_state_toggled(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
102 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
103
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104 def on_add_state_toggled(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
105 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
106
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
107 def on_close_window_activate(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
108 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
109
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110 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
111 pass
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
112
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
113 def on_state_apply_clicked(self, *args):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
114 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
115
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
116 def on_state_cancel_clicked(self, *args):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
117 state_editor = self._state_editor
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
118 state_editor.hide()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
119 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
120
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
121 def on_error_dialog_ok_clicked(self, *args):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
122 error_dialog = self._error_dialog
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
123 error_dialog.hide()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
124 pass
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
125 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
126
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
127 class FSM_transition(object):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
128 _doc = None
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
129 _domview = None
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
130 _fsm_layer = None
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
131 _control_layer = None
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
132 _state = None
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
133 trn_cond = None
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
134 trn_g = None
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
135 _arrow_node = None
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
136 _path_node = None
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
137
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
138 def __init__(self, trn_cond):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
139 self.trn_cond = trn_cond
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
140 pass
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
141
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
142 def init(self, doc, domview, state, fsm_layer, control_layer):
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
143 self._doc = doc
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
144 self._domview = domview
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
145 self._state = state
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
146 self._fsm_layer = fsm_layer
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
147 self._control_layer = control_layer
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
148 pass
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
149
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
150 @staticmethod
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
151 def _update_graph(path, arrow_node, path_node):
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
152 import math
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
153
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
154 path_txt = 'M %f,%f C %f,%f %f,%f %f,%f' % tuple(path)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
155 path_node.setAttribute('d', path_txt)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
156 path_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; '
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
157 'fill: none')
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
158
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
159 # c0 c1 c2 c3 of cubic curve
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
160 c3 = (path[6], path[7])
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
161 c2 = (path[4], path[5])
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
162 c23_v = (c3[0] - c2[0], c3[1] - c2[1])
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
163 c23_len = math.sqrt(c23_v[0] * c23_v[0] + c23_v[1] * c23_v[1])
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
164 adir = (c23_v[0] / c23_len, c23_v[1] / c23_len) # arrow direction
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
165 odir = (-adir[1], adir[0]) # othogonal direction
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
166 arrow_pts = (c3[0], c3[1],
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
167 -adir[0] * 10 + odir[0] * 4, -adir[1] * 10 + odir[1] * 4,
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
168 -odir[0] * 8, -odir[1] * 8)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
169 arrow_txt = 'M %f,%f l %f,%f l %f,%f z' % arrow_pts
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
170 arrow_node.setAttribute('d', arrow_txt)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
171 arrow_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; '
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
172 'fill: #000000')
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
173 pass
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
174
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
175 def _draw_transition_real(self, parent, path):
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
176 doc = self._doc
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
177
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
178 trn_g = doc.createElement('svg:g')
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
179
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
180 path_node = doc.createElement('svg:path')
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
181 arrow_node = doc.createElement('svg:path')
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
182 self._update_graph(path, arrow_node, path_node)
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
183
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
184 trn_g.appendChild(path_node)
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
185 trn_g.appendChild(arrow_node)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
186
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
187 parent.appendChild(trn_g)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
188
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
189 return trn_g, path_node, arrow_node
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
190
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
191 @property
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
192 def path(self):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
193 domview = self._domview
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
194 state_name = self._state.state_name
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
195 trn_cond = self.trn_cond
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
196 trn = domview.get_transition(state_name, trn_cond)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
197 return trn[3]
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
198
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
199 def draw(self, parent):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
200 path = self.path
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
201 trn_g, arrow_node, path_node = self._draw_transition_real(parent, path)
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
202 self.trn_g = trn_g
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
203 self._arrow_node = arrow_node
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
204 self._path_node = path_node
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
205 pass
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
206
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
207 def clear(self):
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
208 trn_g = self.trn_g
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
209 parent = trn_g.parent()
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
210 parent.removeChild(trn_g)
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
211 pass
1474
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
212
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
213 def update(self):
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
214 path = self.path
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
215 arrow_node = self._arrow_node
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
216 path_node = self._path_node
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
217 self._update_graph(path, arrow_node, path_node)
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
218 pass
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
219 pass
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
220
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
221 class FSM_state(object):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
222 _doc = None
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
223 _domview = None
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
224 _fsm_layer = None
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
225 _control_layer = None
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
226 state_name = None
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
227 state_g = None
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
228 _text_node = None
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
229 _circle_node = None
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
230 transitions = None
1480
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
231
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
232 _state_g_hdl_id = None
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
233 _selected_rect = None
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
234
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
235 def __init__(self, state_name):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
236 self.state_name = state_name
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
237 self.transitions = {}
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
238 pass
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
239
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
240 def init(self, doc, domview, fsm_layer, control_layer):
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
241 self._doc = doc
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
242 self._domview = domview
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
243 self._fsm_layer = fsm_layer
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
244 self._control_layer = control_layer
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
245 pass
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
246
1480
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
247 def _update_graph(self, text_node, text_content, circle_node,
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
248 state_name, r, x, y):
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
249 circle_node.setAttribute('r', str(r))
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
250 circle_node.setAttribute('cx', str(x))
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
251 circle_node.setAttribute('cy', str(y))
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
252 circle_node.setAttribute('style', 'stroke: #000000; stroke-width: 1; '
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
253 'fill: #ffffff')
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
254
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
255 text_node.setAttribute('style', 'stroke: #000000; fill: #000000; font-size: 16px')
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
256
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
257 text_content.setContent(state_name)
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
258
1480
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
259 doc = self._doc
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
260 spdoc = doc.spdoc
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
261 spdoc.ensureUpToDate()
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
262 tx, ty, tw, th = text_node.getBBox()
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
263 text_node.setAttribute('x', str(x - tw / 2))
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
264 text_node.setAttribute('y', str(y + th / 2))
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
265 pass
1480
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
266
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
267 def grab(self, callback):
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
268 assert not self._state_g_hdl_id
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
269
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
270 state_g = self.state_g
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
271 state_g_spitem = state_g.spitem
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
272 state_g_hdl_id = state_g_spitem.connect('mouse-event', callback)
1480
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
273 self._state_g_hdl_id = state_g_hdl_id
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
274 pass
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
275
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
276 def ungrab(self):
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
277 if not self._state_g_hdl:
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
278 return
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
279 state_g = self.state_g
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
280 state_g_hdl_id = self._state_g_hdl_id
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
281 state_g.disconnect(state_g_hdl_id)
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
282 pass
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
283
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
284 def show_selected(self):
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
285 if not self._selected_rect:
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
286 doc = self._doc
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
287 rect = doc.createElement('svg:rect')
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
288 control_layer = self._control_layer
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
289 rect.setAttribute('style',
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
290 'stroke: #404040; stroke-width: 1; '
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
291 'stroke-dasharray: 5 3; fill: none')
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
292 control_layer.appendChild(rect)
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
293 self._selected_rect = rect
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
294 pass
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
295
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
296 state_g = self.state_g
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
297 rect = self._selected_rect
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
298
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
299 x, y, w, h = state_g.getBBox()
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
300 rect.setAttribute('x', str(x - 2))
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
301 rect.setAttribute('y', str(y - 2))
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
302 rect.setAttribute('width', str(w + 4))
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
303 rect.setAttribute('height', str(h + 4))
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
304 pass
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
305
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
306 def hide_selected(self):
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
307 if not self._selected_rect:
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
308 return
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
309
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
310 state_g = self.state_g
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
311 rect = self._selected_rect
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
312 state_g.removeChild(rect)
e11ffd5fd609 functions for showing selected control for state
Thinker K.F. Li <thinker@codemud.net>
parents: 1479
diff changeset
313 pass
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
314
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
315 def _draw_state_real(self, parent, state_name, r, x, y):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
316 doc = self._doc
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
317
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
318 state_g = doc.createElement('svg:g')
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
319
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
320 text = doc.createElement('svg:text')
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
321 circle = doc.createElement('svg:circle')
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
322 text_content = doc.createTextNode(state_name)
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
323
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
324 text.appendChild(text_content)
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
325 state_g.appendChild(circle)
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
326 state_g.appendChild(text)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
327 parent.appendChild(state_g)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
328
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
329 self._update_graph(text, text_content, circle, state_name, r, x, y)
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
330
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
331 return state_g, text, text_content, circle
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
332
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
333 @property
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
334 def r(self):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
335 domview = self._domview
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
336 state_name = self.state_name
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
337 r = domview.get_state_r(state_name)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
338 return r
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
339
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
340 @property
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
341 def xy(self):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
342 domview = self._domview
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
343 state_name = self.state_name
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
344 xy = domview.get_state_xy(state_name)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
345 return xy
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
346
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
347 @property
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
348 def all_transitions(self):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
349 domview = self._domview
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
350 state_name = self.state_name
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
351 conds = domview.all_transitions(state_name)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
352 return conds
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
353
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
354 def draw(self, parent):
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
355 domview = self._domview
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
356 state_name = self.state_name
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
357
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
358 r = self.r
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
359 x, y = self.xy
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
360 state_g, text_node, text_content, circle_node = \
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
361 self._draw_state_real(parent, state_name, r, x, y)
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
362 self.state_g = state_g
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
363 self._text_node = text_node
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
364 self._text_content = text_content
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
365 self._circle_node = circle_node
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
366
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
367 for trn_cond in self.all_transitions:
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
368 trn = FSM_transition(trn_cond)
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
369 trn.init(self._doc, domview, self,
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
370 self._fsm_layer, self._control_layer)
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
371 trn.draw(parent)
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
372 self.transitions[trn_cond] = trn
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
373 pass
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
374 pass
1473
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
375
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
376 def clear(self):
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
377 state_g = self.state_g
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
378 parent = state_g.parent()
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
379 parent.removeChild(state_g)
e807ad5aeb91 Extract update_graph for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1472
diff changeset
380 pass
1474
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
381
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
382 def update(self):
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
383 text_node = self._text_node
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
384 text_content = self._text_content
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
385 circle_node = self._circle_node
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
386 state_name = self.state_name
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
387 r = self.r
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
388 x, y = self.xy
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
389 self._update_graph(text_node, text_content, circle_node, state_name,
1474
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
390 r, x, y)
697ebfa9dc47 Update state and transition from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1473
diff changeset
391 pass
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
392 pass
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
393
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
394 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
395 __metaclass__ = data_monitor.data_monitor
59b90d7fcf57 Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1457
diff changeset
396 __data_monitor_prefix__ = 'on_'
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
397
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
398 _background = None
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
399 _fsm_layer = None
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
400 _control_layer = None
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
401 width = 1024
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
402 height = 768
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
403
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
404 _grab_hdl = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
405 _bg_hdl = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
406
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
407 _saved_x = 0
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
408 _saved_y = 0
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
409
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
410 _state_mouse_event_handler = None
1458
59b90d7fcf57 Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1457
diff changeset
411
59b90d7fcf57 Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1457
diff changeset
412 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
413 super(FSM_window, self).__init__()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
414
1458
59b90d7fcf57 Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1457
diff changeset
415 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
416
6927debad4ee Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents: 1458
diff changeset
417 self._domview = domview_ui
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
418 self._states = {}
1458
59b90d7fcf57 Lock UI event handlers for FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1457
diff changeset
419
1457
416a18409603 Show an empty document in SVG viewer widget
Thinker K.F. Li <thinker@codemud.net>
parents: 1456
diff changeset
420 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
421 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
422 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
423
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
424 def _init_layers(self):
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
425 doc = self._doc()
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
426 root = self._root()
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
427
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
428 root.setAttribute('inkscape:groupmode', 'layer')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
429
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
430 background = doc.createElement('svg:rect')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
431 background.setAttribute('x', '0')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
432 background.setAttribute('y', '0')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
433 background.setAttribute('width', str(self.width))
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
434 background.setAttribute('height', str(self.height))
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
435 background.setAttribute('style', 'fill: #ffffff')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
436 root.appendChild(background)
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
437 self._background = background
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
438
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
439 fsm_layer = doc.createElement('svg:g')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
440 fsm_layer.setAttribute('inkscape:groupmode', 'layer')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
441 root.appendChild(fsm_layer)
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
442 self._fsm_layer = fsm_layer
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
443
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
444 control_layer = doc.createElement('svg:g')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
445 control_layer.setAttribute('inkscape:groupmode', 'layer')
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
446 root.appendChild(control_layer)
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
447 self._control_layer = control_layer
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
448
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
449 self.grab_bg(self.on_add_state_background)
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
450 pass
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
451
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
452 def _doc(self):
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
453 view_widget = self._view_widget
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
454 view = view_widget.view
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
455 doc = view.doc().rdoc
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
456 return doc
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
457
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
458 def _root(self):
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
459 doc = self._doc()
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
460 root = doc.root()
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
461 return root
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
462
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
463 def _translate_xy(self, x, y):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
464 return x, y
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
465
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
466 def _clear_view(self):
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
467 if not self._background:
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
468 self._init_layers()
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
469 return
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
470
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
471 children = [child for child in self._fsm_layer.childList()] + \
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
472 [child for child in self._control_layer.childList()]
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
473 for child in children:
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
474 parent = child.parent()
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
475 parent.removeChild(child)
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
476 pass
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
477
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
478 self._states = {}
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
479 pass
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
480
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
481 def _draw_state_domview(self, state_name):
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
482 domview = self._domview
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
483 doc = self._doc()
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
484 fsm_layer = self._fsm_layer
1472
7cb7abb5063b Refactory drawing functions into classes for state and transition
Thinker K.F. Li <thinker@codemud.net>
parents: 1471
diff changeset
485
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
486 state = FSM_state(state_name)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
487 state.init(doc, domview, self._fsm_layer, self._control_layer)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
488 self._states[state_name] = state
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
489
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
490 state.draw(fsm_layer)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
491 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
492
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
493 def on_state_mouse_event(self, state, evtype, buttons, x, y):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
494 if self._state_mouse_event_handler:
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
495 self._state_mouse_event_handler(state, evtype, buttons, x, y)
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
496 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
497 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
498
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
499 def _install_state_event_handler(self, state):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
500 def mouse_event_handler(item, evtype, buttons, x, y):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
501 self.on_state_mouse_event(state, evtype, buttons, x, y)
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
502 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
503 state.grab(mouse_event_handler)
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
504 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
505
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
506 def grab_state(self, callback):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
507 assert self._state_mouse_event_handler is None
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
508 self._state_mouse_event_handler = callback
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
509 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
510
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
511 def ungrab_state(self):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
512 self._state_mouse_event_handler = None
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
513 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
514
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
515 def _update_view(self):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
516 self._clear_view()
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
517 states = self._states
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
518
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
519 domview = self._domview
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
520
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
521 state_names = domview.all_state_names()
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
522 for state_name in state_names:
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
523 self._draw_state_domview(state_name)
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
524 state = states[state_name]
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
525 self._install_state_event_handler(state)
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
526 pass
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
527 pass
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
528
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
529 def set_svg_view(self, view):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
530 self._view_box.add(view)
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
531 self._view_widget = view
1479
92a8497d0361 Make FSM editor scrollable
Thinker K.F. Li <thinker@codemud.net>
parents: 1478
diff changeset
532
92a8497d0361 Make FSM editor scrollable
Thinker K.F. Li <thinker@codemud.net>
parents: 1478
diff changeset
533 root = self._root()
92a8497d0361 Make FSM editor scrollable
Thinker K.F. Li <thinker@codemud.net>
parents: 1478
diff changeset
534 root.setAttribute('width', '1024')
92a8497d0361 Make FSM editor scrollable
Thinker K.F. Li <thinker@codemud.net>
parents: 1478
diff changeset
535 root.setAttribute('height', '768')
92a8497d0361 Make FSM editor scrollable
Thinker K.F. Li <thinker@codemud.net>
parents: 1478
diff changeset
536 view.setResize(True, 800, 600)
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
537 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
538
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
539 def on_close_window_activate(self, *args):
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
540 self._close_cb()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
541 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
542
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
543 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
544 self._destroy_cb()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
545 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
546
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
547 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
548 self._destroy_cb()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
549 pass
1467
6927debad4ee Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents: 1458
diff changeset
550
6927debad4ee Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents: 1458
diff changeset
551 def on_add_state_toggled(self, *args):
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
552 self.ungrab_bg()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
553 self.grab_bg(self.on_add_state_background)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
554 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
555
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
556 def on_move_state_toggled(self, *args):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
557 self.ungrab_bg()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
558 self.grab_bg(self.on_move_state_background)
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
559 self.ungrab_state()
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
560 self.grab_state(self.handle_move_state_state)
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
561 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
562
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
563 def on_add_state_background(self, item, evtype, buttons, x, y):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
564 import pybInkscape
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
565
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
566 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_RELEASE and \
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
567 buttons == 1:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
568 self._saved_x = x
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
569 self._saved_y = y
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
570 self.show_state_editor()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
571 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
572 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
573
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
574 def on_move_state_background(self, item, evtype, buttons, x, y):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
575 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
576
1481
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
577 def handle_move_state_state(self, state, evtype, buttons, x, y):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
578 import pybInkscape
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
579
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
580 def moving_state(item, evtype, buttons, x, y):
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
581 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_RELEASE:
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
582 self.ungrab_mouse()
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
583 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
584 new_state_x = orign_state_x + start_x - x
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
585 new_state_y = orign_state_y + start_y - y
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
586
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
587 domview = self._domview
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
588 domview.set_state_xy(state.state_name, x, y)
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
589 state.update()
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
590 state.show_selected()
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
591 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
592
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
593 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_PRESS:
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
594 start_x = x
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
595 start_y = y
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
596 orign_state_x, orign_state_y = state.xy
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
597
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
598 state.show_selected()
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
599
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
600 self.grab_mouse(moving_state)
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
601 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
602 pass
28ab64f8581e Moving state
Thinker K.F. Li <thinker@codemud.net>
parents: 1480
diff changeset
603
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
604 def on_state_apply_clicked(self, *args):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
605 import traceback
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
606
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
607 domview = self._domview
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
608 x, y = self._translate_xy(self._saved_x, self._saved_y)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
609
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
610 state_name = self._state_name.get_text()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
611 r_txt = self._state_radius.get_text()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
612 try:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
613 r = float(r_txt)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
614 except ValueError:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
615 traceback.print_exc()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
616 self.show_error('Invalid value: "%s" is not a valid value '
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
617 'for radius.' % (r_txt))
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
618 return
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
619
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
620 try:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
621 domview.add_state(state_name)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
622 except:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
623 traceback.print_exc()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
624 self.show_error('Invalid state name: "%s" is existing' %
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
625 (state_name))
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
626 return
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
627 domview.set_state_xy(state_name, x, y)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
628 domview.set_state_r(state_name, r)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
629
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
630 self._draw_state_domview(state_name)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
631
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
632 self.hide_state_editor()
1475
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
633 pass
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
634
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
635 def _install_test_data(self):
1476
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
636 self._init_layers()
eddec4543761 Install layers for FSM_window.
Thinker K.F. Li <thinker@codemud.net>
parents: 1475
diff changeset
637
1467
6927debad4ee Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents: 1458
diff changeset
638 domview = self._domview
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
639
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
640 view = self._view_widget.view
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
641 doc = view.doc()
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
642 rdoc = doc.rdoc
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
643 root_node = doc.root().repr
1479
92a8497d0361 Make FSM editor scrollable
Thinker K.F. Li <thinker@codemud.net>
parents: 1478
diff changeset
644
1469
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
645 line_node = rdoc.createElement('svg:line')
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
646 line_node.setAttribute('x1', '10')
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
647 line_node.setAttribute('y1', '10')
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
648 line_node.setAttribute('x2', '100')
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
649 line_node.setAttribute('y2', '100')
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
650 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
651
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
652 root_node.appendChild(line_node)
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
653
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
654 def show_msg(*args, **kws):
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
655 print 'mouse_event'
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
656 print args
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
657 pass
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
658 print 'before connect'
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
659 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
660 print hdl_id
c1e70540541c Drawing functions for states and transitions
Thinker K.F. Li <thinker@codemud.net>
parents: 1467
diff changeset
661
1471
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
662 state1 = 'state 1'
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
663 domview.add_state(state1)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
664 domview.set_state_r(state1, 50)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
665 domview.set_state_xy(state1, 200, 100)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
666 state2 = 'state 2'
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
667 domview.add_state(state2)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
668 domview.set_state_r(state2, 30)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
669 domview.set_state_xy(state2, 300, 100)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
670 domview.add_transition(state1, 'event1', state2)
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
671 domview.set_transition_path(state1, 'event1', (200, 150,
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
672 240, 180,
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
673 260, 180,
055845649807 Update FSM_window from domview
Thinker K.F. Li <thinker@codemud.net>
parents: 1469
diff changeset
674 300, 130))
1467
6927debad4ee Fix issue and a simple testcase for FSM
Thinker K.F. Li <thinker@codemud.net>
parents: 1458
diff changeset
675 pass
1475
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
676
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
677 def show(self):
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
678 self._install_test_data()
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
679 self._install_test_data = lambda: None
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
680 self._update_view()
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
681 super(FSM_window, self).show()
8c6078c17f2d Install test data when show FSM_window
Thinker K.F. Li <thinker@codemud.net>
parents: 1474
diff changeset
682 pass
1478
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
683
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
684 def grab_mouse(self, callback):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
685 assert self._grab_hdl is None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
686
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
687 root = self._root()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
688 root.setAttribute('inkscape:groupmode', '')
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
689 self._grab_hdl = root.spitem.connect('mouse-event', callback)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
690 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
691
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
692 def ungrab_mouse(self):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
693 if not self._grab_hdl:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
694 return
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
695
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
696 root = self._root()
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
697 root.spitem.disconnect(self._grab_hdl)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
698 self._grab_hdl = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
699 root.setAttribute('inkscape:groupmode', 'layer')
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
700 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
701
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
702 def grab_bg(self, callback):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
703 assert self._bg_hdl is None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
704 assert self._background
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
705
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
706 background = self._background
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
707 bg_hdl = background.spitem.connect('mouse-event', callback)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
708 self._bg_hdl = bg_hdl
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
709 pass
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
710
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
711 def ungrab_bg(self):
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
712 if not self._bg_hdl:
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
713 return
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
714
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
715 background = self._background
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
716 bg_hdl = self._bg_hdl
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
717 background.spitem.disconnect(bg_hdl)
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
718 self._bg_hdl = None
6fe773e62b2a Add state to FSM.
Thinker K.F. Li <thinker@codemud.net>
parents: 1476
diff changeset
719 pass
1456
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
720 pass
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
721
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
722 if __name__ == '__main__':
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
723 win = FSM_window()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
724 win._main_win.connect('destroy', gtk.main_quit)
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
725 win.show()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
726 gtk.main()
894a4bf35fe6 Start implement FSM editor window
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
727 pass