File: entry_widget.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 (284 lines) | stat: -rw-r--r-- 10,190 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
279
280
281
282
283
284
#!/usr/bin/env python3
# QTVcp Widget
#
# Copyright (c) 2017 Chris Morley
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# base code Found on Code Cookbook
#
# This widget pops up an onscreen keyboard for entries
# Used in the Macro and MDI line widget

from PyQt5 import QtWidgets, QtCore, QtGui
#from decimal import Decimal

# applicationle widgets
SIP_WIDGETS = [QtWidgets.QLineEdit]


class MyFlatPushButton(QtWidgets.QPushButton):
    def __init__(self, caption, min_size=(50, 50)):
        self.MIN_SIZE = min_size
        super(MyFlatPushButton, self).__init__(caption)
        self.setFocusPolicy(QtCore.Qt.NoFocus)

    def sizeHint(self):
        return QtCore.QSize(self.MIN_SIZE[0], self.MIN_SIZE[1])

class SoftInputWidget(QtWidgets.QDialog):
    def __init__(self, parent=None, keyboard_type='default'):
        super(SoftInputWidget, self).__init__(parent)
        self.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint |
                            QtCore.Qt.WindowStaysOnTopHint)
        self.INPUT_WIDGET = None
        self.PARENT_OBJECT = parent
        self.signalMapper = QtCore.QSignalMapper(self)

        self.NO_ORD_KEY_LIST = list()
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Left)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Up)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Right)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Down)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Backspace)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Enter)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Tab)
        self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Escape)

        self.do_layout(keyboard_type)

        self.signalMapper.mapped[int].connect(self.buttonClicked)

    def do_layout(self, keyboard_type='default'):
        """
        @param   keyboard_type:
        @return:
        """
        gl = QtWidgets.QVBoxLayout()
        self.setFont(self.PARENT_OBJECT.font())
        number_widget_list = []
        sym_list = list(range(0, 10))
        for sym in sym_list:
            button = MyFlatPushButton(str(sym))
            button.KEY_CHAR = ord(str(sym))
            number_widget_list.append(button)

        # alphabets
        alpha_widget_list = []
        sym_list = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
                    'new_row',
                    'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
                    'new_row',
                    'Z', 'X', 'C', 'V', 'B', 'N', 'M']
        for sym in sym_list:
            if sym == 'new_row':
                alpha_widget_list.append('new_row')
            else:
                button = MyFlatPushButton(sym)
                button.KEY_CHAR = ord(sym)
                alpha_widget_list.append(button)

        button = MyFlatPushButton('.')
        button.KEY_CHAR = ord('.')
        alpha_widget_list.append(button)
        button = MyFlatPushButton('-')
        button.KEY_CHAR = ord('-')
        alpha_widget_list.append(button)

        control_widget_list = []

        button = MyFlatPushButton('LINE\nUP')
        button.setToolTip('Cursor Up')
        button.KEY_CHAR = QtCore.Qt.Key_Up
        control_widget_list.append(button)

        button = MyFlatPushButton('LINE\nDOWN')
        button.setToolTip('Cursor Down')
        button.KEY_CHAR = QtCore.Qt.Key_Down
        control_widget_list.append(button)

        # space
        button = MyFlatPushButton('SPACE', min_size=(160, 50))
        button.KEY_CHAR = QtCore.Qt.Key_Space
        control_widget_list.append(button)

        # back space
        button = MyFlatPushButton('BACK')
        button.setToolTip('Backspace')
        button.KEY_CHAR = QtCore.Qt.Key_Backspace
        control_widget_list.append(button)

        # close
        button = MyFlatPushButton('CLOSE')
        button.setToolTip('Close Keyboard')
        button.KEY_CHAR = QtCore.Qt.Key_Escape
        control_widget_list.append(button)

        # enter
        button = MyFlatPushButton('ENTER', min_size=(105, 50))
        button.setToolTip('Enter Command')
        button.KEY_CHAR = QtCore.Qt.Key_Enter
        control_widget_list.append(button)

        MAX_COL = 10
        col = 0
        tlist = list()
        if keyboard_type == 'numeric':
            widget_list = number_widget_list
        elif keyboard_type == 'alpha':
            widget_list = alpha_widget_list
        else:
            widget_list = list()
            widget_list.extend(number_widget_list)
            widget_list.append('new_row')
            widget_list.extend(alpha_widget_list)

        widget_list.append('new_row')
        widget_list.extend(control_widget_list)

        for widget in widget_list:
            if widget == 'new_row':
                col = MAX_COL
            else:
                tlist.append(widget)
                widget.clicked.connect(self.signalMapper.map)
                self.signalMapper.setMapping(widget, widget.KEY_CHAR)

            if col == MAX_COL:
                col = 0
                v = QtWidgets.QHBoxLayout()
                v.addStretch()
                v.setSpacing(5)
                list(map(v.addWidget, tlist))
                v.addStretch()
                gl.addLayout(v)
                tlist = []
            else:
                col += 1

        v = QtWidgets.QHBoxLayout()
        v.setSpacing(5)
        v.addStretch()
        list(map(v.addWidget, tlist))
        v.addStretch()
        gl.addLayout(v)
        gl.setContentsMargins(4, 4, 4, 4)
        gl.setSpacing(4)
        gl.setSizeConstraint(gl.SetFixedSize)

        self.setLayout(gl)

    def reject(self):
        self.buttonClicked(QtCore.Qt.Key_Escape)

    def buttonClicked(self, char_ord):
        w = self.INPUT_WIDGET
        if char_ord in self.NO_ORD_KEY_LIST:
            keyPress = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char_ord, QtCore.Qt.NoModifier, '')
        else:
            keyPress = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char_ord, QtCore.Qt.NoModifier, chr(char_ord))
        # hide on enter or esc button click
        if char_ord == QtCore.Qt.Key_Escape:
            self.hide()
        else:
            # send keypress event to widget
            QtWidgets.QApplication.sendEvent(w, keyPress)
            if char_ord == QtCore.Qt.Key_Enter:
                self.hide()

        # line edit returnPressed event is triggering twice for press and release both
        # that is why do not send release event for special key
        if char_ord not in self.NO_ORD_KEY_LIST:
            keyRelease = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char_ord, QtCore.Qt.NoModifier, '')
            QtWidgets.QApplication.sendEvent(w, keyRelease)

    def show_input_panel(self, widget):
        self.INPUT_WIDGET = widget
        self.show()
        self.update_panel_position()
        self.setFocus()
        self.raise_()

    def update_panel_position(self):
        widget = self.INPUT_WIDGET
        if not widget: return

        widget_rect = widget.rect()
        widget_bottom = widget.mapToGlobal(QtCore.QPoint(widget.frameGeometry().x(),
                                                         widget.frameGeometry().y())).y()
        screen_height = QtWidgets.qApp.desktop().availableGeometry().height()
        input_panel_height = self.geometry().height() + 5

        if (screen_height - widget_bottom) > input_panel_height:
            # display input panel at bottom of widget
            panelPos = QtCore.QPoint(widget_rect.left(), widget_rect.bottom() + 2)
        else:
            # display input panel at top of widget
            panelPos = QtCore.QPoint(widget_rect.left(), widget_rect.top() - input_panel_height)

        panelPos = widget.mapToGlobal(panelPos)
        self.move(panelPos)


