kshortcutmenu.cpp Source File (kdecore Library)

| # kdecore Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kshortcutmenu.cpp

    00001 /*
    00002     Copyright (c) 2002 Ellis Whitehead <ellis@kde.org>
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public License
    00015     along with this library; see the file COPYING.LIB.  If not, write to
    00016     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00017     Boston, MA 02111-1307, USA.
    00018 */
    00019
    00020 #include <qkeysequence.h>
    00021 #include <qlabel.h>
    00022 #include <qpopupmenu.h>
    00023
    00024 #include "kaccelaction.h"
    00025 #include <kdebug.h>
    00026 #include <kglobalsettings.h>
    00027 #include "kshortcutmenu.h"
    00028 //#include <kkeynative.h>
    00029
    00030 KShortcutMenu::KShortcutMenu( QWidget* pParent, KAccelActions* pActions, KKeySequence seq )
    00031 :       QPopupMenu( pParent ),
    00032         m_pActions( pActions ),
    00033         m_seq( seq )
    00034 {
    00035         kdDebug() << seq.toStringInternal() << endl;
    00036
    00037         QFont fontTitle = KGlobalSettings::menuFont();
    00038         fontTitle.setBold( true );
    00039
    00040         pTitle = new QLabel( "", (QWidget*)0 );
    00041         pTitle->setFont( fontTitle );
    00042         pTitle->setFrameShape( QFrame::Panel );
    00043
    00044         insertItem( pTitle );
    00045 }
    00046
    00047 bool KShortcutMenu::insertAction( uint iAction, KKeySequence seq )
    00048 {
    00049         KAccelAction* pAction = m_pActions->actionPtr( iAction );
    00050
    00051         if( pAction ) {
    00052                 insertItem( "", iAction );
    00053                 m_seqs[indexOf(iAction)] = seq;
    00054                 return true;
    00055         } else
    00056                 return false;
    00057 }
    00058
    00059
    00060 void KShortcutMenu::updateShortcuts()
    00061 {
    00062         pTitle->setText( m_seq.toString() + ",..." );
    00063
    00064         for( uint iItem = 1; iItem < count(); iItem++ ) {
    00065                 int iAction = idAt( iItem );
    00066                 if( iAction >= 0 ) {
    00067                         KAccelAction* pAction = m_pActions->actionPtr( iAction );
    00068                         if( pAction ) {
    00069                                 KKeySequence seq = m_seqs[iItem];
    00070                                 QString sSeq = seq.key(m_seq.count()).toString();
    00071                                 for( uint iKey = m_seq.count() + 1; iKey < seq.count(); iKey++ )
    00072                                         sSeq += QString(",") + seq.key(iKey).toString();
    00073
    00074                                 kdDebug(125) << "seq = " << seq.toStringInternal() << " sSeq = " << sSeq << endl;
    00075                                 changeItem( iAction, pAction->label() + "\t" + sSeq );
    00076                         }
    00077                 }
    00078         }
    00079 }
    00080
    00081 void KShortcutMenu::keyPressEvent( QKeyEvent* pEvent )
    00082 {
    00083         kdDebug() << "keypress; " << pEvent->key() << endl;
    00084         KKey key( pEvent );
    00085
    00086         switch( pEvent->key() ) {
    00087          case Key_Shift:
    00088          case Key_Control:
    00089          case Key_Alt:
    00090          case Key_Meta:
    00091          case Key_Super_L:
    00092          case Key_Super_R:
    00093          case Key_Hyper_L:
    00094          case Key_Hyper_R:
    00095                 break;
    00096          default:
    00097                 int iItem = searchForKey( key );
    00098                 // If key not found, look for unmodified version.
    00099                 if( iItem == -1 ) {
    00100                         key = pEvent->key();
    00101                         iItem = searchForKey( key );
    00102                 }
    00103
    00104                 if( iItem == -1 ) {
    00105                         // Let Up and Down keys navigate menu,
    00106                         // And permit Enter, Return to select the item.
    00107                         if( pEvent->key() == Qt::Key_Up    || pEvent->key() == Qt::Key_Down ||
    00108                             pEvent->key() == Qt::Key_Enter || pEvent->key() == Qt::Key_Return )
    00109                                 QPopupMenu::keyPressEvent( pEvent );
    00110                         else
    00111                                 close();
    00112                 }
    00113                 else if( iItem == 0 )
    00114                         keepItemsMatching( key );
    00115                 else
    00116                         activateItemAt( iItem );
    00117         }
    00118 }
    00119
    00120 int KShortcutMenu::searchForKey( KKey key )
    00121 {
    00122         int iItemFound = -1; // -1 indicates no match
    00123         uint iKey = m_seq.count();
    00124
    00125         for( uint iItem = 1; iItem < count(); iItem++ ) {
    00126                 if( m_seqs.contains( iItem ) ) {
    00127                         KKey keyItem = m_seqs[iItem].key( iKey );
    00128                         //kdDebug(125) << "iItem = " << iItem << " key = " << key.toStringInternal() << " keyItem = " << keyItem.toStringInternal() << endl;
    00129                         if( key == keyItem ) {
    00130                                 if( iItemFound == -1 )
    00131                                         iItemFound = iItem;
    00132                                 else
    00133                                         return 0; // 0 indicates duplicate matches
    00134                         }
    00135                 }
    00136         }
    00137
    00138         return iItemFound;
    00139 }
    00140
    00141 void KShortcutMenu::keepItemsMatching( KKey key )
    00142 {
    00143         kdDebug(125) << "MyAccel::keepItemsMatching( " << key.toStringInternal() << " )" << endl;
    00144
    00145         uint iKey = m_seq.count();
    00146         m_seq.setKey( iKey, key );
    00147
    00148         for( uint iItem = 1; iItem < count(); iItem++ ) {
    00149                 if( m_seqs.contains( iItem ) ) {
    00150                         KKey keyItem = m_seqs[iItem].key( iKey );
    00151                         if( key != keyItem ) {
    00152                                 m_seqs.remove( iItem );
    00153                                 removeItemAt( iItem-- );
    00154                         }
    00155                 }
    00156         }
    00157
    00158         updateShortcuts();
    00159 }
    00160
    00161 #include "kshortcutmenu.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:53 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001