|
| 1 | +from PyQt5.QtCore import pyqtProperty, pyqtSignal, QSize, QObject, Qt |
| 2 | +from PyQt5.QtGui import QDoubleValidator, QIntValidator |
| 3 | +from PyQt5.QtWidgets import QWidget, QPushButton,QLineEdit, QHBoxLayout |
| 4 | + |
| 5 | +import decimal |
| 6 | +from decimal import Decimal |
| 7 | + |
| 8 | + |
| 9 | +class LineEdit(QLineEdit): |
| 10 | + |
| 11 | + clicked = pyqtSignal(bool) |
| 12 | + |
| 13 | + def __init__(self, parent=None): |
| 14 | + super().__init__(parent) |
| 15 | + #self.clicked.connect(lambda :self.entryClicked()) |
| 16 | + |
| 17 | + def mousePressEvent(self,event): |
| 18 | + self.clicked.emit(True) |
| 19 | + |
| 20 | +class TouchSpinBox(QWidget): |
| 21 | + def __init__(self, parent=None): |
| 22 | + super().__init__(parent) |
| 23 | + self._value = 0 |
| 24 | + self._multiplierIndex = 0 |
| 25 | + |
| 26 | + self.setMinimumSize(QSize(200, 25)) |
| 27 | + self.setMaximumSize(QSize(300, 50)) |
| 28 | + |
| 29 | + lay = QHBoxLayout() |
| 30 | + lay.setContentsMargins(0,0,0,0) |
| 31 | + lay.setSpacing(0) |
| 32 | + self.setLayout(lay) |
| 33 | + |
| 34 | + self.entry = LineEdit() |
| 35 | + self.entry.setMaximumHeight(100) |
| 36 | + self.entry.setMinimumWidth(100) |
| 37 | + |
| 38 | + self.entry.clicked.connect(lambda :self.callDialog(self)) |
| 39 | + lay.addWidget(self.entry) |
| 40 | + |
| 41 | + up = QPushButton('Up') |
| 42 | + up.setMaximumHeight(100) |
| 43 | + up.setMaximumWidth(40) |
| 44 | + up.pressed.connect(lambda :self.updateValue(True)) |
| 45 | + lay.addWidget(up) |
| 46 | + |
| 47 | + self.select = QPushButton(str('<-')) |
| 48 | + self.select.setMaximumHeight(100) |
| 49 | + self.select.setMaximumWidth(40) |
| 50 | + self.select.pressed.connect(lambda :self.moveLeft()) |
| 51 | + lay.addWidget(self.select) |
| 52 | + |
| 53 | + self.selectR = QPushButton(str('->')) |
| 54 | + self.selectR.setMaximumHeight(100) |
| 55 | + self.selectR.setMaximumWidth(40) |
| 56 | + self.selectR.pressed.connect(lambda :self.moveRight()) |
| 57 | + lay.addWidget(self.selectR) |
| 58 | + |
| 59 | + self.down = QPushButton('Dwn') |
| 60 | + self.down.setMaximumHeight(100) |
| 61 | + self.down.setMaximumWidth(40) |
| 62 | + self.down.pressed.connect(lambda :self.updateValue(False)) |
| 63 | + lay.addWidget(self.down) |
| 64 | + |
| 65 | + self.init_settings() |
| 66 | + |
| 67 | + def init_settings(self): |
| 68 | + self.entry.setText(str('00')) |
| 69 | + self.validInt = QIntValidator(-99999, 99999) |
| 70 | + self.entry.setValidator(self.validInt) |
| 71 | + |
| 72 | + def updateValue(self, state): |
| 73 | + mult = pow(10,self._multiplierIndex) |
| 74 | + print('update mult:',mult) |
| 75 | + decimal.getcontext().prec = 8 |
| 76 | + x = Decimal(self._value) + Decimal( mult) |
| 77 | + print('Dec:',x) |
| 78 | + if state: |
| 79 | + self.setValue(Decimal(self._value) + Decimal( mult)) |
| 80 | + else: |
| 81 | + self.setValue(Decimal(self._value) - Decimal( mult)) |
| 82 | + |
| 83 | + def moveLeft(self): |
| 84 | + mult = self._multiplierIndex + 1 |
| 85 | + t = len(self.entry.text()) |
| 86 | + print('mult',mult, 't',t) |
| 87 | + if t < 0:t=0 |
| 88 | + if mult >= t: |
| 89 | + mult = 0 |
| 90 | + |
| 91 | + pwr = pow(10,mult) |
| 92 | + print('pow',pwr) |
| 93 | + |
| 94 | + self._multiplierIndex = mult |
| 95 | + #self.select.setText(str(pow(10,mult))) |
| 96 | + print('left',t-self._multiplierIndex-1,mult) |
| 97 | + self.entry.setSelection (t-self._multiplierIndex-1,1) |
| 98 | + |
| 99 | + def moveRight(self): |
| 100 | + mult = self._multiplierIndex - 1 |
| 101 | + t = len(self.entry.text()) |
| 102 | + print('mult',mult, 't',t) |
| 103 | + if t < 0:t=0 |
| 104 | + if mult < 0: |
| 105 | + mult = t-1 |
| 106 | + |
| 107 | + pwr = pow(10,mult) |
| 108 | + print('pow',pwr) |
| 109 | + |
| 110 | + self._multiplierIndex = mult |
| 111 | + #self.select.setText(str(pow(10,mult))) |
| 112 | + print('right',t-self._multiplierIndex-1,mult) |
| 113 | + self.entry.setSelection (t-self._multiplierIndex-1,1) |
| 114 | + |
| 115 | + def text(self): |
| 116 | + return self.entry.text() |
| 117 | + |
| 118 | + def value(self): |
| 119 | + return float(self.entry.text()) |
| 120 | + |
| 121 | + def setValue(self, value): |
| 122 | + self._value = value |
| 123 | + self.entry.setText(str(value)) |
| 124 | + |
| 125 | + # class patch or should we call a dialog from here too? |
| 126 | + def callDialog(self,widget): |
| 127 | + pass |
| 128 | + |
| 129 | +class TouchDoubleSpinBox(TouchSpinBox): |
| 130 | + def __init__(self, parent=None): |
| 131 | + super(TouchDoubleSpinBox, self).__init__(parent) |
| 132 | + |
| 133 | + def init_settings(self): |
| 134 | + self._value = 10.001 |
| 135 | + self.validDouble = QDoubleValidator(-999.999, 999.999, 3) |
| 136 | + self.entry.setText(str('10.001')) |
| 137 | + self.entry.setValidator(self.validDouble) |
| 138 | + self.entry.returnPressed.connect(lambda :self.entryUpdate()) |
| 139 | + |
| 140 | + def updateValue(self, state): |
| 141 | + def convert(x): |
| 142 | + return ['0','1','2','3','4','5','6','7','8','9','-',' ','end'][x] |
| 143 | + def indexof(x): |
| 144 | + return ['0','1','2','3','4','5','6','7','8','9','-',' ','end'].index(x) |
| 145 | + if self.entry.text() == '': |
| 146 | + self.entry.setText('0') |
| 147 | + pos = self.getPosition(self._multiplierIndex) |
| 148 | + x = self.entry.text()[pos] |
| 149 | + print('update txt:',x,'pos',pos) |
| 150 | + if state: |
| 151 | + next = convert(indexof(x)+1) |
| 152 | + if next in( '-',' ','end'): |
| 153 | + if pos == 0: |
| 154 | + if next != 'end': |
| 155 | + pass |
| 156 | + else: |
| 157 | + next = 0 |
| 158 | + else: |
| 159 | + next = 0 |
| 160 | + else: |
| 161 | + next = convert(indexof(x)-1) |
| 162 | + if next in( '-',' ','end'): |
| 163 | + if pos == 0: |
| 164 | + if next != 'end': |
| 165 | + pass |
| 166 | + else: |
| 167 | + next = 0 |
| 168 | + else: |
| 169 | + next = 0 |
| 170 | + self.entry.setText( self.replace_at_index(self.entry.text(),pos,str(next))) |
| 171 | + self.entry.setSelection (pos,1) |
| 172 | + |
| 173 | + def replace_at_index(self,text,index=0,replacement=''): |
| 174 | + return '%s%s%s'%(text[:index],replacement,text[index+1:]) |
| 175 | + |
| 176 | + def getPosition(self, index): |
| 177 | + t = len(self.entry.text())-1 |
| 178 | + print('T:',t,'Index:',index) |
| 179 | + if t < 0:t=0 |
| 180 | + if index > t: |
| 181 | + self.entry.setText('0'+ self.entry.text()) |
| 182 | + t = len(self.entry.text())-1 |
| 183 | + #index = 0 |
| 184 | + |
| 185 | + if self.entry.text()[t-index] in ('.',','): |
| 186 | + index+=1 |
| 187 | + pos = t-index |
| 188 | + self._multiplierIndex = index |
| 189 | + return pos |
| 190 | + |
| 191 | + def moveLeft(self): |
| 192 | + index = self._multiplierIndex +1 |
| 193 | + t = len(self.entry.text())-1 |
| 194 | + print('T:',t,'Index:',index) |
| 195 | + if t < 0:t=0 |
| 196 | + if index == -1: |
| 197 | + if '.' in self.entry.text(): |
| 198 | + self.entry.setText(self.entry.text()+'0') |
| 199 | + t = len(self.entry.text())-1 |
| 200 | + index = 0 |
| 201 | + else: |
| 202 | + self.entry.setText(self.entry.text()+'.0') |
| 203 | + t = len(self.entry.text())-1 |
| 204 | + index = 0 |
| 205 | + |
| 206 | + |
| 207 | + if self.entry.text()[t-index] in ('.',','): |
| 208 | + index-=1 |
| 209 | + pos = t-index |
| 210 | + self._multiplierIndex = index |
| 211 | + |
| 212 | + try: |
| 213 | + print('text;',self.entry.text()[pos]) |
| 214 | + except: |
| 215 | + pass |
| 216 | + #self.select.setText(str(pos)) |
| 217 | + self.entry.setSelection (pos,1) |
| 218 | + |
| 219 | + def moveRight(self): |
| 220 | + index = self._multiplierIndex -1 |
| 221 | + t = len(self.entry.text())-1 |
| 222 | + print('T:',t,'Index:',index) |
| 223 | + if t < 0:t=0 |
| 224 | + if index == -1: |
| 225 | + if '.' in self.entry.text(): |
| 226 | + self.entry.setText(self.entry.text()+'0') |
| 227 | + t = len(self.entry.text())-1 |
| 228 | + index = 0 |
| 229 | + else: |
| 230 | + self.entry.setText(self.entry.text()+'.0') |
| 231 | + t = len(self.entry.text())-1 |
| 232 | + index = 0 |
| 233 | + |
| 234 | + |
| 235 | + if self.entry.text()[t-index] in ('.',','): |
| 236 | + index-=1 |
| 237 | + pos = t-index |
| 238 | + self._multiplierIndex = index |
| 239 | + |
| 240 | + try: |
| 241 | + print('text;',self.entry.text()[pos]) |
| 242 | + except: |
| 243 | + pass |
| 244 | + #self.select.setText(str(pos)) |
| 245 | + self.entry.setSelection (pos,1) |
| 246 | + |
| 247 | + def moveLeft(self): |
| 248 | + pos = self.getPosition(self._multiplierIndex +1) |
| 249 | + |
| 250 | + try: |
| 251 | + print('text;',self.entry.text()[pos]) |
| 252 | + except: |
| 253 | + pass |
| 254 | + |
| 255 | + #self.select.setText(str(pos)) |
| 256 | + self.entry.setSelection (pos,1) |
| 257 | + |
| 258 | + def setValue(self, value): |
| 259 | + self.entry.setText('{:4.3f}'.format(value)) |
| 260 | + |
| 261 | + def entryUpdate(self): |
| 262 | + print('Update:',self.entry.text()) |
| 263 | + if self.entry.text() == '': |
| 264 | + self.setValue(0) |
| 265 | + |
| 266 | +# for direct testing |
| 267 | +if __name__ == "__main__": |
| 268 | + from PyQt5.QtWidgets import * |
| 269 | + from PyQt5.QtCore import * |
| 270 | + from PyQt5.QtGui import * |
| 271 | + import sys |
| 272 | + |
| 273 | + app = QApplication(sys.argv) |
| 274 | + w = TouchSpinBox() |
| 275 | + w.show() |
| 276 | + w = TouchDoubleSpinBox() |
| 277 | + w.show() |
| 278 | + sys.exit( app.exec_() ) |
0 commit comments