diff --git a/src/Model/Results/River/River.py b/src/Model/Results/River/River.py index 578236883aa818214fb2d256207609b2b20bce28..4505b145a4ed5a9b51cf8b83a5cd3abe5b27bd0c 100644 --- a/src/Model/Results/River/River.py +++ b/src/Model/Results/River/River.py @@ -126,3 +126,11 @@ class River(object): self._reachs.append(new) return new + + def get_reach_by_geometry(self, geometry_reach): + return next( + filter( + lambda r: r.geometry is geometry_reach, + self._reachs + ) + ) diff --git a/src/View/InitialConditions/Table.py b/src/View/InitialConditions/Table.py index 9144a809878ea35a16f0fb78cb4beb238879574e..5461e862105ec08b3bb7cf0a9ad0e17210b246b7 100644 --- a/src/View/InitialConditions/Table.py +++ b/src/View/InitialConditions/Table.py @@ -37,7 +37,7 @@ from View.Tools.PamhyrTable import PamhyrTableModel from View.InitialConditions.UndoCommand import ( SetCommand, AddCommand, DelCommand, - SortCommand, MoveCommand, PasteCommand, + SortCommand, MoveCommand, InsertCommand, DuplicateCommand, GenerateCommand, ) @@ -230,7 +230,44 @@ class InitialConditionTableModel(PamhyrTableModel): self.layoutAboutToBeChanged.emit() self._undo.push( - PasteCommand( + InsertCommand( + self._lst, index, + list( + map( + lambda d: self._lst.new_from_data(*d), + data + ) + ) + ) + ) + + self.layoutAboutToBeChanged.emit() + self.layoutChanged.emit() + + def import_from_results(self, index, results): + if results is None: + logger.error("No results data") + return + + self.layoutAboutToBeChanged.emit() + + ts = max(results.get("timestamps")) + res_reach = results.river.get_reach_by_geometry( + self._reach.reach + ) + data = list( + map( + lambda p: [ + p.geometry.kp, + p.get_ts_key(ts, "Q"), + p.get_ts_key(ts, "Z"), + ], + res_reach.profiles + ) + ) + + self._undo.push( + InsertCommand( self._lst, index, list( map( diff --git a/src/View/InitialConditions/UndoCommand.py b/src/View/InitialConditions/UndoCommand.py index 06274ae3ad6488f6eb912c55ab92400a8ad61c1e..fecb6c6d4a0c2f7341047889ee424e4b61876e39 100644 --- a/src/View/InitialConditions/UndoCommand.py +++ b/src/View/InitialConditions/UndoCommand.py @@ -139,7 +139,7 @@ class MoveCommand(QUndoCommand): self._ics.move_down(self._i) -class PasteCommand(QUndoCommand): +class InsertCommand(QUndoCommand): def __init__(self, ics, row, ic): QUndoCommand.__init__(self) diff --git a/src/View/InitialConditions/Window.py b/src/View/InitialConditions/Window.py index 78532521b430972427d8f33cd84590b2c4a3be99..5dfb4cfae563fe8c9392ace024acd2c25af5a569 100644 --- a/src/View/InitialConditions/Window.py +++ b/src/View/InitialConditions/Window.py @@ -16,6 +16,7 @@ # -*- coding: utf-8 -*- +import os import logging from tools import trace, timer, logger_exception @@ -43,7 +44,7 @@ from Modules import Modules from View.InitialConditions.UndoCommand import ( SetCommand, AddCommand, DelCommand, - SortCommand, MoveCommand, PasteCommand, + SortCommand, MoveCommand, InsertCommand, DuplicateCommand, ) @@ -59,6 +60,9 @@ from View.InitialConditions.PlotDischarge import PlotDischarge from View.InitialConditions.translate import ICTranslate from View.InitialConditions.DialogHeight import HeightDialog from View.InitialConditions.DialogDischarge import DischargeDialog +from View.Results.ReadingResultsDialog import ReadingResultsDialog + +from Solver.Mage import Mage8 _translate = QCoreApplication.translate @@ -166,6 +170,7 @@ class InitialConditionsWindow(PamhyrWindow): self.find(QAction, "action_add").triggered.connect(self.add) self.find(QAction, "action_del").triggered.connect(self.delete) self.find(QAction, "action_sort").triggered.connect(self.sort) + self.find(QAction, "action_import").triggered.connect(self.import_from_file) self.find(QPushButton, "pushButton_generate_1").clicked.connect( self.generate_growing_constante_height @@ -179,9 +184,14 @@ class InitialConditionsWindow(PamhyrWindow): def index_selected_row(self): table = self.find(QTableView, f"tableView") - return table.selectionModel()\ - .selectedRows()[0]\ - .row() + rows = table.selectionModel()\ + .selectedRows() + + if len(rows) == 0: + return 0 + + return rows[0].row() + def update(self): self._update_plot() @@ -230,6 +240,42 @@ class InitialConditionsWindow(PamhyrWindow): self._table.sort(False) self._update() + def import_from_file(self): + workdir = os.path.dirname(self._study.filename) + + return self.file_dialog( + callback=lambda d: self._import_from_file(d[0]), + directory=workdir, + default_suffix=".BIN", + file_filter=["Mage (*.BIN)"], + ) + + def _import_from_file(self, file_name): + solver = Mage8("dummy") + name = os.path.basename(file_name)\ + .replace(".BIN", "") + + def reading(): + self._tmp_results = solver.results( + self._study, + os.path.dirname(file_name), + name=name + ) + + dlg = ReadingResultsDialog( + reading_fn=reading, + parent=self + ) + dlg.exec_() + results = self._tmp_results + self._import_from_results(results) + + def _import_from_results(self, results): + logger.debug(f"import from results: {results}") + row = self.index_selected_row() + + self._table.import_from_results(row, results) + def move_up(self): row = self.index_selected_row() self._table.move_up(row) @@ -278,7 +324,7 @@ class InitialConditionsWindow(PamhyrWindow): ) try: - row = 0 if len(self._ics.lst()) == 0 else self.index_selected_row() + row = self.index_selected_row() # self._table.paste(row, header, data) self._table.paste(row, [], data) except Exception as e: diff --git a/src/View/Tools/ASubWindow.py b/src/View/Tools/ASubWindow.py index b3e131e17f9d6e08e7943224b8f77c245ff806f0..786553589d2a1f8a05369e51972975e8d79a6db4 100644 --- a/src/View/Tools/ASubWindow.py +++ b/src/View/Tools/ASubWindow.py @@ -87,7 +87,9 @@ class WindowToolKit(object): def file_dialog(self, select_file=True, callback=lambda x: None, - directory=None): + directory=None, + default_suffix=None, + file_filter=None): """Open a new file dialog and send result to callback function Args: @@ -95,7 +97,8 @@ class WindowToolKit(object): callback: The callback function with one arguments, files selection list directory: Defaut directory - + default_suffix: Default file suffix + file_filter: List of file filter Returns: The returns of callback """ @@ -110,6 +113,13 @@ class WindowToolKit(object): if directory is not None: dialog.setDirectory(directory) + if select_file: + if default_suffix is not None: + dialog.setDefaultSuffix(default_suffix) + + if file_filter is not None: + dialog.setNameFilters(file_filter) + if dialog.exec_(): file_names = dialog.selectedFiles() return callback(file_names) diff --git a/src/View/ui/InitialConditions.ui b/src/View/ui/InitialConditions.ui index 272398b2b247dcc9f82c5713e36147fd60cccf6e..38f9673437b45964903f27dd16125fa44fb32dc2 100644 --- a/src/View/ui/InitialConditions.ui +++ b/src/View/ui/InitialConditions.ui @@ -81,6 +81,7 @@ <attribute name="toolBarBreak"> <bool>false</bool> </attribute> + <addaction name="action_import"/> <addaction name="action_add"/> <addaction name="action_del"/> <addaction name="action_sort"/> @@ -121,6 +122,18 @@ <string>Sort inital condition</string> </property> </action> + <action name="action_import"> + <property name="icon"> + <iconset> + <normaloff>ressources/import.png</normaloff>ressources/import.png</iconset> + </property> + <property name="text"> + <string>Import</string> + </property> + <property name="toolTip"> + <string>Import from file</string> + </property> + </action> </widget> <resources/> <connections/>