Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

KeyMappings.cpp

Go to the documentation of this file.
00001 #include "KeyMappings.h"
00002 
00003 static QIntDict<QWSServer::KeyMap> g_mapCache(127);
00004 
00005 MapInfo::MapInfo(int code, int mod, int uni,
00006         int shift_uni, int ctrl_uni)
00007 {
00008         const QWSServer::KeyMap* map;
00009         if(uni == 0 || shift_uni == 0 || ctrl_uni == 0){
00010                 map = MapInfo::findKeyMap(code);
00011                 if(map != NULL){
00012                         isDefined = true;
00013                         unicode = map->unicode;
00014                         shift_unicode = map->shift_unicode;
00015                         ctrl_unicode = map->ctrl_unicode;
00016                 } else {
00017                         isDefined = false;
00018                         unicode = 0;
00019                         shift_unicode = 0;
00020                         ctrl_unicode = 0;
00021                 }
00022         } else {
00023                 isDefined = true;
00024         }
00025         keycode = code;
00026         modifiers = mod;
00027         if(uni != 0){
00028                 unicode = uni;
00029         }
00030         if(shift_uni != 0){
00031                 shift_unicode = shift_uni;
00032         }
00033         if(ctrl_uni != 0){
00034                 ctrl_unicode = ctrl_uni;
00035         }
00036 }
00037 
00038 const QWSServer::KeyMap* MapInfo::findKeyMap(int keycode)
00039 {
00040         const QWSServer::KeyMap* m = QWSServer::keyMap();
00041 
00042         while(m->key_code != 0){
00043                 if(m->key_code == keycode){
00044                         return(m);
00045                 }
00046                 m++;
00047         }
00048         return(NULL);
00049 }
00050 
00051 KeyMappings::KeyMappings()
00052 {
00053         qDebug("KeyMappings::KeyMappings()");
00054         init();
00055 }
00056 
00057 KeyMappings::~KeyMappings()
00058 {
00059         qDebug("KeyMappings::~KeyMappings()");
00060         clear();
00061 }
00062 
00063 void KeyMappings::init()
00064 {
00065         m_capslock = false;
00066 }
00067 
00068 void KeyMappings::reset()
00069 {
00070         clear();
00071 }
00072 
00073 void KeyMappings::clear()
00074 {
00075         for(QMap<int, CodeMaps*>::Iterator it = m_keymaps.begin();
00076                         it!=m_keymaps.end(); ++it){
00077                 delete (*it);
00078         }
00079         m_keymaps.clear();
00080         g_mapCache.setAutoDelete(true);
00081         g_mapCache.clear();
00082 }
00083 
00084 void KeyMappings::assign(int keycode, int keymask, int mapcode,
00085         int mapmodifiers, int unicode, int shift_unicode, int ctrl_unicode)
00086 {
00087         CodeMaps* map;
00088         if(m_keymaps.contains(keycode)){
00089                 map = m_keymaps[keycode];
00090         } else {
00091                 map = new CodeMaps();
00092                 m_keymaps.insert(keycode, map);
00093         }
00094 
00095         MapInfo info(mapcode, mapmodifiers, unicode, shift_unicode, ctrl_unicode);
00096         m_it = map->insert(keymask, info);
00097 }
00098 
00099 void KeyMappings::assignModifier(const QString& type, const QString& state)
00100 {
00101         int maskbit = 0;
00102         QString str;
00103         str = type.lower();
00104         if(str == "shift"){
00105                 maskbit = Qt::ShiftButton;
00106         } else if(str == "control"){
00107                 maskbit = Qt::ControlButton;
00108         } else if(str == "alt"){
00109                 maskbit = Qt::AltButton;
00110         }
00111         str = state.lower();
00112         if(str == "off"){
00113                 maskbit = (maskbit << 16) & 0xFFFF0000;
00114         }
00115         (*m_it).modifiers |= maskbit;
00116 }
00117 
00118 void KeyMappings::assignUnicode(const QString& kind, const QString& ch)
00119 {
00120         QString str;
00121         int code = ch[0].unicode();
00122         str = kind.lower();
00123         if(str == "unicode"){
00124                 (*m_it).unicode = code;
00125         } else if(str == "shift_unicode"){
00126                 (*m_it).shift_unicode = code;
00127         } else if(str == "ctrl_unicode"){
00128                 (*m_it).ctrl_unicode = code;
00129         }
00130 }
00131 
00132 void KeyMappings::assignUnicode(int unicode)
00133 {
00134         (*m_it).unicode = (*m_it).shift_unicode = 
00135                 (*m_it).ctrl_unicode = unicode;
00136 }
00137 
00138 void KeyMappings::statistics()
00139 {
00140         qWarning("KeyMappings::statistics()");
00141         for(QMap<int, CodeMaps*>::Iterator it=m_keymaps.begin();
00142                         it!=m_keymaps.end(); ++it){
00143                 qWarning(" code = %x", it.key());
00144                 for(QMap<int, MapInfo>::Iterator it2=(*it)->begin();
00145                                 it2!=(*it)->end(); ++it2){
00146                         qDebug("  [%x]-[%x][%x][%x][%x][%x]", it2.key(),
00147                                 (*it2).keycode,
00148                                 (*it2).modifiers,
00149                                 (*it2).unicode,
00150                                 (*it2).shift_unicode,
00151                                 (*it2).ctrl_unicode);
00152                 }
00153         }
00154 }
00155 
00156 bool KeyMappings::apply(int unicode, int keycode, int modifiers,
00157         int keymask, bool isPress)
00158 {
00159         CodeMaps* map;
00160         m_isMapped = false;
00161 
00162         if(m_keymaps.contains(keycode)){
00163                 map = m_keymaps[keycode];
00164                 if(map->contains(keymask)){
00165                         m_isMapped = true;
00166                         m_keyinfo = (*map)[keymask];
00167                 } else {
00168                         int mask = -1;
00169                         for(CodeMaps::Iterator it=map->begin();
00170                                         it!=map->end(); ++it){
00171                                 if((keymask & it.key()) == it.key()
00172                                         && it.key() > mask){
00173                                         mask = it.key();
00174                                 }
00175                         }
00176                         if(mask != -1){
00177                                 m_isMapped = true;
00178                                 m_keyinfo = (*map)[mask];
00179                         }
00180                 }
00181         }
00182 
00183         if(m_isMapped == false){
00184                 QWSServer::KeyMap* cache = g_mapCache[keycode];
00185                 if(cache == NULL){
00186                         cache = new QWSServer::KeyMap();
00187                         g_mapCache.insert(keycode, cache);
00188                         cache->unicode = cache->shift_unicode = cache->ctrl_unicode = 0;
00189                 }
00190                 if(cache->unicode == 0 || cache->shift_unicode == 0 || cache->ctrl_unicode == 0){
00191                         QChar ch(unicode);
00192                         if(modifiers & Qt::ControlButton){
00193                                 cache->ctrl_unicode = unicode;
00194                         } else if(modifiers & Qt::ShiftButton){
00195                                 cache->shift_unicode = ch.upper().unicode();
00196                         } else {
00197                                 cache->unicode = ch.lower().unicode();
00198                         }
00199                 }
00200                 m_keyinfo = MapInfo(keycode, 0,
00201                         cache->unicode, cache->shift_unicode, cache->ctrl_unicode);
00202                 if(m_keyinfo.isDefined){
00203                         setOriginal(unicode, modifiers);
00204                 } else {
00205                         setUnicode(unicode);
00206                 }
00207         }
00208 
00209 #if 1
00210         if(isPress){
00211                 if(m_keyinfo.keycode == Qt::Key_CapsLock){
00212                         m_capslock = !m_capslock;
00213                 }
00214         }
00215 #endif
00216         return(m_isMapped);
00217 }
00218 
00219 bool KeyMappings::apply(int keycode, int keymask, bool isPress)
00220 {
00221         CodeMaps* map;
00222         m_isMapped = false;
00223 
00224         if(m_keymaps.contains(keycode)){
00225                 map = m_keymaps[keycode];
00226                 if(map->contains(keymask)){
00227                         m_isMapped = true;
00228                         m_keyinfo = (*map)[keymask];
00229                 } else {
00230                         int mask = -1;
00231                         for(CodeMaps::Iterator it=map->begin();
00232                                         it!=map->end(); ++it){
00233                                 if((keymask & it.key()) == it.key()
00234                                         && it.key() > mask){
00235                                         mask = it.key();
00236                                 }
00237                         }
00238                         if(mask != -1){
00239                                 m_isMapped = true;
00240                                 m_keyinfo = (*map)[mask];
00241                         }
00242                 }
00243         }
00244         if(m_isMapped == false){
00245                 m_keyinfo = MapInfo(keycode);
00246         }
00247 #if 1
00248         if(isPress){
00249                 if(m_keyinfo.keycode == Qt::Key_CapsLock){
00250                         m_capslock = !m_capslock;
00251                 }
00252         }
00253 #endif
00254         return(m_isMapped);
00255 }
00256 
00260 void KeyMappings::setOriginal(int unicode, int modifiers)
00261 {
00262         if(modifiers & Qt::ControlButton){
00263                 m_keyinfo.ctrl_unicode = unicode;
00264         } else if(modifiers & Qt::ShiftButton){
00265                 m_keyinfo.shift_unicode = unicode;
00266         } else {
00267                 m_keyinfo.unicode = unicode;
00268         }
00269 }
00270 
00271 void KeyMappings::setModifiers(int modifiers)
00272 {
00273         m_modifiers = modifiers;
00274         m_modifiers |= (m_keyinfo.modifiers & 0xFFFF);
00275         m_modifiers &= ~((m_keyinfo.modifiers >> 16) & 0xFFFF);
00276 }
00277 
00278 void KeyMappings::setUnicode(int unicode)
00279 {
00280         m_keyinfo.unicode = unicode;
00281         m_keyinfo.shift_unicode = unicode;
00282         m_keyinfo.ctrl_unicode = unicode;
00283 }
00284 
00285 void KeyMappings::setKeycode(int keycode)
00286 {
00287         m_keyinfo.keycode = keycode;
00288 }
00289 
00290 int KeyMappings::getUnicode()
00291 {
00292         int unicode;
00293         if(m_modifiers & Qt::ControlButton){
00294                 unicode = m_keyinfo.ctrl_unicode;
00295         } else if(m_modifiers & Qt::ShiftButton){
00296                 unicode = m_keyinfo.shift_unicode;
00297                 QChar ch(unicode);
00298                 if(m_capslock){
00299                         unicode = ch.lower().unicode();
00300                 } else {
00301                         unicode = ch.upper().unicode();
00302                 }
00303         } else {
00304                 unicode = m_keyinfo.unicode;
00305                 QChar ch(unicode);
00306                 if(m_capslock){
00307                         unicode = ch.upper().unicode();
00308                 } else {
00309                         unicode = ch.lower().unicode();
00310                 }
00311         }
00312         qDebug("UNICODE[%d][%x][%x]", m_capslock, unicode, m_keyinfo.unicode);
00313 #if 0
00314         if(m_isMapped && m_capslock){
00315                 QChar ch(unicode);
00316                 QChar::Category category = ch.category();
00317                 if(category == QChar::Letter_Uppercase){
00318                         unicode = ch.lower().unicode();
00319                 } else if(category == QChar::Letter_Lowercase){
00320                         unicode = ch.upper().unicode();
00321                 }
00322         }
00323 #endif
00324         return(unicode);
00325 }
00326 
00327 int KeyMappings::getKeycode()
00328 {
00329         return(m_keyinfo.keycode);
00330 }
00331 
00332 int KeyMappings::getModifiers()
00333 {
00334         return(m_modifiers);
00335 }
00336 
00337 bool KeyMappings::isDefined()
00338 {
00339         return(m_keyinfo.isDefined);
00340 }

Generated on Sat Nov 5 16:16:43 2005 for OPIE by  doxygen 1.4.2