Methodes of interest

Simon Hausmann hausmann at kde.org
Mon May 27 09:50:04 UTC 2002


On Sun, May 26, 2002 at 06:35:57PM +0200, Christoph Cullmann wrote:
> Hi,
> 2 new global functions to create Documents/Editor parts just from given 
> library name:
> 
> Document *KTextEditor::createDocument ( const char* libname, QObject *parent = 
> 0, const char *name = 0 );
> 
> Editor *KTextEditor::createEditor ( const char* libname, QWidget *parentWidget 
> = 0, const char *widgetName = 0, QObject *parent = 0, const char *name = 0 );
> 
> What we win:
>  - looks nice ;)
>  - short
>  - does give back the right kind of pointers, if no factory can be create or   
>    the factory don't create any object/right object, 0 returned
> 
> What we lose ;)
>  - the chance of missusing dyn. casts ;) 

BTW, KParts::ComponentFactory provides pretty much the same, in a
generic fashion :)

Here's a patch for the existing methods.

Simon
-------------- next part --------------
Index: ktexteditor.cpp
===================================================================
RCS file: /home/kde/kdelibs/interfaces/ktexteditor/ktexteditor.cpp,v
retrieving revision 1.28
diff -u -p -b -r1.28 ktexteditor.cpp
--- ktexteditor.cpp	2002/05/26 16:44:04	1.28
+++ ktexteditor.cpp	2002/05/27 07:46:03
@@ -26,6 +26,7 @@
 
 #include <kaction.h>
 #include <kparts/factory.h>
+#include <kparts/componentfactory.h>
 
 #include "document.moc"
 #include "view.moc"
@@ -180,47 +181,20 @@ unsigned int Editor::editorNumber () con
 
 Editor *KTextEditor::createEditor ( const char* libname, QWidget *parentWidget, const char *widgetName, QObject *parent, const char *name )
 {
-  if ( KLibFactory *tmpFactory = KLibLoader::self()->factory( libname ) )
-  {   
-    if ( KParts::Factory *factory = static_cast<KParts::Factory *>(tmpFactory->qt_cast ("KParts::Factory")) )
-    {
-      if ( QObject *obj = factory->createPart( parentWidget, widgetName, parent, name, "KTextEditor::Editor" ) )    
-        return static_cast<Editor *>(obj->qt_cast ("KTextEditor::Editor"));
-    }
-  }
-               
-  return 0;
+  return KParts::ComponentFactory::createPartInstanceFromLibrary<Editor>( libname, parentWidget, widgetName, parent, name );
 }
 
 Document *KTextEditor::createDocument ( const char* libname, QObject *parent, const char *name )
 {
-  if ( KLibFactory *factory = KLibLoader::self()->factory( libname ) )
-  {
-    if ( QObject *obj = factory->create( parent, name, "KTextEditor::Document" ) )  
-      return static_cast<Document *>(obj->qt_cast ("KTextEditor::Document"));
-  }
-               
-  return 0;
+  return KParts::ComponentFactory::createInstanceFromLibrary<Document>( libname, parent, name );
 }     
 
 Plugin *KTextEditor::createPlugin ( const char* libname, QObject *parent, const char *name )
 {
-  if ( KLibFactory *factory = KLibLoader::self()->factory( libname ) )
-  {
-    if ( QObject *obj = factory->create( parent, name, "KTextEditor::Plugin" ) )
-      return static_cast<Plugin *>(obj->qt_cast ("KTextEditor::Plugin"));
-  }
-               
-  return 0;
+  return KParts::ComponentFactory::createInstanceFromLibrary<Plugin>( libname, parent, name );
 }
 
 ViewPlugin *KTextEditor::createViewPlugin ( const char* libname, QObject *parent, const char *name )
 {
-  if ( KLibFactory *factory = KLibLoader::self()->factory( libname ) )
-  {
-    if ( QObject *obj = factory->create( parent, name, "KTextEditor::ViewPlugin" ) )
-      return static_cast<ViewPlugin *>(obj->qt_cast ("KTextEditor::ViewPlugin"));
-  }
-               
-  return 0;
+  return KParts::ComponentFactory::createInstanceFromLibrary<ViewPlugin>( libname, parent, name );
 }


More information about the KDevelop-devel mailing list