def _prompt_for_save(self): """ Prompts the user to save if necessary. Returns whether the dialog was cancelled. """ dirty_editors = { editor.name: editor for editor in self.editor_area.editors if editor.dirty and (editor.obj or editor.code) } if not dirty_editors: return True message = 'You have unsaved files. Would you like to save them?' dialog = ConfirmationDialog(parent=self.window.control, message=message, cancel=True, default=CANCEL, title='Save Changes?') result = dialog.open() if result == CANCEL: return False elif result == YES: for name, editor in dirty_editors.items(): editor.save(editor.path) return True
def on_problem(self): log = str(self._get_package_versions()) + "\n" + self.application.application_log.getvalue() msg = "The best way to report a problem is send an application log to " \ "the developers. If you click 'Yes' below, you will be given then " \ "opportunity to save the log to a file and then file a " \ "new issue on GitHub at " \ "https://github.com/cytoflow/cytoflow/issues/new" dialog = ConfirmationDialog(message = msg, informative = "Would you like to report an issue to the developers?") if dialog.open() == YES: dialog = DefaultFileDialog(parent = self.window.control, action = 'save as', default_suffix = "log", wildcard = (FileDialog.create_wildcard("Log files", "*.log") + ';' + #@UndefinedVariable FileDialog.create_wildcard("All files", "*"))) #@UndefinedVariable if dialog.open() == OK: with open(dialog.path, 'w') as f: f.write(log) webbrowser.open_new_tab("https://github.com/cytoflow/cytoflow/issues/new") return
def is_current_project_saved(self, parent_window): """ Give the user the option to save any modifications to the current project prior to closing it. If the user wanted to cancel the closing of the current project, this method returns False. Otherwise, it returns True. """ # The default is the user okay'd the closing of the project result = True # If the current project is dirty, handle that now by challenging the # user for how they want to handle them. current = self.model_service.project if not (self._get_project_state(current)): dialog = ConfirmationDialog( parent=parent_window, cancel=True, title="Unsaved Changes", message='Do you want to save the changes to project "%s"?' % (current.name), ) action = dialog.open() if action == CANCEL: result = False elif action == YES: result = self._save(current, parent_window) elif action == NO: # Delete the autosaved file as the user does not wish to # retain the unsaved changes. self._clean_autosave_location(current.location.strip()) return result
def close(self): """ Closes the window. It first prompts the user to save the current Workflow. The user can either save, quit without saving, or cancel. If the user wants to save but the save fails, the application is not closed so data is not lost. Overrides close from `pyface.tasks.task_window` """ # The attached wfmanager_setup_task for saving methods setup_task = None for window in self.application.windows: for task in window.tasks: if task.id == "force_wfmanager.wfmanager_setup_task": setup_task = task # If we don't have a setup task for some reason, just close if setup_task is None: return True # Pop up for user input dialog = ConfirmationDialog( parent=None, message='Do you want to save before exiting the Workflow ' 'Manager ?', cancel=True, yes_label='Save', no_label='Don\'t save', ) result = dialog.open() # Save if result is YES: save_result = setup_task.save_workflow() # On a failed save, don't close the window if not save_result: return False close_result = super(TaskWindowClosePrompt, self).close() return close_result # Don't save elif result is NO: close_result = super(TaskWindowClosePrompt, self).close() return close_result # Cancel elif result is CANCEL: return False
def _prompt_for_save(self): """ Check if the user wants to save changes """ from pyface.api import ConfirmationDialog, CANCEL, YES if not self.command_stack.clean: message = 'The current survey has unsaved changes. ' \ 'Do you want to save your changes?' dialog = ConfirmationDialog(parent=self.window.control, message=message, cancel=True, default=CANCEL, title='Save Changes?') result = dialog.open() if result == CANCEL: return False elif result == YES: if not self._save(): return self._prompt_for_save() return True
def _prompt_for_save(self): """ Prompts the user to save if necessary. Returns whether the dialog was cancelled. """ if self.window.central_pane.editor.dirty: message = 'The current file has unsaved changes. ' \ 'Do you want to save your changes?' dialog = ConfirmationDialog(parent=self.window.control, message=message, cancel=True, default=CANCEL, title='Save Changes?') result = dialog.open() if result == CANCEL: return False elif result == YES: if not self.save(): return self._prompt_for_save() return True
def on_problem(self): log = str(self._get_package_versions()) + "\n" + self.application.application_log.getvalue() msg = "The best way to report a problem is send an application log to " \ "the developers. You can do so by either sending us an email " \ "with the log in it, or saving the log to a file and filing a " \ "new issue on GitHub at " \ "https://github.com/bpteague/cytoflow/issues/new" dialog = ConfirmationDialog(message = msg, informative = "Which would you like to do?", yes_label = "Send an email...", no_label = "Save to a file...") if dialog.open() == NO: dialog = DefaultFileDialog(parent = self.window.control, action = 'save as', default_suffix = "log", wildcard = (FileDialog.create_wildcard("Log files", "*.log") + ';' + #@UndefinedVariable FileDialog.create_wildcard("All files", "*"))) #@UndefinedVariable if dialog.open() == OK: with open(dialog.path, 'w') as f: f.write(log) webbrowser.open_new_tab("https://github.com/bpteague/cytoflow/issues/new") return information(None, "I'll now try to open your email client and create a " "new message to the developer. Debugging logs are " "attached. Please fill out the template bug report and " "send -- thank you for reporting a bug!") log = self.application.application_log.getvalue() versions = ["{0} {1}".format(key, value) for key, value in self._get_package_versions().items()] body = """ Thank you for your bug report! Please fill out the following template. PLATFORM (Mac, PC, Linux, other): OPERATING SYSTEM (eg OSX 10.7, Windows 8.1): SEVERITY (Critical? Major? Minor? Enhancement?): DESCRIPTION: - What were you trying to do? - What happened? - What did you expect to happen? PACKAGE VERSIONS: {0} DEBUG LOG: {1} """.format(versions, log) mailto("*****@*****.**", subject = "Cytoflow bug report", body = body)
def save_data_file(sourceFile, destination=None, subdirectory=None, user=None, verbose=True): """ Function used to save (i.e copy) a data file into a directory of choice after an experimental session Parameters: sourceFile - the path of the file that was generated by the experimental session and that resides in the local file system. destination - An optional destination path where to save the file. File name may be included or not at the end of the path. subdirectory - An optional subdirectory, i.e folder, to add to the destination path. For example, if the destination path is a folder called "experiments", the subdirectory can be a child folder of "experiments", named after the experiment type ("behaviour" for instance). user - An optional parameter to indicate which user is conducting the experiments. If supplied, and if no destination is passed, a configuration file is looked up to retrieve the folder into which the user is usually copying data files. If no destination and no user is provided, a default directory is looked up in the configuration file as the default destination of the file to be copied. Either way, a save as dialog box will appear and the user will have final say. """ # Validate file parameter passed. Also check to see if the path provided is lacking the default .h5 extension if not os.path.exists(sourceFile): if not os.path.exists(sourceFile + ".h5"): # Error message if the source file path could not be found in the system error(None,"Woah there!\n\n1. Couldn't find the file that you want to copy.\ \n2. Check to see if it exists in the file system and the path provided is correct"\ , "File Finding Police report") return else: # File exists but has an extension and one was not provided in the path given. # Add it to file path descriptor sourceFile += ".h5" # information(None, "the filename of source provided lacked the \".h5\" extension.\ # \n\nA file with the extension was found and presumed to be the source meant"\ # ,"Path Police report") # Get file extension fileExtension = os.path.splitext(sourceFile)[-1] # Get the destination file name from the path provided destinationFile = os.path.split(sourceFile)[-1] destinationFolder = "" # If file has no extension, add the default .h5 extension to destination file name if fileExtension == "": warning(None, "The file you are trying to save has no extension\n\nAdding \".h5\" to the name of destination file"\ , ".h5 Extension Police") destinationFile = file + ".h5" # The file provided has different extension. Display a warning but do nothing. elif fileExtension != ".h5": warning(None, "Your file to be copied does not have an \".h5\" extension\n\nNo action taken."\ , "h5 Extension Police") # Display confirmation dialog for copying the file dlg = ConfirmationDialog( title="You there!", yes_label="Yes Please!", no_label="Nah...", message= "Would you like to copy the data file generated after the session?\ \n\nIf you say Nah... and change your mind, you'll have to copy it manually later" ) # Open the dialog GUI dlg.open() # User provided a destination path if destination: # Check to see if destination is a file name with an extension. destinationExtension = os.path.splitext(destination)[-1] if destinationExtension: # Is it .h5? If not, warn but don't override. if destinationExtension != ".h5": warning(None, "Your destination filename does not have an \".h5\" extension\n\nNo action taken."\ , "h5 Extension Police") destinationFolder, destinationFile = os.path.split(destination) # Assume destination is directory since there is no extension. else: destinationFolder = destination # Look up a default destination from the config file since no <destination> parameter was provided. else: configFile = os.environ.get("Voyeur_config") config = ConfigObj(configFile) # A user specific folder was provided. if user: destinationFolder = config['server']['folder']['data']['user'] # Use default data folder as read from the config file. else: destinationFolder = config['server']['folder']['data']['default'] # User provided a subdirectory, i.e subfolder, into which to place the file. if subdirectory: # The subdirectory provided has common path with the directory provided. Display warning but do nothing. if os.path.commonprefix((destination, subdirectory)): warning(None, "Friendly warning!\n<subdirectory> parameter provided has a common path with the <destination>\ path parameter\n\n1. No action taken.\n2. Check your final destination path to make sure it is what you want"\ , "Path Police report") destinationFolder = os.path.join(destinationFolder, subdirectory) # Path of the destination of file to be copied. destinationPath = os.path.join(destinationFolder, destinationFile) if dlg.return_code == YES: # A file with same name exists. if os.path.isfile(destinationPath): warning( None, "A file with given path already exists!\n\n1. No action taken\ \n2. Make sure to either rename file or choose different folder", "Path Police report") # Provided folder does not exist. Make one and inform the user. elif not os.path.isdir(destinationFolder): information(None, "Making a new folder to put the file into...", "Information Transparency report") # TODO: What if this results in an exception? Catch and do something? # TODO: Keep track of made directories so we may delete them later os.makedirs(os.path.abspath(destinationFolder)) # The save as dialog box. # TODO: change wildcard to current extension wildcard dialog = FileDialog(action="save as", title = "Select directory into which the data file will be copied",\ wildcard = "*.*", default_directory = destinationFolder, default_filename = destinationFile) #*.h5||| elif dlg.return_code == NO and verbose: information(None, "No file was copied.\n\nIf you change your mind, you will have to transfer the data file manually."\ , "Information Transparency report") return dialog.open() # User clicked Save and successful input received. if dialog.return_code == OK: # The actual copying of the file. TODO: See if the copy2 function throws an exception copy2(sourceFile, dialog.path) # The user clicked Cancel. elif dialog.return_code == CANCEL: information(None, "No file was copied.\n\nIf you change your mind, you will have to transfer the data file manually."\ , "Information Transparency report") #TODO: update the Voyeur config file after asking user return dialog.path