HistoryField.cpp

Go to the documentation of this file.
00001 #include "HistoryField.h"
00002 
00003 HistoryField::HistoryField( QWidget* parent, const char* name = 0 ) : QComboBox( true, parent, name ), digraphEnabled( false ) {
00004     setDuplicatesEnabled( false );
00005     setInsertionPolicy( QComboBox::AtBottom );
00006     setAutoCompletion( true );
00007     installEventFilter( this );
00008 }
00009 
00010 bool HistoryField::eventFilter( QObject* obj, QEvent* event ) {
00011     if( event->type() == 6 ) {
00012         // The following code should be located in keyPressEvent() (like for DigraphLineEdit).
00013         // However, there seems to have a bug in the implementation of QComboBox where keyPressEvent()
00014         // is always called twice when we hit a key.  Placing the code here fixes that.
00015         QKeyEvent* keyEvent = (QKeyEvent*)event;
00016         if( digraphEnabled ) {
00017             // We consider only printable keys.  Control keys are processed normally.
00018             // I'm not sure if this test covers all the cases though.
00019             if( keyEvent->count() > 0 ) {
00020                 if( keyEvent->key() == Key_Backspace ) {
00021                     if( lineEdit()->cursorPosition() > 0 ) {
00022                         if( buffer.isNull() ) {
00023                             QString charToDelete( lineEdit()->text().mid( lineEdit()->cursorPosition() - 1, 1 ) );
00024                             buffer = charToDelete;
00025                         }
00026                         else 
00027                             buffer = QString::null;
00028                     }
00029                 }
00030                 else {
00031                     if( !buffer.isNull() ) {
00032                         buffer += keyEvent->text();
00033                         const QString newChar( Util::getDigraph( buffer ) );
00034                         if( newChar == QString::null )
00035                             buffer = QString::null;
00036                         else {
00037                             QKeyEvent* digraphEvent = new QKeyEvent( QEvent::KeyPress, 0, 0, 0, newChar, keyEvent->isAutoRepeat(), 0 );
00038                             qApp->sendEvent( lineEdit(), digraphEvent );
00039                             buffer = QString::null;
00040                             return( true );
00041                         }
00042                     }
00043                 }
00044             }
00045         }
00046     }
00047     return( QComboBox::eventFilter( obj, event ) );
00048 }
00049 
00050 void HistoryField::setDigraphEnabled( bool isEnabled ) {
00051     this->digraphEnabled = isEnabled;
00052 }
00053 
00054 bool HistoryField::isDigraphEnabled() const {
00055     return( digraphEnabled );
00056 }
00057 

Generated on Sun Mar 1 17:30:47 2009 for toMOTko by  doxygen 1.5.6