LateralContributions: propagate LAT when splitting reach + display only enabled reach in combobox

split_reach
Dylan Jeannin 2026-07-24 15:35:12 +02:00
parent 87d31c134e
commit bbdd18e8ef
11 changed files with 148 additions and 20 deletions

View File

@ -192,6 +192,13 @@ class Data(SQLSubModel):
def __setitem__(self, key, value):
self._data[key] = self._types[key](value)
def cloned(self):
return Data(
self[0], self[1],
types=self._types,
status=self._status
)
class LateralContributionAdisTS(SQLSubModel):
_sub_classes = [Data]
@ -477,6 +484,18 @@ class LateralContributionAdisTS(SQLSubModel):
self._end_rk = end_rk
self.modified()
def cloned_for(self, reach, begin_rk, end_rk):
new = LateralContributionAdisTS(
pollutant=self._pollutant,
status=self._status
)
new._reach = reach
new._begin_rk = begin_rk
new._end_rk = end_rk
new._data = [data.cloned() for data in self.data]
new.modified()
return new
@property
def _default_0(self):
return self._types[0](0)

View File

@ -68,6 +68,46 @@ class LateralContributionsAdisTSList(PamhyrModelList):
self._status.modified()
return n
def split_reach(self, reach, profile, reach1, reach2):
parts = []
for new_reach in (reach1, reach2):
rks = new_reach.reach.get_rk()
if len(rks) == 0:
continue
parts.append((new_reach.id, min(rks), max(rks)))
contributions = [
contribution
for contribution in self.lst
if contribution.reach == reach.id
]
clones = []
for contribution in contributions:
begin = contribution.begin_rk
end = contribution.end_rk
if begin is None or end is None:
continue
lower, upper = sorted((begin, end))
reverse = begin > end
for new_reach, part_lower, part_upper in parts:
clipped_lower = max(lower, part_lower)
clipped_upper = min(upper, part_upper)
if clipped_lower > clipped_upper:
continue
begin_rk = clipped_upper if reverse else clipped_lower
end_rk = clipped_lower if reverse else clipped_upper
clones.append(contribution.cloned_for(
new_reach, begin_rk, end_rk
))
if len(clones) != 0:
self._lst.extend(clones)
self._status.modified()
@property
def Lat_Cont_List(self):
return self.lst

View File

