Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KGlobalAccel (./kdelibs/kdecore/kglobalaccel.h:79)

class KGlobalAccel : public QObject
{
	Q_OBJECT
	
 public:
	/**
	 * Creates a KGlobalAccel object.
	 */
	KGlobalAccel( bool _do_not_grab = false);
			
	/**
	   Creates a KGlobalAccel object with a parent and a name. The
	   parent has the only effect that the KGlobalAccel object
	   will be automatically destroyed in the parent's destructor,
	   thus releaseing the keys.
	*/
	KGlobalAccel(QWidget * parent, const char *name = 0, bool _do_not_grab = false);


	/**
	 * Destroys the accelerator object.and ungrabs any allocated key bindings.
	 */
	~KGlobalAccel();
	
	/**
	 * Removes all accelerator items.
	 */
	void clear();
	
	/**
	 * Connect an accelerator item to a slot/signal in another object.
	 *
	 * Arguments:
	 *
	 *  @param action is the accelerator item action name.
	 *  @param receiver is the object to receive a signal
	 *  @param member is a slot or signal in the receiver
	 *  @param activate indicates whether the accelrator item should be
	 *  enabled immediately
	 */
	void connectItem( const QString& action,
			  const QObject* receiver, const char *member,
						  bool activate = true );
						
	/**
	* Returns the number of accelerator items.
	*/					
	uint count() const;
	
	/**
	* Returns the key code of the accelerator item with the action name
	* action, or zero if either the action name cannot be found or the current
	* key is set to no key.
	*/
	uint currentKey( const QString& action ) const;

	/**
	* Returns the default key code of the accelerator item with the action name
	* action, or zero if the action name cannot be found.
	*/
	uint defaultKey( const QString& action) const;
	
	/**
	 * Disconnects an accelerator item from a function in another object.
	 */
	void disconnectItem( const QString& action,
			     const QObject* receiver, const char *member );
	
	/**
	 * Returns that identifier of the accelerator item with the keycode key,
	 * or zero if the item cannot be found.
	 */
	QString findKey( int key ) const;
	
	/**
	 * Attempts to make a passive X server grab of the key specified by key symbol
	 * keysym and modifier mod. Returns false if unsuccessful.
	 *
	 * Modifications with num lock and caps lock are also grabbed.
	 *
	 */
	bool grabKey(uint keysym, uint mod);
	
	/**
	 * Inserts an accelerator item and returns false if the key code
	 * 	defaultKeyCode is not valid.
	 *	
	 * Arguments:
	 *
	 *  @param action is the accelerator item action name.
	 *  @param defaultKeyCode is a key code to be used as the default for the action.
	 *  @param configurable indicates whether a user can configure the key
	 *	binding using the KKeyChooser GUI and whether the key will be written
	 *	back to configuration files on calling writeSettings.
	 *
	 * If an action already exists the old association and connections will be
	 * removed..
	 * 	
	 */
	bool insertItem( const QString& descr, const QString& action,
			 uint defaultKeyCode,
			 bool configurable = true );
	
	 /**
	 * Inserts an accelerator item and returns false if the key code
	 * 	defaultKeyCode is not valid.
	 *	
	 * Arguments:
	 *
	 *  @param action is the accelerator item action name.
	 *  @param defaultKeyCode is a key plus a combination of SHIFT, CTRL
	 *	and ALT to be used as the default for the action.
	 *  @param configurable indicates whether a user can configure
	 *  the key
	 *	binding using the KKeyChooser GUI and whether the key
	 *	will be written back to configuration files on calling
	 *	writeSettings.
	 *
	 * If an action already exists the old association and connections
	 * will be removed..

	 */
	bool insertItem( const QString& descr, const QString& action,
			 const QString& defaultKeyCode,
			 bool configurable = true );
	
	bool isEnabled() const;
	bool isItemEnabled( const QString& action ) const;
	
	/**
	* Returns the dictionary of accelerator action names and KKeyEntry
	* objects. Note that only a shallow copy is returned so that
	* items will be lost when the KKeyEntry objects are deleted.
	*/	
	QDict<KKeyEntry> keyDict();
				
