# -*- coding:utf-8

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

import random #Pour générer une courbe aléatoire

class Graph(QtGui.QWidget):
    def __init__(self, parent=None, width=500, height=500, points=QtGui.QPolygon()):
        QtGui.QWidget.__init__(self,parent=parent)
        self.__width=width
        self.__height=height
        self.__points=points

    def setData(self, width, height, points):
        """modifier les paramètres du widget"""
        self.__width=width
        self.__height=height
        self.__points=points

        QtGui.QPixmapCache.remove(self.key())
        self.updateGeometry()
        self.update()

    def width(self):
        return self.__width

    def height(self):
        return self.__height

    def points(self):
        return self.__points

    def sizeHint(self):
        """taille optimale du widget"""
        return QtCore.QSize(self.__width, self.__height)

    def paintEvent(self, event):
        """méthode de mise à jour"""
        if self.__width<=0 or self.__height<=0:
            return

        pixmap=QtGui.QPixmap()
        painter=QtGui.QPainter(self)

        if not QtGui.QPixmapCache.find(self.key(), pixmap):
            pixmap=self.generatePixmap()
            QtGui.QPixmapCache.insert(self.key(), pixmap)

        painter.drawPixmap(0,0 , pixmap)

    def key(self):
        """génére la clef qui identifie la pixmap du widget"""
        return repr(self)

    def generatePixmap(self):
        pixmap=QtGui.QPixmap(self.__width, self.__height)
        pixmap.fill(self, 0, 0)

        painter=QtGui.QPainter(pixmap)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.drawPolyline(self.__points)
        return pixmap

class Window(QtGui.QWidget):
    """ classe de la fenêtre principale qui implémente une méthode pour
        générer une nouvelle courbe rapidement
        """
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.graph=Graph(self)
        self.graph.setData(self.graph.width(), self.graph.height(), self.grabSomePoints())

        new=QtGui.QPushButton("Nouveau Graph", self)
        new.connect(new, QtCore.SIGNAL("clicked()"),
                   self.makeNewGraph)

        layout=QtGui.QVBoxLayout()
        layout.addWidget(self.graph)
        layout.addWidget(new)

        self.setLayout(layout)

    def grabSomePoints(self):
        """renvoie unne liste de points"""
        poly=QtGui.QPolygon()
        for x in range(0,self.graph.width()+1,10):
            poly.putPoints(poly.size(), x, random.randint(0,self.graph.height()))
        return poly

    def makeNewGraph(self):
        """crée un nouveau graph"""
        self.graph.setData(self.graph.width(), self.graph.height(), self.grabSomePoints())

def main():
    global graph
    app=QtGui.QApplication(sys.argv)
    w=Window()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