@ -991,5 +991,8 @@ Last export at: @date."""
self._InitialConditionsAdisTS.split_reach(
reach, profile, r1, r2
)
self._LateralContributionsAdisTS.split_reach(
reach, profile, r1, r2
)
return r1, r2

View File

@ -549,7 +549,7 @@ class AdisTSwc(AdisTS):
for LC in POL_LC:
reach = next((
edge for edge in study.river.enable_edges()
if edge.id == LC.edge
if edge.id == LC.reach
), None)
if reach is None:
continue

View File

@ -78,7 +78,7 @@ class ComboBoxDelegate(QItemDelegate):
else:
val = list(
map(
lambda n: n.name, self._data.edges()
lambda n: n.name, self._data.enable_edges()
)
)

View File

@ -80,7 +80,7 @@ class ComboBoxDelegate(QItemDelegate):
else:
val = list(
map(
lambda n: n.name, self._data.edges()
lambda n: n.name, self._data.enable_edges()
)
)

View File

@ -75,7 +75,7 @@ class ComboBoxDelegate(QItemDelegate):
else:
val = list(
map(
lambda n: n.name, self._data.edges()
lambda n: n.name, self._data.enable_edges()
)
)

View File

@ -81,7 +81,10 @@ class ComboBoxDelegate(QItemDelegate):
else:
self.editor.addItems(
[self._trad['not_associated']] +
self._data.edges_names()
[
reach.name
for reach in self._data.enable_edges()
]
)
self.editor.setCurrentText(index.data(Qt.DisplayRole))
@ -119,6 +122,11 @@ class TableModel(PamhyrTableModel):
self._setup_lst()
def _setup_lst(self):
enabled_reach_ids = {
reach.id
for reach in self._data.enable_edges()
}
if self._lcs_list is not None:
self._lcs_pol_list = [
lcs for lcs in self._lcs_list._lst
@ -127,7 +135,13 @@ class TableModel(PamhyrTableModel):
self._lst = list(
filter(
lambda x: x._deleted is False,
lambda x: (
x._deleted is False and
(
x.reach in (None, -1) or
x.reach in enabled_reach_ids
)
),
self._lcs_pol_list
)
)
@ -137,6 +151,18 @@ class TableModel(PamhyrTableModel):
def rowCount(self, parent):
return len(self._lst)
def get(self, row):
if 0 <= row < len(self._lst):
return self._lst[row]
return None
def refresh(self):
self.beginResetModel()
try:
self._setup_lst()
finally:
self.endResetModel()
def data(self, index, role):
if role != Qt.ItemDataRole.DisplayRole:
return QVariant()
@ -170,11 +196,18 @@ class TableModel(PamhyrTableModel):
try:
if self._headers[column] == "reach":
reach = self._data.reach(value)
rks = reach.reach.get_rk()
if len(rks) == 0:
return False
self._undo.push(
SetReachCommand(
self._lcs_list, self._lst,
row,
self._data.reach(value).id
reach.id,
min(rks),
max(rks)
)
)
elif self._headers[column] == "begin_rk":
@ -193,7 +226,13 @@ class TableModel(PamhyrTableModel):
logger.info(e)
logger.debug(traceback.format_exc())
self.dataChanged.emit(index, index)
if self._headers[column] == "reach":
end_index = self.index(
row, self._headers.index("end_rk")
)
self.dataChanged.emit(index, end_index)
else:
self.dataChanged.emit(index, index)
return True
def add(self, row, parent=QModelIndex()):

View File

@ -64,20 +64,30 @@ class SetEndCommand(QUndoCommand):
class SetReachCommand(QUndoCommand):
def __init__(self, lcs, lcs_lst, index, reach):
def __init__(self, lcs, lcs_lst, index, reach, begin_rk, end_rk):
QUndoCommand.__init__(self)
self._lcs = lcs
self._lcs_lst = lcs_lst
self._index = index
self._old = self._lcs_lst[self._index].reach
self._old_begin_rk = self._lcs_lst[self._index].begin_rk
self._old_end_rk = self._lcs_lst[self._index].end_rk
self._new = reach
self._new_begin_rk = begin_rk
self._new_end_rk = end_rk
def undo(self):
self._lcs_lst[self._index].reach = self._old
contribution = self._lcs_lst[self._index]
contribution.reach = self._old
contribution.begin_rk = self._old_begin_rk
contribution.end_rk = self._old_end_rk
def redo(self):
self._lcs_lst[self._index].reach = self._new
contribution = self._lcs_lst[self._index]
contribution.reach = self._new
contribution.begin_rk = self._new_begin_rk
contribution.end_rk = self._new_end_rk
class AddCommand(QUndoCommand):

View File

@ -20,6 +20,7 @@ import logging
from tools import trace, timer
from Modules import Modules
from View.Tools.PamhyrWindow import PamhyrWindow
from PyQt5.QtGui import (
@ -187,12 +188,13 @@ class LateralContributionAdisTSWindow(PamhyrWindow):
data = None
highlight = None
tab = "liquid"
if len(rows) > 0:
reach_id = self._study.river\
.lateral_contributions_adists.lst[rows[0]]\
.reach
contribution = self._table.get(rows[0])
reach_id = (
contribution.reach
if contribution is not None
else None
)
if reach_id:
reach = next(
@ -201,8 +203,10 @@ class LateralContributionAdisTSWindow(PamhyrWindow):
self._study.river.reachs()))
data = reach.reach
lc = self._lcs.lst[rows[0]]
highlight = (lc.begin_rk, lc.end_rk)
highlight = (
contribution.begin_rk,
contribution.end_rk
)
for delegate in self._delegate_rk:
delegate.data = reach
@ -254,13 +258,23 @@ class LateralContributionAdisTSWindow(PamhyrWindow):
self._table.redo()
self._set_current_reach()
def _propagated_update(self, key=Modules(0)):
if Modules.NETWORK not in key:
return
self._table.refresh()
self.find(QTableView, "tableView").clearSelection()
self._set_current_reach()
def edit(self):
rows = self.index_selected_rows()
if not rows:
return
for row in rows:
data = self._lcs.lst[row]
data = self._table.get(row)
if data is None:
continue
if self.sub_window_exists(
EditLateralContributionAdisTSWindow,

View File

@ -87,7 +87,10 @@ class ComboBoxDelegate(QItemDelegate):
else:
self.editor.addItems(
[self._trad['not_associated']] +
self._data.edges_names()
[
reach.name
for reach in self._data.enable_edges()
]
)
self.editor.setCurrentText(str(index.data(Qt.DisplayRole)))