diff --git a/src/Model/DIFAdisTS/DIFAdisTS.py b/src/Model/DIFAdisTS/DIFAdisTS.py index 341f71d9..113bbc70 100644 --- a/src/Model/DIFAdisTS/DIFAdisTS.py +++ b/src/Model/DIFAdisTS/DIFAdisTS.py @@ -142,24 +142,24 @@ class DIFAdisTS(SQLSubModel): enabled = (next(it) == 1) owner_scenario = next(it) - dif = cls( + DIF = cls( id=pid, name=name, status=status, owner_scenario=owner_scenario, ) - dif.method = method - dif.dif = dif - dif.b = b - dif.c = c - dif.enabled = enabled + DIF.method = method + DIF.dif = dif + DIF.b = b + DIF.c = c + DIF.enabled = enabled data['dif_default_id'] = pid - dif._data = DIFAdisTSSpec._db_load(execute, data) + DIF._data = DIFAdisTSSpec._db_load(execute, data) loaded.add(pid) - new.append(dif) + new.append(DIF) data["scenario"] = scenario.parent new += cls._db_load(execute, data) @@ -190,7 +190,7 @@ class DIFAdisTS(SQLSubModel): b = self.b c = -1. - if self.dif is not None: + if self.c is not None: c = self.c sql = ( @@ -201,7 +201,7 @@ class DIFAdisTS(SQLSubModel): "VALUES (" + f"{self.id}, '{self._db_format(self._name)}', " + f"'{self._db_format(self._method)}', " + - f"{dif.id}, {b}, {c}, {self._enabled}, " + + f"{dif}, {b}, {c}, {self._enabled}, " + f"{self._status.scenario_id}" + ")" ) diff --git a/src/Model/DIFAdisTS/DIFAdisTSSpec.py b/src/Model/DIFAdisTS/DIFAdisTSSpec.py index ea43585d..85e8f6c0 100644 --- a/src/Model/DIFAdisTS/DIFAdisTSSpec.py +++ b/src/Model/DIFAdisTS/DIFAdisTSSpec.py @@ -33,15 +33,15 @@ class DIFAdisTSSpec(SQLSubModel): _id_cnt = 0 def __init__(self, id: int = -1, method: str = "", - status=None): + status=None, owner_scenario=-1): super(DIFAdisTSSpec, self).__init__() self._status = status if id == -1: - self.id = DIFAdisTSSpec._id_cnt + self._id = DIFAdisTSSpec._id_cnt else: - self.id = id + self._id = id self._method = method self._reach = None @@ -52,7 +52,7 @@ class DIFAdisTSSpec(SQLSubModel): self._c = None self._enabled = True - DIFAdisTSSpec._id_cnt = max(DIFAdisTSSpec._id_cnt + 1, self.id) + DIFAdisTSSpec._id_cnt = max(DIFAdisTSSpec._id_cnt + 1, self._id) @classmethod def _db_create(cls, execute, ext=""): @@ -195,7 +195,7 @@ class DIFAdisTSSpec(SQLSubModel): new_spec.c = c new_spec.enabled = enabled - loaded.add(pid) + loaded.add(id) new.append(new_spec) data["scenario"] = scenario.parent @@ -210,22 +210,23 @@ class DIFAdisTSSpec(SQLSubModel): dif_default = data['dif_default_id'] - execute( + sql = ( "INSERT INTO " + "dif_adists_spec(pamhyr_id, deleted, " + "dif_default, method, reach, " + "start_rk, end_rk, dif, b, c, enabled, scenario) " + "VALUES (" + - f"{self.id}, {self._db_format(self.is_deleted())}" + + f"{self.id}, {self._db_format(self.is_deleted())}, " + f"{dif_default}, " + f"'{self._db_format(self._method)}', " + f"{self._reach}, " + f"{self._start_rk}, {self._end_rk}, " + f"{self._dif}, {self._b}, {self._c}, " + - f"{self._enabled}" + + f"{self._enabled}, " + f"{self._status.scenario_id}" + ")" ) + execute(sql) return True diff --git a/src/View/DIFAdisTS/Table.py b/src/View/DIFAdisTS/Table.py index 51bff003..b9b51c5d 100644 --- a/src/View/DIFAdisTS/Table.py +++ b/src/View/DIFAdisTS/Table.py @@ -118,9 +118,16 @@ class DIFTableModel(PamhyrTableModel): super(DIFTableModel, self).__init__(data=data, **kwargs) self._data = data + if self._undo is not None: + self._undo.indexChanged.connect(lambda _: self.update()) def _setup_lst(self): - self._lst = self._data._data + self._lst = list( + filter( + lambda dif: dif._deleted is False, + self._data._data + ) + ) def rowCount(self, parent): return len(self._lst) @@ -183,7 +190,10 @@ class DIFTableModel(PamhyrTableModel): if self._headers[column] != "reach": self._undo.push( SetCommandSpec( - self._lst, row, self._headers[column], value + self._lst, + row, + self._headers[column], + value ) ) elif self._headers[column] == "reach": @@ -211,25 +221,34 @@ class DIFTableModel(PamhyrTableModel): ) ) + self._setup_lst() self.endInsertRows() self.layoutChanged.emit() def delete(self, rows, parent=QModelIndex()): self.beginRemoveRows(parent, rows[0], rows[-1]) + data_rows = { + id(dif): i for i, dif in enumerate(self._data._data) + } self._undo.push( DelCommand( - self._data, self._lst, rows + self._data, + self._data._data, + [data_rows[id(self._lst[row])] for row in rows + if 0 <= row < len(self._lst)] ) ) - + self._setup_lst() self.endRemoveRows() self.layoutChanged.emit() def undo(self): self._undo.undo() + self._setup_lst() self.layoutChanged.emit() def redo(self): self._undo.redo() + self._setup_lst() self.layoutChanged.emit() diff --git a/src/View/DIFAdisTS/UndoCommand.py b/src/View/DIFAdisTS/UndoCommand.py index 81078251..6f543485 100644 --- a/src/View/DIFAdisTS/UndoCommand.py +++ b/src/View/DIFAdisTS/UndoCommand.py @@ -136,11 +136,11 @@ class SetCommandSpec(QUndoCommand): class AddCommand(QUndoCommand): - def __init__(self, data, ics_spec, index): + def __init__(self, data, dif_spec, index): QUndoCommand.__init__(self) self._data = data - self._ics_spec = ics_spec + self._dif_spec = dif_spec self._index = index self._new = None @@ -155,17 +155,16 @@ class AddCommand(QUndoCommand): class DelCommand(QUndoCommand): - def __init__(self, data, ics_spec, rows): + def __init__(self, data, dif_spec, rows): QUndoCommand.__init__(self) self._data = data - self._ics_spec = ics_spec + self._dif_spec = dif_spec self._rows = rows - # self._data = data self._ic = [] for row in rows: - self._ic.append((row, self._ics_spec[row])) + self._ic.append((row, self._dif_spec[row])) self._ic.sort() def undo(self): diff --git a/src/View/DIFAdisTS/Window.py b/src/View/DIFAdisTS/Window.py index 2fa531c3..7191491f 100644 --- a/src/View/DIFAdisTS/Window.py +++ b/src/View/DIFAdisTS/Window.py @@ -51,6 +51,8 @@ from View.DIFAdisTS.Table import ( DIFTableModel, ComboBoxDelegate, ) +from View.DIFAdisTS.UndoCommand import SetCommand + from View.DIFAdisTS.translate import DIFAdisTSTranslate from Solver.Mage import Mage8 @@ -272,11 +274,25 @@ class DIFAdisTSWindow(PamhyrWindow): self._update() def _undo(self): - self._table.undo() + undo_stack = self._undo_stack + if undo_stack is None or not undo_stack.canUndo(): + return + + if isinstance(undo_stack.command(undo_stack.index() - 1), SetCommand): + self._table.undo() + else: + self._table_spec.undo() self._update() def _redo(self): - self._table.redo() + undo_stack = self._undo_stack + if undo_stack is None or not undo_stack.canRedo(): + return + + if isinstance(undo_stack.command(undo_stack.index()), SetCommand): + self._table.redo() + else: + self._table_spec.redo() self._update() def add(self):