Mercurial > MadButterfly
comparison pyink/FSM_window.py @ 1510:47b02e97bdee
Delete states and transitions
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 10 May 2011 01:14:55 +0800 |
parents | 4a2402ac34dd |
children | 4c349b214877 |
comparison
equal
deleted
inserted
replaced
1509:4a2402ac34dd | 1510:47b02e97bdee |
---|---|
413 state_name = self._state.state_name | 413 state_name = self._state.state_name |
414 trn_cond = self.trn_cond | 414 trn_cond = self.trn_cond |
415 trn = domview.get_transition(state_name, trn_cond) | 415 trn = domview.get_transition(state_name, trn_cond) |
416 return trn[2] | 416 return trn[2] |
417 | 417 |
418 def draw(self, parent): | 418 def draw(self): |
419 path = self.path | 419 path = self.path |
420 trn_g, path_node, arrow_node = self._draw_transition_real(parent, path) | 420 fsm_layer = self._fsm_layer |
421 trn_g, path_node, arrow_node = \ | |
422 self._draw_transition_real(fsm_layer, path) | |
421 self.trn_g = trn_g | 423 self.trn_g = trn_g |
422 self._arrow_node = arrow_node | 424 self._arrow_node = arrow_node |
423 self._path_node = path_node | 425 self._path_node = path_node |
424 pass | 426 pass |
425 | 427 |
763 states = self._states | 765 states = self._states |
764 | 766 |
765 trn = FSM_transition(condition) | 767 trn = FSM_transition(condition) |
766 trn.init(self._doc, domview, self, states, | 768 trn.init(self._doc, domview, self, states, |
767 self._fsm_layer, self._control_layer) | 769 self._fsm_layer, self._control_layer) |
768 trn.draw(parent) | 770 trn.draw() |
769 self.transitions[condition] = trn | 771 self.transitions[condition] = trn |
770 pass | 772 pass |
771 | 773 |
772 def draw(self, parent): | 774 def draw(self): |
773 state_name = self.state_name | 775 state_name = self.state_name |
776 fsm_layer = self._fsm_layer | |
774 | 777 |
775 r = self.r | 778 r = self.r |
776 x, y = self.xy | 779 x, y = self.xy |
777 state_g, text_node, text_content, circle_node = \ | 780 state_g, text_node, text_content, circle_node = \ |
778 self._draw_state_real(parent, state_name, r, x, y) | 781 self._draw_state_real(fsm_layer, state_name, r, x, y) |
779 self.state_g = state_g | 782 self.state_g = state_g |
780 self._text_node = text_node | 783 self._text_node = text_node |
781 self._text_content = text_content | 784 self._text_content = text_content |
782 self._circle_node = circle_node | 785 self._circle_node = circle_node |
783 | 786 |
784 for trn_cond in self.all_transitions: | 787 for trn_cond in self.all_transitions: |
785 self._load_transition_domview(parent, trn_cond) | 788 self._load_transition_domview(fsm_layer, trn_cond) |
786 pass | 789 pass |
787 pass | 790 pass |
788 | 791 |
789 def clear(self): | 792 def clear(self): |
790 state_g = self.state_g | 793 state_g = self.state_g |
821 for target_state in target_states: | 824 for target_state in target_states: |
822 target_state.from_states.add(state_name) | 825 target_state.from_states.add(state_name) |
823 pass | 826 pass |
824 pass | 827 pass |
825 | 828 |
829 def rm_target_from_states(self): | |
830 state_name = self.state_name | |
831 | |
832 transitions = self.transitions.values() | |
833 target_names = [trn.target for trn in transitions] | |
834 | |
835 states = self._states | |
836 for target_name in target_names: | |
837 target_state = states[target_name] | |
838 target_state.from_states.remove(state_name) | |
839 pass | |
840 pass | |
841 | |
826 def adjust_transitions(self): | 842 def adjust_transitions(self): |
827 import itertools | 843 import itertools |
828 | 844 |
829 states = self._states | 845 states = self._states |
830 | 846 |
917 self.selected_state = state | 933 self.selected_state = state |
918 state.show_selected() | 934 state.show_selected() |
919 | 935 |
920 def hide(): | 936 def hide(): |
921 state.hide_selected() | 937 state.hide_selected() |
922 self.selected_state = None | 938 self.reset() |
923 pass | 939 pass |
924 self.deselect = hide | 940 self.deselect = hide |
925 pass | 941 pass |
926 | 942 |
927 def select_transition(self, transition): | 943 def select_transition(self, transition): |
930 self.selected_transition = transition | 946 self.selected_transition = transition |
931 transition.show_selected() | 947 transition.show_selected() |
932 | 948 |
933 def hide(): | 949 def hide(): |
934 transition.hide_selected() | 950 transition.hide_selected() |
935 self.selected_transition = None | 951 self.reset() |
936 pass | 952 pass |
937 self.deselect = hide | 953 self.deselect = hide |
938 pass | 954 pass |
939 | 955 |
940 def control_transition(self, transition): | 956 def control_transition(self, transition): |
943 self.controlled_transition = transition | 959 self.controlled_transition = transition |
944 transition.show_control_points() | 960 transition.show_control_points() |
945 | 961 |
946 def hide(): | 962 def hide(): |
947 transition.hide_control_points() | 963 transition.hide_control_points() |
948 self.controlled_transition = None | 964 self.reset() |
949 pass | 965 pass |
950 self.deselect = hide | 966 self.deselect = hide |
967 pass | |
968 | |
969 def reset(self): | |
970 try: | |
971 del self.deselect | |
972 except AttributeError: | |
973 pass | |
974 self.selected_state = None | |
975 self.selected_transition = None | |
976 self.controlled_transition = None | |
951 pass | 977 pass |
952 pass | 978 pass |
953 | 979 |
954 | 980 |
955 class _FSM_popup(object): | 981 class _FSM_popup(object): |
1043 src_state = self._menu_state | 1069 src_state = self._menu_state |
1044 src_name = src_state.state_name | 1070 src_name = src_state.state_name |
1045 cond = '' | 1071 cond = '' |
1046 | 1072 |
1047 domview = self._domview | 1073 domview = self._domview |
1048 domview.add_transition(src_name, cond, target_name) | 1074 try: |
1049 src_state.add_transition(fsm_layer, cond) | 1075 domview.add_transition(src_name, cond, target_name) |
1050 | 1076 except: |
1051 trn = src_state.transitions[cond] | 1077 import traceback |
1052 window._install_transition_event_handler(trn) | 1078 traceback.print_exc() |
1079 window.show_error('invalid condition: %s' % (cond)) | |
1080 else: | |
1081 src_state.add_transition(fsm_layer, cond) | |
1082 | |
1083 trn = src_state.transitions[cond] | |
1084 window._install_transition_event_handler(trn) | |
1085 pass | |
1053 | 1086 |
1054 window.pop_grabs() | 1087 window.pop_grabs() |
1055 | 1088 |
1056 target_state.stop_hint() | 1089 target_state.stop_hint() |
1057 select = self._select | 1090 select = self._select |
1166 | 1199 |
1167 window.hide_state_editor() | 1200 window.hide_state_editor() |
1168 select.deselect() | 1201 select.deselect() |
1169 pass | 1202 pass |
1170 | 1203 |
1204 def _handle_del_state(self, *args): | |
1205 window = self._window | |
1206 select = self._select | |
1207 domview = self._domview | |
1208 | |
1209 state = select.selected_state | |
1210 state_name = state.state_name | |
1211 | |
1212 select.deselect() | |
1213 | |
1214 states = window._states | |
1215 del states[state_name] | |
1216 | |
1217 state.clear() | |
1218 | |
1219 # Remove out transitions | |
1220 transitions = state.transitions | |
1221 for trn in transitions.values(): | |
1222 trn.clear() | |
1223 pass | |
1224 | |
1225 state.rm_target_from_states() | |
1226 | |
1227 # Remove in-transition | |
1228 src_state_names = state.from_states | |
1229 for src_state_name in src_state_names: | |
1230 src_state = states[src_state_name] | |
1231 in_cond_trns = [(in_cond, in_trn) | |
1232 for in_cond, in_trn in \ | |
1233 src_state.transitions.items() | |
1234 if in_trn.target == state_name] | |
1235 for in_cond, in_trn in in_cond_trns: | |
1236 in_trn.clear() | |
1237 del src_state.transitions[in_cond] | |
1238 domview.rm_transition(src_state_name, in_cond) | |
1239 pass | |
1240 pass | |
1241 | |
1242 domview.rm_state(state_name) | |
1243 pass | |
1244 | |
1245 def _handle_del_transition(self, *args): | |
1246 window = self._window | |
1247 select = self._select | |
1248 domview = self._domview | |
1249 | |
1250 trn = select.selected_transition | |
1251 trn.clear() | |
1252 trn_cond = trn.trn_cond | |
1253 | |
1254 trn_state = trn.state | |
1255 trn_state_name = trn_state.state_name | |
1256 trn_target_name = trn.target | |
1257 target_names = [trn.target for trn in trn_state.transitions.values()] | |
1258 | |
1259 # the transition must live until getting all info. | |
1260 domview.rm_transition(trn_state_name, trn_cond) | |
1261 | |
1262 if trn_target_name not in target_names: | |
1263 trn_target_state = window._states[trn_target_name] | |
1264 trn_target_state.from_states.remove(trn_state_name) | |
1265 pass | |
1266 | |
1267 del trn_state.transitions[trn_cond] | |
1268 pass | |
1269 | |
1171 def popup_install_handler(self): | 1270 def popup_install_handler(self): |
1172 window = self._window | 1271 window = self._window |
1173 | 1272 |
1174 window.grab_add_transition(self._handle_add_transition) | 1273 window.grab_add_transition(self._handle_add_transition) |
1175 window.grab_edit_transition(self._handle_edit_transition) | 1274 window.grab_edit_transition(self._handle_edit_transition) |
1176 window.grab_edit_state(self._handle_edit_state) | 1275 window.grab_edit_state(self._handle_edit_state) |
1177 window.grab_transition_apply(self._handle_transition_apply) | 1276 window.grab_transition_apply(self._handle_transition_apply) |
1178 window.grab_state_apply(self._handle_state_change) | 1277 window.grab_state_apply(self._handle_state_change) |
1278 window.grab_del_state(self._handle_del_state) | |
1279 window.grab_del_transition(self._handle_del_transition) | |
1179 pass | 1280 pass |
1180 pass | 1281 pass |
1181 | 1282 |
1182 | 1283 |
1183 class _FSM_move_state_mode(object): | 1284 class _FSM_move_state_mode(object): |
1585 _edit_transition_cb = None | 1686 _edit_transition_cb = None |
1586 _transition_apply_cb = None | 1687 _transition_apply_cb = None |
1587 _transition_mouse_event_handler = None | 1688 _transition_mouse_event_handler = None |
1588 _edit_state_cb = None | 1689 _edit_state_cb = None |
1589 _state_apply_cb = None | 1690 _state_apply_cb = None |
1691 _del_state_cb = None | |
1692 _del_transition_cb = None | |
1590 | 1693 |
1591 _grab_stack = None | 1694 _grab_stack = None |
1592 | 1695 |
1593 _select = None | 1696 _select = None |
1594 | 1697 |
1669 pass | 1772 pass |
1670 | 1773 |
1671 self._states = {} | 1774 self._states = {} |
1672 pass | 1775 pass |
1673 | 1776 |
1674 def _draw_state_domview(self, state_name): | 1777 def _make_state_domview(self, state_name): |
1675 domview = self._domview | 1778 domview = self._domview |
1676 doc = self._doc() | 1779 doc = self._doc() |
1677 fsm_layer = self._fsm_layer | 1780 fsm_layer = self._fsm_layer |
1678 states = self._states | 1781 states = self._states |
1679 | 1782 |
1680 state = FSM_state(state_name) | 1783 state = FSM_state(state_name) |
1681 state.init(doc, domview, states, self._fsm_layer, self._control_layer) | 1784 state.init(doc, domview, states, self._fsm_layer, self._control_layer) |
1682 self._states[state_name] = state | 1785 self._states[state_name] = state |
1683 | 1786 |
1684 state.draw(fsm_layer) | 1787 return state |
1685 pass | |
1686 | 1788 |
1687 def _set_leave_mode_cb(self, callback): | 1789 def _set_leave_mode_cb(self, callback): |
1688 self._leave_mode_cb = callback | 1790 self._leave_mode_cb = callback |
1689 pass | 1791 pass |
1690 | 1792 |
1706 self.ungrab_transition() | 1808 self.ungrab_transition() |
1707 self.ungrab_edit_transition() | 1809 self.ungrab_edit_transition() |
1708 self.ungrab_edit_state() | 1810 self.ungrab_edit_state() |
1709 self.ungrab_transition_apply() | 1811 self.ungrab_transition_apply() |
1710 self.ungrab_state_apply() | 1812 self.ungrab_state_apply() |
1813 self.ungrab_del_state() | |
1814 self.ungrab_del_transition() | |
1711 pass | 1815 pass |
1712 | 1816 |
1713 def save_grabs(self): | 1817 def save_grabs(self): |
1714 save = (self._bg_mouse_event_cb, | 1818 save = (self._bg_mouse_event_cb, |
1715 self._state_mouse_event_handler, | 1819 self._state_mouse_event_handler, |
1716 self._transition_mouse_event_handler, | 1820 self._transition_mouse_event_handler, |
1717 self._add_transition_cb, | 1821 self._add_transition_cb, |
1718 self._edit_transition_cb, | 1822 self._edit_transition_cb, |
1719 self._transition_apply_cb, | 1823 self._transition_apply_cb, |
1720 self._edit_state_cb, | 1824 self._edit_state_cb, |
1721 self._state_apply_cb) | 1825 self._state_apply_cb, |
1826 self._del_state_cb, | |
1827 self._del_transition_cb) | |
1722 return save | 1828 return save |
1723 | 1829 |
1724 def restore_grabs(self, save): | 1830 def restore_grabs(self, save): |
1725 self._bg_mouse_event_cb, \ | 1831 self._bg_mouse_event_cb, \ |
1726 self._state_mouse_event_handler, \ | 1832 self._state_mouse_event_handler, \ |
1727 self._transition_mouse_event_handler, \ | 1833 self._transition_mouse_event_handler, \ |
1728 self._add_transition_cb, \ | 1834 self._add_transition_cb, \ |
1729 self._edit_transition_cb, \ | 1835 self._edit_transition_cb, \ |
1730 self._transition_apply_cb, \ | 1836 self._transition_apply_cb, \ |
1731 self._edit_state_cb, \ | 1837 self._edit_state_cb, \ |
1732 self._state_apply_cb \ | 1838 self._state_apply_cb, \ |
1839 self._del_state_cb, \ | |
1840 self._del_transition_cb \ | |
1733 = save | 1841 = save |
1734 pass | 1842 pass |
1735 | 1843 |
1736 ## \brief Push current setting of grab handles into the stack. | 1844 ## \brief Push current setting of grab handles into the stack. |
1737 # | 1845 # |
1835 | 1943 |
1836 def ungrab_state_apply(self): | 1944 def ungrab_state_apply(self): |
1837 self._state_apply_cb = None | 1945 self._state_apply_cb = None |
1838 pass | 1946 pass |
1839 | 1947 |
1840 def _load_new_state(self, state_name): | 1948 def grab_del_state(self, callback): |
1949 assert self._del_state_cb is None | |
1950 self._del_state_cb = callback | |
1951 pass | |
1952 | |
1953 def ungrab_del_state(self): | |
1954 self._del_state_cb = None | |
1955 pass | |
1956 | |
1957 def grab_del_transition(self, callback): | |
1958 assert self._del_transition_cb is None | |
1959 self._del_transition_cb = callback | |
1960 pass | |
1961 | |
1962 def ungrab_del_transition(self): | |
1963 self._del_transition_cb = None | |
1964 pass | |
1965 | |
1966 def _draw_new_state(self, state_name): | |
1841 states = self._states | 1967 states = self._states |
1842 | 1968 |
1843 self._draw_state_domview(state_name) | |
1844 state = states[state_name] | 1969 state = states[state_name] |
1970 state.draw() | |
1845 self._install_state_event_handler(state) | 1971 self._install_state_event_handler(state) |
1846 | 1972 |
1847 for trn in state.transitions.values(): | 1973 for trn in state.transitions.values(): |
1848 self._install_transition_event_handler(trn) | 1974 self._install_transition_event_handler(trn) |
1849 pass | 1975 pass |
1976 pass | |
1977 | |
1978 def _load_new_state(self, state_name): | |
1979 state = self._make_state_domview(state_name) | |
1980 self._draw_new_state(state_name) | |
1850 pass | 1981 pass |
1851 | 1982 |
1852 ## \brief Load new state incrementally. | 1983 ## \brief Load new state incrementally. |
1853 # | 1984 # |
1854 def _load_new_state_incr(self, state_name): | 1985 def _load_new_state_incr(self, state_name): |
1862 states = self._states | 1993 states = self._states |
1863 domview = self._domview | 1994 domview = self._domview |
1864 state_names = domview.all_state_names() | 1995 state_names = domview.all_state_names() |
1865 for state_name in state_names: | 1996 for state_name in state_names: |
1866 state = states[state_name] | 1997 state = states[state_name] |
1998 self._draw_new_state(state_name) | |
1867 state.tell_target_states() | 1999 state.tell_target_states() |
1868 pass | 2000 pass |
1869 pass | 2001 pass |
1870 | 2002 |
1871 def _update_view(self): | 2003 def _update_view(self): |
1874 | 2006 |
1875 domview = self._domview | 2007 domview = self._domview |
1876 | 2008 |
1877 state_names = domview.all_state_names() | 2009 state_names = domview.all_state_names() |
1878 for state_name in state_names: | 2010 for state_name in state_names: |
1879 self._load_new_state(state_name) | 2011 self._make_state_domview(state_name) |
1880 pass | 2012 pass |
1881 self._rebuild_from_states() | 2013 self._rebuild_from_states() |
1882 pass | 2014 pass |
1883 | 2015 |
1884 def set_svg_view(self, view): | 2016 def set_svg_view(self, view): |
1943 pass | 2075 pass |
1944 | 2076 |
1945 def on_edit_state_activate(self, *args): | 2077 def on_edit_state_activate(self, *args): |
1946 if self._edit_state_cb: | 2078 if self._edit_state_cb: |
1947 self._edit_state_cb(*args) | 2079 self._edit_state_cb(*args) |
2080 pass | |
2081 pass | |
2082 | |
2083 def on_del_state_activate(self, *args): | |
2084 if self._del_state_cb: | |
2085 self._del_state_cb(*args) | |
2086 pass | |
2087 pass | |
2088 | |
2089 def on_del_transition_activate(self, *args): | |
2090 if self._del_transition_cb: | |
2091 self._del_transition_cb(*args) | |
1948 pass | 2092 pass |
1949 pass | 2093 pass |
1950 | 2094 |
1951 def _install_test_data(self): | 2095 def _install_test_data(self): |
1952 self._init_layers() | 2096 self._init_layers() |
1993 def show(self): | 2137 def show(self): |
1994 if _install_test_data_flag: | 2138 if _install_test_data_flag: |
1995 self._install_test_data() | 2139 self._install_test_data() |
1996 self._install_test_data = lambda: None | 2140 self._install_test_data = lambda: None |
1997 pass | 2141 pass |
2142 | |
2143 # | |
2144 # Crash if without line. | |
2145 # This line remove selection infor to prevent select manager to | |
2146 # deselect an object selected in previous session. It would crash | |
2147 # if we don't reset it and domview of previous session were | |
2148 # removed. | |
2149 # | |
2150 self._select.reset() | |
2151 | |
1998 self._update_view() | 2152 self._update_view() |
1999 self._add_state_mode.activate() | 2153 self._add_state_mode.activate() |
2000 super(FSM_window, self).show() | 2154 super(FSM_window, self).show() |
2001 pass | 2155 pass |
2002 | 2156 |