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