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