Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / gui / NotebookPage.py
1 """
2 Copyright 2008 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 .. utils import ParseXML
25 from StateCache import StateCache
26 from .. platforms.base.Constants import FLOW_GRAPH_DTD
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                 file_path = file_path or flow_graph.get_parent().get_default_flow_graph()
48                 ############################################################
49                 from .. utils import converter
50                 converter.convert(file_path, flow_graph.get_parent())
51                 ############################################################
52                 ParseXML.validate_dtd(file_path, FLOW_GRAPH_DTD)
53                 initial_state = ParseXML.from_file(file_path)
54                 self.state_cache = StateCache(initial_state)
55                 self.set_saved(True)
56                 #import the data to the flow graph
57                 self.get_flow_graph().import_data(initial_state)
58                 self.get_flow_graph().update()
59                 #initialize page gui
60                 gtk.HBox.__init__(self, False, 0)
61                 self.show()
62                 #tab box to hold label and close button
63                 self.tab = gtk.HBox(False, 0)
64                 #setup tab label
65                 self.label = gtk.Label()
66                 self.tab.pack_start(self.label, False)
67                 #setup button image
68                 image = gtk.Image()
69                 image.set_from_stock('gtk-close', gtk.ICON_SIZE_MENU)
70                 #setup image box
71                 image_box = gtk.HBox(False, 0)
72                 image_box.pack_start(image, True, False, 0)
73                 #setup the button
74                 button = gtk.Button()
75                 button.connect("clicked", self._handle_button)
76                 button.set_relief(gtk.RELIEF_NONE)
77                 button.add(image_box)
78                 #button size
79                 w, h = gtk.icon_size_lookup_for_settings(button.get_settings(), gtk.ICON_SIZE_MENU)
80                 button.set_size_request(w+6, h+6)
81                 self.tab.pack_start(button, False)
82                 self.tab.show_all()
83
84         def get_generator(self):
85                 """
86                 Get the generator object for this flow graph.
87                 @return generator
88                 """
89                 return self.get_flow_graph().get_parent().get_generator()(
90                         self.get_flow_graph(),
91                         self.get_file_path(),
92                 )
93
94         def _handle_button(self, button):
95                 """
96                 The button was clicked.
97                 Make the current page selected, then close.
98                 @param the button
99                 """
100                 self.main_window.page_to_be_closed = self
101                 self.main_window.handle_states(FLOW_GRAPH_CLOSE)
102
103         def set_text(self, text):
104                 """
105                 Set the text in this label.
106                 @param text the new text
107                 """
108                 self.label.set_text(text)
109
110         def get_tab(self):
111                 """
112                 Get the gtk widget for this page's tab.
113                 @return gtk widget
114                 """
115                 return self.tab
116
117         def get_pid(self):
118                 """
119                 Get the pid for the flow graph.
120                 @return the pid number
121                 """
122                 return self.pid
123
124         def set_pid(self, pid):
125                 """
126                 Set the pid number.
127                 @param pid the new pid number
128                 """
129                 self.pid = pid
130
131         def get_flow_graph(self):
132                 """
133                 Get the flow graph.
134                 @return the flow graph
135                 """
136                 return self._flow_graph
137
138         def get_file_path(self):
139                 """
140                 Get the file path for the flow graph.
141                 @return the file path or ''
142                 """
143                 return self.file_path
144
145         def set_file_path(self, file_path=''):
146                 """
147                 Set the file path, '' for no file path.
148                 @param file_path file path string
149                 """
150                 if file_path: self.file_path = os.path.abspath(file_path)
151                 else: self.file_path = ''
152
153         def get_saved(self):
154                 """
155                 Get the saved status for the flow graph.
156                 @return true if saved
157                 """
158                 return self.saved
159
160         def set_saved(self, saved=True):
161                 """
162                 Set the saved status.
163                 @param saved boolean status
164                 """
165                 self.saved = saved
166
167         def get_state_cache(self):
168                 """
169                 Get the state cache for the flow graph.
170                 @return the state cache
171                 """
172                 return self.state_cache