|
| 1 | +############################ |
| 2 | +# **** IMPORT SECTION **** # |
| 3 | +############################ |
| 4 | +import sys |
| 5 | +from PyQt5.QtCore import * |
| 6 | +from PyQt5.QtGui import * |
| 7 | +from PyQt5.QtWidgets import * |
| 8 | +from qtvcp.core import Status |
| 9 | +from qtvcp.widgets.hal_selectionbox import HALSelectionBox |
| 10 | +from qtvcp.widgets.simple_widgets import PushButton |
| 11 | +from qtvcp.lib.aux_program_loader import Aux_program_loader |
| 12 | + |
| 13 | +########################################### |
| 14 | +# **** instantiate libraries section **** # |
| 15 | +########################################### |
| 16 | +STATUS = Status() |
| 17 | +EXTPROG = Aux_program_loader() |
| 18 | +################################### |
| 19 | +# **** HANDLER CLASS SECTION **** # |
| 20 | +################################### |
| 21 | + |
| 22 | +class HandlerClass: |
| 23 | + |
| 24 | + ######################## |
| 25 | + # **** INITIALIZE **** # |
| 26 | + ######################## |
| 27 | + # widgets allows access to widgets from the qtvcp files |
| 28 | + # at this point the widgets and hal pins are not instantiated |
| 29 | + def __init__(self, halcomp,widgets,paths): |
| 30 | + self.w = widgets |
| 31 | + self.h = halcomp |
| 32 | + self.buttonDict = dict() |
| 33 | + |
| 34 | + ########################################## |
| 35 | + # Special Functions called from QTVCP |
| 36 | + ########################################## |
| 37 | + |
| 38 | + # at this point: |
| 39 | + # the widgets are instantiated. |
| 40 | + # the HAL pins are built but HAL is not set ready |
| 41 | + def initialized__(self): |
| 42 | + geom = self.w.frameGeometry() |
| 43 | + geom.moveCenter(QDesktopWidget().availableGeometry().center()) |
| 44 | + self.w.setGeometry(geom) |
| 45 | + |
| 46 | + if self.w.USEROPTIONS_ is not None: |
| 47 | + try: |
| 48 | + num = int(self.w.USEROPTIONS_[0]) |
| 49 | + except: |
| 50 | + print('Error with test_button number selection - not a number - using 1') |
| 51 | + num = 1 |
| 52 | + |
| 53 | + if num >1: |
| 54 | + for i in range(num-1): |
| 55 | + self.addButton() |
| 56 | + # make window taller now |
| 57 | + x = self.w.geometry().x() |
| 58 | + y = self.w.geometry().y() |
| 59 | + w = self.w.geometry().width() |
| 60 | + h = self.w.geometry().height() + 37 |
| 61 | + self.w.setGeometry(x,y,w,h) |
| 62 | + self.w.setMinimumHeight(h) |
| 63 | + |
| 64 | + self.addButton() |
| 65 | + self.w.setWindowFlags(Qt.WindowStaysOnTopHint) |
| 66 | + self.w.setWindowTitle('{}'.format(self.h.comp.getprefix())) |
| 67 | + |
| 68 | + # build a button/controls line |
| 69 | + def addButton(self): |
| 70 | + |
| 71 | + # new toolbar added to window |
| 72 | + toolbar = QToolBar(self.w) |
| 73 | + self.w.addToolBar(Qt.LeftToolBarArea, toolbar) |
| 74 | + |
| 75 | + # containers |
| 76 | + w = QWidget() |
| 77 | + w.setContentsMargins(0,0,0,0) |
| 78 | + hbox = QHBoxLayout(w) |
| 79 | + hbox.setContentsMargins(0,0,0,0) |
| 80 | + |
| 81 | + # for button name |
| 82 | + le = QLineEdit() |
| 83 | + le.setText(self.h.comp.getprefix()) |
| 84 | + |
| 85 | + # HAL button to display state |
| 86 | + button = PushButton() |
| 87 | + button.setProperty('indicator_option',True) |
| 88 | + button._halpin_option = False |
| 89 | + button.hal_init('button') |
| 90 | + button.setMaximumWidth(30) |
| 91 | + |
| 92 | + hbox.addWidget(button) |
| 93 | + hbox.addWidget(le) |
| 94 | + |
| 95 | + # menu for controls |
| 96 | + menu = QMenu() |
| 97 | + menu.setMinimumWidth(259) |
| 98 | + |
| 99 | + # Check option checkbutton |
| 100 | + actionCheck = QAction('Set Checkable',menu) |
| 101 | + actionCheck.setCheckable(True) |
| 102 | + actionCheck.triggered.connect(lambda w, b=(actionCheck,button): self.actionTriggered('Checkable',b)) |
| 103 | + menu.addAction(actionCheck) |
| 104 | + |
| 105 | + # color option launch button |
| 106 | + actionColor = QAction('Set Indicator Color',menu) |
| 107 | + actionColor.triggered.connect(lambda w, b=(actionColor,button): self.actionTriggered('Color',b)) |
| 108 | + menu.addAction(actionColor) |
| 109 | + |
| 110 | + # remember signal watched, line edit widget, button widget and last state |
| 111 | + self.buttonDict[button] = [None,le,None,button,False] |
| 112 | + |
| 113 | + # button to pop menu |
| 114 | + btn = QPushButton('Opt') |
| 115 | + btn.setMaximumWidth(50) |
| 116 | + btn.setMenu(menu) |
| 117 | + |
| 118 | + hbox.addWidget(btn) |
| 119 | + |
| 120 | + # combo box for HAL pin selection |
| 121 | + cb = HALSelectionBox() |
| 122 | + cb.setShowTypes([cb.PINS,cb.SIGNALS]) |
| 123 | + cb.setPinTypes([cb.HAL_BIT]) |
| 124 | + cb.setSignalTypes([cb.HAL_BIT], driven = [False,True]) |
| 125 | + cb.hal_init() |
| 126 | + cb.selectionUpdated.connect(lambda w: self.signalSelected(w,button)) |
| 127 | + |
| 128 | + # wrap combobox so as to add it to menu |
| 129 | + action = QWidgetAction(menu) |
| 130 | + action.setDefaultWidget(cb) |
| 131 | + #menu.addAction(action) |
| 132 | + |
| 133 | + # HalMeter option launch button |
| 134 | + actionHalMeter = QAction('Load HalMeter',menu) |
| 135 | + actionHalMeter.triggered.connect(lambda w, b=(actionHalMeter,button): self.actionTriggered('HalMeter',b)) |
| 136 | + menu.addAction(actionHalMeter) |
| 137 | + |
| 138 | + # Led option launch button |
| 139 | + actionLed = QAction('Load Test Led',menu) |
| 140 | + actionLed.triggered.connect(lambda w, b=(actionLed,button): self.actionTriggered('Led',b)) |
| 141 | + menu.addAction(actionLed) |
| 142 | + |
| 143 | + # button to add another button toolbar |
| 144 | + actionAdd = QAction('Add Button',menu) |
| 145 | + actionAdd.triggered.connect(lambda b: self.actionTriggered('Add', None)) |
| 146 | + menu.addAction(actionAdd) |
| 147 | + |
| 148 | + toolbar.addWidget(w) |
| 149 | + |
| 150 | + ######################## |
| 151 | + # callbacks from STATUS # |
| 152 | + ######################## |
| 153 | + |
| 154 | + ####################### |
| 155 | + # callbacks from form # |
| 156 | + ####################### |
| 157 | + |
| 158 | + def actionTriggered(self, widget,b=None): |
| 159 | + # b[0] is action button |
| 160 | + # b[1] is main button |
| 161 | + if widget == 'Add': |
| 162 | + # add a toolbar with button and controls |
| 163 | + self.addButton() |
| 164 | + # make window taller now |
| 165 | + x = self.w.geometry().x() |
| 166 | + y = self.w.geometry().y() |
| 167 | + w = self.w.geometry().width() |
| 168 | + h = self.w.geometry().height() + 37 |
| 169 | + self.w.setGeometry(x,y,w,h) |
| 170 | + self.w.setMinimumHeight(h) |
| 171 | + elif widget == 'Checkable': |
| 172 | + b[1].setCheckable(b[0].isChecked()) |
| 173 | + # kill existing signals - we need to change signal type |
| 174 | + b[1].disconnectSignals() |
| 175 | + # add new signals back |
| 176 | + b[1].connectSignals() |
| 177 | + elif widget == 'Color': |
| 178 | + # Get a color from the text dialog |
| 179 | + color = QColorDialog.getColor() |
| 180 | + b[1].setProperty('on_color',color) |
| 181 | + elif widget == 'HalMeter': |
| 182 | + EXTPROG.load_halmeter() |
| 183 | + elif widget == 'Led': |
| 184 | + EXTPROG.load_test_led() |
| 185 | + |
| 186 | + ##################### |
| 187 | + # general functions # |
| 188 | + ##################### |
| 189 | + |
| 190 | + def updateLabel(self,v): |
| 191 | + self.w.hallabel.setDisplay(v) |
| 192 | + |
| 193 | + def signalSelected(self, sig,button): |
| 194 | + print('Watching:',sig) |
| 195 | + if sig != '': |
| 196 | + self.buttonDict[button][0] = sig |
| 197 | + return |
| 198 | + |
| 199 | + ##################### |
| 200 | + # KEY BINDING CALLS # |
| 201 | + ##################### |
| 202 | + |
| 203 | + ########################### |
| 204 | + # **** closing event **** # |
| 205 | + ########################### |
| 206 | + |
| 207 | + ############################## |
| 208 | + # required class boiler code # |
| 209 | + ############################## |
| 210 | + |
| 211 | + def __getitem__(self, item): |
| 212 | + return getattr(self, item) |
| 213 | + def __setitem__(self, item, value): |
| 214 | + return setattr(self, item, value) |
| 215 | + |
| 216 | + |
| 217 | +################################ |
| 218 | +# required handler boiler code # |
| 219 | +################################ |
| 220 | + |
| 221 | +def get_handlers(halcomp,widgets,paths): |
| 222 | + return [HandlerClass(halcomp,widgets,paths)] |
0 commit comments