HS: Basic: Add parameters table and fix save/load sql methods.

setup.py
Pierre-Antoine Rouby 2023-12-12 14:23:36 +01:00
parent 6c9e90ceaf
commit 02bbdf9fda
7 changed files with 140 additions and 16 deletions

View File

@ -80,11 +80,15 @@ class BasicHS(SQLSubModel):
@classmethod @classmethod
def _get_ctor_from_type(cls, t): def _get_ctor_from_type(cls, t):
from Model.HydraulicStructure.Basic.Types import ( from Model.HydraulicStructures.Basic.Types import (
NotDefined, NotDefined, Dummy,
) )
res = NotDefined res = NotDefined
if t == "DU":
res = Dummy
return res return res
@classmethod @classmethod
@ -93,7 +97,7 @@ class BasicHS(SQLSubModel):
table = execute( table = execute(
"SELECT id, name, type, enabled, hs " + "SELECT id, name, type, enabled, hs " +
"FROM hydraulic_structures " "FROM hydraulic_structures_basic "
) )
for row in table: for row in table:
@ -113,7 +117,7 @@ class BasicHS(SQLSubModel):
bhs.enabled = enabled bhs.enabled = enabled
data['bhs_id'] = bhs_id data['bhs_id'] = bhs_id
bhs._data = BasicHSValue._db_load( bhs._data = BHSValue._db_load(
execute, data execute, data
) )
@ -144,7 +148,7 @@ class BasicHS(SQLSubModel):
data['bhs_id'] = self.id data['bhs_id'] = self.id
execute( execute(
"DELETE FROM hydraulic_structures_basic_value " + "DELETE FROM hydraulic_structures_basic_value " +
f"WHERE bhs = {bhs_id}" f"WHERE bhs = {self.id}"
) )
for values in self._data: for values in self._data:
@ -183,8 +187,8 @@ class BasicHS(SQLSubModel):
self._status.modified() self._status.modified()
@property @property
def lst(self): def parameters(self):
return self._data.copy() return self._data
def convert(self, new_type): def convert(self, new_type):
return new_type(id=self.id, name=self.name, status=self._status) return new_type(id=self.id, name=self.name, status=self._status)

View File

@ -35,9 +35,9 @@ class NotDefined(BasicHS):
self._type = "ND" self._type = "ND"
self._data = [ self._data = [
BHSValue("foo", float, 0.0), BHSValue("foo", float, 0.0, status=status),
BHSValue("bar", float, 42.0), BHSValue("bar", float, 42.0, status=status),
BHSValue("baz", int, 13), BHSValue("baz", int, 13, status=status),
] ]
@ -51,9 +51,9 @@ class Dummy(BasicHS):
self._type = "DU" self._type = "DU"
self._data = [ self._data = [
BHSValue("foo", float, 0.0), BHSValue("foo", float, 0.0, status=status),
BHSValue("bar", float, 42.0), BHSValue("bar", float, 42.0, status=status),
BHSValue("baz", int, 13), BHSValue("baz", int, 13, status=status),
] ]

View File

@ -17,6 +17,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
from functools import reduce
from tools import trace, timer, old_pamhyr_date_to_timestamp from tools import trace, timer, old_pamhyr_date_to_timestamp
@ -125,7 +126,7 @@ class HydraulicStructure(SQLSubModel):
n if n.id == input_reach_id else acc[0], n if n.id == input_reach_id else acc[0],
n if n.id == output_reach_id else acc[1] n if n.id == output_reach_id else acc[1]
), ),
data["reachs"], data["edges"],
[None, None] [None, None]
) )

View File

