summaryrefslogtreecommitdiff
path: root/libopie2/opiecore/okeyconfigmanager.cpp
Unidiff
Diffstat (limited to 'libopie2/opiecore/okeyconfigmanager.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiecore/okeyconfigmanager.cpp733
1 files changed, 733 insertions, 0 deletions
diff --git a/libopie2/opiecore/okeyconfigmanager.cpp b/libopie2/opiecore/okeyconfigmanager.cpp
new file mode 100644
index 0000000..e6055e0
--- a/dev/null
+++ b/libopie2/opiecore/okeyconfigmanager.cpp
@@ -0,0 +1,733 @@
1#include "okeyconfigmanager.h"
2
3#include "okeyconfigmanager_p.h"
4
5namespace Opie {
6namespace Core {
7namespace Internal {
8 /*
9 * the virtual and hardware key events have both issues...
10 */
11 void fixupKeys( int& key, int &mod, QKeyEvent* e ) {
12 key = e->key();
13 mod = e->state();
14 /*
15 * virtual keyboard
16 * else change the button mod only
17 */
18 if ( key == 0 ) {
19 key = e->ascii();
20 if ( key > 96 && key < 123)
21 key -= 32;
22 }else{
23 int new_mod = 0;
24 if ( mod & 256 )
25 new_mod |= Qt::ShiftButton;
26 else if ( mod & 512 )
27 new_mod |= Qt::ControlButton;
28 else if ( mod & 1024 )
29 new_mod |= Qt::AltButton;
30
31 mod = new_mod == 0? mod : new_mod;
32 }
33 }
34}
35
36/**
37 * The default Constructor of a OKeyPair.
38 * A Key and a Modifier ( Alt/Shift/Ctrl )
39 * needs to be supplied.
40 * Use Qt::Key for the information.
41 * The default arguments create an Empty OKeyPair. If you
42 * want to get an empty OKeyPair use the static method for getting
43 * the emptyKey()
44 *
45 * @see OKeyPair OKeyPair::emptyKey()
46 */
47OKeyPair::OKeyPair( int key, int mod )
48 : m_key( key ), m_mod( mod )
49{}
50
51/**
52 * The destructor
53 */
54OKeyPair::~OKeyPair() {}
55
56
57/**
58 * Is this OKeyPair empty/valid?
59 */
60bool OKeyPair::isEmpty()const {
61 return ( ( m_key == -1 )&& ( m_mod == -1 ) );
62}
63
64/**
65 * get the keycode for this OKeyPair. The Key relates to Qt::Key.
66 *
67 * @see Qt::Key
68 * @see setKey
69 */
70int OKeyPair::keycode()const {
71 return m_key;
72}
73
74/**
75 * get the modifier key for this OKeyPair. The Modifier State relates
76 * to the Qt::ButtonState
77 *
78 * @see Qt::ButtonState
79 * @see setModifier
80 */
81int OKeyPair::modifier()const {
82 return m_mod;
83}
84
85
86/**
87 * Set the keycode
88 * @param key The Keycode to set
89 *
90 * @see keycode()
91 * @see Qt::Key
92 */
93void OKeyPair::setKeycode( int key ) {
94 m_key = key;
95}
96
97/**
98 * Set the modifier key
99 *
100 * @param the Modifier key
101 * @see Qt::ButtonState
102 * @see modifier()
103 */
104void OKeyPair::setModifier( int mod ) {
105 m_mod = mod;
106}
107
108/**
109 * Return an OKeyPair for the Return Key without any modifier.
110 */
111OKeyPair OKeyPair::returnKey() {
112 return OKeyPair( Qt::Key_Return, 0 );
113}
114
115/**
116 * Return an OKeyPair for the Left Arrow Key
117 * without any modifier Key
118 */
119OKeyPair OKeyPair::leftArrowKey() {
120 return OKeyPair( Qt::Key_Left, 0 );
121}
122
123/**
124 * Return an OKeyPair for the Right Arrow Key
125 * without any modifier Key
126 */
127OKeyPair OKeyPair::rightArrowKey() {
128 return OKeyPair( Qt::Key_Right, 0 );
129}
130
131/**
132 * Return an OKeyPair for the Up Arrow Key
133 * without any modifier Key
134 */
135OKeyPair OKeyPair::upArrowKey() {
136 return OKeyPair( Qt::Key_Up, 0 );
137}
138
139/**
140 * Return an OKeyPair for the Down Arrow Key
141 * without any modifier Key
142 */
143OKeyPair OKeyPair::downArrowKey() {
144 return OKeyPair( Qt::Key_Down, 0 );
145}
146
147/**
148 * Return an Empty OKeyPair
149 */
150OKeyPair OKeyPair::emptyKey() {
151 return OKeyPair();
152}
153
154/**
155 * This functions uses the Opie::Core::ODevice::buttons
156 * for OKeyPairList
157 *
158 * @see Opie::Core::ODevice
159 * @see Opie::Core::ODeviceButton
160 * @see Opie::Core::ODevice::button
161 */
162OKeyPair::List OKeyPair::hardwareKeys() {
163 const QValueList<Opie::Core::ODeviceButton> but = Opie::Core::ODevice::inst()->buttons();
164 OKeyPair::List lst;
165
166 for ( QValueList<Opie::Core::ODeviceButton>::ConstIterator it = but.begin();
167 it != but.end(); ++it )
168 lst.append( OKeyPair( (*it).keycode(), 0 ) );
169
170
171 return lst;
172}
173
174/**
175 * Equals operator. Check if two OKeyPairs have the same key and modifier
176 * @see operator!=
177 */
178bool OKeyPair::operator==( const OKeyPair& pair)const {
179 if ( m_key != pair.m_key ) return false;
180 if ( m_mod != pair.m_mod ) return false;
181
182 return true;
183}
184
185/**
186 * Not equal operator. calls the equal operator internally
187 */
188bool OKeyPair::operator!=( const OKeyPair& pair)const {
189 return !(*this == pair);
190}
191
192
193/**
194 * The normal Constructor to create a OKeyConfigItem
195 *
196 * You can set the the key paramater of this item but if
197 * you use this item with the OKeyConfigManager your setting
198 * will be overwritten.
199 * You can also specify a QObject and slot which sould get called
200 * once this item is activated. This slot only works if you
201 * use the OKeyConfigManager.
202 * The actual Key is read by load()
203 *
204 * \code
205 * void MySlot::create(){
206 * OKeyConfigItem item(tr("Delete"),"delete",Resource::loadPixmap("trash"),
207 * 123, OKeyPair(Qt::Key_D,Qt::ControlButton),
208 * this,SLOT(slotDelete(QWidget*,QKeyEvent*)));
209 * }
210 * \endcode
211 *
212 * @param text The text exposed to the user
213 * @param config_key The key used in the config
214 * @param pix A Pixmap associated with this Item
215 * @param def The OKeyPair used as default
216 * @param caller The object where the slot exists
217 * @param slot The slot which should get called
218 *
219 */
220OKeyConfigItem::OKeyConfigItem( const QString& text, const QCString& config_key,
221 const QPixmap& pix, int id, const OKeyPair& def,
222 QObject *caller,
223 const char* slot )
224 : m_text( text ), m_config( config_key ), m_pix( pix ),
225 m_id( id ), m_def( def ),
226 m_obj( caller ), m_str( slot ) {}
227
228/**
229 * A special Constructor for converting from an Opie::Core::ODeviceButton
230 * delivered by Opie::Core::ODevice::buttons()
231 * There is no Config Key set and both default key and key are set
232 * to Opie::Core::ODeviceButton::keycode() and 0 to modifier
233 *
234 * @see Opie::Core::ODevice
235 * @see Opie::Core::ODeviceButton
236 * @see Opie::Core::ODevice::buttons()
237 */
238OKeyConfigItem::OKeyConfigItem( const Opie::Core::ODeviceButton& b )
239 : m_text( b.userText() ), m_pix( b.pixmap() ), m_id( -1 ),
240 m_key( OKeyPair( b.keycode(), 0 ) ), m_def( OKeyPair( b.keycode(), 0 ) )
241{}
242
243
244/**
245 * Destructor
246 */
247OKeyConfigItem::~OKeyConfigItem() {}
248
249
250/**
251 * The text exposed to the user
252 *
253 * @see setText
254 */
255QString OKeyConfigItem::text()const {
256 return m_text;
257}
258
259/**
260 * The pixmap shown to the user for your action/key
261 *
262 * @see setPixmap
263 */
264QPixmap OKeyConfigItem::pixmap()const {
265 return m_pix;
266}
267
268/**
269 * Return the OKeyPair this OKeyConfigItem is configured for.
270 *
271 * @see setKeyPair
272 */
273OKeyPair OKeyConfigItem::keyPair()const {
274 return m_key;
275}
276
277/**
278 * Return the default OKeyPair
279 * @see setDefaultKeyPair
280 */
281OKeyPair OKeyConfigItem::defaultKeyPair()const {
282 return m_def;
283}
284
285
286/**
287 * Return the Id you assigned to this item.
288 * setting is only possible by the constructor
289 */
290int OKeyConfigItem::id()const{
291 return m_id;
292}
293
294/**
295 * reutrn the Config Key. Setting it is only possible
296 * by the constructor
297 */
298QCString OKeyConfigItem::configKey()const {
299 return m_config;
300}
301
302/**
303 * @internal
304 */
305QObject* OKeyConfigItem::object()const{
306 return m_obj;
307}
308
309/**
310 * @internal
311 */
312QCString OKeyConfigItem::slot()const {
313 return m_str;
314}
315
316/**
317 * Set the text
318 *
319 * @param text Set the Text of this Action to text
320 * @see text()
321 */
322void OKeyConfigItem::setText( const QString& text ) {
323 m_text = text;
324}
325
326/**
327 * Set the pixmap of this action
328 *
329 * @param pix The Pixmap to set
330 * @see pixmap()
331 */
332void OKeyConfigItem::setPixmap( const QPixmap& pix ) {
333 m_pix = pix;
334}
335
336/**
337 * Set the KeyPair the OKeyConfigItem uses.
338 * Your set Key could get overwritten if you use
339 * the manager or GUI to configure the key
340 *
341 * @param key Set the OKeyPair used
342 * @see keyPair()
343 */
344void OKeyConfigItem::setKeyPair( const OKeyPair& key) {
345 m_key = key;
346}
347
348/**
349 * Set the default KeyPair.
350 *
351 * @param key The default keypair
352 * @see defaultKeyPair()
353 */
354void OKeyConfigItem::setDefaultKeyPair( const OKeyPair& key ) {
355 m_def = key;
356}
357
358/**
359 * @internal
360 */
361void OKeyConfigItem::setConfigKey( const QCString& str) {
362 m_config = str;
363 m_config.detach();
364}
365
366/**
367 * @internal
368 */
369void OKeyConfigItem::setId( int id ) {
370 m_id = id;
371}
372
373/**
374 * If the item is not configured isEmpty() will return true
375 * It is empty if no text is present and no default
376 * and no configured key
377 */
378bool OKeyConfigItem::isEmpty()const {
379 if ( !m_def.isEmpty() ) return false;
380 if ( !m_key.isEmpty() ) return false;
381 if ( !m_text.isEmpty() ) return false;
382 if ( m_id != -1 ) return false;
383
384 return true;
385}
386
387/**
388 * Check if the KeyPairs are the same
389 */
390bool OKeyConfigItem::operator==( const OKeyConfigItem& conf )const {
391/* if ( isEmpty() == conf.isEmpty() ) return true;
392 else if ( isEmpty() != conf.isEmpty() ) return false;
393 else if ( !isEmpty()!= conf.isEmpty() ) return false;
394*/
395
396 if ( m_id != conf.m_id ) return false;
397 if ( m_obj != conf.m_obj ) return false;
398 if ( m_text != conf.m_text ) return false;
399 if ( m_key != conf.m_key ) return false;
400 if ( m_def != conf.m_def ) return false;
401
402
403
404 return true;
405
406}
407
408bool OKeyConfigItem::operator!=( const OKeyConfigItem& conf )const {
409 return !( *this == conf );
410}
411
412/**
413 * \brief c'tor
414 * The Constructor for a OKeyConfigManager
415 *
416 * You can use this manager in multiple ways. Either make it handle
417 * QKeyEvents
418 *
419 * \code
420 * Opie::Core::Config conf = oApp->config();
421 * Opie::Core::OKeyPairList blackList;
422 * blackList.append(Opie::Core::OKeyPair::leftArrowKey());
423 * blackList.append(Opie::Core::OKeyPair::rightArrowKey());
424 * Opie::Core::OKeyConfigManager *manager = new Opie::Core::OKeyConfigManager(conf,"key_actions",blackList,
425 * false);
426 * QListView *view = new QListView();
427 * manager->handleWidget(view);
428 * manager->addKeyConfig( Opie::Core::OKeyPair::returnKey());
429 * manager->load();
430 *
431 * connect(manager,SIGNAL(actionActivated(QWidget*,QKeyEvent*,const Opie::Core::OKeyConfigItem&)),
432 * this,SLOT(slotHandleKey(QWidget*,QKeyEvent*,const Opie::Core::OKeyConfigItem&)));
433 *
434 * ....
435 *
436 * void update(){
437 * QDialog diag(true);
438 * QHBoxLayout *lay = new QHBoxLayout(&diag);
439 * Opie::Ui::OKeyConfigWidget *wid = new Opie::Ui::OKeyConfigWidget(manager,&diag);
440 * wid->setChangeMode(Opie::Ui::OKeyConfigWidget::Queu);
441 * lay->addWidget(wid);
442 * if(QPEApplication::execDialog( &diag)== QDialog::Accepted){
443 * wid->save();
444 * }
445 * }
446 *
447 * ....
448 * MyListView::keyPressEvent( QKeyEvent* e ){
449 * Opie::Ui::OKeyConfigItem item = manager->handleKeyEvent(e);
450 * if(!item.isEmpty() ){
451 * switch(item.id()){
452 * case My_Delete_Key:
453 * break;
454 * }
455 * }
456 * }
457 *
458 * \endcode
459 *
460 * @param conf The Config where the KeySetting should be stored
461 * @param group The group where the KeySetting will be stored
462 * @param black Which keys shouldn't be allowed to handle
463 * @param grabkeyboard Calls QPEApplication::grabKeyboard to allow handling of DeviceButtons
464 * @param par The parent/owner of this manager
465 * @param name The name of this object
466 */
467OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf,
468 const QString& group,
469 const OKeyPair::List& black,
470 bool grabkeyboard, QObject* par,
471 const char* name)
472 : QObject( par, name ), m_conf( conf ), m_group( group ),
473 m_blackKeys( black ), m_grab( grabkeyboard ), m_map( 0 ){
474 if ( m_grab )
475 QPEApplication::grabKeyboard();
476}
477
478
479/**
480 * Destructor
481 */
482OKeyConfigManager::~OKeyConfigManager() {
483 if ( m_grab )
484 QPEApplication::ungrabKeyboard();
485}
486
487/**
488 * Load the Configuration from the OConfig
489 * If a Key is restricted but was in the config we will
490 * make it be the empty key paur
491 * We will change the group but restore to the previous.
492 *
493 * @see OKeyPair::emptyKey
494 */
495void OKeyConfigManager::load() {
496 Opie::Core::OConfigGroupSaver( m_conf, m_group );
497
498 /*
499 * Read each item
500 */
501 int key, mod;
502 for( OKeyConfigItem::List::Iterator it = m_keys.begin(); it != m_keys.end(); ++it ) {
503 key = m_conf->readNumEntry( (*it).configKey()+"key", (*it).defaultKeyPair().keycode() );
504 mod = m_conf->readNumEntry( (*it).configKey()+"mod", (*it).defaultKeyPair().modifier() );
505 OKeyPair okey( key, mod );
506
507 if ( !m_blackKeys.contains( okey ) && key != -1 && mod != -1 )
508 (*it).setKeyPair( OKeyPair(key, mod) );
509 else
510 (*it).setKeyPair( OKeyPair::emptyKey() );
511 }
512 delete m_map; m_map = 0;
513}
514
515/**
516 * We will save the current configuration
517 * to the OConfig. We will change the group but restore
518 * to the previous
519 */
520void OKeyConfigManager::save() {
521 Opie::Core::OConfigGroupSaver( m_conf, m_group );
522
523 /*
524 * Write each item
525 */
526 for( OKeyConfigItem::List::Iterator it = m_keys.begin();it != m_keys.end(); ++it ) {
527 /* skip empty items */
528 if ( (*it).isEmpty() )
529 continue;
530 OKeyPair pair = (*it).keyPair();
531 OKeyPair deft = (*it).defaultKeyPair();
532 /*
533 * don't write if it is the default setting
534 * FIXME allow to remove Keys from config
535 if ( (pair.keycode() == deft.keycode()) &&
536 (pair.modifier()== deft.modifier() ) )
537 return;
538 */
539
540 m_conf->writeEntry((*it).configKey()+"key", pair.keycode() );
541 m_conf->writeEntry((*it).configKey()+"mod", pair.modifier() );
542 }
543 m_conf->write();
544}
545
546/**
547 * This is function uses a QMap internally but you can have the same keycode
548 * with different modifier key. The behaviour is undefined if you add a OKeyConfigItem
549 * with same keycode and modifier key. The GUI takes care that a user can't
550 * cofigure two keys.
551 *
552 * Make sure you call e->ignore if you don't want to handle this event
553 */
554OKeyConfigItem OKeyConfigManager::handleKeyEvent( QKeyEvent* e ) {
555 /*
556 * Fix Up issues with Qt/E, my keybard, and virtual input
557 * methods
558 * First my Keyboard delivers 256,512,1024 for shift/ctrl/alt instead of the button state
559 * Also key() on virtual inputmethods are zero and only ascii. We need to fix upper and lower
560 * case ascii
561 */
562 int key, mod;
563 Opie::Core::Internal::fixupKeys( key, mod, e );
564
565 OKeyConfigItem::List _keyList = keyList( key );
566 if ( _keyList.isEmpty() )
567 return OKeyConfigItem();
568
569 OKeyConfigItem item;
570 for ( OKeyConfigItem::List::Iterator it = _keyList.begin(); it != _keyList.end();
571 ++it ) {
572 if ( (*it).keyPair().modifier() == mod ) {
573 item = *it;
574 break;
575 }
576
577 }
578
579 return item;
580}
581
582/**
583 * Return the associated id of the item or -1 if no item
584 * matched the key
585 *
586 * @see handleKeyEvent
587 */
588int OKeyConfigManager::handleKeyEventId( QKeyEvent* ev) {
589 return handleKeyEvent( ev ).id();
590}
591
592/**
593 * Add Key Config to the List of items
594 */
595void OKeyConfigManager::addKeyConfig( const OKeyConfigItem& item ) {
596 m_keys.append( item );
597 delete m_map; m_map = 0;
598}
599
600/**
601 * Remove the Key from the Config. Internal lists will be destroyed
602 * and rebuild on demand later
603 */
604void OKeyConfigManager::removeKeyConfig( const OKeyConfigItem& item ) {
605 m_keys.remove( item );
606 delete m_map; m_map = 0;
607}
608
609/**
610 * Clears the complete list
611 */
612void OKeyConfigManager::clearKeyConfig() {
613 m_keys.clear();
614 delete m_map; m_map = 0;
615}
616
617/**
618 *
619 */
620Opie::Core::OKeyConfigItem::List OKeyConfigManager::keyConfigList()const{
621 return m_keys;
622}
623
624/**
625 * Add this OKeyPair to the blackList.
626 * Internal lists will be destroyed
627 */
628void OKeyConfigManager::addToBlackList( const OKeyPair& key) {
629 m_blackKeys.append( key );
630 delete m_map; m_map = 0;
631}
632
633
634/**
635 * Remove this OKeyPair from the black List
636 * Internal lists will be destroyed
637 */
638void OKeyConfigManager::removeFromBlackList( const OKeyPair& key ) {
639 m_blackKeys.remove( key );
640 delete m_map; m_map = 0;
641}
642
643
644/**
645 * Clear the blackList
646 */
647void OKeyConfigManager::clearBlackList() {
648 m_blackKeys.clear();
649 delete m_map; m_map = 0;
650}
651
652
653/**
654 * Return a copy of the blackList
655 */
656OKeyPair::List OKeyConfigManager::blackList()const {
657 return m_blackKeys;
658}
659
660
661/**
662 * Ask the Manager to handle KeyEvents for you.
663 * All handled keys will emit a QSignal and return true
664 * that it handled the keyevent
665 */
666void OKeyConfigManager::handleWidget( QWidget* wid ) {
667 wid->installEventFilter( this );
668}
669
670/**
671 * @internal
672 */
673bool OKeyConfigManager::eventFilter( QObject* obj, QEvent* ev) {
674 if ( !obj->isWidgetType() )
675 return false;
676
677 if ( ev->type() != QEvent::KeyPress && ev->type() != QEvent::KeyRelease )
678 return false;
679
680 QKeyEvent *key = static_cast<QKeyEvent*>( ev );
681 OKeyConfigItem item = handleKeyEvent( key );
682
683 if ( item.isEmpty() )
684 return false;
685
686 QWidget *wid = static_cast<QWidget*>( obj );
687
688 if ( item.object() && !item.slot().isEmpty() ) {
689 connect( this, SIGNAL( actionActivated(QWidget*, QKeyEvent*)),
690 item.object(), item.slot().data() );
691 emit actionActivated(wid, key);
692 disconnect( this, SIGNAL(actionActivated(QWidget*,QKeyEvent*)),
693 item.object(), item.slot().data() );
694 }
695 emit actionActivated( wid, key, item );
696
697 return true;
698}
699
700/**
701 * @internal
702 */
703OKeyConfigItem::List OKeyConfigManager::keyList( int keycode) {
704 /*
705 * Create the map if not existing anymore
706 */
707 if ( !m_map ) {
708 m_map = new OKeyMapConfigPrivate;
709 /* for every key */
710 for ( OKeyConfigItem::List::Iterator it = m_keys.begin();
711 it!= m_keys.end(); ++it ) {
712
713 bool add = true;
714 /* see if this key is blocked */
715 OKeyPair pair = (*it).keyPair();
716 for ( OKeyPair::List::Iterator pairIt = m_blackKeys.begin();
717 pairIt != m_blackKeys.end(); ++pairIt ) {
718 if ( (*pairIt).keycode() == pair.keycode() &&
719 (*pairIt).modifier() == pair.modifier() ) {
720 add = false;
721 break;
722 }
723 }
724 /* check if we added it */
725 if ( add )
726 (*m_map)[pair.keycode()].append( *it );
727 }
728 }
729 return (*m_map)[keycode];
730}
731
732}
733}