Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / NotebookPage.py
1 """
2 Copyright 2008, 2009 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_CLOSE
21 import pygtk
22 pygtk.require('2.0')
23 import gtk
24 from StateCache import StateCache
25 from Constants import MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT
26 from DrawingArea import DrawingArea
27 import os
28
29 ############################################################
30 ## Notebook Page
31 ############################################################
32
33 class NotebookPage(gtk.HBox):
34         """A page in the notebook."""
35
36         def __init__(self, main_window, flow_graph, file_path=''):
37                 """
38                 Page constructor.
39                 @param main_window main window
40                 @param file_path path to a flow graph file
41                 """
42                 self._flow_graph = flow_graph
43                 self.set_pid(None)
44                 #import the file
45                 self.main_window = main_window
46                 self.set_file_path(file_path)
47                 initial_state = flow_graph.get_parent().parse_flow_graph(file_path)
48                 self.state_cache = StateCache(initial_state)
49                 self.set_saved(True)
50                 #import the data to the flow graph
51                 self.get_flow_graph().import_data(initial_state)
52                 #initialize page gui
53                 gtk.HBox.__init__(self, False, 0)
54                 self.show()
55                 #tab box to hold label and close button
56                 self.tab = gtk.HBox(False, 0)
57                 #setup tab label
58                 self.label = gtk.Label()
59                 self.tab.pack_start(self.label, False)
60                 #setup button image
61                 image = gtk.Image()
62                 image.set_from_stock('gtk-close', gtk.ICON_SIZE_MENU)
63                 #setup image box
64                 image_box = gtk.HBox(False, 0)
65                 image_box.pack_start(image, True, False, 0)
66                 #setup the button
67                 button = gtk.Button()
68                 button.connect("clicked", self._handle_button)
69                 button.set_relief(gtk.RELIEF_NONE)
70                 button.add(image_box)
71                 #button size
72                 w, h = gtk.icon_size_lookup_for_settings(button.get_settings(), gtk.ICON_SIZE_MENU)
73                 button.set_size_request(w+6, h+6)
74                 self.tab.pack_start(button, False)
75                 self.tab.show_all()
76                 #setup scroll window and drawing area
77                 self.scrolled_window = gtk.ScrolledWindow()
78                 self.scrolled_window.set_size_request(MIN_WINDOW_WIDTH, MIN_WINDOW_HEIGHT)
79                 self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
80                 self.drawing_area = DrawingArea(self.get_flow_graph())
81                 self.scrolled_window.add_with_viewport(self.get_drawing_area())
82                 self.pack_start(self.scrolled_window)
83                 #inject drawing area and handle states into flow graph
84                 self.get_flow_graph().drawing_area = self.get_drawing_area()
85                 self.get_flow_graph().handle_states = main_window.handle_states
86                 self.show_all()
87
88         def get_drawing_area(self): return self.drawing_area
89
90         def get_generator(self):
91                 """
92                 Get the generator object for this flow graph.
93                 @return generator
94                 """
95                 return self.get_flow_graph().get_parent().get_generator()(
96                         self.get_flow_graph(),
97                         self.get_file_path(),
98                 )
99
100         def _handle_button(self, button):
101                 """
102                 The button was clicked.
103                 Make the current page selected, then close.
104                 @param the button
105                 """
106                 self.main_window.page_to_be_closed = self
107                 self.main_window.handle_states(FLOW_GRAPH_CLOSE)
108
109         def set_markup(self, markup):
110                 """
111                 Set the markup in this label.
112                 @param markup the new markup text
113                 """
114                 self.label.set_markup(markup)
115
116         def get_tab(self):
117                 """
118                 Get the gtk widget for this page's tab.
119                 @return gtk widget
120                 """
121                 return self.tab
122
123         def get_pid(self):
124                 """
125                 Get the pid for the flow graph.
126                 @return the pid number
127                 """
128                 return self.pid
129
130         def set_pid(self, pid):
131                 """
132                 Set the pid number.
133                 @param pid the new pid number
134                 """
135                 self.pid = pid
136
137         def get_flow_graph(self):
138                 """
139                 Get the flow graph.
140                 @return the flow graph
141                 """
142                 return self._flow_graph
143
144         def get_read_only(self):
145                 """
146                 Get the read-only state of the file.
147                 Always false for empty path.
148                 @return true for read-only
149                 """
150                 if not self.get_file_path(): return False
151                 return os.path.exists(self.get_file_path()) and \
152                 not os.access(self.get_file_path(), os.W_OK)
153
154         def get_file_path(self):
155                 """
156                 Get the file path for the flow graph.
157                 @return the file path or ''
158                 """
159                 return self.file_path
160
161         def set_file_path(self, file_path=''):
162                 """
163                 Set the file path, '' for no file path.
164                 @param file_path file path string
165                 """
166                 if file_path: self.file_path = os.path.abspath(file_path)
167                 else: self.file_path = ''
168
169         def get_saved(self):
170                 """
171                 Get the saved status for the flow graph.
172                 @return true if saved
173                 """
174                 return self.saved
175
176         def set_saved(self, saved=True):
177                 """
178                 Set the saved status.
179                 @param saved boolean status
180                 """
181                 self.saved = saved
182
183         def get_state_cache(self):
184                 """
185                 Get the state cache for the flow graph.
186                 @return the state cache
187                 """
188                 return self.state_cache