Public repository: timo / schach

Reader

Ein aufgeräumtes Schach-Programm für GNOME

feat(movelist): context menu, delete variations with Del

public

feat(movelist): context menu, delete variations with Del

Right-click: continue from here, copy FEN, delete (variations
only; deleting a move removes everything after it).
Changeset613dd40fd1273ab939ac8454139b356db81f92f7
Author (claimed)Timo Stolz <timo.stolz@nullachtvierzehn.de>,
Parentsb5a695421283
Childrenc41b081c2c01
Pushlog
by timo via ssh
by timo via ssh

modified schach/app.py +3 −0

@@ -341,6 +341,9 @@ class Window(OnlineMixin, PuzzleMixin, O
341341
         self.movelist.on_copy = self._copy_fen
342342
         self.movelist.on_continue = self._continue_from
343343
         self.movelist.on_why_not = self._why_not_from_list
344
+        self.movelist.on_variation_delete = self._variation_delete
345
+        self.movelist.on_variation_continue = self._variation_continue
346
+        self.movelist.on_variation_copy = self._variation_copy
344347
         self.movelist.on_variation = self._on_list_variation
345348
         self.view.on_shapes = self._drawn
346349
 

modified schach/movelist.py +69 −2

@@ -10,7 +10,7 @@ import chess
1010
 import gi
1111
 
1212
 gi.require_version("Gtk", "4.0")
13
-from gi.repository import Gdk, GLib, Gtk  # noqa: E402
13
+from gi.repository import Gdk, Gio, GLib, Gtk  # noqa: E402
1414
 
1515
 from . import notation  # noqa: E402
1616
 from .board import BADGE  # noqa: E402
@@ -58,7 +58,11 @@ class MoveList(Gtk.Box):
5858
         self.on_copy = None  # callback(halbzug)
5959
         self.on_continue = None  # callback(halbzug)
6060
         self.on_why_not = None  # callback(halbzug): „Warum nicht?“ bei ?!, ? und ??
61
-        self.on_variation = None  # callback(nummer): Zug einer Nebenvariante vom Analysebrett angeklickt
61
+        self.on_variation = None  # callback(nummer): Zug einer Nebenvariante angeklickt
62
+        # Kontextmenü (Rechtsklick) an Variantenzügen; Entf löscht ebenfalls
63
+        self.on_variation_delete = None  # callback(nummer): diesen Zug samt allem danach löschen
64
+        self.on_variation_continue = None  # callback(nummer): ab hier gegen Maia weiterspielen
65
+        self.on_variation_copy = None  # callback(nummer): Stellung als FEN kopieren
6266
 
6367
         header = Gtk.Box(spacing=6, margin_start=12, margin_end=6, margin_top=6, margin_bottom=6)
6468
         header.append(Gtk.Box(hexpand=True))  # Überschrift steht schon im Umschalter „Partie | Buch | Lichess“
@@ -74,6 +78,18 @@ class MoveList(Gtk.Box):
7478
         keys = Gtk.EventControllerKey()  # ← und → klappen auf und zu; sonst weiter ans Fenster (Brett)
7579
         keys.connect("key-pressed", self._on_key)
7680
         self.listbox.add_controller(keys)
81
+        menu_click = Gtk.GestureClick(button=Gdk.BUTTON_SECONDARY)
82
+        menu_click.connect("pressed", self._on_menu_click)
83
+        self.listbox.add_controller(menu_click)
84
+        self._menu_row = None  # Zeile, zu der das Kontextmenü gerade offen ist
85
+        actions = Gio.SimpleActionGroup()
86
+        for name, handler in (("continue", self._menu_continue), ("copy", self._menu_copy),
87
+                              ("delete", self._menu_delete)):
88
+            action = Gio.SimpleAction.new(name, None)
89
+            action.connect("activate", lambda _a, _p, handler=handler: handler())
90
+            actions.add_action(action)
91
+        self.insert_action_group("zug", actions)
92
+        self.menu_actions = actions
7793
         self.scroller = Gtk.ScrolledWindow(vexpand=True, hscrollbar_policy=Gtk.PolicyType.NEVER)
7894
         self.scroller.set_child(self.listbox)
7995
         self.append(self.scroller)
@@ -267,9 +283,60 @@ class MoveList(Gtk.Box):
267283
     def _children(self, row):
268284
         return [r for r in self._variation_rows if r.parent_row is row]
269285
 
286
+    # --- Kontextmenü -------------------------------------------------------
287
+
288
+    def _on_menu_click(self, gesture, _n, x, y):
289
+        row = self.listbox.get_row_at_y(int(y))
290
+        if row is None or (row.ply is None and row.variation is None):
291
+            return  # Kopfzeile „Variante n“
292
+        gesture.set_state(Gtk.EventSequenceState.CLAIMED)
293
+        self._menu_row = row
294
+        menu = Gio.Menu()
295
+        menu.append("Ab hier _weiterspielen", "zug.continue")
296
+        menu.append("Stellung als _FEN kopieren", "zug.copy")
297
+        if row.variation is not None:  # Züge der Partie bleiben; löschen kann man nur Varianten
298
+            menu.append("Ab hier _löschen (Entf)", "zug.delete")
299
+        popover = Gtk.PopoverMenu(menu_model=menu, has_arrow=False, halign=Gtk.Align.START)
300
+        popover.set_parent(self.listbox)
301
+        rect = Gdk.Rectangle()
302
+        rect.x, rect.y, rect.width, rect.height = int(x), int(y), 1, 1
303
+        popover.set_pointing_to(rect)
304
+        popover.connect("closed", lambda p: GLib.idle_add(p.unparent))
305
+        popover.popup()
306
+
307
+    def _menu_target(self):
308
+        row, self._menu_row = self._menu_row, None
309
+        return row
310
+
311
+    def _menu_continue(self):
312
+        row = self._menu_target()
313
+        if row is not None and row.variation is not None and self.on_variation_continue:
314
+            GLib.idle_add(lambda: self.on_variation_continue(row.variation) and False)
315
+        elif row is not None and row.ply is not None and self.on_continue:
316
+            GLib.idle_add(lambda: self.on_continue(row.ply) and False)
317
+
318
+    def _menu_copy(self):
319
+        row = self._menu_target()
320
+        if row is not None and row.variation is not None and self.on_variation_copy:
321
+            self.on_variation_copy(row.variation)
322
+        elif row is not None and row.ply is not None and self.on_copy:
323
+            self.on_copy(row.ply)
324
+
325
+    def _menu_delete(self, row=None):
326
+        row = row or self._menu_target()
327
+        if row is not None and row.variation is not None and self.on_variation_delete:
328
+            token = row.variation
329
+            GLib.idle_add(lambda: self.on_variation_delete(token) and False)  # baut die Liste neu
330
+
270331
     def _on_key(self, _controller, keyval, _keycode, state):
271332
         if state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.ALT_MASK | Gdk.ModifierType.SHIFT_MASK):
