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
23 from Dialogs import MessageDialogHelper
24 from Constants import \
25 DEFAULT_FILE_PATH, IMAGE_FILE_EXTENSION, \
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 def get_flow_graph_files_filter():
36 filter = gtk.FileFilter()
37 filter.set_name('Flow Graph Files')
38 filter.add_pattern('*'+Preferences.file_extension())
39 filter.add_pattern('*.xml') #TEMP
42 ##the filter for image files
43 def get_image_files_filter():
44 filter = gtk.FileFilter()
45 filter.set_name('Image Files')
46 filter.add_pattern('*'+IMAGE_FILE_EXTENSION)
49 ##the filter for all files
50 def get_all_files_filter():
51 filter = gtk.FileFilter()
52 filter.set_name('All Files')
53 filter.add_pattern('*')
56 class FileDialogHelper(gtk.FileChooserDialog):
58 A wrapper class for the gtk file chooser dialog.
59 Implement a file chooser dialog with only necessary parameters.
62 def __init__(self, action, title):
64 FileDialogHelper contructor.
65 Create a save or open dialog with cancel and ok buttons.
66 Use standard settings: no multiple selection, local files only, and the * filter.
67 @param action gtk.FILE_CHOOSER_ACTION_OPEN or gtk.FILE_CHOOSER_ACTION_SAVE
68 @param title the title of the dialog (string)
70 ok_stock = {gtk.FILE_CHOOSER_ACTION_OPEN : 'gtk-open', gtk.FILE_CHOOSER_ACTION_SAVE : 'gtk-save'}[action]
71 gtk.FileChooserDialog.__init__(self, title, None, action, ('gtk-cancel', gtk.RESPONSE_CANCEL, ok_stock, gtk.RESPONSE_OK))
72 self.set_select_multiple(False)
73 self.set_local_only(True)
74 self.add_filter(get_all_files_filter())
76 class FileDialog(FileDialogHelper):
77 """A dialog box to save or open flow graph files. This is a base class, do not use."""
79 def __init__(self, current_file_path=''):
81 FileDialog constructor.
82 @param current_file_path the current directory or path to the open flow graph
84 if not current_file_path: current_file_path = path.join(DEFAULT_FILE_PATH, NEW_FLOGRAPH_TITLE + Preferences.file_extension())
85 if self.type == OPEN_FLOW_GRAPH:
86 FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, 'Open a Flow Graph from a File...')
87 self.add_and_set_filter(get_flow_graph_files_filter())
88 self.set_select_multiple(True)
89 elif self.type == SAVE_FLOW_GRAPH:
90 FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph to a File...')
91 self.add_and_set_filter(get_flow_graph_files_filter())
92 self.set_current_name(path.basename(current_file_path)) #show the current filename
93 elif self.type == SAVE_IMAGE:
94 FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph Screen Shot...')
95 self.add_and_set_filter(get_image_files_filter())
96 current_file_path = current_file_path + IMAGE_FILE_EXTENSION
97 self.set_current_name(path.basename(current_file_path)) #show the current filename
98 self.set_current_folder(path.dirname(current_file_path)) #current directory
100 def add_and_set_filter(self, filter):
102 Add the gtk file filter to the list of filters and set it as the default file filter.
103 @param filter a gtk file filter.
105 self.add_filter(filter)
106 self.set_filter(filter)
108 def get_rectified_filename(self):
110 Run the dialog and get the filename.
111 If this is a save dialog and the file name is missing the extension, append the file extension.
112 If the file name with the extension already exists, show a overwrite dialog.
113 If this is an open dialog, return a list of filenames.
114 @return the complete file path
116 if gtk.FileChooserDialog.run(self) != gtk.RESPONSE_OK: return None #response was cancel
117 #############################################
118 # Handle Save Dialogs
119 #############################################
120 if self.type in (SAVE_FLOW_GRAPH, SAVE_IMAGE):
121 filename = self.get_filename()
123 SAVE_FLOW_GRAPH: Preferences.file_extension(),
124 SAVE_IMAGE: IMAGE_FILE_EXTENSION,
126 #append the missing file extension if the filter matches
127 if path.splitext(filename)[1].lower() != extension: filename += extension
128 self.set_current_name(path.basename(filename)) #show the filename with extension
129 if path.exists(filename): #ask the user to confirm overwrite
130 if MessageDialogHelper(
131 gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 'Confirm Overwrite!',
132 'File <b>"%s"</b> Exists!\nWould you like to overwrite the existing file?'%filename,
133 ) == gtk.RESPONSE_NO: return self.get_rectified_filename()
135 #############################################
136 # Handle Open Dialogs
137 #############################################
138 elif self.type in (OPEN_FLOW_GRAPH,):
139 filenames = self.get_filenames()
140 for filename in filenames:
141 if not path.exists(filename): #show a warning and re-run
143 gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, 'Cannot Open!',
144 'File <b>"%s"</b> Does not Exist!'%filename,
146 return self.get_rectified_filename()
151 Get the filename and destroy the dialog.
152 @return the filename or None if a close/cancel occured.
154 filename = self.get_rectified_filename()
158 class OpenFlowGraphFileDialog(FileDialog): type = OPEN_FLOW_GRAPH
159 class SaveFlowGraphFileDialog(FileDialog): type = SAVE_FLOW_GRAPH
160 class SaveImageFileDialog(FileDialog): type = SAVE_IMAGE