	/**
	 * Reads all key associations from the application's configuration
	 * files.
	 */
	void readSettings();
		
 	/**
	 * Removes the accelerator item with the action name action.
	 */
	void removeItem( const QString& action );
	
	void setConfigGroup( const QString& group );
	
	QString configGroup() const;
	
	/**
	 * Enables the accelerator if activate is true, or disables it if
	 * activate is false...
	 *
	 * Individual keys can also be enabled or disabled.
	 */
	void setEnabled( bool activate );
	
	/**
	 * Enables or disables an accelerator item.
	 *
	 * Arguments:
	 *
	 *  @param action is the accelerator item action name.
	 *	@param activate specifies whether the item should be enabled or
	 *	disabled.
	 */
	void setItemEnabled( const QString& action, bool activate );
	
	/**
	* Sets the dictionary of accelerator action names and KKeyEntry
	* objects to nKeyDict.. Note that only a shallow copy is made so that items will be
	* lost when the KKeyEntry objects are deleted.
	*/	
	bool setKeyDict( QDict<KKeyEntry> nKeyDict );
	
	/**
	 * Ungrabs the key specified by key symbol
	 * keysym and modifier mod. Returns false if unsuccessful.
	 *
	 * Modifications with num lock and caps lock are also ungrabbed.
	 *
	 */
	bool ungrabKey(uint keysym, uint mod);

	/**
	 * Writes the current configurable associations to the application's
	 * configuration files
	 */	
	void writeSettings();

    
    	/**
	 * Filters X11 events ev for key bindings in the accelerator dictionary.
	 * If a match is found the activated activated is emitted and the function
	 * returns true. Return false if the event is not processed.
	 *
	 * This is public for compatibility only. You do not need to call it.
	 */
	bool x11EventFilter(const XEvent *);


signals:
	void activated();	

protected:
	int aAvailableId;
	QDict<KKeyEntry> aKeyDict;
	bool bEnabled;
	QString aGroup;
	bool do_not_grab;
	KGlobalAccelPrivate* d;

 protected:
};

kdelibs'KGlobalAccel::KGlobalAccel() (./kdelibs/kdecore/kglobalaccel.cpp:64)

KGlobalAccel::KGlobalAccel(bool _do_not_grab)
 : QObject(), aKeyDict(100)
{
	aAvailableId = 1;
	bEnabled = true;
	aGroup = "Global Keys";
	do_not_grab =_do_not_grab;
	d = 0;
	if ( !do_not_grab )
	    d = new KGlobalAccelPrivate( this );
}


kdelibs'KGlobalAccel::KGlobalAccel() (./kdelibs/kdecore/kglobalaccel.cpp:76)

KGlobalAccel::KGlobalAccel(QWidget * parent, const char *name, bool _do_not_grab)
    : QObject(parent, name), aKeyDict(100) {
    	aAvailableId = 1;
	bEnabled = true;
	aGroup = "Global Keys";
	do_not_grab =_do_not_grab;
	d = 0;
	if ( !do_not_grab )
	    d = new KGlobalAccelPrivate( this );
}


kdelibs'KGlobalAccel::~KGlobalAccel() (./kdelibs/kdecore/kglobalaccel.cpp:87)

KGlobalAccel::~KGlobalAccel()
{
	setEnabled( false );
	delete d;
}


kdelibs'KGlobalAccel::clear() (./kdelibs/kdecore/kglobalaccel.cpp:93)

void KGlobalAccel::clear()
{
	setEnabled( false );
	aKeyDict.clear();
}


kdelibs'KGlobalAccel::connectItem() (./kdelibs/kdecore/kglobalaccel.cpp:99)

