File: touchEntry.py

package info (click to toggle)
linuxcnc 1%3A2.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 285,604 kB
  • sloc: python: 202,568; ansic: 109,036; cpp: 99,239; tcl: 16,054; xml: 10,631; sh: 10,303; makefile: 1,255; javascript: 138; sql: 72; asm: 15
file content (278 lines) | stat: -rw-r--r-- 8,323 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from PyQt5.QtCore import pyqtProperty, pyqtSignal, QSize, QObject, Qt
from PyQt5.QtGui import QDoubleValidator, QIntValidator
from PyQt5.QtWidgets import QWidget, QPushButton,QLineEdit, QHBoxLayout

import decimal
from decimal import Decimal


class LineEdit(QLineEdit):

    clicked = pyqtSignal(bool)

    def __init__(self, parent=None):
        super().__init__(parent)
        #self.clicked.connect(lambda :self.entryClicked())

    def mousePressEvent(self,event):
        self.clicked.emit(True)

class TouchSpinBox(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._value = 0
        self._multiplierIndex = 0

        self.setMinimumSize(QSize(200, 25))    
        self.setMaximumSize(QSize(300, 50))    

        lay = QHBoxLayout()
        lay.setContentsMargins(0,0,0,0)
        lay.setSpacing(0)
        self.setLayout(lay)

        self.entry = LineEdit()
        self.entry.setMaximumHeight(100)
        self.entry.setMinimumWidth(100)

        self.entry.clicked.connect(lambda :self.callDialog(self))
        lay.addWidget(self.entry)

        up = QPushButton('Up')
        up.setMaximumHeight(100)
        up.setMaximumWidth(40)
        up.pressed.connect(lambda :self.updateValue(True))
        lay.addWidget(up)

        self.select = QPushButton(str('<-'))
        self.select.setMaximumHeight(100)
        self.select.setMaximumWidth(40)
        self.select.pressed.connect(lambda :self.moveLeft())
        lay.addWidget(self.select)

        self.selectR = QPushButton(str('->'))
        self.selectR.setMaximumHeight(100)
        self.selectR.setMaximumWidth(40)
        self.selectR.pressed.connect(lambda :self.moveRight())
        lay.addWidget(self.selectR)

        self.down = QPushButton('Dwn')
        self.down.setMaximumHeight(100)
        self.down.setMaximumWidth(40)
        self.down.pressed.connect(lambda :self.updateValue(False))
        lay.addWidget(self.down)

        self.init_settings()

    def init_settings(self):
        self.entry.setText(str('00'))
        self.validInt = QIntValidator(-99999, 99999)
        self.entry.setValidator(self.validInt)

    def updateValue(self, state):
        mult = pow(10,self._multiplierIndex)
        print('update mult:',mult)
        decimal.getcontext().prec = 8
        x = Decimal(self._value) + Decimal( mult)
        print('Dec:',x)
        if state:
            self.setValue(Decimal(self._value) + Decimal( mult))
        else:
            self.setValue(Decimal(self._value) - Decimal( mult))

    def moveLeft(self):
        mult = self._multiplierIndex + 1
        t = len(self.entry.text())
        print('mult',mult, 't',t)
        if t < 0:t=0
        if mult >= t:
            mult = 0

        pwr = pow(10,mult)
        print('pow',pwr)

        self._multiplierIndex = mult
        #self.select.setText(str(pow(10,mult)))
        print('left',t-self._multiplierIndex-1,mult)
        self.entry.setSelection (t-self._multiplierIndex-1,1)

    def moveRight(self):
        mult = self._multiplierIndex - 1
        t = len(self.entry.text())
        print('mult',mult, 't',t)
        if t < 0:t=0
        if mult < 0:
            mult = t-1

        pwr = pow(10,mult)
        print('pow',pwr)

        self._multiplierIndex = mult
        #self.select.setText(str(pow(10,mult)))
        print('right',t-self._multiplierIndex-1,mult)
        self.entry.setSelection (t-self._multiplierIndex-1,1)

    def text(self):
        return self.entry.text()

    def value(self):
        return float(self.entry.text())

    def setValue(self, value):
        self._value = value
        self.entry.setText(str(value))

    # class patch or should we call a dialog from here too?
    def callDialog(self,widget):
        pass

class TouchDoubleSpinBox(TouchSpinBox):
    def __init__(self, parent=None):
        super(TouchDoubleSpinBox, self).__init__(parent)

    def init_settings(self):
        self._value = 10.001
        self.validDouble = QDoubleValidator(-999.999, 999.999, 3)
        self.entry.setText(str('10.001'))
        self.entry.setValidator(self.validDouble)
        self.entry.returnPressed.connect(lambda :self.entryUpdate())

    def updateValue(self, state):
        def convert(x):
            return ['0','1','2','3','4','5','6','7','8','9','-',' ','end'][x]
        def indexof(x):
            return ['0','1','2','3','4','5','6','7','8','9','-',' ','end'].index(x)
        if self.entry.text() == '':
            self.entry.setText('0')
        pos = self.getPosition(self._multiplierIndex)
        x = self.entry.text()[pos]
        print('update txt:',x,'pos',pos)
        if state:
            next = convert(indexof(x)+1)
            if next in( '-',' ','end'):
                if pos == 0:
                    if next != 'end':
                        pass
                    else:
                        next = 0
                else:
                    next = 0
        else:
            next = convert(indexof(x)-1)
            if next in( '-',' ','end'):
                if pos == 0:
                    if next != 'end':
                        pass
                    else:
                        next = 0
                else:
                    next = 0
        self.entry.setText( self.replace_at_index(self.entry.text(),pos,str(next)))
        self.entry.setSelection (pos,1)

    def replace_at_index(self,text,index=0,replacement=''):
        return '%s%s%s'%(text[:index],replacement,text[index+1:])

    def getPosition(self, index):
        t = len(self.entry.text())-1
        print('T:',t,'Index:',index)
        if t < 0:t=0
        if index > t:
            self.entry.setText('0'+ self.entry.text())
            t = len(self.entry.text())-1
            #index = 0

        if self.entry.text()[t-index] in ('.',','):
            index+=1
        pos = t-index
        self._multiplierIndex = index
        return pos

    def moveLeft(self):
        index = self._multiplierIndex +1
        t = len(self.entry.text())-1
        print('T:',t,'Index:',index)
        if t < 0:t=0
        if index == -1:
            if '.' in self.entry.text():
                self.entry.setText(self.entry.text()+'0')
                t = len(self.entry.text())-1
                index = 0
            else:
                self.entry.setText(self.entry.text()+'.0')
                t = len(self.entry.text())-1
                index = 0


        if self.entry.text()[t-index] in ('.',','):
            index-=1
        pos = t-index
        self._multiplierIndex = index

        try:
            print('text;',self.entry.text()[pos])
        except:
            pass
        #self.select.setText(str(pos))
        self.entry.setSelection (pos,1)

    def moveRight(self):
        index = self._multiplierIndex -1
        t = len(self.entry.text())-1
        print('T:',t,'Index:',index)
        if t < 0:t=0
        if index == -1:
            if '.' in self.entry.text():
                self.entry.setText(self.entry.text()+'0')
                t = len(self.entry.text())-1
                index = 0
            else:
                self.entry.setText(self.entry.text()+'.0')
                t = len(self.entry.text())-1
                index = 0


        if self.entry.text()[t-index] in ('.',','):
            index-=1
        pos = t-index
        self._multiplierIndex = index

        try:
            print('text;',self.entry.text()[pos])
        except:
            pass
        #self.select.setText(str(pos))
        self.entry.setSelection (pos,1)

    def moveLeft(self):
        pos = self.getPosition(self._multiplierIndex +1)

        try:
            print('text;',self.entry.text()[pos])
        except:
            pass

        #self.select.setText(str(pos))
        self.entry.setSelection (pos,1)

    def setValue(self, value):
        self.entry.setText('{:4.3f}'.format(value))

    def entryUpdate(self):
        print('Update:',self.entry.text())
        if self.entry.text() == '':
            self.setValue(0)

# for direct testing
if __name__ == "__main__":
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    import sys

    app = QApplication(sys.argv)
    w = TouchSpinBox()
    w.show()
    w = TouchDoubleSpinBox()
    w.show()
    sys.exit( app.exec_() )