KDEUI
kkeyserver.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2001 Ellis Whitehead <ellis@kde.org> 00003 00004 Win32 port: 00005 Copyright (C) 2004 Jarosław Staniek <staniek@kde.org> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "kkeyserver.h" 00024 00025 #include <config.h> 00026 #include <klocale.h> 00027 #include <kglobal.h> 00028 #include <kconfig.h> 00029 #include <kconfiggroup.h> 00030 00031 namespace KKeyServer { 00032 //--------------------------------------------------------------------- 00033 // Array Structures 00034 //--------------------------------------------------------------------- 00035 00036 struct ModInfo 00037 { 00038 int modQt; 00039 const char* psName; 00040 QString* sLabel; // this struct is used in static objects, so must use a pointer here. 00041 }; 00042 00043 //--------------------------------------------------------------------- 00044 // Arrays 00045 //--------------------------------------------------------------------- 00046 00047 // Key names with this context are extracted elsewhere, 00048 // no need for I18N_NOOP2's here. 00049 #define KEYCTXT "keyboard-key-name" 00050 static ModInfo g_rgModInfo[4] = 00051 { 00052 { Qt::SHIFT, "Shift", 0 }, 00053 { Qt::CTRL, "Ctrl", 0 }, 00054 { Qt::ALT, "Alt", 0 }, 00055 { Qt::META, "Meta", 0 } 00056 }; 00057 00058 //--------------------------------------------------------------------- 00059 // Initialization 00060 //--------------------------------------------------------------------- 00061 static bool g_bInitializedKKeyLabels; 00062 static bool g_bMacLabels; 00063 00064 static void intializeKKeyLabels() 00065 { 00066 KConfigGroup cg( KGlobal::config(), "Keyboard" ); 00067 g_rgModInfo[0].sLabel = new QString( cg.readEntry( "Label Shift", i18nc(KEYCTXT, g_rgModInfo[0].psName) ) ); 00068 g_rgModInfo[1].sLabel = new QString( cg.readEntry( "Label Ctrl", i18nc(KEYCTXT, g_rgModInfo[1].psName) ) ); 00069 g_rgModInfo[2].sLabel = new QString( cg.readEntry( "Label Alt", i18nc(KEYCTXT, g_rgModInfo[2].psName) ) ); 00070 g_rgModInfo[3].sLabel = new QString( cg.readEntry( "Label Win", i18nc(KEYCTXT, g_rgModInfo[3].psName) ) ); 00071 g_bMacLabels = (*g_rgModInfo[2].sLabel == "Command"); 00072 g_bInitializedKKeyLabels = true; 00073 00074 } 00075 00076 //--------------------------------------------------------------------- 00077 // Public functions 00078 //--------------------------------------------------------------------- 00079 00080 static QString modToString( uint mod, bool bUserSpace ) 00081 { 00082 if( bUserSpace && !g_bInitializedKKeyLabels ) 00083 intializeKKeyLabels(); 00084 00085 QString s; 00086 for( int i = 3; i >= 0; i-- ) { 00087 if( mod & g_rgModInfo[i].modQt ) { 00088 if( !s.isEmpty() ) 00089 s += '+'; 00090 s += (bUserSpace) 00091 ? *g_rgModInfo[i].sLabel 00092 : QString(g_rgModInfo[i].psName); 00093 } 00094 } 00095 return s; 00096 } 00097 00098 QString modToStringUser( uint mod ) 00099 { 00100 return modToString( mod, true ); 00101 } 00102 00103 uint stringUserToMod( const QString& mod ) 00104 { 00105 QString s; 00106 for( int i = 3; i >= 0; i-- ) { 00107 if( mod.toLower() == g_rgModInfo[i].sLabel->toLower()) 00108 return g_rgModInfo[i].modQt; 00109 } 00110 return 0; 00111 } 00112 00113 00114 }
KDE 4.6 API Reference