void KGlobalAccel::connectItem( const QString& action,
				const QObject* receiver, const char *member,
				bool activate )
{
    KKeyEntry *pEntry = aKeyDict[ action ];
	if ( !pEntry ) {
		QString str = i18n("KGlobalAccel : Cannot connect action %1 "
				   "which is not in the object dictionary").arg(action);
		warning( str.ascii() );
		return;
	}
	
	pEntry->receiver = receiver;
	pEntry->member = member;
	pEntry->aAccelId = aAvailableId;
	aAvailableId++;
	
	setItemEnabled( action, activate );

}


kdelibs'KGlobalAccel::count() (./kdelibs/kdecore/kglobalaccel.cpp:120)

uint KGlobalAccel::count() const
{
	return aKeyDict.count();
}


kdelibs'KGlobalAccel::currentKey() (./kdelibs/kdecore/kglobalaccel.cpp:125)

uint KGlobalAccel::currentKey( const QString& action ) const
{
	KKeyEntry *pEntry = aKeyDict[ action ];
	
	if ( !pEntry )
		return 0;
	else
		return pEntry->aCurrentKeyCode;
}


kdelibs'KGlobalAccel::defaultKey() (./kdelibs/kdecore/kglobalaccel.cpp:135)

uint KGlobalAccel::defaultKey( const QString& action ) const
{
	KKeyEntry *pEntry = aKeyDict[ action ];
	
	if ( !pEntry )
        return 0;
    else
        return pEntry->aDefaultKeyCode;
}


kdelibs'KGlobalAccel::disconnectItem() (./kdelibs/kdecore/kglobalaccel.cpp:145)

void KGlobalAccel::disconnectItem( const QString& action,
				   const QObject* /*receiver*/, const char */*member*/ )
{
    KKeyEntry *pEntry = aKeyDict[ action ];
    if ( !pEntry )
	return;

}


kdelibs'KGlobalAccel::findKey() (./kdelibs/kdecore/kglobalaccel.cpp:154)

QString KGlobalAccel::findKey( int key ) const
{
	QDictIterator<KKeyEntry> aKeyIt( aKeyDict );
	aKeyIt.toFirst();

kdelibs'KGlobalAccel::grabKey() (./kdelibs/kdecore/kglobalaccel.cpp:179)

bool KGlobalAccel::grabKey( uint keysym, uint mod ) {
	// Most of this comes from kpanel/main.C
	// Copyright (C) 1996,97 Matthias Ettrich
	static int NumLockMask = 0;

	if (do_not_grab)
	  return true;

	if (!keysym || !XKeysymToKeycode(qt_xdisplay(), keysym)) return false;
	if (!NumLockMask){
		XModifierKeymap* xmk = XGetModifierMapping(qt_xdisplay());
		int i;
		for (i=0; i<8; i++){
		   if (xmk->modifiermap[xmk->max_keypermod * i] ==
		   		XKeysymToKeycode(qt_xdisplay(), XK_Num_Lock))
		   			NumLockMask = (1<<i);
		}
	}

	grabFailed = false;

	// We wan't to catch only our own errors
	XSync(qt_xdisplay(),0);
	XErrorHandler savedErrorHandler=XSetErrorHandler(XGrabErrorHandler);
	
	XGrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod,
		qt_xrootwin(), True,
		GrabModeAsync, GrabModeSync);
	XGrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod | LockMask,
		qt_xrootwin(), True,
		GrabModeAsync, GrabModeSync);
	XGrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod | NumLockMask,
		qt_xrootwin(), True,
		GrabModeAsync, GrabModeSync);
	XGrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod | LockMask | NumLockMask,
		qt_xrootwin(), True,
		GrabModeAsync, GrabModeSync);

	XSync(qt_xdisplay(),0);
	XSetErrorHandler(savedErrorHandler);
	
	if (grabFailed) {
		// FIXME: ungrab all successfull grabs!
		//warning("Global grab failed!");
   		return false;
	}
	return true;
}


kdelibs'KGlobalAccel::insertItem() (./kdelibs/kdecore/kglobalaccel.cpp:232)

