SedimentLayer.py 5.41 KiB
# -*- coding: utf-8 -*-

from tools import trace, timer

from Model.DB import SQLSubModel
from Model.Except import NotImplementedMethodeError

class Layer(SQLSubModel):
    _sub_classes = []
    _id_cnt = 0

    def __init__(self,
                 id:int = -1, name:str = "",
                 type = "", height = 0,
                 sl = None, status = None):
        super(Layer, self).__init__()

        self._status = status
        self._sl = sl

        self._name = name
        self._type = type
        self._height = height

        if id == -1:
            self.id = Layer._id_cnt
        else:
            self.id = id


    @classmethod
    def _sql_create(cls, execute):
        execute("""
          CREATE TABLE sedimentary_layer_layer(
            id INTEGER NOT NULL PRIMARY KEY,
            ind INTEGER NOT NULL,
            name TEXT NOT NULL,
            type TEXT NOT NULL,
            height INTEGER NOT NULL,
            sl INTEGER,
            FOREIGN KEY(sl) REFERENCES sedimentary_layer(id)
          )
        """)

        return cls._create_submodel(execute)

    @classmethod
    def _sql_update(cls, execute, version):
        return True

    @classmethod
    def _sql_load(cls, execute, data = None):
        new = []
        sl = data["sl"]

        table = execute(
            "SELECT id, ind, name, type, height " +
            "FROM sedimentary_layer_layer " +
            f"WHERE sl = {sl.id}"
        )

        for _ in table:
            new.append(None)

        for row in table:
            ind = row[1]

            layer = cls(
                id = row[0], name = row[2],
                type = row[3], height = row[4],
                sl = sl, status = data['status']
            )

            new[ind] = layer

        return new

    def _sql_save(self, execute, data = None):
        ind = data["ind"]

        sql = (
            "INSERT INTO " +
            "sedimentary_layer_layer(id, ind, name, type, height, sl) "+
            "VALUES (" +
            f"{self.id}, {ind}, '{self._sql_format(self._name)}', " +
            f"'{self._sql_format(self._type)}', '{self._height}', {self._sl.id}" +
            ")"
        )
        execute(sql)

        return True


class SedimentLayer(SQLSubModel):
    _sub_classes = [Layer]
    _id_cnt = 0

    def __init__(self, id:int = -1,
                 name:str = "", comment:str = "",
                 status = None):
        super(SedimentLayer, self).__init__()

        self._status = status
        self._name = name
        self._comment = comment
        self._layers = []

        if id == -1:
            self.id = SedimentLayer._id_cnt
        else:
            self.id = id

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, name):
        self._name = name

    @property
    def comment(self):
        return self._comment

    @comment.setter
    def comment(self, comment):
        self._comment = comment

    @classmethod
    def _sql_create(cls, execute):
        execute("""
          CREATE TABLE sedimentary_layer(
            id INTEGER NOT NULL PRIMARY KEY,
            name TEXT NOT NULL,
            comment TEXT NOT NULL
          )
        """)

        return cls._create_submodel(execute)

    @classmethod
    def _sql_update(cls, execute, version):
        return True

    @classmethod
    def _sql_load(cls, execute, data = None):
        new = []

        table = execute(
            "SELECT id, name, comment" +
            "FROM sedimentary_layer "
        )

        for row in table:
            sl = cls(
                id = row[0],
                name = row[1],
                comment = row[2],
                status = data['status']
            )

            data["sl"] = sl.id
            sl._layers = Layer._sql_load(execute, data)

            new.append(sl)

        return new

    def _sql_save(self, execute, data = None):
        if data is None:
            data = {}

        sql = (
            "INSERT INTO sedimentary_layer (id, name) "+
            f"VALUES ({self.id}, '{self._sql_format(self._name)}')"
        )
        execute(sql)

        ind = 0
        for l in self._layers:
            data["ind"] = ind
            l._sql_save(execute, data)
            ind += 1

        return True

    def get(self, index):
        return self._layers[index]

    def set(self, index, new):
        self._layers[index] = new
        self._status.modified()

    def insert(self, index, new):
        self._layers.insert(index, new)
        self._status.modified()

    def new(self, index):
        n = Layer(status = self._status)
        self.insert(index, n)
        self._status.modified()
        return n

    def delete(self, els):
        for el in els:
            self._layers.remove(el)
        self._status.modified()

    def delete_i(self, indexes):
        els = list(
            map(
                lambda x: x[1],
                filter(
                    lambda x: x[0] in indexes,
                    enumerate(self._layers)
                )
            )
        )
        self.delete(els)

    def move_up(self, index):
        if index < len(self._layers):
            next = index - 1

            l = self._layers
            l[index], l[next] = l[next], l[index]
            self._status.modified()

    def move_down(self, index):
        if index >= 0:
            prev = index + 1

            l = self._layers
            l[index], l[prev] = l[prev], l[index]
            self._status.modified()