#from __future__ import unicode_literals
# -*- coding: utf-8 -*-
"""Scene affichee dans la QGraphicsView lorsqu'aucun [combat, plateau] n'est cree"""
from __future__ import division
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class EcranFondPlateau(QGraphicsScene):
    """ecran de fond lorsqu'aucun plateau n'est cree"""
    def __init__(self, fenetre, parent=None):
        super(EcranFondPlateau, self).__init__()
        self.fenetre = fenetre
        self.setSceneRect(0, 0, 0.8*self.fenetre.ui.cbt_vue.width(), 0.8*self.fenetre.ui.cbt_vue.height())
        
        self.connect(self.fenetre.ui.cbt_vue, SIGNAL("resizeEvent()"), self.actu)
        self.fenetre.connect(self.fenetre.ui.cbt_vue, SIGNAL("resizeEvent()"), self.actu)
        self.cmdEnCours = CommandeFondPlateau(self, "enCours", ":/interface/512/ressource/armes_512.png", "Afficher le dernier combat", 40, 60, 30, 60)
        self.cmdEnCours.creer()
        self.cmdCharger = CommandeFondPlateau(self, "charger", ":/interface/512/ressource/dossier_512.png", "Charger un combat", 65, 15, 30, 60)
        self.cmdCharger.creer()
        self.cmdCreer = CommandeFondPlateau(self, "creer", ":/interface/512/ressource/plus_512.png", "Créer un nouveau combat", 15, 15, 30, 60)
        self.cmdCreer.creer()

        self.setSceneRect(QRectF())
        self.fenetre.ui.cbt_vue.fitInView(self.sceneRect(), Qt.KeepAspectRatioByExpanding)

        self.txt = QGraphicsTextItem()
        self.txt.setPos(QPointF(0,0))
        self.txt.setFont(QFont("Stencil", 18))
        self.txt.setDefaultTextColor(QColor(170,170,170))
        self.txt.setVisible(False)
        self.addItem(self.txt)

    def actu(self):
        self.setSceneRect(0, 0, 0.8*self.fenetre.ui.cbt_vue.width(), 0.8*self.fenetre.ui.cbt_vue.height())
        self.cmdEnCours.actu()
        self.cmdCharger.actu()
        self.cmdCreer.actu()  

    def clic(self, cmdId):
        """on a clique sur une des commandes"""
        if cmdId == "creer":
            self.fenetre.afficherEcranCreerPlateau()
        elif cmdId == "charger":
            self.fenetre.afficherEcranChargerPlateau()
        elif cmdId == "enCours":
            self.fenetre.chargerDernierPlateau()
        else:
            print "Commande invalide"

    def majTexte(self, txt):
        """affiche la legende en haut de l'ecran lors du surviol d'une commande"""
        if len(txt) > 0:
            self.txt.setTextWidth(self.width())
            self.txt.setPlainText(QString.fromUtf8(txt))
            self.centrerTxt()
        self.txt.setVisible((len(txt)>0))

    def centrerTxt(self):
        """centre le texte affiche"""
        f = QTextBlockFormat()
        f.setAlignment(Qt.AlignHCenter)
        cursor = self.txt.textCursor()
        cursor.select(QTextCursor.Document)
        cursor.mergeBlockFormat(f)
        cursor.clearSelection()
        self.txt.setTextCursor(cursor)
        
class CommandeFondPlateau(QGraphicsPixmapItem):
    """bouton image affiche sur l'EcranFondPlateau"""
    def __init__(self, ecran, cmdId, img = "", txt = "", x = 0, y = 0, h = 100, transparence = 0, parent=None):
        """Attention: x, y, h sont en % des dimensions de l'ecran!! et transparence est en %"""
        super(CommandeFondPlateau, self).__init__()
        self.ecran = ecran
        self.cmdId = cmdId
        self.img = img
        self.txt = txt
        self.x = x
        self.y = y
        self.h = h
        self.transparence = transparence

    def actu(self):
        """ajoute une image au fond de l'ecran. """
        self.setShapeMode(QGraphicsPixmapItem.BoundingRectShape)
        lEcran = self.ecran.width()
        hEcran = self.ecran.height()

        pix = QPixmap()
        pix.load(self.img)
        pix = pix.scaledToHeight(((self.h*hEcran)/100), Qt.SmoothTransformation)
        
        self.setPixmap(pix)
        self.setPos(((self.x*lEcran)/100), \
                    ((self.y*hEcran)/100)  )
        self.setOpacity(self.transparence/100)

    def creer(self):
        self.setAcceptHoverEvents(True)
        self.setFlag(QGraphicsItem.ItemIsFocusable)
        self.actu()
        self.ecran.addItem(self)

    def mousePressEvent(self, event):
        """cree un plateau sur clic gauche"""
        self.ecran.clic(self.cmdId)

    def hoverEnterEvent(self, event):
        effet = QGraphicsColorizeEffect()
        effet.setColor(QColor(170, 170, 170))
        self.setGraphicsEffect(effet)
        self.ecran.majTexte(self.txt)

    def hoverLeaveEvent(self, event):
        self.setGraphicsEffect(None)
        self.ecran.majTexte("")






        
        