bool KGlobalAccel::insertItem(  const QString& descr, const QString& action, uint keyCode,
					   bool configurable )
{
	KKeyEntry *pEntry = aKeyDict[ action ];
	
	if ( pEntry )
		removeItem( action );

	pEntry = new KKeyEntry;
	aKeyDict.insert( action, pEntry );
	
	pEntry->aDefaultKeyCode = keyCode;
	pEntry->aCurrentKeyCode = keyCode;
	pEntry->bConfigurable = configurable;
	pEntry->bEnabled = false;
	pEntry->aAccelId = 0;
	pEntry->receiver = 0;
	pEntry->member = 0;
	pEntry->descr = descr;

	return true;
}


kdelibs'KGlobalAccel::insertItem() (./kdelibs/kdecore/kglobalaccel.cpp:255)

bool KGlobalAccel::insertItem( const QString& descr, const QString& action,
					   const QString& keyCode, bool configurable )
{
	uint iKeyCode = KAccel::stringToKey( keyCode );
	return insertItem(descr, action, iKeyCode, configurable);
}


kdelibs'KGlobalAccel::isEnabled() (./kdelibs/kdecore/kglobalaccel.cpp:262)

bool KGlobalAccel::isEnabled() const
{
	return bEnabled;
}


kdelibs'KGlobalAccel::isItemEnabled() (./kdelibs/kdecore/kglobalaccel.cpp:267)

bool KGlobalAccel::isItemEnabled( const QString& action ) const
{
	KKeyEntry *pEntry = aKeyDict[ action ];
	
	if ( !pEntry )
        return false;
    else
        return pEntry->bEnabled;
}


kdelibs'KGlobalAccel::keyDict() (./kdelibs/kdecore/kglobalaccel.cpp:277)

QDict<KKeyEntry> KGlobalAccel::keyDict()
{
	return aKeyDict;
}


kdelibs'KGlobalAccel::readSettings() (./kdelibs/kdecore/kglobalaccel.cpp:282)