272333
             return False
334
+        if keyval in (Gdk.KEY_Delete, Gdk.KEY_KP_Delete):
335
+            row = self.listbox.get_selected_row()
336
+            if row is not None and row.variation is not None:
337
+                self._menu_delete(row)
338
+                return True
339
+            return False
273340
         row = self.listbox.get_selected_row()
274341
         if row is None or keyval not in (Gdk.KEY_Right, Gdk.KEY_Left):
275342
             return False

modified schach/training.py +41 −1

@@ -648,8 +648,48 @@ class OpeningMixin:
648648
         node = self.demo["node"] if self.demo is not None and self.demo.get("game") else None
649649
         if node is None or self.demo["busy"]:
650650
             return
651
+        self._continue_node(node)
652
+
653
+    def _list_node(self, token):
654
+        """Knoten des Partiebaums zu einer Nummer aus der Zugliste (siehe _list_variations)."""
655
+        return getattr(self, "list_nodes", {}).get(token)
656
+
657
+    def _variation_continue(self, token):
658
+        node = self._list_node(token)
659
+        if node is not None:
660
+            self._continue_node(node)
661
+
662
+    def _variation_copy(self, token):
663
+        node = self._list_node(token)
664
+        if node is not None:
665
+            self.get_clipboard().set(node.board().fen())
666
+            self.toasts.add_toast(Adw.Toast(title="FEN kopiert", timeout=2))
667
+
668
+    def _variation_delete(self, token):
669
+        """Variantenzug samt allem danach löschen (Züge der Partie bleiben)."""
670
+        node, tree = self._list_node(token), self.game_tree
671
+        if node is None or tree is None or tree.meta.get(node, {}).get("played") or node.parent is None:
672
+            return
673
+        if self.demo is not None and self.demo["busy"]:
674
+            return
675
+        parent = node.parent
676
+        if self.demo is not None and self.demo.get("game"):
677
+            step = self.demo["node"]
678
+            while step is not None and step is not node:
679
+                step = step.parent
680
+            if step is node:
681
+                self.demo["node"] = parent  # das Brett stand im gelöschten Teil
682
+            if self.demo["chosen"].get(parent) is node:
683
+                del self.demo["chosen"][parent]
684
+        parent.remove_variation(node)
685
+        self._schedule_autosave()
686
+        self._refresh()
687
+        self._coach_show()
688
+
689
+    def _continue_node(self, node):
690
+        """Gegen Maia ab der Stellung an ``node`` weiterspielen, in einem neuen Fenster."""
651691
         board = node.board()
652
-        if self._demo_real(node) and node.parent is not None:
692
+        if node.parent is not None and self.game_tree.meta.get(node, {}).get("played"):
653693
             state = self.state().truncated(self._game_ply(node))  # mit Zeichen, Bewertung, Varianten
654694
         else:
655695
             state = savegames.GameState(board=board, human=self.human, elo=0)