mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
merge
This commit is contained in:
commit
79b31e8d08
16 files changed
+335
-71
No files matched your search
@@ -31,14 +31,15 @@ from PyQt5.QtWidgets import (
|
||||
QDialogButtonBox, QPushButton, QLineEdit,
|
||||
QFileDialog, QTableView, QAbstractItemView,
|
||||
QUndoStack, QShortcut, QAction, QItemDelegate,
|
||||
QComboBox,
|
||||
QComboBox, QMessageBox,
|
||||
)
|
||||
|
||||
from View.Tools.PamhyrTable import PamhyrTableModel
|
||||
|
||||
from View.HydraulicStructures.BasicHydraulicStructures.UndoCommand import (
|
||||
SetNameCommand, SetTypeCommand,
|
||||
AddCommand, DelCommand,
|
||||
SetEnabledCommand, AddCommand, DelCommand,
|
||||
SetValueCommand,
|
||||
)
|
||||
from Model.HydraulicStructures.Basic.Types import BHS_types
|
||||
|
||||
@@ -61,15 +62,13 @@ class ComboBoxDelegate(QItemDelegate):
|
||||
def createEditor(self, parent, option, index):
|
||||
self.editor = QComboBox(parent)
|
||||
|
||||
|
||||
lst = list(
|
||||
map(
|
||||
lambda k: self._long_types[k], BHS_types.keys()
|
||||
lambda k: self._long_types[k],
|
||||
BHS_types.keys()
|
||||
)
|
||||
)
|
||||
self.editor.addItems(
|
||||
lst
|
||||
)
|
||||
self.editor.addItems(lst)
|
||||
|
||||
self.editor.setCurrentText(index.data(Qt.DisplayRole))
|
||||
return self.editor
|
||||
@@ -137,20 +136,35 @@ class TableModel(PamhyrTableModel):
|
||||
)
|
||||
)
|
||||
elif self._headers[column] == "type":
|
||||
key = next(k for k, v in self._long_types.items()
|
||||
if v == value)
|
||||
self._undo.push(
|
||||
SetTypeCommand(
|
||||
self._data, row, BHS_types[key]
|
||||
if self._question_set_type():
|
||||
key = next(
|
||||
k for k, v in self._long_types.items()
|
||||
if v == value
|
||||
)
|
||||
|
||||
self._undo.push(
|
||||
SetTypeCommand(
|
||||
self._data, row, BHS_types[key]
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.info(e)
|
||||
logger.error(e)
|
||||
logger.debug(traceback.format_exc())
|
||||
|
||||
self.dataChanged.emit(index, index)
|
||||
return True
|
||||
|
||||
def _question_set_type(self):
|
||||
question = QMessageBox(self._parent)
|
||||
|
||||
question.setWindowTitle(self._trad['msg_type_change_title'])
|
||||
question.setText(self._trad['msg_type_change_text'])
|
||||
question.setStandardButtons(QMessageBox.Cancel | QMessageBox.Ok)
|
||||
question.setIcon(QMessageBox.Question)
|
||||
|
||||
res = question.exec()
|
||||
return res == QMessageBox.Ok
|
||||
|
||||
def add(self, row, parent=QModelIndex()):
|
||||
self.beginInsertRows(parent, row, row - 1)
|
||||
|
||||
@@ -175,6 +189,14 @@ class TableModel(PamhyrTableModel):
|
||||
self.endRemoveRows()
|
||||
self.layoutChanged.emit()
|
||||
|
||||
def enabled(self, row, enabled, parent=QModelIndex()):
|
||||
self._undo.push(
|
||||
SetEnabledCommand(
|
||||
self._lst, row, enabled
|
||||
)
|
||||
)
|
||||
self.layoutChanged.emit()
|
||||
|
||||
def undo(self):
|
||||
self._undo.undo()
|
||||
self.layoutChanged.emit()
|
||||
@@ -182,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()
|
||||
@@ -27,6 +27,17 @@ class BasicHydraulicStructuresTranslate(PamhyrTranslate):
|
||||
def __init__(self):
|
||||
super(BasicHydraulicStructuresTranslate, self).__init__()
|
||||
|
||||
self._dict['msg_type_change_title'] = _translate(
|
||||
"BasicHydraulicStructures",
|
||||
"Change hydraulic structure type"
|
||||
)
|
||||
|
||||
self._dict['msg_type_change_text'] = _translate(
|
||||
"BasicHydraulicStructures",
|
||||
"Do you want to change the hydraulic structure type and reset \
|
||||
hydraulic structure values?"
|
||||
)
|
||||
|
||||
self._sub_dict["long_types"] = {
|
||||
"ND": _translate("BasicHydraulicStructures", "Not defined"),
|
||||
"S1": _translate("BasicHydraulicStructures", "Seuil déversoir"),
|
||||
@@ -45,3 +56,8 @@ class BasicHydraulicStructuresTranslate(PamhyrTranslate):
|
||||
"name": _translate("BasicHydraulicStructures", "Name"),
|
||||
"type": _translate("BasicHydraulicStructures", "Type"),
|
||||
}
|
||||
|
||||
self._sub_dict["table_headers_parameters"] = {
|
||||
"name": _translate("BasicHydraulicStructures", "Name"),
|
||||
"value": _translate("BasicHydraulicStructures", "Value"),
|
||||
}
|
||||
@@ -60,6 +60,22 @@ class SetTypeCommand(QUndoCommand):
|
||||
self._hs.insert(self._index, self._new)
|
||||
|
||||
|
||||
class SetEnabledCommand(QUndoCommand):
|
||||
def __init__(self, hs, index, enabled):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._hs = hs
|
||||
self._index = index
|
||||
self._old = not enabled
|
||||
self._new = enabled
|
||||
|
||||
def undo(self):
|
||||
self._hs.basic_structure(self._index).enabled = self._old
|
||||
|
||||
def redo(self):
|
||||
self._hs.basic_structure(self._index).enabled = self._new
|
||||
|
||||
|
||||
class AddCommand(QUndoCommand):
|
||||
def __init__(self, hs, index):
|
||||
QUndoCommand.__init__(self)
|
||||
@@ -96,7 +112,7 @@ class DelCommand(QUndoCommand):
|
||||
self._hs.insert(row, el)
|
||||
|
||||
def redo(self):
|
||||
self._hs.delete_i([self._rows])
|
||||
self._hs.delete_i(self._rows)
|
||||
|
||||
|
||||
class PasteCommand(QUndoCommand):
|
||||
@@ -110,9 +126,28 @@ class PasteCommand(QUndoCommand):
|
||||
self._bhs.reverse()
|
||||
|
||||
def undo(self):
|
||||
self._hs.delete_i(range(self._row, self._row + len(self._bhs))
|
||||
)
|
||||
self._hs.delete_i(range(self._row, self._row + len(self._bhs)))
|
||||
|
||||
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,11 +41,13 @@ 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
|
||||
from View.HydraulicStructures.BasicHydraulicStructures.Translate import BasicHydraulicStructuresTranslate
|
||||
from View.HydraulicStructures.BasicHydraulicStructures.Translate import (
|
||||
BasicHydraulicStructuresTranslate
|
||||
)
|
||||
|
||||
_translate = QCoreApplication.translate
|
||||
|
||||
@@ -77,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(
|
||||
@@ -95,6 +101,7 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
||||
trad=self._trad,
|
||||
data=self._hs,
|
||||
undo=self._undo_stack,
|
||||
parent=self,
|
||||
)
|
||||
|
||||
selectionModel = table.selectionModel()
|
||||
@@ -108,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()
|
||||
@@ -145,27 +167,29 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
||||
def setup_connections(self):
|
||||
self.find(QAction, "action_add").triggered.connect(self.add)
|
||||
self.find(QAction, "action_delete").triggered.connect(self.delete)
|
||||
self._checkbox.stateChanged.connect(self._set_basic_structure_state)
|
||||
self._checkbox.clicked.connect(self._set_basic_structure_state)
|
||||
|
||||
table = self.find(QTableView, "tableView")
|
||||
table.selectionModel()\
|
||||
.selectionChanged\
|
||||
.connect(self.update)
|
||||
.selectionChanged\
|
||||
.connect(self.update)
|
||||
|
||||
self._table.layoutChanged.connect(self.update)
|
||||
|
||||
def index_selected(self):
|
||||
table = self.find(QTableView, "tableView")
|
||||
r = table.selectionModel()\
|
||||
.selectedRows()
|
||||
if len(r)>0:
|
||||
r = table.selectionModel().selectedRows()
|
||||
|
||||
if len(r) > 0:
|
||||
return r[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
def index_selected_row(self):
|
||||
table = self.find(QTableView, "tableView")
|
||||
r = table.selectionModel()\
|
||||
.selectedRows()
|
||||
if len(r)>0:
|
||||
r = table.selectionModel().selectedRows()
|
||||
|
||||
if len(r) > 0:
|
||||
return r[0].row()
|
||||
else:
|
||||
return None
|
||||
@@ -184,6 +208,7 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
||||
|
||||
def add(self):
|
||||
rows = self.index_selected_rows()
|
||||
|
||||
if len(self._hs) == 0 or len(rows) == 0:
|
||||
self._table.add(0)
|
||||
else:
|
||||
@@ -191,6 +216,7 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
||||
|
||||
def delete(self):
|
||||
rows = self.index_selected_rows()
|
||||
|
||||
if len(rows) == 0:
|
||||
return
|
||||
|
||||
@@ -210,6 +236,7 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
||||
|
||||
def _set_checkbox_state(self):
|
||||
row = self.index_selected_row()
|
||||
|
||||
if row is None:
|
||||
self._checkbox.setEnabled(False)
|
||||
self._checkbox.setChecked(True)
|
||||
@@ -219,14 +246,17 @@ class BasicHydraulicStructuresWindow(PamhyrWindow):
|
||||
|
||||
def _set_basic_structure_state(self):
|
||||
row = self.index_selected_row()
|
||||
if row is None:
|
||||
self._checkbox.setEnabled(False)
|
||||
else:
|
||||
self._hs.basic_structure(row).enabled = self._checkbox.isChecked()
|
||||
|
||||
if row is not None:
|
||||
self._table.enabled(
|
||||
row,
|
||||
self._checkbox.isChecked()
|
||||
)
|
||||
|
||||
def update(self):
|
||||
self._set_checkbox_state()
|
||||
self._update_clear_plot()
|
||||
self._update_parameters_table()
|
||||
|
||||
def _update_clear_plot(self):
|
||||
rows = self.index_selected_rows()
|
||||
def _update_parameters_table(self):
|
||||
row = self.index_selected_row()
|
||||
self._table_parameters.update_hs_index(row)
|
||||
@@ -106,13 +106,13 @@ class PlotAC(PamhyrPlot):
|
||||
self.update()
|
||||
|
||||
def update(self):
|
||||
|
||||
if self.line_kp is None:
|
||||
self.draw()
|
||||
else:
|
||||
if self._current_reach is None or self._current_profile is None:
|
||||
self.clear()
|
||||
return
|
||||
|
||||
profile = self._current_profile
|
||||
x = profile.get_station()
|
||||
z = profile.z()
|
||||
|
||||
@@ -80,7 +80,7 @@ class ComboBoxDelegate(QItemDelegate):
|
||||
val
|
||||
)
|
||||
|
||||
self.editor.setCurrentText(index.data(Qt.DisplayRole))
|
||||
self.editor.setCurrentText(str(index.data(Qt.DisplayRole)))
|
||||
return self.editor
|
||||
|
||||
def setEditorData(self, editor, index):
|
||||
|
||||
@@ -92,11 +92,9 @@ class SetEnabledCommand(QUndoCommand):
|
||||
self._new = enabled
|
||||
|
||||
def undo(self):
|
||||
logger.info(f"Undo {self._new} -> {self._old}")
|
||||
self._h_s_lst.get(self._index).enabled = self._old
|
||||
|
||||
def redo(self):
|
||||
logger.info(f"Undo {self._old} -> {self._new}")
|
||||
self._h_s_lst.get(self._index).enabled = self._new
|
||||
|
||||
|
||||
|
||||
@@ -48,7 +48,9 @@ from View.HydraulicStructures.Table import (
|
||||
from View.Network.GraphWidget import GraphWidget
|
||||
from View.HydraulicStructures.Translate import HydraulicStructuresTranslate
|
||||
|
||||
from View.HydraulicStructures.BasicHydraulicStructures.Window import BasicHydraulicStructuresWindow
|
||||
from View.HydraulicStructures.BasicHydraulicStructures.Window import (
|
||||
BasicHydraulicStructuresWindow
|
||||
)
|
||||
|
||||
_translate = QCoreApplication.translate
|
||||
|
||||
|
||||
Reference in new issue
Block a user