diff options
Diffstat (limited to 'grc/gui/FileDialogs.py')
-rw-r--r-- | grc/gui/FileDialogs.py | 132 |
1 files changed, 69 insertions, 63 deletions
diff --git a/grc/gui/FileDialogs.py b/grc/gui/FileDialogs.py index 9d047b1ffd..afd41af58c 100644 --- a/grc/gui/FileDialogs.py +++ b/grc/gui/FileDialogs.py @@ -17,16 +17,15 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import pygtk -pygtk.require('2.0') -import gtk -from Dialogs import MessageDialogHelper -from Constants import \ - DEFAULT_FILE_PATH, IMAGE_FILE_EXTENSION, TEXT_FILE_EXTENSION, \ - NEW_FLOGRAPH_TITLE -import Preferences +from __future__ import absolute_import + from os import path -import Utils + +from gi.repository import Gtk + +from . import Constants, Preferences, Utils +from .Dialogs import MessageDialogWrapper + ################################################## # Constants @@ -37,69 +36,70 @@ SAVE_CONSOLE = 'save console' SAVE_IMAGE = 'save image' OPEN_QSS_THEME = 'open qss theme' -FILE_OVERWRITE_MARKUP_TMPL="""\ -File <b>$encode($filename)</b> Exists!\nWould you like to overwrite the existing file?""" - -FILE_DNE_MARKUP_TMPL="""\ -File <b>$encode($filename)</b> Does not Exist!""" - - # File Filters def get_flow_graph_files_filter(): - filter = gtk.FileFilter() + filter = Gtk.FileFilter() filter.set_name('Flow Graph Files') filter.add_pattern('*'+Preferences.file_extension()) return filter def get_text_files_filter(): - filter = gtk.FileFilter() + filter = Gtk.FileFilter() filter.set_name('Text Files') - filter.add_pattern('*'+TEXT_FILE_EXTENSION) + filter.add_pattern('*' + Constants.TEXT_FILE_EXTENSION) return filter def get_image_files_filter(): - filter = gtk.FileFilter() + filter = Gtk.FileFilter() filter.set_name('Image Files') - filter.add_pattern('*'+IMAGE_FILE_EXTENSION) + filter.add_pattern('*' + Constants.IMAGE_FILE_EXTENSION) return filter def get_all_files_filter(): - filter = gtk.FileFilter() + filter = Gtk.FileFilter() filter.set_name('All Files') filter.add_pattern('*') return filter def get_qss_themes_filter(): - filter = gtk.FileFilter() + filter = Gtk.FileFilter() filter.set_name('QSS Themes') filter.add_pattern('*.qss') return filter # File Dialogs -class FileDialogHelper(gtk.FileChooserDialog): +class FileDialogHelper(Gtk.FileChooserDialog): """ A wrapper class for the gtk file chooser dialog. Implement a file chooser dialog with only necessary parameters. """ - def __init__(self, action, title): + def __init__(self, parent, action, title): """ FileDialogHelper contructor. Create a save or open dialog with cancel and ok buttons. Use standard settings: no multiple selection, local files only, and the * filter. Args: - action: gtk.FILE_CHOOSER_ACTION_OPEN or gtk.FILE_CHOOSER_ACTION_SAVE + action: Gtk.FileChooserAction.OPEN or Gtk.FileChooserAction.SAVE title: the title of the dialog (string) """ - ok_stock = {gtk.FILE_CHOOSER_ACTION_OPEN : 'gtk-open', gtk.FILE_CHOOSER_ACTION_SAVE : 'gtk-save'}[action] - gtk.FileChooserDialog.__init__(self, title, None, action, ('gtk-cancel', gtk.RESPONSE_CANCEL, ok_stock, gtk.RESPONSE_OK)) + ok_stock = { + Gtk.FileChooserAction.OPEN: 'gtk-open', + Gtk.FileChooserAction.SAVE: 'gtk-save' + }[action] + + Gtk.FileChooserDialog.__init__( + self, title=title, action=action, + transient_for=parent + ) + self.add_buttons('gtk-cancel', Gtk.ResponseType.CANCEL, ok_stock, Gtk.ResponseType.OK) self.set_select_multiple(False) self.set_local_only(True) self.add_filter(get_all_files_filter()) @@ -108,37 +108,39 @@ class FileDialogHelper(gtk.FileChooserDialog): class FileDialog(FileDialogHelper): """A dialog box to save or open flow graph files. This is a base class, do not use.""" - def __init__(self, current_file_path=''): + def __init__(self, parent, current_file_path=''): """ FileDialog constructor. Args: current_file_path: the current directory or path to the open flow graph """ - if not current_file_path: current_file_path = path.join(DEFAULT_FILE_PATH, NEW_FLOGRAPH_TITLE + Preferences.file_extension()) + if not current_file_path: + current_file_path = path.join(Constants.DEFAULT_FILE_PATH, + Constants.NEW_FLOGRAPH_TITLE + Preferences.file_extension()) if self.type == OPEN_FLOW_GRAPH: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, 'Open a Flow Graph from a File...') + FileDialogHelper.__init__(self, parent, Gtk.FileChooserAction.OPEN, 'Open a Flow Graph from a File...') self.add_and_set_filter(get_flow_graph_files_filter()) self.set_select_multiple(True) elif self.type == SAVE_FLOW_GRAPH: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph to a File...') + FileDialogHelper.__init__(self, parent, Gtk.FileChooserAction.SAVE, 'Save a Flow Graph to a File...') self.add_and_set_filter(get_flow_graph_files_filter()) self.set_current_name(path.basename(current_file_path)) elif self.type == SAVE_CONSOLE: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save Console to a File...') + FileDialogHelper.__init__(self, parent, Gtk.FileChooserAction.SAVE, 'Save Console to a File...') self.add_and_set_filter(get_text_files_filter()) file_path = path.splitext(path.basename(current_file_path))[0] - self.set_current_name(file_path) #show the current filename + self.set_current_name(file_path) # show the current filename elif self.type == SAVE_IMAGE: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_SAVE, 'Save a Flow Graph Screen Shot...') + FileDialogHelper.__init__(self, parent, Gtk.FileChooserAction.SAVE, 'Save a Flow Graph Screen Shot...') self.add_and_set_filter(get_image_files_filter()) - current_file_path = current_file_path + IMAGE_FILE_EXTENSION - self.set_current_name(path.basename(current_file_path)) #show the current filename + current_file_path = current_file_path + Constants.IMAGE_FILE_EXTENSION + self.set_current_name(path.basename(current_file_path)) # show the current filename elif self.type == OPEN_QSS_THEME: - FileDialogHelper.__init__(self, gtk.FILE_CHOOSER_ACTION_OPEN, 'Open a QSS theme...') + FileDialogHelper.__init__(self, parent, Gtk.FileChooserAction.OPEN, 'Open a QSS theme...') self.add_and_set_filter(get_qss_themes_filter()) self.set_select_multiple(False) - self.set_current_folder(path.dirname(current_file_path)) #current directory + self.set_current_folder(path.dirname(current_file_path)) # current directory def add_and_set_filter(self, filter): """ @@ -160,7 +162,7 @@ class FileDialog(FileDialogHelper): Returns: the complete file path """ - if gtk.FileChooserDialog.run(self) != gtk.RESPONSE_OK: return None #response was cancel + if Gtk.FileChooserDialog.run(self) != Gtk.ResponseType.OK: return None # response was cancel ############################################# # Handle Save Dialogs ############################################# @@ -168,17 +170,21 @@ class FileDialog(FileDialogHelper): filename = self.get_filename() extension = { SAVE_FLOW_GRAPH: Preferences.file_extension(), - SAVE_CONSOLE: TEXT_FILE_EXTENSION, - SAVE_IMAGE: IMAGE_FILE_EXTENSION, + SAVE_CONSOLE: Constants.TEXT_FILE_EXTENSION, + SAVE_IMAGE: Constants.IMAGE_FILE_EXTENSION, }[self.type] - #append the missing file extension if the filter matches - if path.splitext(filename)[1].lower() != extension: filename += extension - self.set_current_name(path.basename(filename)) #show the filename with extension - if path.exists(filename): #ask the user to confirm overwrite - if MessageDialogHelper( - gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 'Confirm Overwrite!', - Utils.parse_template(FILE_OVERWRITE_MARKUP_TMPL, filename=filename), - ) == gtk.RESPONSE_NO: return self.get_rectified_filename() + # append the missing file extension if the filter matches + if path.splitext(filename)[1].lower() != extension: + filename += extension + self.set_current_name(path.basename(filename)) # show the filename with extension + if path.exists(filename): # ask the user to confirm overwrite + response = MessageDialogWrapper( + Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, 'Confirm Overwrite!', + 'File <b>{filename}</b> Exists!\nWould you like to overwrite the existing file?' + ''.format(filename=Utils.encode(filename)), + ).run_and_destroy() + if response == Gtk.ResponseType.NO: + return self.get_rectified_filename() return filename ############################################# # Handle Open Dialogs @@ -186,11 +192,11 @@ class FileDialog(FileDialogHelper): elif self.type in (OPEN_FLOW_GRAPH, OPEN_QSS_THEME): filenames = self.get_filenames() for filename in filenames: - if not path.exists(filename): #show a warning and re-run - MessageDialogHelper( - gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, 'Cannot Open!', - Utils.parse_template(FILE_DNE_MARKUP_TMPL, filename=filename), - ) + if not path.exists(filename): # show a warning and re-run + MessageDialogWrapper( + Gtk.MessageType.WARNING, Gtk.ButtonsType.CLOSE, 'Cannot Open!', + 'File <b>{filename}</b> Does not Exist!'.format(filename=Utils.encode(filename)), + ).run_and_destroy() return self.get_rectified_filename() return filenames @@ -206,19 +212,19 @@ class FileDialog(FileDialogHelper): return filename -class OpenFlowGraphFileDialog(FileDialog): +class OpenFlowGraph(FileDialog): type = OPEN_FLOW_GRAPH -class SaveFlowGraphFileDialog(FileDialog): +class SaveFlowGraph(FileDialog): type = SAVE_FLOW_GRAPH -class OpenQSSFileDialog(FileDialog): +class OpenQSS(FileDialog): type = OPEN_QSS_THEME -class SaveConsoleFileDialog(FileDialog): +class SaveConsole(FileDialog): type = SAVE_CONSOLE @@ -226,11 +232,11 @@ class SaveImageFileDialog(FileDialog): type = SAVE_IMAGE -class SaveScreenShotDialog(SaveImageFileDialog): +class SaveScreenShot(SaveImageFileDialog): - def __init__(self, current_file_path=''): - SaveImageFileDialog.__init__(self, current_file_path) - self._button = button = gtk.CheckButton('_Background transparent') + def __init__(self, parent, current_file_path=''): + SaveImageFileDialog.__init__(self, parent, current_file_path) + self._button = button = Gtk.CheckButton(label='Background transparent') self._button.set_active(Preferences.screen_shot_background_transparent()) self.set_extra_widget(button) |