<commit_before>"""Layout like layouts in qt(QGridLayout)."""
# coding=utf-8

from abc import ABCMeta, abstractmethod
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem


class Layout(metaclass=ABCMeta):
    """Base layout class(For inheritance only)."""

    def __init__(self, scene: QGraphicsScene):
        self._scene = scene
        # Keeps all items in layout
        self.__items = []
        # State of layout
        self.__pause = False
        # Parent item for group
        self.__rootItem = None

    @abstractmethod
    def addItem(self):
        pass

    @abstractmethod
    def show(self):
        """Add items to scene."""
        pass

    @abstractmethod
    def hide(self):
        """Remove items from scene."""
        pass

    def pause(self):
        pass

    def update(self):
        """Updates all items in layout."""
        pass


class GridLayout(Layout):
    """Grid layout for managing simple menus."""

    # Dummy item
    DUMMY = ''

    def __init__(self, scene: QGraphicsScene):
        super().__init__(scene)
        self._scene = scene
        self.__width, self.__height = scene.width(), scene.height()
        self.__rectWidth, self.__rectHeight = None, None
        self.rows, self.colls = 1, 1
        self.items = [[]]

    def hasItem(self):
        for row in self.items:
            for item in row:
                if item != self.DUMMY:
                    return True

        return False

    def addItem(self, item: QGraphicsItem, row: int, coll: int):
        if item != self.DUMMY:
            if not self.hasItem():
                self.__rootItem = item
                self.__rootItem.setParentItem(None)
            else:
                item.setParentItem(self.__rootItem)

        self.rows = self.rows if row + 1 <= self.rows else row + 1
        self.colls = self.colls if coll + 1 <= self.colls else coll + 1
        self.resize()

        self.items[row][coll] = item
        self.repaint()

    def resize(self):
        """Fill item matrix to fit in all colls and rows."""
        for x in range(self.rows + 1 - len(self.items)):
            self.items.append([])

        for row in range(len(self.items)):
            for y in range(self.colls - len(self.items[row])):
                self.items[row].append(self.DUMMY)

        self.__rectWidth = self.__width / self.colls
        self.__rectHeight = self.__height / self.rows

    def repaint(self):
        """Redraws all widgets after new added and matrix resized."""
        for row in range(self.rows):
            for coll in range(self.colls):
                item = self.items[row][coll]

                if item == self.DUMMY:
                    continue

                rect = item.boundingRect()
                width, height = rect.width(), rect.height()

                # Center of scene rect in which we will place widget
                sceneX = self.__rectWidth * coll + self.__rectWidth / 2
                sceneY = self.__rectHeight * row + self.__rectHeight / 2
<commit_msg>added coll and rowspan to layout<commit_after>"""Layout like layouts in qt(QGridLayout)."""
# coding=utf-8

from abc import ABCMeta, abstractmethod
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsItem


class Layout(metaclass=ABCMeta):
    """Base layout class(For inheritance only)."""

    def __init__(self, scene: QGraphicsScene):
        self._scene = scene
        # Keeps all items in layout
        self.__items = []
        # State of layout
        self.__pause = False
        # Parent item for group
        self.__rootItem = None

    @abstractmethod
    def addItem(self):
        pass

    @abstractmethod
    def show(self):
        """Add items to scene."""
        pass

    @abstractmethod
    def hide(self):
        """Remove items from scene."""
        pass

    def pause(self):
        pass

    def update(self):
        """Updates all items in layout."""
        pass


class GridLayout(Layout):
    """Grid layout for managing simple menus."""

    # Dummy item
    DUMMY = ''

    def __init__(self, scene: QGraphicsScene):
        super().__init__(scene)
        self._scene = scene
        self.__width, self.__height = scene.width(), scene.height()
        self.__rectWidth, self.__rectHeight = None, None
        self.rows, self.colls = 1, 1
        self.items = [[]]

    def hasItem(self):
        for row in self.items:
            for item, _, _ in row:
                if item != self.DUMMY:
                    return True

        return False

    def addItem(self, item: QGraphicsItem, row, coll, rowspan=1, collspan=1):
        if item != self.DUMMY:
            if not self.hasItem():
                self.__rootItem = item
                self.__rootItem.setParentItem(None)
            else:
                item.setParentItem(self.__rootItem)

        self.rows = self.rows if row + rowspan <= self.rows else row + 1
        self.colls = self.colls if coll + collspan <= self.colls else coll + 1
        self.resize()

        self.items[row][coll] = (item, rowspan, collspan)
        self.repaint()

    def resize(self):
        """Fill item matrix to fit in all colls and rows."""
        for x in range(self.rows + 1 - len(self.items)):
            self.items.append([])

        for row in range(len(self.items)):
            for y in range(self.colls - len(self.items[row])):
                self.items[row].append((self.DUMMY, 1, 1))

        self.__rectWidth = self.__width / self.colls
        self.__rectHeight = self.__height / self.rows

    def repaint(self):
        """Redraws all widgets after new added and matrix resized."""
        for row in range(self.rows):
            for coll in range(self.colls):
                item, rspan, cspan = self.items[row][coll]

                if item == self.DUMMY:
                    continue

                rect = item.boundingRect()
                width, height = rect.width(), rect.height()

                # Center of scene rect in which we will place widget
                sceneX = self.__rectWidth * coll + self.__rectWidth * cspan / 2
                sceneY = (self.__rectHeight * row +
                          self.__rectHeight * rspan / 2)

{'fraction_non_alphanumeric': 0.10590894413948983, 'fraction_numerical': 0.0038747174685179204, 'mean_word_length': 1.8443526170798898, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1247061', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1696, 'n_words': 517}