from test_graphiques import Ui_Form
import sys, os, time
from PyQt4 import QtCore,QtGui, QtOpenGL

class Plateau(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.scene = QtGui.QGraphicsScene()
        self.ui.view.setScene(self.scene)
        self.scene.setSceneRect(0,0,self.ui.view.width(),self.ui.view.height())
        self.ui.view.setViewport(QtOpenGL.QGLWidget())
        self.cases = {}
        
        self.majQuadrillage(10,10)
        self.nouveauPion()


    def majQuadrillage(self, x, y):
        #autre essai avec des rectancgles
        if x > 0 and y > 0 and x == int(x) and y == int(y):
            i = 1
            lPlateau = self.ui.view.width()
            hPlateau = self.ui.view.height()
            self.cCase = int(lPlateau / x)  #cote de la case
            
            pinceau = QtGui.QPen()
            pinceau.setColor(QtGui.QColor(85, 85, 85, 85))
            pinceau.setWidth(1)
            
            #cree les cases
            for i in range(x):
                for j in range(y):
                    self.cases[(i,j)] = QtGui.QGraphicsRectItem(i*self.cCase, j*self.cCase, self.cCase, self.cCase)
                    self.cases[(i,j)].setPen(pinceau)
                    self.scene.addItem(self.cases[(i,j)])


    def nouveauPion(self):
        #creation d'un pion
        pinceau = QtGui.QPen()
        pinceau.setColor(QtGui.QColor(0, 0, 0, 120))
        pinceau.setWidth(2)
        self.pion = QtGui.QGraphicsEllipseItem(0, 0, self.cCase, self.cCase)
        self.pion.setPen(pinceau)
        self.pion.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
        self.pion.setFlag(QtGui.QGraphicsItem.ItemIsFocusable)
        self.pion.setBrush(QtGui.QColor(255, 0, 0, 150))

        #options graphiques
        painter = QtGui.QPainter()
        painter.setRenderHints(QtGui.QPainter.Antialiasing, True)
        self.pion.paint(painter, 1)


        #ombre
        self.shadow = QtGui.QGraphicsDropShadowEffect() 
        self.shadow.setColor(QtGui.QColor(50, 50, 50, 200)) 
        self.shadow.setXOffset(1) 
        self.shadow.setYOffset(1) 
        self.shadow.setBlurRadius(3) 
        self.pion.setGraphicsEffect(self.shadow)
        self.shadow.setEnabled(True)

        #recentrage
        #self.pion.mousePressEvent(self.test())
        self.pion.setAcceptHoverEvents(True)
        #self.pion.mouseReleaseEvent(self.test())
        self.scene.addItem(self.pion)
    


def main():
    app = QtGui.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())  

if __name__ == "__main__":
    main()
