#from __future__ import unicode_literals
# -*- coding: utf-8 -*-
from __future__ import division

from PyQt4.QtCore import *
from PyQt4.QtGui import *



class EntreeSortie(QGraphicsPolygonItem):
    """marqueur d'entree sortie du plateau"""
    def __init__(self, plateau, entreeSortie = "", parent=None):
        """initialisation de la fenetre"""
        super (EntreeSortie, self).__init__()
        self.plateau = plateau
        self.entreeSortie = entreeSortie
        self.position = QPointF(0,0)
        self.nbRotations = 0
        self.prepareGeometryChange()
        self.setFlag(QGraphicsItem.ItemIsFocusable)
        

    def __getstate__(self):
        state = {key:value for key, value in self.__dict__.items() if key in ["entreeSortie", "position", "nbRotations"]}
        return (state)

    def __setstate__(self, state):
        self.__dict__ = state
    
    def creerForme(self):
        """cree le polygone graphique"""
        polygone = QPolygonF()
        polygone << QPointF(60,0) \
                 << QPointF(-60,60) \
                 << QPointF(-60,-60)
        if self.entreeSortie == "E":
            couleur = QColor("green")
            self.txtEtiquette = "Entree"
        elif self.entreeSortie == "S":
            couleur = QColor("red")
            self.txtEtiquette = "Sortie"
        if couleur.isValid():
            self.setBrush(couleur)
            self.setPen(QPen(couleur))
        self.setPolygon(polygone)
        self.setOpacity(0.5)
        self.setTransformOriginPoint(0,0)
        self.plateau.fenetre.ui.vuePlateau.fitInView(self.plateau.sceneRect(), Qt.KeepAspectRatio)
        self.plateau.addItem(self)
        
    def majProjection(self, position = None):
        """met a jour la projection de la position de l'objet"""
        if position != None:
            self.setPos(position)
            self.position = position
        self.setRotation(90*self.nbRotations)

    def positionner(self):
        """la projection est validee, le marqueur est laisse a cette position"""
        self.setOpacity(1)

    def recreer(self, plateau):
        """recreation et repositionnement dans le cadre d'un chargement du plateau"""
        self.plateau = plateau
        super(EntreeSortie, self).__init__()
        self.creerForme()
        self.setPos(self.position)
        self.setRotation(self.nbRotations*90)
        self.setOpacity(1)
    
    def mousePressEvent(self, event):
        event.ignore()
        if event.button() == 1:
            if self.plateau.modeActif != "placementEntreeSortie":
                self.setOpacity(0.5)
                self.plateau.modeParam["entreeSortie"] = self
                self.plateau.majMode("placementEntreeSortie")
                event.accept()
