
# this module creates the necesarry HAL pins 
# for atc control
# for qtdragon_hd:
# put/rename the file in YOUR CONFIG/qtvcp/screens/qtdragon_hd/qtdragon_hd_handler.py

import sys
import importlib
from qtvcp.widgets.simple_widgets import PushButton as Button
from PyQt5.QtWidgets import QSizePolicy
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

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

class UserHandlerClass(HandlerClass):
    print('\nCustom subclassed handler loaded\n')

    def init_widgets(self):
        # call the original function:
        super().init_widgets()

        # hide the buttons we don't want:
        self.w.btn_laser_on.hide()
        self.w.btn_goto_sensor.hide()
        self.w.btn_ref_laser.hide()
        self.w.btn_ref_camera.hide()
        self.w.btn_touchplate.hide()

        # add and set up 3 HAL pushButtons:
        for i in('atc_release',"atc_cw","atc_ccw"):
            self.w['btn_{}'.format(i)] = Button()
            self.w['btn_{}'.format(i)].setObjectName(i)
            self.w['btn_{}'.format(i)].setProperty('isManSensitive',True)
            self.w['btn_{}'.format(i)].hal_init(HAL_NAME=i)
            self.w['btn_{}'.format(i)].setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
            self.w['btn_{}'.format(i)].setMinimumSize(66, 50)
            STATUS.connect('spindle-control-changed',  lambda w, num, state, speed, upto,obj= self.w['btn_{}'.format(i)]: obj.setEnabled(not state))
            self.w.horizontalLayout_40.addWidget(self.w['btn_{}'.format(i)] )
        self.w.btn_atc_release.setText('TOOL\nRELEASE')
        self.w.btn_atc_cw.setText('ATC\nCW')
        self.w.btn_atc_ccw.setText('ATC\nCCW')
