diff --git a/src/Model/LateralContribution/LateralContribution.py b/src/Model/LateralContribution/LateralContribution.py index 557f7374..5e9e5469 100644 --- a/src/Model/LateralContribution/LateralContribution.py +++ b/src/Model/LateralContribution/LateralContribution.py @@ -196,6 +196,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 LateralContribution(SQLSubModel): _sub_classes = [Data] @@ -511,6 +518,15 @@ class LateralContribution(SQLSubModel): def has_reach(self): return self._reach is not None + def cloned_for(self, reach, begin_section, end_section): + new = type(self)(name=self._name, status=self._status) + new._reach = reach + new._begin_section = begin_section + new._end_section = end_section + new._data = [data.cloned() for data in self.data] + new.modified() + return new + @property def begin_rk(self): if self._begin_section is None: diff --git a/src/Model/LateralContribution/LateralContributionList.py b/src/Model/LateralContribution/LateralContributionList.py index 49bffef1..c6aa7162 100644 --- a/src/Model/LateralContribution/LateralContributionList.py +++ b/src/Model/LateralContribution/LateralContributionList.py @@ -71,6 +71,89 @@ class LateralContributionList(PamhyrModelListWithTab): self._status.modified() return n + def split_reach(self, reach, profile, reach1, reach2): + profiles = reach.reach.profiles + split_index = profiles.index(profile) + + parts = [ + ( + reach1, + 0, + split_index, + dict(zip( + profiles[:split_index + 1], + reach1.reach.profiles + )) + ), + ( + reach2, + split_index, + len(profiles) - 1, + dict(zip( + profiles[split_index:], + reach2.reach.profiles + )) + ), + ] + + for tab in self._tabs: + contributions = [ + contribution + for contribution in self.get_tab(tab) + if contribution.reach is reach + ] + + for contribution in contributions: + if (contribution.begin_section not in profiles or + contribution.end_section not in profiles): + continue + + begin = profiles.index(contribution.begin_section) + end = profiles.index(contribution.end_section) + lower, upper = sorted((begin, end)) + reverse = begin > end + clones = [] + + for new_reach, part_lower, part_upper, sections in parts: + clipped_lower = max(lower, part_lower) + clipped_upper = min(upper, part_upper) + if clipped_lower > clipped_upper: + continue + + begin_index = ( + clipped_upper if reverse else clipped_lower + ) + end_index = ( + clipped_lower if reverse else clipped_upper + ) + clones.append(contribution.cloned_for( + new_reach, + sections[profiles[begin_index]], + sections[profiles[end_index]] + )) + + if len(clones) == 0: + continue + + self._tabs[tab].extend(clones) + + self._status.modified() + + def get_tab_for_enabled_reaches(self, tab): + return [ + contribution + for contribution in self.get_tab(tab) + if (contribution.reach is None or + (not contribution.reach.is_deleted() and + contribution.reach.is_enable())) + ] + + def get_for_enabled_reaches(self, tab, index): + contributions = self.get_tab_for_enabled_reaches(tab) + if not 0 <= index < len(contributions): + return None + return contributions[index] + def __copy__(self): new = LateralContributionList() diff --git a/src/Model/River.py b/src/Model/River.py index 0e29be45..a6643c3e 100644 --- a/src/Model/River.py +++ b/src/Model/River.py @@ -982,5 +982,8 @@ Last export at: @date.""" self._initial_conditions.split_reach( reach, profile, r1, r2 ) + self._lateral_contribution.split_reach( + reach, profile, r1, r2 + ) return r1, r2 diff --git a/src/Model/test_Model.py b/src/Model/test_Model.py index 56a7728f..f42f2a67 100644 --- a/src/Model/test_Model.py +++ b/src/Model/test_Model.py @@ -23,6 +23,7 @@ import tempfile from Model.Status import StudyStatus from Model.Study import Study from Model.River import River +from Model.LateralContribution.LateralContributionTypes import LateralContrib class StudyTestCase(unittest.TestCase): diff --git a/src/View/LateralContribution/Table.py b/src/View/LateralContribution/Table.py index a7319cf0..f76bea3c 100644 --- a/src/View/LateralContribution/Table.py +++ b/src/View/LateralContribution/Table.py @@ -103,7 +103,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)) @@ -154,7 +157,9 @@ class TableModel(PamhyrTableModel): self._long_types = self._trad.get_dict("long_types") def get_true_data_row(self, row): - lc = self._lst.get(self._tab, row) + lc = self._lst.get_for_enabled_reaches(self._tab, row) + if lc is None: + return len(self._lst.get_tab(self._tab)) return next( map( @@ -167,7 +172,7 @@ class TableModel(PamhyrTableModel): ) def rowCount(self, parent): - return self._lst.len(self._tab) + return len(self._lst.get_tab_for_enabled_reaches(self._tab)) def data(self, index, role): if role != Qt.ItemDataRole.DisplayRole: @@ -175,21 +180,22 @@ class TableModel(PamhyrTableModel): row = index.row() column = index.column() + contribution = self._lst.get_for_enabled_reaches(self._tab, row) if self._headers[column] == "name": - return self._lst.get(self._tab, row).name + return contribution.name elif self._headers[column] == "type": - t = self._lst.get(self._tab, row).lctype + t = contribution.lctype return self._long_types[t] elif self._headers[column] == "edge": - n = self._lst.get(self._tab, row).reach + n = contribution.reach if n is None: return self._trad['not_associated'] return n.name elif self._headers[column] == "begin_rk": - return str(self._lst.get(self._tab, row).begin_rk) + return str(contribution.begin_rk) elif self._headers[column] == "end_rk": - return str(self._lst.get(self._tab, row).end_rk) + return str(contribution.end_rk) return QVariant() @@ -202,6 +208,8 @@ class TableModel(PamhyrTableModel): row = index.row() column = index.column() + contribution = self._lst.get_for_enabled_reaches(self._tab, row) + row = self.get_true_data_row(row) try: if self._headers[column] == "name": @@ -225,7 +233,7 @@ class TableModel(PamhyrTableModel): ) ) elif self._headers[column] == "begin_rk": - _edge = self._lst.get(self._tab, row).reach + _edge = contribution.reach _begin_rk = next( p for p in _edge.reach.profiles if p.pamhyr_id == value @@ -236,7 +244,7 @@ class TableModel(PamhyrTableModel): ) ) elif self._headers[column] == "end_rk": - _edge = self._lst.get(self._tab, row).reach + _edge = contribution.reach _end_rk = next( p for p in _edge.reach.profiles if p.pamhyr_id == value diff --git a/src/View/LateralContribution/Window.py b/src/View/LateralContribution/Window.py index e69fd58e..2cc857d9 100644 --- a/src/View/LateralContribution/Window.py +++ b/src/View/LateralContribution/Window.py @@ -213,11 +213,11 @@ class LateralContributionWindow(PamhyrWindow): if len(rows) > 0: edge = self._study.river\ .lateral_contribution\ - .get(tab, rows[0])\ + .get_for_enabled_reaches(tab, rows[0])\ .reach if edge: data = edge.reach - lc = self._lcs.get(tab, rows[0]) + lc = self._lcs.get_for_enabled_reaches(tab, rows[0]) highlight = (lc.begin_rk, lc.end_rk) for delegate in self._delegate_rk: @@ -236,7 +236,8 @@ class LateralContributionWindow(PamhyrWindow): def add(self): tab = self.current_tab() rows = self.index_selected_rows() - if self._lcs.len(tab) == 0 or len(rows) == 0: + visible = self._lcs.get_tab_for_enabled_reaches(tab) + if len(visible) == 0 or len(rows) == 0: self._table[tab].add(0) else: self._table[tab].add(rows[0]) @@ -291,7 +292,7 @@ class LateralContributionWindow(PamhyrWindow): tab = self.current_tab() rows = self.index_selected_rows() for row in rows: - data = self._lcs.get(tab, row) + data = self._lcs.get_for_enabled_reaches(tab, row) if self.sub_window_exists( EditLateralContributionWindow,