2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
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.
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.
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
20 from Constants import \
21 NEW_FLOGRAPH_TITLE, DEFAULT_REPORTS_WINDOW_WIDTH
27 from BlockTreeWindow import BlockTreeWindow
28 from Dialogs import TextDisplay, MessageDialogHelper
29 from NotebookPage import NotebookPage
35 MAIN_WINDOW_TITLE_TMPL = """\
42 $new_flowgraph_title#slurp
50 - $platform_name#slurp
53 PAGE_TITLE_MARKUP_TMPL = """\
54 #set $foreground = $saved and 'black' or 'red'
55 <span foreground="$foreground">$encode($title or $new_flowgraph_title)</span>#slurp
61 ############################################################
63 ############################################################
65 class MainWindow(gtk.Window):
66 """The topmost window with menus, the tool bar, and other major windows."""
68 def __init__(self, platform):
71 Setup the menu, toolbar, flowgraph editor notebook, block selection window...
73 self._platform = platform
75 gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
77 self.hpaned = gtk.HPaned()
79 #create the menu bar and toolbar
80 self.add_accel_group(Actions.get_accel_group())
81 vbox.pack_start(Bars.MenuBar(), False)
82 vbox.pack_start(Bars.Toolbar(), False)
83 vbox.pack_start(self.hpaned)
85 self.notebook = gtk.Notebook()
86 self.page_to_be_closed = None
87 self.current_page = None
88 self.notebook.set_show_border(False)
89 self.notebook.set_scrollable(True) #scroll arrows for page tabs
90 self.notebook.connect('switch-page', self._handle_page_change)
92 self.flow_graph_vpaned = gtk.VPaned()
93 #flow_graph_box.pack_start(self.scrolled_window)
94 self.flow_graph_vpaned.pack1(self.notebook)
95 self.hpaned.pack1(self.flow_graph_vpaned)
96 self.hpaned.pack2(BlockTreeWindow(platform, self.get_flow_graph), False) #dont allow resize
97 #create the reports window
98 self.text_display = TextDisplay()
99 #house the reports in a scrolled window
100 self.reports_scrolled_window = gtk.ScrolledWindow()
101 self.reports_scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
102 self.reports_scrolled_window.add_with_viewport(self.text_display)
103 self.reports_scrolled_window.set_size_request(-1, DEFAULT_REPORTS_WINDOW_WIDTH)
104 self.flow_graph_vpaned.pack2(self.reports_scrolled_window, False) #dont allow resize
105 #load preferences and show the main window
106 Preferences.load(platform)
107 self.resize(*Preferences.main_window_size())
108 self.flow_graph_vpaned.set_position(Preferences.reports_window_position())
109 self.hpaned.set_position(Preferences.blocks_window_position())
112 ############################################################
114 ############################################################
116 def _quit(self, window, event):
118 Handle the delete event from the main window.
119 Generated by pressing X to close, alt+f4, or right click+close.
120 This method in turns calls the state handler to quit.
123 Actions.APPLICATION_QUIT()
126 def _handle_page_change(self, notebook, page, page_num):
128 Handle a page change. When the user clicks on a new tab,
129 reload the flow graph to update the vars window and
130 call handle states (select nothing) to update the buttons.
131 @param notebook the notebook
133 @param page_num new page number
135 self.current_page = self.notebook.get_nth_page(page_num)
136 Messages.send_page_switch(self.current_page.get_file_path())
137 Actions.PAGE_CHANGE()
139 ############################################################
141 ############################################################
143 def add_report_line(self, line):
145 Place line at the end of the text buffer, then scroll its window all the way down.
146 @param line the new text
148 self.text_display.insert(line)
149 vadj = self.reports_scrolled_window.get_vadjustment()
150 vadj.set_value(vadj.upper)
153 ############################################################
154 # Pages: create and close
155 ############################################################
157 def new_page(self, file_path='', show=False):
159 Create a new notebook page.
160 Set the tab to be selected.
161 @param file_path optional file to load into the flow graph
162 @param show true if the page should be shown after loading
164 #if the file is already open, show the open page and return
165 if file_path and file_path in self._get_files(): #already open
166 page = self.notebook.get_nth_page(self._get_files().index(file_path))
169 try: #try to load from file
170 if file_path: Messages.send_start_load(file_path)
171 flow_graph = self._platform.get_new_flow_graph()
174 flow_graph=flow_graph,
177 if file_path: Messages.send_end_load()
178 except Exception, e: #return on failure
179 Messages.send_fail_load(e)
181 #add this page to the notebook
182 self.notebook.append_page(page, page.get_tab())
183 try: self.notebook.set_tab_reorderable(page, True)
184 except: pass #gtk too old
185 self.notebook.set_tab_label_packing(page, False, False, gtk.PACK_START)
186 #only show if blank or manual
187 if not file_path or show: self._set_page(page)
189 def close_pages(self):
191 Close all the pages in this notebook.
192 @return true if all closed
194 open_files = filter(lambda file: file, self._get_files()) #filter blank files
195 open_file = self.get_page().get_file_path()
197 for page in self._get_pages():
198 self.page_to_be_closed = page
199 self.close_page(False)
200 if self.notebook.get_n_pages(): return False
201 #save state before closing
202 Preferences.files_open(open_files)
203 Preferences.file_open(open_file)
204 Preferences.main_window_size(self.get_size())
205 Preferences.reports_window_position(self.flow_graph_vpaned.get_position())
206 Preferences.blocks_window_position(self.hpaned.get_position())
210 def close_page(self, ensure=True):
212 Close the current page.
213 If the notebook becomes empty, and ensure is true,
214 call new page upon exit to ensure that at least one page exists.
215 @param ensure boolean
217 if not self.page_to_be_closed: self.page_to_be_closed = self.get_page()
218 #show the page if it has an executing flow graph or is unsaved
219 if self.page_to_be_closed.get_pid() or not self.page_to_be_closed.get_saved():
220 self._set_page(self.page_to_be_closed)
221 #unsaved? ask the user
222 if not self.page_to_be_closed.get_saved() and self._save_changes():
223 Actions.FLOW_GRAPH_SAVE() #try to save
224 if not self.page_to_be_closed.get_saved(): #still unsaved?
225 self.page_to_be_closed = None #set the page to be closed back to None
227 #stop the flow graph if executing
228 if self.page_to_be_closed.get_pid(): Actions.FLOW_GRAPH_KILL()
230 self.notebook.remove_page(self.notebook.page_num(self.page_to_be_closed))
231 if ensure and self.notebook.get_n_pages() == 0: self.new_page() #no pages, make a new one
232 self.page_to_be_closed = None #set the page to be closed back to None
234 ############################################################
236 ############################################################
240 Set the title of the main window.
241 Set the titles on the page tabs.
242 Show/hide the reports window.
243 @param title the window title
245 gtk.Window.set_title(self, Utils.parse_template(MAIN_WINDOW_TITLE_TMPL,
246 basename=os.path.basename(self.get_page().get_file_path()),
247 dirname=os.path.dirname(self.get_page().get_file_path()),
248 new_flowgraph_title=NEW_FLOGRAPH_TITLE,
249 read_only=self.get_page().get_read_only(),
250 saved=self.get_page().get_saved(),
251 platform_name=self._platform.get_name(),
255 for page in self._get_pages(): page.set_markup(
256 Utils.parse_template(PAGE_TITLE_MARKUP_TMPL,
257 #get filename and strip out file extension
258 title=os.path.splitext(os.path.basename(page.get_file_path()))[0],
259 read_only=page.get_read_only(), saved=page.get_saved(),
260 new_flowgraph_title=NEW_FLOGRAPH_TITLE,
263 #show/hide notebook tabs
264 self.notebook.set_show_tabs(len(self._get_pages()) > 1)
268 Get the selected page.
269 @return the selected page
271 return self.current_page
273 def get_flow_graph(self):
275 Get the selected flow graph.
276 @return the selected flow graph
278 return self.get_page().get_flow_graph()
280 def get_focus_flag(self):
282 Get the focus flag from the current page.
283 @return the focus flag
285 return self.get_page().get_drawing_area().get_focus_flag()
287 ############################################################
289 ############################################################
291 def _set_page(self, page):
293 Set the current page.
294 @param page the page widget
296 self.current_page = page
297 self.notebook.set_current_page(self.notebook.page_num(self.current_page))
299 def _save_changes(self):
301 Save changes to flow graph?
304 return MessageDialogHelper(
305 gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 'Unsaved Changes!',
306 'Would you like to save changes before closing?'
307 ) == gtk.RESPONSE_YES
309 def _get_files(self):
311 Get the file names for all the pages, in order.
312 @return list of file paths
314 return map(lambda page: page.get_file_path(), self._get_pages())
316 def _get_pages(self):
318 Get a list of all pages in the notebook.
319 @return list of pages
321 return [self.notebook.get_nth_page(page_num) for page_num in range(self.notebook.get_n_pages())]