''' qtplasmac_custom.py - custom python code for qtplasmac '''

''' add external jog rate pot 0%~100% '''
def custom_code(self):
    ''' keep all custom code inside this function '''

    # create new function to replace existing
    def new_on_keycall_NUMBER(event, state, shift, cntrl, number):
        ''' the difference to the original on_keycall_NUMBER is
            don't allow changing the jog velocity slider '''
        if self.key_is_valid(event, state) and self.w.main_tab_widget.currentIndex() == self.MAIN:
            if shift and cntrl:
                pass
            elif shift and not cntrl:
                if number:
                    self.w.rapid_slider.setValue(10 * number)
                else:
                    self.w.rapid_slider.setValue(100)
            elif cntrl and not shift:
                if number:
                    self.w.feed_slider.setValue(10 * number)
                else:
                    self.w.feed_slider.setValue(100)

    # create new function to replace existing
    def new_on_keycall_QUOTELEFT(event, state, shift, cntrl):
        ''' the difference to the original on_keycall_QUOTELEFT is
            don't allow changing the jog velocity slider '''
        if self.key_is_valid(event, state) and self.w.main_tab_widget.currentIndex() == self.MAIN:
            if shift and cntrl:
                pass
            elif cntrl and not shift:
                pass

    # create new function to replace existing
    def new_jogs_label_pressed():
        ''' the difference to the original jogs_label_pressed is
            don't allow resetting the jog velocity slider '''
        pass

    # create new function to add
    def new_jog_ovr_changed(value):
        if value < 0:
            value = 0
        elif value > 100:
            value = 100
        self.w.jog_slider.setValue(self.w.jog_slider.maximum() * value/100)

    # replace old functions with new functions
    self.jogs_label_pressed = new_jogs_label_pressed
    self.on_keycall_NUMBER = new_on_keycall_NUMBER
    self.on_keycall_QUOTELEFT = new_on_keycall_QUOTELEFT

    # add new function
    self.jog_ovr_changed = new_jog_ovr_changed

    # create new hal pin
    self.extJogOvrPin = self.h.newpin('ext_jog_override', hal.HAL_FLOAT, hal.HAL_IN)
    self.extJogOvrPin.value_changed.connect(lambda v:self.jog_ovr_changed(v))
custom_code(self)

# set jog rate to the pot percentage
self.jog_ovr_changed(self.extJogOvrPin.get())
