another distcheck fix
[debian/gnuradio] / grc / src / grc / StateCache.py
1 """
2 Copyright 2007 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19 ##@package StateCache
20 #Stores the flow graph states to drive the undo/redo and save interface.
21
22 from Actions import FLOW_GRAPH_UNDO, FLOW_GRAPH_REDO, get_action_from_name
23
24 from Constants import STATE_CACHE_SIZE
25
26 class StateCache(object):
27         """
28         The state cache is an interface to a list to record data/states and to revert to previous states.
29         States are recorded into the list in a circular fassion by using an index for the current state,
30         and counters for the range where states are stored.
31         """     
32         
33         def __init__(self, initial_state):
34                 """!
35                 StateCache constructor.
36                 @param initial_state the intial state (nested data)
37                 """
38                 self.states = [None] * STATE_CACHE_SIZE #fill states
39                 self.current_state_index = 0
40                 self.num_prev_states = 0
41                 self.num_next_states = 0
42                 self.states[0] = initial_state
43                 self.update_actions()
44                         
45         def save_new_state(self, state):
46                 """!
47                 Save a new state.
48                 Place the new state at the next index and add one to the number of previous states.
49                 @param state the new state
50                 """
51                 self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
52                 self.states[self.current_state_index] = state           
53                 self.num_prev_states = self.num_prev_states + 1
54                 if self.num_prev_states == STATE_CACHE_SIZE: self.num_prev_states = STATE_CACHE_SIZE - 1
55                 self.num_next_states = 0
56                 self.update_actions()
57                         
58         def get_current_state(self):
59                 """!
60                 Get the state at the current index.
61                 @return the current state (nested data)
62                 """
63                 self.update_actions()
64                 return self.states[self.current_state_index]            
65         
66         def get_prev_state(self):
67                 """!
68                 Get the previous state and decrement the current index.
69                 @return the previous state or None
70                 """
71                 if self.num_prev_states > 0:
72                         self.current_state_index = (self.current_state_index + STATE_CACHE_SIZE -1)%STATE_CACHE_SIZE
73                         self.num_next_states = self.num_next_states + 1
74                         self.num_prev_states = self.num_prev_states - 1
75                         return self.get_current_state()
76                 return None             
77         
78         def get_next_state(self):
79                 """!
80                 Get the nest state and increment the current index.
81                 @return the next state or None
82                 """
83                 if self.num_next_states > 0:
84                         self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
85                         self.num_next_states = self.num_next_states - 1
86                         self.num_prev_states = self.num_prev_states + 1
87                         return self.get_current_state()
88                 return None
89                 
90         def update_actions(self):
91                 """
92                 Update the undo and redo actions based on the number of next and prev states.
93                 """
94                 get_action_from_name(FLOW_GRAPH_REDO).set_sensitive(self.num_next_states != 0)
95                 get_action_from_name(FLOW_GRAPH_UNDO).set_sensitive(self.num_prev_states != 0)
96                 
97