void KGlobalAccel::readSettings()
{
	QString s;
	//KConfig *pConfig = kapp->getConfig();
	KConfig globalConfig;// this way we are certain to get the global stuff!
	KConfig *pConfig = &globalConfig;

	pConfig->setGroup( aGroup );

	QDictIterator<KKeyEntry> aKeyIt( aKeyDict );
	aKeyIt.toFirst();

kdelibs'KGlobalAccel::removeItem() (./kdelibs/kdecore/kglobalaccel.cpp:327)

void KGlobalAccel::removeItem( const QString& action )
{
    KKeyEntry *pEntry = aKeyDict[ action ];
	
    if ( !pEntry )
		return;
	
	if ( pEntry->aAccelId ) {
	}
	
	aKeyDict.remove( action );
}


kdelibs'KGlobalAccel::setConfigGroup() (./kdelibs/kdecore/kglobalaccel.cpp:340)

void KGlobalAccel::setConfigGroup( const QString& group )
{
	aGroup = group;
}


kdelibs'KGlobalAccel::configGroup() (./kdelibs/kdecore/kglobalaccel.cpp:345)

QString KGlobalAccel::configGroup() const
{
	return aGroup;
}


kdelibs'KGlobalAccel::setEnabled() (./kdelibs/kdecore/kglobalaccel.cpp:350)

void KGlobalAccel::setEnabled( bool activate )
{
	QDictIterator<KKeyEntry> aKeyIt( aKeyDict );
	aKeyIt.toFirst();

kdelibs'KGlobalAccel::setItemEnabled() (./kdelibs/kdecore/kglobalaccel.cpp:363)

void KGlobalAccel::setItemEnabled( const QString& action, bool activate )
{	

    KKeyEntry *pEntry = aKeyDict[ action ];
	if ( !pEntry ) {
	    QString str = i18n("KGlobalAccel : cannont enable action %1 "
			       "which is not in the object dictionary").arg(action);
	    warning( str.ascii() );
	    return;
	}

	bool old = pEntry->bEnabled;
	pEntry->bEnabled = activate;
	if ( pEntry->bEnabled == old ) return;

	if ( pEntry->aCurrentKeyCode == 0 ) return;
	
	uint keysym = keyToXSym( pEntry->aCurrentKeyCode );
	uint mod = keyToXMod( pEntry->aCurrentKeyCode );
	
	if ( keysym == NoSymbol ) return;

	if ( pEntry->bEnabled ) {
    		grabKey( keysym, mod );
	} else {
		ungrabKey( keysym, mod );
	}

	return;
}


kdelibs'KGlobalAccel::setKeyDict() (./kdelibs/kdecore/kglobalaccel.cpp:394)

bool KGlobalAccel::setKeyDict( QDict<KKeyEntry> nKeyDict )
{
	// ungrab all connected and enabled keys
	QDictIterator<KKeyEntry> *aKeyIt = new QDictIterator<KKeyEntry>( aKeyDict );
	aKeyIt->toFirst();

kdelibs'KGlobalAccel::ungrabKey() (./kdelibs/kdecore/kglobalaccel.cpp:448)

bool KGlobalAccel::ungrabKey( uint keysym, uint mod ) {
	// Most of this comes from kpanel/main.C
	// Copyright (C) 1996,97 Matthias Ettrich
	static int NumLockMask = 0;
	
	if (do_not_grab)
	  return true;

	if (!keysym||!XKeysymToKeycode(qt_xdisplay(), keysym)) return false;
	if (!NumLockMask){
		XModifierKeymap* xmk = XGetModifierMapping(qt_xdisplay());
		int i;
		for (i=0; i<8; i++){
		   if (xmk->modifiermap[xmk->max_keypermod * i] ==
		   		XKeysymToKeycode(qt_xdisplay(), XK_Num_Lock))
		   			NumLockMask = (1<<i);
		}
	}

	grabFailed = false;

	// We wan't to catch only our own errors
	XSync(qt_xdisplay(),0);
	XErrorHandler savedErrorHandler=XSetErrorHandler(XGrabErrorHandler);
	
	XUngrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod,
		qt_xrootwin());
	XUngrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod | LockMask,
		qt_xrootwin());
	XUngrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod | NumLockMask,
		qt_xrootwin());
	XUngrabKey(qt_xdisplay(),
		XKeysymToKeycode(qt_xdisplay(), keysym), mod | LockMask | NumLockMask,
		qt_xrootwin());

	XSync(qt_xdisplay(),0);
	XSetErrorHandler(savedErrorHandler);
	if (grabFailed) {
		// FIXME: ungrab all successfull grabs!
		//warning("Global grab failed!");
   		return false;
	}
	return true;
}


kdelibs'KGlobalAccel::writeSettings() (./kdelibs/kdecore/kglobalaccel.cpp:496)

void KGlobalAccel::writeSettings()
{
	// KConfig *pConfig = kapp->getConfig();
	KConfig globalConfig;// this way we are certain to get the global stuff!
	KConfig *pConfig = &globalConfig;

	pConfig->setGroup( aGroup );

	QDictIterator<KKeyEntry> aKeyIt( aKeyDict );
	aKeyIt.toFirst();
	while ( aKeyIt.current() ) {
	  if ( aKeyIt.current()->bConfigurable ){
		  pConfig->writeEntry( aKeyIt.currentKey(),
				       KAccel::keyToString( aKeyIt.current()->aCurrentKeyCode),
				       true, true);
	  }
		++aKeyIt;
	}
	pConfig->sync();
}


kdelibs'KGlobalAccel::x11EventFilter() (./kdelibs/kdecore/kglobalaccel.cpp:517)

bool KGlobalAccel::x11EventFilter( const XEvent *event_ ) {

	if ( aKeyDict.isEmpty() ) return false;
	if ( event_->type != KeyPress ) return false;
	
	uint mod=event_->xkey.state & (ControlMask | ShiftMask | Mod1Mask);
	uint keysym= XKeycodeToKeysym(qt_xdisplay(), event_->xkey.keycode, 0);
	

	QDictIterator<KKeyEntry> *aKeyIt = new QDictIterator<KKeyEntry>( aKeyDict );
	aKeyIt->toFirst();