Merged changeset r9285:9377 from jblum/grc into trunk, with distcheck fixes
[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 #@author Josh Blum
22
23 from Actions import FLOW_GRAPH_UNDO, FLOW_GRAPH_REDO, get_action_from_name
24
25 from Constants import STATE_CACHE_SIZE
26
27 class StateCache(object):
28         """
29         The state cache is an interface to a list to record data/states and to revert to previous states.
30         States are recorded into the list in a circular fassion by using an index for the current state,
31         and counters for the range where states are stored.
32         """     
33         
34         def __init__(self, initial_state):
35                 """!
36                 StateCache constructor.
37                 @param initial_state the intial state (nested data)
38                 """
39                 self.states = [None] * STATE_CACHE_SIZE #fill states
40                 self.current_state_index = 0
41                 self.num_prev_states = 0
42                 self.num_next_states = 0
43                 self.states[0] = initial_state
44                 self.update_actions()
45                         
46         def save_new_state(self, state):
47                 """!
48                 Save a new state.
49                 Place the new state at the next index and add one to the number of previous states.
50                 @param state the new state
51                 """
52                 self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
53                 self.states[self.current_state_index] = state           
54                 self.num_prev_states = self.num_prev_states + 1
55                 if self.num_prev_states == STATE_CACHE_SIZE: self.num_prev_states = STATE_CACHE_SIZE - 1
56                 self.num_next_states = 0
57                 self.update_actions()
58                         
59         def get_current_state(self):
60                 """!
61                 Get the state at the current index.
62                 @return the current state (nested data)
63                 """
64                 self.update_actions()
65                 return self.states[self.current_state_index]            
66         
67         def get_prev_state(self):
68                 """!
69                 Get the previous state and decrement the current index.
70                 @return the previous state or None
71                 """
72                 if self.num_prev_states > 0:
73                         self.current_state_index = (self.current_state_index + STATE_CACHE_SIZE -1)%STATE_CACHE_SIZE
74                         self.num_next_states = self.num_next_states + 1
75                         self.num_prev_states = self.num_prev_states - 1
76                         return self.get_current_state()
77                 return None             
78         
79         def get_next_state(self):
80                 """!
81                 Get the nest state and increment the current index.
82                 @return the next state or None
83                 """
84                 if self.num_next_states > 0:
85                         self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
86                         self.num_next_states = self.num_next_states - 1
87                         self.num_prev_states = self.num_prev_states + 1
88                         return self.get_current_state()
89                 return None
90                 
91         def update_actions(self):
92                 """
93                 Update the undo and redo actions based on the number of next and prev states.
94                 """
95                 get_action_from_name(FLOW_GRAPH_REDO).set_sensitive(self.num_next_states != 0)
96                 get_action_from_name(FLOW_GRAPH_UNDO).set_sensitive(self.num_prev_states != 0)
97                 
98