KDECore
kshortcutmenu.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
00099 if( iItem == -1 ) {
00100 key = pEvent->key();
00101 iItem = searchForKey( key );
00102 }
00103
00104 if( iItem == -1 ) {
00105
00106
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;
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
00129 if( key == keyItem ) {
00130 if( iItemFound == -1 )
00131 iItemFound = iItem;
00132 else
00133 return 0;
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"