Commit 89b9fd21 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby
Browse files

Model: Add a Pamhyr List Abstract implementation and use it for

Stricklers and friction.
Showing with 141 additions and 119 deletions
+141 -119
...@@ -22,25 +22,17 @@ from copy import copy ...@@ -22,25 +22,17 @@ from copy import copy
from tools import trace, timer from tools import trace, timer
from Model.DB import SQLSubModel from Model.DB import SQLSubModel
from Model.Tools.PamhyrList import PamhyrModelList
from Model.Friction.Friction import Friction from Model.Friction.Friction import Friction
logger = logging.getLogger() logger = logging.getLogger()
class FrictionList(SQLSubModel): class FrictionList(PamhyrModelList):
_sub_classes = [ _sub_classes = [
Friction 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 @classmethod
def _sql_update_0_0_1(cls, execute, version): def _sql_update_0_0_1(cls, execute, version):
execute("ALTER TABLE `section` RENAME TO `friction`") execute("ALTER TABLE `section` RENAME TO `friction`")
...@@ -57,7 +49,7 @@ class FrictionList(SQLSubModel): ...@@ -57,7 +49,7 @@ class FrictionList(SQLSubModel):
def _sql_load(cls, execute, data = None): def _sql_load(cls, execute, data = None):
new = cls(status = data['status']) new = cls(status = data['status'])
new._frictions = Friction._sql_load( new._lst = Friction._sql_load(
execute, data execute, data
) )
...@@ -69,70 +61,19 @@ class FrictionList(SQLSubModel): ...@@ -69,70 +61,19 @@ class FrictionList(SQLSubModel):
ok = True ok = True
ind = 0 ind = 0
for friction in self._frictions: for friction in self._lst:
data["ind"] = ind data["ind"] = ind
ok &= friction._sql_save(execute, data = data) ok &= friction._sql_save(execute, data = data)
ind += 1 ind += 1
return ok return ok
def __len__(self):
return len(self._frictions)
@property @property
def frictions(self): def frictions(self):
return self._frictions.copy() return self.lst
def get(self, row):
return self._frictions[row]
def set(self, row, new):
self._frictions[row] = new
self._status.modified()
def new(self, index): def new(self, index):
n = Friction(status = self._status) n = Friction(status = self._status)
self._frictions.insert(index, n) self._lst.insert(index, n)
self._status.modified() self._status.modified()
return n 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()
...@@ -311,10 +311,6 @@ class River(Graph, SQLSubModel): ...@@ -311,10 +311,6 @@ class River(Graph, SQLSubModel):
self._save_submodel(execute, objs, data) self._save_submodel(execute, objs, data)
return True return True
@property
def frictions(self):
return self._frictions
@property @property
def boundary_condition(self): def boundary_condition(self):
return self._boundary_condition return self._boundary_condition
......
...@@ -20,20 +20,14 @@ from tools import trace, timer ...@@ -20,20 +20,14 @@ from tools import trace, timer
from Model.DB import SQLSubModel from Model.DB import SQLSubModel
from Model.Saved import SavedStatus from Model.Saved import SavedStatus
from Model.Tools.PamhyrList import PamhyrModelList
from Model.Stricklers.Stricklers import Stricklers from Model.Stricklers.Stricklers import Stricklers
class StricklersList(SQLSubModel): class StricklersList(PamhyrModelList):
_sub_classes = [ _sub_classes = [
Stricklers, Stricklers,
] ]
def __init__(self, status = None):
if status is None:
status = SavedStatus()
self._status = status
self._stricks = []
@classmethod @classmethod
def _sql_create(cls, execute): def _sql_create(cls, execute):
return cls._create_submodel(execute) return cls._create_submodel(execute)
...@@ -46,7 +40,7 @@ class StricklersList(SQLSubModel): ...@@ -46,7 +40,7 @@ class StricklersList(SQLSubModel):
def _sql_load(cls, execute, data = None): def _sql_load(cls, execute, data = None):
new = cls(status = data["status"]) new = cls(status = data["status"])
new._stricks = Stricklers._sql_load( new._lst = Stricklers._sql_load(
execute, execute,
data = data data = data
) )
...@@ -56,60 +50,26 @@ class StricklersList(SQLSubModel): ...@@ -56,60 +50,26 @@ class StricklersList(SQLSubModel):
def _sql_save(self, execute, data = None): def _sql_save(self, execute, data = None):
execute("DELETE FROM stricklers") execute("DELETE FROM stricklers")
objs = self._stricks objs = self._lst
return self._save_submodel(execute, objs, data) return self._save_submodel(execute, objs, data)
def __len__(self):
return len(self._stricks)
@property @property
def stricklers(self): def stricklers(self):
return self._stricks.copy() return self.lst
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()
def add(self, index): def new(self, index):
s = Stricklers(status = self._status) s = Stricklers(status = self._status)
self.insert(index, s) self.insert(index, s)
return 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 @timer
def sort(self, reverse:bool = False, key=None): def sort(self, reverse:bool = False, key=None):
f = lambda st: st.name f = lambda st: st.name
if key is not None: if key is not None:
f = key f = key
self._stricks = sorted( self._lst = sorted(
self._stricks, self._lst,
key = f, key = f,
reverse = reverse, reverse = reverse,
) )
......
# 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()
...@@ -100,7 +100,7 @@ class AddCommand(QUndoCommand): ...@@ -100,7 +100,7 @@ class AddCommand(QUndoCommand):
def redo(self): def redo(self):
if self._new is None: if self._new is None:
self._new = self._data.add(self._index) self._new = self._data.new(self._index)
else: else:
self._data.insert(self._index, self._new) self._data.insert(self._index, self._new)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment