-rw-r--r-- | libopie2/opieui/okeyconfigwidget.cpp | 309 | ||||
-rw-r--r-- | libopie2/opieui/okeyconfigwidget.h | 10 |
2 files changed, 310 insertions, 9 deletions
diff --git a/libopie2/opieui/okeyconfigwidget.cpp b/libopie2/opieui/okeyconfigwidget.cpp index d52a55f..4482754 100644 --- a/libopie2/opieui/okeyconfigwidget.cpp +++ b/libopie2/opieui/okeyconfigwidget.cpp | |||
@@ -46,9 +46,9 @@ int OKeyPair::keycode()const { | |||
46 | 46 | ||
47 | /** | 47 | /** |
48 | * get the modifier key for this OKeyPair. The Modifier State relates | 48 | * get the modifier key for this OKeyPair. The Modifier State relates |
49 | * to the Qt::Modifier | 49 | * to the Qt::ButtonState |
50 | * | 50 | * |
51 | * @see Qt::Modifier | 51 | * @see Qt::ButtonState |
52 | * @see setModifier | 52 | * @see setModifier |
53 | */ | 53 | */ |
54 | int OKeyPair::modifier()const { | 54 | int OKeyPair::modifier()const { |
@@ -71,7 +71,7 @@ void OKeyPair::setKeycode( int key ) { | |||
71 | * Set the modifier key | 71 | * Set the modifier key |
72 | * | 72 | * |
73 | * @param the Modifier key | 73 | * @param the Modifier key |
74 | * @see Qt::Modifier | 74 | * @see Qt::ButtonState |
75 | * @see modifier() | 75 | * @see modifier() |
76 | */ | 76 | */ |
77 | void OKeyPair::setModifier( int mod ) { | 77 | void OKeyPair::setModifier( int mod ) { |
@@ -145,6 +145,25 @@ OKeyPairList OKeyPair::hardwareKeys() { | |||
145 | } | 145 | } |
146 | 146 | ||
147 | /** | 147 | /** |
148 | * Equals operator. Check if two OKeyPairs have the same key and modifier | ||
149 | * @see operator!= | ||
150 | */ | ||
151 | bool OKeyPair::operator==( const OKeyPair& pair) { | ||
152 | if ( m_key != pair.m_key ) return false; | ||
153 | if ( m_mod != pair.m_mod ) return false; | ||
154 | |||
155 | return true; | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Not equal operator. calls the equal operator internally | ||
160 | */ | ||
161 | bool OKeyPair::operator!=( const OKeyPair& pair) { | ||
162 | return !(*this == pair); | ||
163 | } | ||
164 | |||
165 | |||
166 | /** | ||
148 | * The normal Constructor to create a OKeyConfigItem | 167 | * The normal Constructor to create a OKeyConfigItem |
149 | * | 168 | * |
150 | * You can set the the key paramater of this item but if | 169 | * You can set the the key paramater of this item but if |
@@ -309,6 +328,28 @@ bool OKeyConfigItem::isEmpty()const { | |||
309 | } | 328 | } |
310 | 329 | ||
311 | /** | 330 | /** |
331 | * Check if the KeyPairs are the same | ||
332 | */ | ||
333 | bool OKeyConfigItem::operator==( const OKeyConfigItem& conf ) { | ||
334 | if ( isEmpty() == conf.isEmpty() ) return true; | ||
335 | else if ( isEmpty() != conf.isEmpty() ) return false; | ||
336 | else if ( !isEmpty()!= conf.isEmpty() ) return false; | ||
337 | |||
338 | if ( m_id != conf.m_id ) return false; | ||
339 | if ( m_text != conf.m_text ) return false; | ||
340 | if ( m_pix != conf.m_pix ) return false; | ||
341 | if ( m_key != conf.m_key ) return false; | ||
342 | if ( m_def != conf.m_def ) return false; | ||
343 | |||
344 | return true; | ||
345 | |||
346 | } | ||
347 | |||
348 | bool OKeyConfigItem::operator!=( const OKeyConfigItem& conf ) { | ||
349 | return !( *this == conf ); | ||
350 | } | ||
351 | |||
352 | /** | ||
312 | * \brief c'tor | 353 | * \brief c'tor |
313 | * The Constructor for a OKeyConfigManager | 354 | * The Constructor for a OKeyConfigManager |
314 | * | 355 | * |
@@ -369,20 +410,22 @@ OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf, | |||
369 | bool grabkeyboard, QObject* par, | 410 | bool grabkeyboard, QObject* par, |
370 | const char* name) | 411 | const char* name) |
371 | : QObject( par, name ), m_conf( conf ), m_group( group ), | 412 | : QObject( par, name ), m_conf( conf ), m_group( group ), |
372 | m_blackKeys( black ), m_grab( grabkeyboard ) | 413 | m_blackKeys( black ), m_grab( grabkeyboard ), m_map( 0 ) |
373 | {} | 414 | {} |
374 | 415 | ||
375 | 416 | ||
376 | /** | 417 | /** |
377 | * Destructor | 418 | * Destructor |
378 | */ | 419 | */ |
379 | OKeyConfigWidget::~OKeyConfigWidget() {} | 420 | OKeyConfigManager::~OKeyConfigManager() {} |
380 | 421 | ||
381 | /** | 422 | /** |
382 | * Load the Configuration from the OConfig | 423 | * Load the Configuration from the OConfig |
383 | * If a Key is restricted but was in the config we will | 424 | * If a Key is restricted but was in the config we will |
384 | * make the empty | 425 | * make it be the empty key paur |
385 | * We will change the group of the OConfig Item! | 426 | * We will change the group of the OConfig Item! |
427 | * | ||
428 | * @see OKeyPair::emptyKey | ||
386 | */ | 429 | */ |
387 | void OKeyConfigWidget::load() { | 430 | void OKeyConfigWidget::load() { |
388 | m_conf->setGroup( m_group ); | 431 | m_conf->setGroup( m_group ); |
@@ -395,8 +438,13 @@ void OKeyConfigWidget::load() { | |||
395 | it != m_keys.end(); ++it ) { | 438 | it != m_keys.end(); ++it ) { |
396 | key = m_conf->readNumEntry( (*it).configKey()+"key", (*it).defaultKeyPair().keycode() ); | 439 | key = m_conf->readNumEntry( (*it).configKey()+"key", (*it).defaultKeyPair().keycode() ); |
397 | mod = m_conf->readNumEntry( (*it).configKey()+"mod", (*it).defaultKeyPair().modifier() ); | 440 | mod = m_conf->readNumEntry( (*it).configKey()+"mod", (*it).defaultKeyPair().modifier() ); |
441 | OKeyPair okey( key, mod ); | ||
442 | if ( !m_blackKeys.contains( okey ) && key != -1 && mod != -1 ) | ||
398 | (*it).setKeyPair( OKeyPair(key, mod) ); | 443 | (*it).setKeyPair( OKeyPair(key, mod) ); |
444 | else | ||
445 | (*it).setKeyPair( OKeyPair::emptyKey() ); | ||
399 | } | 446 | } |
447 | delete m_map; m_map = 0; | ||
400 | } | 448 | } |
401 | 449 | ||
402 | /** | 450 | /** |
@@ -404,5 +452,254 @@ void OKeyConfigWidget::load() { | |||
404 | * to the OConfig. We will change the group. | 452 | * to the OConfig. We will change the group. |
405 | */ | 453 | */ |
406 | void OKeyConfigWidget::save() { | 454 | void OKeyConfigWidget::save() { |
455 | m_conf->setGroup( m_group ); | ||
456 | |||
457 | /* | ||
458 | * Write each item | ||
459 | */ | ||
460 | int key, mod; | ||
461 | for( OKeyConfigItemList::Iterator it = m_keys.begin(); | ||
462 | it != m_keys.end(); ++it ) { | ||
463 | if ( (*it).isEmpty() ) | ||
464 | continue; | ||
465 | OKeyPair pair = (*it).keyPair(); | ||
466 | m_conf->writeEntry(pair.configKey()+"key", pair.keycode() ); | ||
467 | m_conf->writeEntry(pair.configKey()+"mod", pair.modifier() ); | ||
468 | } | ||
469 | } | ||
470 | |||
471 | /** | ||
472 | * This is function uses a QMap internally but you can have the same keycode | ||
473 | * with different modifier key. The behaviour is undefined if you add a OKeyConfigItem | ||
474 | * with same keycode and modifier key. The GUI takes care that a user can't | ||
475 | * cofigure two keys. | ||
476 | * | ||
477 | * Make sure you call e->ignore if you don't want to handle this event | ||
478 | */ | ||
479 | OKeyConfigItem OKeyConfigManager::handleKeyEvent( QKeyEvent* e ) { | ||
480 | OKeyConfigItemList keyList = keyList( e->key() ); | ||
481 | if ( keyList.isEmpty() ) | ||
482 | return OKeyConfigItem(); | ||
483 | |||
484 | OKeyConfigItem item; | ||
485 | for ( OKeyConfigItemList::Iterator it = keyList.begin(); it != keyList.end(); | ||
486 | ++it ) { | ||
487 | if ( (*it).keyPair().modifier() == e->state() ) { | ||
488 | item = *it; | ||
489 | break; | ||
490 | } | ||
491 | |||
492 | } | ||
493 | |||
494 | return item; | ||
495 | } | ||
496 | |||
497 | /** | ||
498 | * Return the associated id of the item or -1 if no item | ||
499 | * matched the key | ||
500 | * | ||
501 | * @see handleKeyEvent | ||
502 | */ | ||
503 | int OKeyConfigManager::handleKeyEventId( QKeyEvent* ev) { | ||
504 | return handleKeyEvent( ev ).id(); | ||
505 | } | ||
506 | |||
507 | /** | ||
508 | * Add Key Config to the List of items | ||
509 | */ | ||
510 | void OKeyConfigManager::addKeyConfig( const OKeyConfigItem& item ) { | ||
511 | m_keys.append( item ); | ||
512 | delete m_map; m_map = 0; | ||
513 | } | ||
514 | |||
515 | /** | ||
516 | * Remove the Key from the Config. Internal lists will be destroyed | ||
517 | * and rebuild on demand later | ||
518 | */ | ||
519 | void OKeyConfigManager::removeKeyConfig( const OKeyConfigItem& item ) { | ||
520 | m_keys.remove( item ); | ||
521 | delete m_map; m_map = 0; | ||
522 | } | ||
523 | |||
524 | /** | ||
525 | * Clears the complete list | ||
526 | */ | ||
527 | void OKeyConfigManager::clearKeyConfig() { | ||
528 | m_keys.clear(); | ||
529 | delete m_map; m_map = 0; | ||
530 | } | ||
531 | |||
532 | |||
533 | /** | ||
534 | * Add this OKeyPair to the blackList. | ||
535 | * Internal lists will be destroyed | ||
536 | */ | ||
537 | void OKeyConfigManager::addToBlackList( const OKeyPair& key) { | ||
538 | m_blackKeys.append( key ); | ||
539 | delete m_map; m_map = 0; | ||
540 | } | ||
407 | 541 | ||
542 | |||
543 | /** | ||
544 | * Remove this OKeyPair from the black List | ||
545 | * Internal lists will be destroyed | ||
546 | */ | ||
547 | void OKeyConfigManager::removeFromBlackList( const OKeyPair& key ) { | ||
548 | m_blackKeys.remove( key ); | ||
549 | delete m_map; m_map = 0; | ||
550 | } | ||
551 | |||
552 | |||
553 | /** | ||
554 | * Clear the blackList | ||
555 | */ | ||
556 | void OKeyConfigManager::clearBlackList() { | ||
557 | m_blackKeys.clear(); | ||
558 | delete m_map; m_map = 0; | ||
408 | } | 559 | } |
560 | |||
561 | |||
562 | /** | ||
563 | * Return a copy of the blackList | ||
564 | */ | ||
565 | OKeyPairList OKeyConfigManager::blackList()const { | ||
566 | return m_blackKeys; | ||
567 | } | ||
568 | |||
569 | |||
570 | /** | ||
571 | * Ask the Manager to handle KeyEvents for you. | ||
572 | * All handled keys will emit a QSignal and return true | ||
573 | * that it handled the keyevent | ||
574 | */ | ||
575 | void OKeyConfigManager::handleWidget( QWidget* wid ) { | ||
576 | wid->installEventFilter( this ); | ||
577 | } | ||
578 | |||
579 | /** | ||
580 | * @internal | ||
581 | */ | ||
582 | bool OKeyConfigManager::eventFilter( QObject* obj, QEvent* ev) { | ||
583 | if ( !obj->isWidgetType() ) | ||
584 | return false; | ||
585 | |||
586 | if ( ev->type() != QEvent::KeyPress && ev->type() != QEvent::KeyRelease ) | ||
587 | return false; | ||
588 | |||
589 | QKeyEvent *key = static_cast<QKeyEvent*>( ev ); | ||
590 | OKeyConfigItem item = handleKeyEvent( key ); | ||
591 | |||
592 | if ( item.isEmpty() ) | ||
593 | return false; | ||
594 | |||
595 | emit actionActivated( static_cast<QWidget*>( obj ), key, item ); | ||
596 | return true; | ||
597 | } | ||
598 | |||
599 | /** | ||
600 | * @internal | ||
601 | */ | ||
602 | OKeyConfigItemList OKeyConfigManager::keyList( int keycode) { | ||
603 | if ( !m_map ) { | ||
604 | m_map = new OKeyMapConfigPrivate; | ||
605 | /* for every key */ | ||
606 | for ( OKeyConfigItemList::Iterator it = m_keys.begin(); | ||
607 | it!= m_keys.end(); ++it ) { | ||
608 | bool add = true; | ||
609 | /* see if this key is blocked */ | ||
610 | for ( OKeyPairList::Iterator pairIt = m_blackKeys.begin(); | ||
611 | pairIt != m_blackKeys.end(); ++pairIt ) { | ||
612 | if ( (*pairIt).keycode() == (*it).keycode() && | ||
613 | (*pairIt).modifier() == (*it).modifier() ) { | ||
614 | add = false; | ||
615 | break; | ||
616 | } | ||
617 | } | ||
618 | /* check if we added it */ | ||
619 | if ( add ) { | ||
620 | if ( m_map->contains( (*it).keycode() ) ) | ||
621 | (m_map[(*it).keycode()]).append( *it ); | ||
622 | else | ||
623 | m_map.insert( (*it).keycode(), OKeyConfigItemList( *it ) ); | ||
624 | } | ||
625 | } | ||
626 | } | ||
627 | return m_map[keycode]; | ||
628 | } | ||
629 | |||
630 | |||
631 | |||
632 | ///////////////////////// | ||
633 | //////// Widget Starts Here | ||
634 | namespace Opie { | ||
635 | namespace Ui { | ||
636 | namespace Private { | ||
637 | static QString keyToString( const OKeyPair& ); | ||
638 | class OItemBox : public QHBox { | ||
639 | Q_OBJECT | ||
640 | public: | ||
641 | OItemBox( const OKeyConfigItem& item, QWidget* parent = 0, const char* name = 0, WFlags fl = 0); | ||
642 | ~OItemBox(); | ||
643 | |||
644 | OKeyConfigItem item()const; | ||
645 | void setItem( const OKeyConfigItem& item ); | ||
646 | private slots: | ||
647 | void slotClicked(); | ||
648 | signals: | ||
649 | void configureBox( OItemBox* ); | ||
650 | private: | ||
651 | QLabel *m_pix; | ||
652 | QLabel *m_text; | ||
653 | QPushButton *m_btn; | ||
654 | OKeyConfigItem m_item; | ||
655 | }; | ||
656 | |||
657 | OItemBox::OItemBox( const OKeyConfigItem& item, QWidget* parent, | ||
658 | const char* name, WFlags fl ) | ||
659 | : QHBox( parent, name, fl ), { | ||
660 | m_pix = new QLabel( this ); | ||
661 | m_text = new QLabel( this ); | ||
662 | m_btn = new QPushButton( this ); | ||
663 | |||
664 | connect(m_btn, SIGNAL(clicked()), | ||
665 | this, SLOT(slotClicked())); | ||
666 | |||
667 | setItem( item ); | ||
668 | } | ||
669 | |||
670 | OItemBox::~OItemBox() {} | ||
671 | OKeyConfigItem OItemBox::item()const{ | ||
672 | return m_item; | ||
673 | } | ||
674 | void OKeyConfigItem::setItem( const OKeyConfigItem& item ) { | ||
675 | m_item = item; | ||
676 | m_pix ->setPixmap( item.pixmap() ); | ||
677 | m_text->setText( item.text() ); | ||
678 | m_btn->setText( keyToString( item.keyPair() ) ); | ||
679 | } | ||
680 | void OKeyConfigItem::slotClicked() { | ||
681 | emit configureBox( this ); | ||
682 | } | ||
683 | |||
684 | QString keyToString( const OKeyPair& pair ) { | ||
685 | QStringList mod; | ||
686 | if ( ( pair.modifier() & Qt::ShiftButton )== Qt::ShiftButton ) | ||
687 | mod.append( QObject::tr( "Shift", "The Keyboard key" ) ); | ||
688 | if ( ( pair.modifier() & Qt::ControlButton )== Qt::ControlButton ) | ||
689 | mod.append( QObject::tr( "Ctrl", "The Ctrl key" ) ); | ||
690 | if ( ( pair.modifier() & Qt::AltButton ) )== Qt::AltButton ) | ||
691 | mod.append( QObject::tr( "Alt", "The keyboard Alt Key" ) ); | ||
692 | |||
693 | |||
694 | } | ||
695 | } | ||
696 | } | ||
697 | } | ||
698 | |||
699 | |||
700 | |||
701 | //////////////////////// | ||
702 | |||
703 | |||
704 | |||
705 | #include "okeyconfigwidget.moc" | ||
diff --git a/libopie2/opieui/okeyconfigwidget.h b/libopie2/opieui/okeyconfigwidget.h index 0d5d26f..414ee7e 100644 --- a/libopie2/opieui/okeyconfigwidget.h +++ b/libopie2/opieui/okeyconfigwidget.h | |||
@@ -136,6 +136,7 @@ private: | |||
136 | */ | 136 | */ |
137 | class OKeyConfigManager : public QObject { | 137 | class OKeyConfigManager : public QObject { |
138 | Q_OBJECT | 138 | Q_OBJECT |
139 | typedef QMap<it, OKeyConfigItemList> OKeyMapConfigPrivate; | ||
139 | public: | 140 | public: |
140 | OKeyConfigManager(Opie::Core::OConfig *conf = 0, | 141 | OKeyConfigManager(Opie::Core::OConfig *conf = 0, |
141 | const QString& group = QString::null, | 142 | const QString& group = QString::null, |
@@ -148,13 +149,14 @@ public: | |||
148 | void save(); | 149 | void save(); |
149 | 150 | ||
150 | OKeyConfigItem handleKeyEvent( QKeyEvent* ); | 151 | OKeyConfigItem handleKeyEvent( QKeyEvent* ); |
151 | QString handleKeyEventString( QKeyEvent* ); | 152 | int handleKeyEventId( QKeyEvent* ); |
152 | 153 | ||
153 | void addKeyConfig( const OKeyConfigItem& ); | 154 | void addKeyConfig( const OKeyConfigItem& ); |
154 | void removeKeyConfig( const OKeyConfigItem& ); | 155 | void removeKeyConfig( const OKeyConfigItem& ); |
156 | void clearKeyConfig(); | ||
155 | 157 | ||
156 | void addBlackList( const OKeyPair& ); | 158 | void addToBlackList( const OKeyPair& ); |
157 | void removeBlackList( const OKeyPair& ); | 159 | void removeFromBlackList( const OKeyPair& ); |
158 | void clearBlackList(); | 160 | void clearBlackList(); |
159 | OKeyPairList blackList()const; | 161 | OKeyPairList blackList()const; |
160 | 162 | ||
@@ -166,12 +168,14 @@ signals: | |||
166 | void actionActivated( QWidget*, QKeyEvent*, const Opie::Ui::OKeyConfigItem& ); | 168 | void actionActivated( QWidget*, QKeyEvent*, const Opie::Ui::OKeyConfigItem& ); |
167 | 169 | ||
168 | private: | 170 | private: |
171 | OKeyConfigItemList keyList( int ); | ||
169 | OKeyPairList m_blackKeys; | 172 | OKeyPairList m_blackKeys; |
170 | OKeyConfigItemList m_keys; | 173 | OKeyConfigItemList m_keys; |
171 | QValueList<QWidget*> m_widgets; | 174 | QValueList<QWidget*> m_widgets; |
172 | Opie::Core::OConfig *m_conf; | 175 | Opie::Core::OConfig *m_conf; |
173 | QString m_group; | 176 | QString m_group; |
174 | bool m_grab : 1; | 177 | bool m_grab : 1; |
178 | OKeyMapConfigPrivate *m_map; | ||
175 | class Private; | 179 | class Private; |
176 | Private *d; | 180 | Private *d; |
177 | }; | 181 | }; |