def save_game(self, b): dialog = Gtk.FileChooserDialog("Save..", None, Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK)) dialog.set_default_response(Gtk.ResponseType.OK) dialog.set_current_folder(os.path.expanduser("~")) filter = Gtk.FileFilter() filter.set_name("pdn files") filter.add_pattern("*.pdn") dialog.add_filter(filter) filter = Gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") dialog.add_filter(filter) response = dialog.run() if response == Gtk.ResponseType.OK: filename = dialog.get_filename() if filename[-4:] != '.pdn': filename = filename + '.pdn' engine.savegame(filename) dialog.destroy()
def save_game(self, b): dialog = Gtk.FileChooserDialog( "Save..", gv.gui.get_window(), Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK)) dialog.set_default_response(Gtk.ResponseType.OK) dialog.set_current_folder(os.path.expanduser("~")) filter = Gtk.FileFilter() filter.set_name("psn files") filter.add_pattern("*.psn") dialog.add_filter(filter) filter = Gtk.FileFilter() filter.set_name("gshog files") filter.add_pattern("*.gshog") dialog.add_filter(filter) filter = Gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") dialog.add_filter(filter) response = dialog.run() if response == Gtk.ResponseType.OK: startpos = gv.gshogi.get_startpos() # # filename must end with .gshog or .psn # filename = dialog.get_filename() if not (filename.endswith(".gshog") and not filename.endswith(".psn")): if dialog.get_filter().get_name() == "psn files": filename = filename + ".psn" else: filename = filename + ".gshog" # If file already exists then ask before overwriting if os.path.isfile(filename): resp = gv.gui.ok_cancel_box( "Warning - file already exists and will be replaced.\n" "Press Cancel if you do not want to overwrite it.") if resp == Gtk.ResponseType.CANCEL: dialog.destroy() return if filename.endswith(".psn"): # save in psn format gamestr = self.get_game() f = open(filename, "w") f.write(gamestr) f.close() else: # save in gshog format # engine.command("save " + filename) # comments cannot be saved in gshog format # if there are comments warn the user if self.comments.has_comments(): msg = "Warning. This Game has comments which will be " \ "lost if you save in this format." msg += "\nTo save the comments save in PSN format instead." gv.gui.info_box(msg) # if the movelist is positioned part way through the game then # we must redo all moves so the full game will be saved redo_count = len(gv.gshogi.get_redolist()) for i in range(0, redo_count): gv.gshogi.redo_move() engine.savegame(filename, "startpos " + startpos + "\n") # if we did any redo moves then undo them now to get things # back the way they were for i in range(0, redo_count): gv.gshogi.undo_move() gv.gui.set_status_bar_msg("game saved") dialog.destroy()
def save_game(self, b): dialog = gtk.FileChooserDialog("Save..", None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)) dialog.set_default_response(gtk.RESPONSE_OK) dialog.set_current_folder(os.path.expanduser("~")) filter = gtk.FileFilter() filter.set_name("psn files") filter.add_pattern("*.psn") dialog.add_filter(filter) filter = gtk.FileFilter() filter.set_name("gshog files") filter.add_pattern("*.gshog") dialog.add_filter(filter) filter = gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") dialog.add_filter(filter) response = dialog.run() if response == gtk.RESPONSE_OK: startpos = self.game.get_startpos() # # filename must end with .gshog or .psn # filename = dialog.get_filename() if not filename.endswith('.gshog') and not filename.endswith('.psn'): if dialog.get_filter().get_name() == 'psn files': filename = filename + '.psn' else: filename = filename + '.gshog' # If file already exists then ask before overwriting if os.path.isfile(filename): resp = self.gui.ok_cancel_box("Warning - file already exists and will be replaced.\nPress Cancel if you do not want to overwrite it.") if resp == gtk.RESPONSE_CANCEL: dialog.destroy() return if filename.endswith('.psn'): # save in psn format gamestr = self.get_game() f = open(filename, 'w') f.write(gamestr) f.close() else: # save in gshog format #engine.command('save ' + filename) # comments cannot be saved in gshog format # if there are comments warn the user if self.comments.has_comments(): msg = "Warning. This Game has comments which will be lost if you save in this format." msg += "\nTo save the comments save in PSN format instead." self.gui.info_box(msg) # if the movelist is positioned part way through the game then # we must redo all moves so the full game will be saved redo_count = len(self.game.get_redolist()) for i in range(0, redo_count): self.game.redo_move() engine.savegame(filename, 'startpos ' + startpos + '\n') # if we did any redo moves then undo them now to get things back # the way they were for i in range(0, redo_count): self.game.undo_move() self.gui.set_status_bar_msg("game saved") dialog.destroy()