def OnImport(self, event): """File import event handler""" # Get filepath from user wildcards = get_filetypes2wildcards(["csv", "txt"]).values() wildcard = "|".join(wildcards) message = _("Choose file to import.") style = wx.OPEN filepath, filterindex = \ self.interfaces.get_filepath_findex_from_user(wildcard, message, style) if filepath is None: return # Get generator of import data import_data = self.main_window.actions.import_file( filepath, filterindex) if import_data is None: return # Paste import data to grid grid = self.main_window.grid tl_cell = grid.GetGridCursorRow(), grid.GetGridCursorCol() grid.actions.paste(tl_cell, import_data) self.main_window.grid.ForceRefresh()
def OnMacroListLoad(self, event): """Macro list load event handler""" # Get filepath from user wildcards = get_filetypes2wildcards(["py", "all"]).values() wildcard = "|".join(wildcards) message = _("Choose macro file.") style = wx.OPEN filepath, filterindex = \ self.interfaces.get_filepath_findex_from_user(wildcard, message, style) if filepath is None: return # Enter safe mode because macro file could be harmful post_command_event(self.main_window, self.main_window.SafeModeEntryMsg) # Load macros from file self.main_window.actions.open_macros(filepath) event.Skip()
def OnMacroListSave(self, event): """Macro list save event handler""" # Get filepath from user wildcards = get_filetypes2wildcards(["py", "all"]).values() wildcard = "|".join(wildcards) message = _("Choose macro file.") style = wx.SAVE filepath, filterindex = \ self.interfaces.get_filepath_findex_from_user(wildcard, message, style) if filepath is None: return # Save macros to file macros = self.main_window.grid.code_array.macros self.main_window.actions.save_macros(filepath, macros) event.Skip()
def OnImport(self, event): """File import event handler""" # Get filepath from user wildcards = get_filetypes2wildcards(["csv", "txt"]).values() wildcard = "|".join(wildcards) message = _("Choose file to import.") style = wx.OPEN filepath, filterindex = \ self.interfaces.get_filepath_findex_from_user(wildcard, message, style) if filepath is None: return # Get generator of import data import_data = self.main_window.actions.import_file(filepath, filterindex) if import_data is None: return # Paste import data to grid grid = self.main_window.grid tl_cell = grid.GetGridCursorRow(), grid.GetGridCursorCol() grid.actions.paste(tl_cell, import_data) self.main_window.grid.ForceRefresh()
def OnSave(self, event): """File save event handler""" try: filetype = event.attr["filetype"] except (KeyError, AttributeError): filetype = None filepath = self.main_window.filepath if filepath is None: filetype = config["default_save_filetype"] if filetype is None: f2w = get_filetypes2wildcards(["pys", "pysu", "xls", "all"]) __filetypes = f2w.keys() # Check if the file extension matches any valid save filetype for __filetype in __filetypes: if splitext(filepath)[-1][1:] == __filetype: filetype = __filetype break # If there is no filepath or no filetype is found then jump to save as if self.main_window.filepath is None or filetype is None: post_command_event(self.main_window, self.main_window.SaveAsMsg) return # Save the grid post_command_event(self.main_window, self.main_window.GridActionSaveMsg, attr={ "filepath": self.main_window.filepath, "filetype": filetype }) # Update undo stack savepoint undo.stack().savepoint() # Display file save in status bar statustext = self.main_window.filepath.split("/")[-1] + " saved." post_command_event(self.main_window, self.main_window.StatusBarMsg, text=statustext)
def OnSave(self, event): """File save event handler""" try: filetype = event.attr["filetype"] except (KeyError, AttributeError): filetype = None filepath = self.main_window.filepath if filepath is None: filetype = config["default_save_filetype"] if filetype is None: f2w = get_filetypes2wildcards(["pys", "pysu", "xls", "all"]) __filetypes = f2w.keys() # Check if the file extension matches any valid save filetype for __filetype in __filetypes: if splitext(filepath)[-1][1:] == __filetype: filetype = __filetype break # If there is no filepath or no filetype is found then jump to save as if self.main_window.filepath is None or filetype is None: post_command_event(self.main_window, self.main_window.SaveAsMsg) return # Save the grid post_command_event(self.main_window, self.main_window.GridActionSaveMsg, attr={"filepath": self.main_window.filepath, "filetype": filetype}) # Update undo stack savepoint undo.stack().savepoint() # Display file save in status bar statustext = self.main_window.filepath.split("/")[-1] + " saved." post_command_event(self.main_window, self.main_window.StatusBarMsg, text=statustext)
def OnExportPDF(self, event): """Export PDF event handler""" wildcards = get_filetypes2wildcards(["pdf"]).values() if not wildcards: return wildcard = "|".join(wildcards) # Get filepath from user message = _("Choose file path for PDF export.") style = wx.SAVE filepath, filterindex = \ self.interfaces.get_filepath_findex_from_user(wildcard, message, style) if filepath is None: return self.main_window.actions.export_cairo(filepath, "pdf") event.Skip()
def OnSaveAs(self, event): """File save as event handler""" # Get filepath from user f2w = get_filetypes2wildcards(["pys", "pysu", "xls", "all"]) filetypes = f2w.keys() wildcards = f2w.values() wildcard = "|".join(wildcards) message = _("Choose filename for saving.") style = wx.SAVE default_filetype = config["default_save_filetype"] try: default_filterindex = filetypes.index(default_filetype) except ValueError: # Be graceful if the user has entered an unkown filetype default_filterindex = 0 get_fp_fidx = self.interfaces.get_filepath_findex_from_user filepath, filterindex = get_fp_fidx(wildcard, message, style, filterindex=default_filterindex) if filepath is None: return 0 filetype = filetypes[filterindex] # Look if path is already present if os.path.exists(filepath): if not os.path.isfile(filepath): # There is a directory with the same path statustext = _("Directory present. Save aborted.") post_command_event(self.main_window, self.main_window.StatusBarMsg, text=statustext) return 0 # There is a file with the same path message = \ _("The file {filepath} is already present.\nOverwrite?")\ .format(filepath=filepath) short_msg = _("File collision") if not self.main_window.interfaces.get_warning_choice( message, short_msg): statustext = _("File present. Save aborted by user.") post_command_event(self.main_window, self.main_window.StatusBarMsg, text=statustext) return 0 # Put pys suffix if wildcard choice is 0 if filterindex == 0 and filepath[-4:] != ".pys": filepath += ".pys" # Set the filepath state self.main_window.filepath = filepath # Set Window title to new filepath title_text = filepath.split("/")[-1] + " - pyspread" post_command_event(self.main_window, self.main_window.TitleMsg, text=title_text) # Now jump to save post_command_event(self.main_window, self.main_window.SaveMsg, attr={"filetype": filetype})
def OnOpen(self, event): """File open event handler""" # If changes have taken place save of old grid if self.main_window.changed_since_save: save_choice = self.interfaces.get_save_request_from_user() if save_choice is None: # Cancelled close operation return elif save_choice: # User wants to save content post_command_event(self.main_window, self.main_window.SaveMsg) # Get filepath from user f2w = get_filetypes2wildcards( ["pys", "pysu", "xls", "xlsx", "ods", "all"]) filetypes = f2w.keys() wildcards = f2w.values() wildcard = "|".join(wildcards) message = _("Choose file to open.") style = wx.OPEN default_filetype = config["default_open_filetype"] try: default_filterindex = filetypes.index(default_filetype) except ValueError: # Be graceful if the user has entered an unkown filetype default_filterindex = 0 get_fp_fidx = self.interfaces.get_filepath_findex_from_user filepath, filterindex = get_fp_fidx(wildcard, message, style, filterindex=default_filterindex) if filepath is None: return filetype = filetypes[filterindex] # Change the main window filepath state self.main_window.filepath = filepath # Load file into grid post_command_event(self.main_window, self.main_window.GridActionOpenMsg, attr={ "filepath": filepath, "filetype": filetype }) # Set Window title to new filepath title_text = filepath.split("/")[-1] + " - pyspread" post_command_event(self.main_window, self.main_window.TitleMsg, text=title_text) self.main_window.grid.ForceRefresh() if is_gtk(): try: wx.Yield() except: pass # Mark content as unchanged try: post_command_event(self.main_window, self.ContentChangedMsg, changed=False) except TypeError: # The main window does not exist any more pass
def OnExport(self, event): """File export event handler Currently, only CSV export is supported """ code_array = self.main_window.grid.code_array tab = self.main_window.grid.current_table selection = self.main_window.grid.selection # Check if no selection is present selection_bbox = selection.get_bbox() f2w = get_filetypes2wildcards(["csv", "pdf", "svg"]) filters = f2w.keys() wildcards = f2w.values() wildcard = "|".join(wildcards) if selection_bbox is None: # No selection --> Use smallest filled area for bottom right edge maxrow, maxcol, __ = code_array.get_last_filled_cell(tab) (top, left), (bottom, right) = (0, 0), (maxrow, maxcol) else: (top, left), (bottom, right) = selection_bbox # Generator of row and column keys in correct order __top = 0 if top is None else top __bottom = code_array.shape[0] if bottom is None else bottom + 1 __left = 0 if left is None else left __right = code_array.shape[1] if right is None else right + 1 def data_gen(top, bottom, left, right): for row in xrange(top, bottom): yield (code_array[row, col, tab] for col in xrange(left, right)) data = data_gen(__top, __bottom, __left, __right) preview_data = data_gen(__top, __bottom, __left, __right) # Get target filepath from user # No selection --> Provide svg export of current cell # if current cell is a matplotlib figure if selection_bbox is None: cursor = self.main_window.grid.actions.cursor figure = code_array[cursor] if Figure is not None and isinstance(figure, Figure): wildcard += \ "|" + _("SVG of current cell") + " (*.svg)|*.svg" + \ "|" + _("EPS of current cell") + " (*.eps)|*.eps" + \ "|" + _("PS of current cell") + " (*.ps)|*.ps" + \ "|" + _("PDF of current cell") + " (*.pdf)|*.pdf" + \ "|" + _("PNG of current cell") + " (*.png)|*.png" filters.append("cell_svg") filters.append("cell_eps") filters.append("cell_ps") filters.append("cell_pdf") filters.append("cell_png") message = _("Choose filename for export.") style = wx.SAVE path, filterindex = \ self.interfaces.get_filepath_findex_from_user(wildcard, message, style) if path is None: return # If an single cell is exported then the selection bbox # has to be changed to the current cell if filters[filterindex].startswith("cell_"): data = figure # Export file # ----------- self.main_window.actions.export_file(path, filters[filterindex], data, preview_data)
def OnSaveAs(self, event): """File save as event handler""" # Get filepath from user f2w = get_filetypes2wildcards(["pys", "pysu", "xls", "all"]) filetypes = f2w.keys() wildcards = f2w.values() wildcard = "|".join(wildcards) message = _("Choose filename for saving.") style = wx.SAVE default_filetype = config["default_save_filetype"] try: default_filterindex = filetypes.index(default_filetype) except ValueError: # Be graceful if the user has entered an unkown filetype default_filterindex = 0 get_fp_fidx = self.interfaces.get_filepath_findex_from_user filepath, filterindex = get_fp_fidx(wildcard, message, style, filterindex=default_filterindex) if filepath is None: return 0 filetype = filetypes[filterindex] # Look if path is already present if os.path.exists(filepath): if not os.path.isfile(filepath): # There is a directory with the same path statustext = _("Directory present. Save aborted.") post_command_event(self.main_window, self.main_window.StatusBarMsg, text=statustext) return 0 # There is a file with the same path message = \ _("The file {filepath} is already present.\nOverwrite?")\ .format(filepath=filepath) short_msg = _("File collision") if not self.main_window.interfaces.get_warning_choice(message, short_msg): statustext = _("File present. Save aborted by user.") post_command_event(self.main_window, self.main_window.StatusBarMsg, text=statustext) return 0 # Put pys suffix if wildcard choice is 0 if filterindex == 0 and filepath[-4:] != ".pys": filepath += ".pys" # Set the filepath state self.main_window.filepath = filepath # Set Window title to new filepath title_text = filepath.split("/")[-1] + " - pyspread" post_command_event(self.main_window, self.main_window.TitleMsg, text=title_text) # Now jump to save post_command_event(self.main_window, self.main_window.SaveMsg, attr={"filetype": filetype})
def OnOpen(self, event): """File open event handler""" # If changes have taken place save of old grid if undo.stack().haschanged(): save_choice = self.interfaces.get_save_request_from_user() if save_choice is None: # Cancelled close operation return elif save_choice: # User wants to save content post_command_event(self.main_window, self.main_window.SaveMsg) # Get filepath from user f2w = get_filetypes2wildcards( ["pys", "pysu", "xls", "xlsx", "ods", "all"]) filetypes = f2w.keys() wildcards = f2w.values() wildcard = "|".join(wildcards) message = _("Choose file to open.") style = wx.OPEN default_filetype = config["default_open_filetype"] try: default_filterindex = filetypes.index(default_filetype) except ValueError: # Be graceful if the user has entered an unkown filetype default_filterindex = 0 get_fp_fidx = self.interfaces.get_filepath_findex_from_user filepath, filterindex = get_fp_fidx(wildcard, message, style, filterindex=default_filterindex) if filepath is None: return filetype = filetypes[filterindex] # Change the main window filepath state self.main_window.filepath = filepath # Load file into grid post_command_event(self.main_window, self.main_window.GridActionOpenMsg, attr={"filepath": filepath, "filetype": filetype}) # Set Window title to new filepath title_text = filepath.split("/")[-1] + " - pyspread" post_command_event(self.main_window, self.main_window.TitleMsg, text=title_text) self.main_window.grid.ForceRefresh() if is_gtk(): try: wx.Yield() except: pass # Update savepoint and clear the undo stack undo.stack().clear() undo.stack().savepoint() # Update content changed state try: post_command_event(self.main_window, self.ContentChangedMsg) except TypeError: # The main window does not exist any more pass