00001 """
00002 Copyright 2007 Free Software Foundation, Inc.
00003 This file is part of GNU Radio
00004
00005 GNU Radio Companion is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009
00010 GNU Radio Companion is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00018 """
00019
00020
00021
00022
00023 from Actions import FLOW_GRAPH_UNDO, FLOW_GRAPH_REDO, get_action_from_name
00024
00025 from Constants import STATE_CACHE_SIZE
00026
00027 class StateCache(object):
00028 """
00029 The state cache is an interface to a list to record data/states and to revert to previous states.
00030 States are recorded into the list in a circular fassion by using an index for the current state,
00031 and counters for the range where states are stored.
00032 """
00033
00034 def __init__(self, initial_state):
00035 """!
00036 StateCache constructor.
00037 @param initial_state the intial state (nested data)
00038 """
00039 self.states = [None] * STATE_CACHE_SIZE
00040 self.current_state_index = 0
00041 self.num_prev_states = 0
00042 self.num_next_states = 0
00043 self.states[0] = initial_state
00044 self.update_actions()
00045
00046 def save_new_state(self, state):
00047 """!
00048 Save a new state.
00049 Place the new state at the next index and add one to the number of previous states.
00050 @param state the new state
00051 """
00052 self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
00053 self.states[self.current_state_index] = state
00054 self.num_prev_states = self.num_prev_states + 1
00055 if self.num_prev_states == STATE_CACHE_SIZE: self.num_prev_states = STATE_CACHE_SIZE - 1
00056 self.num_next_states = 0
00057 self.update_actions()
00058
00059 def get_current_state(self):
00060 """!
00061 Get the state at the current index.
00062 @return the current state (nested data)
00063 """
00064 self.update_actions()
00065 return self.states[self.current_state_index]
00066
00067 def get_prev_state(self):
00068 """!
00069 Get the previous state and decrement the current index.
00070 @return the previous state or None
00071 """
00072 if self.num_prev_states > 0:
00073 self.current_state_index = (self.current_state_index + STATE_CACHE_SIZE -1)%STATE_CACHE_SIZE
00074 self.num_next_states = self.num_next_states + 1
00075 self.num_prev_states = self.num_prev_states - 1
00076 return self.get_current_state()
00077 return None
00078
00079 def get_next_state(self):
00080 """!
00081 Get the nest state and increment the current index.
00082 @return the next state or None
00083 """
00084 if self.num_next_states > 0:
00085 self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
00086 self.num_next_states = self.num_next_states - 1
00087 self.num_prev_states = self.num_prev_states + 1
00088 return self.get_current_state()
00089 return None
00090
00091 def update_actions(self):
00092 """
00093 Update the undo and redo actions based on the number of next and prev states.
00094 """
00095 get_action_from_name(FLOW_GRAPH_REDO).set_sensitive(self.num_next_states != 0)
00096 get_action_from_name(FLOW_GRAPH_UNDO).set_sensitive(self.num_prev_states != 0)
00097
00098