mirror of https://gitlab.com/pamhyr/pamhyr2
HS: Basic: Add parameters table and fix save/load sql methods.
parent
6c9e90ceaf
commit
02bbdf9fda
|
|
@ -80,11 +80,15 @@ class BasicHS(SQLSubModel):
|
|||
|
||||
@classmethod
|
||||
def _get_ctor_from_type(cls, t):
|
||||
from Model.HydraulicStructure.Basic.Types import (
|
||||
NotDefined,
|
||||
from Model.HydraulicStructures.Basic.Types import (
|
||||
NotDefined, Dummy,
|
||||
)
|
||||
|
||||
res = NotDefined
|
||||
|
||||
if t == "DU":
|
||||
res = Dummy
|
||||
|
||||
return res
|
||||
|
||||
@classmethod
|
||||
|
|
@ -93,7 +97,7 @@ class BasicHS(SQLSubModel):
|
|||
|
||||
table = execute(
|
||||
"SELECT id, name, type, enabled, hs " +
|
||||
"FROM hydraulic_structures "
|
||||
"FROM hydraulic_structures_basic "
|
||||
)
|
||||
|
||||
for row in table:
|
||||
|
|
@ -113,7 +117,7 @@ class BasicHS(SQLSubModel):
|
|||
bhs.enabled = enabled
|
||||
|
||||
data['bhs_id'] = bhs_id
|
||||
bhs._data = BasicHSValue._db_load(
|
||||
bhs._data = BHSValue._db_load(
|
||||
execute, data
|
||||
)
|
||||
|
||||
|
|
@ -144,7 +148,7 @@ class BasicHS(SQLSubModel):
|
|||
data['bhs_id'] = self.id
|
||||
execute(
|
||||
"DELETE FROM hydraulic_structures_basic_value " +
|
||||
f"WHERE bhs = {bhs_id}"
|
||||
f"WHERE bhs = {self.id}"
|
||||
)
|
||||
|
||||
for values in self._data:
|
||||
|
|
@ -183,8 +187,8 @@ class BasicHS(SQLSubModel):
|
|||
self._status.modified()
|
||||
|
||||
@property
|
||||
def lst(self):
|
||||
return self._data.copy()
|
||||
def parameters(self):
|
||||
return self._data
|
||||
|
||||
def convert(self, new_type):
|
||||
return new_type(id=self.id, name=self.name, status=self._status)
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@ class NotDefined(BasicHS):
|
|||
|
||||
self._type = "ND"
|
||||
self._data = [
|
||||
BHSValue("foo", float, 0.0),
|
||||
BHSValue("bar", float, 42.0),
|
||||
BHSValue("baz", int, 13),
|
||||
BHSValue("foo", float, 0.0, status=status),
|
||||
BHSValue("bar", float, 42.0, status=status),
|
||||
BHSValue("baz", int, 13, status=status),
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -51,9 +51,9 @@ class Dummy(BasicHS):
|
|||
|
||||
self._type = "DU"
|
||||
self._data = [
|
||||
BHSValue("foo", float, 0.0),
|
||||
BHSValue("bar", float, 42.0),
|
||||
BHSValue("baz", int, 13),
|
||||
BHSValue("foo", float, 0.0, status=status),
|
||||
BHSValue("bar", float, 42.0, status=status),
|
||||
BHSValue("baz", int, 13, status=status),
|
||||
]
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
import logging
|
||||
from functools import reduce
|
||||
|
||||
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 == output_reach_id else acc[1]
|
||||
),
|
||||
data["reachs"],
|
||||
data["edges"],
|
||||
[None, None]
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ from View.Tools.PamhyrTable import PamhyrTableModel
|
|||
from View.HydraulicStructures.BasicHydraulicStructures.UndoCommand import (
|
||||
SetNameCommand, SetTypeCommand,
|
||||
SetEnabledCommand, AddCommand, DelCommand,
|
||||
SetValueCommand,
|
||||
)
|
||||
from Model.HydraulicStructures.Basic.Types import BHS_types
|
||||
|
||||
|
|
@ -147,7 +148,7 @@ class TableModel(PamhyrTableModel):
|
|||
)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
logger.error(e)
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
self.dataChanged.emit(index, index)
|
||||
|
|
@ -203,3 +204,72 @@ class TableModel(PamhyrTableModel):
|
|||
def redo(self):
|
||||
self._undo.redo()
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -47,3 +47,8 @@ hydraulic structure values?"
|
|||
"name": _translate("BasicHydraulicStructures", "Name"),
|
||||
"type": _translate("BasicHydraulicStructures", "Type"),
|
||||
}
|
||||
|
||||
self._sub_dict["table_headers_parameters"] = {
|
||||
"name": _translate("BasicHydraulicStructures", "Name"),
|
||||
"value": _translate("BasicHydraulicStructures", "Value"),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,3 +131,23 @@ class PasteCommand(QUndoCommand):
|
|||
def redo(self):
|
||||
for r in self._bhs:
|
||||
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
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ from View.Tools.Plot.PamhyrToolbar import PamhyrPlotToolbar
|
|||
from View.HydraulicStructures.PlotAC import PlotAC
|
||||
|
||||
from View.HydraulicStructures.BasicHydraulicStructures.Table import (
|
||||
TableModel, ComboBoxDelegate
|
||||
ComboBoxDelegate, TableModel, ParametersTableModel,
|
||||
)
|
||||
|
||||
from View.Network.GraphWidget import GraphWidget
|
||||
|
|
@ -79,6 +79,10 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
|||
self.setup_connections()
|
||||
|
||||
def setup_table(self):
|
||||
self.setup_table_bhs()
|
||||
self.setup_table_bhs_parameters()
|
||||
|
||||
def setup_table_bhs(self):
|
||||
self._table = None
|
||||
|
||||
self._delegate_type = ComboBoxDelegate(
|
||||
|
|
@ -111,6 +115,21 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
|||
)
|
||||
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):
|
||||
self._checkbox = self.find(QCheckBox, f"checkBox")
|
||||
self._set_checkbox_state()
|
||||
|
|
@ -236,3 +255,8 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
|||
|
||||
def update(self):
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue