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

List:       kde-core-devel
Subject:    Re: PATCH: defining actions enabled/disabled status in XML
From:       Guillaume Laurent <glaurent () telegraph-road ! org>
Date:       2001-11-01 15:30:06
[Download RAW message or body]

On Thursday 01 November 2001 12:13, Simon Hausmann wrote:

> Excellent idea!

Thank you.

> Implementation looks good to me. Just some minor comments (I'm a
> nitpicker ;-)
>
> 1) Just an idea, but how about reflecting the notion of the state in
>    the API a bit more, by having sth. like an ActionState structure:
>
>    struct ActionState (TODO: find better name ;)

OK, I called it StateChange (figuring we could possibly stuff some more 
things in it later on).

> 2) The stateChanged method sounds generally useful. Why not move it
>    to KXMLGUIClient? (it should still be possible to declare it as
>    slot in kmainwindow, I think)

Done. The slot in KMainWindow is now slotStateChanged().

Here's the new patch.

-- 
					Guillaume.
					http://www.telegraph-road.org

["patch" (text/x-diff)]

? patch
? Makefile.bk
? tests/Makefile.bk
Index: kmainwindow.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmainwindow.cpp,v
retrieving revision 1.52
diff -u -b -p -u -r1.52 kmainwindow.cpp
--- kmainwindow.cpp	2001/10/12 14:27:56	1.52
+++ kmainwindow.cpp	2001/11/01 15:18:29
@@ -27,6 +27,7 @@
 #include <qobjectlist.h>
 
 #include <kaccel.h>
+#include <kaction.h>
 #include <kapplication.h>
 #include <kconfig.h>
 #include <kdebug.h>
@@ -435,6 +436,10 @@ void KMainWindow::appHelpActivated( void
     mHelpMenu->appHelpActivated();
 }
 
+void KMainWindow::slotStateChanged(const QString &newstate)
+{
+  stateChanged(newstate);
+}
 
 void KMainWindow::closeEvent ( QCloseEvent *e )
 {
Index: kmainwindow.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmainwindow.h,v
retrieving revision 1.30
diff -u -b -p -u -r1.30 kmainwindow.h
--- kmainwindow.h	2001/10/26 21:52:58	1.30
+++ kmainwindow.h	2001/11/01 15:18:29
@@ -431,6 +431,13 @@ public slots:
     void appHelpActivated( void );
 
     /**
+     * Apply a state change
+     *
+     * Enable and disable actions as defined in the XML rc file
+     */
+    virtual void slotStateChanged(const QString &newstate);
+
+    /**
      * @internal. Used for the auto-save-settings feature.
      */
     void setSettingsDirty();
Index: kxmlguibuilder.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguibuilder.cpp,v
retrieving revision 1.46
diff -u -b -p -u -r1.46 kxmlguibuilder.cpp
--- kxmlguibuilder.cpp	2001/10/12 10:13:53	1.46
+++ kxmlguibuilder.cpp	2001/11/01 15:18:29
@@ -45,6 +45,7 @@ public:
     QString tagMenu;
     QString tagToolBar;
     QString tagStatusBar;
+    QString tagState;
 
     QString tagSeparator;
     QString tagTearOffHandle;
@@ -80,6 +81,7 @@ KXMLGUIBuilder::KXMLGUIBuilder( QWidget 
   d->tagMenu = QString::fromLatin1( "menu" );
   d->tagToolBar = QString::fromLatin1( "toolbar" );
   d->tagStatusBar = QString::fromLatin1( "statusbar" );
+  d->tagState = QString::fromLatin1( "state" );
 
   d->tagSeparator = QString::fromLatin1( "separator" );
   d->tagTearOffHandle = QString::fromLatin1( "tearoffhandle" );
@@ -112,7 +114,9 @@ KXMLGUIBuilder::~KXMLGUIBuilder()
 QStringList KXMLGUIBuilder::containerTags() const
 {
   QStringList res;
-  res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar;
+  res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar
+      << d->tagState;
+
   return res;
 }
 
Index: kxmlguiclient.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguiclient.cpp,v
retrieving revision 1.55
diff -u -b -p -u -r1.55 kxmlguiclient.cpp
--- kxmlguiclient.cpp	2001/10/20 22:48:25	1.55
+++ kxmlguiclient.cpp	2001/11/01 15:18:30
@@ -772,3 +772,55 @@ void KXMLGUIClient::storeActionPropertie
       action.setAttribute( attrIt.key(), attrIt.data() );
   }
 }
