<commit_before># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:

"""Implementation of a Tree model for surface data display.

"""

import numpy as np
from PyQt4.QtCore import *

class TreeModel(QAbstractItemModel):
    """Definition of class TreeModel."""
    # customizes signal
    repaint_slices = pyqtSignal(int, name='repaint_slices')

    def __init__(self, hemisphere_list, parent=None):
        """Initialize an instance."""
        super(TreeModel, self).__init__(parent)
        
        self._data = hemisphere_list
        self.checkLisk = {}

    def index(self, row, column, parent):
        """Return the index of item in the model."""
        if not self.hasIndex(row, column, parent):
            return QModelIndex()

        if not parent.isValid():
            parentItem = self._data[row]
            return self.createIndex(row, column, parentItem)
        else:
            parentItem = parent.internalPointer()
            if parentItem in self._data:

                childItem_idx = parentItem.overlay_idx[
                                    parentItem.overlay_count()-1-row]
                childItem = parentItem.overlay_list[childItem_idx]
                if childItem:
                    return self.createIndex(row, column, childItem)
                else:
                    return QModelIndex()
            else:
                return QModelIndex()

    def parent(self, index):
        """Return the parent of the model item with the given index."""
        if not index.isValid():
            return QModelIndex()

        item = index.internalPointer()
        if item in self._data:
            return QModelIndex()
        else:
            for hemi in self._data:
                if item in hemi.overlay_list:
                    return self.createIndex(self._data.index(hemi), 0, hemi)

    def rowCount(self, parent):
        """Return the number of rows for display."""
        if parent.isValid():
            if parent.internalPointer() in self._data:
                return self._data[parent.row()].overlay_count()
            else:
                return 0
        else:
            return len(self._data)

    def columnCount(self, parent):
        """Return the number of overlays in a hemispheres."""
        return 1
        
    def data(self, index, role):
        """Return specific data."""
        if not index.isValid():
            return None

        item = index.internalPointer()

        if item in self._data:
            if role == Qt.UserRole + 2:
                return item.get_alpha()
            elif role == Qt.UserRole + 3:
                return item.get_colormap()
        else:
            if role == Qt.UserRole:
                return item.get_min()
            elif role == Qt.UserRole + 1:
                return item.get_max()
            elif role == Qt.UserRole + 2:
                return item.get_alpha()
            elif role == Qt.UserRole + 3:
                return item.get_colormap()

        if role == Qt.DisplayRole or role == Qt.EditRole:
           return item.name

        if role == Qt.CheckStateRole:
            if index.column() == 0:
                return self.checkState(index)

    def checkState(self, index):
        if index in self.checkLisk:
            return self.checkLisk[index]
        else:
            return Qt.Unchecked

    def flags(self, index):
        if not index.isValid():
            return Qt.NoItemFlags
        result = Qt.ItemIsEnabled | Qt.ItemIsSelectable
        if index.column() == 0:
            result |= Qt.ItemIsUserCheckable

        return result

    def headerData(self, section, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return 'Name'
        return None

    def setData(self, index, value, role=Qt.EditRole):
        if not index.isValid():
            return None

        item = index.internalPointer()
        if role == Qt.EditRole:
            value_str = value.toPyObject()
            if not value_str == '':
                if not item.get_name() == value_str:
                    item.set_name(str(value_str))
                else:
                    return False
            else:
                return False
        elif role == Qt.CheckStateRole and index.column() == 0:
            self.checkLisk[index] = value

        if item in self._data:
            if role == Qt.UserRole + 2:
                if not item.get_alpha == value:
                    item.set_alpha(value)
            elif role == Qt.UserRole + 3:
                if not item.get_colormap == value:
                    item.set_colormap(value)
        else:
            if role == Qt.UserRole:
                if not str(item.get_min()) == value:
                    item.set_min(value)
                else:
                    return False
            elif role == Qt.UserRole + 1:
                if not str(item.get_max()) == value:
                    item.set_max(value)
                else:
                    return False
            elif role == Qt.UserRole + 2:
                if not item.get_alpha() == value:
                    item.set_alpha(value)
                else:
                    return False
            elif role == Qt.UserRole + 3:
                if not item.get_colormap() == value:
                    item.set_colormap(value)
                else:
                    return False
        self.dataChanged.emit(index, index)
        return True

    def moveUp(self, index):
        item = index.internalPointer()
        for hemi in self._data:
            if item in hemi.overlay_list:
                idx = hemi.overlay_list.index(item)
                hemi.overlay_up(idx)
        # print "moveup"

    def moveDown(self, index):
        item = index.internalPointer()
        for hemi in self._data:
            if item in hemi.overlay_list:
                idx = hemi.overlay_list.index(item)
                hemi.overlay_down(idx)

    def setCurrentIndex(self, index):<commit_msg>item up and down<commit_after># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:

"""Implementation of a Tree model for surface data display.

"""

import numpy as np
from PyQt4.QtCore import *


class TreeModel(QAbstractItemModel):
    """Definition of class TreeModel."""
    def __init__(self, hemisphere_list, parent=None):
        """Initialize an instance."""
        super(TreeModel, self).__init__(parent)
        
        self._data = hemisphere_list

    def get_data(self):
        return self._data

    def index(self, row, column, parent):
        """Return the index of item in the model."""
        if not self.hasIndex(row, column, parent):
            return QModelIndex()

        if not parent.isValid():
            parentItem = self._data[row]
            return self.createIndex(row, column, parentItem)
        else:
            parentItem = parent.internalPointer()
            if parentItem in self._data:

                childItem_idx = parentItem.overlay_idx[
                                    parentItem.overlay_count()-1-row]
                childItem = parentItem.overlay_list[childItem_idx]
                if childItem:
                    return self.createIndex(row, column, childItem)
                else:
                    return QModelIndex()
            else:
                return QModelIndex()

    def parent(self, index):
        """Return the parent of the model item with the given index."""
        if not index.isValid():
            return QModelIndex()

        item = index.internalPointer()
        if item in self._data:
            return QModelIndex()
        else:
            for hemi in self._data:
                if item in hemi.overlay_list:
                    return self.createIndex(self._data.index(hemi), 0, hemi)

    def rowCount(self, parent):
        """Return the number of rows for display."""
        if parent.isValid():
            if parent.internalPointer() in self._data:
                return self._data[parent.row()].overlay_count()
            else:
                return 0
        else:
            return len(self._data)

    def columnCount(self, parent):
        """Return the number of overlays in a hemispheres."""
        return 1
        
    def data(self, index, role):
        """Return specific data."""
        if not index.isValid():
            return None

        item = index.internalPointer()
        # print "--------------"
        # print item.get_name()
        # print index.row(), index.column()
        # if index.parent().internalPointer():
        #     print index.parent().internalPointer().get_name()


        if item in self._data:
            if role == Qt.UserRole + 2:
                return item.get_alpha()
            elif role == Qt.UserRole + 3:
                return item.get_colormap()
        else:
            if role == Qt.UserRole:
                return item.get_min()
            elif role == Qt.UserRole + 1:
                return item.get_max()
            elif role == Qt.UserRole + 2:
                return item.get_alpha()
            elif role == Qt.UserRole + 3:
                return item.get_colormap()

        if role == Qt.DisplayRole or role == Qt.EditRole:
           return item.get_name()

        if role == Qt.CheckStateRole:

            if index.column() == 0:
                if item.is_visible():
                    return Qt.Checked
                else:
                    return Qt.Unchecked

    def flags(self, index):
        item = index.internalPointer()
        if not index.isValid():
            return Qt.NoItemFlags
        result = Qt.ItemIsEnabled | Qt.ItemIsSelectable
        if index.column() == 0:
            result |= Qt.ItemIsUserCheckable
        if item in self._data:
             result |= Qt.ItemIsTristate

        return result

    def headerData(self, section, orientation, role=None):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return 'Name'
        return None

    def setData(self, index, value, role=Qt.EditRole):
        if not index.isValid():
            return None

        item = index.internalPointer()
        if role == Qt.EditRole:
            value_str = value.toPyObject()
            if not value_str == '':
                if not item.get_name() == value_str:
                    item.set_name(str(value_str))
                else:
                    return False
            else:
                return False
        elif role == Qt.CheckStateRole and index.column() == 0:
            if value == Qt.Unchecked:
                item.set_visible(False)
            else:
                item.set_visible(True)

        if item in self._data:
            if role == Qt.UserRole + 2:
                if not item.get_alpha == value:
                    item.set_alpha(value)
            elif role == Qt.UserRole + 3:
                if not item.get_colormap == value:
                    item.set_colormap(value)
        else:
            if role == Qt.UserRole:
                if not str(item.get_min()) == value:
                    item.set_min(value)
                else:
                    return False
            elif role == Qt.UserRole + 1:
                if not str(item.get_max()) == value:
                    item.set_max(value)
                else:
                    return False
            elif role == Qt.UserRole + 2:
                if not item.get_alpha() == value:
                    item.set_alpha(value)
                else:
                    return False
            elif role == Qt.UserRole + 3:
                if not item.get_colormap() == value:
                    item.set_colormap(value)
                else:
                    return False

        self.dataChanged.emit(index, index)
        return True

    def moveUp(self, index):
        item = index.internalPointer()
        row = index.row()
        parent = index.parent()
        self.beginMoveRows(parent, row, row, parent, row-1)
        for hemi in self._data:
            if item in hemi.overlay_list:
                idx = hemi.overlay_list.index(item)
                hemi.overlay_up(idx)
        self.endMoveRows()

    def moveDown(self, index):
        item = index.internalPointer()
        row = index.row()
        parent = index.parent()
        self.beginMoveRows(parent, row+1, row+1, parent, row)
        for hemi in self._data:
            if item in hemi.overlay_list:
                idx = hemi.overlay_list.index(item)
                hemi.overlay_down(idx)
        self.endMoveRows()

    def setCurrentIndex(self, index):
{'fraction_non_alphanumeric': 0.09551090880552504, 'fraction_numerical': 0.0036885889185371214, 'mean_word_length': 1.4893533893338542, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5737711', 'n_tokens_mistral': 3478, 'n_tokens_neox': 3299, 'n_words': 931}