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

List:       kde-core-devel
Subject:    createStandardKWindow()
From:       Benjamin Meyer <ben () meyerhome ! net>
Date:       2004-06-07 0:13:50
Message-ID: 200406062013.58775.ben () meyerhome ! net
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Your typical KDE application does a number of the exact same things.  The 
biggest thing that drew my attention was the Configure dialog.  Now we have 
KConfigXT which removes that from the programmers list of basic things that 
have to be re-implemented.  Every application used to manually turned on/off 
their statusbar in out of which createStandardStatusBarAction() was added in 
3.2.  The key shortcuts also was typically a one line function, but with the 
addition of  configureShortcuts in guiFactory in 3.3 this no longer needed.  
Although most application add setAutoSaveSettings() by default I still find 
applications that manually store the width/height of the window simply out of 
lack of knowledge about the function.  
	Now that we have a number of simple functions that can handle the basic 
actions we are finding that application after application have the same half 
dozen or so function calls.  Some call them have default values that are 
unnecessary and some forget one or two of the functions, but most of the time 
they are simply stuck in like stubs.
	This brings us to createStandardKWindow()  It is a simple function that I 
would like to add to KMainWindow (Yes I know this is a week late and I will 
close a dozen bugs as a penalty)  First off why shouldn't this stuff be added 
to createGUI or to the constructor?  Doing some testing with that I found 
that difficult to catch all cases.  The simples was how to know if the 
developer already had a shortcuts action.  Can't be in the constructor sense 
there are no actions than and it can't be in createGUI sense they might have 
named it something other than the standard name from stdaction.  This leaves 
adding a new function which once was finished works out really well.   
	For the typical application createStandardKWindow() replaces the line where 
createGUI() is located and also allows the removal of a bunch of others.  If 
for example you don't have a toolbar you pass in the first arg of false.  
This makes the change source compatible in the sense that it doesn't change 
anything unexpecently unless the developer makes the change.

Rather then go into detail of what it does here is the function doc

    /**
     * Configures the current windows and its actions in the typical KDE
     * fashion.  The options are all enabled by default but can be turned
     * off if desired through the params or if the prereqs don't exists.
     *
     * Typically this function replaces createGUI().
     *
     * @param toolbar adds action to show/hide the toolbar(s) and adds
     *        action to configure the toolbar(s).
     *        @see setStandardToolBarMenuEnabled()
     *
     * @param keys adds action to show the key configure action if any actions
     *        are in the actionCollection() and the action 
     *        options_configure_keybinding doesn't already exists.
     *
     * @param statusbar adds action to show/hide the statusbar if the
     *        statusbar exists.
     *        @see createStandardStatusBarAction()
     *
     * @param save auto-save the toolbar/menubar/statusbar settings and
     *        window size using the default name.
     *        @see setAutoSaveSettings()
     *
     * @param create calls createGUI() once all of the above params have been
     *        taken care of.
     *        @see createGUI()
     * 
     * @sinse 3.3
     */

Some possible changes:

1) Rather than a number of bools for params have only two params.  The first 
being params disabled, and the second being enabled ones (or'd together).  
This way you can turn off one or two things you don't want, but in the future 
as new things are added they will work by default.
2) Rather than looking for options_configure_keybinding if keys is true and 
actions->count() > 0 add?
3) createStandardKWindow() Name it something else? createKMainWindow()   
createStandardKMainWindow()  createStandardWindow()  ?
2) Am I missing anything else that this function should do out of the box?

This function is entirely about creating a consistant interface for users 
(while also reducing the code developers have to implement in their 
application).  By default the window will remember where they were, their 
size, and users can configure everything action related which isn't always 
true currently. 

Feedback/comments welcome.

- -Benjamin Meyer

- -- 
Public Key: http://www.csh.rit.edu/~benjamin/public_key.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAw7ND1rZ3LTw38vIRAuwBAJ0fq8yHbNYdT9mZwzU/4Nll5b7I2gCfYCle
MuzPreZz2o3X+5tBG7FxdlQ=
=jh5M
-----END PGP SIGNATURE-----

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

? diff
Index: kmainwindow.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmainwindow.cpp,v
retrieving revision 1.128
diff -u -3 -p -r1.128 kmainwindow.cpp
--- kmainwindow.cpp	2 Jun 2004 23:30:56 -0000	1.128
+++ kmainwindow.cpp	7 Jun 2004 00:09:43 -0000
@@ -421,6 +421,46 @@ KXMLGUIFactory *KMainWindow::guiFactory(
     return factory_;
 }
 