@ -39,6 +39,7 @@ from View.Tools.PamhyrTable import PamhyrTableModel
from View.HydraulicStructures.BasicHydraulicStructures.UndoCommand import ( from View.HydraulicStructures.BasicHydraulicStructures.UndoCommand import (
SetNameCommand, SetTypeCommand, SetNameCommand, SetTypeCommand,
SetEnabledCommand, AddCommand, DelCommand, SetEnabledCommand, AddCommand, DelCommand,
SetValueCommand,
) )
from Model.HydraulicStructures.Basic.Types import BHS_types from Model.HydraulicStructures.Basic.Types import BHS_types
@ -147,7 +148,7 @@ class TableModel(PamhyrTableModel):
) )
) )
except Exception as e: except Exception as e:
logger.info(e) logger.error(e)
logger.debug(traceback.format_exc()) logger.debug(traceback.format_exc())
self.dataChanged.emit(index, index) self.dataChanged.emit(index, index)
@ -203,3 +204,72 @@ class TableModel(PamhyrTableModel):
def redo(self): def redo(self):
self._undo.redo() self._undo.redo()
self.layoutChanged.emit() self.layoutChanged.emit()
class ParametersTableModel(PamhyrTableModel):
def __init__(self, trad=None, **kwargs):
self._trad = trad
self._long_types = {}
if self._trad is not None:
self._long_types = self._trad.get_dict("long_types")
self._hs_index = None
super(ParametersTableModel, self).__init__(trad=trad, **kwargs)
def rowCount(self, parent):
if self._hs_index is None:
return 0
return len(
self._data.basic_structure(self._hs_index)
)
def data(self, index, role):
if role != Qt.ItemDataRole.DisplayRole:
return QVariant()
if self._hs_index is None:
return QVariant()
row = index.row()
column = index.column()
hs = self._data.basic_structure(self._hs_index)
if self._headers[column] == "name":
return hs.parameters[row].name
elif self._headers[column] == "value":
return str(hs.parameters[row].value)
return QVariant()
def setData(self, index, value, role=Qt.EditRole):
if not index.isValid() or role != Qt.EditRole:
return False
if self._hs_index is None:
return QVariant()
row = index.row()
column = index.column()
try:
if self._headers[column] == "value":
self._undo.push(
SetValueCommand(
self._data.basic_structure(self._hs_index),
row, value
)
)
except Exception as e:
logger.error(e)
logger.debug(traceback.format_exc())
self.dataChanged.emit(index, index)
return True
def update_hs_index(self, index):
self._hs_index = index
self.layoutChanged.emit()

View File

@ -47,3 +47,8 @@ hydraulic structure values?"
"name": _translate("BasicHydraulicStructures", "Name"), "name": _translate("BasicHydraulicStructures", "Name"),
"type": _translate("BasicHydraulicStructures", "Type"), "type": _translate("BasicHydraulicStructures", "Type"),
} }
self._sub_dict["table_headers_parameters"] = {
"name": _translate("BasicHydraulicStructures", "Name"),
"value": _translate("BasicHydraulicStructures", "Value"),
}

View File

@ -131,3 +131,23 @@ class PasteCommand(QUndoCommand):
def redo(self): def redo(self):
for r in self._bhs: for r in self._bhs:
self._hs.insert(self._row, r) self._hs.insert(self._row, r)
####################################
# Basic hydraulic structure values #
####################################
class SetValueCommand(QUndoCommand):
def __init__(self, bhs, index, value):
QUndoCommand.__init__(self)
self._bhs = bhs
self._index = index
self._old = self._bhs.parameters[self._index].value
self._new = self._bhs.parameters[self._index].type(value)
def undo(self):
self._bhs.parameters[self._index].value = self._old
def redo(self):
self._bhs.parameters[self._index].value = self._new

View File

@ -41,7 +41,7 @@ from View.Tools.Plot.PamhyrToolbar import PamhyrPlotToolbar
from View.HydraulicStructures.PlotAC import PlotAC from View.HydraulicStructures.PlotAC import PlotAC
from View.HydraulicStructures.BasicHydraulicStructures.Table import ( from View.HydraulicStructures.BasicHydraulicStructures.Table import (
TableModel, ComboBoxDelegate ComboBoxDelegate, TableModel, ParametersTableModel,
) )
from View.Network.GraphWidget import GraphWidget from View.Network.GraphWidget import GraphWidget
@ -79,6 +79,10 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
self.setup_connections() self.setup_connections()
def setup_table(self): def setup_table(self):
self.setup_table_bhs()
self.setup_table_bhs_parameters()
def setup_table_bhs(self):
self._table = None self._table = None
self._delegate_type = ComboBoxDelegate( self._delegate_type = ComboBoxDelegate(
@ -111,6 +115,21 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
) )
table.scrollTo(index) table.scrollTo(index)
def setup_table_bhs_parameters(self):
self._table_parameters = None
table = self.find(QTableView, f"tableView_2")
self._table_parameters = ParametersTableModel(
table_view=table,
table_headers=self._trad.get_dict("table_headers_parameters"),
editable_headers=["value"],
delegates={},
trad=self._trad,
data=self._hs,
undo=self._undo_stack,
parent=self,
)
def setup_checkbox(self): def setup_checkbox(self):
self._checkbox = self.find(QCheckBox, f"checkBox") self._checkbox = self.find(QCheckBox, f"checkBox")
self._set_checkbox_state() self._set_checkbox_state()
@ -236,3 +255,8 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
def update(self): def update(self):
self._set_checkbox_state() self._set_checkbox_state()
self._update_parameters_table()
def _update_parameters_table(self):
row = self.index_selected_row()
self._table_parameters.update_hs_index(row)