kdeui
kcmenumngr.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
00021 #include <qwidget.h>
00022 #include <qpopupmenu.h>
00023 #include "kcmenumngr.h"
00024 #include "kglobal.h"
00025 #include "kconfig.h"
00026 #include "kshortcut.h"
00027
00028 #undef KeyPress
00029 #undef None
00030
00031 template class QPtrDict<QPopupMenu>;
00032
00033 KContextMenuManager* KContextMenuManager::manager = 0;
00034
00035 KContextMenuManager::KContextMenuManager( QObject* parent, const char* name )
00036 : QObject( parent, name)
00037 {
00038 KConfigGroupSaver saver ( KGlobal::config(), QString::fromLatin1("Shortcuts") ) ;
00039 menuKey = KShortcut( saver.config()->readEntry(QString::fromLatin1("PopupContextMenu"), QString::fromLatin1("Menu") ) ).keyCodeQt();
00040 saver.config()->setGroup( QString::fromLatin1("ContextMenus") ) ;
00041 showOnPress = saver.config()->readBoolEntry(QString::fromLatin1("ShowOnPress"), true );
00042 }
00043
00044 KContextMenuManager::~KContextMenuManager()
00045 {
00046 }
00047
00048
00049 bool KContextMenuManager::showOnButtonPress( void )
00050 {
00051 if ( !manager )
00052 manager = new KContextMenuManager;
00053 return manager->showOnPress;
00054 }
00055
00056
00057 void KContextMenuManager::insert( QWidget* widget, QPopupMenu* popup )
00058 {
00059 if ( !manager )
00060 manager = new KContextMenuManager;
00061
00062 manager->connect( widget, SIGNAL( destroyed() ), manager, SLOT( widgetDestroyed() ) );
00063 manager->menus.insert( widget, popup );
00064 widget->installEventFilter( manager );
00065 }
00066
00067 bool KContextMenuManager::eventFilter( QObject *o, QEvent * e)
00068 {
00069 QPopupMenu* popup = 0;
00070 QPoint pos;
00071 switch ( e->type() ) {
00072 case QEvent::MouseButtonPress:
00073 if (((QMouseEvent*) e )->button() != RightButton )
00074 break;
00075 if ( !showOnPress )
00076 return true;
00077 popup = menus[o];
00078 pos = ((QMouseEvent*) e )->globalPos();
00079 break;
00080 case QEvent::MouseButtonRelease:
00081 if ( showOnPress || ((QMouseEvent*) e )->button() != RightButton )
00082 break;
00083 popup = menus[o];
00084 pos = ((QMouseEvent*) e )->globalPos();
00085 break;
00086 case QEvent::KeyPress:
00087 {
00088 if ( !o->isWidgetType() )
00089 break;
00090 QKeyEvent *k = (QKeyEvent *)e;
00091 int key = k->key();
00092 if ( k->state() & ShiftButton )
00093 key |= SHIFT;
00094 if ( k->state() & ControlButton )
00095 key |= CTRL;
00096 if ( k->state() & AltButton )
00097 key |= ALT;
00098 if ( key != menuKey )
00099 break;
00100 popup = menus[o];
00101 if ( popup ) {
00102 QWidget* w = (QWidget*) o ;
00103
00104
00105 pos = w->mapToGlobal( w->rect().center() );
00106
00107
00108 }
00109 }
00110 break;
00111 default:
00112 break;
00113 }
00114
00115 if ( popup ) {
00116 popup->popup( pos );
00117 return true;
00118 }
00119
00120 return false;
00121 }
00122
00123 void KContextMenuManager::widgetDestroyed()
00124 {
00125 if ( menus.find( (QObject*)sender() ) )
00126 menus.remove( (QObject*)sender() );
00127 }
00128
00129 #include "kcmenumngr.moc"