Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / 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
20 from Actions import FLOW_GRAPH_UNDO, FLOW_GRAPH_REDO, get_action_from_name
21 from Constants import STATE_CACHE_SIZE
22
23 class StateCache(object):
24         """
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.
28         """
29
30         def __init__(self, initial_state):
31                 """
32                 StateCache constructor.
33                 @param initial_state the intial state (nested data)
34                 """
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
40                 self.update_actions()
41
42         def save_new_state(self, state):
43                 """
44                 Save a new 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
47                 """
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
53                 self.update_actions()
54
55         def get_current_state(self):
56                 """
57                 Get the state at the current index.
58                 @return the current state (nested data)
59                 """
60                 self.update_actions()
61                 return self.states[self.current_state_index]
62
63         def get_prev_state(self):
64                 """
65                 Get the previous state and decrement the current index.
66                 @return the previous state or None
67                 """
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()
73                 return None
74
75         def get_next_state(self):
76                 """
77                 Get the nest state and increment the current index.
78                 @return the next state or None
79                 """
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()
85                 return None
86
87         def update_actions(self):
88                 """
89                 Update the undo and redo actions based on the number of next and prev states.
90                 """
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)