globalaccel.cpp

来自「可以播放MP3,wma等文件格式的播放器」· C++ 代码 · 共 683 行 · 第 1/2 页

CPP
683
字号
    // todo}        void GlobalAccel::removeItem( const QString& action ){    aKeyMap.remove(action);}void GlobalAccel::setConfigGroup( const QString& group ){    aGroup = group;}QString GlobalAccel::configGroup() const{    return aGroup;}void GlobalAccel::setEnabled( bool activate ){    for (KeyEntryMap::ConstIterator it = aKeyMap.begin();         it != aKeyMap.end(); ++it)        setItemEnabled( it.key(), activate );    bEnabled = activate;}void GlobalAccel::setItemEnabled( const QString& action, bool activate ){           if ( !aKeyMap.contains(action) ) {        std::cerr << QString::fromLatin1("GlobalAccel : cannot enable action %1 "                                    "which is not in the object dictionary\n").arg(action);        return;    }    KeyEntry& entry = aKeyMap[action];    if( entry.bEnabled == activate )        return;    aKeyMap[action].bEnabled = activate;    if ( entry.aCurrentKeyCode == 0 ) return;            uint keysym = keyToXSym( entry.aCurrentKeyCode );    uint mod = keyToXMod( entry.aCurrentKeyCode );    if ( keysym == NoSymbol ) return;    if ( entry.bEnabled )        grabKey( keysym, mod );    else        ungrabKey( keysym, mod );}bool GlobalAccel::setKeyDict( const KeyEntryMap& nKeyMap ){    for (KeyEntryMap::ConstIterator it = aKeyMap.begin();         it != aKeyMap.end(); ++it) {        // ungrab all connected and enabled keys        QString s;        if ( (*it).bEnabled ) {            uint keysym = keyToXSym( (*it).aCurrentKeyCode );            uint mod = keyToXMod( (*it).aCurrentKeyCode );            ungrabKey( keysym, mod );        }    }            // Clear the dictionary    aKeyMap.clear();            // Insert the new items into the dictionary and reconnect if neccessary    // Note also swap config and current key codes !!!    for (KeyEntryMap::ConstIterator it = nKeyMap.begin();         it != nKeyMap.end(); ++it) {        KeyEntry entry = *it;        // Not we write config key code to current key code !!        entry.aCurrentKeyCode = (*it).aConfigKeyCode;                aKeyMap[it.key()] = entry;        if ( entry.bEnabled ) {            uint keysym = keyToXSym( entry.aCurrentKeyCode );            uint mod = keyToXMod( entry.aCurrentKeyCode );            grabKey( keysym, mod );        }                    }    return true;}bool GlobalAccel::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);        }        XFreeModifiermap(xmk);    }    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;}void GlobalAccel::writeSettings() const{    // todo}bool GlobalAccel::x11EventFilter( const XEvent *event_ ) {    if ( aKeyMap.isEmpty() ) return false;    if ( event_->type != XKeyPress ) return false;            uint mod=event_->xkey.state & (ControlMask | ShiftMask | Mod1Mask);    uint keysym= XKeycodeToKeysym(qt_xdisplay(), event_->xkey.keycode, 0);            KeyEntry entry;    for (KeyEntryMap::ConstIterator it = aKeyMap.begin();         it != aKeyMap.end(); ++it) {        int kc = (*it).aCurrentKeyCode;        if ( mod == keyToXMod( kc ) && keysym == keyToXSym( kc ) ) {            entry = *it;        }    }            if ( !entry.receiver || !entry.bEnabled )        return false;            if ( !QWidget::keyboardGrabber() ) {        XAllowEvents(qt_xdisplay(), AsyncKeyboard, CurrentTime);        XUngrabKeyboard(qt_xdisplay(), CurrentTime);        XSync(qt_xdisplay(), false);        connect( this, SIGNAL( activated() ),                 entry.receiver, entry.member);        emit activated();        disconnect( this, SIGNAL( activated() ), entry.receiver,                    entry.member );    }    return true;}/*****************************************************************************/uint GlobalAccel::keyToXMod( int keyCode ){    uint mod = 0;            if ( keyCode == 0 ) return mod;            if ( keyCode & Qt::SHIFT )        mod |= ShiftMask;    if ( keyCode & Qt::CTRL )        mod |= ControlMask;    if ( keyCode & Qt::ALT )        mod |= Mod1Mask;                    return mod;}uint GlobalAccel::keyToXSym( int keyCode ){    char *toks[4], *next_tok;    char sKey[100];    int nb_toks = 0;    uint keysym = 0;    QString s = keyToString( keyCode);            if ( s.isEmpty() ) return keysym;    qstrncpy(sKey, s.ascii(), sizeof(sKey));    next_tok = strtok( sKey, "+" );    if ( next_tok == 0L ) return 0;            do {        toks[nb_toks] = next_tok;        nb_toks++;        if ( nb_toks == 5 ) return 0;        next_tok = strtok( 0L, "+" );    } while ( next_tok != 0L );    // Test for exactly one key (other tokens are accelerators)    // Fill the keycode with infos    bool  keyFound = false;    for ( int i=0; i<nb_toks; i++ ) {        if (qstricmp(toks[i], "SHIFT") != 0 &&            qstricmp(toks[i], "CTRL")  != 0 &&            qstricmp(toks[i], "ALT")   != 0) {            if ( keyFound ) return 0;            keyFound = true;            QCString l = toks[i];            l = l.lower();            keysym = XStringToKeysym(l.data());            if (keysym == NoSymbol){                keysym = XStringToKeysym( toks[i] );            }            if ( keysym == NoSymbol ) {                return 0;            }        }    }            return keysym;}// from KAccel from the KDE librariesQString GlobalAccel::keyToString( int keyCode ){    QString res = "";    if ( keyCode == 0 ) return res;    if ( keyCode & Qt::SHIFT ){        res += "Shift+";    }    if ( keyCode & Qt::CTRL ){        res += "Ctrl+";    }    if ( keyCode & Qt::ALT ){        res += "Alt+";    }    int kCode = keyCode & ~(Qt::SHIFT | Qt::CTRL | Qt::ALT);    for (int i=0; i<NB_KEYS; i++) {        if ( kCode == (int)KKEYS[i].code ) {            res += KKEYS[i].name;            return res;        }    }            return QString::null;}int GlobalAccel::stringToKey(const QString& key){    // Empty string is interpreted as code zero, which is    // consistent with the behaviour of KAccel methods        if ( key.isNull() ) {        return 0;    } else if ( key.isEmpty() ) {        return 0;    }        // break the string in tokens separated by "+"    int k = 0;    QArray<int> tokens;    int plus = -1;    do {        tokens.resize(k+1);        tokens[k] = plus+1;        plus = key.find('+', plus+1);        k++;    } while ( plus!=-1 );    tokens.resize(k+1);    tokens[k] = key.length() + 1;        // we have k tokens.    // find a keycode (only one)    // the other tokens are accelerators (SHIFT, CTRL & ALT)    // the order is unimportant    bool codeFound = false;    QString str;    int keyCode = 0;    for (int i=0; i<k; i++) {        str = key.mid(tokens[i], tokens[i+1]-tokens[i]-1);        str.stripWhiteSpace();        if ( str.isEmpty() ) {            std::cerr << "GlobalAccel::stringToKey::Empty token" << std::endl;            return 0;        }                if ( k!=1 ) { // for e.g. "Shift" can be a modifier or a key            if ( str.upper()=="SHIFT" )     { keyCode |= Qt::SHIFT; continue; }            if ( str.upper()=="CTRL" )      { keyCode |= Qt::CTRL;  continue; }            if ( str.upper()=="ALT" )       { keyCode |= Qt::ALT;   continue; }        }                if (codeFound) {            std::cerr << "GlobalAccell::stringToKey::Duplicate keycode" << std::endl;            return 0;        }        // search for keycode        int j;        for(j=0; j<NB_KEYS; j++) {            if ( str==KKEYS[j].name ) {                keyCode |= KKEYS[j].code;                break;            }        }        if ( j==NB_KEYS ) {            std::cerr << "globalaccel::stringToKey::Unknown key name " << str << std::endl;            return 0;        }    }        return keyCode;}// end from KAccel

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?