changeset 1506:f185a9d23a30

_FSM_popup handles edit state
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 09 May 2011 17:13:43 +0800
parents 7b8e67b2391b
children bebf73ee38c8
files pyink/FSM_window.py pyink/domview.py pyink/domview_ui.py
diffstat 3 files changed, 75 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pyink/FSM_window.py	Mon May 09 15:20:29 2011 +0800
+++ b/pyink/FSM_window.py	Mon May 09 17:13:43 2011 +0800
@@ -168,8 +168,8 @@
         state_editor = self._state_editor
         
         state_name_inp.set_text(state_name)
-        state_radius_inp.set_text(radius)
-        state_entry_action.set_text(entry_action)
+        state_radius_inp.set_text(str(radius))
+        state_entry_action.set_text(entry_action or '')
         state_editor.show()
         pass
 
@@ -745,6 +745,13 @@
         return xy
 
     @property
+    def entry_action(self):
+        domview = self._domview
+        state_name = self.state_name
+        entry_action = domview.get_state_entry_action(state_name)
+        return entry_action
+
+    @property
     def all_transitions(self):
         domview = self._domview
         state_name = self.state_name
@@ -984,6 +991,7 @@
         if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_PRESS and \
                 button == 3:
             self._show_state_menu(state)
+            self._select.select_state(state)
         elif evtype == pybInkscape.PYSPItem.PYB_EVENT_MOUSE_ENTER:
             state.start_hint()
         elif evtype == pybInkscape.PYSPItem.PYB_EVENT_MOUSE_LEAVE:
@@ -1119,6 +1127,45 @@
         pass
 
     def _handle_edit_state(self, *args):
+        select = self._select
+        state = select.selected_state
+        name = state.state_name
+        r = state.r
+        entry_action = state.entry_action
+        
+        window = self._window
+        window.show_state_editor(name, r, entry_action)
+        pass
+
+    def _handle_state_change(self):
+        window = self._window
+        domview = self._domview
+        select = self._select
+
+        state_name_txt = window._state_name
+        state_radius_txt = window._state_radius
+        state_entry_action_txt = window._state_entry_action
+
+        state_name = state_name_txt.get_text()
+        state_radius = state_radius_txt.get_text()
+        state_entry_action = state_entry_action_txt.get_text()
+
+        state_radius_i = int(state_radius)
+
+        state = select.selected_state
+        old_state_name = state.state_name
+        if old_state_name != state_name:
+            domview.rename_state(old_state_name, state_name)
+            state.state_name = state_name
+            pass
+        
+        domview.set_state_r(state_name, state_radius_i)
+        domview.set_state_entry_action(state_name, state_entry_action)
+        
+        state.update()
+
+        window.hide_state_editor()
+        select.deselect()
         pass
 
     def popup_install_handler(self):
@@ -1128,6 +1175,7 @@
         window.grab_edit_transition(self._handle_edit_transition)
         window.grab_edit_state(self._handle_edit_state)
         window.grab_transition_apply(self._handle_transition_apply)
+        window.grab_state_apply(self._handle_state_change)
         pass
     pass
 
@@ -1417,7 +1465,7 @@
         self._select = select_man
         pass
 
-    def handle_new_state(self):
+    def _handle_add_new_state(self):
         import traceback
         
         domview = self._domview
@@ -1538,9 +1586,9 @@
         window._emit_leave_mode()
         window.ungrab_all()
         
-        window.ungrab_bg()
         window.grab_bg(self._handle_add_state_background)
         window.grab_state(self._handle_state_mouse_events)
+        window.grab_state_apply(self._handle_add_new_state)
         pass
 
     def deactivate(self):
@@ -1577,6 +1625,7 @@
     _transition_apply_cb = None
     _transition_mouse_event_handler = None
     _edit_state_cb = None
+    _state_apply_cb = None
 
     _grab_stack = None
 
@@ -1697,6 +1746,7 @@
         self.ungrab_edit_transition()
         self.ungrab_edit_state()
         self.ungrab_transition_apply()
+        self.ungrab_state_apply()
         pass
 
     def save_grabs(self):
@@ -1706,7 +1756,8 @@
                 self._add_transition_cb,
                 self._edit_transition_cb,
                 self._transition_apply_cb,
-                self._edit_state_cb)
+                self._edit_state_cb,
+                self._state_apply_cb)
         return save
 
     def restore_grabs(self, save):
@@ -1716,7 +1767,8 @@
             self._add_transition_cb, \
             self._edit_transition_cb, \
             self._transition_apply_cb, \
-            self._edit_state_cb \
+            self._edit_state_cb, \
+            self._state_apply_cb \
             = save
         pass
 
@@ -1815,6 +1867,15 @@
         self._edit_state_cb = None
         pass
 
+    def grab_state_apply(self, callback):
+        assert self._state_apply_cb is None
+        self._state_apply_cb = callback
+        pass
+
+    def ungrab_state_apply(self):
+        self._state_apply_cb = None
+        pass
+
     def _load_new_state(self, state_name):
         states = self._states
         
@@ -1897,7 +1958,9 @@
         pass
 
     def on_state_apply_clicked(self, *args):
-        self._add_state_mode.handle_new_state()
+        if self._state_apply_cb:
+            self._state_apply_cb()
+            pass
         pass
 
     def on_add_transition_activate(self, *args):
--- a/pyink/domview.py	Mon May 09 15:20:29 2011 +0800
+++ b/pyink/domview.py	Mon May 09 17:13:43 2011 +0800
@@ -529,6 +529,9 @@
         pass
 
     def rename_state(self, state_name, new_name):
+        if new_name in self.fsm_states:
+            raise ValueError, 'New state name %s is duplicated' % (new_name)
+        
         state = self.fsm_states[state_name]
         del self.fsm_states[state_name]
         self.fsm_states[new_name] = state
@@ -1132,7 +1135,7 @@
 
     def get_state_entry_action(self, state_name):
         state = self._get_state(state_name)
-        action = state.get_entry_action()
+        action = state.entry_action
         return action
 
     def get_state_r(self, state_name):
--- a/pyink/domview_ui.py	Mon May 09 15:20:29 2011 +0800
+++ b/pyink/domview_ui.py	Mon May 09 17:13:43 2011 +0800
@@ -540,7 +540,7 @@
         pass
 
     def set_state_entry_action(self, state_name, entry_action):
-        self._dom.set_entry_action(state_name, entry_action)
+        self._dom.set_state_entry_action(state_name, entry_action)
         pass
 
     def set_state_r(self, state_name, r):