SectionList.py 1.80 KiB
# -*- coding: utf-8 -*-

from copy import copy
from tools import trace, timer

from Model.Section.Section import Section

class SectionList(object):
    def __init__(self, status = None):
        super(SectionList, self).__init__()

        self._status = status
        self._sections = []

    def __len__(self):
        return len(self._sections)

    @property
    def sections(self):
        return self._sections.copy()

    def get(self, row):
        return self._sections[row]

    def set(self, row, new):
        self._sections[row] = new
        self._status.modified()

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

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

    def delete(self, sections):
        for section in sections:
            self._sections.remove(section)
        self._status.modified()

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

    def sort(self, reverse=False, key=None):
        self._sections.sort(reverse=reverse, key=key)
        self._status.modified()

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

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

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

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