[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdelibs/kdeui/actions
From:       Michael Jansen <kde () michael-jansen ! biz>
Date:       2008-02-29 23:43:37
Message-ID: 1204328617.735674.20714.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 780778 by mjansen:

Add the possibility to save global shortcuts to a configuration. Normally the global \
shortcuts are handled completly from kdedglobalaccel. These function are needed to \
export import shortcut schemes.

 M  +11 -0     kaction.cpp  
 M  +13 -0     kaction.h  
 M  +93 -19    kactioncollection.cpp  
 M  +17 -0     kactioncollection.h  


--- trunk/KDE/kdelibs/kdeui/actions/kaction.cpp #780777:780778
@@ -171,6 +171,17 @@
   return d->globalShortcut;
 }
 
+bool KAction::isLocalShortcutModified() const
+{
+  return property("defaultPrimaryShortcut").value<QKeySequence>() != \
shortcuts().value(0) +      || \
property("defaultAlternateShortcut").value<QKeySequence>() != shortcuts().value(1); \
+} +
+bool KAction::isGlobalShortcutModified() const
+{
+  return d->defaultGlobalShortcut != d->globalShortcut;
+}
+
 void KAction::setGlobalShortcut( const KShortcut & shortcut, ShortcutTypes type,
                                  GlobalShortcutLoading load )
 {
--- trunk/KDE/kdelibs/kdeui/actions/kaction.h #780777:780778
@@ -256,6 +256,19 @@
     virtual ~KAction();
 
     /**
+     * Differs the shortcut from the default setting?
+     *
+     * @return yes/no
+     */
+    //@{
+    inline bool isModified() const {
+        return isLocalShortcutModified() || isGlobalShortcutModified();
+    }
+    bool isLocalShortcutModified() const;
+    bool isGlobalShortcutModified() const;
+    //@}
+
+    /**
      * Get the shortcut for this action.
      *
      * This is preferred over QAction::shortcut(), as it allows for multiple \
                shortcuts
--- trunk/KDE/kdelibs/kdeui/actions/kactioncollection.cpp #780777:780778
@@ -30,9 +30,9 @@
 #include "kxmlguiclient.h"
 #include "kxmlguifactory.h"
 
-#include <kdebug.h>
-#include <kglobal.h>
-#include <kaction.h>
+#include "kdebug.h"
+#include "kglobal.h"
+#include "kaction.h"
 
 #include <QtXml/QDomDocument>
 #include <QSet>
@@ -43,8 +43,8 @@
 #include <QAction>
 
 #include <stdio.h>
-#include <kcomponentdata.h>
-#include <kconfiggroup.h>
+#include "kcomponentdata.h"
+#include "kconfiggroup.h"
 
 class KActionCollectionPrivate
 {
@@ -322,15 +322,46 @@
   d->configIsGlobal = global;
 }
 
+void KActionCollection::readGlobalSettings( KConfigGroup* config )
+{
+  Q_ASSERT(config);
+  if (!config) {
+    return;
+  }
+
+  if( !config || !config->exists()) {
+    return;
+  }
+
+  for (QMap<QString, QAction *>::ConstIterator it = d->actionByName.constBegin();
+       it != d->actionByName.constEnd(); ++it) {
+      KAction *kaction = qobject_cast<KAction*>(it.value());
+      if (!kaction)
+          continue;
+
+      QString actionName = it.key();
+
+      if( kaction->isShortcutConfigurable() ) {
+          QString entry = config->readEntry(actionName, QString());
+          if( !entry.isEmpty() ) {
+              kaction->setGlobalShortcut( KShortcut(entry), KAction::ActiveShortcut, \
KAction::NoAutoloading ); +          } else {
+              kaction->setGlobalShortcut( \
kaction->shortcut(KAction::DefaultShortcut), KAction::ActiveShortcut, \
KAction::NoAutoloading ); +          }
+      }
+  }
+}
+
+
 void KActionCollection::readSettings( KConfigGroup* config )
 {
   KConfigGroup cg( KGlobal::config(), configGroup() );
   if( !config )
       config = &cg;
 
-  kDebug(125) << configGroup() << config->exists();
-  if( !config->exists())
+  if( !config->exists()) {
     return;
+  }
 
   for (QMap<QString, QAction *>::ConstIterator it = d->actionByName.constBegin();
        it != d->actionByName.constEnd(); ++it) {
@@ -343,25 +374,66 @@
       if( kaction->isShortcutConfigurable() ) {
           QString entry = config->readEntry(actionName, QString());
           if( !entry.isEmpty() ) {
-              if( entry == "none" )
-                  kaction->setShortcut( KShortcut(), KAction::ActiveShortcut );
-              else
-                  kaction->setShortcut( KShortcut(entry), KAction::ActiveShortcut );
+              kaction->setShortcut( KShortcut(entry), KAction::ActiveShortcut );
+          } else {
+              kaction->setShortcut( kaction->shortcut(KAction::DefaultShortcut) );
           }
-          else // default shortcut
-              kaction->setShortcut( kaction->shortcut(KAction::DefaultShortcut) );
-
-          kDebug(125) << actionName << "=" << entry;
       }
   }
 
   //kDebug(125) << " done";
 }
 
+void KActionCollection::writeGlobalSettings( KConfigGroup* config, bool writeAll, \
QAction* oneAction ) const +{
+  Q_ASSERT(config);
+  if (!config) {
+    return;
+  }
+
+  QList<QAction*> writeActions;
+  if (oneAction)
+    writeActions.append(oneAction);
+  else
+    writeActions = actions();
+
+  for (QMap<QString, QAction *>::ConstIterator it = d->actionByName.constBegin();
+       it != d->actionByName.constEnd(); ++it) {
+
+      KAction *kaction = qobject_cast<KAction*>(it.value());
+      if (!kaction)
+          continue;
+      QString actionName = it.key();
+
+      if( kaction->isShortcutConfigurable() && kaction->globalShortcutAllowed() ) {
+          bool bConfigHasAction = !config->readEntry( actionName, QString() \
).isEmpty(); +          bool bSameAsDefault = kaction->isGlobalShortcutModified();
+          // If we're using a global config or this setting
+          //  differs from the default, then we want to write.
+          KConfigGroup::WriteConfigFlags flags = KConfigGroup::Persistent;
+          if (configIsGlobal())
+              flags |= KConfigGroup::Global;
+          if( writeAll || !bSameAsDefault ) {
+              QString s = kaction->globalShortcut().toString();
+              if( s.isEmpty() )
+                  s = "none";
+              kDebug(125) << "\twriting " << actionName << " = " << s;
+              config->writeEntry( actionName, s, flags );
+          }
+          // Otherwise, this key is the same as default
+          //  but exists in config file.  Remove it.
+          else if( bConfigHasAction ) {
+              kDebug(125) << "\tremoving " << actionName << " because == default";
+              config->deleteEntry( actionName, flags );
+          }
+      }
+  }
+
+  config->sync();
+}
+
 void KActionCollection::writeSettings( KConfigGroup* config, bool writeAll, QAction* \
oneAction ) const  {
-  kDebug(125) << configGroup() << ", " << config << ", " << writeAll << ", " << \
                configIsGlobal();
-
   if (parentGUIClient() && !parentGUIClient()->xmlFile().isEmpty()) {
     kDebug(129) << "xmlFile=" << parentGUIClient()->xmlFile();
 
@@ -386,8 +458,10 @@
 
       QString actionName = it.key();
 
-      bool bSameAsDefault = (kaction->shortcut(KAction::ActiveShortcut) == \
                kaction->shortcut(KAction::DefaultShortcut));
-      kDebug(129) << "name = " << actionName << " shortcut = " << \
kaction->shortcut(KAction::ActiveShortcut).toString() << " def = " << \
kaction->shortcut(KAction::DefaultShortcut).toString(); +      bool bSameAsDefault = \
!kaction->isModified(); +      kDebug(129) << "name = " << actionName 
+                  << " shortcut = " << \
kaction->shortcut(KAction::ActiveShortcut).toString()  +                  << " def = \
" << kaction->shortcut(KAction::DefaultShortcut).toString();  
       // now see if this element already exists
       // and create it if necessary (unless bSameAsDefault)
--- trunk/KDE/kdelibs/kdeui/actions/kactioncollection.h #780777:780778
@@ -145,8 +145,25 @@
     * in the group set by setConfigGroup().
     */
   void readSettings( KConfigGroup* config = 0 );
+  
+  /**
+    * Read from @p config all configurable global key associations.
+    *
+    * \param config Config object to read from
+    */
+  void readGlobalSettings( KConfigGroup* config );
 
   /**
+    * Write the current configurable global key associations to @p config.
+    *
+    * \param config Config object to save to
+    * \param writeDefaults set to true to write settings which are already at \
defaults. +    * \param oneAction pass an action here if you just want to save the \
values for one action, eg. +    *                  if you know that action is the \
only one which has changed. +    */
+  void writeGlobalSettings( KConfigGroup* config = 0, bool writeDefaults = false, \
QAction* oneAction = 0 ) const; +
+  /**
     * Write the current configurable key associations to @p config,
     * or (if @p config is zero) to the application's
     * configuration file.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic