# -*- coding: latin-1 -*-
"""
Rutinas básicas para la edición en las listas de registros.
"""

from PySide import QtCore, QtGui

import Iconos

###############################################################################
class ComboBox(QtGui.QItemDelegate):
    """
    Delegado para la edición de un campo tipo combobox directamente en la tabla.
    """
    def __init__(self, liTextos):
        QtGui.QItemDelegate.__init__(self)
        self.liTextos = liTextos

    def createEditor(self, parent, option, index):
        editor = QtGui.QComboBox(parent)
        editor.addItems( self.liTextos )
        editor.installEventFilter(self)
        return editor

    def setEditorData(self, cb, index):
        value = index.model().data(index, QtCore.Qt.DisplayRole)
        num = self.liTextos.index( value )
        cb.setCurrentIndex(num)

    def setModelData(self, cb, model, index):
        num = cb.currentIndex()
        model.setData(index, self.liTextos[num])

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

###############################################################################
class LineaTexto(QtGui.QItemDelegate):
    """
    Delegado para la edición de un campo-texto directamente en la tabla.
    """
    def __init__(self, parent = None, siPassword = False ):
        QtGui.QItemDelegate.__init__(self, parent)
        self.siPassword = siPassword

    def createEditor(self, parent, option, index):
        editor = QtGui.QLineEdit(parent)
        if self.siPassword :
            editor.setEchoMode(QtGui.QLineEdit.Password)
        editor.installEventFilter(self)
        return editor

    def setEditorData(self, sle, index):
        value = index.model().data(index, QtCore.Qt.DisplayRole)
        sle.setText(value)

    def setModelData(self, sle, model, index):
        value = str(sle.text())
        model.setData(index, value)

    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

###############################################################################
class EtiquetaCentrada(QtGui.QStyledItemDelegate):
    """
    Delegado para la muestra con html
    """
    def __init__(self, parent = None):
        QtGui.QStyledItemDelegate.__init__(self, parent)

        self.dic = { "#V":Iconos.pmComentario(), "#R":Iconos.pmApertura(), "#M":Iconos.pmComentarioMas(), "#S":Iconos.pmAperturaComentario() }

    def rehazPosicion( self ) :
        posicion = self.bloquePieza.posicion
        self.setPos(posicion.x,posicion.y)

    def paint(self, painter, option, index):
        text = str(index.model().data(index, QtCore.Qt.DisplayRole))

        pm = None
        for k in self.dic :
            if k in text :
                pm = self.dic[k]
                text = text.replace( k, "" )

        document = QtGui.QTextDocument()
        document.setDefaultFont(option.font)
        document.setHtml(text)

        if option.state & QtGui.QStyle.State_Selected :
            painter.fillRect(option.rect, QtGui.QColor("#678DB2")) # sino no se ve en CDE-Motif-Windows
            # painter.fillRect(option.rect, palette.highlight().color())

        if pm :
            painter.save()
            painter.translate(option.rect.x(), option.rect.y())
            painter.drawPixmap( 0, 0, pm )
            painter.restore()

        painter.save()
        x = (option.rect.width()-document.idealWidth())/2
        painter.translate(option.rect.x()+x, option.rect.y())
        document.drawContents(painter)
        painter.restore()

