Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

okeyconfigmanager.cpp

Go to the documentation of this file.
00001 #include "okeyconfigmanager.h"
00002 
00003 #include "okeyconfigmanager_p.h"
00004 
00005 namespace Opie {
00006 namespace Core {
00007 namespace Internal {
00008     /*
00009      * the virtual and hardware key events have both issues...
00010      */
00011     void fixupKeys( int& key, int &mod, QKeyEvent* e ) {
00012         key = e->key();
00013         mod = e->state();
00014        /*
00015         * virtual keyboard
00016         * else change the button mod only
00017         */
00018         if ( key == 0 ) {
00019             key = e->ascii();
00020             if (  key > 96 && key < 123)
00021                 key -=  32;
00022         }else{
00023             int new_mod = 0;
00024             if ( mod & 256 )
00025                 new_mod |= Qt::ShiftButton;
00026             else if ( mod & 512 )
00027                 new_mod |= Qt::ControlButton;
00028             else if ( mod & 1024 )
00029                 new_mod |= Qt::AltButton;
00030 
00031             mod = new_mod == 0? mod : new_mod;
00032         }
00033     }
00034 }
00035 
00047 OKeyPair::OKeyPair( int key, int mod )
00048     : m_key( key ), m_mod( mod )
00049 {}
00050 
00054 OKeyPair::~OKeyPair() {}
00055 
00056 
00060 bool OKeyPair::isEmpty()const {
00061     return ( ( m_key == -1 )&& ( m_mod == -1 ) );
00062 }
00063 
00070 int OKeyPair::keycode()const {
00071     return m_key;
00072 }
00073 
00081 int OKeyPair::modifier()const {
00082     return m_mod;
00083 }
00084 
00085 
00093 void OKeyPair::setKeycode( int key ) {
00094     m_key = key;
00095 }
00096 
00104 void OKeyPair::setModifier( int mod ) {
00105     m_mod = mod;
00106 }
00107 
00111 OKeyPair OKeyPair::returnKey() {
00112     return OKeyPair( Qt::Key_Return, 0 );
00113 }
00114 
00119 OKeyPair OKeyPair::leftArrowKey() {
00120     return OKeyPair( Qt::Key_Left, 0 );
00121 }
00122 
00127 OKeyPair OKeyPair::rightArrowKey() {
00128     return OKeyPair( Qt::Key_Right, 0 );
00129 }
00130 
00135 OKeyPair OKeyPair::upArrowKey() {
00136     return OKeyPair( Qt::Key_Up, 0 );
00137 }
00138 
00143 OKeyPair OKeyPair::downArrowKey() {
00144     return OKeyPair( Qt::Key_Down, 0 );
00145 }
00146 
00150 OKeyPair OKeyPair::emptyKey() {
00151     return OKeyPair();
00152 }
00153 
00162 OKeyPair::List OKeyPair::hardwareKeys() {
00163     const QValueList<Opie::Core::ODeviceButton> but = Opie::Core::ODevice::inst()->buttons();
00164     OKeyPair::List lst;
00165 
00166     for ( QValueList<Opie::Core::ODeviceButton>::ConstIterator it = but.begin();
00167           it != but.end(); ++it )
00168         lst.append( OKeyPair( (*it).keycode(), 0 ) );
00169 
00170 
00171     return lst;
00172 }
00173 
00178 bool OKeyPair::operator==( const OKeyPair& pair)const {
00179     if ( m_key != pair.m_key ) return false;
00180     if ( m_mod != pair.m_mod ) return false;
00181 
00182     return true;
00183 }
00184 
00188 bool OKeyPair::operator!=( const OKeyPair& pair)const  {
00189     return !(*this == pair);
00190 }
00191 
00192 
00220 OKeyConfigItem::OKeyConfigItem( const QString& text, const QCString& config_key,
00221                                 const QPixmap& pix, int id, const OKeyPair& def,
00222                                 QObject *caller,
00223                                 const char* slot )
00224     : m_text( text ), m_config( config_key ), m_pix( pix ),
00225       m_id( id ), m_def( def ),
00226       m_obj( caller ), m_str( slot ) {}
00227 
00238 OKeyConfigItem::OKeyConfigItem( const Opie::Core::ODeviceButton& b )
00239     : m_text( b.userText() ), m_pix( b.pixmap() ), m_id( -1 ),
00240       m_key( OKeyPair( b.keycode(), 0 ) ), m_def( OKeyPair( b.keycode(), 0 ) )
00241 {}
00242 
00243 
00247 OKeyConfigItem::~OKeyConfigItem() {}
00248 
00249 
00255 QString OKeyConfigItem::text()const {
00256     return m_text;
00257 }
00258 
00264 QPixmap OKeyConfigItem::pixmap()const {
00265     return m_pix;
00266 }
00267 
00273 OKeyPair OKeyConfigItem::keyPair()const {
00274     return m_key;
00275 }
00276 
00281 OKeyPair OKeyConfigItem::defaultKeyPair()const {
00282     return m_def;
00283 }
00284 
00285 
00290 int OKeyConfigItem::id()const{
00291     return m_id;
00292 }
00293 
00298 QCString OKeyConfigItem::configKey()const {
00299     return m_config;
00300 }
00301 
00305 QObject* OKeyConfigItem::object()const{
00306     return m_obj;
00307 }
00308 
00312 QCString OKeyConfigItem::slot()const {
00313     return m_str;
00314 }
00315 
00322 void OKeyConfigItem::setText( const QString& text ) {
00323     m_text = text;
00324 }
00325 
00332 void OKeyConfigItem::setPixmap( const QPixmap& pix ) {
00333     m_pix = pix;
00334 }
00335 
00344 void OKeyConfigItem::setKeyPair( const OKeyPair& key) {
00345     m_key = key;
00346 }
00347 
00354 void OKeyConfigItem::setDefaultKeyPair( const OKeyPair& key ) {
00355     m_def = key;
00356 }
00357 
00361 void OKeyConfigItem::setConfigKey( const QCString& str) {
00362     m_config = str;
00363     m_config.detach();
00364 }
00365 
00369 void OKeyConfigItem::setId( int id ) {
00370     m_id = id;
00371 }
00372 
00378 bool OKeyConfigItem::isEmpty()const {
00379     if ( !m_def.isEmpty()  ) return false;
00380     if ( !m_key.isEmpty()  ) return false;
00381     if ( !m_text.isEmpty() ) return false;
00382     if ( m_id != -1       ) return false;
00383 
00384     return true;
00385 }
00386 
00390 bool OKeyConfigItem::operator==( const OKeyConfigItem& conf )const {
00391 /*    if ( isEmpty() == conf.isEmpty() ) return true;
00392     else if ( isEmpty() != conf.isEmpty() ) return false;
00393     else if ( !isEmpty()!= conf.isEmpty() ) return false;
00394 */
00395 
00396     if ( m_id != conf.m_id )     return false;
00397     if ( m_obj != conf.m_obj )   return false;
00398     if ( m_text != conf.m_text ) return false;
00399     if ( m_key != conf.m_key )   return false;
00400     if ( m_def != conf.m_def )   return false;
00401 
00402 
00403 
00404     return true;
00405 
00406 }
00407 
00408 bool OKeyConfigItem::operator!=( const OKeyConfigItem& conf )const {
00409     return !( *this == conf );
00410 }
00411 
00477 OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf,
00478                                       const QString& group,
00479                                       const OKeyPair::List& black,
00480                                       bool grabkeyboard, QObject* par,
00481                                       const char* name)
00482     : QObject( par, name ), m_conf( conf ), m_group( group ),
00483       m_blackKeys( black ), m_grab( grabkeyboard ), m_map( 0 ){
00484     if ( m_grab )
00485         QPEApplication::grabKeyboard();
00486     m_event_mask = OKeyConfigManager::MaskReleased;
00487 }
00488 
00489 
00493 OKeyConfigManager::~OKeyConfigManager() {
00494     if ( m_grab )
00495         QPEApplication::ungrabKeyboard();
00496     delete m_map;
00497 }
00498 
00507 void OKeyConfigManager::load() {
00508     Opie::Core::OConfigGroupSaver grp( m_conf, m_group );
00509 
00510     /*
00511      * Read each item
00512      */
00513     int key, mod;
00514     for( OKeyConfigItem::List::Iterator it = m_keys.begin(); it != m_keys.end(); ++it ) {
00515         key = m_conf->readNumEntry( (*it).configKey()+"key",
00516                                     (*it).defaultKeyPair().keycode()  );
00517         mod = m_conf->readNumEntry( (*it).configKey()+"mod",
00518                                     (*it).defaultKeyPair().modifier() );
00519         OKeyPair okey( key, mod );
00520 
00521         if (  !m_blackKeys.contains( okey ) && key != -1  && mod != -1 )
00522             (*it).setKeyPair( okey );
00523         else
00524             (*it).setKeyPair( OKeyPair::emptyKey() );
00525     }
00526     delete m_map; m_map = 0;
00527 }
00528 
00534 void OKeyConfigManager::save() {
00535     Opie::Core::OConfigGroupSaver grp( m_conf, m_group );
00536 
00537     /*
00538      * Write each item
00539      */
00540     for( OKeyConfigItem::List::Iterator it = m_keys.begin();it != m_keys.end(); ++it ) {
00541         /* skip empty items */
00542         if ( (*it).isEmpty() )
00543             continue;
00544         OKeyPair pair = (*it).keyPair();
00545         OKeyPair deft = (*it).defaultKeyPair();
00546         /*
00547          * don't write if it is the default setting
00548          * FIXME allow to remove Keys from config
00549         if (  (pair.keycode() == deft.keycode()) &&
00550               (pair.modifier()== deft.modifier() ) )
00551             return;
00552         */
00553 
00554         m_conf->writeEntry((*it).configKey()+"key", pair.keycode()  );
00555         m_conf->writeEntry((*it).configKey()+"mod", pair.modifier() );
00556     }
00557     m_conf->write();
00558 }
00559 
00568 OKeyConfigItem OKeyConfigManager::handleKeyEvent( QKeyEvent* e ) {
00569    /*
00570     * Fix Up issues with Qt/E, my keybard, and virtual input
00571     * methods
00572     * First my Keyboard delivers 256,512,1024 for shift/ctrl/alt instead of the button state
00573     * Also key() on virtual inputmethods are zero and only ascii. We need to fix upper and lower
00574     * case ascii
00575     */
00576     int key, mod;
00577     Opie::Core::Internal::fixupKeys( key, mod, e );
00578 
00579     OKeyConfigItem::List _keyList =  keyList( key );
00580     if ( _keyList.isEmpty() )
00581         return OKeyConfigItem();
00582 
00583     OKeyConfigItem item;
00584     for ( OKeyConfigItem::List::Iterator it = _keyList.begin(); it != _keyList.end();
00585           ++it ) {
00586         if ( (*it).keyPair().modifier() == mod ) {
00587             item = *it;
00588             break;
00589         }
00590 
00591     }
00592 
00593     return item;
00594 }
00595 
00602 int OKeyConfigManager::handleKeyEventId( QKeyEvent* ev) {
00603     return handleKeyEvent( ev ).id();
00604 }
00605 
00609 void OKeyConfigManager::addKeyConfig( const OKeyConfigItem& item ) {
00610     m_keys.append( item );
00611     delete m_map; m_map = 0;
00612 }
00613 
00618 void OKeyConfigManager::removeKeyConfig( const OKeyConfigItem& item ) {
00619     m_keys.remove( item );
00620     delete m_map; m_map = 0;
00621 }
00622 
00626 void OKeyConfigManager::clearKeyConfig() {
00627     m_keys.clear();
00628     delete m_map; m_map = 0;
00629 }
00630 
00634 Opie::Core::OKeyConfigItem::List OKeyConfigManager::keyConfigList()const{
00635     return m_keys;
00636 }
00637 
00645 void OKeyConfigManager::addToBlackList( const OKeyPair& key) {
00646     m_blackKeys.append( key );
00647     delete m_map; m_map = 0;
00648 }
00649 
00650 
00658 void OKeyConfigManager::removeFromBlackList( const OKeyPair& key ) {
00659     m_blackKeys.remove( key );
00660     delete m_map; m_map = 0;
00661 }
00662 
00663 
00667 void OKeyConfigManager::clearBlackList() {
00668     m_blackKeys.clear();
00669     delete m_map; m_map = 0;
00670 }
00671 
00672 
00680 OKeyPair::List OKeyConfigManager::blackList()const {
00681     return m_blackKeys;
00682 }
00683 
00684 
00690 void OKeyConfigManager::handleWidget( QWidget* wid ) {
00691     wid->installEventFilter( this );
00692 }
00693 
00697 bool OKeyConfigManager::eventFilter( QObject* obj, QEvent* ev) {
00698     if ( !obj->isWidgetType() )
00699         return false;
00700 
00701     /*
00702      * check  if we care for the event
00703      */
00704     if ( (ev->type() != QEvent::KeyPress||!testEventMask(MaskPressed)) &&
00705         (ev->type() != QEvent::KeyRelease||!testEventMask(MaskReleased)) )
00706         return false;
00707 
00708     QKeyEvent *key = static_cast<QKeyEvent*>( ev );
00709     OKeyConfigItem item =  handleKeyEvent( key );
00710 
00711     if ( item.isEmpty() )
00712         return false;
00713 
00714     QWidget *wid = static_cast<QWidget*>( obj );
00715 
00716     if ( item.object() && !item.slot().isEmpty() ) {
00717         connect( this, SIGNAL( actionActivated(QWidget*, QKeyEvent*)),
00718                  item.object(), item.slot().data() );
00719         emit actionActivated(wid, key);
00720         disconnect( this, SIGNAL(actionActivated(QWidget*,QKeyEvent*)),
00721                     item.object(), item.slot().data() );
00722     }
00723     emit actionActivated( wid, key, item );
00724 
00725     return true;
00726 }
00727 
00731 OKeyConfigItem::List OKeyConfigManager::keyList( int keycode) {
00732    /*
00733     * Create the map if not existing anymore
00734     */
00735     if ( !m_map ) {
00736         m_map = new OKeyMapConfigPrivate;
00737         /* for every key */
00738         for ( OKeyConfigItem::List::Iterator it = m_keys.begin();
00739               it!= m_keys.end(); ++it ) {
00740 
00741             bool add = true;
00742             /* see if this key is blocked */
00743             OKeyPair pair = (*it).keyPair();
00744             for ( OKeyPair::List::Iterator pairIt = m_blackKeys.begin();
00745                   pairIt != m_blackKeys.end(); ++pairIt ) {
00746                 if ( (*pairIt).keycode()  == pair.keycode() &&
00747                      (*pairIt).modifier() == pair.modifier() ) {
00748                     add = false;
00749                     break;
00750                 }
00751             }
00752             /* check if we added it */
00753             if ( add )
00754                 (*m_map)[pair.keycode()].append( *it );
00755         }
00756     }
00757     return (*m_map)[keycode];
00758 }
00759 
00760 }
00761 }

Generated on Sat Nov 5 16:16:08 2005 for OPIE by  doxygen 1.4.2