00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 from PyQt4.QtCore import *
00020 from PyQt4.QtGui import *
00021 
00022 
00023 
00024 class AbstractKeyboardWidget(QWidget):
00025         
00026         
00027         def __init__(self, parent):
00028                 QWidget.__init__(self, parent)
00029 
00030         
00031         
00032         def init(self):
00033                 self.connect( self.pushEscape, SIGNAL('clicked()'), self.escape )
00034                 if hasattr(self, 'pushCaps'):
00035                         self.connect( self.pushCaps, SIGNAL('clicked()'), self.caps )
00036                 else:
00037                         self.pushCaps = None
00038                 buttons = self.findChildren( QPushButton )
00039                 for button in buttons:
00040                         if button in (self.pushCaps, self.pushEscape):
00041                                 continue
00042                         self.connect( button, SIGNAL('clicked()'), self.clicked )
00043 
00044                 self.setWindowFlags( Qt.Popup )
00045                 self.setWindowModality( Qt.ApplicationModal )
00046                 self.setFocusPolicy( Qt.NoFocus )
00047                 all = self.findChildren( QWidget )
00048                 for widget in all:
00049                         widget.setFocusPolicy( Qt.NoFocus )
00050                 self.fitInScreen()
00051                 if self.pushCaps:
00052                         self.caps()
00053                 self.show()
00054 
00055         
00056         def fitInScreen(self):
00057                 parent = self.parent()
00058                 parentPos = parent.parent().mapToGlobal( parent.pos() )
00059                 screenHeight = QApplication.desktop().screenGeometry().height()
00060                 screenWidth = QApplication.desktop().screenGeometry().width()
00061                 
00062                 y = parentPos.y() + parent.height()
00063                 if y + self.height() > screenHeight:
00064                         y = parentPos.y() - self.height()
00065                         if y < 0:
00066                                 y = screenHeight - self.height()
00067                 
00068                 x = parentPos.x() + parent.width() / 2 - self.width() / 2
00069                 if x < 0:
00070                         x = 0
00071                 elif x + self.width() > screenWidth:
00072                         x = screenWidth - self.width()
00073                 self.move( x, y )
00074                 
00075         def clicked(self):
00076                 button = self.sender()
00077                 
00078                 
00079                 key = self.key( unicode( button.objectName() ) )
00080                 if not key:
00081                         print 'No key assigned to button "%s"' % unicode( button.text() )
00082                         return
00083                 if key == Qt.Key_Space:
00084                         text = ' '
00085                 else:
00086                         text = button.text()
00087                 if key == Qt.Key_Tab:
00088                         event = QKeyEvent( QEvent.KeyPress, key, Qt.NoModifier, text )
00089                         QApplication.sendEvent( self.parent(), event )
00090                         self.emit(SIGNAL('tabKeyPressed'))
00091                         return
00092                 event = QKeyEvent( QEvent.KeyPress, key, Qt.NoModifier, text )
00093                 QApplication.sendEvent( self.parent(), event )
00094         
00095         
00096         def key(self, text):
00097                 return eval( 'Qt.%s' % text )
00098 
00099         
00100         def escape(self):
00101                 self.hide()
00102 
00103         def caps(self):
00104                 buttons = self.findChildren( QPushButton )
00105                 for button in buttons:
00106                         if button.text().count() == 1:
00107                                 if self.pushCaps.isChecked():
00108                                         button.setText( button.text().toUpper() )
00109                                 else:
00110                                         button.setText( button.text().toLower() )
00111