diff --git a/src/Model/InitialConditions/InitialConditions.py b/src/Model/InitialConditions/InitialConditions.py index 83665f2b3857c261f31b7b46a1c053b61f431648..142f099687fc3bf2052e162bda946d88ea319e1a 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 525a898b81d453977c4c6862b8123881f941ec9a..d969ee514c759b47ffc0ffe7f48a8c4b9b474a74 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 480b3d43d30715242e2045748d3a750bff240076..0daeb4b0be7a8436b6e39e4693be9df29482ebef 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 cb7970facca45e4ddbf4230161d0f2fec51271ec..2969188857d63e97578bd0d8c574032345e19307 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)