+
+void KXMLGUIClient::addStateActionEnabled(const QString& state,
+                                          const QString& action)
+{
+  StateChange stateChange = getActionsToChangeForState(state);
+  
+  stateChange.actionsToEnable.append( action );
+
+  m_actionsStateMap.replace( state, stateChange );
+}
+
+
+void KXMLGUIClient::addStateActionDisabled(const QString& state,
+                                           const QString& action)
+{
+  StateChange stateChange = getActionsToChangeForState(state);
+  
+  stateChange.actionsToDisable.append( action );
+
+  m_actionsStateMap.replace( state, stateChange );
+}
+
+
+KXMLGUIClient::StateChange KXMLGUIClient::getActionsToChangeForState(const QString& state)
+{
+  return m_actionsStateMap[state];
+}
+
+
+void KXMLGUIClient::stateChanged(const QString &newstate)
+{
+  StateChange stateChange = getActionsToChangeForState(newstate);
+
+  // Enable actions which need to be enabled...
+  //
+  for ( QStringList::Iterator it = stateChange.actionsToEnable.begin();
+        it != stateChange.actionsToEnable.end(); ++it ) {
+
+    KAction *action = actionCollection()->action((*it).latin1());
+    if (action) action->setEnabled(true);
+  }
+
+  // and disable actions which need to be disabled...
+  //
+  for ( QStringList::Iterator it = stateChange.actionsToDisable.begin();
+        it != stateChange.actionsToDisable.end(); ++it ) {
+
+    KAction *action = actionCollection()->action((*it).latin1());
+    if (action) action->setEnabled(false);
+  }
+    
+}
Index: kxmlguiclient.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguiclient.h,v
retrieving revision 1.29
diff -u -b -p -u -r1.29 kxmlguiclient.h
--- kxmlguiclient.h	2001/10/15 21:02:40	1.29
+++ kxmlguiclient.h	2001/11/01 15:18:30
@@ -187,6 +187,19 @@ public:
 
   static QString findMostRecentXMLFile( const QStringList &files, QString &doc );
 
+  void addStateActionEnabled(const QString& state, const QString& action);
+
+  void addStateActionDisabled(const QString& state, const QString& action);
+
+
+  struct StateChange
+  {
+    QStringList actionsToEnable;
+    QStringList actionsToDisable;
+  };
+
+  StateChange getActionsToChangeForState(const QString& state);
+  
 protected:
   /**
    * Sets the instance (@ref KInstance) for this part.
@@ -235,6 +248,8 @@ protected:
    */
   virtual void conserveMemory();
 
+  virtual void stateChanged(const QString &newstate);
+
 private:
   struct DocStruct
   {
@@ -255,6 +270,9 @@ private:
   static void storeActionProperties( QDomDocument &doc, const ActionPropertiesMap &properties );
 
   static QString findVersionNumber( const QString &_xml );
+
+  // Actions to enable/disable on a state change
+  QMap<QString,StateChange> m_actionsStateMap;
 
   KXMLGUIClientPrivate *d;
 };
Index: kxmlguifactory.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguifactory.cpp,v
retrieving revision 1.105
diff -u -b -p -u -r1.105 kxmlguifactory.cpp
--- kxmlguifactory.cpp	2001/10/20 22:48:25	1.105
+++ kxmlguifactory.cpp	2001/11/01 15:18:31
@@ -673,6 +673,7 @@ void KXMLGUIFactory::buildRecursive( con
     // some often used QStrings
     static const QString &tagAction = KGlobal::staticQString( "action" );
     static const QString &tagMerge = KGlobal::staticQString( "merge" );
+    static const QString &tagState = KGlobal::staticQString( "state" );
     static const QString &tagDefineGroup = KGlobal::staticQString( "definegroup" );
     static const QString &attrGroup = KGlobal::staticQString( "group" );
 
@@ -836,6 +837,10 @@ void KXMLGUIFactory::buildRecursive( con
             calcMergingIndex( parentNode, QString::null, d->m_currentClientMergingIt,
                               ignoreDefaultMergingIndex );
         }
+        else if ( tag == tagState )
+        {
+          processStateElement( e );
+        }
         else if ( containerTags.findIndex( tag ) != -1 )
         {
             /*
@@ -1336,5 +1341,43 @@ void KXMLGUIFactory::unplugActionListRec
     for (; childIt.current(); ++childIt )
         unplugActionListRecursive( childIt.current() );
 }
+
+void KXMLGUIFactory::processStateElement( const QDomElement &element )
+{
+  QString stateName = element.attribute( "name" );
+
+  if ( !stateName || !stateName.length() ) return;
+
+  QDomElement e = element.firstChild().toElement();
+
+  for (; !e.isNull(); e = e.nextSibling().toElement() ) {
+    QString tagName = e.tagName().lower();
+    
+    if ( tagName != "enable" && tagName != "disable" )
+      continue;
+    
+    bool processingActionsToEnable = (tagName == "enable");
+
+    // process action names
+    QDomElement actionEl = e.firstChild().toElement();
+
+    for (; !actionEl.isNull(); actionEl = actionEl.nextSibling().toElement() ) {
+      if ( actionEl.tagName().lower() != "action" ) continue;
+
+      QString actionName = actionEl.attribute( "name" );
+      if ( !actionName || !actionName.length() ) return;
+
+      if ( processingActionsToEnable )
+        m_client->addStateActionEnabled( stateName, actionName );
+      else
+        m_client->addStateActionDisabled( stateName, actionName );
+
+    }
+
+  }
+  
+  
+}
+
 
 #include "kxmlguifactory.moc"
Index: kxmlguifactory.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguifactory.h,v
retrieving revision 1.54
diff -u -b -p -u -r1.54 kxmlguifactory.h
--- kxmlguifactory.h	2001/10/20 22:48:25	1.54
+++ kxmlguifactory.h	2001/11/01 15:18:31
@@ -174,6 +174,8 @@ class KXMLGUIFactory : public QObject
   void plugActionListRecursive( KXMLGUIContainerNode *node );
   void unplugActionListRecursive( KXMLGUIContainerNode *node );
 
+  void processStateElement( const QDomElement &element );
+
   KXMLGUIClient *m_client;
   KXMLGUIBuilder *m_builder;
 


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

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