load frictions from file

master
Theophile Terraz 2025-09-15 15:40:17 +02:00
parent b582e133a1
commit 31fe765e51
7 changed files with 125 additions and 9 deletions

View File

@ -214,15 +214,15 @@ class TableModel(PamhyrTableModel):
def read_from_file(self, file_name, bctype):
logger.debug(f"Import initial conditions from {file_name}")
logger.debug(f"Import boundary conditions from {file_name}")
data0 = []
data1 = []
if bctype == "ZD":
mult = 1
else:
mult = 60
with open(file_name, encoding="utf-8") as ini_file:
for line in ini_file:
with open(file_name, encoding="utf-8") as bc_file:
for line in bc_file:
if not (line.startswith("#") or
line.startswith("*") or
line.startswith("$")):

View File

@ -38,7 +38,7 @@ from View.Frictions.UndoCommand import (
SetNameCommand, SetBeginCommand, SetEndCommand,
SetBeginStricklerCommand, SetEndStricklerCommand,
AddCommand, DelCommand, SortCommand,
MoveCommand, PasteCommand, DuplicateCommand,
MoveCommand, PasteCommand, DuplicateCommand, ReplaceDataCommand,
)
from View.Tools.PamhyrTable import PamhyrTableModel
@ -99,7 +99,7 @@ class ComboBoxDelegate(QItemDelegate):
self.commitData.emit(self.sender())
class TableModel(PamhyrTableModel):
class FrictionTableModel(PamhyrTableModel):
def _setup_lst(self):
self._lst = self._data.frictions
self._study = self._opt_data
@ -199,6 +199,18 @@ class TableModel(PamhyrTableModel):
self.endRemoveRows()
self.layoutChanged.emit()
def replace_data(self, new_data):
self.layoutAboutToBeChanged.emit()
self._undo.push(
ReplaceDataCommand(
self._lst, new_data
)
)
self.layoutAboutToBeChanged.emit()
self.layoutChanged.emit()
def sort(self, _reverse, parent=QModelIndex()):
self.layoutAboutToBeChanged.emit()
@ -252,3 +264,34 @@ class TableModel(PamhyrTableModel):
def redo(self):
self._undo.redo()
self.layoutChanged.emit()
def read_from_file(self, file_name):
reach_id = self._study.river.enable_edges().index(self._data) + 1
logger.debug(f"Import frictions from {file_name}")
data = []
with open(file_name, encoding="utf-8") as rug_file:
for line in rug_file:
if line.upper().startswith("K"):
line = line.split()
if int(line[1]) == reach_id:
data.append(line[1:])
print(data)
new_data = []
for d in data:
new = None
minor = float(d[3])
medium = float(d[4])
for s in self._study.river.stricklers.stricklers:
if s.minor == minor and s.medium == medium:
new = s
break
if new is None:
new = self._study.river.stricklers.new(len(
self._study.river.stricklers))
new.minor = minor
new.medium = medium
new_data.append([self._data, float(d[1]), float(d[2]), new, new])
self.replace_data(new_data)

View File

@ -143,6 +143,41 @@ class AddCommand(QUndoCommand):
self._frictions.insert(self._index, self._new)
class ReplaceDataCommand(QUndoCommand):
def __init__(self, frictions, new_data):
QUndoCommand.__init__(self)
self._frictions = frictions
self._new_data = new_data
self._old_rows = list(range(len(frictions)))
self._new_rows = list(range(len(new_data)))
self._old_friction = []
for row in self._old_rows:
self._old_friction.append((row, self._frictions.lst[row]))
self._new_friction = []
for row in self._new_rows:
new = Friction(status=self._frictions._status)
d = new_data[row]
new.edge = d[0]
new.begin_rk = d[1]
new.end_rk = d[2]
new.begin_strickler = d[3]
new.end_strickler = d[4]
self._new_friction.append((row, new))
def undo(self):
self._frictions.delete_i(self._new_rows)
for row, el in self._old_friction:
self._frictions.insert(row, el)
def redo(self):
self._frictions.delete_i(self._old_rows)
for row, el in self._new_friction:
self._frictions.insert(row, el)
class DelCommand(QUndoCommand):
def __init__(self, frictions, rows):
QUndoCommand.__init__(self)

View File

@ -26,10 +26,11 @@ from PyQt5.QtGui import (
QKeySequence,
)
from PyQt5 import QtWidgets
from PyQt5.QtCore import (
Qt, QVariant, QAbstractTableModel,
QCoreApplication, QModelIndex, pyqtSlot,
QRect,
QRect, QSettings,
)
from PyQt5.QtWidgets import (
@ -46,7 +47,7 @@ from View.Frictions.UndoCommand import (
)
from View.Frictions.Table import (
TableModel, ComboBoxDelegate
FrictionTableModel, ComboBoxDelegate
)
from View.Tools.Plot.PamhyrCanvas import MplCanvas
@ -106,7 +107,7 @@ class FrictionsWindow(PamhyrWindow):
)
table = self.find(QTableView, f"tableView")
self._table = TableModel(
self._table = FrictionTableModel(
table_view=table,
table_headers=self._trad.get_dict("table_headers"),
editable_headers=[
@ -157,6 +158,8 @@ class FrictionsWindow(PamhyrWindow):
self.plot_2.draw()
def setup_connections(self):
self.find(QAction, "action_import").triggered\
.connect(self.import_from_file)
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)
@ -265,3 +268,22 @@ class FrictionsWindow(PamhyrWindow):
parent=self
)
strick.show()
def import_from_file(self):
options = QFileDialog.Options()
settings = QSettings(QSettings.IniFormat,
QSettings.UserScope, 'MyOrg', )
options |= QFileDialog.DontUseNativeDialog
file_types = [self._trad["file_rug"], self._trad["file_all"]]
file_name, _ = QtWidgets.QFileDialog.getOpenFileName(
self,
self._trad["open_file"],
"",
";; ".join(file_types),
options=options
)
if file_name != "":
self._table.read_from_file(file_name)

View File

@ -38,6 +38,10 @@ class FrictionsTranslate(MainTranslate):
self._dict["Edit frictions"] = _translate(
"Frictions", "Edit frictions"
)
self._dict["file_rug"] = _translate(
"Frictions", "Mage initial frictions file (*.RUG *.rug)")
self._dict["file_all"] = _translate(
"Frictions", "All files (*)")
self._sub_dict["table_headers"] = {
# "name": self._dict["name"],

View File

@ -284,7 +284,6 @@ class InitialConditionTableModel(PamhyrTableModel):
def read_from_ini(self, file_name):
reach_id = self._data.river.enable_edges().index(self._reach) + 1
print(f"reach : {reach_id}")
logger.debug(f"Import initial conditions from {file_name}")
data = []

View File

@ -60,6 +60,7 @@
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="action_import"/>
<addaction name="action_add"/>
<addaction name="action_del"/>
<addaction name="action_sort"/>
@ -107,6 +108,18 @@
<string>Ctrl+E</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/>