|
| 1 | +from PyQt5.QtWidgets import (QToolButton, QMenu, QAction, |
| 2 | + QComboBox, QWidgetAction, QSizePolicy) |
| 3 | +from PyQt5.QtCore import pyqtProperty, pyqtSignal |
| 4 | +from PyQt5.QtGui import QIcon |
| 5 | + |
| 6 | +from qtvcp.widgets.widget_baseclass import _HalWidgetBase |
| 7 | +from qtvcp.widgets.indicatorMixIn import IndicatedMixIn |
| 8 | +from qtvcp.core import Status, Action, Info |
| 9 | +from qtvcp import logger |
| 10 | + |
| 11 | +# Instiniate the libraries with global reference |
| 12 | +# STATUS gives us status messages from linuxcnc |
| 13 | +# AUX_PRGM holds helper program loader |
| 14 | +# INI holds ini details |
| 15 | +# ACTION gives commands to linuxcnc |
| 16 | +# LOG is for running code logging |
| 17 | +STATUS = Status() |
| 18 | +INFO = Info() |
| 19 | +ACTION = Action() |
| 20 | +LOG = logger.getLogger(__name__) |
| 21 | +# Force the log level for this module |
| 22 | +#LOG.setLevel(logger.DEBUG) # One of DEBUG, INFO, WARNING, ERROR, CRITICAL |
| 23 | + |
| 24 | +class AxisPickToolButton(QToolButton, IndicatedMixIn): |
| 25 | + AxisSelected = pyqtSignal(str) |
| 26 | + CurrentAxisPosition = pyqtSignal(str,float) |
| 27 | + |
| 28 | + def __init__(self, parent=None): |
| 29 | + super(AxisPickToolButton, self).__init__(parent) |
| 30 | + self._textTemplate = 'Axis: %s' |
| 31 | + self._currentAxis = 'X' |
| 32 | + |
| 33 | + |
| 34 | + def _hal_init(self): |
| 35 | + self.createAxisButton(self._currentAxis) |
| 36 | + self.axisTriggered(self._currentAxis) |
| 37 | + |
| 38 | + def createAxisButton(self, text): |
| 39 | + tmpl = lambda s: self._textTemplate % s |
| 40 | + self.setText(tmpl(text)) |
| 41 | + self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) |
| 42 | + self.setMinimumSize(70, 40) |
| 43 | + self.clicked.connect(self.axisClicked) |
| 44 | + SettingMenu = QMenu() |
| 45 | + self.settingMenu = SettingMenu |
| 46 | + for i in INFO.AVAILABLE_AXES: |
| 47 | + axisButton = QAction(QIcon('exit24.png'), i, self) |
| 48 | + # weird lambda i=i to work around 'function closure' |
| 49 | + axisButton.triggered.connect(lambda state, i=i: self.axisTriggered(i)) |
| 50 | + SettingMenu.addAction(axisButton) |
| 51 | + self.setMenu(SettingMenu) |
| 52 | + |
| 53 | + def axisTriggered(self, data): |
| 54 | + tmpl = lambda s: self._textTemplate % s |
| 55 | + self.setText(tmpl(data)) |
| 56 | + self.AxisSelected.emit(data) |
| 57 | + |
| 58 | + def axisClicked(self): |
| 59 | + conversion = {'X':0, 'Y':1, "Z":2, 'A':3, "B":4, "C":5, 'U':6, 'V':7, 'W':8} |
| 60 | + digitValue = 0.0 |
| 61 | + try: |
| 62 | + p,relp,dtg = STATUS.get_position() |
| 63 | + text = self.text() |
| 64 | + for let in INFO.AVAILABLE_AXES: |
| 65 | + if let == text[-1]: |
| 66 | + digitValue = round(p[conversion[let]],5) |
| 67 | + break |
| 68 | + except Exception as e: |
| 69 | + print(e) |
| 70 | + return |
| 71 | + print(let, digitValue) |
| 72 | + self.AxisSelected.emit(let) |
| 73 | + self.CurrentAxisPosition.emit(let, digitValue) |
| 74 | + |
| 75 | + def setTextTemplate(self, data): |
| 76 | + self._textTemplate = data |
| 77 | + def getTextTemplate(self): |
| 78 | + return self._textTemplate |
| 79 | + def resetTextTemplate(self): |
| 80 | + self._textTemplate = 'Axis: %s' |
| 81 | + textTemplate = pyqtProperty(str, getTextTemplate, setTextTemplate, resetTextTemplate) |
| 82 | + |
| 83 | + def setCurrentAxis(self, data): |
| 84 | + self._currentAxis = data |
| 85 | + def getCurrentAxis(self): |
| 86 | + return self._currentAxis |
| 87 | + def resetCurrentAxis(self): |
| 88 | + self._currentAxis = 'X' |
| 89 | + currentAxis = pyqtProperty(str, getCurrentAxis, setCurrentAxis, resetCurrentAxis) |
| 90 | + |
0 commit comments