kdeui Library API Documentation

kcmenumngr.cpp

00001 /*
00002  *  This file is part of the KDE Libraries
00003  *  Copyright (C) 1999 Matthias Ettrich <ettrich@kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
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; // eat event for safety
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                 // ### workaround
00105                 pos = w->mapToGlobal( w->rect().center() );
00106                 // with later Qt snapshot 
00107                 // pos = w->mapToGlobal( w->microFocusHint().center() );
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"
KDE Logo
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:56:15 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001