+void KMainWindow::createStandardKWindow( bool toolbar, bool keys,
+                            bool statusbar, bool save, bool create ){
+    if(keys){
+        // Check for a custom action accel dialog
+        KActionCollection *ac = actionCollection();
+        int count = ac->count();
+        bool foundCustomShortcutAction = false;
+        for( int i = 0; i < count; i++ ){
+            KAction *action = ac->action( i );
+            // Anyway to not hard code this?
+            if(QString("options_configure_keybinding") == action->name()){
+                foundCustomShortcutAction = true;
+                break;
+            }
+        }
+        if( count > 0 && !foundCustomShortcutAction ){
+            KStdAction::keyBindings(guiFactory(),
+                        SLOT(configureShortcuts()), actionCollection());
+        }
+    }
+    
+    if( statusbar && internalStatusBar() ){
+        createStandardStatusBarAction();
+    }
+
+    if( toolbar ){
+        setStandardToolBarMenuEnabled( true );
+        KStdAction::configureToolbars(guiFactory(),
+                      SLOT(configureToolbars() ), actionCollection());
+    }
+
+    if( save ){
+        setAutoSaveSettings();
+    }
+
+    if( create ){
+        createGUI();
+    }
+}
+
 void KMainWindow::createGUI( const QString &xmlfile, bool _conserveMemory )
 {
     // disabling the updates prevents unnecessary redraws
Index: kmainwindow.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kmainwindow.h,v
retrieving revision 1.101
diff -u -3 -p -r1.101 kmainwindow.h
--- kmainwindow.h	1 Jun 2004 16:44:57 -0000	1.101
+++ kmainwindow.h	7 Jun 2004 00:09:43 -0000
@@ -514,6 +514,37 @@ public:
      */
     void createStandardStatusBarAction();
 
+    /**
+     * Configures the current windows and its actions in the typical KDE
+     * fashion.  The options are all enabled by default but can be turned
+     * off if desired through the params or if the prereqs don't exists.
+     *
+     * Typically this function replaces createGUI().
+     *
+     * @param toolbar adds action to show/hide the toolbar(s) and adds
+     *        action to configure the toolbar(s).
+     *        @see setStandardToolBarMenuEnabled()
+     *
+     * @param keys adds action to show the key configure action if any actions
+     *        are in the actionCollection() and the action 
+     *        options_configure_keybinding doesn't already exists.
+     *
+     * @param statusbar adds action to show/hide the statusbar if the
+     *        statusbar exists.
+     *        @see createStandardStatusBarAction()
+     *
+     * @param save auto-save the toolbar/menubar/statusbar settings and
+     *        window size using the default name.
+     *        @see setAutoSaveSettings()
+     *
+     * @param create calls createGUI() once all of the above params have been
+     *        taken care of.
+     *        @see createGUI()
+     * 
+     * @sinse 3.3
+     */
+    void createStandardKWindow( bool toolbar=true, bool keys=true,
+		      bool statusbar=true, bool save=true, bool create=true );
 
     /**
      * Returns a pointer to the mainwindows action responsible for the toolbars menu
Index: kxmlguifactory.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguifactory.cpp,v
retrieving revision 1.147
diff -u -3 -p -r1.147 kxmlguifactory.cpp
--- kxmlguifactory.cpp	13 May 2004 16:53:28 -0000	1.147
+++ kxmlguifactory.cpp	7 Jun 2004 00:09:43 -0000
@@ -38,6 +38,8 @@
 #include <kshortcut.h>
 #include <kstandarddirs.h>
 #include <kkeydialog.h>
+#include <kedittoolbar.h>
+#include <kmainwindow.h>
 
 using namespace KXMLGUI;
 
@@ -553,6 +555,20 @@ int KXMLGUIFactory::configureShortcuts(b
 	return dlg.configure(bSaveSettings);
 }
 
+int KXMLGUIFactory::configureToolbars()
+{
+	(dynamic_cast<KMainWindow*>(parent()))->saveMainWindowSettings(KGlobal::config());
+	KEditToolbar dlg(this, (dynamic_cast<KMainWindow*>(parent())) );
+	connect(&dlg, SIGNAL(newToolbarConfig()), SLOT(saveNewToolbarConfig()));
+	return dlg.exec();
+}
+
+void KXMLGUIFactory::saveNewToolbarConfig()
+{
+	(dynamic_cast<KMainWindow*>(parent()))->createGUI();
+	(dynamic_cast<KMainWindow*>(parent()))->applyMainWindowSettings( KGlobal::config() );
+}
+
 QDomElement KXMLGUIFactory::actionPropertiesElement( QDomDocument& doc )
 {
 	const QString tagActionProp = QString::fromLatin1("ActionProperties");
Index: kxmlguifactory.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kxmlguifactory.h,v
retrieving revision 1.79
diff -u -3 -p -r1.79 kxmlguifactory.h
--- kxmlguifactory.h	28 Apr 2004 14:12:07 -0000	1.79
+++ kxmlguifactory.h	7 Jun 2004 00:09:43 -0000
@@ -192,6 +192,19 @@ class KXMLGUIFactory : public QObject
    * @since 3.3
    */
   int configureShortcuts(bool bAllowLetterShortcuts = true, bool bSaveSettings = true);
+  
+  /**
+   * Show a standard configure toolbar dialog.
+   *
+   * This slot can be connected dirrectly to the action to configure shortcuts.
+   * This is very simple to do that by adding a single line
+   * \code
+   * KStdAction::keyBindings( guiFactory(), SLOT( configureToolbars() ), actionCollection() );
+   * \endcode
+   *
+   * @since 3.3
+   */
+   int configureToolbars();
 
  signals:
   void clientAdded( KXMLGUIClient *client );
@@ -209,6 +222,14 @@ class KXMLGUIFactory : public QObject
 
 protected:
   virtual void virtual_hook( int id, void* data );
+
+protected slots:
+  /**
+   * Stores the new toolbar
+   * @see configureToolbars()
+   */
+  void saveNewToolbarConfig();
+
 private:
   KXMLGUIFactoryPrivate *d;
 };


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

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