KParts
genericfactory.h
Go to the documentation of this file.00001 #ifndef __kparts__genericfactory_h__
00002 #define __kparts__genericfactory_h__
00003
00004 #include <kparts/factory.h>
00005 #include <kparts/part.h>
00006 #include <kgenericfactory.h>
00007 #include <kaboutdata.h>
00008 #include <kdebug.h>
00009
00010 namespace KParts
00011 {
00012
00016 template <class T>
00017 class GenericFactoryBase : public KParts::Factory
00018 {
00019 public:
00020 GenericFactoryBase()
00021 {
00022 if ( s_self )
00023 kWarning() << "KParts::GenericFactory instantiated more than once!";
00024 s_self = this;
00025 }
00026 virtual ~GenericFactoryBase()
00027 {
00028 delete s_aboutData;
00029 delete s_componentData;
00030 s_aboutData = 0;
00031 s_componentData = 0;
00032 s_self = 0;
00033 }
00034
00035 static const KComponentData &componentData();
00036 static KAboutData *aboutData();
00037 virtual KComponentData partComponentData()
00038 {
00039 return componentData();
00040 }
00041
00042
00043 protected:
00044 virtual KComponentData *createComponentData()
00045 {
00046 return new KComponentData(aboutData());
00047 }
00048
00049
00050 private:
00051 static GenericFactoryBase<T> *s_self;
00052 static KComponentData *s_componentData;
00053 static KAboutData *s_aboutData;
00054 };
00055
00089 template <class T>
00090 class GenericFactory : public GenericFactoryBase<T>
00091 {
00092 public:
00093 GenericFactory() { }
00094
00095 virtual KParts::Part *createPartObject( QWidget *parentWidget,
00096 QObject *parent,
00097 const char *className,
00098 const QStringList &args )
00099 {
00100 T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget,
00101 parent,
00102 className,
00103 args );
00104
00105 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00106 {
00107 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00108 if ( rwp )
00109 rwp->setReadWrite( false );
00110 }
00111 return part;
00112 }
00113 };
00114
00115 template <class T1, class T2>
00116 class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
00117 {
00118 public:
00119 GenericFactory() { }
00120
00121 virtual KParts::Part *createPartObject( QWidget *parentWidget,
00122 QObject *parent,
00123 const char *className,
00124 const QStringList &args )
00125 {
00126 QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget,
00127 parent,
00128 className,
00129 args );
00130
00131
00132 KParts::Part *part = dynamic_cast<KParts::Part *>( object );
00133
00134 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00135 {
00136 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00137 if ( rwp )
00138 rwp->setReadWrite( false );
00139 }
00140 return part;
00141 }
00142 };
00143
00147 template <class T>
00148 GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0;
00149
00153 template <class T>
00154 KComponentData *GenericFactoryBase<T>::s_componentData = 0;
00155
00159 template <class T>
00160 KAboutData *GenericFactoryBase<T>::s_aboutData = 0;
00161
00165 template <class T>
00166 const KComponentData &GenericFactoryBase<T>::componentData()
00167 {
00168 if ( !s_componentData )
00169 {
00170 if ( s_self )
00171 s_componentData = s_self->createComponentData();
00172 else
00173 s_componentData = new KComponentData(aboutData());
00174 }
00175 return *s_componentData;
00176 }
00177
00181 template <class T>
00182 KAboutData *GenericFactoryBase<T>::aboutData()
00183 {
00184 if ( !s_aboutData )
00185 s_aboutData = T::createAboutData();
00186 return s_aboutData;
00187 }
00188
00189 }
00190
00191 #endif
00192