2 Copyright 2007 Free Software Foundation, Inc.
3 This file is part of GNU Radio
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.
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.
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
20 from Actions import FLOW_GRAPH_UNDO, FLOW_GRAPH_REDO, get_action_from_name
21 from Constants import STATE_CACHE_SIZE
23 class StateCache(object):
25 The state cache is an interface to a list to record data/states and to revert to previous states.
26 States are recorded into the list in a circular fassion by using an index for the current state,
27 and counters for the range where states are stored.
30 def __init__(self, initial_state):
32 StateCache constructor.
33 @param initial_state the intial state (nested data)
35 self.states = [None] * STATE_CACHE_SIZE #fill states
36 self.current_state_index = 0
37 self.num_prev_states = 0
38 self.num_next_states = 0
39 self.states[0] = initial_state
42 def save_new_state(self, state):
45 Place the new state at the next index and add one to the number of previous states.
46 @param state the new state
48 self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
49 self.states[self.current_state_index] = state
50 self.num_prev_states = self.num_prev_states + 1
51 if self.num_prev_states == STATE_CACHE_SIZE: self.num_prev_states = STATE_CACHE_SIZE - 1
52 self.num_next_states = 0
55 def get_current_state(self):
57 Get the state at the current index.
58 @return the current state (nested data)
61 return self.states[self.current_state_index]
63 def get_prev_state(self):
65 Get the previous state and decrement the current index.
66 @return the previous state or None
68 if self.num_prev_states > 0:
69 self.current_state_index = (self.current_state_index + STATE_CACHE_SIZE -1)%STATE_CACHE_SIZE
70 self.num_next_states = self.num_next_states + 1
71 self.num_prev_states = self.num_prev_states - 1
72 return self.get_current_state()
75 def get_next_state(self):
77 Get the nest state and increment the current index.
78 @return the next state or None
80 if self.num_next_states > 0:
81 self.current_state_index = (self.current_state_index + 1)%STATE_CACHE_SIZE
82 self.num_next_states = self.num_next_states - 1
83 self.num_prev_states = self.num_prev_states + 1
84 return self.get_current_state()
87 def update_actions(self):
89 Update the undo and redo actions based on the number of next and prev states.
91 get_action_from_name(FLOW_GRAPH_REDO).set_sensitive(self.num_next_states != 0)
92 get_action_from_name(FLOW_GRAPH_UNDO).set_sensitive(self.num_prev_states != 0)