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 #Stores the flow graph states to drive the undo/redo and save interface.
23 from Actions import FLOW_GRAPH_UNDO, FLOW_GRAPH_REDO, get_action_from_name
25 from Constants import STATE_CACHE_SIZE
27 class StateCache(object):
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.
34 def __init__(self, initial_state):
36 StateCache constructor.
37 @param initial_state the intial state (nested data)
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
46 def save_new_state(self, 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
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
59 def get_current_state(self):
61 Get the state at the current index.
62 @return the current state (nested data)
65 return self.states[self.current_state_index]
67 def get_prev_state(self):
69 Get the previous state and decrement the current index.
70 @return the previous state or None
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()
79 def get_next_state(self):
81 Get the nest state and increment the current index.
82 @return the next state or None
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()
91 def update_actions(self):
93 Update the undo and redo actions based on the number of next and prev states.
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)