# a widget that calls our keyboard dialog
class TouchInterface(QtWidgets.QWidget):
    def __init__(self, parent):
        super(TouchInterface, self).__init__(parent)
        self._PARENT_WIDGET = parent
        self._input_panel_alpha = SoftInputWidget(parent, 'alpha')
        self._input_panel_numeric = SoftInputWidget(parent, 'numeric')
        self._input_panel_full = SoftInputWidget(parent, 'default')
        self.keyboard_enable = True
        self.keyboard_type = 'numeric'

    def childEvent(self, event):
        if event.type() == QtCore.QEvent.ChildAdded:
            if isinstance(event.child(), *SIP_WIDGETS):
                event.child().installEventFilter(self)

    def eventFilter(self, widget, event):
      try:
        if self._PARENT_WIDGET.focusWidget() == widget and event.type() == QtCore.QEvent.MouseButtonPress:
            if self.keyboard_enable is False:
                return False
            self.callDialog(widget, self.keyboard_type.lower())
        return False
      except:
        return False

    # can be class patched to call other entries - like qtvcp dialogs
    def callDialog(self, widget, ktype):
        if ktype == 'alpha':
            self._input_panel_alpha.show_input_panel(widget)
        elif ktype == 'numeric':
            self._input_panel_numeric.show_input_panel(widget)
        else:
            self._input_panel_full.show_input_panel(widget)

## testing ##
if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QWidget
    app = QtWidgets.QApplication([])
    w = QWidget()
    w.setGeometry(100, 100, 200, 100)
    w.setWindowTitle('Entry Widget')
    line = QtWidgets.QLineEdit()
    layout = QtWidgets.QVBoxLayout()
    layout.addWidget(line)
    test = TouchInterface(line)
#    test.callDialog(line, 'numeric')
#    test.callDialog(line, 'alpha')
    test.callDialog(line, 'default')
    w.setLayout(layout)
    w.show()
    sys.exit(app.exec_())