
import sys
import importlib
from qtvcp.core import Path, Qhal, Action, Status
PATH = Path()
QHAL = Qhal()
ACTION = Action()
STATUS = Status()

# get reference to original handler file so we can subclass it
sys.path.insert(0, PATH.SCREENDIR)
module = "{}.{}_handler".format(PATH.BASEPATH,PATH.BASEPATH)
mod = importlib.import_module(module, PATH.SCREENDIR)
sys.path.remove(PATH.SCREENDIR)
HandlerClass = mod.HandlerClass

CRITICAL = 2

# return our subclassed handler object
def get_handlers(halcomp, widgets, paths):
    return [UserHandlerClass(halcomp, widgets, paths)]

class UserHandlerClass(HandlerClass):
    # add message to the terminal
    print('Custom subclassed handler for jog increment pins loaded\n')

    def __init__(self, halcomp, widgets, paths):
        super().__init__(halcomp, widgets, paths)

    def init_pins(self):
        # initialize the standard pins
        super().init_pins()

        # add a heads up that this is customized
        self.add_status('Custom subclassed handler "add jog increment pins" loaded\n',CRITICAL)

        # generate the new pins
        self.jog_increments_pin = QHAL.newpin("linear-jog-increment", QHAL.HAL_FLOAT, QHAL.HAL_OUT)
        self.jog_angular_increments_pin = QHAL.newpin("angular-jog-increment", QHAL.HAL_FLOAT, QHAL.HAL_OUT)

        # register status messages for increment changes
        STATUS.connect('jogincrement-changed', lambda w,v,t: self.jogLinearIncrementsChanged(v,t))
        STATUS.connect('jogincrement-angular-changed', lambda w,v,t: self.jogAngularIncrementsChanged(v,t))

    # set HAL pins when values changes
    def jogLinearIncrementsChanged(self, value,text):
        self.jog_increments_pin.set(value)
    def jogAngularIncrementsChanged(self, value,text):
        self.jog_angular_increments_pin.set(value)
