diff --git a/src/View/WeatherParameters/Table.py b/src/View/WeatherParameters/Table.py index 7ce653d8..ef0b16af 100644 --- a/src/View/WeatherParameters/Table.py +++ b/src/View/WeatherParameters/Table.py @@ -30,7 +30,7 @@ from PyQt5.QtWidgets import ( QDialogButtonBox, QPushButton, QLineEdit, QFileDialog, QTableView, QAbstractItemView, QUndoStack, QShortcut, QAction, QItemDelegate, - QComboBox, + QComboBox, QMessageBox, ) from View.Tools.PamhyrTable import PamhyrTableModel @@ -222,6 +222,10 @@ class WeatherParametersTableModel(PamhyrTableModel): p for p in _edge.reach.profiles if p.pamhyr_id == value ) + if self._overlaps_existing_interval( + row, begin_section=_begin_rk): + self._show_overlap_warning() + return False self._undo.push( SetBeginCommand( self._data, global_row, _begin_rk @@ -233,15 +237,24 @@ class WeatherParametersTableModel(PamhyrTableModel): p for p in _edge.reach.profiles if p.pamhyr_id == value ) + if self._overlaps_existing_interval( + row, end_section=_end_rk): + self._show_overlap_warning() + return False self._undo.push( SetEndCommand( self._data, global_row, _end_rk ) ) elif self._headers[column] == "reach": + new_reach = self._river.edge(value) + if self._overlaps_existing_interval( + row, reach=new_reach): + self._show_overlap_warning() + return False self._undo.push( SetEdgeCommand( - self._data, global_row, self._river.edge(value) + self._data, global_row, new_reach ) ) except Exception as e: @@ -251,6 +264,48 @@ class WeatherParametersTableModel(PamhyrTableModel): self.dataChanged.emit(index, index) return True + def _overlaps_existing_interval(self, row, reach=None, + begin_section=None, end_section=None): + current = self._lst[row] + reach = current.reach if reach is None else reach + if reach is None: + return False + + if begin_section is None: + begin_section = ( + reach.reach.profiles[0] + if reach is not current.reach and reach.reach.profiles + else current.begin_section + ) + if end_section is None: + end_section = ( + reach.reach.profiles[-1] + if reach is not current.reach and reach.reach.profiles + else current.end_section + ) + if begin_section is None or end_section is None: + return False + + lower, upper = sorted((begin_section.rk, end_section.rk)) + return any( + other is not current + and not other.is_deleted() + and other.type == current.type + and other.reach is reach + and other.begin_section is not None + and other.end_section is not None + and max(lower, min(other.begin_rk, other.end_rk)) + < min(upper, max(other.begin_rk, other.end_rk)) + for other in self._data.lst + ) + + def _show_overlap_warning(self): + QMessageBox.warning( + self._table_view, + self._trad["msg_rk_overlap_title"], + self._trad["msg_rk_overlap_text"] + ) + def add(self, row, parent=QModelIndex()): self.beginInsertRows(parent, row, row) diff --git a/src/View/WeatherParameters/translate.py b/src/View/WeatherParameters/translate.py index 7f4fc107..0b8b3162 100644 --- a/src/View/WeatherParameters/translate.py +++ b/src/View/WeatherParameters/translate.py @@ -41,6 +41,14 @@ class WeatherParametersTranslate(MainTranslate): "These values are applied to sections that have not been " "defined using time series." ) + self._dict["msg_rk_overlap_title"] = _translate( + "WeatherParameters", "Overlapping interval" + ) + self._dict["msg_rk_overlap_text"] = _translate( + "WeatherParameters", + "Two weather-parameter intervals of the same type cannot " + "overlap on the same reach." + ) self._dict["rk"] = self._dict["unit_rk"]