KParts
genericfactory.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef KPARTS_GENERICFACTORY_H
00020 #define KPARTS_GENERICFACTORY_H
00021
00022 #include <kparts/factory.h>
00023 #include <kparts/part.h>
00024 #include <kgenericfactory.h>
00025 #include <kaboutdata.h>
00026 #include <kdebug.h>
00027
00028 namespace KParts
00029 {
00030
00034 template <class T>
00035 class GenericFactoryBase : public KParts::Factory
00036 {
00037 public:
00038 GenericFactoryBase()
00039 {
00040 if ( s_self )
00041 kWarning() << "KParts::GenericFactory instantiated more than once!";
00042 s_self = this;
00043 }
00044 virtual ~GenericFactoryBase()
00045 {
00046 delete s_aboutData;
00047 delete s_componentData;
00048 s_aboutData = 0;
00049 s_componentData = 0;
00050 s_self = 0;
00051 }
00052
00053 static const KComponentData &componentData();
00054 static KAboutData *aboutData();
00055 virtual KComponentData partComponentData()
00056 {
00057 return componentData();
00058 }
00059
00060
00061 protected:
00062 virtual KComponentData *createComponentData()
00063 {
00064 return new KComponentData(aboutData());
00065 }
00066
00067
00068 private:
00069 static GenericFactoryBase<T> *s_self;
00070 static KComponentData *s_componentData;
00071 static KAboutData *s_aboutData;
00072 };
00073
00107 template <class T>
00108 class GenericFactory : public GenericFactoryBase<T>
00109 {
00110 public:
00111 GenericFactory() { }
00112
00113 virtual KParts::Part *createPartObject( QWidget *parentWidget,
00114 QObject *parent,
00115 const char *className,
00116 const QStringList &args )
00117 {
00118 T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget,
00119 parent,
00120 className,
00121 args );
00122
00123 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00124 {
00125 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00126 if ( rwp )
00127 rwp->setReadWrite( false );
00128 }
00129 return part;
00130 }
00131 };
00132
00133 template <class T1, class T2>
00134 class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
00135 {
00136 public:
00137 GenericFactory() { }
00138
00139 virtual KParts::Part *createPartObject( QWidget *parentWidget,
00140 QObject *parent,
00141 const char *className,
00142 const QStringList &args )
00143 {
00144 QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget,
00145 parent,
00146 className,
00147 args );
00148
00149
00150 KParts::Part *part = dynamic_cast<KParts::Part *>( object );
00151
00152 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
00153 {
00154 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
00155 if ( rwp )
00156 rwp->setReadWrite( false );
00157 }
00158 return part;
00159 }
00160 };
00161
00165 template <class T>
00166 GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0;
00167
00171 template <class T>
00172 KComponentData *GenericFactoryBase<T>::s_componentData = 0;
00173
00177 template <class T>
00178 KAboutData *GenericFactoryBase<T>::s_aboutData = 0;
00179
00183 template <class T>
00184 const KComponentData &GenericFactoryBase<T>::componentData()
00185 {
00186 if ( !s_componentData )
00187 {
00188 if ( s_self )
00189 s_componentData = s_self->createComponentData();
00190 else
00191 s_componentData = new KComponentData(aboutData());
00192 }
00193 return *s_componentData;
00194 }
00195
00199 template <class T>
00200 KAboutData *GenericFactoryBase<T>::aboutData()
00201 {
00202 if ( !s_aboutData )
00203 s_aboutData = T::createAboutData();
00204 return s_aboutData;
00205 }
00206
00207 }
00208
00209 #endif
00210