diff --git a/src/Model/Friction/FrictionList.py b/src/Model/Friction/FrictionList.py index 21e10695bbf23f6b2b80c4c47a133ba80e021d65..0db5e86ff1a1422c4346f965f7c7c98b67d0eef3 100644 --- a/src/Model/Friction/FrictionList.py +++ b/src/Model/Friction/FrictionList.py @@ -22,25 +22,17 @@ from copy import copy from tools import trace, timer from Model.DB import SQLSubModel +from Model.Tools.PamhyrList import PamhyrModelList + from Model.Friction.Friction import Friction logger = logging.getLogger() -class FrictionList(SQLSubModel): +class FrictionList(PamhyrModelList): _sub_classes = [ Friction ] - def __init__(self, status = None): - super(FrictionList, self).__init__() - - self._status = status - self._frictions = [] - - @classmethod - def _sql_create(cls, execute): - return cls._create_submodel(execute) - @classmethod def _sql_update_0_0_1(cls, execute, version): execute("ALTER TABLE `section` RENAME TO `friction`") @@ -57,7 +49,7 @@ class FrictionList(SQLSubModel): def _sql_load(cls, execute, data = None): new = cls(status = data['status']) - new._frictions = Friction._sql_load( + new._lst = Friction._sql_load( execute, data ) @@ -69,70 +61,19 @@ class FrictionList(SQLSubModel): ok = True ind = 0 - for friction in self._frictions: + for friction in self._lst: data["ind"] = ind ok &= friction._sql_save(execute, data = data) ind += 1 return ok - def __len__(self): - return len(self._frictions) - @property def frictions(self): - return self._frictions.copy() - - def get(self, row): - return self._frictions[row] - - def set(self, row, new): - self._frictions[row] = new - self._status.modified() + return self.lst def new(self, index): n = Friction(status = self._status) - self._frictions.insert(index, n) + self._lst.insert(index, n) self._status.modified() return n - - def insert(self, index, new): - self._frictions.insert(index, new) - self._status.modified() - - def delete(self, frictions): - for friction in frictions: - self._frictions.remove(friction) - self._status.modified() - - def delete_i(self, indexes): - frictions = list( - map( - lambda x: x[1], - filter( - lambda x: x[0] in indexes, - enumerate(self._frictions) - ) - ) - ) - self.delete(frictions) - - def sort(self, reverse=False, key=None): - self._frictions.sort(reverse=reverse, key=key) - self._status.modified() - - def move_up(self, index): - if index < len(self._frictions): - next = index - 1 - - l = self._frictions - l[index], l[next] = l[next], l[index] - self._status.modified() - - def move_down(self, index): - if index >= 0: - prev = index + 1 - - l = self._frictions - l[index], l[prev] = l[prev], l[index] - self._status.modified() diff --git a/src/Model/River.py b/src/Model/River.py index b1969543e04a56cf179e9654b566833e34e6e2bb..77daf43a0ce310d0e937e6743f2b0b119b15b836 100644 --- a/src/Model/River.py +++ b/src/Model/River.py @@ -311,10 +311,6 @@ class River(Graph, SQLSubModel): self._save_submodel(execute, objs, data) return True - @property - def frictions(self): - return self._frictions - @property def boundary_condition(self): return self._boundary_condition diff --git a/src/Model/Stricklers/StricklersList.py b/src/Model/Stricklers/StricklersList.py index 82076bb1b4d1a3aca3cc3215689d6a000b1c8c01..640c110cc991e9129288e478f272b648c22d79ad 100644 --- a/src/Model/Stricklers/StricklersList.py +++ b/src/Model/Stricklers/StricklersList.py @@ -20,20 +20,14 @@ from tools import trace, timer from Model.DB import SQLSubModel from Model.Saved import SavedStatus +from Model.Tools.PamhyrList import PamhyrModelList from Model.Stricklers.Stricklers import Stricklers -class StricklersList(SQLSubModel): +class StricklersList(PamhyrModelList): _sub_classes = [ Stricklers, ] - def __init__(self, status = None): - if status is None: - status = SavedStatus() - self._status = status - - self._stricks = [] - @classmethod def _sql_create(cls, execute): return cls._create_submodel(execute) @@ -46,7 +40,7 @@ class StricklersList(SQLSubModel): def _sql_load(cls, execute, data = None): new = cls(status = data["status"]) - new._stricks = Stricklers._sql_load( + new._lst = Stricklers._sql_load( execute, data = data ) @@ -56,60 +50,26 @@ class StricklersList(SQLSubModel): def _sql_save(self, execute, data = None): execute("DELETE FROM stricklers") - objs = self._stricks + objs = self._lst return self._save_submodel(execute, objs, data) - def __len__(self): - return len(self._stricks) - @property def stricklers(self): - return self._stricks.copy() - - def get(self, index): - if 0 <= index < len(self._stricks): - return self._stricks[index] - - return None - - def insert(self, index, strick): - self._stricks.insert(index, strick) - self._status.modified() + return self.lst - def add(self, index): + def new(self, index): s = Stricklers(status = self._status) self.insert(index, s) return s - def delete(self, stricks): - self._stricks = list( - filter( - lambda s: s not in stricks, - self._stricks - ) - ) - self._status.modified() - - def delete_i(self, indexes): - stricks = set( - map( - lambda e: e[1], - filter( - lambda e: e[0] in indexes, - enumerate(self._stricks) - ) - ) - ) - self.delete(stricks) - @timer def sort(self, reverse:bool = False, key=None): f = lambda st: st.name if key is not None: f = key - self._stricks = sorted( - self._stricks, + self._lst = sorted( + self._lst, key = f, reverse = reverse, ) diff --git a/src/Model/Tools/PamhyrList.py b/src/Model/Tools/PamhyrList.py new file mode 100644 index 0000000000000000000000000000000000000000..3446d85b04da8f473b1086e707d35f09b9cddc4b --- /dev/null +++ b/src/Model/Tools/PamhyrList.py @@ -0,0 +1,125 @@ +# PamhyrList.py -- Pamhyr Abstract List object for the Model +# Copyright (C) 2023 INRAE +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +# -*- coding: utf-8 -*- + +import logging + +from copy import copy +from tools import trace, timer + +from Model.DB import SQLSubModel + +logger = logging.getLogger() + +class PamhyrModelList(SQLSubModel): + _sub_classes = [ + ] + + def __init__(self, status = None): + super(PamhyrModelList, self).__init__() + + self._status = status + self._lst = [] + + ####### + # SQL # + ####### + + @classmethod + def _sql_create(cls, execute): + return cls._create_submodel(execute) + + @classmethod + def _sql_update(cls, execute, version): + raise NotImplementedMethodeError(cls, cls._sql_update) + + @classmethod + def _sql_load(cls, execute, data = None): + raise NotImplementedMethodeError(cls, cls._sql_load) + + def _sql_save(self, execute, data = None): + raise NotImplementedMethodeError(self, self._sql_save) + + ################ + # MODEL METHOD # + ################ + + def __len__(self): + return len(self._lst) + + @property + def lst(self): + return self._lst.copy() + + def get(self, row): + return self._lst[row] + + def set(self, row, new): + self._lst[row] = new + self._status.modified() + + def new(self, index): + """Create new elements and add it to list + + Args: + index: The index of new elements + + Returns: + The new elements + """ + raise NotImplementedMethodeError(self, self._sql_save) + + def insert(self, index, new): + self._lst.insert(index, new) + self._status.modified() + + def delete(self, lst): + for el in lst: + self._lst.remove(el) + self._status.modified() + + def delete_i(self, indexes): + lst = list( + map( + lambda x: x[1], + filter( + lambda x: x[0] in indexes, + enumerate(self._lst) + ) + ) + ) + self.delete(lst) + + def sort(self, reverse=False, key=None): + self._lst.sort(reverse=reverse, key=key) + self._status.modified() + + def move_up(self, index): + if index < len(self._lst): + next = index - 1 + + l = self._lst + l[index], l[next] = l[next], l[index] + self._status.modified() + + def move_down(self, index): + if index >= 0: + prev = index + 1 + + l = self._lst + l[index], l[prev] = l[prev], l[index] + self._status.modified() diff --git a/src/View/Stricklers/UndoCommand.py b/src/View/Stricklers/UndoCommand.py index ee75cf99c9b2205a87853d86a0bc7ebfa8208ef6..52988b262eb2b66e66e822602c513cf11c33f16a 100644 --- a/src/View/Stricklers/UndoCommand.py +++ b/src/View/Stricklers/UndoCommand.py @@ -100,7 +100,7 @@ class AddCommand(QUndoCommand): def redo(self): if self._new is None: - self._new = self._data.add(self._index) + self._new = self._data.new(self._index) else: self._data.insert(self._index, self._new)