-rw-r--r-- | libopie2/opieui/okeyconfigwidget.cpp | 311 | ||||
-rw-r--r-- | libopie2/opieui/okeyconfigwidget.h | 10 |
2 files changed, 311 insertions, 10 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 | |||
@@ -25,74 +25,74 @@ OKeyPair::OKeyPair( int key, int mod ) | |||
25 | * The destructor | 25 | * The destructor |
26 | */ | 26 | */ |
27 | OKeyPair::~OKeyPair() {} | 27 | OKeyPair::~OKeyPair() {} |
28 | 28 | ||
29 | 29 | ||
30 | /** | 30 | /** |
31 | * Is this OKeyPair empty/valid? | 31 | * Is this OKeyPair empty/valid? |
32 | */ | 32 | */ |
33 | bool OKeyPair::isEmpty()const { | 33 | bool OKeyPair::isEmpty()const { |
34 | return ( ( m_key == -1 )&& ( m_mod == -1 ) ); | 34 | return ( ( m_key == -1 )&& ( m_mod == -1 ) ); |
35 | } | 35 | } |
36 | 36 | ||
37 | /** | 37 | /** |
38 | * get the keycode for this OKeyPair. The Key relates to Qt::Key. | 38 | * get the keycode for this OKeyPair. The Key relates to Qt::Key. |
39 | * | 39 | * |
40 | * @see Qt::Key | 40 | * @see Qt::Key |
41 | * @see setKey | 41 | * @see setKey |
42 | */ | 42 | */ |
43 | int OKeyPair::keycode()const { | 43 | int OKeyPair::keycode()const { |
44 | return m_key; | 44 | return m_key; |
45 | } | 45 | } |
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 { |
55 | return m_mod; | 55 | return m_mod; |
56 | } | 56 | } |
57 | 57 | ||
58 | 58 | ||
59 | /** | 59 | /** |
60 | * Set the keycode | 60 | * Set the keycode |
61 | * @param key The Keycode to set | 61 | * @param key The Keycode to set |
62 | * | 62 | * |
63 | * @see keycode() | 63 | * @see keycode() |
64 | * @see Qt::Key | 64 | * @see Qt::Key |
65 | */ | 65 | */ |
66 | void OKeyPair::setKeycode( int key ) { | 66 | void OKeyPair::setKeycode( int key ) { |
67 | 67 | ||
68 | } | 68 | } |
69 | 69 | ||
70 | /** | 70 | /** |
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 ) { |
78 | 78 | ||
79 | } | 79 | } |
80 | 80 | ||
81 | /** | 81 | /** |
82 | * Return an OKeyPair for the Return Key without any modifier. | 82 | * Return an OKeyPair for the Return Key without any modifier. |
83 | */ | 83 | */ |
84 | OKeyPair OKeyPair::returnKey() { | 84 | OKeyPair OKeyPair::returnKey() { |
85 | return OKeyPair( Qt::Key_Return, 0 ); | 85 | return OKeyPair( Qt::Key_Return, 0 ); |
86 | } | 86 | } |
87 | 87 | ||
88 | /** | 88 | /** |
89 | * Return an OKeyPair for the Left Arrow Key | 89 | * Return an OKeyPair for the Left Arrow Key |
90 | * without any modifier Key | 90 | * without any modifier Key |
91 | */ | 91 | */ |
92 | OKeyPair OKeyPair::leftArrowKey() { | 92 | OKeyPair OKeyPair::leftArrowKey() { |
93 | return OKeyPair( Qt::Key_Left, 0 ); | 93 | return OKeyPair( Qt::Key_Left, 0 ); |
94 | } | 94 | } |
95 | 95 | ||
96 | /** | 96 | /** |
97 | * Return an OKeyPair for the Right Arrow Key | 97 | * Return an OKeyPair for the Right Arrow Key |
98 | * without any modifier Key | 98 | * without any modifier Key |
@@ -124,48 +124,67 @@ OKeyPair OKeyPair::emptyKey() { | |||
124 | return OKeyPair; | 124 | return OKeyPair; |
125 | } | 125 | } |
126 | 126 | ||
127 | /** | 127 | /** |
128 | * This functions uses the Opie::Core::ODevice::buttons | 128 | * This functions uses the Opie::Core::ODevice::buttons |
129 | * for OKeyPairList | 129 | * for OKeyPairList |
130 | * | 130 | * |
131 | * @see Opie::Core::ODevice | 131 | * @see Opie::Core::ODevice |
132 | * @see Opie::Core::ODeviceButton | 132 | * @see Opie::Core::ODeviceButton |
133 | * @see Opie::Core::ODevice::button | 133 | * @see Opie::Core::ODevice::button |
134 | */ | 134 | */ |
135 | OKeyPairList OKeyPair::hardwareKeys() { | 135 | OKeyPairList OKeyPair::hardwareKeys() { |
136 | const QValueList<Opie::Core::ODeviceButton> but = Opie::Core::ODevice::inst()->buttons(); | 136 | const QValueList<Opie::Core::ODeviceButton> but = Opie::Core::ODevice::inst()->buttons(); |
137 | OKeyPairList lst; | 137 | OKeyPairList lst; |
138 | 138 | ||
139 | for ( QValueList<Opie::Core::ODeviceButton>::Iterator it = but.begin(); | 139 | for ( QValueList<Opie::Core::ODeviceButton>::Iterator it = but.begin(); |
140 | it != but.end(); ++it ) | 140 | it != but.end(); ++it ) |
141 | lst.append( OKeyPair( (*it).keycode(), 0 ) ); | 141 | lst.append( OKeyPair( (*it).keycode(), 0 ) ); |
142 | 142 | ||
143 | 143 | ||
144 | return lst; | 144 | return lst; |
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 |
151 | * you use this item with the OKeyConfigManager your setting | 170 | * you use this item with the OKeyConfigManager your setting |
152 | * will be overwritten. | 171 | * will be overwritten. |
153 | * | 172 | * |
154 | * @param text The text exposed to the user | 173 | * @param text The text exposed to the user |
155 | * @param config_key The key used in the config | 174 | * @param config_key The key used in the config |
156 | * @param pix A Pixmap associated with this Item | 175 | * @param pix A Pixmap associated with this Item |
157 | * @param key The OKeyPair used | 176 | * @param key The OKeyPair used |
158 | * @param def The OKeyPair used as default | 177 | * @param def The OKeyPair used as default |
159 | * | 178 | * |
160 | */ | 179 | */ |
161 | OKeyConfigItem::OKeyConfigItem( const QString& text, const QCString& config_key, | 180 | OKeyConfigItem::OKeyConfigItem( const QString& text, const QCString& config_key, |
162 | const QPixmap& pix, int id, const OKeyPair& def, | 181 | const QPixmap& pix, int id, const OKeyPair& def, |
163 | const OKeyPair& key) | 182 | const OKeyPair& key) |
164 | : m_text( text ), m_config( config_key ), m_pix( pix ), | 183 | : m_text( text ), m_config( config_key ), m_pix( pix ), |
165 | m_id( id ), m_def( def ), m_key( key ) {} | 184 | m_id( id ), m_def( def ), m_key( key ) {} |
166 | 185 | ||
167 | /** | 186 | /** |
168 | * A special Constructor for converting from an Opie::Core::ODeviceButton | 187 | * A special Constructor for converting from an Opie::Core::ODeviceButton |
169 | * delivered by Opie::Core::ODevice::buttons() | 188 | * delivered by Opie::Core::ODevice::buttons() |
170 | * There is no Config Key set and both default key and key are set | 189 | * There is no Config Key set and both default key and key are set |
171 | * to Opie::Core::ODeviceButton::keycode() and 0 to modifier | 190 | * to Opie::Core::ODeviceButton::keycode() and 0 to modifier |
@@ -288,48 +307,70 @@ void OKeyConfigItem::setConfigKey( const QCString& str) { | |||
288 | m_config.detach(); | 307 | m_config.detach(); |
289 | } | 308 | } |
290 | 309 | ||
291 | /** | 310 | /** |
292 | * @internal | 311 | * @internal |
293 | */ | 312 | */ |
294 | void OKeyConfigItem::setId( int id ) { | 313 | void OKeyConfigItem::setId( int id ) { |
295 | m_id = id; | 314 | m_id = id; |
296 | } | 315 | } |
297 | 316 | ||
298 | /** | 317 | /** |
299 | * If the item is not configured isEmpty() will return true | 318 | * If the item is not configured isEmpty() will return true |
300 | * It is empty if no text is present and no default | 319 | * It is empty if no text is present and no default |
301 | * and no configured key | 320 | * and no configured key |
302 | */ | 321 | */ |
303 | bool OKeyConfigItem::isEmpty()const { | 322 | bool OKeyConfigItem::isEmpty()const { |
304 | if ( !m_def.isEmpty() ) return false; | 323 | if ( !m_def.isEmpty() ) return false; |
305 | if ( !m_key.isEmpty() ) return false; | 324 | if ( !m_key.isEmpty() ) return false; |
306 | if ( !m_text.isEmpty() ) return false; | 325 | if ( !m_text.isEmpty() ) return false; |
307 | 326 | ||
308 | return true; | 327 | return true; |
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 | * |
315 | * You can use this manager in multiple ways. Either make it handle | 356 | * You can use this manager in multiple ways. Either make it handle |
316 | * QKeyEvents | 357 | * QKeyEvents |
317 | * | 358 | * |
318 | * \code | 359 | * \code |
319 | * Opie::Core::Config conf = oApp->config(); | 360 | * Opie::Core::Config conf = oApp->config(); |
320 | * Opie::Ui::OKeyPairList blackList; | 361 | * Opie::Ui::OKeyPairList blackList; |
321 | * blackList.append(Opie::Ui::OKeyPair::leftArrowKey()); | 362 | * blackList.append(Opie::Ui::OKeyPair::leftArrowKey()); |
322 | * blackList.append(Opie::Ui::OKeyPair::rightArrowKey()); | 363 | * blackList.append(Opie::Ui::OKeyPair::rightArrowKey()); |
323 | * Opie::Ui::OKeyConfigManager *manager = new Opie::Ui::OKeyConfigManager(conf,"key_actions",blackList, | 364 | * Opie::Ui::OKeyConfigManager *manager = new Opie::Ui::OKeyConfigManager(conf,"key_actions",blackList, |
324 | * false); | 365 | * false); |
325 | * QListView *view = new QListView(); | 366 | * QListView *view = new QListView(); |
326 | * manager->handleWidget(view); | 367 | * manager->handleWidget(view); |
327 | * manager->addKeyConfig( Opie::Ui::OKeyPair::returnKey()); | 368 | * manager->addKeyConfig( Opie::Ui::OKeyPair::returnKey()); |
328 | * manager->load(); | 369 | * manager->load(); |
329 | * | 370 | * |
330 | * connect(manager,SIGNAL(actionActivated(QWidget*,QKeyEvent*,const Opie::Ui::OKeyConfigItem&)), | 371 | * connect(manager,SIGNAL(actionActivated(QWidget*,QKeyEvent*,const Opie::Ui::OKeyConfigItem&)), |
331 | * this,SLOT(slotHandleKey(QWidget*,QKeyEvent*,const Opie::Ui::OKeyConfigItem&))); | 372 | * this,SLOT(slotHandleKey(QWidget*,QKeyEvent*,const Opie::Ui::OKeyConfigItem&))); |
332 | * | 373 | * |
333 | * .... | 374 | * .... |
334 | * | 375 | * |
335 | * void update(){ | 376 | * void update(){ |
@@ -348,61 +389,317 @@ bool OKeyConfigItem::isEmpty()const { | |||
348 | * Opie::Ui::OKeyConfigItem item = manager->handleKeyEvent(e); | 389 | * Opie::Ui::OKeyConfigItem item = manager->handleKeyEvent(e); |
349 | * if(!item.isEmpty() ){ | 390 | * if(!item.isEmpty() ){ |
350 | * switch(item.id()){ | 391 | * switch(item.id()){ |
351 | * case My_Delete_Key: | 392 | * case My_Delete_Key: |
352 | * break; | 393 | * break; |
353 | * } | 394 | * } |
354 | * } | 395 | * } |
355 | * } | 396 | * } |
356 | * | 397 | * |
357 | * \endcode | 398 | * \endcode |
358 | * | 399 | * |
359 | * @param conf The Config where the KeySetting should be stored | 400 | * @param conf The Config where the KeySetting should be stored |
360 | * @param group The group where the KeySetting will be stored | 401 | * @param group The group where the KeySetting will be stored |
361 | * @param black Which keys shouldn't be allowed to handle | 402 | * @param black Which keys shouldn't be allowed to handle |
362 | * @param grabkeyboard Calls QPEApplication::grabKeyboard to allow handling of DeviceButtons | 403 | * @param grabkeyboard Calls QPEApplication::grabKeyboard to allow handling of DeviceButtons |
363 | * @param par The parent/owner of this manager | 404 | * @param par The parent/owner of this manager |
364 | * @param name The name of this object | 405 | * @param name The name of this object |
365 | */ | 406 | */ |
366 | OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf, | 407 | OKeyConfigManager::OKeyConfigManager( Opie::Core::OConfig* conf, |
367 | const QString& group, | 408 | const QString& group, |
368 | OKeyPairList black, | 409 | OKeyPairList black, |
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 ); |
389 | 432 | ||
390 | /* | 433 | /* |
391 | * Read each item | 434 | * Read each item |
392 | */ | 435 | */ |
393 | int key, mod; | 436 | int key, mod; |
394 | for( OKeyConfigItemList::Iterator it = m_keys.begin(); | 437 | for( OKeyConfigItemList::Iterator it = m_keys.begin(); |
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() ); |
398 | (*it).setKeyPair( OKeyPair(key, mod) ); | 441 | OKeyPair okey( key, mod ); |
442 | if ( !m_blackKeys.contains( okey ) && key != -1 && mod != -1 ) | ||
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 | /** |
403 | * We will save the current configuration | 451 | * We will save the current configuration |
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 ); | ||
407 | 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 | } | ||
408 | } | 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 | } | ||
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; | ||
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 | |||
@@ -115,84 +115,88 @@ private: | |||
115 | OKeyPair m_def; | 115 | OKeyPair m_def; |
116 | class Private; | 116 | class Private; |
117 | Private *d; | 117 | Private *d; |
118 | }; | 118 | }; |
119 | 119 | ||
120 | 120 | ||
121 | 121 | ||
122 | /** | 122 | /** |
123 | * \brief A manager to load and save Key Actions and get notified | 123 | * \brief A manager to load and save Key Actions and get notified |
124 | * This is the Manager for KeyActions. | 124 | * This is the Manager for KeyActions. |
125 | * You can say from which config and group to read data, to grab the | 125 | * You can say from which config and group to read data, to grab the |
126 | * keyboard to handle hardware keys, you can supply a blacklist of | 126 | * keyboard to handle hardware keys, you can supply a blacklist of |
127 | * keys which should not be used by allowed to be used. | 127 | * keys which should not be used by allowed to be used. |
128 | * You can even pass this manager to a Widget to do the configuration for you. | 128 | * You can even pass this manager to a Widget to do the configuration for you. |
129 | * You need to add OKeyConfigItem for your keys and then issue a load() to | 129 | * You need to add OKeyConfigItem for your keys and then issue a load() to |
130 | * read the Key information. | 130 | * read the Key information. |
131 | * You can either handle the QKeyEvent yourself and ask this class if it is | 131 | * You can either handle the QKeyEvent yourself and ask this class if it is |
132 | * handled by your action and let give you the action. Or you can install | 132 | * handled by your action and let give you the action. Or you can install |
133 | * the event filter and get a signal. | 133 | * the event filter and get a signal. |
134 | * | 134 | * |
135 | * @since 1.1.2 | 135 | * @since 1.1.2 |
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, |
142 | OKeyPairList &block = OKeyPairList(), | 143 | OKeyPairList &block = OKeyPairList(), |
143 | bool grabkeyboard = false, QObject *= 0, | 144 | bool grabkeyboard = false, QObject *= 0, |
144 | const char* name = 0 ); | 145 | const char* name = 0 ); |
145 | ~OKeyConfigManager(); | 146 | ~OKeyConfigManager(); |
146 | 147 | ||
147 | void load(); | 148 | void load(); |
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 | ||
161 | void handleWidget( QWidget* ); | 163 | void handleWidget( QWidget* ); |
162 | 164 | ||
163 | bool eventFilter( QObject*, QEvent* ); | 165 | bool eventFilter( QObject*, QEvent* ); |
164 | signals: | 166 | signals: |
165 | void keyConfigChanged( Opie::Ui::OKeyConfigManager* ); | 167 | void keyConfigChanged( Opie::Ui::OKeyConfigManager* ); |
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 | }; |
178 | 182 | ||
179 | 183 | ||
180 | class OKeyConfigWidget : public QHBox { | 184 | class OKeyConfigWidget : public QHBox { |
181 | Q_OBJECT | 185 | Q_OBJECT |
182 | public: | 186 | public: |
183 | enum ChangeMode { Imediate, Queu }; | 187 | enum ChangeMode { Imediate, Queu }; |
184 | OKeyConfigWidget( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); | 188 | OKeyConfigWidget( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 ); |
185 | OKeyConfigWidget( OKeyConfigManager *, QWidget* parent = 0, const char* = 0, WFlags = 0 ); | 189 | OKeyConfigWidget( OKeyConfigManager *, QWidget* parent = 0, const char* = 0, WFlags = 0 ); |
186 | ~OKeyConfigWidget(); | 190 | ~OKeyConfigWidget(); |
187 | 191 | ||
188 | void setChangeMode( enum ChangeMode ); | 192 | void setChangeMode( enum ChangeMode ); |
189 | ChangeMode changeMode()const; | 193 | ChangeMode changeMode()const; |
190 | 194 | ||
191 | void setKeyConfig( OKeyConfigManager* ); | 195 | void setKeyConfig( OKeyConfigManager* ); |
192 | 196 | ||
193 | void reload(); | 197 | void reload(); |
194 | void save(); | 198 | void save(); |
195 | private: | 199 | private: |
196 | OKeyConfigManager* m_manager; | 200 | OKeyConfigManager* m_manager; |
197 | class Private; | 201 | class Private; |
198 | Private *d; | 202 | Private *d; |