kdecore Library API Documentation

kshortcutlist.cpp

00001 #include <qstring.h>
00002 #include <qvariant.h>
00003 
00004 #include <kaccel.h>
00005 #include <kaccelaction.h>
00006 #include <kconfig.h>
00007 #include <kdebug.h>
00008 #include <kglobal.h>
00009 #include <kglobalaccel.h>
00010 #include <kinstance.h>
00011 #include <kshortcut.h>
00012 #include "kshortcutlist.h"
00013 
00014 //---------------------------------------------------------------------
00015 // KShortcutList
00016 //---------------------------------------------------------------------
00017 
00018 KShortcutList::KShortcutList()
00019 {
00020 }
00021 
00022 KShortcutList::~KShortcutList()
00023 {
00024 }
00025 
00026 bool KShortcutList::isGlobal( uint ) const
00027 {
00028     return false;
00029 }
00030 
00031 // FIXME: implement!
00032 int KShortcutList::index( const QString& /*sName*/ ) const
00033 {
00034     return -1;
00035 }
00036 
00037 int KShortcutList::index( const KKeySequence& seq ) const
00038 {
00039     if( seq.isNull() )
00040         return -1;
00041 
00042     uint nSize = count();
00043     for( uint i = 0; i < nSize; i++ ) {
00044         if( shortcut(i).contains( seq ) )
00045             return i;
00046     }
00047 
00048     return -1;
00049 }
00050 
00051 const KInstance* KShortcutList::instance() const
00052 {
00053     return 0;
00054 }
00055 
00056 QVariant KShortcutList::getOther( Other, uint ) const
00057 {
00058     return QVariant();
00059 }
00060 
00061 bool KShortcutList::setOther( Other, uint, QVariant )
00062 {
00063     return false;
00064 }
00065 
00066 bool KShortcutList::readSettings( const QString& sConfigGroup, KConfigBase* pConfig )
00067 {
00068     kdDebug(125) << "KShortcutList::readSettings( \"" << sConfigGroup << "\", " << pConfig << " ) start" << endl;
00069     if( !pConfig )
00070         pConfig = KGlobal::config();
00071     QString sGroup = (!sConfigGroup.isEmpty()) ? sConfigGroup : QString("Shortcuts");
00072 
00073     // If the config file still has the old group name:
00074     // FIXME: need to rename instead? -- and don't do this if hasGroup( "Shortcuts" ).
00075     if( sGroup == "Shortcuts" && pConfig->hasGroup( "Keys" ) ) {
00076         readSettings( "Keys", pConfig );
00077     }
00078 
00079     kdDebug(125) << "\treadSettings( \"" << sGroup << "\", " << pConfig << " )" << endl;
00080     if( !pConfig->hasGroup( sGroup ) )
00081         return true;
00082     KConfigGroupSaver cgs( pConfig, sGroup );
00083 
00084     uint nSize = count();
00085     for( uint i = 0; i < nSize; i++ ) {
00086         if( isConfigurable(i) ) {
00087             QString sEntry = pConfig->readEntry( name(i) );
00088             if( !sEntry.isEmpty() ) {
00089                 if( sEntry == "none" )
00090                     setShortcut( i, KShortcut() );
00091                 else
00092                     setShortcut( i, KShortcut(sEntry) );
00093             }
00094             else // default shortcut
00095                 setShortcut( i, shortcutDefault(i) );
00096             kdDebug(125) << "\t" << name(i) << " = '" << sEntry << "'" << endl;
00097         }
00098     }
00099 
00100     kdDebug(125) << "KShortcutList::readSettings done" << endl;
00101     return true;
00102 }
00103 
00104 bool KShortcutList::writeSettings( const QString &sConfigGroup, KConfigBase* pConfig, bool bWriteAll, bool bGlobal ) const
00105 {
00106     kdDebug(125) << "KShortcutList::writeSettings( " << sConfigGroup << ", " << pConfig << ", " << bWriteAll << ", " << bGlobal << " )" << endl;
00107     if( !pConfig )
00108         pConfig = KGlobal::config();
00109 
00110     QString sGroup = (!sConfigGroup.isEmpty()) ? sConfigGroup : QString("Shortcuts");
00111 
00112     // If it has the depricated group [Keys], remove it
00113     if( pConfig->hasGroup( "Keys" ) )
00114         pConfig->deleteGroup( "Keys", true );
00115 
00116     KConfigGroupSaver cs( pConfig, sGroup );
00117 
00118     uint nSize = count();
00119     for( uint i = 0; i < nSize; i++ ) {
00120         if( isConfigurable(i) ) {
00121             const QString& sName = name(i);
00122             bool bConfigHasAction = !pConfig->readEntry( sName ).isEmpty();
00123             bool bSameAsDefault = (shortcut(i) == shortcutDefault(i));
00124             // If we're using a global config or this setting
00125             //  differs from the default, then we want to write.
00126             if( bWriteAll || !bSameAsDefault ) {
00127                 QString s = shortcut(i).toStringInternal();
00128                 if( s.isEmpty() )
00129                     s = "none";
00130                 kdDebug(125) << "\twriting " << sName << " = " << s << endl;
00131                 pConfig->writeEntry( sName, s, true, bGlobal );
00132             }
00133             // Otherwise, this key is the same as default
00134             //  but exists in config file.  Remove it.
00135             else if( bConfigHasAction ) {
00136                 kdDebug(125) << "\tremoving " << sName << " because == default" << endl;
00137                 pConfig->deleteEntry( sName, false, bGlobal );
00138             }
00139         }
00140     }
00141 
00142     pConfig->sync();
00143     return true;
00144 }
00145 
00146 //---------------------------------------------------------------------
00147 // KAccelShortcutList
00148 //---------------------------------------------------------------------
00149 
00150 KAccelShortcutList::KAccelShortcutList( KAccel* pAccel )
00151 : m_actions( pAccel->actions() )
00152 {
00153     m_bGlobal = false;
00154 }
00155 
00156 KAccelShortcutList::KAccelShortcutList( KGlobalAccel* pAccel )
00157 : m_actions( pAccel->actions() )
00158 {
00159     m_bGlobal = true;
00160 }
00161 
00162 KAccelShortcutList::KAccelShortcutList( KAccelActions& actions, bool bGlobal )
00163 : m_actions( actions )
00164 {
00165     m_bGlobal = bGlobal;
00166 }
00167 
00168 
00169 KAccelShortcutList::~KAccelShortcutList()
00170     { }
00171 uint KAccelShortcutList::count() const
00172     { return m_actions.count(); }
00173 QString KAccelShortcutList::name( uint i ) const
00174     { return m_actions.actionPtr(i)->name(); }
00175 QString KAccelShortcutList::label( uint i ) const
00176     { return m_actions.actionPtr(i)->label(); }
00177 QString KAccelShortcutList::whatsThis( uint i ) const
00178     { return m_actions.actionPtr(i)->whatsThis(); }
00179 const KShortcut& KAccelShortcutList::shortcut( uint i ) const
00180     { return m_actions.actionPtr(i)->shortcut(); }
00181 const KShortcut& KAccelShortcutList::shortcutDefault( uint i ) const
00182     { return m_actions.actionPtr(i)->shortcutDefault(); }
00183 bool KAccelShortcutList::isConfigurable( uint i ) const
00184     { return m_actions.actionPtr(i)->isConfigurable(); }
00185 bool KAccelShortcutList::setShortcut( uint i, const KShortcut& cut )
00186     { return m_actions.actionPtr(i)->setShortcut( cut ); }
00187 QVariant KAccelShortcutList::getOther( Other, uint ) const
00188     { return QVariant(); }
00189 bool KAccelShortcutList::isGlobal( uint ) const
00190     { return m_bGlobal; }
00191 bool KAccelShortcutList::setOther( Other, uint, QVariant )
00192     { return false; }
00193 bool KAccelShortcutList::save() const
00194     { return writeSettings(); }
00195 
00196 void KShortcutList::virtual_hook( int, void* )
00197 { /*BASE::virtual_hook( id, data );*/ }
00198 
00199 void KAccelShortcutList::virtual_hook( int id, void* data )
00200 { KShortcutList::virtual_hook( id, data ); }
00201 
00202 void KStdAccel::ShortcutList::virtual_hook( int id, void* data )
00203 { KShortcutList::virtual_hook( id, data ); }
00204 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:41 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001