# -*- coding: latin-1 -*-

from PySide import QtCore, QtGui

from Code.Traducir import tr

import TabBloques

#==========================================================================================================================================
class MarcoSC(TabBloques.BloqueSC):

    def __init__( self, escena, bloqueMarco, rutinaPulsada=None ):

        super(MarcoSC, self).__init__(escena, bloqueMarco)

        self.rutinaPulsada = rutinaPulsada
        self.rutinaPulsadaCarga = None

        self.distBordes = 0.20*bloqueMarco.anchoCasilla

        self.posicion2xy()

        self.siMove = False
        self.tpSize = None

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def ponRutinaPulsada( self, rutina, carga ) :
        self.rutinaPulsada = rutina
        self.rutinaPulsadaCarga = carga

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def reset( self ) :
        self.posicion2xy()
        bm = self.bloqueDatos
        self.setOpacity( bm.opacidad )
        self.setZValue( bm.posicion.orden )
        self.update()

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def posicion2xy( self ) :

        bm = self.bloqueDatos
        posicion = bm.posicion
        ac = bm.anchoCasilla

        df, dc, hf, hc = self.tablero.a1h8_fc(bm.a1h8)

        if df > hf :
            df, hf = hf, df
        if dc > hc :
            dc, hc = hc, dc

        posicion.x = ac*(dc-1)
        posicion.y = ac*(df-1)
        posicion.ancho = (hc-dc+1)*ac
        posicion.alto = (hf-df+1)*ac

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def xy2posicion( self ) :

        bm = self.bloqueDatos
        posicion = bm.posicion
        ac = bm.anchoCasilla

        f = lambda xy : int(round(float(xy)/float(ac),0) )

        dc = f(posicion.x)+1
        df = f(posicion.y)+1
        hc = f(posicion.x+posicion.ancho)
        hf = f(posicion.y+posicion.alto)

        bien = lambda fc : (fc < 9) and (fc > 0)
        if bien(dc) and bien(df) and bien(hc) and bien(hf) :
            bm.a1h8 = self.tablero.fc_a1h8( df, dc, hf, hc )

        self.posicion2xy()

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def contiene(self, p) :
        def distancia( p1, p2 ) :
            t = p2-p1
            return ((t.x())**2 + (t.y())**2)**0.5

        db = self.distBordes
        self.tpSize = None
        for k,v in self.dicEsquinas.iteritems() :
            if distancia( p, v) <= db :
                self.tpSize = k
                return True
        self.siMove = self.rect.contains(p)
        return self.siMove

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def nombre(self) :
        return tr( "TUT_MARCO" )

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def mousePressEvent(self, event) :
        QtGui.QGraphicsItem.mousePressEvent(self,event)

        p = event.scenePos()
        self.expX = p.x()
        self.expY = p.y()

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def mouseMoveEvent (self, event) :
        event.ignore()
        if not (self.siMove or self.tpSize ) :
            return

        p = event.scenePos()
        x = p.x()
        y = p.y()

        dx = x - self.expX
        dy = y - self.expY

        self.expX = x
        self.expY = y

        posicion = self.bloqueDatos.posicion
        if self.siMove :
            posicion.x += dx
            posicion.y += dy
        else :
            tp = self.tpSize
            if tp == "br" :
                posicion.ancho += dx
                posicion.alto += dy
            elif tp == "bl" :
                posicion.x += dx
                posicion.ancho -= dx
                posicion.alto += dy
            elif tp == "tr" :
                posicion.y += dy
                posicion.ancho += dx
                posicion.alto -= dy
            elif tp == "tl" :
                posicion.x += dx
                posicion.y += dy
                posicion.ancho -= dx
                posicion.alto -= dy

        self.escena.update()

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def mouseReleaseEvent(self, event) :
        QtGui.QGraphicsItem.mouseReleaseEvent(self,event)
        if self.siActivo :
            if self.siMove or self.tpSize :
                self.xy2posicion()
                self.escena.update()
                self.siMove = False
                self.tpSize = None
            self.activa( False )

        if self.rutinaPulsada :
            if self.rutinaPulsadaCarga :
                self.rutinaPulsada( self.rutinaPulsadaCarga )
            else :
                self.rutinaPulsada( )

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def paint(self, painter, option, widget):

        bm = self.bloqueDatos

        posicion = bm.posicion
        dx = posicion.x
        dy = posicion.y
        ancho = posicion.ancho
        alto = posicion.alto

        self.rect = rect = QtCore.QRectF( dx, dy, ancho, alto )

        self.dicEsquinas = { "tl":rect.topLeft(), "tr":rect.topRight(), "bl":rect.bottomLeft(), "br":rect.bottomRight() }

        color = QtGui.QColor(bm.color)
        pen = QtGui.QPen()
        pen.setWidth( bm.grosor )
        pen.setColor(color)
        pen.setStyle( bm.tipoqt() )
        pen.setCapStyle(QtCore.Qt.RoundCap)
        pen.setJoinStyle(QtCore.Qt.RoundJoin)
        painter.setPen(pen)

        pen = QtGui.QPen()
        pen.setColor(QtGui.QColor(bm.color))
        pen.setWidth( bm.grosor )
        pen.setStyle( bm.tipoqt() )
        painter.setPen(pen)
        if bm.colorinterior >= 0 :
            color = QtGui.QColor(bm.colorinterior)
            if bm.colorinterior2 >= 0 :
                color2 = QtGui.QColor(bm.colorinterior2)
                gradient = QtGui.QLinearGradient(0, 0,bm.posicion.ancho,bm.posicion.alto)
                gradient.setColorAt(0.0, color)
                gradient.setColorAt(1.0, color2)
                painter.setBrush(QtGui.QBrush(gradient))
            else :
                painter.setBrush(color)

        if bm.redEsquina :
            painter.drawRoundedRect( self.rect, bm.redEsquina, bm.redEsquina )
        else :
            painter.drawRect( self.rect )

    #--------------------------------------------------------------------------------------------------------------------------------------------
    def boundingRect(self):
        x = self.bloqueDatos.grosor
        return QtCore.QRectF(self.rect).adjusted( -x, -x, x*2, x*2 )

#==========================================================================================================================================
