2 Copyright 2007 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
19 ##@package grc.gui.FileDialogs
20 #The open/save dialog for flow graph fFileDialogiles and screen shots.
26 from Dialogs import MessageDialogHelper
27 from grc.Constants import DEFAULT_FILE_PATH,FLOW_GRAPH_FILE_EXTENSION,IMAGE_FILE_EXTENSION,NEW_FLOGRAPH_TITLE
30 OPEN_FLOW_GRAPH = 'open flow graph'
31 SAVE_FLOW_GRAPH = 'save flow graph'
32 SAVE_IMAGE = 'save image'
34 ##the filter for flow graph files
35 FLOW_GRAPH_FILE_FILTER = gtk.FileFilter()
36 FLOW_GRAPH_FILE_FILTER.set_name('GRC Files')
37 FLOW_GRAPH_FILE_FILTER.add_pattern('*'+FLOW_GRAPH_FILE_EXTENSION)
38 FLOW_GRAPH_FILE_FILTER.add_pattern('*.xml') #TEMP
40 ##the filter for image files
41 IMAGE_FILE_FILTER = gtk.FileFilter()
42 IMAGE_FILE_FILTER.set_name('Image Files')
43 IMAGE_FILE_FILTER.add_pattern('*'+IMAGE_FILE_EXTENSION)
45 ##the filter for all files
46 ALL_FILE_FILTER = gtk.FileFilter()
47 ALL_FILE_FILTER.set_name('All Files')
48 ALL_FILE_FILTER.add_pattern('*')
50 class FileDialogHelper(gtk.FileChooserDialog):
52 A wrapper class for the gtk file chooser dialog.
53 Implement a file chooser dialog with only necessary parameters.
56 def __init__(self, action, title):
58 FileDialogHelper contructor.
59 Create a save or open dialog with cancel and ok buttons.
60 Use standard settings: no multiple selection, local files only, and the * filter.
61 @param action gtk.FILE_CHOOSER_ACTION_OPEN or gtk.FILE_CHOOSER_ACTION_SAVE
62 @param title the title of the dialog (string)
64 ok_stock = {gtk.FILE_CHOOSER_ACTION_OPEN : 'gtk-open', gtk.FILE_CHOOSER_ACTION_SAVE : 'gtk-save'}[action]
65 gtk.FileChooserDialog.__init__(self, title, None, action, ('gtk-cancel', gtk.RESPONSE_CANCEL, ok_stock, gtk.RESPONSE_OK))
66 self.set_select_multiple(False)
67 self.set_local_only(True)
68 self.add_filter(ALL_FILE_FILTER)
70 class FileDialog(FileDialogHelper):
71 """A dialog box to save or open flow graph files. This is a base class, do not use."""
73 def __init__(self, current_file_path=''):
75 FileDialog constructor.
76 @param current_file_path the current directory or path to the open flow graph
78 if not current_file_path: current_file_path = path.join(DEFAULT_FILE_PATH, NEW_FLOGRAPH_TITLE + FLOW_GRAPH_FILE_EXTENSION)
79 if self.type == OPEN_FLOW_GRAPH:
80 FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, 'Open a Flow Graph from a File...')
81 self.add_and_set_filter(FLOW_GRAPH_FILE_FILTER)
82 self.set_select_multiple(True)
83 elif self.type == SAVE_FLOW_GRAPH:
84 FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph to a File...')
85 self.add_and_set_filter(FLOW_GRAPH_FILE_FILTER)
86 self.set_current_name(path.basename(current_file_path)) #show the current filename
87 elif self.type == SAVE_IMAGE:
88 FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph Screen Shot...')
89 self.add_and_set_filter(IMAGE_FILE_FILTER)
90 current_file_path = current_file_path + IMAGE_FILE_EXTENSION
91 self.set_current_name(path.basename(current_file_path)) #show the current filename
92 self.set_current_folder(path.dirname(current_file_path)) #current directory
94 def add_and_set_filter(self, filter):
96 Add the gtk file filter to the list of filters and set it as the default file filter.
97 @param filter a gtk file filter.
99 self.add_filter(filter)
100 self.set_filter(filter)
102 def get_rectified_filename(self):
104 Run the dialog and get the filename.
105 If this is a save dialog and the file name is missing the extension, append the file extension.
106 If the file name with the extension already exists, show a overwrite dialog.
107 If this is an open dialog, return a list of filenames.
108 @return the complete file path
110 if gtk.FileChooserDialog.run(self) != gtk.RESPONSE_OK: return None #response was cancel
111 #############################################
112 # Handle Save Dialogs
113 #############################################
114 if self.type in (SAVE_FLOW_GRAPH, SAVE_IMAGE):
115 filename = self.get_filename()
116 for extension, filter in (
117 (FLOW_GRAPH_FILE_EXTENSION, FLOW_GRAPH_FILE_FILTER),
118 (IMAGE_FILE_EXTENSION, IMAGE_FILE_FILTER),
119 ): #append the missing file extension if the filter matches
120 if filename[len(filename)-len(extension):] != extension \
121 and filter == self.get_filter(): filename += extension
122 self.set_current_name(path.basename(filename)) #show the filename with extension
123 if path.exists(filename): #ask the user to confirm overwrite
124 if MessageDialogHelper(
125 gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 'Confirm Overwrite!',
126 'File <b>"%s"</b> Exists!\nWould you like to overwrite the existing file?'%filename,
127 ) == gtk.RESPONSE_NO: return self.get_rectified_filename()
129 #############################################
130 # Handle Open Dialogs
131 #############################################
132 elif self.type in (OPEN_FLOW_GRAPH,):
133 filenames = self.get_filenames()
134 for filename in filenames:
135 if not path.exists(filename): #show a warning and re-run
137 gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, 'Cannot Open!',
138 'File <b>"%s"</b> Does not Exist!'%filename,
140 return self.get_rectified_filename()
145 Get the filename and destroy the dialog.
146 @return the filename or None if a close/cancel occured.
148 filename = self.get_rectified_filename()
152 class OpenFlowGraphFileDialog(FileDialog): type = OPEN_FLOW_GRAPH
153 class SaveFlowGraphFileDialog(FileDialog): type = SAVE_FLOW_GRAPH
154 class SaveImageFileDialog(FileDialog): type = SAVE_IMAGE