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

from PySide import QtCore, QtGui

from Code.Traducir import tr

import QTUtil2
import Colocacion
import Iconos

import Grid
import Columnas
import Delegados

# --------------------------------------------------------------------------------------------------------
class WEtiquetasPGN( QtGui.QDialog ):

    def __init__(self, procesador, liPGN )  :

        wParent = procesador.pantalla
        self.procesador = procesador
        self.liPGN = liPGN[:] # Trabajamos con la copia
        while len(self.liPGN) < 30 :
            self.liPGN.append( ["",""] )

        super(WEtiquetasPGN, self).__init__(wParent)

        self.setWindowTitle( tr("JS_EDITARPGN" ) )
        self.setWindowIcon( Iconos.PGN() )
        self.setWindowFlags( QtCore.Qt.Dialog | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowMinimizeButtonHint )

        #~ self.setMinimumWidth( 450 )

        # Toolbar
        tb = QTUtil2.creaTBAceptarCancelar( self )
        # Lista
        oColumnas = Columnas.ListaColumnas()
        oColumnas.nueva( "ETIQUETA", tr( "JS_ETIQUETAPGN" ), 150, edicion=Delegados.LineaTexto() )
        oColumnas.nueva( "VALOR", tr( "JS_VALORPGN" ), 400, edicion=Delegados.LineaTexto() )

        self.grid = Grid.Grid( self, oColumnas, siEditable=True )
        n = self.grid.anchoColumnas()
        self.grid.setFixedWidth( n + 20 )

        # Layout
        layout = Colocacion.V().control(tb).control(self.grid).margen( 3 )
        self.setLayout( layout )

    #--------------------------------------------------------------------------------------------------------------------------------
    def procesarTB( self ) :
        accion = self.sender().clave
        if accion == "aceptar" :
            self.accept()
        elif accion == "cancelar" :
            self.reject()

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridNumDatos( self, grid ) :
        return len(self.liPGN)

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridPonValor( self, grid, fila, oColumna, valor ) :
        col = 0 if oColumna.clave == "ETIQUETA" else 1
        self.liPGN[fila][col] = valor

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDato( self, grid, fila, oColumna ) :
        col = 0 if oColumna.clave == "ETIQUETA" else 1
        return self.liPGN[fila][col]

# --------------------------------------------------------------------------------------------------------
def editarEtiquetasPGN( procesador, liPGN ) :
    w = WEtiquetasPGN( procesador, liPGN )
    if w.exec_() :
        li = []
        for eti, valor in w.liPGN :
            if (len(eti.strip()) > 0)  and (len(valor.strip()) > 0) :
                li.append( [eti,valor] )
        return li
    else :
        return None
# --------------------------------------------------------------------------------------------------------

