kdeui Library API Documentation

kkeydialog.cpp

00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1998 Mark Donohoe <donohoe@kde.org>
00003     Copyright (C) 1997 Nicolas Hadacek <hadacek@kde.org>
00004     Copyright (C) 1998 Matthias Ettrich <ettrich@kde.org>
00005     Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020     Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include "kkeydialog.h"
00024 #include "kkeybutton.h"
00025 
00026 #include <string.h>
00027 
00028 #include <qbuttongroup.h>
00029 #include <qlabel.h>
00030 #include <qlayout.h>
00031 #include <qdrawutil.h>
00032 #include <qpainter.h>
00033 #include <qradiobutton.h>
00034 #include <qregexp.h>
00035 #include <qwhatsthis.h>
00036 
00037 #include <kaccel.h>
00038 #include <kaction.h>
00039 #include <kaccelaction.h>
00040 #include <kactionshortcutlist.h>
00041 #include <kapplication.h>
00042 #include <kconfig.h>
00043 #include <kdebug.h>
00044 #include <kglobal.h>
00045 #include <kglobalaccel.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 #include <kshortcut.h>
00049 #include <kshortcutlist.h>
00050 #include <kxmlguifactory.h>
00051 #include <kaboutdata.h>
00052 #include <kstaticdeleter.h>
00053 
00054 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00055 #define XK_XKB_KEYS
00056 #define XK_MISCELLANY
00057 #include <X11/Xlib.h>   // For x11Event() // schroder
00058 #include <X11/keysymdef.h> // For XK_... // schroder
00059 
00060 #ifdef KeyPress
00061 const int XFocusOut = FocusOut;
00062 const int XFocusIn = FocusIn;
00063 const int XKeyPress = KeyPress;
00064 const int XKeyRelease = KeyRelease;
00065 #undef KeyRelease
00066 #undef KeyPress
00067 #undef FocusOut
00068 #undef FocusIn
00069 #endif // KEYPRESS
00070 #endif // Q_WX_X11 && ! K_WS_QTONLY
00071 
00072 //---------------------------------------------------------------------
00073 // KKeyChooserItem
00074 //---------------------------------------------------------------------
00075 
00076 class KKeyChooserItem : public KListViewItem
00077 {
00078  public:
00079     KKeyChooserItem( KListView* parent, QListViewItem* after, KShortcutList* pList, uint iAction );
00080     KKeyChooserItem( QListViewItem* parent, QListViewItem* after, KShortcutList* pList, uint iAction );
00081 
00082     QString actionName() const;
00083     const KShortcut& shortcut() const;
00084     bool isConfigurable() const
00085         { return m_pList->isConfigurable( m_iAction ); }
00086     const KShortcut& shortcutDefault() const
00087         { return m_pList->shortcutDefault( m_iAction ); }
00088 
00089     void setShortcut( const KShortcut& cut );
00090     void commitChanges();
00091 
00092     virtual QString text( int iCol ) const;
00093     virtual int compare( QListViewItem*, int iCol, bool bAscending ) const;
00094 
00095  protected:
00096     KShortcutList* m_pList;
00097     uint m_iAction;
00098     bool m_bModified;
00099     KShortcut m_cut;
00100 };
00101 
00102 //---------------------------------------------------------------------
00103 // KKeyChooserPrivate
00104 //---------------------------------------------------------------------
00105 
00106 class KKeyChooserPrivate
00107 {
00108  public:
00109     QValueList<KShortcutList*> rgpLists;
00110     QValueList<KShortcutList*> rgpListsAllocated;
00111 
00112     KListView *pList;
00113     QLabel *lInfo;
00114     KKeyButton *pbtnShortcut;
00115     QGroupBox *fCArea;
00116     QButtonGroup *kbGroup;
00117 
00118     QMap<QString, KShortcut> mapGlobals;
00119 
00120     // If this is set, then shortcuts require a modifier:
00121     //  so 'A' would not be valid, whereas 'Ctrl+A' would be.
00122     // Note, however, that this only applies to printable characters.
00123     //  'F1', 'Insert', etc., could still be used.
00124     bool bAllowLetterShortcuts;
00125     // When set, pressing the 'Default' button will select the aDefaultKeycode4,
00126     //  otherwise aDefaultKeycode.
00127     bool bPreferFourModifierKeys;
00128 };
00129 
00130 //---------------------------------------------------------------------
00131 // KKeyChooser
00132 //---------------------------------------------------------------------
00133 
00134 KKeyChooser::KKeyChooser( QWidget* parent, ActionType type, bool bAllowLetterShortcuts )
00135 : QWidget( parent )
00136 {
00137     initGUI( type, bAllowLetterShortcuts );
00138 }
00139 
00140 KKeyChooser::KKeyChooser( KActionCollection* coll, QWidget* parent, bool bAllowLetterShortcuts )
00141 : QWidget( parent )
00142 {
00143     initGUI( Application, bAllowLetterShortcuts );
00144     insert( coll );
00145 }
00146 
00147 KKeyChooser::KKeyChooser( KAccel* pAccel, QWidget* parent, bool bAllowLetterShortcuts )
00148 : QWidget( parent )
00149 {
00150     initGUI( Application, bAllowLetterShortcuts );
00151     insert( pAccel );
00152 }
00153 
00154 KKeyChooser::KKeyChooser( KGlobalAccel* pAccel, QWidget* parent )
00155 : QWidget( parent )
00156 {
00157     initGUI( ApplicationGlobal, false );
00158     insert( pAccel );
00159 }
00160 
00161 KKeyChooser::KKeyChooser( KShortcutList* pList, QWidget* parent, ActionType type, bool bAllowLetterShortcuts )
00162 : QWidget( parent )
00163 {
00164     initGUI( type, bAllowLetterShortcuts );
00165     insert( pList );
00166 }
00167 
00168 KKeyChooser::KKeyChooser( KAccel* actions, QWidget* parent,
00169             bool bCheckAgainstStdKeys,
00170             bool bAllowLetterShortcuts,
00171             bool bAllowWinKey )
00172 : QWidget( parent )
00173 {
00174     ActionType type;
00175     if( bAllowWinKey )
00176         type = (bCheckAgainstStdKeys) ? ApplicationGlobal : Global;
00177     else
00178         type = Application;
00179 
00180     initGUI( type, bAllowLetterShortcuts );
00181     insert( actions );
00182 }
00183 
00184 KKeyChooser::KKeyChooser( KGlobalAccel* actions, QWidget* parent,
00185             bool bCheckAgainstStdKeys,
00186             bool bAllowLetterShortcuts,
00187             bool /*bAllowWinKey*/ )
00188 : QWidget( parent )
00189 {
00190     ActionType type = (bCheckAgainstStdKeys) ? ApplicationGlobal : Global;
00191 
00192     initGUI( type, bAllowLetterShortcuts );
00193     insert( actions );
00194 }
00195 
00196 // list of all existing KKeyChooser's
00197 // Used when checking global shortcut for a possible conflict
00198 // (just checking against kdeglobals isn't enough, the shortcuts
00199 // might have changed in KKeyChooser and not being saved yet).
00200 // Also used when reassigning a shortcut from one chooser to another.
00201 static QValueList< KKeyChooser* >* allChoosers = NULL;
00202 static KStaticDeleter< QValueList< KKeyChooser* > > allChoosersDeleter;
00203 
00204 KKeyChooser::~KKeyChooser()
00205 {
00206         allChoosers->remove( this );
00207     // Delete allocated KShortcutLists
00208     for( uint i = 0; i < d->rgpListsAllocated.count(); i++ )
00209         delete d->rgpListsAllocated[i];
00210     delete d;
00211 }
00212 
00213 bool KKeyChooser::insert( KActionCollection *pColl)
00214 {
00215     return insert(pColl, QString::null);
00216 }
00217 
00218 bool KKeyChooser::insert( KActionCollection* pColl, const QString &title )
00219 {
00220     QString str = title;
00221     if ( title.isEmpty() && pColl->instance()
00222          && pColl->instance()->aboutData() )
00223         str = pColl->instance()->aboutData()->programName();
00224 
00225     KShortcutList* pList = new KActionShortcutList( pColl );
00226     d->rgpListsAllocated.append( pList );
00227     d->rgpLists.append( pList );
00228     buildListView(d->rgpLists.count() - 1, str);
00229     return true;
00230 }
00231 
00232 bool KKeyChooser::insert( KAccel* pAccel )
00233 {
00234     KShortcutList* pList = new KAccelShortcutList( pAccel );
00235     d->rgpListsAllocated.append( pList );
00236     return insert( pList );
00237 }
00238 
00239 bool KKeyChooser::insert( KGlobalAccel* pAccel )
00240 {
00241     KShortcutList* pList = new KAccelShortcutList( pAccel );
00242     d->rgpListsAllocated.append( pList );
00243     return insert( pList );
00244 }
00245 
00246 bool KKeyChooser::insert( KShortcutList* pList )
00247 {
00248     d->rgpLists.append( pList );
00249     buildListView( d->rgpLists.count() - 1, QString::null );
00250     return true;
00251 }
00252 
00253 void KKeyChooser::commitChanges()
00254 {
00255     kdDebug(125) << "KKeyChooser::commitChanges()" << endl;
00256 
00257     QListViewItemIterator it( d->pList );
00258     for( ; it.current(); ++it ) {
00259         KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>(it.current());
00260         if( pItem )
00261             pItem->commitChanges();
00262     }
00263 }
00264 
00265 void KKeyChooser::save()
00266 {
00267     commitChanges();
00268     for( uint i = 0; i < d->rgpLists.count(); i++ )
00269         d->rgpLists[i]->save();
00270 }
00271 
00272 void KKeyChooser::initGUI( ActionType type, bool bAllowLetterShortcuts )
00273 {
00274   d = new KKeyChooserPrivate();
00275 
00276   m_type = type;
00277   d->bAllowLetterShortcuts = bAllowLetterShortcuts;
00278 
00279   d->bPreferFourModifierKeys = KGlobalAccel::useFourModifierKeys();
00280 
00281   //
00282   // TOP LAYOUT MANAGER
00283   //
00284   // The following layout is used for the dialog
00285   //            LIST LABELS LAYOUT
00286   //            SPLIT LIST BOX WIDGET
00287   //            CHOOSE KEY GROUP BOX WIDGET
00288   //            BUTTONS LAYOUT
00289   // Items are added to topLayout as they are created.
00290   //
00291 
00292   QBoxLayout *topLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00293 
00294   QGridLayout *stackLayout = new QGridLayout(2, 2, 2);
00295   topLayout->addLayout( stackLayout, 10 );
00296   stackLayout->setRowStretch( 1, 10 ); // Only list will stretch
00297 
00298   //
00299   // CREATE SPLIT LIST BOX
00300   //
00301   // fill up the split list box with the action/key pairs.
00302   //
00303   d->pList = new KListView( this );
00304   d->pList->setFocus();
00305 
00306   stackLayout->addMultiCellWidget( d->pList, 1, 1, 0, 1 );
00307   QString wtstr = i18n("Here you can see a list of key bindings, "
00308                        "i.e. associations between actions (e.g. 'Copy') "
00309                        "shown in the left column and keys or combination "
00310                        "of keys (e.g. Ctrl+V) shown in the right column.");
00311 
00312   QWhatsThis::add( d->pList, wtstr );
00313 
00314   d->pList->setAllColumnsShowFocus( true );
00315   d->pList->addColumn(i18n("Action"));
00316   d->pList->addColumn(i18n("Shortcut"));
00317   d->pList->addColumn(i18n("Alternate"));
00318 
00319   connect( d->pList, SIGNAL(currentChanged(QListViewItem*)),
00320            SLOT(slotListItemSelected(QListViewItem*)) );
00321 
00322   // handle double clicking an item
00323   connect ( d->pList, SIGNAL ( doubleClicked ( QListViewItem *, const QPoint &, int ) ),
00324                        SLOT ( captureCurrentItem()) );
00325   connect ( d->pList, SIGNAL ( spacePressed( QListViewItem* )), SLOT( captureCurrentItem()));
00326   //
00327   // CREATE CHOOSE KEY GROUP
00328   //
00329   d->fCArea = new QGroupBox( this );
00330   topLayout->addWidget( d->fCArea, 1 );
00331 
00332   d->fCArea->setTitle( i18n("Shortcut for Selected Action") );
00333   d->fCArea->setFrameStyle( QFrame::Box | QFrame::Sunken );
00334 
00335   //
00336   // CHOOSE KEY GROUP LAYOUT MANAGER
00337   //
00338   QGridLayout *grid = new QGridLayout( d->fCArea, 3, 4, KDialog::spacingHint() );
00339   grid->addRowSpacing( 0, fontMetrics().lineSpacing() );
00340 
00341   d->kbGroup = new QButtonGroup( d->fCArea );
00342   d->kbGroup->hide();
00343   d->kbGroup->setExclusive( true );
00344 
00345   m_prbNone = new QRadioButton( i18n("no key", "&None"), d->fCArea );
00346   d->kbGroup->insert( m_prbNone, NoKey );
00347   m_prbNone->setEnabled( false );
00348   //grid->addMultiCellWidget( rb, 1, 1, 1, 2 );
00349   grid->addWidget( m_prbNone, 1, 0 );
00350   QWhatsThis::add( m_prbNone, i18n("The selected action will not be associated with any key.") );
00351   connect( m_prbNone, SIGNAL(clicked()), SLOT(slotNoKey()) );
00352 
00353   m_prbDef = new QRadioButton( i18n("default key", "De&fault"), d->fCArea );
00354   d->kbGroup->insert( m_prbDef, DefaultKey );
00355   m_prbDef->setEnabled( false );
00356   //grid->addMultiCellWidget( rb, 2, 2, 1, 2 );
00357   grid->addWidget( m_prbDef, 1, 1 );
00358   QWhatsThis::add( m_prbDef, i18n("This will bind the default key to the selected action. Usually a reasonable choice.") );
00359   connect( m_prbDef, SIGNAL(clicked()), SLOT(slotDefaultKey()) );
00360 
00361   m_prbCustom = new QRadioButton( i18n("C&ustom"), d->fCArea );
00362   d->kbGroup->insert( m_prbCustom, CustomKey );
00363   m_prbCustom->setEnabled( false );
00364   //grid->addMultiCellWidget( rb, 3, 3, 1, 2 );
00365   grid->addWidget( m_prbCustom, 1, 2 );
00366   QWhatsThis::add( m_prbCustom, i18n("If this option is selected you can create a customized key binding for the"
00367     " selected action using the buttons below.") );
00368   connect( m_prbCustom, SIGNAL(clicked()), SLOT(slotCustomKey()) );
00369 
00370   //connect( d->kbGroup, SIGNAL( clicked( int ) ), SLOT( keyMode( int ) ) );
00371 
00372   QBoxLayout *pushLayout = new QHBoxLayout( KDialog::spacingHint() );
00373   grid->addLayout( pushLayout, 1, 3 );
00374 
00375   d->pbtnShortcut = new KKeyButton(d->fCArea, "key");
00376   d->pbtnShortcut->setEnabled( false );
00377   connect( d->pbtnShortcut, SIGNAL(capturedShortcut(const KShortcut&)), SLOT(capturedShortcut(const KShortcut&)) );
00378   grid->addRowSpacing( 1, d->pbtnShortcut->sizeHint().height() + 5 );
00379 
00380   wtstr = i18n("Use this button to choose a new shortcut key. Once you click it, "
00381         "you can press the key-combination which you would like to be assigned "
00382         "to the currently selected action.");
00383   QWhatsThis::add( d->pbtnShortcut, wtstr );
00384 
00385   //
00386   // Add widgets to the geometry manager
00387   //
00388   pushLayout->addSpacing( KDialog::spacingHint()*2 );
00389   pushLayout->addWidget( d->pbtnShortcut );
00390   pushLayout->addStretch( 10 );
00391 
00392   d->lInfo = new QLabel(d->fCArea);
00393   //resize(0,0);
00394   //d->lInfo->setAlignment( AlignCenter );
00395   //d->lInfo->setEnabled( false );
00396   //d->lInfo->hide();
00397   grid->addMultiCellWidget( d->lInfo, 2, 2, 0, 3 );
00398 
00399   //d->globalDict = new QDict<int> ( 100, false );
00400   //d->globalDict->setAutoDelete( true );
00401   readGlobalKeys();
00402   //d->stdDict = new QDict<int> ( 100, false );
00403   //d->stdDict->setAutoDelete( true );
00404   //if (type == Application || type == ApplicationGlobal)
00405   //  readStdKeys();
00406   connect( kapp, SIGNAL( settingsChanged( int )), SLOT( slotSettingsChanged( int )));
00407   if( allChoosers == NULL )
00408         allChoosers = allChoosersDeleter.setObject( allChoosers, new QValueList< KKeyChooser* > );
00409   allChoosers->append( this );
00410 }
00411 
00412 // Add all shortcuts to the list
00413 void KKeyChooser::buildListView( uint iList, const QString &title )
00414 {
00415     KShortcutList* pList = d->rgpLists[iList];
00416 
00417         if( m_type == Global || m_type == ApplicationGlobal )
00418         d->pList->setSorting( -1 );
00419     KListViewItem *pProgramItem, *pGroupItem = 0, *pParentItem, *pItem;
00420 
00421     QString str = (title.isEmpty() ? i18n("Shortcuts") : title);
00422     pParentItem = pProgramItem = pItem = new KListViewItem( d->pList, str );
00423     pParentItem->setExpandable( true );
00424     pParentItem->setOpen( true );
00425     pParentItem->setSelectable( false );
00426     uint nSize = pList->count();
00427     for( uint iAction = 0; iAction < nSize; iAction++ ) {
00428         QString sName = pList->name(iAction);
00429         kdDebug(125) << "Key: " << sName << endl;
00430         if( sName.startsWith( "Program:" ) ) {
00431             pItem = new KListViewItem( d->pList, pProgramItem, pList->label(iAction) );
00432             pItem->setSelectable( false );
00433             pItem->setExpandable( true );
00434             pItem->setOpen( true );
00435             if( !pProgramItem->firstChild() )
00436                 delete pProgramItem;
00437             pProgramItem = pParentItem = pItem;
00438         } else if( sName.startsWith( "Group:" ) ) {
00439             pItem = new KListViewItem( pProgramItem, pParentItem, pList->label(iAction) );
00440             pItem->setSelectable( false );
00441             pItem->setExpandable( true );
00442             pItem->setOpen( true );
00443             if( pGroupItem && !pGroupItem->firstChild() )
00444                 delete pGroupItem;
00445             pGroupItem = pParentItem = pItem;
00446         } else if( !sName.isEmpty() && pList->isConfigurable(iAction) )
00447             pItem = new KKeyChooserItem( pParentItem, pItem, pList, iAction );
00448     }
00449     if( !pProgramItem->firstChild() )
00450         delete pProgramItem;
00451     if( pGroupItem && !pGroupItem->firstChild() )
00452         delete pGroupItem;
00453 }
00454 
00455 void KKeyChooser::updateButtons()
00456 {
00457     // Hack: Do this incase we still have changeKey() running.
00458     //  Better would be to capture the mouse pointer so that we can't click
00459     //   around while we're supposed to be entering a key.
00460     //  Better yet would be a modal dialog for changeKey()!
00461     releaseKeyboard();
00462     KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>( d->pList->currentItem() );
00463 
00464     if ( !pItem ) {
00465         // if nothing is selected -> disable radio boxes
00466         m_prbNone->setEnabled( false );
00467         m_prbDef->setEnabled( false );
00468         m_prbCustom->setEnabled( false );
00469         d->pbtnShortcut->setEnabled( false );
00470         d->pbtnShortcut->setShortcut( KShortcut(), false );
00471     } else {
00472         bool bConfigurable = pItem->isConfigurable();
00473         bool bQtShortcut = (m_type == Application || m_type == Standard);
00474         const KShortcut& cutDef = pItem->shortcutDefault();
00475 
00476         // Set key strings
00477         QString keyStrCfg = pItem->shortcut().toString();
00478         QString keyStrDef = cutDef.toString();
00479 
00480         d->pbtnShortcut->setShortcut( pItem->shortcut(), bQtShortcut );
00481         //item->setText( 1, keyStrCfg );
00482         pItem->repaint();
00483         d->lInfo->setText( i18n("Default key:") + QString(" %1").arg(keyStrDef.isEmpty() ? i18n("None") : keyStrDef) );
00484 
00485         // Select the appropriate radio button.
00486         int index = (pItem->shortcut().isNull()) ? NoKey
00487                 : (pItem->shortcut() == cutDef) ? DefaultKey
00488                 : CustomKey;
00489         m_prbNone->setChecked( index == NoKey );
00490         m_prbDef->setChecked( index == DefaultKey );
00491         m_prbCustom->setChecked( index == CustomKey );
00492 
00493         // Enable buttons if this key is configurable.
00494         // The 'Default Key' button must also have a default key.
00495         m_prbNone->setEnabled( bConfigurable );
00496         m_prbDef->setEnabled( bConfigurable && cutDef.count() != 0 );
00497         m_prbCustom->setEnabled( bConfigurable );
00498         d->pbtnShortcut->setEnabled( bConfigurable );
00499     }
00500 }
00501 
00502 void KKeyChooser::slotNoKey()
00503 {
00504     // return if no key is selected
00505     KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>( d->pList->currentItem() );
00506     if( pItem ) {
00507         //kdDebug(125) << "no Key" << d->pList->currentItem()->text(0) << endl;
00508         pItem->setShortcut( KShortcut() );
00509         updateButtons();
00510         emit keyChange();
00511     }
00512 }
00513 
00514 void KKeyChooser::slotDefaultKey()
00515 {
00516     // return if no key is selected
00517     KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>( d->pList->currentItem() );
00518     if( pItem ) // don't set it directly, check for conflicts
00519         setShortcut( pItem->shortcutDefault() );
00520 }
00521 
00522 void KKeyChooser::slotCustomKey()
00523 {
00524     d->pbtnShortcut->captureShortcut();
00525 }
00526 
00527 void KKeyChooser::readGlobalKeys()
00528 {
00529         d->mapGlobals.clear();
00530         if( m_type == Global )
00531             return; // they will be checked normally, because we're configuring them
00532         readGlobalKeys( d->mapGlobals );
00533 }
00534 
00535 void KKeyChooser::readGlobalKeys( QMap< QString, KShortcut >& map )
00536 {
00537     QMap<QString, QString> mapEntry = KGlobal::config()->entryMap( "Global Shortcuts" );
00538     QMap<QString, QString>::Iterator it( mapEntry.begin() );
00539     for( uint i = 0; it != mapEntry.end(); ++it, i++ )
00540         map[it.key()] = KShortcut(*it);
00541 }
00542 
00543 void KKeyChooser::slotSettingsChanged( int category )
00544 {
00545     if( category == KApplication::SETTINGS_SHORTCUTS )
00546         readGlobalKeys(); // reread
00547 }
00548 
00549 void KKeyChooser::fontChange( const QFont & )
00550 {
00551         d->fCArea->setMinimumHeight( 4*d->pbtnShortcut->sizeHint().height() );
00552 
00553         int widget_width = 0;
00554 
00555         setMinimumWidth( 20+5*(widget_width+10) );
00556 }
00557 
00558 // KDE4 IMHO this shouldn't be here at all - it cannot check whether the default
00559 // shortcut don't conflict with some already changed ones (e.g. global shortcuts).
00560 // Also, I personally find reseting all shortcuts to default (i.e. hardcoded in the app)
00561 // ones after pressing the 'Default' button rather a misfeature.
00562 void KKeyChooser::allDefault()
00563 {
00564     kdDebug(125) << "KKeyChooser::allDefault()" << endl;
00565 
00566     QListViewItemIterator it( d->pList );
00567     for( ; it.current(); ++it ) {
00568         KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>(it.current());
00569         if( pItem )
00570             pItem->setShortcut( pItem->shortcutDefault() );
00571     }
00572 
00573     updateButtons();
00574     emit keyChange();
00575 }
00576 
00577 void KKeyChooser::slotListItemSelected( QListViewItem* )
00578 {
00579     updateButtons();
00580 }
00581 
00582 void KKeyChooser::slotListItemDoubleClicked ( QListViewItem *, const QPoint & , int )
00583 { // KDE4 dump this
00584   captureCurrentItem();
00585 }
00586 
00587 void KKeyChooser::captureCurrentItem()
00588 {
00589   KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>( d->pList->currentItem() );
00590   if( pItem != NULL && pItem->isConfigurable())
00591       d->pbtnShortcut->captureShortcut ( );
00592 }
00593 
00594 void KKeyChooser::setPreferFourModifierKeys( bool bPreferFourModifierKeys )
00595 {
00596     d->bPreferFourModifierKeys = bPreferFourModifierKeys;
00597 }
00598 
00599 void KKeyChooser::capturedShortcut( const KShortcut& cut )
00600 {
00601     if( cut.isNull() )
00602         slotNoKey();
00603     else
00604         setShortcut( cut );
00605 }
00606 
00607 // FIXME: give this functionality again -- I don't think it's ever used, though. -- ellis
00608 // TODO: Check lxr.kde.org to see if it's used anywhere
00609 void KKeyChooser::listSync()
00610 {
00611 /*  kdDebug(125) << "KKeyChooser::listSync()" << endl;
00612 
00613     if( d->pColl ) {
00614         // TODO: This is very inefficient.  Come up with something better.
00615         KAccelActions aa;
00616         d->pColl->createKeyMap( aa );
00617         d->actionsNew.updateShortcuts( aa );
00618     } else if( d->pActionsOrig ) {
00619         d->actionsNew.updateShortcuts( *d->pActionsOrig );
00620         update();
00621         updateButtons();
00622     }*/
00623 }
00624 
00625 void KKeyChooser::syncToConfig( const QString& sConfigGroup, KConfigBase* pConfig, bool bClearUnset )
00626 {
00627     kdDebug(125) << "KKeyChooser::syncToConfig( \"" << sConfigGroup << "\", " << pConfig << " ) start" << endl;
00628     if( !pConfig )
00629         pConfig = KGlobal::config();
00630     KConfigGroupSaver cgs( pConfig, sConfigGroup );
00631 
00632     QListViewItemIterator it( d->pList );
00633     for( ; it.current(); ++it ) {
00634         KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>(it.current());
00635         if( pItem ) {
00636             QString sEntry = pConfig->readEntry( pItem->actionName() );
00637             if( !sEntry.isNull() || bClearUnset ) {
00638                 if( sEntry == "none" )
00639                     sEntry = QString::null;
00640                 pItem->setShortcut( sEntry );
00641             }
00642             kdDebug(125) << pItem->actionName() << " = " << pItem->shortcut().toStringInternal() << endl;
00643         }
00644     }
00645     updateButtons();
00646     kdDebug(125) << "KKeyChooser::syncToConfig() done" << endl;
00647 }
00648 
00649 void KKeyChooser::setShortcut( const KShortcut& cut )
00650 {
00651     kdDebug(125) << "KKeyChooser::setShortcut( " << cut.toString() << " )" << endl;
00652     KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>(d->pList->currentItem());
00653     if( !pItem )
00654         return;
00655 
00656     for( uint i = 0; i < cut.count(); i++ ) {
00657         const KKeySequence& seq = cut.seq(i);
00658         const KKey& key = seq.key(0);
00659 
00660         if( !d->bAllowLetterShortcuts && key.modFlags() == 0
00661             && key.sym() < 0x3000 && QChar(key.sym()).isLetterOrNumber() ) {
00662             QString s = i18n(   "In order to use the '%1' key as a shortcut, "
00663                         "it must be combined with the "
00664                         "Win, Alt, Ctrl, and/or Shift keys." ).arg(QChar(key.sym()));
00665             KMessageBox::sorry( this, s, i18n("Invalid Shortcut Key") );
00666             return;
00667         }
00668     }
00669 
00670     // If key isn't already in use,
00671     if( !isKeyPresent( cut ) ) {
00672         // Set new key code
00673         pItem->setShortcut( cut );
00674         // Update display
00675         updateButtons();
00676         emit keyChange();
00677     }
00678 }
00679 
00680 // Returns iSeq index if cut2 has a sequence of equal or higher priority to a sequence in cut.
00681 // else -1
00682 static int keyConflict( const KShortcut& cut, const KShortcut& cut2 )
00683 {
00684     for( uint iSeq = 0; iSeq < cut.count(); iSeq++ ) {
00685         for( uint iSeq2 = 0; iSeq2 <= iSeq && iSeq2 < cut2.count(); iSeq2++ ) {
00686             if( cut.seq(iSeq) == cut2.seq(iSeq2) )
00687                 return iSeq;
00688         }
00689     }
00690     return -1;
00691 }
00692 
00693 bool KKeyChooser::isKeyPresent( const KShortcut& cut, bool bWarnUser )
00694 {
00695     KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>(d->pList->currentItem());
00696 
00697     if (!pItem) {
00698         return false;
00699     }
00700 
00701     // If editing global shortcuts, check them for conflicts with the stdaccels.
00702     if( m_type == ApplicationGlobal || m_type == Global ) {
00703                 if( checkStandardShortcutsConflict( cut, bWarnUser, this ))
00704                     return true;
00705     }
00706 
00707         bool has_global_chooser = false;
00708         for( QValueList< KKeyChooser* >::ConstIterator it = allChoosers->begin();
00709              it != allChoosers->end();
00710              ++it )
00711             has_global_chooser |= ((*it)->m_type == Global);
00712         // only check the global keys if one of the keychoosers isn't global
00713         if( !has_global_chooser ) {
00714             if( checkGlobalShortcutsConflict( cut, bWarnUser, this, d->mapGlobals,
00715                 m_type == Global ? pItem->actionName() : QString::null ))
00716                 return true;
00717         }
00718 
00719         if( isKeyPresentLocally( cut, pItem, bWarnUser ))
00720             return true;
00721 
00722         // check also other KKeyChooser's
00723         for( QValueList< KKeyChooser* >::ConstIterator it = allChoosers->begin();
00724              it != allChoosers->end();
00725              ++it ) {
00726             if( (*it) != this && (*it)->isKeyPresentLocally( cut, NULL, bWarnUser ))
00727                     return true;
00728             }
00729     return false;
00730 }
00731 
00732 // KDE4 remove
00733 bool KKeyChooser::isKeyPresentLocally( const KShortcut& cut, KKeyChooserItem* ignoreItem, const QString& warnText )
00734 {
00735     return isKeyPresentLocally( cut, ignoreItem, !warnText.isNull());
00736 }
00737 
00738 bool KKeyChooser::isKeyPresentLocally( const KShortcut& cut, KKeyChooserItem* ignoreItem, bool bWarnUser )
00739 {
00740     if ( cut.toString().isEmpty())
00741         return false;
00742     // Search for shortcut conflicts with other actions in the
00743     //  lists we're configuring.
00744     for( QListViewItemIterator it( d->pList ); it.current(); ++it ) {
00745         KKeyChooserItem* pItem2 = dynamic_cast<KKeyChooserItem*>(it.current());
00746         if( pItem2 && pItem2 != ignoreItem ) {
00747             int iSeq = keyConflict( cut, pItem2->shortcut() );
00748             if( iSeq > -1 ) {
00749                 if( bWarnUser ) {
00750                                         if( !promptForReassign( cut.seq(iSeq), pItem2->text(0), Application, this ))
00751                                 return true;
00752                                         // else remove the shortcut from it
00753                                         pItem2->setShortcut( KShortcut());
00754                                         updateButtons();
00755                                 emit keyChange();
00756                                 }
00757             }
00758         }
00759     }
00760         return false;
00761 }
00762 
00763 bool KKeyChooser::checkStandardShortcutsConflict( const KShortcut& cut, bool bWarnUser, QWidget* parent )
00764 {
00765     // For each key sequence in the shortcut,
00766     for( uint i = 0; i < cut.count(); i++ ) {
00767     const KKeySequence& seq = cut.seq(i);
00768     KStdAccel::StdAccel id = KStdAccel::findStdAccel( seq );
00769     if( id != KStdAccel::AccelNone
00770         && keyConflict( cut, KStdAccel::shortcut( id ) ) > -1 ) {
00771         if( bWarnUser ) {
00772             if( !promptForReassign( seq, KStdAccel::label(id), Standard, parent ))
00773                                 return true;
00774                         removeStandardShortcut( KStdAccel::label(id), dynamic_cast< KKeyChooser* > ( parent ));
00775                 }
00776     }
00777     }
00778     return false;
00779 }
00780 
00781 bool KKeyChooser::checkGlobalShortcutsConflict( const KShortcut& cut, bool bWarnUser, QWidget* parent )
00782 {
00783     QMap< QString, KShortcut > map;
00784     readGlobalKeys( map );
00785     return checkGlobalShortcutsConflict( cut, bWarnUser, parent, map, QString::null );
00786 }
00787 
00788 bool KKeyChooser::checkGlobalShortcutsConflict( const KShortcut& cut, bool bWarnUser, QWidget* parent,
00789     const QMap< QString, KShortcut >& map, const QString& ignoreAction )
00790 {
00791     QMap<QString, KShortcut>::ConstIterator it;
00792     for( it = map.begin(); it != map.end(); ++it ) {
00793         int iSeq = keyConflict( cut, (*it) );
00794         if( iSeq > -1 ) {
00795             if( ignoreAction.isEmpty() || it.key() != ignoreAction ) {
00796                     if( bWarnUser ) {
00797             if( !promptForReassign( cut.seq(iSeq), it.key(), Global, parent ))
00798                                     return true;
00799                             removeGlobalShortcut( it.key(), dynamic_cast< KKeyChooser* >( parent ));
00800                     }
00801         }
00802     }
00803     }
00804     return false;
00805 }
00806 
00807 void KKeyChooser::removeStandardShortcut( const QString& name, KKeyChooser* chooser )
00808 {
00809     bool was_in_choosers = false;
00810     if( allChoosers != NULL ) {
00811         for( QValueList< KKeyChooser* >::ConstIterator it = allChoosers->begin();
00812              it != allChoosers->end();
00813              ++it ) {
00814             if( (*it) != chooser && (*it)->m_type == Standard ) {
00815                 was_in_choosers |= ( (*it)->resetShortcut( name ));
00816             }
00817         }
00818     }
00819     if( !was_in_choosers ) { // not edited, needs to be changed in config file
00820         KStdAccel::ShortcutList std_list;
00821         std_list.setShortcut( std_list.index( name ), KShortcut());
00822         std_list.save();
00823     }
00824 }
00825 
00826 void KKeyChooser::removeGlobalShortcut( const QString& name, KKeyChooser* chooser )
00827 {
00828     bool was_in_choosers = false;
00829     if( allChoosers != NULL ) {
00830         for( QValueList< KKeyChooser* >::ConstIterator it = allChoosers->begin();
00831              it != allChoosers->end();
00832              ++it ) {
00833             if( (*it) != chooser && (*it)->m_type == Global ) {
00834                 was_in_choosers |= ( (*it)->resetShortcut( name ));
00835             }
00836         }
00837     }
00838     if( !was_in_choosers ) { // not edited, needs to be changed in config file
00839         KAccelActions actions;
00840         actions.insert( name, "", "", KShortcut(), KShortcut());
00841     actions.writeActions( "Global Shortcuts", 0, true, true );
00842     }
00843 }
00844 
00845 bool KKeyChooser::resetShortcut( const QString& name )
00846 {
00847     for( QListViewItemIterator it( d->pList ); it.current(); ++it ) {
00848         KKeyChooserItem* pItem2 = dynamic_cast<KKeyChooserItem*>(it.current());
00849                     if( pItem2 && pItem2->actionName() == name ) {
00850                         pItem2->setShortcut( KShortcut());
00851                         updateButtons();
00852                 emit keyChange();
00853                         return true;
00854                     }
00855         }
00856         return false;
00857 }
00858 
00859 // KDE4 remove this
00860 void KKeyChooser::_warning( const KKeySequence& cut, QString sAction, QString sTitle )
00861 {
00862     sAction = sAction.stripWhiteSpace();
00863 
00864     QString s =
00865         i18n("The '%1' key combination has already been allocated "
00866         "to the \"%2\" action.\n"
00867         "Please choose a unique key combination.").
00868         arg(cut.toString()).arg(sAction);
00869 
00870     KMessageBox::sorry( this, s, sTitle );
00871 }
00872 
00873 bool KKeyChooser::promptForReassign( const KKeySequence& cut, const QString& sAction, ActionType type, QWidget* parent )
00874 {
00875         QString sTitle;
00876         QString s;
00877         if( type == Standard ) {
00878                 sTitle = i18n("Conflict with Standard Application Shortcut");
00879         s = i18n("The '%1' key combination has already been allocated "
00880         "to the standard action \"%2\".\n"
00881         "Do you want to reassign it from that action to the current one?");
00882         }
00883         else if( type == Global ) {
00884                 sTitle = i18n("Conflict with Global Shortcut");
00885         s = i18n("The '%1' key combination has already been allocated "
00886         "to the global action \"%2\".\n"
00887         "Do you want to reassign it from that action to the current one?");
00888         }
00889         else {
00890                 sTitle = i18n("Key Conflict");
00891         s = i18n("The '%1' key combination has already been allocated "
00892         "to the \"%2\" action.\n"
00893         "Do you want to reassign it from that action to the current one?");
00894         }
00895     s = s.arg(cut.toString()).arg(sAction.stripWhiteSpace());
00896 
00897     return KMessageBox::warningYesNo( parent, s, sTitle ) == KMessageBox::Yes;
00898 }
00899 
00900 //---------------------------------------------------
00901 KKeyChooserItem::KKeyChooserItem( KListView* parent, QListViewItem* after, KShortcutList* pList, uint iAction )
00902 :   KListViewItem( parent, after )
00903 {
00904     m_pList = pList;
00905     m_iAction = iAction;
00906     m_bModified = false;
00907     m_cut = m_pList->shortcut(m_iAction);
00908 }
00909 
00910 KKeyChooserItem::KKeyChooserItem( QListViewItem* parent, QListViewItem* after, KShortcutList* pList, uint iAction )
00911 :   KListViewItem( parent, after )
00912 {
00913     m_pList = pList;
00914     m_iAction = iAction;
00915     m_bModified = false;
00916     m_cut = m_pList->shortcut(m_iAction);
00917 }
00918 
00919 QString KKeyChooserItem::actionName() const
00920 {
00921     return m_pList->name(m_iAction);
00922 }
00923 
00924 const KShortcut& KKeyChooserItem::shortcut() const
00925 {
00926     return m_cut;
00927 }
00928 
00929 void KKeyChooserItem::setShortcut( const KShortcut& cut )
00930 {
00931     m_cut = cut;
00932     m_bModified = (m_cut != m_pList->shortcut(m_iAction));
00933     listView()->repaintItem( this );
00934 }
00935 
00936 void KKeyChooserItem::commitChanges()
00937 {
00938     if( m_bModified )
00939         m_pList->setShortcut( m_iAction, m_cut );
00940 }
00941 
00942 QString KKeyChooserItem::text( int iCol ) const
00943 {
00944     if( iCol == 0 ) {
00945         // Quick HACK to get rid of '&'s.
00946         QString s = m_pList->label(m_iAction);
00947         QString s2;
00948         for( uint i = 0; i < s.length(); i++ )
00949             if( s[i] != '&' || ( i+1<s.length() && s[i+1] == '&' ) )
00950                 s2 += s[i];
00951         return s2;
00952     }
00953     else if( iCol <= (int) m_cut.count() )
00954         return m_cut.seq(iCol-1).toString();
00955     else
00956         return QString::null;
00957 }
00958 
00959 int KKeyChooserItem::compare( QListViewItem* item, int iCol, bool bAscending ) const
00960 {
00961     KKeyChooserItem* pItem = dynamic_cast<KKeyChooserItem*>( item );
00962     if( iCol == 0 && pItem ) {
00963         QString psName1 = m_pList->name(m_iAction);
00964         QString psName2 = pItem->m_pList->name(pItem->m_iAction);
00965         QRegExp rxNumber1( " (\\d+)$" );
00966         QRegExp rxNumber2( " (\\d+)$" );
00967         int iNumber1 = rxNumber1.search( psName1 );
00968         int iNumber2 = rxNumber2.search( psName2 );
00969 
00970         // Check if the last word is one or more digits
00971         if( iNumber1 >= 0 && iNumber1 == iNumber2 && psName1.startsWith( psName2.left( iNumber1+1 ) ) ) {
00972             int n1 = rxNumber1.cap(1).toInt();
00973             int n2 = rxNumber2.cap(1).toInt();
00974             return (n1 < n2) ? -1 : (n1 > n2) ? 1 : 0;
00975         }
00976     }
00977 
00978     return QListViewItem::compare( item, iCol, bAscending );
00979 }
00980 
00981 /************************************************************************/
00982 /* KKeyDialog                                                           */
00983 /*                                                                      */
00984 /* Originally by Nicolas Hadacek <hadacek@via.ecp.fr>                   */
00985 /*                                                                      */
00986 /* Substantially revised by Mark Donohoe <donohoe@kde.org>              */
00987 /*                                                                      */
00988 /* And by Espen Sand <espen@kde.org> 1999-10-19                         */
00989 /* (by using KDialogBase there is almost no code left ;)                */
00990 /*                                                                      */
00991 /************************************************************************/
00992 KKeyDialog::KKeyDialog( KKeyChooser::ActionType type, bool bAllowLetterShortcuts, QWidget *parent, const char* name )
00993 : KDialogBase( parent, name, true, i18n("Configure Shortcuts"), Help|Default|Ok|Cancel, Ok )
00994 {
00995     m_pKeyChooser = new KKeyChooser( this, type, bAllowLetterShortcuts );
00996     setMainWidget( m_pKeyChooser );
00997     connect( this, SIGNAL(defaultClicked()), m_pKeyChooser, SLOT(allDefault()) );
00998     enableButton( Help, false );
00999 
01000     KConfigGroup group( KGlobal::config(), "KKeyDialog Settings" );
01001     QSize sz = size();
01002     resize( group.readSizeEntry( "Dialog Size", &sz ) );
01003 }
01004 
01005 KKeyDialog::KKeyDialog( bool bAllowLetterShortcuts, QWidget *parent, const char* name )
01006 : KDialogBase( parent, name, true, i18n("Configure Shortcuts"), Help|Default|Ok|Cancel, Ok )
01007 {
01008     m_pKeyChooser = new KKeyChooser( this, KKeyChooser::Application, bAllowLetterShortcuts );
01009     setMainWidget( m_pKeyChooser );
01010     connect( this, SIGNAL(defaultClicked()), m_pKeyChooser, SLOT(allDefault()) );
01011     enableButton( Help, false );
01012 
01013     KConfigGroup group( KGlobal::config(), "KKeyDialog Settings" );
01014     QSize sz = size();
01015     resize( group.readSizeEntry( "Dialog Size", &sz ) );
01016 }
01017 
01018 KKeyDialog::~KKeyDialog()
01019 {
01020     KConfigGroup group( KGlobal::config(), "KKeyDialog Settings" );
01021     group.writeEntry( "Dialog Size", size(), true, true );
01022 }
01023 
01024 bool KKeyDialog::insert( KActionCollection* pColl )
01025 {
01026     return m_pKeyChooser->insert( pColl );
01027 }
01028 
01029 bool KKeyDialog::insert(KActionCollection *pColl, const QString &title)
01030 {
01031     return m_pKeyChooser->insert(pColl, title);
01032 }
01033 
01034 bool KKeyDialog::configure( bool bSaveSettings )
01035 {
01036     int retcode = exec();
01037     if( retcode == Accepted ) {
01038         if( bSaveSettings )
01039             m_pKeyChooser->save();
01040         else
01041             commitChanges();
01042     }
01043     return retcode;
01044 }
01045 
01046 void KKeyDialog::commitChanges()
01047 {
01048     m_pKeyChooser->commitChanges();
01049 }
01050 
01051 int KKeyDialog::configure( KActionCollection* coll, QWidget* parent, bool bSaveSettings )
01052 {
01053     return configure( coll, true, parent, bSaveSettings);
01054 }
01055 
01056 int KKeyDialog::configure( KAccel* keys, QWidget* parent, bool bSaveSettings )
01057 {
01058     return configure( keys, true, parent, bSaveSettings);
01059 }
01060 
01061 int KKeyDialog::configure( KGlobalAccel* keys, QWidget* parent, bool bSaveSettings )
01062 {
01063     return configure( keys, true, parent, bSaveSettings);
01064 }
01065 
01066 int KKeyDialog::configure( KAccel* keys, bool bAllowLetterShortcuts, QWidget *parent, bool bSaveSettings )
01067 {
01068     KKeyDialog dlg( bAllowLetterShortcuts, parent );
01069     dlg.m_pKeyChooser->insert( keys );
01070     bool b = dlg.configure( bSaveSettings );
01071     if( b && bSaveSettings )
01072         keys->updateConnections();
01073     return b;
01074 }
01075 
01076 int KKeyDialog::configure( KGlobalAccel* keys, bool bAllowLetterShortcuts, QWidget *parent, bool bSaveSettings )
01077 {
01078     KKeyDialog dlg( KKeyChooser::ApplicationGlobal, bAllowLetterShortcuts, parent );
01079     dlg.m_pKeyChooser->insert( keys );
01080     bool b = dlg.configure( bSaveSettings );
01081     if( b && bSaveSettings )
01082         keys->updateConnections();
01083     return b;
01084 }
01085 
01086 int KKeyDialog::configure( KActionCollection* coll, bool bAllowLetterShortcuts, QWidget *parent, bool bSaveSettings )
01087 {
01088     kdDebug(125) << "KKeyDialog::configureKeys( KActionCollection*, " << bSaveSettings << " )" << endl;
01089     KKeyDialog dlg( bAllowLetterShortcuts, parent );
01090     dlg.m_pKeyChooser->insert( coll );
01091     return dlg.configure( bSaveSettings );
01092 }
01093 
01094 /*int KKeyDialog::configure( KActionPtrList* coll, const QString& file, QWidget *parent, bool bSaveSettings )
01095 {
01096     kdDebug(125) << "KKeyDialog::configureKeys( KActionCollection*, " << file << ", " << bSaveSettings << " )" << endl;
01097     KAccelActions actions;
01098     coll->createKeyMap( actions );
01099 
01100     int retcode = configure( actions, file, parent, bSaveSettings );
01101     if( retcode == Accepted )
01102         coll->setKeyMap( actions );
01103 
01104     return retcode;
01105 }*/
01106 
01107 void KKeyChooser::virtual_hook( int, void* )
01108 { /*BASE::virtual_hook( id, data );*/ }
01109 
01110 void KKeyDialog::virtual_hook( int id, void* data )
01111 { KDialogBase::virtual_hook( id, data ); }
01112 
01113 #include "kkeydialog.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Feb 4 12:34:17 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003