from wxPython.wx import *
from Numeric import *
from NaCurves import *

class GraphPanel(wxScrolledWindow):
        def __init__(self, parent, id=NewId(), pos=wxDefaultPosition, size=wxDefaultSize):
                wxScrolledWindow.__init__(self, parent, id, pos, size)
                self.normalFunc = NormalFunction()
                self.lagrange = Lagrange(DATASET)
                self.piecewiseLagrange = PiecewiseLagrange(DATASET, 4)
                self.cubicSpline = Spline(DATASET)
                self.errorLagrange = ErrorLagrange(DATASET)
                self.errorPiecewiseLagrange = ErrorPiecewiseLagrange(DATASET, 4)
                self.errorCubicSpline = ErrorSpline(DATASET)

                EVT_PAINT(self, self.OnPaint)

        def mappingToScreenX(self, x):
                cx,cy = self.GetClientSizeTuple()
                return (x) * (cx / 2) + cx / 2

        def mappingToScreenY(self, y):
                cx,cy = self.GetClientSizeTuple()
                return (-y) * (cy / 2) + cy / 2

        def OnPaint(self, event):
                dc = wxPaintDC(self)
                self.PrepareDC(dc)

                dc.BeginDrawing()
                self.drawBackground(dc)
                self.drawGraph(dc)
                #self.drawGuideLines(dc)
                dc.EndDrawing()

        def drawBackground(self, dc):
                cx, cy = self.GetClientSizeTuple()
                brush = wxBrush(wxColour(255,255,230), wxSOLID)
                oldbrush = dc.GetBrush()
                dc.SetBrush(brush)
                dc.DrawRectangle(0,0, cx, cy)
                dc.SetBrush(oldbrush)

        def drawGuideLines(self, dc):
                cx, cy = self.GetClientSizeTuple()
                marginX = 100
                marginY = 100
                dc.DrawLine(marginX,marginY, marginX, cy-marginY)
                dc.DrawLine(marginX,cy-marginY, cx-marginX, cy-marginY)

        def drawGraph(self, dc):
                self.plotNormalFunction(dc)
                self.plotLagrange(dc)
                self.plotPiecewiseLagrange(dc)
                self.plotCubicSpline(dc)
                self.plotErrorLagrange(dc)
                self.plotErrorPiecewiseLagrange(dc)
                self.plotErrorCubicSpline(dc)

        def plotNormalFunction(self, dc):
                self.plotGraph(dc, self.normalFunc, wxColour(255,0,0))

        def plotGraph(self, dc, aFuncObject, aColor):
                pen = wxPen(aColor, 1, wxSOLID)
                oldpen = dc.GetPen()
                dc.SetPen(pen)
                for x in arange(-1.0, 1.0, 0.001):
                        y = aFuncObject.perform(x)
                        realX = self.mappingToScreenX(x)
                        realY = self.mappingToScreenY(y)
                        dc.DrawPoint(realX, realY)
                dc.SetPen(oldpen)

        def plotLagrange(self, dc):
                self.plotGraph(dc, self.lagrange, wxColour(0,0,255))

        def plotPiecewiseLagrange(self, dc):
                self.plotGraph(dc, self.piecewiseLagrange, wxColour(255,255,0))

        def plotCubicSpline(self, dc):
                self.plotGraph(dc, self.cubicSpline, wxColour(0,0,0))

        def     plotErrorLagrange(self, dc):
                self.plotGraph(dc, self.errorLagrange, wxColour(128,128,128))

        def plotErrorPiecewiseLagrange(self, dc):
                self.plotGraph(dc, self.errorPiecewiseLagrange, wxColour(255,255,200))

        def plotErrorCubicSpline(self, dc):
                self.plotGraph(dc, self.errorCubicSpline, wxColour(255,0,255))