From 16742b0319c49e118c99978da2278f82a3d476d6 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Wed, 31 May 2023 14:42:48 +0200
Subject: [PATCH] IC: Add combobox to match kp value on geometry profile kp and
 fix sort command.

---
 .../InitialConditions/InitialConditions.py    | 10 +++-
 src/View/InitialConditions/Table.py           | 54 +++++++++++++++++--
 src/View/InitialConditions/UndoCommand.py     |  2 +-
 src/View/InitialConditions/Window.py          | 12 ++++-
 4 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py
index 83665f2b..142f0996 100644
--- a/src/Model/InitialConditions/InitialConditions.py
+++ b/src/Model/InitialConditions/InitialConditions.py
@@ -4,11 +4,13 @@ from copy import copy
 from tools import trace, timer
 
 class Data(object):
-    def __init__(self, status = None):
+    def __init__(self, reach = None, status = None):
         super(Data, self).__init__()
 
         self._status = status
 
+        self._reach = reach
+
         self._name = ""
         self._comment = ""
 
@@ -18,6 +20,10 @@ class Data(object):
         self._elevation = 0.0
         self._draft = 0.0
 
+    @property
+    def name(self):
+        return self._name
+
     def __getitem__(self, key):
         val = None
 
@@ -89,7 +95,7 @@ class InitialConditions(object):
         self._status.modified()
 
     def new(self, index):
-        n = Data(self._status)
+        n = Data(reach = self._reach, status = self._status)
         self._data.insert(index, n)
         self._status.modified()
 
diff --git a/src/View/InitialConditions/Table.py b/src/View/InitialConditions/Table.py
index 525a898b..d969ee51 100644
--- a/src/View/InitialConditions/Table.py
+++ b/src/View/InitialConditions/Table.py
@@ -25,6 +25,48 @@ from View.InitialConditions.translate import *
 
 _translate = QCoreApplication.translate
 
+class ComboBoxDelegate(QItemDelegate):
+    def __init__(self, reach=None, parent=None):
+        super(ComboBoxDelegate, self).__init__(parent)
+
+        self._reach = reach.reach
+
+    def createEditor(self, parent, option, index):
+        self.editor = QComboBox(parent)
+
+        self.editor.addItems(
+            list(
+                map(
+                    str,
+                    self._reach.get_kp()
+                )
+            )
+        )
+
+        self.editor.setCurrentText(str(index.data(Qt.DisplayRole)))
+        return self.editor
+
+    def setEditorData(self, editor, index):
+        value = index.data(Qt.DisplayRole)
+        self.editor.currentTextChanged.connect(self.currentItemChanged)
+
+    def setModelData(self, editor, model, index):
+        text = str(editor.currentText())
+        model.setData(index, text)
+        editor.close()
+        editor.deleteLater()
+
+    def updateEditorGeometry(self, editor, option, index):
+        r = QRect(option.rect)
+        if self.editor.windowFlags() & Qt.Popup and editor.parent() is not None:
+            r.setTopLeft(self.editor.parent().mapToGlobal(r.topLeft()))
+        editor.setGeometry(r)
+
+    @pyqtSlot()
+    def currentItemChanged(self):
+        self.commitData.emit(self.sender())
+
+
 class TableModel(QAbstractTableModel):
     def __init__(self, river=None, reach=None, undo=None):
         super(QAbstractTableModel, self).__init__()
@@ -35,8 +77,11 @@ class TableModel(QAbstractTableModel):
         self._ics = self._river.initial_conditions.get(reach)
 
     def flags(self, index):
-        options = Qt.ItemIsEnabled | Qt.ItemIsSelectable
-        options |= Qt.ItemIsEditable
+        column = index.column()
+
+        options = Qt.ItemIsSelectable
+        if self._headers[column] != "speed":
+            options |= Qt.ItemIsEnabled | Qt.ItemIsEditable
 
         return options
 
@@ -53,7 +98,10 @@ class TableModel(QAbstractTableModel):
         row = index.row()
         column = index.column()
 
-        if self._headers[column] is not None:
+        if self._headers[column] not in ["name", "comment"]:
+            v = self._ics.get(row)[self._headers[column]]
+            return f"{v:.4f}"
+        else:
             return self._ics.get(row)[self._headers[column]]
 
         return QVariant()
diff --git a/src/View/InitialConditions/UndoCommand.py b/src/View/InitialConditions/UndoCommand.py
index 480b3d43..0daeb4b0 100644
--- a/src/View/InitialConditions/UndoCommand.py
+++ b/src/View/InitialConditions/UndoCommand.py
@@ -81,7 +81,7 @@ class SortCommand(QUndoCommand):
     def redo(self):
         self._ics.sort(
             reverse=self._reverse,
-            key=lambda x: x.name
+            key=lambda x: x["kp"]
         )
         if self._indexes is None:
             self._indexes = list(
diff --git a/src/View/InitialConditions/Window.py b/src/View/InitialConditions/Window.py
index cb7970fa..29691888 100644
--- a/src/View/InitialConditions/Window.py
+++ b/src/View/InitialConditions/Window.py
@@ -28,7 +28,7 @@ from View.InitialConditions.UndoCommand import (
     DuplicateCommand,
 )
 
-from View.InitialConditions.Table import TableModel
+from View.InitialConditions.Table import TableModel, ComboBoxDelegate
 
 from View.Plot.MplCanvas import MplCanvas
 from View.Geometry.PlotXY import PlotXY
@@ -81,6 +81,16 @@ class InitialConditionsWindow(ASubMainWindow, ListedSubWindow):
         )
         table.setModel(self._table)
 
+        self._delegate_kp = ComboBoxDelegate(
+            reach = self._reach,
+            parent=self
+        )
+
+        table.setItemDelegateForColumn(
+            list(table_headers).index("kp"),
+            self._delegate_kp
+        )
+
         table.setSelectionBehavior(QAbstractItemView.SelectRows)
         table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
         table.setAlternatingRowColors(True)
-- 
GitLab