#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Thu Jun 19 11:59:19 2008

import wx, wx.wizard, replath.preferences

# begin wxGlade: extracode
# end wxGlade

class PreferencesPanel(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: PreferencesPanel.__init__
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, -1, "Pen Positions :")
        self.label_2 = wx.StaticText(self, -1, "Up :")
        self.text_penUpPos = wx.TextCtrl(self, -1, "")
        self.label_3 = wx.StaticText(self, -1, "Down :")
        self.text_penDownPos = wx.TextCtrl(self, -1, "")
        self.button_wizard = wx.Button(self, -1, "&Setup Wizard")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.onClickSetupWizard, self.button_wizard)
        # end wxGlade
        
        self.prefHandler = replath.preferences.PreferenceHandler(self,  "toolhead_pen.conf")
        
        # default values for preferences
        self.pref_penUpPos = 0
        self.pref_penDownPos = 0
        
        self.prefHandler.load()
        self.setPrefValues()


    def __set_properties(self):
        # begin wxGlade: PreferencesPanel.__set_properties
        pass
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: PreferencesPanel.__do_layout
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.FlexGridSizer(3, 7, 5, 10)
        grid_sizer_1.Add(self.label_1, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((10, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.label_2, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.text_penUpPos, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add((10, 20), 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.label_3, 0, wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.Add(self.text_penDownPos, 0, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
        grid_sizer_1.AddGrowableCol(3)
        grid_sizer_1.AddGrowableCol(6)
        sizer_2.Add(grid_sizer_1, 0, wx.ALL|wx.EXPAND, 10)
        sizer_2.Add(self.button_wizard, 0, wx.ALL|wx.ALIGN_RIGHT|wx.ADJUST_MINSIZE, 10)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        # end wxGlade
    
    # Set values of frame control
    def setPrefValues(self):
        self.text_penUpPos.SetValue( str(self.pref_penUpPos) )
        self.text_penDownPos.SetValue( str(self.pref_penDownPos) )

    def savePrefValues(self):
        self.pref_penUpPos = int( self.text_penUpPos.GetValue() )
        self.pref_penDownPos = int( self.text_penDownPos.GetValue() )
        self.prefHandler.save()

    def onClickSetupWizard(self, event): # wxGlade: PreferencesPanel.<event_handler>
        #TODO
        #toolModule = reprap.plugins.getPlugin( reprap.plugins.PLUGIN_TOOLHEAD, self.choice_output.GetSelection() )
        #toolModule.runGUIConfig(parent = self)
        wizard = PenSetupWizard( parent = self, title = "Pen Calibration", )
        wizard.addPages( [ ("pane1", Wizard_Page1), ("pane2", Wizard_Page2), ("pane3", Wizard_Page3), ("pane4", Wizard_Page4), ], )
        # TODO - pass the wizard previous values
        if wizard.run():
            reprap.closeSerial()
            self.pref_penUpPos, self.pref_penDownPos = wizard.getPenPositions()
            print self.pref_penUpPos, self.pref_penDownPos
            self.prefHandler.save()
            self.setPrefValues()
        wizard.Destroy()

# end of class PreferencesPanel

#Derived from
#http://wiki.wxpython.org/WizardFromWxGlade
class PenSetupWizard(wx.wizard.Wizard):
    """
    Wizard that can be built up from panel classes
    generated by wxGlade
    """
    def __init__(self, parent, id = -1,
                title = wx.EmptyString,
                bitmap = wx.NullBitmap,
                *args, **kw):

        wx.wizard.Wizard.__init__(self, parent, id, title, bitmap, *args, **kw)
        self.prevPanel = None
        self.panels = []
    def addPages(self, pages):
        for name, pageClass in pages:
            self.addPage(name, pageClass)
        return

    def addPage(self, attrname, panelClass):
        # create wizard page object, and add on
        # instance of our panel class to it
        page = wx.wizard.WizardPageSimple(self)
        page.sizer = wx.BoxSizer(wx.VERTICAL)
        #page.sizer = wx.BoxSizer(wx.HORIZONTAL)
        pageInst = panelClass(page, -1)
        #page.sizer.Add(pageInst, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        page.sizer.Add(pageInst, 1, wx.EXPAND, 0)
        page.SetSizer(page.sizer)
        #page.sizer.Fit(self)
        #self.Layout()
        #self.Centre()
        # save the panel instance as named attribute
        setattr(self, attrname, pageInst)
        print "newp", page
        print "prevp", self.prevPanel
        #page.nextPanel = False
        # chain to previous, if this is not the first page
        if self.prevPanel:
            #t.prevPanel = self.prevPanel
            self.prevPanelInst.setNextPanel(pageInst)
            wx.wizard.WizardPageSimple_Chain(self.prevPanel, page)
        else:
            self.firstPanel = page
        self.panels.append(pageInst)
        # remember this as 'previous' page, for adding/chaining the next
        self.prevPanel = page
        self.prevPanelInst = pageInst

    def run(self):
        return self.RunWizard(self.firstPanel)
    def getPenPositions(self):
        return self.panels[3].upPos, self.panels[3].downPos

class DialogWizardUnused(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: DialogWizardUnused.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.notebook_Wizard = wx.Notebook(self, -1, style=0)
        self.pane_Reset = Wizard_Page1(self.notebook_Wizard, -1)
        self.pane_PenUp = Wizard_Page2(self.notebook_Wizard, -1)
        self.pane_PenDown = Wizard_Page3(self.notebook_Wizard, -1)
        self.pane_Test = Wizard_Page4(self.notebook_Wizard, -1)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: DialogWizardUnused.__set_properties
        self.SetTitle("dialog_2")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: DialogWizardUnused.__do_layout
        sizer_13 = wx.BoxSizer(wx.HORIZONTAL)
        self.notebook_Wizard.AddPage(self.pane_Reset, "Reset")
        self.notebook_Wizard.AddPage(self.pane_PenUp, "Pen Up Position")
        self.notebook_Wizard.AddPage(self.pane_PenDown, "Pen Down Position")
        self.notebook_Wizard.AddPage(self.pane_Test, "Test")
        sizer_13.Add(self.notebook_Wizard, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_13)
        sizer_13.Fit(self)
        self.Layout()
        # end wxGlade

# end of class DialogWizardUnused

class Wizard_Page1(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Wizard_Page1.__init__
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.sizer_1_copy_staticbox = wx.StaticBox(self, -1, "Reset")
        self.label_15 = wx.StaticText(self, -1, "This wizard is designed to assist with pen alignment.\n\nMake sure the serial SNAP RepRap is switched on, then press the 'Reset' button to reset the Z axis to 0.\n")
        self.button_Reset = wx.Button(self, -1, "Reset")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.hanBtnReset, self.button_Reset)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Wizard_Page1.__set_properties
        pass
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Wizard_Page1.__do_layout
        sizer_1_copy = wx.StaticBoxSizer(self.sizer_1_copy_staticbox, wx.VERTICAL)
        sizer_2_copy = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1_copy.Add(self.label_15, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 12)
        sizer_2_copy.Add(self.button_Reset, 1, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0)
        sizer_1_copy.Add(sizer_2_copy, 1, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 25)
        self.SetSizer(sizer_1_copy)
        sizer_1_copy.Fit(self)
        # end wxGlade

    def setNextPanel(self, panel):
        self.nextPanel = panel

    def hanBtnReset(self, event): # wxGlade: Wizard_Page1.<event_handler>
        if reprap.openSerial( 0, 19200, 60 ):	# TODO, get prefs properly from reprap_output
        #if reprap.openSerial( self.pref_serialPort, self.pref_baudRate, self.pref_timeout ):	# Initialise serial port.
            reprap.cartesian.z.active = True
            reprap.cartesian.z.setNotify()
            #reprap.cartesian.setMoveSpeed(self.pref_speed)
            reprap.cartesian.setMoveSpeed(220)
            #reprap.cartesian.setPower(self.pref_torque)
            reprap.cartesian.setPower(82)
            reprap.cartesian.z.homeReset()
            self.pos = reprap.cartesian.z.getPos()
            self.nextPanel.pos = self.pos
            self.nextPanel.text_penUpPos.SetValue( str(self.nextPanel.pos) )
        else:
            print "Comms error in pen config, make this gui"

# end of class Wizard_Page1

class Wizard_Page2(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Wizard_Page2.__init__
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.sizer_27_staticbox = wx.StaticBox(self, -1, "Pen Position")
        self.sizer_3_staticbox = wx.StaticBox(self, -1, "Pen Up Position")
        self.sizer_24_copy_staticbox = wx.StaticBox(self, -1, "Move Pen")
        self.label_20 = wx.StaticText(self, -1, "Use the 'Up' and 'Down' buttons, or enter a value manually to position the pen where it is just a few millimeters above the paper.")
        self.button_stepUp_up = wx.Button(self, wx.ID_UP, "")
        self.button_stepDown_up = wx.Button(self, wx.ID_DOWN, "")
        self.text_penUpPos = wx.TextCtrl(self, -1, "0")
        self.button_apply_up = wx.Button(self, wx.ID_APPLY, "")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnBtnStepUp, self.button_stepUp_up)
        self.Bind(wx.EVT_BUTTON, self.OnBtnStepDown, self.button_stepDown_up)
        self.Bind(wx.EVT_BUTTON, self.hanBtnApplyUp, self.button_apply_up)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Wizard_Page2.__set_properties
        pass
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Wizard_Page2.__do_layout
        sizer_3 = wx.StaticBoxSizer(self.sizer_3_staticbox, wx.VERTICAL)
        sizer_27 = wx.StaticBoxSizer(self.sizer_27_staticbox, wx.VERTICAL)
        sizer_14_copy = wx.BoxSizer(wx.HORIZONTAL)
        sizer_24_copy = wx.StaticBoxSizer(self.sizer_24_copy_staticbox, wx.VERTICAL)
        sizer_3.Add(self.label_20, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 12)
        sizer_24_copy.Add(self.button_stepUp_up, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_24_copy.Add(self.button_stepDown_up, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_3.Add(sizer_24_copy, 0, wx.ALL|wx.EXPAND, 6)
        sizer_14_copy.Add(self.text_penUpPos, 1, wx.ADJUST_MINSIZE, 0)
        sizer_14_copy.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        sizer_14_copy.Add(self.button_apply_up, 0, wx.ADJUST_MINSIZE, 0)
        sizer_27.Add(sizer_14_copy, 1, wx.EXPAND, 0)
        sizer_3.Add(sizer_27, 0, wx.ALL|wx.EXPAND, 6)
        self.SetSizer(sizer_3)
        sizer_3.Fit(self)
        # end wxGlade
        
    def setNextPanel(self, panel):
        self.nextPanel = panel
    
    def OnBtnStepUp(self, event): # wxGlade: Wizard_Page2.<event_handler>
        self.pos = reprap.cartesian.z.getPos()
        self.pos -= 1
        reprap.cartesian.z.seek(self.pos)
        self.text_penUpPos.SetValue( str(self.pos) )
        self.nextPanel.pos = self.pos
        self.nextPanel.text_penDownPos.SetValue( str(self.nextPanel.pos) )
        self.nextPanel.nextPanel.downPos = self.pos
    
    def OnBtnStepDown(self, event): # wxGlade: Wizard_Page2.<event_handler>
        self.pos = reprap.cartesian.z.getPos()
        self.pos += 1
        reprap.cartesian.z.seek(self.pos)
        self.text_penUpPos.SetValue( str(self.pos) )
        self.nextPanel.pos = self.pos
        self.nextPanel.text_penDownPos.SetValue( str(self.nextPanel.pos) )
        self.nextPanel.nextPanel.downPos = self.pos
    
    def hanBtnApplyUp(self, event): # wxGlade: Wizard_Page2.<event_handler>
        self.pos = int( self.text_penUpPos.GetValue() )
        reprap.cartesian.z.seek(self.pos)
        self.nextPanel.pos = self.pos
        self.nextPanel.text_penDownPos.SetValue( str(self.nextPanel.pos) )
        self.nextPanel.nextPanel.downPos = self.pos

# end of class Wizard_Page2

class Wizard_Page3(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Wizard_Page3.__init__
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.sizer_25_staticbox = wx.StaticBox(self, -1, "Pen Position")
        self.sizer_16_staticbox = wx.StaticBox(self, -1, "Pen Down Position")
        self.sizer_24_staticbox = wx.StaticBox(self, -1, "Move Pen")
        self.label_19 = wx.StaticText(self, -1, "Use the 'Up' and 'Down' buttons, or enter a value manually to position the pen where it is just making contact with the paper.")
        self.button_stepUp_dp = wx.Button(self, wx.ID_UP, "")
        self.button_stepDown_dp = wx.Button(self, wx.ID_DOWN, "")
        self.text_penDownPos = wx.TextCtrl(self, -1, "0")
        self.button_apply_dp = wx.Button(self, wx.ID_APPLY, "")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnBtnStepUp, self.button_stepUp_dp)
        self.Bind(wx.EVT_BUTTON, self.OnBtnStepDown, self.button_stepDown_dp)
        self.Bind(wx.EVT_BUTTON, self.hanBtnApplyDown, self.button_apply_dp)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Wizard_Page3.__set_properties
        pass
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Wizard_Page3.__do_layout
        sizer_16 = wx.StaticBoxSizer(self.sizer_16_staticbox, wx.VERTICAL)
        sizer_25 = wx.StaticBoxSizer(self.sizer_25_staticbox, wx.VERTICAL)
        sizer_14 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_24 = wx.StaticBoxSizer(self.sizer_24_staticbox, wx.VERTICAL)
        sizer_16.Add(self.label_19, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 12)
        sizer_24.Add(self.button_stepUp_dp, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_24.Add(self.button_stepDown_dp, 0, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ADJUST_MINSIZE, 0)
        sizer_16.Add(sizer_24, 0, wx.ALL|wx.EXPAND, 6)
        sizer_14.Add(self.text_penDownPos, 1, wx.ADJUST_MINSIZE, 0)
        sizer_14.Add((20, 20), 0, wx.ADJUST_MINSIZE, 0)
        sizer_14.Add(self.button_apply_dp, 0, wx.ADJUST_MINSIZE, 0)
        sizer_25.Add(sizer_14, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 0)
        sizer_16.Add(sizer_25, 0, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 6)
        self.SetSizer(sizer_16)
        sizer_16.Fit(self)
        # end wxGlade

    def setNextPanel(self, panel):
        self.nextPanel = panel

    def OnBtnStepUp(self, event): # wxGlade: Wizard_Page3.<event_handler>
        self.pos = reprap.cartesian.z.getPos()
        self.pos -= 1
        reprap.cartesian.z.seek(self.pos)
        self.text_penDownPos.SetValue( str(self.pos) )
        self.nextPanel.upPos = self.pos

    def OnBtnStepDown(self, event): # wxGlade: Wizard_Page3.<event_handler>
        self.pos = reprap.cartesian.z.getPos()
        self.pos += 1
        reprap.cartesian.z.seek(self.pos)
        self.text_penDownPos.SetValue( str(self.pos) )
        self.nextPanel.upPos = self.pos

    def hanBtnApplyDown(self, event): # wxGlade: Wizard_Page3.<event_handler>
        self.pos = int( self.text_penDownPos.GetValue() )
        reprap.cartesian.z.seek(self.pos)
        self.nextPanel.upPos = self.pos

# end of class Wizard_Page3

class Wizard_Page4(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Wizard_Page4.__init__
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.sizer_17_staticbox = wx.StaticBox(self, -1, "Test")
        self.label_18 = wx.StaticText(self, -1, "Test the pen positons with the buttons below.")
        self.button_PenUp = wx.Button(self, -1, "Test Pen Up")
        self.button_PenDown = wx.Button(self, -1, "Test Pen Down")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.OnBtnPenUp, self.button_PenUp)
        self.Bind(wx.EVT_BUTTON, self.OnBtnPenDown, self.button_PenDown)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Wizard_Page4.__set_properties
        pass
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Wizard_Page4.__do_layout
        sizer_17 = wx.StaticBoxSizer(self.sizer_17_staticbox, wx.VERTICAL)
        sizer_21_copy = wx.BoxSizer(wx.VERTICAL)
        sizer_17.Add(self.label_18, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 12)
        sizer_21_copy.Add(self.button_PenUp, 0, wx.EXPAND, 0)
        sizer_21_copy.Add(self.button_PenDown, 0, wx.EXPAND, 0)
        sizer_17.Add(sizer_21_copy, 1, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 51)
        self.SetSizer(sizer_17)
        sizer_17.Fit(self)
        # end wxGlade

    def setNextPanel(self, panel):
        self.nextPanel = panel

    def OnBtnPenUp(self, event): # wxGlade: Wizard_Page4.<event_handler>
        reprap.cartesian.z.seek(self.upPos)

    def OnBtnPenDown(self, event): # wxGlade: Wizard_Page4.<event_handler>
        reprap.cartesian.z.seek(self.downPos)

# end of class Wizard_Page4


# Unused
class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.PreferencesPanel = PreferencesPanel(self, -1)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyDialog.__set_properties
        self.SetTitle("dialog_1")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyDialog.__do_layout
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(self.PreferencesPanel, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

# end of class MyDialog

