class Database(GObject.GObject, Perspective): __gsignals__ = { 'chessfile_opened': (GObject.SignalFlags.RUN_FIRST, None, (object, )), 'chessfile_closed': (GObject.SignalFlags.RUN_FIRST, None, ()), 'chessfile_imported': (GObject.SignalFlags.RUN_FIRST, None, (object, )), 'chessfile_switched': (GObject.SignalFlags.RUN_FIRST, None, (object, )), } def __init__(self): GObject.GObject.__init__(self) Perspective.__init__(self, "database", _("Database")) self.gamelist = None def create_toolbuttons(self): self.import_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CONVERT) self.import_button.set_tooltip_text(_("Import PGN file")) self.import_button.connect("clicked", self.on_import_clicked) self.close_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CLOSE) self.close_button.set_tooltip_text(_("Close")) self.close_button.connect("clicked", self.close) def init_layout(self): perspective_widget = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) perspective_manager.set_perspective_widget("database", perspective_widget) self.gamelist = GameList(database.load(None)) self.switcher_panel = SwitcherPanel(self.gamelist) self.opening_tree_panel = OpeningTreePanel(self.gamelist) self.filter_panel = FilterPanel(self.gamelist) self.preview_panel = PreviewPanel(self.gamelist) self.progressbar0 = Gtk.ProgressBar(show_text=True) self.progressbar = Gtk.ProgressBar(show_text=True) self.progress_dialog = Gtk.Dialog("Import", None, 0, ( Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)) self.progress_dialog.get_content_area().pack_start(self.progressbar0, True, True, 0) self.progress_dialog.get_content_area().pack_start(self.progressbar, True, True, 0) self.progress_dialog.get_content_area().show_all() perspective = perspective_manager.get_perspective("database") self.dock = PyDockTop("database", perspective) align = Gtk.Alignment() align.show() align.add(self.dock) self.dock.show() perspective_widget.pack_start(align, True, True, 0) dockLocation = addUserConfigPrefix("pydock-database.xml") docks = { "gamelist": (Gtk.Label(label="gamelist"), self.gamelist.box), "switcher": (dock_panel_tab(_("Database switcher"), "", addDataPrefix("glade/panel_docker.svg")), self.switcher_panel.alignment), "openingtree": (dock_panel_tab(_("Opening tree"), "", addDataPrefix("glade/panel_docker.svg")), self.opening_tree_panel.box), "filter": (dock_panel_tab(_("Filter"), "", addDataPrefix("glade/panel_docker.svg")), self.filter_panel.box), "preview": (dock_panel_tab(_("Preview"), "", addDataPrefix("glade/panel_docker.svg")), self.preview_panel.box), } if os.path.isfile(dockLocation): try: self.dock.loadFromXML(dockLocation, docks) except Exception as e: stringio = StringIO() traceback.print_exc(file=stringio) error = stringio.getvalue() log.error("Dock loading error: %s\n%s" % (e, error)) msg_dia = Gtk.MessageDialog(None, type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.CLOSE) msg_dia.set_markup(_( "<b><big>PyChess was unable to load your panel settings</big></b>")) msg_dia.format_secondary_text(_( "Your panel settings have been reset. If this problem repeats, \ you should report it to the developers")) msg_dia.run() msg_dia.hide() os.remove(dockLocation) for title, panel in docks.values(): title.unparent() panel.unparent() if not os.path.isfile(dockLocation): leaf = self.dock.dock(docks["gamelist"][1], CENTER, docks["gamelist"][0], "gamelist") leaf.setDockable(False) leaf.dock(docks["switcher"][1], NORTH, docks["switcher"][0], "switcher") leaf = leaf.dock(docks["filter"][1], EAST, docks["filter"][0], "filter") leaf = leaf.dock(docks["openingtree"][1], SOUTH, docks["openingtree"][0], "openingtree") leaf.dock(docks["preview"][1], SOUTH, docks["preview"][0], "preview") def unrealize(dock): dock.saveToXML(dockLocation) dock._del() self.dock.connect("unrealize", unrealize) self.dock.show_all() perspective_widget.show_all() perspective_manager.set_perspective_toolbuttons("database", [self.import_button, self.close_button]) self.switcher_panel.connect("chessfile_switched", self.on_chessfile_switched) def open_chessfile(self, filename): if filename.endswith(".pdb"): chessfile = database.load(filename) elif filename.endswith(".pgn"): chessfile = pgn.load(protoopen(filename)) elif filename.endswith(".epd"): chessfile = epd.load(protoopen(filename)) elif filename.endswith(".fen"): chessfile = fen.load(protoopen(filename)) else: return if self.gamelist is None: self.init_layout() perspective_manager.activate_perspective("database") self.emit("chessfile_opened", chessfile) def close(self, widget): self.emit("chessfile_closed") def on_chessfile_switched(self, switcher, chessfile): self.emit("chessfile_switched", chessfile) def on_import_endgame_nl(self): self.do_import(JvR) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_import_twic(self): LATEST = get_latest_twic() if LATEST is None: return html = "http://www.theweekinchess.com/html/twic%s.html" twic = [] pgn = "https://raw.githubusercontent.com/rozim/ChessData/master/Twic/fix-twic%s.pgn" # pgn = "/home/tamas/PGN/twic/twic%sg.zip" for i in range(210, 920): twic.append((html % i, pgn % i)) pgn = "http://www.theweekinchess.com/zips/twic%sg.zip" # pgn = "/home/tamas/PGN/twic/twic%sg.zip" for i in range(920, LATEST + 1): twic.append((html % i, pgn % i)) twic.append((html % LATEST, pgn % LATEST)) # TODO: importing all twic .pgn files creates a huge (~8Gb) .pdb # and sqlite seems too slow to handle my current selects # until I find better solution import will be limited to latest twic .pgn twic = twic[-1:] self.do_import(twic) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_update_players(self): def importing(): self.importer = FIDEPlayersImport(self.gamelist.chessfile.engine) self.importer.import_players(progressbar=self.progressbar) GLib.idle_add(self.progress_dialog.hide) thread = threading.Thread(target=importing) thread.daemon = True thread.start() response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_import_clicked(self, widget): dialog = Gtk.FileChooserDialog( _("Open chess file"), None, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) dialog.set_select_multiple(True) filter_text = Gtk.FileFilter() filter_text.set_name(".pgn") filter_text.add_pattern("*.pgn") filter_text.add_mime_type("application/x-chess-pgn") dialog.add_filter(filter_text) filter_text = Gtk.FileFilter() filter_text.set_name(".zip") filter_text.add_pattern("*.zip") filter_text.add_mime_type("application/zip") dialog.add_filter(filter_text) dialog = NestedFileChooserDialog(dialog) filenames = dialog.run() if filenames is not None: self.do_import(filenames) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def do_import(self, filenames): if len(filenames) == 1: self.progressbar0.hide() else: self.progressbar0.show() self.progressbar.set_text("Preparing to start import...") # @profile_me def importing(): drop_indexes(self.gamelist.chessfile.engine) self.importer = PgnImport(self.gamelist.chessfile.engine) for i, filename in enumerate(filenames): filename = unicode(filename) GLib.idle_add(self.progressbar0.set_fraction, i / float(len(filenames))) # GLib.idle_add(self.progressbar0.set_text, filename) if self.importer.cancel: break if isinstance(filename, tuple): info_link, pgn_link = filename self.importer.do_import(pgn_link, info=info_link, progressbar=self.progressbar) else: self.importer.do_import(filename, progressbar=self.progressbar) GLib.idle_add(self.progressbar.set_text, "Recreating indexes...") create_indexes(self.gamelist.chessfile.engine) self.gamelist.offset = 0 self.gamelist.chessfile.build_where_tags("") self.gamelist.chessfile.build_where_bitboards(0, 0) self.gamelist.chessfile.build_query() self.gamelist.chessfile.update_count() self.gamelist.chessfile.update_count_stats() GLib.idle_add(self.gamelist.load_games) GLib.idle_add(self.emit, "chessfile_imported", self.gamelist.chessfile) GLib.idle_add(self.progress_dialog.hide) thread = threading.Thread(target=importing) thread.daemon = True thread.start()
class Database(GObject.GObject, Perspective): __gsignals__ = { 'chessfile_opened0': (GObject.SignalFlags.RUN_FIRST, None, (object, )), 'chessfile_opened': (GObject.SignalFlags.RUN_FIRST, None, (object, )), 'chessfile_closed': (GObject.SignalFlags.RUN_FIRST, None, ()), 'chessfile_imported': (GObject.SignalFlags.RUN_FIRST, None, (object, )), } def __init__(self): GObject.GObject.__init__(self) Perspective.__init__(self, "database", _("Database")) self.widgets = gamewidget.getWidgets() self.chessfile = None self.chessfiles = [] self.gamelists = [] self.filter_panels = [] self.notebooks = {} self.connect("chessfile_opened0", self.on_chessfile_opened0) @property def gamelist(self): if self.chessfile is None: return None else: return self.gamelists[self.chessfiles.index(self.chessfile)] @property def filter_panel(self): if self.chessfile is None: return None else: return self.filter_panels[self.chessfiles.index(self.chessfile)] def create_toolbuttons(self): self.import_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CONVERT) self.import_button.set_tooltip_text(_("Import PGN file")) self.import_button.connect("clicked", self.on_import_clicked) self.save_as_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_SAVE_AS) self.save_as_button.set_tooltip_text(_("Save to PGN file as...")) self.save_as_button.connect("clicked", self.on_save_as_clicked) self.close_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CLOSE) self.close_button.set_tooltip_text(_("Close")) self.close_button.connect("clicked", self.close) def init_layout(self): perspective_widget = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) perspective_manager.set_perspective_widget("database", perspective_widget) self.switcher_panel = SwitcherPanel(self) self.notebooks["gamelist"] = new_notebook() self.notebooks["opening_tree"] = new_notebook() self.notebooks["filter"] = new_notebook() self.notebooks["preview"] = new_notebook() self.spinner = Gtk.Spinner() self.spinner.set_size_request(50, 50) self.progressbar0 = Gtk.ProgressBar(show_text=True) self.progressbar1 = Gtk.ProgressBar(show_text=True) self.progress_dialog = Gtk.Dialog( "", mainwindow(), 0, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)) self.progress_dialog.get_content_area().pack_start( self.spinner, True, True, 0) self.progress_dialog.get_content_area().pack_start( self.progressbar0, True, True, 0) self.progress_dialog.get_content_area().pack_start( self.progressbar1, True, True, 0) self.progress_dialog.get_content_area().show_all() self.dock = PyDockTop("database", self) align = Gtk.Alignment() align.show() align.add(self.dock) self.dock.show() perspective_widget.pack_start(align, True, True, 0) dockLocation = addUserConfigPrefix("pydock-database.xml") docks = { "gamelist": (Gtk.Label(label="gamelist"), self.notebooks["gamelist"]), "switcher": (dock_panel_tab(_("Databases"), "", addDataPrefix("glade/panel_database.svg")), self.switcher_panel.alignment), "openingtree": (dock_panel_tab(_("Openings"), "", addDataPrefix("glade/panel_book.svg")), self.notebooks["opening_tree"]), "filter": (dock_panel_tab(_("Filters"), "", addDataPrefix("glade/panel_filter.svg")), self.notebooks["filter"]), "preview": (dock_panel_tab(_("Preview"), "", addDataPrefix("glade/panel_games.svg")), self.notebooks["preview"]), } if os.path.isfile(dockLocation): try: self.dock.loadFromXML(dockLocation, docks) except Exception as e: stringio = StringIO() traceback.print_exc(file=stringio) error = stringio.getvalue() log.error("Dock loading error: %s\n%s" % (e, error)) msg_dia = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.CLOSE) msg_dia.set_markup( _("<b><big>PyChess was unable to load your panel settings</big></b>" )) msg_dia.format_secondary_text( _("Your panel settings have been reset. If this problem repeats, \ you should report it to the developers")) msg_dia.run() msg_dia.hide() os.remove(dockLocation) for title, panel in docks.values(): title.unparent() panel.unparent() if not os.path.isfile(dockLocation): leaf = self.dock.dock(docks["gamelist"][1], CENTER, docks["gamelist"][0], "gamelist") leaf.setDockable(False) leaf.dock(docks["switcher"][1], NORTH, docks["switcher"][0], "switcher") leaf = leaf.dock(docks["openingtree"][1], EAST, docks["openingtree"][0], "openingtree") leaf = leaf.dock(docks["filter"][1], CENTER, docks["filter"][0], "filter") leaf.dock(docks["preview"][1], SOUTH, docks["preview"][0], "preview") def unrealize(dock): dock.saveToXML(dockLocation) dock._del() self.dock.connect("unrealize", unrealize) self.dock.show_all() perspective_widget.show_all() perspective_manager.set_perspective_toolbuttons( "database", [self.import_button, self.save_as_button, self.close_button]) self.switcher_panel.connect("chessfile_switched", self.on_chessfile_switched) def set_sensitives(self, on): self.import_button.set_sensitive(on) self.widgets["import_chessfile"].set_sensitive(on) self.widgets["database_save_as"].set_sensitive(on) self.widgets["import_endgame_nl"].set_sensitive(on) self.widgets["import_twic"].set_sensitive(on) def open_chessfile(self, filename): if self.chessfile is None: self.init_layout() perspective_manager.activate_perspective("database") self.progress_dialog.set_title(_("Open")) self.progressbar0.hide() self.progressbar1.show() self.progressbar1.set_text("Importing game headers...") self.spinner.show() self.spinner.start() def opening(): if filename.endswith(".pgn"): chessfile = pgn.load(protoopen(filename), self.progressbar1) elif filename.endswith(".epd"): chessfile = epd.load(protoopen(filename)) elif filename.endswith(".fen"): chessfile = fen.load(protoopen(filename)) else: chessfile = None GLib.idle_add(self.spinner.stop) GLib.idle_add(self.progress_dialog.hide) if chessfile is not None: self.chessfile = chessfile self.chessfiles.append(chessfile) GLib.idle_add(self.emit, "chessfile_opened0", chessfile) thread = threading.Thread(target=opening) thread.daemon = True thread.start() response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_chessfile_opened0(self, persp, chessfile): gamelist = GameList(self) self.gamelists.append(gamelist) opening_tree_panel = OpeningTreePanel(self) filter_panel = FilterPanel(self) self.filter_panels.append(filter_panel) preview_panel = PreviewPanel(self) self.notebooks["gamelist"].append_page(gamelist.box) self.notebooks["opening_tree"].append_page(opening_tree_panel.box) self.notebooks["filter"].append_page(filter_panel.box) self.notebooks["preview"].append_page(preview_panel.box) self.on_chessfile_switched(None, self.chessfile) gamelist.load_games() opening_tree_panel.update_tree(load_games=False) self.set_sensitives(True) self.emit("chessfile_opened", chessfile) def close(self, widget): i = self.chessfiles.index(self.chessfile) if self.chessfile.path is not None: self.notebooks["gamelist"].remove_page(i) self.notebooks["opening_tree"].remove_page(i) self.notebooks["filter"].remove_page(i) self.notebooks["preview"].remove_page(i) del self.gamelists[i] del self.filter_panels[i] del self.chessfiles[i] self.chessfile.close() if len(self.chessfiles) == 0: self.set_sensitives(False) perspective_manager.disable_perspective("database") self.emit("chessfile_closed") def on_chessfile_switched(self, switcher, chessfile): self.chessfile = chessfile i = self.chessfiles.index(chessfile) self.notebooks["gamelist"].set_current_page(i) self.notebooks["opening_tree"].set_current_page(i) self.notebooks["filter"].set_current_page(i) self.notebooks["preview"].set_current_page(i) def on_import_endgame_nl(self): self.do_import(JvR) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_import_twic(self): LATEST = get_latest_twic() if LATEST is None: return html = "http://www.theweekinchess.com/html/twic%s.html" twic = [] pgn = "https://raw.githubusercontent.com/rozim/ChessData/master/Twic/fix-twic%s.pgn" # pgn = "/home/tamas/PGN/twic/twic%sg.zip" for i in range(210, 920): twic.append((html % i, pgn % i)) pgn = "http://www.theweekinchess.com/zips/twic%sg.zip" # pgn = "/home/tamas/PGN/twic/twic%sg.zip" for i in range(920, LATEST + 1): twic.append((html % i, pgn % i)) twic.append((html % LATEST, pgn % LATEST)) # import limited to latest twic .pgn for now twic = twic[-1:] self.do_import(twic) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_save_as_clicked(self, widget): dialog = Gtk.FileChooserDialog( "", mainwindow(), Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT)) dialog.set_current_folder(os.path.expanduser("~")) response = dialog.run() if response == Gtk.ResponseType.ACCEPT: filename = dialog.get_filename() else: filename = None dialog.destroy() if filename is not None: with open(filename, "w") as to_file: records, plys = self.chessfile.get_records(FIRST_PAGE) self.save_records(records, to_file) while True: records, plys = self.chessfile.get_records(NEXT_PAGE) if records: self.save_records(records, to_file) else: break def save_records(self, records, to_file): f = self.chessfile.handle for i, rec in enumerate(records): offs = rec["Offset"] f.seek(offs) game = '' for line in f: if line.startswith('[Event "'): if game: break # Second one, start of next game else: game = line # First occurence elif game: game += line to_file.write(game) def on_import_clicked(self, widget): dialog = Gtk.FileChooserDialog( _("Open chess file"), mainwindow(), Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) dialog.set_select_multiple(True) filter_text = Gtk.FileFilter() filter_text.set_name(".pgn") filter_text.add_pattern("*.pgn") filter_text.add_mime_type("application/x-chess-pgn") dialog.add_filter(filter_text) filter_text = Gtk.FileFilter() filter_text.set_name(".zip") filter_text.add_pattern("*.zip") filter_text.add_mime_type("application/zip") dialog.add_filter(filter_text) response = dialog.run() if response == Gtk.ResponseType.OK: filenames = dialog.get_filenames() else: filenames = None dialog.destroy() if filenames is not None: self.do_import(filenames) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def do_import(self, filenames): self.progress_dialog.set_title(_("Import")) self.spinner.hide() if len(filenames) == 1: self.progressbar0.hide() else: self.progressbar0.show() self.progressbar1.show() self.progressbar1.set_text("Preparing to start import...") # @profile_me def importing(): drop_indexes(self.chessfile.engine) self.importer = PgnImport(self.chessfile, append_pgn=True) for i, filename in enumerate(filenames): GLib.idle_add(self.progressbar0.set_fraction, i / float(len(filenames))) # GLib.idle_add(self.progressbar0.set_text, filename) if self.importer.cancel: break if isinstance(filename, tuple): info_link, pgn_link = filename self.importer.do_import(pgn_link, info=info_link, progressbar=self.progressbar1) else: self.importer.do_import(filename, progressbar=self.progressbar1) GLib.idle_add(self.progressbar1.set_text, "Recreating indexes...") # .sqlite create_indexes(self.chessfile.engine) # .scout self.chessfile.init_scoutfish() # .bin self.chessfile.init_chess_db() self.chessfile.set_tag_filter(None) self.chessfile.set_fen_filter(None) self.chessfile.set_scout_filter(None) GLib.idle_add(self.gamelist.load_games) GLib.idle_add(self.emit, "chessfile_imported", self.chessfile) GLib.idle_add(self.progress_dialog.hide) thread = threading.Thread(target=importing) thread.daemon = True thread.start() def create_database(self): dialog = Gtk.FileChooserDialog( _("Create New Pgn Database"), mainwindow(), Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_NEW, Gtk.ResponseType.ACCEPT)) dialog.set_current_folder(os.path.expanduser("~")) dialog.set_current_name("new.pgn") response = dialog.run() if response == Gtk.ResponseType.ACCEPT: new_pgn = dialog.get_filename() if not new_pgn.endswith(".pgn"): new_pgn = "%s.pgn" % new_pgn if not os.path.isfile(new_pgn): # create new file with open(new_pgn, "w"): pass self.open_chessfile(new_pgn) else: d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK) d.set_markup( _("<big><b>File '%s' already exists.</b></big>") % new_pgn) d.run() d.hide() print("%s allready exist." % new_pgn) dialog.destroy()
class Database(GObject.GObject, Perspective): __gsignals__ = { 'chessfile_opened0': (GObject.SignalFlags.RUN_FIRST, None, (object, )), 'chessfile_opened': (GObject.SignalFlags.RUN_FIRST, None, (object, )), 'chessfile_closed': (GObject.SignalFlags.RUN_FIRST, None, ()), 'chessfile_imported': (GObject.SignalFlags.RUN_FIRST, None, (object, )), } def __init__(self): GObject.GObject.__init__(self) Perspective.__init__(self, "database", _("Database")) self.widgets = gamewidget.getWidgets() self.chessfile = None self.chessfiles = [] self.gamelists = [] self.filter_panels = [] self.notebooks = {} self.connect("chessfile_opened0", self.on_chessfile_opened0) @property def gamelist(self): if self.chessfile is None: return None else: return self.gamelists[self.chessfiles.index(self.chessfile)] @property def filter_panel(self): if self.chessfile is None: return None else: return self.filter_panels[self.chessfiles.index(self.chessfile)] def create_toolbuttons(self): self.import_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CONVERT) self.import_button.set_tooltip_text(_("Import PGN file")) self.import_button.connect("clicked", self.on_import_clicked) self.close_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_CLOSE) self.close_button.set_tooltip_text(_("Close")) self.close_button.connect("clicked", self.close) def init_layout(self): perspective_widget = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) perspective_manager.set_perspective_widget("database", perspective_widget) self.switcher_panel = SwitcherPanel(self) self.notebooks["gamelist"] = new_notebook() self.notebooks["opening_tree"] = new_notebook() self.notebooks["filter"] = new_notebook() self.notebooks["preview"] = new_notebook() self.spinner = Gtk.Spinner() self.spinner.set_size_request(50, 50) self.progressbar0 = Gtk.ProgressBar(show_text=True) self.progressbar1 = Gtk.ProgressBar(show_text=True) mainwindow = gamewidget.getWidgets()["main_window"] self.progress_dialog = Gtk.Dialog("", mainwindow, 0, ( Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)) self.progress_dialog.get_content_area().pack_start(self.spinner, True, True, 0) self.progress_dialog.get_content_area().pack_start(self.progressbar0, True, True, 0) self.progress_dialog.get_content_area().pack_start(self.progressbar1, True, True, 0) self.progress_dialog.get_content_area().show_all() self.dock = PyDockTop("database", self) align = Gtk.Alignment() align.show() align.add(self.dock) self.dock.show() perspective_widget.pack_start(align, True, True, 0) dockLocation = addUserConfigPrefix("pydock-database.xml") docks = { "gamelist": (Gtk.Label(label="gamelist"), self.notebooks["gamelist"]), "switcher": (dock_panel_tab(_("Databases"), "", addDataPrefix("glade/panel_database.svg")), self.switcher_panel.alignment), "openingtree": (dock_panel_tab(_("Openings"), "", addDataPrefix("glade/panel_book.svg")), self.notebooks["opening_tree"]), "filter": (dock_panel_tab(_("Filters"), "", addDataPrefix("glade/panel_filter.svg")), self.notebooks["filter"]), "preview": (dock_panel_tab(_("Preview"), "", addDataPrefix("glade/panel_games.svg")), self.notebooks["preview"]), } if os.path.isfile(dockLocation): try: self.dock.loadFromXML(dockLocation, docks) except Exception as e: stringio = StringIO() traceback.print_exc(file=stringio) error = stringio.getvalue() log.error("Dock loading error: %s\n%s" % (e, error)) msg_dia = Gtk.MessageDialog(None, type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.CLOSE) msg_dia.set_markup(_( "<b><big>PyChess was unable to load your panel settings</big></b>")) msg_dia.format_secondary_text(_( "Your panel settings have been reset. If this problem repeats, \ you should report it to the developers")) msg_dia.run() msg_dia.hide() os.remove(dockLocation) for title, panel in docks.values(): title.unparent() panel.unparent() if not os.path.isfile(dockLocation): leaf = self.dock.dock(docks["gamelist"][1], CENTER, docks["gamelist"][0], "gamelist") leaf.setDockable(False) leaf.dock(docks["switcher"][1], NORTH, docks["switcher"][0], "switcher") leaf = leaf.dock(docks["openingtree"][1], EAST, docks["openingtree"][0], "openingtree") leaf = leaf.dock(docks["filter"][1], CENTER, docks["filter"][0], "filter") leaf.dock(docks["preview"][1], SOUTH, docks["preview"][0], "preview") def unrealize(dock): dock.saveToXML(dockLocation) dock._del() self.dock.connect("unrealize", unrealize) self.dock.show_all() perspective_widget.show_all() perspective_manager.set_perspective_toolbuttons("database", [self.import_button, self.close_button]) self.switcher_panel.connect("chessfile_switched", self.on_chessfile_switched) def set_sensitives(self, on): self.import_button.set_sensitive(on) self.widgets["import_chessfile"].set_sensitive(on) self.widgets["import_endgame_nl"].set_sensitive(on) self.widgets["import_twic"].set_sensitive(on) def open_chessfile(self, filename): if self.chessfile is None: self.init_layout() perspective_manager.activate_perspective("database") self.progress_dialog.set_title(_("Open")) self.progressbar0.hide() self.progressbar1.show() self.progressbar1.set_text("Importing game headers...") self.spinner.show() self.spinner.start() def opening(): if filename.endswith(".pgn"): chessfile = pgn.load(protoopen(filename), self.progressbar1) elif filename.endswith(".epd"): chessfile = epd.load(protoopen(filename)) elif filename.endswith(".fen"): chessfile = fen.load(protoopen(filename)) else: chessfile = None GLib.idle_add(self.spinner.stop) GLib.idle_add(self.progress_dialog.hide) if chessfile is not None: self.chessfile = chessfile self.chessfiles.append(chessfile) GLib.idle_add(self.emit, "chessfile_opened0", chessfile) thread = threading.Thread(target=opening) thread.daemon = True thread.start() response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_chessfile_opened0(self, persp, chessfile): gamelist = GameList(self) self.gamelists.append(gamelist) opening_tree_panel = OpeningTreePanel(self) filter_panel = FilterPanel(self) self.filter_panels.append(filter_panel) preview_panel = PreviewPanel(self) self.notebooks["gamelist"].append_page(gamelist.box) self.notebooks["opening_tree"].append_page(opening_tree_panel.box) self.notebooks["filter"].append_page(filter_panel.box) self.notebooks["preview"].append_page(preview_panel.box) self.on_chessfile_switched(None, self.chessfile) gamelist.load_games() opening_tree_panel.update_tree(load_games=False) self.set_sensitives(True) self.emit("chessfile_opened", chessfile) def close(self, widget): i = self.chessfiles.index(self.chessfile) if self.chessfile.path is not None: self.notebooks["gamelist"].remove_page(i) self.notebooks["opening_tree"].remove_page(i) self.notebooks["filter"].remove_page(i) self.notebooks["preview"].remove_page(i) del self.gamelists[i] del self.filter_panels[i] del self.chessfiles[i] self.chessfile.close() if len(self.chessfiles) == 0: self.set_sensitives(False) perspective_manager.disable_perspective("database") self.emit("chessfile_closed") def on_chessfile_switched(self, switcher, chessfile): self.chessfile = chessfile i = self.chessfiles.index(chessfile) self.notebooks["gamelist"].set_current_page(i) self.notebooks["opening_tree"].set_current_page(i) self.notebooks["filter"].set_current_page(i) self.notebooks["preview"].set_current_page(i) def on_import_endgame_nl(self): self.do_import(JvR) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_import_twic(self): LATEST = get_latest_twic() if LATEST is None: return html = "http://www.theweekinchess.com/html/twic%s.html" twic = [] pgn = "https://raw.githubusercontent.com/rozim/ChessData/master/Twic/fix-twic%s.pgn" # pgn = "/home/tamas/PGN/twic/twic%sg.zip" for i in range(210, 920): twic.append((html % i, pgn % i)) pgn = "http://www.theweekinchess.com/zips/twic%sg.zip" # pgn = "/home/tamas/PGN/twic/twic%sg.zip" for i in range(920, LATEST + 1): twic.append((html % i, pgn % i)) twic.append((html % LATEST, pgn % LATEST)) # import limited to latest twic .pgn for now twic = twic[-1:] self.do_import(twic) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def on_import_clicked(self, widget): dialog = Gtk.FileChooserDialog( _("Open chess file"), None, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) dialog.set_select_multiple(True) filter_text = Gtk.FileFilter() filter_text.set_name(".pgn") filter_text.add_pattern("*.pgn") filter_text.add_mime_type("application/x-chess-pgn") dialog.add_filter(filter_text) filter_text = Gtk.FileFilter() filter_text.set_name(".zip") filter_text.add_pattern("*.zip") filter_text.add_mime_type("application/zip") dialog.add_filter(filter_text) response = dialog.run() if response == Gtk.ResponseType.OK: filenames = dialog.get_filenames() else: filenames = None dialog.destroy() if filenames is not None: self.do_import(filenames) response = self.progress_dialog.run() if response == Gtk.ResponseType.CANCEL: self.importer.do_cancel() self.progress_dialog.hide() def do_import(self, filenames): self.progress_dialog.set_title(_("Import")) self.spinner.hide() if len(filenames) == 1: self.progressbar0.hide() else: self.progressbar0.show() self.progressbar1.show() self.progressbar1.set_text("Preparing to start import...") # @profile_me def importing(): drop_indexes(self.chessfile.engine) self.importer = PgnImport(self.chessfile, append_pgn=True) for i, filename in enumerate(filenames): GLib.idle_add(self.progressbar0.set_fraction, i / float(len(filenames))) # GLib.idle_add(self.progressbar0.set_text, filename) if self.importer.cancel: break if isinstance(filename, tuple): info_link, pgn_link = filename self.importer.do_import(pgn_link, info=info_link, progressbar=self.progressbar1) else: self.importer.do_import(filename, progressbar=self.progressbar1) GLib.idle_add(self.progressbar1.set_text, "Recreating indexes...") # .sqlite create_indexes(self.chessfile.engine) # .scout self.chessfile.init_scoutfish() # .bin self.chessfile.init_chess_db() self.chessfile.set_tag_filter(None) self.chessfile.set_fen_filter(None) self.chessfile.set_scout_filter(None) GLib.idle_add(self.gamelist.load_games) GLib.idle_add(self.emit, "chessfile_imported", self.chessfile) GLib.idle_add(self.progress_dialog.hide) thread = threading.Thread(target=importing) thread.daemon = True thread.start() def create_database(self): dialog = Gtk.FileChooserDialog( _("Create New Pgn Database"), None, Gtk.FileChooserAction.SAVE, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_NEW, Gtk.ResponseType.ACCEPT)) dialog.set_current_folder(os.path.expanduser("~")) dialog.set_current_name("new.pgn") response = dialog.run() if response == Gtk.ResponseType.ACCEPT: new_pgn = dialog.get_filename() if not new_pgn.endswith(".pgn"): new_pgn = "%s.pgn" % new_pgn if not os.path.isfile(new_pgn): # create new file with open(new_pgn, "w"): pass self.open_chessfile(new_pgn) else: d = Gtk.MessageDialog(type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK) d.set_markup(_("<big><b>File '%s' already exists.</b></big>") % new_pgn) d.run() d.hide() print("%s allready exist." % new_pgn) dialog.destroy()
class Games(GObject.GObject, Perspective): __gsignals__ = { 'gmwidg_created': (GObject.SignalFlags.RUN_FIRST, None, (object, )) } def __init__(self): GObject.GObject.__init__(self) Perspective.__init__(self, "games", _("Games")) self.notebooks = {} self.first_run = True self.gamewidgets = set() self.gmwidg_cids = {} self.board_cids = {} self.notify_cids = defaultdict(list) self.key2gmwidg = {} self.key2cid = {} self.dock = None self.dockAlign = None @asyncio.coroutine def generalStart(self, gamemodel, player0tup, player1tup, loaddata=None): """ The player tuples are: (The type af player in a System.const value, A callable creating the player, A list of arguments for the callable, A preliminary name for the player) If loaddata is specified, it should be a tuple of: (A text uri or fileobj, A Savers.something module with a load function capable of loading it, An int of the game in file you want to load, The position from where to start the game) """ log.debug("Games.generalStart: %s\n %s\n %s" % (gamemodel, player0tup, player1tup)) gmwidg = gamewidget.GameWidget(gamemodel, self) self.gamewidgets.add(gmwidg) self.gmwidg_cids[gmwidg] = gmwidg.connect("game_close_clicked", self.closeGame) # worker.publish((gmwidg,gamemodel)) self.attachGameWidget(gmwidg) game_nanny.nurseGame(gmwidg, gamemodel) log.debug("Games.generalStart: -> emit gmwidg_created: %s" % (gmwidg)) self.emit("gmwidg_created", gmwidg) log.debug("Games.generalStart: <- emit gmwidg_created: %s" % (gmwidg)) # Initing players def set_name(none, player, key, alt): player.setName(conf.get(key, alt)) players = [] for i, playertup in enumerate((player0tup, player1tup)): type, func, args, prename = playertup if type != LOCAL: if type == ARTIFICIAL: player = yield from func(*args) else: player = func(*args) players.append(player) # if type == ARTIFICIAL: # def readyformoves (player, color): # gmwidg.setTabText(gmwidg.display_text)) # players[i].connect("readyForMoves", readyformoves, i) else: # Until PyChess has a proper profiles system, as discussed on the # issue tracker, we need to give human players special treatment player = func(gmwidg, *args) players.append(player) # Connect to conf if i == 0 or (i == 1 and player0tup[0] != LOCAL): key = "firstName" alt = _("You") else: key = "secondName" alt = _("Guest") if prename == conf.get(key, alt): self.notify_cids[gmwidg].append(conf.notify_add(key, set_name, player, key, alt)) if player0tup[0] == ARTIFICIAL and player1tup[0] == ARTIFICIAL: def emit_action(board, action, param, gmwidg): if gmwidg.isInFront(): gamemodel.curplayer.emit("offer", Offer(action, param=param)) self.board_cids[gmwidg.board] = gmwidg.board.connect("action", emit_action, gmwidg) log.debug("Games.generalStart: -> gamemodel.setPlayers(): %s" % (gamemodel)) gamemodel.setPlayers(players) log.debug("Games.generalStart: <- gamemodel.setPlayers(): %s" % (gamemodel)) if isinstance(gamemodel, ICGameModel) and not gamemodel.ficsgame.relation == IC_POS_EXAMINATING: if not hasattr(gamemodel.ficsgame, "queue"): gamemodel.ficsgame.queue = asyncio.Queue() # Starting if loaddata: try: uri, loader, gameno, position = loaddata gamemodel.loadAndStart(uri, loader, gameno, position) if position != gamemodel.ply: gmwidg.board.view.shown = position except LoadingError as e: d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK) d.set_markup(_("<big><b>Error loading game</big></b>")) d.format_secondary_text(", ".join(str(a) for a in e.args)) d.show() d.hide() else: if gamemodel.variant.need_initial_board: for player in gamemodel.players: player.setOptionInitialBoard(gamemodel) log.debug("Games..generalStart: -> gamemodel.start(): %s" % (gamemodel)) gamemodel.start() log.debug("Games.generalStart: <- gamemodel.start(): %s" % (gamemodel)) log.debug("Games.generalStart: returning gmwidg=%s\n gamemodel=%s" % (gmwidg, gamemodel)) ################################################################################ # Saving # ################################################################################ def saveGame(self, game, position=None): if not game.isChanged(): return if game.uri and isWriteable(game.uri): self.saveGameSimple(game.uri, game, position=position) else: return self.saveGameAs(game, position=position) def saveGameSimple(self, uri, game, position=None): ending = os.path.splitext(uri)[1] if not ending: return saver = enddir[ending[1:]] game.save(uri, saver, append=False, position=position) def saveGamePGN(self, game): if conf.get("saveOwnGames", False) and not game.hasLocalPlayer(): return True filename = conf.get("autoSaveFormat", "pychess") filename = filename.replace("#n1", game.tags["White"]) filename = filename.replace("#n2", game.tags["Black"]) filename = filename.replace("#y", "%s" % game.tags["Year"]) filename = filename.replace("#m", "%s" % game.tags["Month"]) filename = filename.replace("#d", "%s" % game.tags["Day"]) pgn_path = conf.get("autoSavePath", os.path.expanduser("~")) + \ "/" + filename + ".pgn" append = True try: if not os.path.isfile(pgn_path): # create new file with open(pgn_path, "w"): pass base_offset = os.path.getsize(pgn_path) # save to .sqlite database_path = os.path.splitext(pgn_path)[0] + '.sqlite' database.save(database_path, game, base_offset) # save to .scout from pychess.Savers.pgn import scoutfish_path if scoutfish_path is not None: pgn_text = pgn.save(StringIO(), game) tmp = tempfile.NamedTemporaryFile(mode="w", delete=False) pgnfile = tmp.name with tmp.file as f: f.write(pgn_text) # create new .scout from pgnfile we are importing args = [scoutfish_path, "make", pgnfile, "%s" % base_offset] output = subprocess.check_output(args, stderr=subprocess.STDOUT) # append it to our existing one if output.decode().find(u"Processing...done") > 0: old_scout = os.path.splitext(pgn_path)[0] + '.scout' new_scout = os.path.splitext(pgnfile)[0] + '.scout' with open(old_scout, "ab") as file1, open(new_scout, "rb") as file2: file1.write(file2.read()) # TODO: do we realy want to update .bin ? It can be huge/slow! # save to .pgn game.save(pgn_path, pgn, append) return True except IOError: return False def saveGameAs(self, game, position=None, export=False): savedialog, savecombo = get_save_dialog(export) # Keep running the dialog until the user has canceled it or made an error # free operation title = _("Save Game") if not export else _("Export position") savedialog.set_title(title) while True: savedialog.set_current_name( "%s %s %s" % (game.players[0], _("vs."), game.players[1])) res = savedialog.run() if res != Gtk.ResponseType.ACCEPT: break uri = savedialog.get_filename() ending = os.path.splitext(uri)[1] if ending.startswith("."): ending = ending[1:] append = False index = savecombo.get_active() if index == 0: if ending not in enddir: d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK) folder, file = os.path.split(uri) d.set_markup(_("<big><b>Unknown file type '%s'</b></big>") % ending) d.format_secondary_text(_( "Was unable to save '%(uri)s' as PyChess doesn't know the format '%(ending)s'.") % {'uri': uri, 'ending': ending}) d.run() d.hide() continue else: saver = enddir[ending] else: format = exportformats[index] if export else saveformats[index] saver = format[2] if ending not in enddir or not saver == enddir[ending]: uri += ".%s" % saver.__ending__ if os.path.isfile(uri) and not os.access(uri, os.W_OK): d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK) d.set_markup(_("<big><b>Unable to save file '%s'</b></big>") % uri) d.format_secondary_text(_( "You don't have the necessary rights to save the file.\n\ Please ensure that you have given the right path and try again.")) d.run() d.hide() continue if os.path.isfile(uri): d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.QUESTION) d.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, _("_Replace"), Gtk.ResponseType.ACCEPT) if saver.__append__: d.add_buttons(Gtk.STOCK_ADD, 1) d.set_title(_("File exists")) folder, file = os.path.split(uri) d.set_markup(_( "<big><b>A file named '%s' already exists. Would you like to replace it?</b></big>") % file) d.format_secondary_text(_( "The file already exists in '%s'. If you replace it, its content will be overwritten.") % folder) replaceRes = d.run() d.hide() if replaceRes == 1: append = True elif replaceRes == Gtk.ResponseType.CANCEL: continue else: print(repr(uri)) try: game.save(uri, saver, append, position) except IOError as e: d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR) d.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK) d.set_title(_("Could not save the file")) d.set_markup(_( "<big><b>PyChess was not able to save the game</b></big>")) d.format_secondary_text(_("The error was: %s") % ", ".join( str(a) for a in e.args)) d.run() d.hide() continue break savedialog.destroy() return res ################################################################################ # Closing # ################################################################################ def closeAllGames(self, gamewidgets): log.debug("Games.closeAllGames") response = None changedPairs = [(gmwidg, gmwidg.gamemodel) for gmwidg in gamewidgets if gmwidg.gamemodel.isChanged()] if len(changedPairs) == 0: response = Gtk.ResponseType.OK elif len(changedPairs) == 1: response = self.closeGame(changedPairs[0][0]) else: markup = "<big><b>" + ngettext("There is %d game with unsaved moves.", "There are %d games with unsaved moves.", len(changedPairs)) % len(changedPairs) + " " + \ _("Save moves before closing?") + "</b></big>" for gmwidg, game in changedPairs: if not gmwidg.gamemodel.isChanged(): response = Gtk.ResponseType.OK else: if conf.get("autoSave", False): x = self.saveGamePGN(game) if x: response = Gtk.ResponseType.OK else: response = None markup = "<b><big>" + _("Unable to save to configured file. \ Save the games before closing?") + "</big></b>" break if response is None: widgets = GladeWidgets("saveGamesDialog.glade") dialog = widgets["saveGamesDialog"] heading = widgets["saveGamesDialogHeading"] saveLabel = widgets["saveGamesDialogSaveLabel"] treeview = widgets["saveGamesDialogTreeview"] heading.set_markup(markup) liststore = Gtk.ListStore(bool, str) treeview.set_model(liststore) renderer = Gtk.CellRendererToggle() renderer.props.activatable = True treeview.append_column(Gtk.TreeViewColumn("", renderer, active=0)) treeview.append_column(Gtk.TreeViewColumn("", Gtk.CellRendererText(), text=1)) for gmwidg, game in changedPairs: liststore.append((True, "%s %s %s" % (game.players[0], _("vs."), game.players[1]))) def callback(cell, path): if path: liststore[path][0] = not liststore[path][0] saves = len(tuple(row for row in liststore if row[0])) saveLabel.set_text(ngettext( "_Save %d document", "_Save %d documents", saves) % saves) saveLabel.set_use_underline(True) renderer.connect("toggled", callback) callback(None, None) while True: response = dialog.run() if response == Gtk.ResponseType.YES: for i in range(len(liststore) - 1, -1, -1): checked, name = liststore[i] if checked: cgmwidg, cgame = changedPairs[i] if self.saveGame(cgame) == Gtk.ResponseType.ACCEPT: liststore.remove(liststore.get_iter((i, ))) del changedPairs[i] if cgame.status in UNFINISHED_STATES: cgame.end(ABORTED, ABORTED_AGREEMENT) cgame.terminate() self.delGameWidget(cgmwidg) else: break else: break else: break dialog.destroy() if response not in (Gtk.ResponseType.DELETE_EVENT, Gtk.ResponseType.CANCEL): pairs = [(gmwidg, gmwidg.gamemodel) for gmwidg in gamewidgets] for gmwidg, game in pairs: if game.status in UNFINISHED_STATES: game.end(ABORTED, ABORTED_AGREEMENT) game.terminate() if gmwidg.notebookKey in self.key2gmwidg: self.delGameWidget(gmwidg) return response def closeGame(self, gmwidg): log.debug("Games.closeGame") response = None if not gmwidg.gamemodel.isChanged(): response = Gtk.ResponseType.OK else: markup = "<b><big>" + _("Save the current game before you close it?") + "</big></b>" if conf.get("autoSave", False): x = self.saveGamePGN(gmwidg.gamemodel) if x: response = Gtk.ResponseType.OK else: markup = "<b><big>" + _("Unable to save to configured file. \ Save the current game before you close it?") + "</big></b>" if response is None: d = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.WARNING) d.add_button(_("Close _without Saving"), Gtk.ResponseType.OK) d.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL) if gmwidg.gamemodel.uri: d.add_button(Gtk.STOCK_SAVE, Gtk.ResponseType.YES) else: d.add_button(Gtk.STOCK_SAVE_AS, Gtk.ResponseType.YES) gmwidg.bringToFront() d.set_markup(markup) d.format_secondary_text(_( "It is not possible later to continue the game,\nif you don't save it.")) response = d.run() d.destroy() if response == Gtk.ResponseType.YES: # Test if cancel was pressed in the save-file-dialog if self.saveGame(gmwidg.gamemodel) != Gtk.ResponseType.ACCEPT: response = Gtk.ResponseType.CANCEL if response not in (Gtk.ResponseType.DELETE_EVENT, Gtk.ResponseType.CANCEL): if gmwidg.gamemodel.status in UNFINISHED_STATES: gmwidg.gamemodel.end(ABORTED, ABORTED_AGREEMENT) gmwidg.disconnect(self.gmwidg_cids[gmwidg]) del self.gmwidg_cids[gmwidg] for cid in self.notify_cids[gmwidg]: conf.notify_remove(cid) del self.notify_cids[gmwidg] if gmwidg.board in self.board_cids: gmwidg.board.disconnect(self.board_cids[gmwidg.board]) del self.board_cids[gmwidg.board] self.delGameWidget(gmwidg) self.gamewidgets.remove(gmwidg) gmwidg.gamemodel.terminate() if len(self.gamewidgets) == 0: for widget in MENU_ITEMS: gamewidget.getWidgets()[widget].set_property('sensitive', False) return response def delGameWidget(self, gmwidg): """ Remove the widget from the GUI after the game has been terminated """ log.debug("Games.delGameWidget: starting %s" % repr(gmwidg)) gmwidg.closed = True gmwidg.emit("closed") called_from_preferences = False window_list = Gtk.Window.list_toplevels() widgets = gamewidget.getWidgets() for window in window_list: if window.is_active() and window == widgets["preferences"]: called_from_preferences = True break pageNum = gmwidg.getPageNumber() headbook = self.getheadbook() if gmwidg.notebookKey in self.key2gmwidg: del self.key2gmwidg[gmwidg.notebookKey] if gmwidg.notebookKey in self.key2cid: headbook.disconnect(self.key2cid[gmwidg.notebookKey]) del self.key2cid[gmwidg.notebookKey] headbook.remove_page(pageNum) for notebook in self.notebooks.values(): notebook.remove_page(pageNum) if headbook.get_n_pages() == 1 and conf.get("hideTabs", False): self.show_tabs(False) if headbook.get_n_pages() == 0: if not called_from_preferences: # If the last (but not the designGW) gmwidg was closed # and we are FICS-ing, present the FICS lounge perspective_manager.disable_perspective("games") gmwidg._del() def init_layout(self): perspective_widget = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) perspective_manager.set_perspective_widget("games", perspective_widget) self.notebooks = {"board": cleanNotebook("board"), "buttons": cleanNotebook("buttons"), "messageArea": cleanNotebook("messageArea")} for panel in sidePanels: self.notebooks[panel.__name__] = cleanNotebook(panel.__name__) # Initing headbook align = gamewidget.createAlignment(4, 4, 0, 4) align.set_property("yscale", 0) headbook = Gtk.Notebook() headbook.set_name("headbook") headbook.set_scrollable(True) align.add(headbook) perspective_widget.pack_start(align, False, True, 0) self.show_tabs(not conf.get("hideTabs", False)) # Initing center centerVBox = Gtk.VBox() # The dock self.dock = PyDockTop("main", self) self.dockAlign = gamewidget.createAlignment(4, 4, 0, 4) self.dockAlign.add(self.dock) centerVBox.pack_start(self.dockAlign, True, True, 0) self.dockAlign.show() self.dock.show() self.docks = {"board": (Gtk.Label(label="Board"), self.notebooks["board"])} for panel in sidePanels: box = dock_panel_tab(panel.__title__, panel.__desc__, panel.__icon__) self.docks[panel.__name__] = (box, self.notebooks[panel.__name__]) if os.path.isfile(dockLocation): try: self.dock.loadFromXML(dockLocation, self.docks) except Exception as e: stringio = StringIO() traceback.print_exc(file=stringio) error = stringio.getvalue() log.error("Dock loading error: %s\n%s" % (e, error)) msg_dia = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.CLOSE) msg_dia.set_markup(_( "<b><big>PyChess was unable to load your panel settings</big></b>")) msg_dia.format_secondary_text(_( "Your panel settings have been reset. If this problem repeats, \ you should report it to the developers")) msg_dia.run() msg_dia.hide() os.remove(dockLocation) for title, panel in self.docks.values(): title.unparent() panel.unparent() if not os.path.isfile(dockLocation): leaf = self.dock.dock(self.docks["board"][1], CENTER, Gtk.Label(label=self.docks["board"][0]), "board") self.docks["board"][1].show_all() leaf.setDockable(False) # S epanel = leaf.dock(self.docks["bookPanel"][1], SOUTH, self.docks["bookPanel"][0], "bookPanel") epanel.default_item_height = 45 epanel = epanel.dock(self.docks["engineOutputPanel"][1], CENTER, self.docks["engineOutputPanel"][0], "engineOutputPanel") # NE leaf = leaf.dock(self.docks["annotationPanel"][1], EAST, self.docks["annotationPanel"][0], "annotationPanel") leaf = leaf.dock(self.docks["historyPanel"][1], CENTER, self.docks["historyPanel"][0], "historyPanel") leaf = leaf.dock(self.docks["scorePanel"][1], CENTER, self.docks["scorePanel"][0], "scorePanel") # SE leaf = leaf.dock(self.docks["chatPanel"][1], SOUTH, self.docks["chatPanel"][0], "chatPanel") leaf = leaf.dock(self.docks["commentPanel"][1], CENTER, self.docks["commentPanel"][0], "commentPanel") def unrealize(dock, notebooks): # unhide the panel before saving so its configuration is saved correctly self.notebooks["board"].get_parent().get_parent().zoomDown() dock.saveToXML(dockLocation) dock._del() self.dock.connect("unrealize", unrealize, self.notebooks) hbox = Gtk.HBox() # Buttons self.notebooks["buttons"].set_border_width(4) hbox.pack_start(self.notebooks["buttons"], False, True, 0) # The message area # TODO: If you try to fix this first read issue #958 and 1018 align = gamewidget.createAlignment(0, 0, 0, 0) # sw = Gtk.ScrolledWindow() # port = Gtk.Viewport() # port.add(self.notebooks["messageArea"]) # sw.add(port) # align.add(sw) align.add(self.notebooks["messageArea"]) hbox.pack_start(align, True, True, 0) def ma_switch_page(notebook, gpointer, page_num): notebook.props.visible = notebook.get_nth_page(page_num).\ get_child().props.visible self.notebooks["messageArea"].connect("switch-page", ma_switch_page) centerVBox.pack_start(hbox, False, True, 0) perspective_widget.pack_start(centerVBox, True, True, 0) centerVBox.show_all() perspective_widget.show_all() conf.notify_add("hideTabs", self.tabsCallback) # Connecting headbook to other notebooks def hb_switch_page(notebook, gpointer, page_num): for notebook in self.notebooks.values(): notebook.set_current_page(page_num) gmwidg = self.key2gmwidg[self.getheadbook().get_nth_page(page_num)] if isinstance(gmwidg.gamemodel, ICGameModel): primary = "primary " + str(gmwidg.gamemodel.ficsgame.gameno) gmwidg.gamemodel.connection.client.run_command(primary) headbook.connect("switch-page", hb_switch_page) if hasattr(headbook, "set_tab_reorderable"): def page_reordered(widget, child, new_num, headbook): old_num = self.notebooks["board"].page_num(self.key2gmwidg[child].boardvbox) if old_num == -1: log.error('Games and labels are out of sync!') else: for notebook in self.notebooks.values(): notebook.reorder_child( notebook.get_nth_page(old_num), new_num) headbook.connect("page-reordered", page_reordered, headbook) def getheadbook(self): if len(self.key2gmwidg) == 0: return None headbook = self.widget.get_children()[0].get_children()[0].get_child() # to help StoryText create widget description # headbook.get_tab_label_text = customGetTabLabelText return headbook def cur_gmwidg(self): if len(self.key2gmwidg) == 0: return None headbook = self.getheadbook() notebookKey = headbook.get_nth_page(headbook.get_current_page()) return self.key2gmwidg[notebookKey] def customGetTabLabelText(self, child): gmwidg = self.key2gmwidg[child] return gmwidg.display_text def zoomToBoard(self, view_zoomed): if not self.notebooks["board"].get_parent(): return if view_zoomed: self.notebooks["board"].get_parent().get_parent().zoomUp() else: self.notebooks["board"].get_parent().get_parent().zoomDown() def show_tabs(self, show): head = self.getheadbook() if head is None: return head.set_show_tabs(show) def tabsCallback(self, widget): head = self.getheadbook() if not head: return if head.get_n_pages() == 1: self.show_tabs(not conf.get("hideTabs", False)) def attachGameWidget(self, gmwidg): log.debug("attachGameWidget: %s" % gmwidg) if self.first_run: self.init_layout() self.first_run = False perspective_manager.activate_perspective("games") gmwidg.panels = [panel.Sidepanel().load(gmwidg) for panel in sidePanels] self.key2gmwidg[gmwidg.notebookKey] = gmwidg headbook = self.getheadbook() headbook.append_page(gmwidg.notebookKey, gmwidg.tabcontent) gmwidg.notebookKey.show_all() if hasattr(headbook, "set_tab_reorderable"): headbook.set_tab_reorderable(gmwidg.notebookKey, True) def callback(notebook, gpointer, page_num, gmwidg): if notebook.get_nth_page(page_num) == gmwidg.notebookKey: gmwidg.infront() if gmwidg.gamemodel.players and gmwidg.gamemodel.isObservationGame(): gmwidg.light_on_off(False) text = gmwidg.game_info_label.get_text() gmwidg.game_info_label.set_markup( '<span color="black" weight="bold">%s</span>' % text) self.key2cid[gmwidg.notebookKey] = headbook.connect_after("switch-page", callback, gmwidg) gmwidg.infront() align = gamewidget.createAlignment(0, 0, 0, 0) align.show() align.add(gmwidg.infobar) self.notebooks["messageArea"].append_page(align, None) self.notebooks["board"].append_page(gmwidg.boardvbox, None) gmwidg.boardvbox.show_all() for panel, instance in zip(sidePanels, gmwidg.panels): self.notebooks[panel.__name__].append_page(instance, None) instance.show_all() self.notebooks["buttons"].append_page(gmwidg.stat_hbox, None) gmwidg.stat_hbox.show_all() if headbook.get_n_pages() == 1: self.show_tabs(not conf.get("hideTabs", False)) else: # We should always show tabs if more than one exists self.show_tabs(True) headbook.set_current_page(-1) widgets = gamewidget.getWidgets() if headbook.get_n_pages() == 1 and not widgets["show_sidepanels"].get_active(): self.zoomToBoard(True)
class Games(GObject.GObject, Perspective): __gsignals__ = { 'gmwidg_created': (GObject.SignalFlags.RUN_FIRST, None, (object, )) } def __init__(self): GObject.GObject.__init__(self) Perspective.__init__(self, "games", _("Games")) self.notebooks = {} self.first_run = True self.gamewidgets = set() self.gmwidg_cids = {} self.board_cids = {} self.notify_cids = defaultdict(list) self.key2gmwidg = {} self.key2cid = {} self.dock = None self.dockAlign = None @asyncio.coroutine def generalStart(self, gamemodel, player0tup, player1tup, loaddata=None): """ The player tuples are: (The type af player in a System.const value, A callable creating the player, A list of arguments for the callable, A preliminary name for the player) If loaddata is specified, it should be a tuple of: (A text uri or fileobj, A Savers.something module with a load function capable of loading it, An int of the game in file you want to load, The position from where to start the game) """ log.debug("Games.generalStart: %s\n %s\n %s" % (gamemodel, player0tup, player1tup)) gmwidg = gamewidget.GameWidget(gamemodel, self) self.gamewidgets.add(gmwidg) self.gmwidg_cids[gmwidg] = gmwidg.connect("game_close_clicked", self.closeGame) # worker.publish((gmwidg,gamemodel)) self.attachGameWidget(gmwidg) game_nanny.nurseGame(gmwidg, gamemodel) log.debug("Games.generalStart: -> emit gmwidg_created: %s" % (gmwidg)) self.emit("gmwidg_created", gmwidg) log.debug("Games.generalStart: <- emit gmwidg_created: %s" % (gmwidg)) # Initing players def set_name(none, player, key, alt): player.setName(conf.get(key, alt)) players = [] for i, playertup in enumerate((player0tup, player1tup)): type, func, args, prename = playertup if type != LOCAL: if type == ARTIFICIAL: player = yield from func(*args) else: player = func(*args) players.append(player) # if type == ARTIFICIAL: # def readyformoves (player, color): # gmwidg.setTabText(gmwidg.display_text)) # players[i].connect("readyForMoves", readyformoves, i) else: # Until PyChess has a proper profiles system, as discussed on the # issue tracker, we need to give human players special treatment player = func(gmwidg, *args) players.append(player) # Connect to conf if i == 0 or (i == 1 and player0tup[0] != LOCAL): key = "firstName" alt = _("You") else: key = "secondName" alt = _("Guest") if prename == conf.get(key, alt): self.notify_cids[gmwidg].append(conf.notify_add(key, set_name, player, key, alt)) if player0tup[0] == ARTIFICIAL and player1tup[0] == ARTIFICIAL: def emit_action(board, action, param, gmwidg): if gmwidg.isInFront(): gamemodel.curplayer.emit("offer", Offer(action, param=param)) self.board_cids[gmwidg.board] = gmwidg.board.connect("action", emit_action, gmwidg) log.debug("Games.generalStart: -> gamemodel.setPlayers(): %s" % (gamemodel)) gamemodel.setPlayers(players) log.debug("Games.generalStart: <- gamemodel.setPlayers(): %s" % (gamemodel)) if isinstance(gamemodel, ICGameModel) and not gamemodel.ficsgame.relation == IC_POS_EXAMINATING: if not hasattr(gamemodel.ficsgame, "queue"): gamemodel.ficsgame.queue = asyncio.Queue() # Starting if loaddata: try: uri, loader, gameno, position = loaddata gamemodel.loadAndStart(uri, loader, gameno, position) if position != gamemodel.ply: gmwidg.board.view.shown = position except LoadingError as e: d = Gtk.MessageDialog(type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK) d.set_markup(_("<big><b>Error loading game</big></b>")) d.format_secondary_text(", ".join(str(a) for a in e.args)) d.show() d.hide() else: if gamemodel.variant.need_initial_board: for player in gamemodel.players: player.setOptionInitialBoard(gamemodel) log.debug("Games..generalStart: -> gamemodel.start(): %s" % (gamemodel)) gamemodel.start() log.debug("Games.generalStart: <- gamemodel.start(): %s" % (gamemodel)) log.debug("Games.generalStart: returning gmwidg=%s\n gamemodel=%s" % (gmwidg, gamemodel)) ################################################################################ # Saving # ################################################################################ def saveGame(self, game, position=None): if not game.isChanged(): return if game.uri and isWriteable(game.uri): self.saveGameSimple(game.uri, game, position=position) else: return self.saveGameAs(game, position=position) def saveGameSimple(self, uri, game, position=None): ending = os.path.splitext(uri)[1] if not ending: return saver = enddir[ending[1:]] game.save(uri, saver, append=False, position=position) def saveGamePGN(self, game): if conf.get("saveOwnGames", False) and not game.hasLocalPlayer(): return True filename = conf.get("autoSaveFormat", "pychess") filename = filename.replace("#n1", game.tags["White"]) filename = filename.replace("#n2", game.tags["Black"]) filename = filename.replace("#y", "%s" % game.tags["Year"]) filename = filename.replace("#m", "%s" % game.tags["Month"]) filename = filename.replace("#d", "%s" % game.tags["Day"]) pgn_path = conf.get("autoSavePath", os.path.expanduser("~")) + \ "/" + filename + ".pgn" append = True try: if not os.path.isfile(pgn_path): # create new file with open(pgn_path, "w"): pass base_offset = os.path.getsize(pgn_path) # save to .sqlite database_path = os.path.splitext(pgn_path)[0] + '.sqlite' database.save(database_path, game, base_offset) # save to .scout from pychess.Savers.pgn import scoutfish_path if scoutfish_path is not None: pgn_text = pgn.save(StringIO(), game) tmp = tempfile.NamedTemporaryFile(mode="w", delete=False) pgnfile = tmp.name with tmp.file as f: f.write(pgn_text) # create new .scout from pgnfile we are importing args = [scoutfish_path, "make", pgnfile, "%s" % base_offset] output = subprocess.check_output(args, stderr=subprocess.STDOUT) # append it to our existing one if output.decode().find(u"Processing...done") > 0: old_scout = os.path.splitext(pgn_path)[0] + '.scout' new_scout = os.path.splitext(pgnfile)[0] + '.scout' with open(old_scout, "ab") as file1, open(new_scout, "rb") as file2: file1.write(file2.read()) # TODO: do we realy want to update .bin ? It can be huge/slow! # save to .pgn game.save(pgn_path, pgn, append) return True except IOError: return False def saveGameAs(self, game, position=None, export=False): savedialog, savecombo = get_save_dialog(export) # Keep running the dialog until the user has canceled it or made an error # free operation title = _("Save Game") if not export else _("Export position") savedialog.set_title(title) while True: savedialog.set_current_name( "%s %s %s" % (game.players[0], _("vs."), game.players[1])) res = savedialog.run() if res != Gtk.ResponseType.ACCEPT: break uri = savedialog.get_filename() ending = os.path.splitext(uri)[1] if ending.startswith("."): ending = ending[1:] append = False index = savecombo.get_active() if index == 0: if ending not in enddir: d = Gtk.MessageDialog(type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK) folder, file = os.path.split(uri) d.set_markup(_("<big><b>Unknown file type '%s'</b></big>") % ending) d.format_secondary_text(_( "Was unable to save '%(uri)s' as PyChess doesn't know the format '%(ending)s'.") % {'uri': uri, 'ending': ending}) d.run() d.hide() continue else: saver = enddir[ending] else: format = exportformats[index] if export else saveformats[index] saver = format[2] if ending not in enddir or not saver == enddir[ending]: uri += ".%s" % saver.__ending__ if os.path.isfile(uri) and not os.access(uri, os.W_OK): d = Gtk.MessageDialog(type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK) d.set_markup(_("<big><b>Unable to save file '%s'</b></big>") % uri) d.format_secondary_text(_( "You don't have the necessary rights to save the file.\n\ Please ensure that you have given the right path and try again.")) d.run() d.hide() continue if os.path.isfile(uri): d = Gtk.MessageDialog(type=Gtk.MessageType.QUESTION) d.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, _("_Replace"), Gtk.ResponseType.ACCEPT) if saver.__append__: d.add_buttons(Gtk.STOCK_ADD, 1) d.set_title(_("File exists")) folder, file = os.path.split(uri) d.set_markup(_( "<big><b>A file named '%s' already exists. Would you like to replace it?</b></big>") % file) d.format_secondary_text(_( "The file already exists in '%s'. If you replace it, its content will be overwritten.") % folder) replaceRes = d.run() d.hide() if replaceRes == 1: append = True elif replaceRes == Gtk.ResponseType.CANCEL: continue else: print(repr(uri)) try: game.save(uri, saver, append, position) except IOError as e: d = Gtk.MessageDialog(type=Gtk.MessageType.ERROR) d.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK) d.set_title(_("Could not save the file")) d.set_markup(_( "<big><b>PyChess was not able to save the game</b></big>")) d.format_secondary_text(_("The error was: %s") % ", ".join( str(a) for a in e.args)) d.run() d.hide() continue break savedialog.destroy() return res ################################################################################ # Closing # ################################################################################ def closeAllGames(self, gamewidgets): log.debug("Games.closeAllGames") response = None changedPairs = [(gmwidg, gmwidg.gamemodel) for gmwidg in gamewidgets if gmwidg.gamemodel.isChanged()] if len(changedPairs) == 0: response = Gtk.ResponseType.OK elif len(changedPairs) == 1: response = self.closeGame(changedPairs[0][0]) else: markup = "<big><b>" + ngettext("There is %d game with unsaved moves.", "There are %d games with unsaved moves.", len(changedPairs)) % len(changedPairs) + " " + \ _("Save moves before closing?") + "</b></big>" for gmwidg, game in changedPairs: if not gmwidg.gamemodel.isChanged(): response = Gtk.ResponseType.OK else: if conf.get("autoSave", False): x = self.saveGamePGN(game) if x: response = Gtk.ResponseType.OK else: response = None markup = "<b><big>" + _("Unable to save to configured file. \ Save the games before closing?") + "</big></b>" break if response is None: widgets = GladeWidgets("saveGamesDialog.glade") dialog = widgets["saveGamesDialog"] heading = widgets["saveGamesDialogHeading"] saveLabel = widgets["saveGamesDialogSaveLabel"] treeview = widgets["saveGamesDialogTreeview"] heading.set_markup(markup) liststore = Gtk.ListStore(bool, str) treeview.set_model(liststore) renderer = Gtk.CellRendererToggle() renderer.props.activatable = True treeview.append_column(Gtk.TreeViewColumn("", renderer, active=0)) treeview.append_column(Gtk.TreeViewColumn("", Gtk.CellRendererText(), text=1)) for gmwidg, game in changedPairs: liststore.append((True, "%s %s %s" % (game.players[0], _("vs."), game.players[1]))) def callback(cell, path): if path: liststore[path][0] = not liststore[path][0] saves = len(tuple(row for row in liststore if row[0])) saveLabel.set_text(ngettext( "_Save %d document", "_Save %d documents", saves) % saves) saveLabel.set_use_underline(True) renderer.connect("toggled", callback) callback(None, None) while True: response = dialog.run() if response == Gtk.ResponseType.YES: for i in range(len(liststore) - 1, -1, -1): checked, name = liststore[i] if checked: cgmwidg, cgame = changedPairs[i] if self.saveGame(cgame) == Gtk.ResponseType.ACCEPT: liststore.remove(liststore.get_iter((i, ))) del changedPairs[i] if cgame.status in UNFINISHED_STATES: cgame.end(ABORTED, ABORTED_AGREEMENT) cgame.terminate() self.delGameWidget(cgmwidg) else: break else: break else: break dialog.destroy() if response not in (Gtk.ResponseType.DELETE_EVENT, Gtk.ResponseType.CANCEL): pairs = [(gmwidg, gmwidg.gamemodel) for gmwidg in gamewidgets] for gmwidg, game in pairs: if game.status in UNFINISHED_STATES: game.end(ABORTED, ABORTED_AGREEMENT) game.terminate() if gmwidg.notebookKey in self.key2gmwidg: self.delGameWidget(gmwidg) return response def closeGame(self, gmwidg): log.debug("Games.closeGame") response = None if not gmwidg.gamemodel.isChanged(): response = Gtk.ResponseType.OK else: markup = "<b><big>" + _("Save the current game before you close it?") + "</big></b>" if conf.get("autoSave", False): x = self.saveGamePGN(gmwidg.gamemodel) if x: response = Gtk.ResponseType.OK else: markup = "<b><big>" + _("Unable to save to configured file. \ Save the current game before you close it?") + "</big></b>" if response is None: d = Gtk.MessageDialog(type=Gtk.MessageType.WARNING) d.add_button(_("Close _without Saving"), Gtk.ResponseType.OK) d.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL) if gmwidg.gamemodel.uri: d.add_button(Gtk.STOCK_SAVE, Gtk.ResponseType.YES) else: d.add_button(Gtk.STOCK_SAVE_AS, Gtk.ResponseType.YES) gmwidg.bringToFront() d.set_markup(markup) d.format_secondary_text(_( "It is not possible later to continue the game,\nif you don't save it.")) response = d.run() d.destroy() if response == Gtk.ResponseType.YES: # Test if cancel was pressed in the save-file-dialog if self.saveGame(gmwidg.gamemodel) != Gtk.ResponseType.ACCEPT: response = Gtk.ResponseType.CANCEL if response not in (Gtk.ResponseType.DELETE_EVENT, Gtk.ResponseType.CANCEL): if gmwidg.gamemodel.status in UNFINISHED_STATES: gmwidg.gamemodel.end(ABORTED, ABORTED_AGREEMENT) gmwidg.disconnect(self.gmwidg_cids[gmwidg]) del self.gmwidg_cids[gmwidg] for cid in self.notify_cids[gmwidg]: conf.notify_remove(cid) del self.notify_cids[gmwidg] if gmwidg.board in self.board_cids: gmwidg.board.disconnect(self.board_cids[gmwidg.board]) del self.board_cids[gmwidg.board] self.delGameWidget(gmwidg) self.gamewidgets.remove(gmwidg) gmwidg.gamemodel.terminate() if len(self.gamewidgets) == 0: for widget in MENU_ITEMS: gamewidget.getWidgets()[widget].set_property('sensitive', False) return response def delGameWidget(self, gmwidg): """ Remove the widget from the GUI after the game has been terminated """ log.debug("Games.delGameWidget: starting %s" % repr(gmwidg)) gmwidg.closed = True gmwidg.emit("closed") called_from_preferences = False window_list = Gtk.Window.list_toplevels() widgets = gamewidget.getWidgets() for window in window_list: if window.is_active() and window == widgets["preferences"]: called_from_preferences = True break pageNum = gmwidg.getPageNumber() headbook = self.getheadbook() if gmwidg.notebookKey in self.key2gmwidg: del self.key2gmwidg[gmwidg.notebookKey] if gmwidg.notebookKey in self.key2cid: headbook.disconnect(self.key2cid[gmwidg.notebookKey]) del self.key2cid[gmwidg.notebookKey] headbook.remove_page(pageNum) for notebook in self.notebooks.values(): notebook.remove_page(pageNum) if headbook.get_n_pages() == 1 and conf.get("hideTabs", False): self.show_tabs(False) if headbook.get_n_pages() == 0: if not called_from_preferences: # If the last (but not the designGW) gmwidg was closed # and we are FICS-ing, present the FICS lounge perspective_manager.disable_perspective("games") gmwidg._del() def init_layout(self): perspective_widget = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) perspective_manager.set_perspective_widget("games", perspective_widget) self.notebooks = {"board": cleanNotebook("board"), "buttons": cleanNotebook("buttons"), "messageArea": cleanNotebook("messageArea")} for panel in sidePanels: self.notebooks[panel.__name__] = cleanNotebook(panel.__name__) # Initing headbook align = gamewidget.createAlignment(4, 4, 0, 4) align.set_property("yscale", 0) headbook = Gtk.Notebook() headbook.set_name("headbook") headbook.set_scrollable(True) align.add(headbook) perspective_widget.pack_start(align, False, True, 0) self.show_tabs(not conf.get("hideTabs", False)) # Initing center centerVBox = Gtk.VBox() # The dock self.dock = PyDockTop("main", self) self.dockAlign = gamewidget.createAlignment(4, 4, 0, 4) self.dockAlign.add(self.dock) centerVBox.pack_start(self.dockAlign, True, True, 0) self.dockAlign.show() self.dock.show() self.docks = {"board": (Gtk.Label(label="Board"), self.notebooks["board"])} for panel in sidePanels: box = dock_panel_tab(panel.__title__, panel.__desc__, panel.__icon__) self.docks[panel.__name__] = (box, self.notebooks[panel.__name__]) if os.path.isfile(dockLocation): try: self.dock.loadFromXML(dockLocation, self.docks) except Exception as e: stringio = StringIO() traceback.print_exc(file=stringio) error = stringio.getvalue() log.error("Dock loading error: %s\n%s" % (e, error)) widgets = gamewidget.getWidgets() msg_dia = Gtk.MessageDialog(widgets["main_window"], type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.CLOSE) msg_dia.set_markup(_( "<b><big>PyChess was unable to load your panel settings</big></b>")) msg_dia.format_secondary_text(_( "Your panel settings have been reset. If this problem repeats, \ you should report it to the developers")) msg_dia.run() msg_dia.hide() os.remove(dockLocation) for title, panel in self.docks.values(): title.unparent() panel.unparent() if not os.path.isfile(dockLocation): leaf = self.dock.dock(self.docks["board"][1], CENTER, Gtk.Label(label=self.docks["board"][0]), "board") self.docks["board"][1].show_all() leaf.setDockable(False) # S epanel = leaf.dock(self.docks["bookPanel"][1], SOUTH, self.docks["bookPanel"][0], "bookPanel") epanel.default_item_height = 45 epanel = epanel.dock(self.docks["engineOutputPanel"][1], CENTER, self.docks["engineOutputPanel"][0], "engineOutputPanel") # NE leaf = leaf.dock(self.docks["annotationPanel"][1], EAST, self.docks["annotationPanel"][0], "annotationPanel") leaf = leaf.dock(self.docks["historyPanel"][1], CENTER, self.docks["historyPanel"][0], "historyPanel") leaf = leaf.dock(self.docks["scorePanel"][1], CENTER, self.docks["scorePanel"][0], "scorePanel") # SE leaf = leaf.dock(self.docks["chatPanel"][1], SOUTH, self.docks["chatPanel"][0], "chatPanel") leaf = leaf.dock(self.docks["commentPanel"][1], CENTER, self.docks["commentPanel"][0], "commentPanel") def unrealize(dock, notebooks): # unhide the panel before saving so its configuration is saved correctly self.notebooks["board"].get_parent().get_parent().zoomDown() dock.saveToXML(dockLocation) dock._del() self.dock.connect("unrealize", unrealize, self.notebooks) hbox = Gtk.HBox() # Buttons self.notebooks["buttons"].set_border_width(4) hbox.pack_start(self.notebooks["buttons"], False, True, 0) # The message area # TODO: If you try to fix this first read issue #958 and 1018 align = gamewidget.createAlignment(0, 0, 0, 0) # sw = Gtk.ScrolledWindow() # port = Gtk.Viewport() # port.add(self.notebooks["messageArea"]) # sw.add(port) # align.add(sw) align.add(self.notebooks["messageArea"]) hbox.pack_start(align, True, True, 0) def ma_switch_page(notebook, gpointer, page_num): notebook.props.visible = notebook.get_nth_page(page_num).\ get_child().props.visible self.notebooks["messageArea"].connect("switch-page", ma_switch_page) centerVBox.pack_start(hbox, False, True, 0) perspective_widget.pack_start(centerVBox, True, True, 0) centerVBox.show_all() perspective_widget.show_all() conf.notify_add("hideTabs", self.tabsCallback) # Connecting headbook to other notebooks def hb_switch_page(notebook, gpointer, page_num): for notebook in self.notebooks.values(): notebook.set_current_page(page_num) gmwidg = self.key2gmwidg[self.getheadbook().get_nth_page(page_num)] if isinstance(gmwidg.gamemodel, ICGameModel): primary = "primary " + str(gmwidg.gamemodel.ficsgame.gameno) gmwidg.gamemodel.connection.client.run_command(primary) headbook.connect("switch-page", hb_switch_page) if hasattr(headbook, "set_tab_reorderable"): def page_reordered(widget, child, new_num, headbook): old_num = self.notebooks["board"].page_num(self.key2gmwidg[child].boardvbox) if old_num == -1: log.error('Games and labels are out of sync!') else: for notebook in self.notebooks.values(): notebook.reorder_child( notebook.get_nth_page(old_num), new_num) headbook.connect("page-reordered", page_reordered, headbook) def getheadbook(self): if len(self.key2gmwidg) == 0: return None headbook = self.widget.get_children()[0].get_children()[0].get_child() # to help StoryText create widget description # headbook.get_tab_label_text = customGetTabLabelText return headbook def cur_gmwidg(self): if len(self.key2gmwidg) == 0: return None headbook = self.getheadbook() notebookKey = headbook.get_nth_page(headbook.get_current_page()) return self.key2gmwidg[notebookKey] def customGetTabLabelText(self, child): gmwidg = self.key2gmwidg[child] return gmwidg.display_text def zoomToBoard(self, view_zoomed): if not self.notebooks["board"].get_parent(): return if view_zoomed: self.notebooks["board"].get_parent().get_parent().zoomUp() else: self.notebooks["board"].get_parent().get_parent().zoomDown() def show_tabs(self, show): head = self.getheadbook() if head is None: return head.set_show_tabs(show) def tabsCallback(self, widget): head = self.getheadbook() if not head: return if head.get_n_pages() == 1: self.show_tabs(not conf.get("hideTabs", False)) def attachGameWidget(self, gmwidg): log.debug("attachGameWidget: %s" % gmwidg) if self.first_run: self.init_layout() self.first_run = False perspective_manager.activate_perspective("games") gmwidg.panels = [panel.Sidepanel().load(gmwidg) for panel in sidePanels] self.key2gmwidg[gmwidg.notebookKey] = gmwidg headbook = self.getheadbook() headbook.append_page(gmwidg.notebookKey, gmwidg.tabcontent) gmwidg.notebookKey.show_all() if hasattr(headbook, "set_tab_reorderable"): headbook.set_tab_reorderable(gmwidg.notebookKey, True) def callback(notebook, gpointer, page_num, gmwidg): if notebook.get_nth_page(page_num) == gmwidg.notebookKey: gmwidg.infront() if gmwidg.gamemodel.players and gmwidg.gamemodel.isObservationGame(): gmwidg.light_on_off(False) text = gmwidg.game_info_label.get_text() gmwidg.game_info_label.set_markup( '<span color="black" weight="bold">%s</span>' % text) self.key2cid[gmwidg.notebookKey] = headbook.connect_after("switch-page", callback, gmwidg) gmwidg.infront() align = gamewidget.createAlignment(0, 0, 0, 0) align.show() align.add(gmwidg.infobar) self.notebooks["messageArea"].append_page(align, None) self.notebooks["board"].append_page(gmwidg.boardvbox, None) gmwidg.boardvbox.show_all() for panel, instance in zip(sidePanels, gmwidg.panels): self.notebooks[panel.__name__].append_page(instance, None) instance.show_all() self.notebooks["buttons"].append_page(gmwidg.stat_hbox, None) gmwidg.stat_hbox.show_all() if headbook.get_n_pages() == 1: self.show_tabs(not conf.get("hideTabs", False)) else: # We should always show tabs if more than one exists self.show_tabs(True) headbook.set_current_page(-1) widgets = gamewidget.getWidgets() if headbook.get_n_pages() == 1 and not widgets["show_sidepanels"].get_active(): self.zoomToBoard(True)