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

okeyconfigwidget.cpp

Go to the documentation of this file.
00001 #include "okeyconfigwidget.h"
00002 #include "okeyconfigwidget_p.h"
00003 
00004 
00005 #include <qgroupbox.h>
00006 #include <qradiobutton.h>
00007 #include <qpushbutton.h>
00008 #include <qbuttongroup.h>
00009 #include <qmessagebox.h>
00010 #include <qaccel.h>
00011 #include <qlayout.h>
00012 #include <qlabel.h>
00013 
00014 /* non gui */
00015 #include <qtimer.h>
00016 
00017 using Opie::Core::OKeyConfigItem;
00018 using Opie::Core::OKeyPair;
00019 using Opie::Core::OKeyConfigManager;
00020 
00021 namespace Opie {
00022 namespace Ui {
00023 namespace Internal {
00024 
00025     OKeyListViewItem::OKeyListViewItem( const OKeyConfigItem& item, OKeyConfigManager* man, OListViewItem* parent)
00026         : Opie::Ui::OListViewItem( parent ), m_manager( man )  {
00027         m_origItem = item;
00028         setItem( item );
00029     }
00030     OKeyListViewItem::~OKeyListViewItem() {}
00031     OKeyConfigItem &OKeyListViewItem::item(){
00032         return m_item;
00033     }
00034     OKeyConfigItem  OKeyListViewItem::origItem() const{
00035         return m_origItem;
00036     }
00037     OKeyConfigManager* OKeyListViewItem::manager() {
00038         return m_manager;
00039     }
00040     void OKeyListViewItem::setItem( const OKeyConfigItem& item ) {
00041         m_item = item;
00042         setPixmap( 0, m_item.pixmap() );
00043         setText  ( 1, m_item.text() );
00044         m_item.keyPair().isEmpty() ? setText( 2, QObject::tr( "None" ) ) :
00045                                      setText( 2, keyToString( m_item.keyPair() ) );
00046 
00047         m_item.defaultKeyPair().isEmpty() ? setText( 3, QObject::tr( "None" ) ) :
00048                                             setText  ( 3, keyToString( m_item.defaultKeyPair() ) );
00049     }
00050     void OKeyListViewItem::updateText() {
00051         m_item.keyPair().isEmpty() ? setText( 2, QObject::tr( "None" ) ) :
00052                                      setText( 2, keyToString( m_item.keyPair() ) );
00053     }
00054 
00055     QString keyToString( const OKeyPair& pair ) {
00056         int mod = 0;
00057         if ( pair.modifier() & Qt::ShiftButton )
00058             mod |= Qt::SHIFT;
00059         if ( pair.modifier() & Qt::ControlButton )
00060             mod |= Qt::CTRL;
00061         if ( pair.modifier() & Qt::AltButton )
00062             mod |= Qt::ALT;
00063 
00064         return QAccel::keyToString( mod + pair.keycode() );
00065     }
00066 
00067     struct OKeyConfigWidgetPrivate{
00068         OKeyConfigWidgetPrivate(const QString& = QString::null,
00069                                 OKeyConfigManager* = 0);
00070         bool operator==( const OKeyConfigWidgetPrivate& );
00071         QString name;
00072         OKeyConfigManager *manager;
00073     };
00074 
00075     OKeyConfigWidgetPrivate::OKeyConfigWidgetPrivate( const QString& _name,
00076                                                       OKeyConfigManager* man )
00077         : name( _name ), manager( man ){}
00078 
00079     bool OKeyConfigWidgetPrivate::operator==( const OKeyConfigWidgetPrivate& item) {
00080         if ( manager != item.manager) return false;
00081         if ( name    !=  item.name  ) return false;
00082 
00083         return true;
00084     }
00085 
00086 }
00087 
00088 
00089 
00090 
00094 
00095 
00096 
00097 
00105 OKeyConfigWidget::OKeyConfigWidget( QWidget* parent, const char *name, WFlags fl )
00106     : QWidget( parent, name, fl ) {
00107     initUi();
00108 }
00109 
00110 
00111 
00115 OKeyConfigWidget::~OKeyConfigWidget() {
00116 }
00117 
00118 
00122 void OKeyConfigWidget::initUi() {
00123     QBoxLayout *layout   = new QVBoxLayout( this );
00124     QGridLayout *gridLay = new QGridLayout( 2, 2 );
00125     layout->addLayout( gridLay, 10 );
00126     gridLay->setRowStretch( 1, 10 ); // let only the ListView strecth
00127 
00128 /*
00129  * LISTVIEW with the Keys
00130  */
00131     m_view = new Opie::Ui::OListView( this );
00132     m_view->setFocus();
00133     m_view->setAllColumnsShowFocus( true );
00134     m_view->addColumn( tr("Pixmap") );
00135     m_view->addColumn( tr("Name","Name of the Action in the ListView Header" ) );
00136     m_view->addColumn( tr("Key" ) );
00137     m_view->addColumn( tr("Default Key" ) );
00138     m_view->setRootIsDecorated( true );
00139     connect(m_view, SIGNAL(currentChanged(QListViewItem*)),
00140             this, SLOT(slotListViewItem(QListViewItem*)) );
00141 
00142     gridLay->addMultiCellWidget( m_view, 1, 1, 0, 1 );
00143 
00144 /*
00145  * GROUP with button info
00146  */
00147 
00148     QGroupBox *box = new QGroupBox( this );
00149     box ->setTitle(  tr("Shortcut for Selected Action") );
00150     box ->setFrameStyle( QFrame::Box | QFrame::Sunken );
00151     layout->addWidget( box, 1 );
00152 
00153     gridLay = new QGridLayout( box, 3, 4 );
00154     gridLay->addRowSpacing( 0, fontMetrics().lineSpacing() );
00155     gridLay->setMargin( 4 );
00156 
00157     QButtonGroup *gr = new QButtonGroup( box );
00158     gr->hide();
00159     gr->setExclusive( true );
00160 
00161     QRadioButton *rad = new QRadioButton( tr( "&None" ), box );
00162     connect( rad, SIGNAL(clicked()),
00163              this, SLOT(slotNoKey()) );
00164     gr->insert( rad, 10 );
00165     gridLay->addWidget( rad, 1, 0 );
00166     m_none = rad;
00167 
00168     rad = new QRadioButton( tr("&Default" ), box );
00169     connect( rad, SIGNAL(clicked()),
00170              this, SLOT(slotDefaultKey()) );
00171     gr->insert( rad, 11 );
00172     gridLay->addWidget( rad, 1, 1 );
00173     m_def = rad;
00174 
00175     rad = new QRadioButton( tr("C&ustom"), box );
00176     connect( rad, SIGNAL(clicked()),
00177              this, SLOT(slotCustomKey()) );
00178     gr->insert( rad, 12 );
00179     gridLay->addWidget( rad, 1, 2 );
00180     m_cus = rad;
00181 
00182     m_btn = new QPushButton( tr("Configure Key"), box );
00183     gridLay->addWidget( m_btn, 1, 3 );
00184 
00185     m_lbl= new QLabel( tr( "Default: " ), box );
00186     gridLay->addMultiCellWidget( m_lbl, 2, 2, 0, 3 );
00187 
00188     connect(m_btn, SIGNAL(clicked()),
00189             this, SLOT(slotConfigure()));
00190 
00191     m_box = box;
00192 }
00193 
00200 void OKeyConfigWidget::setChangeMode( enum ChangeMode mode) {
00201     m_mode = mode;
00202 }
00203 
00204 
00208 OKeyConfigWidget::ChangeMode OKeyConfigWidget::changeMode()const {
00209     return m_mode;
00210 }
00211 
00212 
00216 void OKeyConfigWidget::insert( const QString& str, OKeyConfigManager* man ) {
00217     Opie::Ui::Internal::OKeyConfigWidgetPrivate root( str, man );
00218     m_list.append(root);
00219 }
00220 
00221 
00225 void OKeyConfigWidget::load() {
00226     Opie::Ui::Internal::OKeyConfigWidgetPrivateList::Iterator it;
00227     for ( it = m_list.begin(); it != m_list.end(); ++it ) {
00228         OListViewItem *item = new OListViewItem( m_view, (*it).name );
00229         OKeyConfigItem::List list = (*it).manager->keyConfigList();
00230         for (OKeyConfigItem::List::Iterator keyIt = list.begin(); keyIt != list.end();++keyIt )
00231             (void )new  Opie::Ui::Internal::OKeyListViewItem(*keyIt, (*it).manager, item );
00232 
00233     }
00234 }
00235 
00241 void OKeyConfigWidget::save() {
00242     /*
00243      * Iterate over all config items
00244      */
00245     QListViewItemIterator it( m_view );
00246     while ( it.current() ) {
00247         if (it.current()->parent() ) {
00248             Opie::Ui::Internal::OKeyListViewItem *item = static_cast<Opie::Ui::Internal::OKeyListViewItem*>( it.current() );
00249             OKeyConfigManager *man = item->manager();
00250             man->removeKeyConfig( item->origItem() );
00251             man->addKeyConfig( item->item() );
00252         }
00253         ++it;
00254     }
00255 
00256 
00257 }
00258 
00259 
00263 void OKeyConfigWidget::slotListViewItem( QListViewItem* _item) {
00264     if ( !_item || !_item->parent() ) {
00265         m_box->setEnabled( false );
00266         m_none->setChecked( true );
00267         m_btn ->setEnabled( false );
00268         m_def ->setChecked( false );
00269         m_cus ->setChecked( false );
00270     }else{
00271         m_box->setEnabled( true );
00272         Opie::Ui::Internal::OKeyListViewItem *item = static_cast<Opie::Ui::Internal::OKeyListViewItem*>( _item );
00273         OKeyConfigItem keyItem= item->item();
00274         m_lbl->setText(  tr("Default: " )+ item->text( 3 ) );
00275         if ( keyItem.keyPair().isEmpty() ) {
00276             m_none->setChecked( true );
00277             m_btn ->setEnabled( false );
00278             m_def ->setChecked( false );
00279             m_cus ->setChecked( false );
00280         }else {
00281             m_none->setChecked( false );
00282             m_cus ->setChecked( true );
00283             m_btn ->setEnabled( true );
00284             m_def ->setChecked( false );
00285         }
00286     }
00287 }
00288 
00289 void OKeyConfigWidget::slotNoKey() {
00290     m_none->setChecked( true );
00291     m_cus ->setChecked( false );
00292     m_btn ->setEnabled( false );
00293     m_def ->setChecked( false );
00294 
00295     if ( !m_view->currentItem() || !m_view->currentItem()->parent() )
00296         return;
00297 
00298 
00299 
00300     /*
00301      * If immediate we need to remove and readd the key
00302      */
00303     Opie::Ui::Internal::OKeyListViewItem *item =  static_cast<Opie::Ui::Internal::OKeyListViewItem*>(m_view->currentItem());
00304     updateItem( item, OKeyPair::emptyKey() );
00305 }
00306 
00307 void OKeyConfigWidget::slotDefaultKey() {
00308     m_none->setChecked( false );
00309     m_cus ->setChecked( false );
00310     m_btn ->setEnabled( false );
00311     m_def ->setChecked( true );
00312 
00313     if ( !m_view->currentItem() || !m_view->currentItem()->parent() )
00314         return;
00315 
00316     Opie::Ui::Internal::OKeyListViewItem *item =  static_cast<Opie::Ui::Internal::OKeyListViewItem*>(m_view->currentItem());
00317     updateItem( item, item->item().defaultKeyPair() );
00318 }
00319 
00320 void OKeyConfigWidget::slotCustomKey() {
00321     m_cus ->setChecked( true );
00322     m_btn ->setEnabled( true );
00323     m_def ->setChecked( false );
00324     m_none->setChecked( false );
00325 
00326     if ( !m_view->currentItem() || !m_view->currentItem()->parent() )
00327         return;
00328 
00329 
00330 }
00331 
00332 void OKeyConfigWidget::slotConfigure() {
00333     if ( !m_view->currentItem() || !m_view->currentItem()->parent() )
00334         return;
00335 
00336    /* FIXME make use of OModalHelper */
00337     OKeyChooserConfigDialog dlg( this, "Dialog Name", true );
00338     dlg.setCaption(tr("Configure Key"));
00339     connect(&dlg, SIGNAL(keyCaptured()), &dlg, SLOT(accept()) );
00340 
00341     if ( QPEApplication::execDialog( &dlg ) == QDialog::Accepted ) {
00342         Opie::Ui::Internal::OKeyListViewItem *item = static_cast<Opie::Ui::Internal::OKeyListViewItem*>(m_view->currentItem());
00343         updateItem( item, dlg.keyPair() );
00344     }
00345 
00346 
00347 }
00348 
00349 bool OKeyConfigWidget::sanityCheck(  Opie::Ui::Internal::OKeyListViewItem* item,
00350                                      const OKeyPair& newItem ) {
00351     OKeyPair::List bList = item->manager()->blackList();
00352     for ( OKeyPair::List::Iterator it = bList.begin(); it != bList.end(); ++it ) {
00353         /* black list matched */
00354         if ( *it == newItem ) {
00355             QMessageBox::warning( 0, tr("Key is on BlackList" ),
00356                                   tr("<qt>The Key you choose is on the black list "
00357                                      "and may not be used with this manager. Please "
00358                                      "use a different key.</qt>" ) );
00359             return false;
00360         }
00361     }
00362     /* no we need to check the other items which is dog slow */
00363     QListViewItemIterator it( item->parent() );
00364     while ( it.current() ) {
00365         /* if not our parent and not us */
00366         if (it.current()->parent() &&  it.current() != item) {
00367             /* damn already given away*/
00368             if ( newItem == static_cast<Opie::Ui::Internal::OKeyListViewItem*>(it.current() )->item().keyPair() ) {
00369                 QMessageBox::warning( 0, tr("Key is already assigned" ),
00370                                       tr("<qt>The Key you choose is already taken by "
00371                                          "a different Item of your config. Please try"
00372                                          "using a different key.</qt>" ) );
00373                 return false;
00374             }
00375         }
00376         ++it;
00377     }
00378 
00379     return true;
00380 }
00381 
00382 void OKeyConfigWidget::updateItem( Opie::Ui::Internal::OKeyListViewItem *item,
00383                                    const OKeyPair& newItem) {
00384     /* sanity check
00385      * check against the blacklist of the manager
00386      * check if another item uses this key which is o(n) at least
00387      */
00388     if ( !newItem.isEmpty() && !sanityCheck(item, newItem ))
00389         return;
00390 
00391 
00392 
00393     /*
00394     * If immediate we need to remove and readd the key
00395     */
00396     if ( m_mode == Imediate )
00397         item->manager()->removeKeyConfig( item->item() );
00398 
00399     item->item().setKeyPair( newItem );
00400     item->updateText();
00401 
00402     if ( m_mode == Imediate )
00403         item->manager()->addKeyConfig( item->item() );
00404 }
00405 
00406 
00407 
00409 OKeyChooserConfigDialog::OKeyChooserConfigDialog( QWidget* par, const char* nam,
00410                                                   bool mod, WFlags fl )
00411     : QDialog( par, nam, mod, fl ), m_virtKey( false ), m_keyPair( OKeyPair::emptyKey() ) ,
00412       m_key( 0 ), m_mod( 0 ) {
00413     setFocusPolicy( StrongFocus );
00414 
00415     QHBoxLayout *lay = new QHBoxLayout( this );
00416 
00417     QLabel *lbl = new QLabel( tr("Configure Key" ), this );
00418     lay->addWidget( lbl );
00419     lbl->setFocusPolicy( NoFocus );
00420 
00421     m_lbl = new QLabel( this );
00422     lay->addWidget( m_lbl );
00423     m_lbl->setFocusPolicy( NoFocus );
00424 
00425     m_timer = new QTimer( this );
00426     connect(m_timer, SIGNAL(timeout()),
00427             this, SLOT(slotTimeUp()) );
00428 }
00429 
00430 OKeyChooserConfigDialog::~OKeyChooserConfigDialog() {
00431 }
00432 
00433 Opie::Core::OKeyPair OKeyChooserConfigDialog::keyPair()const{
00434     return m_keyPair;
00435 }
00436 
00437 void OKeyChooserConfigDialog::keyPressEvent( QKeyEvent* ev ) {
00438     QDialog::keyPressEvent( ev );
00439 
00440     if ( ev->isAutoRepeat() )
00441         return;
00442 
00443     int mod, key;
00444     Opie::Core::Internal::fixupKeys( key,mod, ev );
00445 
00446     /* either we used software keyboard
00447      * or we've true support
00448      */
00449     if ( !m_virtKey && !ev->key()) {
00450         m_virtKey = true;
00451         m_keyPair = OKeyPair( key, mod );
00452     }else{
00453         mod = 0;
00454         switch( key ) {
00455         case Qt::Key_Control:
00456             mod = Qt::ControlButton;
00457             break;
00458         case Qt::Key_Shift:
00459             mod = Qt::ShiftButton;
00460             break;
00461         case Qt::Key_Alt:
00462             mod = Qt::AltButton;
00463             break;
00464         default:
00465             break;
00466         }
00467         if (mod ) {
00468             m_mod |= mod;
00469             key = 0;
00470         }else
00471             m_key = key;
00472 
00473         if ( ( !mod || m_key || key ) && !m_timer->isActive() )
00474             m_timer->start( 150, true );
00475 
00476         m_keyPair = OKeyPair( m_key, m_mod );
00477     }
00478 
00479     m_lbl->setText( Opie::Ui::Internal::keyToString( m_keyPair ) );
00480 
00481 }
00482 
00483 void OKeyChooserConfigDialog::keyReleaseEvent( QKeyEvent* ev ) {
00484     m_timer->stop();
00485     QDialog::keyPressEvent( ev );
00486 
00487     if ( ev->isAutoRepeat() )
00488         return;
00489 
00490 
00491     if ( m_virtKey && !ev->key()) {
00492         m_virtKey = false;
00493         slotTimeUp();
00494     }else {
00495         int mod = 0;
00496         int key = ev->key();
00497         switch( key ) {
00498         case Qt::Key_Control:
00499             mod = Qt::ControlButton;
00500             break;
00501         case Qt::Key_Shift:
00502             mod = Qt::ShiftButton;
00503             break;
00504         case Qt::Key_Alt:
00505             mod = Qt::AltButton;
00506             break;
00507         default:
00508             break;
00509         }
00510         if (mod )
00511             m_mod &= ~mod;
00512         else
00513             m_key = key;
00514        m_keyPair = OKeyPair( m_key, m_mod );
00515        m_lbl->setText( Opie::Ui::Internal::keyToString( m_keyPair ) );
00516     }
00517 }
00518 
00519 
00520 void OKeyChooserConfigDialog::slotTimeUp() {
00521     m_mod = m_key = 0;
00522     QTimer::singleShot(0, this, SIGNAL(keyCaptured()) );
00523 }
00524 
00525 
00526 }
00527 }

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