author | zecke <zecke> | 2004-05-01 18:08:46 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-05-01 18:08:46 (UTC) |
commit | ec6a887d8abddbab085a0a3aecae5760ff85dbe8 (patch) (unidiff) | |
tree | 93f42bc5ebc3a5428dc93af6b7b90392085a551f /libopie2 | |
parent | 8dd2d693000c916346f0bb7d94cbc02b8456c65b (diff) | |
download | opie-ec6a887d8abddbab085a0a3aecae5760ff85dbe8.zip opie-ec6a887d8abddbab085a0a3aecae5760ff85dbe8.tar.gz opie-ec6a887d8abddbab085a0a3aecae5760ff85dbe8.tar.bz2 |
Make the code more appealing to me
Fix small memory leak for the blacklist QMap
-rw-r--r-- | libopie2/opiecore/okeyconfigmanager.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/libopie2/opiecore/okeyconfigmanager.cpp b/libopie2/opiecore/okeyconfigmanager.cpp index 18740ef..ad0f7f7 100644 --- a/libopie2/opiecore/okeyconfigmanager.cpp +++ b/libopie2/opiecore/okeyconfigmanager.cpp | |||
@@ -432,152 +432,155 @@ bool OKeyConfigItem::operator!=( const OKeyConfigItem& conf )const { | |||
432 | * blackList.append(Opie::Core::OKeyPair::leftArrowKey()); | 432 | * blackList.append(Opie::Core::OKeyPair::leftArrowKey()); |
433 | * blackList.append(Opie::Core::OKeyPair::rightArrowKey()); | 433 | * blackList.append(Opie::Core::OKeyPair::rightArrowKey()); |
434 | * Opie::Core::OKeyConfigManager *manager = new Opie::Core::OKeyConfigManager(conf,"key_actions",blackList, | 434 | * Opie::Core::OKeyConfigManager *manager = new Opie::Core::OKeyConfigManager(conf,"key_actions",blackList, |
435 | * false); | 435 | * false); |
436 | * QListView *view = new QListView(); | 436 | * QListView *view = new QListView(); |
437 | * manager->handleWidget(view); | 437 | * manager->handleWidget(view); |
438 | * manager->addKeyConfig( Opie::Core::OKeyPair::returnKey()); | 438 | * manager->addKeyConfig( Opie::Core::OKeyPair::returnKey()); |
439 | * manager->load(); | 439 | * manager->load(); |
440 | * | 440 | * |
441 | * connect(manager,SIGNAL(actionActivated(QWidget*,QKeyEvent*,const Opie::Core::OKeyConfigItem&)), | 441 | * connect(manager,SIGNAL(actionActivated(QWidget*,QKeyEvent*,const Opie::Core::OKeyConfigItem&)), |
442 | * this,SLOT(slotHandleKey(QWidget*,QKeyEvent*,const Opie::Core::OKeyConfigItem&))); | 442 | * this,SLOT(slotHandleKey(QWidget*,QKeyEvent*,const Opie::Core::OKeyConfigItem&))); |
443 | * | 443 | * |
444 | * .... | 444 | * .... |
445 | * | 445 | * |
446 | * void update(){ | 446 | * void update(){ |
447 | * QDialog diag(true); | 447 | * QDialog diag(true); |
448 | * QHBoxLayout *lay = new QHBoxLayout(&diag); | 448 | * QHBoxLayout *lay = new QHBoxLayout(&diag); |
449 | * Opie::Ui::OKeyConfigWidget *wid = new Opie::Ui::OKeyConfigWidget(manager,&diag); | 449 | * Opie::Ui::OKeyConfigWidget *wid = new Opie::Ui::OKeyConfigWidget(manager,&diag); |
450 | * wid->setChangeMode(Opie::Ui::OKeyConfigWidget::Queu); | 450 | * wid->setChangeMode(Opie::Ui::OKeyConfigWidget::Queu); |
451 | * lay->addWidget(wid); | 451 | * lay->addWidget(wid); |
452 | * if(QPEApplication::execDialog( &diag)== QDialog::Accepted){ | 452 | * if(QPEApplication::execDialog( &diag)== QDialog::Accepted){ |
453 | * wid->save(); | 453 | * wid->save(); |
454 | * } | 454 | * } |
455 | * } | 455 | * } |
456 | * | 456 | * |
457 | * .... | 457 | * .... |
458 | * MyListView::keyPressEvent( QKeyEvent* e ){ | 458 | * MyListView::keyPressEvent( QKeyEvent* e ){ |
459 | * Opie::Ui::OKeyConfigItem item = manager->handleKeyEvent(e); | 459 | * Opie::Ui::OKeyConfigItem item = manager->handleKeyEvent(e); |
460 | * if(!item.isEmpty() ){ | 460 | * if(!item.isEmpty() ){ |
461 | * switch(item.id()){ | 461 | * switch(item.id()){ |
462 | * case My_Delete_Key: | 462 | * case My_Delete_Key: |
463 | * break; | 463 | * break; |
464 | * } | 464 | * } |
465 | * } | 465 | * } |
466 | * } | 466 | * } |
467 | * | 467 | * |
468 | * \endcode | 468 | * \endcode |
469 | * | 469 | * |
470 | * @param conf The Config where the KeySetting should be stored | 470 | * @param conf The Config where the KeySetting should be stored |
471 | * @param group The group where the KeySetting will be stored | 471 | * @param group The group where the KeySetting will be stored |
472 | * @param black Which keys shouldn't be allowed to handle | 472 | * @param black Which keys shouldn't be allowed to handle |
473 | * @param grabkeyboard Calls QPEApplication::grabKeyboard to allow handling of DeviceButtons | 473 | * @param grabkeyboard Calls QPEApplication::grabKeyboard to allow handling of DeviceButtons |
474 | * @param par The parent/owner of this manager | 474 | * @param par The parent/owner of this manager |
475 | * @param name The name of this object | 475 | * @param name The name of this object |
476 | */ | 476 | */ |
477 | OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf, | 477 | OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf, |
478 | const QString& group, | 478 | const QString& group, |
479 | const OKeyPair::List& black, | 479 | const OKeyPair::List& black, |
480 | bool grabkeyboard, QObject* par, | 480 | bool grabkeyboard, QObject* par, |
481 | const char* name) | 481 | const char* name) |
482 | : QObject( par, name ), m_conf( conf ), m_group( group ), | 482 | : QObject( par, name ), m_conf( conf ), m_group( group ), |
483 | m_blackKeys( black ), m_grab( grabkeyboard ), m_map( 0 ){ | 483 | m_blackKeys( black ), m_grab( grabkeyboard ), m_map( 0 ){ |
484 | if ( m_grab ) | 484 | if ( m_grab ) |
485 | QPEApplication::grabKeyboard(); | 485 | QPEApplication::grabKeyboard(); |
486 | m_event_mask = OKeyConfigManager::MaskReleased; | 486 | m_event_mask = OKeyConfigManager::MaskReleased; |
487 | } | 487 | } |
488 | 488 | ||
489 | 489 | ||
490 | /** | 490 | /** |
491 | * Destructor | 491 | * Destructor |
492 | */ | 492 | */ |
493 | OKeyConfigManager::~OKeyConfigManager() { | 493 | OKeyConfigManager::~OKeyConfigManager() { |
494 | if ( m_grab ) | 494 | if ( m_grab ) |
495 | QPEApplication::ungrabKeyboard(); | 495 | QPEApplication::ungrabKeyboard(); |
496 | delete m_map; | ||
496 | } | 497 | } |
497 | 498 | ||
498 | /** | 499 | /** |
499 | * Load the Configuration from the OConfig | 500 | * Load the Configuration from the OConfig |
500 | * If a Key is restricted but was in the config we will | 501 | * If a Key is restricted but was in the config we will |
501 | * make it be the empty key paur | 502 | * make it be the empty key paur |
502 | * We will change the group but restore to the previous. | 503 | * We will change the group but restore to the previous. |
503 | * | 504 | * |
504 | * @see OKeyPair::emptyKey | 505 | * @see OKeyPair::emptyKey |
505 | */ | 506 | */ |
506 | void OKeyConfigManager::load() { | 507 | void OKeyConfigManager::load() { |
507 | Opie::Core::OConfigGroupSaver( m_conf, m_group ); | 508 | Opie::Core::OConfigGroupSaver( m_conf, m_group ); |
508 | 509 | ||
509 | /* | 510 | /* |
510 | * Read each item | 511 | * Read each item |
511 | */ | 512 | */ |
512 | int key, mod; | 513 | int key, mod; |
513 | for( OKeyConfigItem::List::Iterator it = m_keys.begin(); it != m_keys.end(); ++it ) { | 514 | for( OKeyConfigItem::List::Iterator it = m_keys.begin(); it != m_keys.end(); ++it ) { |
514 | key = m_conf->readNumEntry( (*it).configKey()+"key", (*it).defaultKeyPair().keycode() ); | 515 | key = m_conf->readNumEntry( (*it).configKey()+"key", |
515 | mod = m_conf->readNumEntry( (*it).configKey()+"mod", (*it).defaultKeyPair().modifier() ); | 516 | (*it).defaultKeyPair().keycode() ); |
517 | mod = m_conf->readNumEntry( (*it).configKey()+"mod", | ||
518 | (*it).defaultKeyPair().modifier() ); | ||
516 | OKeyPair okey( key, mod ); | 519 | OKeyPair okey( key, mod ); |
517 | 520 | ||
518 | if ( !m_blackKeys.contains( okey ) && key != -1 && mod != -1 ) | 521 | if ( !m_blackKeys.contains( okey ) && key != -1 && mod != -1 ) |
519 | (*it).setKeyPair( OKeyPair(key, mod) ); | 522 | (*it).setKeyPair( okey ); |
520 | else | 523 | else |
521 | (*it).setKeyPair( OKeyPair::emptyKey() ); | 524 | (*it).setKeyPair( OKeyPair::emptyKey() ); |
522 | } | 525 | } |
523 | delete m_map; m_map = 0; | 526 | delete m_map; m_map = 0; |
524 | } | 527 | } |
525 | 528 | ||
526 | /** | 529 | /** |
527 | * We will save the current configuration | 530 | * We will save the current configuration |
528 | * to the OConfig. We will change the group but restore | 531 | * to the OConfig. We will change the group but restore |
529 | * to the previous | 532 | * to the previous |
530 | */ | 533 | */ |
531 | void OKeyConfigManager::save() { | 534 | void OKeyConfigManager::save() { |
532 | Opie::Core::OConfigGroupSaver( m_conf, m_group ); | 535 | Opie::Core::OConfigGroupSaver( m_conf, m_group ); |
533 | 536 | ||
534 | /* | 537 | /* |
535 | * Write each item | 538 | * Write each item |
536 | */ | 539 | */ |
537 | for( OKeyConfigItem::List::Iterator it = m_keys.begin();it != m_keys.end(); ++it ) { | 540 | for( OKeyConfigItem::List::Iterator it = m_keys.begin();it != m_keys.end(); ++it ) { |
538 | /* skip empty items */ | 541 | /* skip empty items */ |
539 | if ( (*it).isEmpty() ) | 542 | if ( (*it).isEmpty() ) |
540 | continue; | 543 | continue; |
541 | OKeyPair pair = (*it).keyPair(); | 544 | OKeyPair pair = (*it).keyPair(); |
542 | OKeyPair deft = (*it).defaultKeyPair(); | 545 | OKeyPair deft = (*it).defaultKeyPair(); |
543 | /* | 546 | /* |
544 | * don't write if it is the default setting | 547 | * don't write if it is the default setting |
545 | * FIXME allow to remove Keys from config | 548 | * FIXME allow to remove Keys from config |
546 | if ( (pair.keycode() == deft.keycode()) && | 549 | if ( (pair.keycode() == deft.keycode()) && |
547 | (pair.modifier()== deft.modifier() ) ) | 550 | (pair.modifier()== deft.modifier() ) ) |
548 | return; | 551 | return; |
549 | */ | 552 | */ |
550 | 553 | ||
551 | m_conf->writeEntry((*it).configKey()+"key", pair.keycode() ); | 554 | m_conf->writeEntry((*it).configKey()+"key", pair.keycode() ); |
552 | m_conf->writeEntry((*it).configKey()+"mod", pair.modifier() ); | 555 | m_conf->writeEntry((*it).configKey()+"mod", pair.modifier() ); |
553 | } | 556 | } |
554 | m_conf->write(); | 557 | m_conf->write(); |
555 | } | 558 | } |
556 | 559 | ||
557 | /** | 560 | /** |
558 | * This is function uses a QMap internally but you can have the same keycode | 561 | * This is function uses a QMap internally but you can have the same keycode |
559 | * with different modifier key. The behaviour is undefined if you add a OKeyConfigItem | 562 | * with different modifier key. The behaviour is undefined if you add a OKeyConfigItem |
560 | * with same keycode and modifier key. The GUI takes care that a user can't | 563 | * with same keycode and modifier key. The GUI takes care that a user can't |
561 | * cofigure two keys. | 564 | * cofigure two keys. |
562 | * | 565 | * |
563 | * Make sure you call e->ignore if you don't want to handle this event | 566 | * Make sure you call e->ignore if you don't want to handle this event |
564 | */ | 567 | */ |
565 | OKeyConfigItem OKeyConfigManager::handleKeyEvent( QKeyEvent* e ) { | 568 | OKeyConfigItem OKeyConfigManager::handleKeyEvent( QKeyEvent* e ) { |
566 | /* | 569 | /* |
567 | * Fix Up issues with Qt/E, my keybard, and virtual input | 570 | * Fix Up issues with Qt/E, my keybard, and virtual input |
568 | * methods | 571 | * methods |
569 | * First my Keyboard delivers 256,512,1024 for shift/ctrl/alt instead of the button state | 572 | * First my Keyboard delivers 256,512,1024 for shift/ctrl/alt instead of the button state |
570 | * Also key() on virtual inputmethods are zero and only ascii. We need to fix upper and lower | 573 | * Also key() on virtual inputmethods are zero and only ascii. We need to fix upper and lower |
571 | * case ascii | 574 | * case ascii |
572 | */ | 575 | */ |
573 | int key, mod; | 576 | int key, mod; |
574 | Opie::Core::Internal::fixupKeys( key, mod, e ); | 577 | Opie::Core::Internal::fixupKeys( key, mod, e ); |
575 | 578 | ||
576 | OKeyConfigItem::List _keyList = keyList( key ); | 579 | OKeyConfigItem::List _keyList = keyList( key ); |
577 | if ( _keyList.isEmpty() ) | 580 | if ( _keyList.isEmpty() ) |
578 | return OKeyConfigItem(); | 581 | return OKeyConfigItem(); |
579 | 582 | ||
580 | OKeyConfigItem item; | 583 | OKeyConfigItem item; |
581 | for ( OKeyConfigItem::List::Iterator it = _keyList.begin(); it != _keyList.end(); | 584 | for ( OKeyConfigItem::List::Iterator it = _keyList.begin(); it != _keyList.end(); |
582 | ++it ) { | 585 | ++it ) { |
583 | if ( (*it).keyPair().modifier() == mod ) { | 586 | if ( (*it).keyPair().modifier() == mod ) { |
@@ -624,122 +627,125 @@ void OKeyConfigManager::clearKeyConfig() { | |||
624 | m_keys.clear(); | 627 | m_keys.clear(); |
625 | delete m_map; m_map = 0; | 628 | delete m_map; m_map = 0; |
626 | } | 629 | } |
627 | 630 | ||
628 | /** | 631 | /** |
629 | * | 632 | * |
630 | */ | 633 | */ |
631 | Opie::Core::OKeyConfigItem::List OKeyConfigManager::keyConfigList()const{ | 634 | Opie::Core::OKeyConfigItem::List OKeyConfigManager::keyConfigList()const{ |
632 | return m_keys; | 635 | return m_keys; |
633 | } | 636 | } |
634 | 637 | ||
635 | /** | 638 | /** |
636 | * Add this OKeyPair to the blackList. | 639 | * Add this OKeyPair to the blackList. |
637 | * Internal lists will be destroyed | 640 | * Internal lists will be destroyed |
638 | */ | 641 | */ |
639 | void OKeyConfigManager::addToBlackList( const OKeyPair& key) { | 642 | void OKeyConfigManager::addToBlackList( const OKeyPair& key) { |
640 | m_blackKeys.append( key ); | 643 | m_blackKeys.append( key ); |
641 | delete m_map; m_map = 0; | 644 | delete m_map; m_map = 0; |
642 | } | 645 | } |
643 | 646 | ||
644 | 647 | ||
645 | /** | 648 | /** |
646 | * Remove this OKeyPair from the black List | 649 | * Remove this OKeyPair from the black List |
647 | * Internal lists will be destroyed | 650 | * Internal lists will be destroyed |
648 | */ | 651 | */ |
649 | void OKeyConfigManager::removeFromBlackList( const OKeyPair& key ) { | 652 | void OKeyConfigManager::removeFromBlackList( const OKeyPair& key ) { |
650 | m_blackKeys.remove( key ); | 653 | m_blackKeys.remove( key ); |
651 | delete m_map; m_map = 0; | 654 | delete m_map; m_map = 0; |
652 | } | 655 | } |
653 | 656 | ||
654 | 657 | ||
655 | /** | 658 | /** |
656 | * Clear the blackList | 659 | * Clear the blackList |
657 | */ | 660 | */ |
658 | void OKeyConfigManager::clearBlackList() { | 661 | void OKeyConfigManager::clearBlackList() { |
659 | m_blackKeys.clear(); | 662 | m_blackKeys.clear(); |
660 | delete m_map; m_map = 0; | 663 | delete m_map; m_map = 0; |
661 | } | 664 | } |
662 | 665 | ||
663 | 666 | ||
664 | /** | 667 | /** |
665 | * Return a copy of the blackList | 668 | * Return a copy of the blackList |
666 | */ | 669 | */ |
667 | OKeyPair::List OKeyConfigManager::blackList()const { | 670 | OKeyPair::List OKeyConfigManager::blackList()const { |
668 | return m_blackKeys; | 671 | return m_blackKeys; |
669 | } | 672 | } |
670 | 673 | ||
671 | 674 | ||
672 | /** | 675 | /** |
673 | * Ask the Manager to handle KeyEvents for you. | 676 | * Ask the Manager to handle KeyEvents for you. |
674 | * All handled keys will emit a QSignal and return true | 677 | * All handled keys will emit a QSignal and return true |
675 | * that it handled the keyevent | 678 | * that it handled the keyevent |
676 | */ | 679 | */ |
677 | void OKeyConfigManager::handleWidget( QWidget* wid ) { | 680 | void OKeyConfigManager::handleWidget( QWidget* wid ) { |
678 | wid->installEventFilter( this ); | 681 | wid->installEventFilter( this ); |
679 | } | 682 | } |
680 | 683 | ||
681 | /** | 684 | /** |
682 | * @internal | 685 | * @internal |
683 | */ | 686 | */ |
684 | bool OKeyConfigManager::eventFilter( QObject* obj, QEvent* ev) { | 687 | bool OKeyConfigManager::eventFilter( QObject* obj, QEvent* ev) { |
685 | if ( !obj->isWidgetType() ) | 688 | if ( !obj->isWidgetType() ) |
686 | return false; | 689 | return false; |
687 | 690 | ||
691 | /* | ||
692 | * check if we care for the event | ||
693 | */ | ||
688 | if ( (ev->type() != QEvent::KeyPress||!testEventMask(MaskPressed)) && | 694 | if ( (ev->type() != QEvent::KeyPress||!testEventMask(MaskPressed)) && |
689 | (ev->type() != QEvent::KeyRelease||!testEventMask(MaskReleased)) ) | 695 | (ev->type() != QEvent::KeyRelease||!testEventMask(MaskReleased)) ) |
690 | return false; | 696 | return false; |
691 | 697 | ||
692 | QKeyEvent *key = static_cast<QKeyEvent*>( ev ); | 698 | QKeyEvent *key = static_cast<QKeyEvent*>( ev ); |
693 | OKeyConfigItem item = handleKeyEvent( key ); | 699 | OKeyConfigItem item = handleKeyEvent( key ); |
694 | 700 | ||
695 | if ( item.isEmpty() ) | 701 | if ( item.isEmpty() ) |
696 | return false; | 702 | return false; |
697 | 703 | ||
698 | QWidget *wid = static_cast<QWidget*>( obj ); | 704 | QWidget *wid = static_cast<QWidget*>( obj ); |
699 | 705 | ||
700 | if ( item.object() && !item.slot().isEmpty() ) { | 706 | if ( item.object() && !item.slot().isEmpty() ) { |
701 | connect( this, SIGNAL( actionActivated(QWidget*, QKeyEvent*)), | 707 | connect( this, SIGNAL( actionActivated(QWidget*, QKeyEvent*)), |
702 | item.object(), item.slot().data() ); | 708 | item.object(), item.slot().data() ); |
703 | emit actionActivated(wid, key); | 709 | emit actionActivated(wid, key); |
704 | disconnect( this, SIGNAL(actionActivated(QWidget*,QKeyEvent*)), | 710 | disconnect( this, SIGNAL(actionActivated(QWidget*,QKeyEvent*)), |
705 | item.object(), item.slot().data() ); | 711 | item.object(), item.slot().data() ); |
706 | } | 712 | } |
707 | emit actionActivated( wid, key, item ); | 713 | emit actionActivated( wid, key, item ); |
708 | 714 | ||
709 | return true; | 715 | return true; |
710 | } | 716 | } |
711 | 717 | ||
712 | /** | 718 | /** |
713 | * @internal | 719 | * @internal |
714 | */ | 720 | */ |
715 | OKeyConfigItem::List OKeyConfigManager::keyList( int keycode) { | 721 | OKeyConfigItem::List OKeyConfigManager::keyList( int keycode) { |
716 | /* | 722 | /* |
717 | * Create the map if not existing anymore | 723 | * Create the map if not existing anymore |
718 | */ | 724 | */ |
719 | if ( !m_map ) { | 725 | if ( !m_map ) { |
720 | m_map = new OKeyMapConfigPrivate; | 726 | m_map = new OKeyMapConfigPrivate; |
721 | /* for every key */ | 727 | /* for every key */ |
722 | for ( OKeyConfigItem::List::Iterator it = m_keys.begin(); | 728 | for ( OKeyConfigItem::List::Iterator it = m_keys.begin(); |
723 | it!= m_keys.end(); ++it ) { | 729 | it!= m_keys.end(); ++it ) { |
724 | 730 | ||
725 | bool add = true; | 731 | bool add = true; |
726 | /* see if this key is blocked */ | 732 | /* see if this key is blocked */ |
727 | OKeyPair pair = (*it).keyPair(); | 733 | OKeyPair pair = (*it).keyPair(); |
728 | for ( OKeyPair::List::Iterator pairIt = m_blackKeys.begin(); | 734 | for ( OKeyPair::List::Iterator pairIt = m_blackKeys.begin(); |
729 | pairIt != m_blackKeys.end(); ++pairIt ) { | 735 | pairIt != m_blackKeys.end(); ++pairIt ) { |
730 | if ( (*pairIt).keycode() == pair.keycode() && | 736 | if ( (*pairIt).keycode() == pair.keycode() && |
731 | (*pairIt).modifier() == pair.modifier() ) { | 737 | (*pairIt).modifier() == pair.modifier() ) { |
732 | add = false; | 738 | add = false; |
733 | break; | 739 | break; |
734 | } | 740 | } |
735 | } | 741 | } |
736 | /* check if we added it */ | 742 | /* check if we added it */ |
737 | if ( add ) | 743 | if ( add ) |
738 | (*m_map)[pair.keycode()].append( *it ); | 744 | (*m_map)[pair.keycode()].append( *it ); |
739 | } | 745 | } |
740 | } | 746 | } |
741 | return (*m_map)[keycode]; | 747 | return (*m_map)[keycode]; |
742 | } | 748 | } |
743 | 749 | ||
744 | } | 750 | } |
745 | } | 751 | } |