From 9057860302f5713a65fcbbe952d8819346ef4b50 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Tue, 25 Apr 2023 16:16:12 +0200
Subject: [PATCH] geometry: Fix delete and paste command.

---
 src/Model/Geometry/Reach.py           | 39 ++++++++++++++++++++++++---
 src/View/Geometry/ReachUndoCommand.py | 15 ++++++-----
 2 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/src/Model/Geometry/Reach.py b/src/Model/Geometry/Reach.py
index 4ef27de1..26bb2468 100644
--- a/src/Model/Geometry/Reach.py
+++ b/src/Model/Geometry/Reach.py
@@ -99,18 +99,51 @@ class Reach:
         self._update_profile_numbers()
 
 
-    def delete(self, index: int):
+    def delete(self, indexes):
         """Delete some elements in profile list
 
         Args:
-            index: The index to delete
+            indexes: The list of index to delete
 
         Returns:
             Nothing.
         """
-        self._profiles.pop(index)
+        profiles = set(
+            map(
+                lambda e: e[1],
+                filter(
+                    lambda e: e[0] in indexes,
+                    enumerate(self.profiles)
+                )
+            )
+        )
+
+        self._profiles = list(
+            filter(
+                lambda p: p not in profiles,
+                self.profiles
+            )
+        )
         self._update_profile_numbers()
 
+    def delete_profiles(self, profiles):
+        """Delete some elements in profile list
+
+        Args:
+            profiles: The list of profile to delete
+
+        Returns:
+            Nothing.
+        """
+        self._profiles = list(
+            filter(
+                lambda p: p not in profiles,
+                self.profiles
+            )
+        )
+        self._update_profile_numbers()
+
+
     def move_up_profile(self, index: int):
         if index < len(self.profiles):
             next = index - 1
diff --git a/src/View/Geometry/ReachUndoCommand.py b/src/View/Geometry/ReachUndoCommand.py
index c783587b..62e99203 100644
--- a/src/View/Geometry/ReachUndoCommand.py
+++ b/src/View/Geometry/ReachUndoCommand.py
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+from copy import deepcopy
 from tools import trace, timer
 
 from PyQt5.QtWidgets import (
@@ -64,9 +65,7 @@ class DelCommand(QUndoCommand):
             self._reach.insert_profile(row, pro)
 
     def redo(self):
-        row = self._rows[0]
-        for _ in self._rows:
-            self._reach.delete(row)
+        self._reach.delete(self._rows)
 
 class SortCommand(QUndoCommand):
     def __init__(self, reach, _reverse):
@@ -120,11 +119,15 @@ class PasteCommand(QUndoCommand):
 
         self._reach = reach
         self._row = row
-        self._profiles = profiles
+        self._profiles = list(
+            map(
+                lambda p: deepcopy(p),
+                profiles
+            )
+        )
 
     def undo(self):
-        for ind in range(len(self._profiles)):
-            self._reach.delete(self._row)
+        self._reach.delete_profiles(self._profiles)
 
     def redo(self):
         self._profiles.reverse()
-- 
GitLab