Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
444
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. import sys
  4. from PyQt4 import QtCore, QtGui, uic
  5.  
  6.  
  7. # applicable widgets
  8. SIP_WIDGETS = [QtGui.QLineEdit]
  9.  
  10. class MyFlatPushButton(QtGui.QPushButton):
  11. def __init__(self, caption, min_size=(75,75)):
  12. self.MIN_SIZE = min_size
  13. QtGui.QPushButton.__init__(self, caption)
  14. self.setFocusPolicy(QtCore.Qt.NoFocus)
  15.  
  16. def sizeHint(self):
  17. return QtCore.QSize(self.MIN_SIZE[0], self.MIN_SIZE[1])
  18.  
  19.  
  20. class SoftInputWidget(QtGui.QDialog):
  21. def __init__(self, parent_object, keyboard_type='default'):
  22. QtGui.QDialog.__init__(self)
  23. self.setWindowFlags(QtCore.Qt.Tool | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint)
  24. self.INPUT_WIDGET = None
  25. self.PARENT_OBJECT = parent_object
  26. self.signalMapper = QtCore.QSignalMapper(self)
  27.  
  28. self.NO_ORD_KEY_LIST = list()
  29. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Left)
  30. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Up)
  31. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Right)
  32. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Down)
  33. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Backspace)
  34. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Enter)
  35. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Tab)
  36. self.NO_ORD_KEY_LIST.append(QtCore.Qt.Key_Escape)
  37.  
  38. self.do_layout(keyboard_type)
  39.  
  40. self.signalMapper.mapped[int].connect(self.buttonClicked)
  41.  
  42. def do_layout(self, keyboard_type='default'):
  43. """
  44. @param keyboard_type:
  45. @return:
  46. """
  47. gl = QtGui.QVBoxLayout()
  48. self.setFont(self.PARENT_OBJECT.font())
  49. number_widget_list = []
  50. sym_list = range(0, 10)
  51. for sym in sym_list:
  52. button = MyFlatPushButton(str(sym))
  53. button.KEY_CHAR = ord(str(sym))
  54. number_widget_list.append(button)
  55.  
  56. button = MyFlatPushButton('*')
  57. button.KEY_CHAR = ord('*')
  58. number_widget_list.append(button)
  59.  
  60. # alphabets
  61. alpha_widget_list = []
  62. sym_list = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
  63. 'new_row',
  64. 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',
  65. 'new_row',
  66. 'z', 'x', 'c', 'v', 'b', 'n', 'm']
  67. for sym in sym_list:
  68. if sym == 'new_row':
  69. alpha_widget_list.append('new_row')
  70. else:
  71. button = MyFlatPushButton(sym)
  72. button.KEY_CHAR = ord(sym)
  73. alpha_widget_list.append(button)
  74.  
  75. # back space
  76. control_widget_list = []
  77. button = MyFlatPushButton('<B')
  78. button.setToolTip('Backspace')
  79. button.KEY_CHAR = QtCore.Qt.Key_Backspace
  80. control_widget_list.append(button)
  81. control_widget_list.append('sep')
  82.  
  83. # tab
  84. button = MyFlatPushButton('>T')
  85. button.KEY_CHAR = QtCore.Qt.Key_Tab
  86. control_widget_list.append(button)
  87. control_widget_list.append('sep')
  88.  
  89. # space
  90. button = MyFlatPushButton('SPC')
  91. button.KEY_CHAR = QtCore.Qt.Key_Space
  92. control_widget_list.append(button)
  93. control_widget_list.append('sep')
  94.  
  95. # close
  96. button = MyFlatPushButton('X')
  97. button.KEY_CHAR = QtCore.Qt.Key_Escape
  98. control_widget_list.append(button)
  99. control_widget_list.append('sep')
  100.  
  101. # enter
  102. button = MyFlatPushButton('ENT', min_size=(110, 60))
  103. button.KEY_CHAR = QtCore.Qt.Key_Enter
  104. control_widget_list.append(button)
  105. control_widget_list.append('sep')
  106.  
  107. alist = list()
  108. alist.append((QtCore.Qt.Key_Left, '<-'))
  109. alist.append((QtCore.Qt.Key_Right, '->'))
  110. alist.append((QtCore.Qt.Key_Up, '^'))
  111. alist.append((QtCore.Qt.Key_Down, 'v'))
  112. for key in alist:
  113. button = MyFlatPushButton(key[1])
  114. button.KEY_CHAR = key[0]
  115. control_widget_list.append(button)
  116.  
  117. MAX_COL = 10
  118. col = 0
  119. tlist = list()
  120. if keyboard_type == 'numeric':
  121. widget_list = number_widget_list
  122. elif keyboard_type == 'alpha':
  123. widget_list = alpha_widget_list
  124. else:
  125. widget_list = list()
  126. widget_list.extend(number_widget_list)
  127. widget_list.append('new_row')
  128. widget_list.extend(alpha_widget_list)
  129.  
  130. widget_list.append('new_row')
  131. widget_list.extend(control_widget_list)
  132.  
  133. for widget in widget_list:
  134. if widget == 'new_row':
  135. col = MAX_COL
  136. elif widget == 'sep':
  137. tlist.append(self.get_vline())
  138. continue
  139. else:
  140. tlist.append(widget)
  141. widget.clicked.connect(self.signalMapper.map)
  142. self.signalMapper.setMapping(widget, widget.KEY_CHAR)
  143.  
  144. if col == MAX_COL:
  145. col = 0
  146. v = QtGui.QHBoxLayout()
  147. v.addStretch()
  148. v.setSpacing(5)
  149. map(v.addWidget, tlist)
  150. v.addStretch()
  151. gl.addLayout(v)
  152. tlist = []
  153. else:
  154. col += 1
  155.  
  156. v = QtGui.QHBoxLayout()
  157. v.setSpacing(5)
  158. v.addStretch()
  159. map(v.addWidget, tlist)
  160. v.addStretch()
  161. gl.addLayout(v)
  162. gl.setContentsMargins(0, 0, 0, 0)
  163. gl.setSpacing(5)
  164. gl.setSizeConstraint(gl.SetFixedSize)
  165. self.setLayout(gl)
  166.  
  167. def reject(self):
  168. self.buttonClicked(QtCore.Qt.Key_Escape)
  169.  
  170. def buttonClicked(self, char_ord):
  171. w = self.INPUT_WIDGET
  172. if char_ord in self.NO_ORD_KEY_LIST:
  173. keyPress = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char_ord, QtCore.Qt.NoModifier, '')
  174. else:
  175. keyPress = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char_ord, QtCore.Qt.NoModifier, chr(char_ord))
  176.  
  177. # send keypress event to widget
  178. QtGui.QApplication.sendEvent(w, keyPress)
  179.  
  180. # line edit returnPressed event is triggering twise for press and release both
  181. # that is why do not send release event for special key
  182. if char_ord not in self.NO_ORD_KEY_LIST:
  183. keyRelease = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, char_ord, QtCore.Qt.NoModifier, '')
  184. QtGui.QApplication.sendEvent(w, keyRelease)
  185.  
  186. # hide on enter or esc button click
  187. if char_ord in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Escape):
  188. self.hide()
  189.  
  190. def show_input_panel(self, widget):
  191. self.INPUT_WIDGET = widget
  192. self.show()
  193. self.update_panel_position()
  194.  
  195. def update_panel_position(self):
  196. widget = self.INPUT_WIDGET
  197. if not widget: return
  198.  
  199. widget_rect = widget.rect()
  200. widget_bottom = widget.mapToGlobal(QtCore.QPoint(widget.frameGeometry().x(), widget.frameGeometry().y())).y()
  201. screen_height = QtGui.qApp.desktop().availableGeometry().height()
  202. input_panel_height = self.geometry().height() + 5
  203.  
  204. #inputPanelPos
  205. if (screen_height - widget_bottom) > input_panel_height:
  206. # display input panel at bottom of widget
  207. panelPos = QtCore.QPoint(0, 35)
  208. else:
  209. # display input panel at top of widget
  210. panelPos = QtCore.QPoint(0, 35)
  211. #
  212. panelPos = widget.mapToGlobal(panelPos)
  213. self.move(panelPos)
  214.  
  215. def _get_line(self, vertical=True):
  216. line = QtGui.QFrame()
  217. line.setContentsMargins(0, 0, 0, 0)
  218. if vertical is True:
  219. line.setFrameShape(line.VLine)
  220. else:
  221. line.setFrameShape(line.HLine)
  222. line.setFrameShadow(line.Sunken)
  223. return line
  224.  
  225. def get_hline(self):
  226. return self._get_line(vertical=False)
  227.  
  228. def get_vline(self):
  229. return self._get_line()
  230.  
  231.  
  232. class TouchInterface(QtCore.QObject):
  233. def __init__(self, PARENT_WIDGET):
  234. QtCore.QObject.__init__(self)
  235. self._PARENT_WIDGET = PARENT_WIDGET
  236. self._input_panel_all = SoftInputWidget(PARENT_WIDGET, 'default')
  237. self._input_panel_numeric = SoftInputWidget(PARENT_WIDGET, 'numeric')
  238.  
  239. def childEvent(self, event):
  240. if event.type() == QtCore.QEvent.ChildAdded:
  241. if isinstance(event.child(), *SIP_WIDGETS):
  242. event.child().installEventFilter(self)
  243.  
  244. def eventFilter(self, widget, event):
  245. if self._PARENT_WIDGET.focusWidget() == widget and event.type() == QtCore.QEvent.MouseButtonPress:
  246. if hasattr(widget, 'keyboard_type'):
  247. if widget.keyboard_type == 'default':
  248. self._input_panel_all.show_input_panel(widget)
  249. elif widget.keyboard_type == 'numeric':
  250. self._input_panel_numeric.show_input_panel(widget)
  251.  
  252. return False
  253.  
  254.  
  255. class TouchInputWidget(QtGui.QWidget):
  256. def __init__(self):
  257. QtGui.QWidget.__init__(self)
  258. self.touch_interface = TouchInterface(self)
  259.  
  260. def childEvent(self, event):
  261. self.touch_interface.childEvent(event)
  262.  
  263. def eventFilter(self, widget, event):
  264. return self.touch_interface.eventFilter(widget, event)
  265.  
  266.  
  267.  
  268. class MyWindowClass(TouchInputWidget):
  269. def __init__ (self, parent=None):
  270. TouchInputWidget.__init__(self)
  271.  
  272.  
  273. self.txtText = QtGui.QLineEdit()
  274. # activate touch input
  275. self.txtText.keyboard_type = 'default'
  276.  
  277. gl = QtGui.QVBoxLayout()
  278. gl.addWidget(self.txtText)
  279.  
  280. self.setWindowTitle('Touch Input Demo')
  281. self.setLayout(gl)
  282.  
  283.  
  284. def cf_clicked(self):
  285. cel = float(self.inputDialog.text())
  286. fahr = cel * 9 / 5.0 +32
  287. self.outputDialog.setText(str(fahr + 0.5))
  288. SoftInputWidget._input_panel_all.show_input_panel(widget)
  289.  
  290.  
  291. def fc_clicked(self):
  292. fahr = float(self.inputDialog.text())
  293. cel = (fahr-32)*5/9
  294. self.outputDialog.setText(str(cel))
  295.  
  296.  
  297. def exitButton_clicked(self):
  298. app.quit()
  299. self.hide()
  300.  
  301.  
  302.  
  303. if __name__ == '__main__':
  304. app = QtGui.QApplication (sys.argv)
  305. MyWindow = MyWindowClass(None)
  306. MyWindow.showFullScreen()
  307. app.exec_()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement