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

#==========================================================================================================================================
import copy

from PySide import QtGui

from Code.Traducir import tr

import Code.Configuracion as Configuracion

import QTUtil2
import Colocacion
import Iconos
import Controles
import Columnas
import Grid
import Tablero

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

#==========================================================================================================================================
class WET_Escena( QtGui.QWidget ):

    def __init__( self, owner, funcTamTablero ) :

        QtGui.QWidget.__init__(self,owner)

        self.tutorial = owner.tutorial
        self.configuracion = owner.procesador.configuracion
        self.tab = owner.tab
        self.funcTamTablero = funcTamTablero

        self.liTareas = []

        flb = Controles.TipoLetra( puntos=8 )

        # toolbar
        liAcciones = [  ( tr( "NUEVO" ), Iconos.TB_Nuevo(), "mas" ), None,
                        ( tr( "BORRAR" ), Iconos.TB_Borrar(), "borrar" ), None,
                        ( tr( "MODIFICAR" ), Iconos.TB_Modificar(), "modificar" ), None,
                        ( tr( "COPIAR" ), Iconos.TB_Copiar(), "copiar" ), None,
                        ( tr( "ARRIBA" ), Iconos.TB_Arriba(), "arriba" ), None,
                        ( tr( "ABAJO" ), Iconos.TB_Abajo(), "abajo" ), None,
                    ]
        tb = Controles.TB( self, liAcciones, siTexto = False, tamIcon=24 )
        tb.setFont( flb )

        # Tareas
        oColumnas = Columnas.ListaColumnas()
        oColumnas.nueva( "NUMERO", tr( "AB_NUM" ), 40, siCentrado=True )
        oColumnas.nueva( "TAREA", tr( "TUT_TAREA" ), 210 )
        self.tareas = Grid.Grid( self, oColumnas, siCabeceraMovible = False, id = "S", siSelecFilas=True )
        nAncho = self.tareas.anchoColumnas() + 20
        self.tareas.setMinimumWidth( nAncho )

        # Tablero
        self.confTablero = Configuracion.ConfigTablero()
        self.confTablero.lee( self.configuracion.cTableroN.graba() )
        self.confTablero.anchoPieza = funcTamTablero()
        self.tablero = Tablero.TutTablero(self, self.confTablero)
        self.tablero.crea()

        # Pgn
        oColumnas = Columnas.ListaColumnas()
        oColumnas.nueva( "NUMERO", tr( "AB_NUM" ), 35, siCentrado = True )
        oColumnas.nueva( "BLANCAS", tr( "PGN_WHITE" ), 100 )
        oColumnas.nueva( "NEGRAS", tr( "PGN_BLACK" ), 100 )
        self.pgn = Grid.Grid( self, oColumnas, siCabeceraMovible = False, id = "PGN" )
        nAnchoPgn = self.pgn.anchoColumnas() + 20
        self.pgn.setMinimumWidth( nAnchoPgn )

        # Layout
        lyT = Colocacion.V().control( self.tablero ).relleno(1)
        lyH = Colocacion.H().control( self.tareas ).otro(lyT).control( self.pgn )

        layout = Colocacion.V().control(tb).otro( lyH )
        self.setLayout( layout )

    #--------------------------------------------------------------------------------------------------------------------------------
    def ponEscena( self, escena ) :
        self.escena = escena
        self.liTareas = escena.liTareas
        self.tareas.refresh()

    #--------------------------------------------------------------------------------------------------------------------------------
    def compruebaTablero( self ) :
        nAnchoPieza = self.funcTamTablero()
        if nAnchoPieza != self.confTablero.anchoPieza :
            self.confTablero.anchoPieza = nAnchoPieza
            self.tablero.reset( self.confTablero )

    #--------------------------------------------------------------------------------------------------------------------------------
    def procesarTB( self ) :
        accion = self.sender().clave
        eval( "self.%s()"%accion )

    #--------------------------------------------------------------------------------------------------------------------------------
    def mas( self ) :
        # e = Tutoriales.Escena()
        # e.id = self.tutorial.idNuevo()
        # nEsc = len(self.tutorial.liEscenas)
        # e.nombre = tr( "TUT_ESCENA", str(nEsc+1) )
        # self.tutorial.liEscenas.append(e)
        # self.grid.goto(nEsc,0)
        # self.grid.refresh()
        pass

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridNumDatos( self, grid ) :
        if grid.id == "S" :
            return len(self.liTareas)
        else :
            return 0

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridCambiadoRegistro( self, grid, fila, columna ) :
        # self.tab.setTabText( self.tab.numTabEscenas, "(%d) %s"%(fila+1,self.liEscenas[fila].nombre) )
        pass

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDato( self, grid, fila, oColumna ) :
        clave = oColumna.clave
        if clave == "NUMERO" :
            return "%d"%(fila+1,)
        if grid == self.tareas :
            tarea = self.liTareas[fila]
            if clave == "TAREA" :
                return str(tarea)

    #--------------------------------------------------------------------------------------------------------------------------------
    def borrar( self ) :
        fila = self.tareas.recno()
        if fila >= 0 :
            if QTUtil2.pregunta( self, tr( "TUT_BORRAMOS_TAREA", str(self.liTareas[fila]) )) :
                del self.liTareas[fila]
                self.tareas.refresh()
                self.tareas.setFocus()

    #--------------------------------------------------------------------------------------------------------------------------------
    def copiar( self ) :
        fila = self.tareas.recno()
        if fila >= 0 :
            regTarea = copy.deepcopy( self.liTareas[fila] )
            self.liTareas.append(regTarea)
            self.tareas.refresh()
            self.tareas.setFocus()

    #--------------------------------------------------------------------------------------------------------------------------------
    def arriba( self ) :
        fila = self.tareas.recno()
        if fila > 0 :
            self.liTareas[fila-1],self.liTareas[fila] = self.liTareas[fila],self.liTareas[fila-1]
            self.tareas.goto(fila-1,0)
            self.tareas.refresh()
            self.tareas.setFocus()

    #--------------------------------------------------------------------------------------------------------------------------------
    def abajo( self ) :
        fila = self.tareas.recno()
        if fila >= 0 and fila < (len(self.liTareas)-1) :
            self.liTareas[fila+1],self.liTareas[fila] = self.liTareas[fila],self.liTareas[fila+1]
            self.tareas.goto(fila+1,0)
            self.tareas.refresh()
            self.tareas.setFocus()

