KParts
genericfactory.h
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2001 Simon Hausmann <hausmann@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 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 { 00042 kWarning() << "KParts::GenericFactory instantiated more than once!"; 00043 } 00044 s_self = this; 00045 } 00046 virtual ~GenericFactoryBase() 00047 { 00048 delete s_aboutData; 00049 delete s_componentData; 00050 s_aboutData = 0; 00051 s_componentData = 0; 00052 s_self = 0; 00053 } 00054 00055 static const KComponentData &componentData(); 00056 static KAboutData *aboutData(); 00057 virtual KComponentData partComponentData() 00058 { 00059 return componentData(); 00060 } 00061 00062 00063 protected: 00064 virtual KComponentData *createComponentData() 00065 { 00066 return new KComponentData(aboutData()); 00067 } 00068 00069 00070 private: 00071 static GenericFactoryBase<T> *s_self; 00072 static KComponentData *s_componentData; 00073 static KAboutData *s_aboutData; 00074 }; 00075 00109 template <class T> 00110 class GenericFactory : public GenericFactoryBase<T> 00111 { 00112 public: 00113 GenericFactory() { } 00114 00115 virtual KParts::Part *createPartObject( QWidget *parentWidget, 00116 QObject *parent, 00117 const char *className, 00118 const QStringList &args ) 00119 { 00120 T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget, 00121 parent, 00122 className, 00123 args ); 00124 00125 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) ) 00126 { 00127 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part ); 00128 if ( rwp ) 00129 rwp->setReadWrite( false ); 00130 } 00131 return part; 00132 } 00133 }; 00134 00135 template <class T1, class T2> 00136 class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1> 00137 { 00138 public: 00139 GenericFactory() { } 00140 00141 virtual KParts::Part *createPartObject( QWidget *parentWidget, 00142 QObject *parent, 00143 const char *className, 00144 const QStringList &args ) 00145 { 00146 QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget, 00147 parent, 00148 className, 00149 args ); 00150 00151 // (this cast is guaranteed to work...) 00152 KParts::Part *part = dynamic_cast<KParts::Part *>( object ); 00153 00154 if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) ) 00155 { 00156 KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part ); 00157 if ( rwp ) 00158 rwp->setReadWrite( false ); 00159 } 00160 return part; 00161 } 00162 }; 00163 00167 template <class T> 00168 GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0; 00169 00173 template <class T> 00174 KComponentData *GenericFactoryBase<T>::s_componentData = 0; 00175 00179 template <class T> 00180 KAboutData *GenericFactoryBase<T>::s_aboutData = 0; 00181 00185 template <class T> 00186 const KComponentData &GenericFactoryBase<T>::componentData() 00187 { 00188 if ( !s_componentData ) 00189 { 00190 if ( s_self ) 00191 s_componentData = s_self->createComponentData(); 00192 else 00193 s_componentData = new KComponentData(aboutData()); 00194 } 00195 return *s_componentData; 00196 } 00197 00201 template <class T> 00202 KAboutData *GenericFactoryBase<T>::aboutData() 00203 { 00204 if ( !s_aboutData ) 00205 s_aboutData = T::createAboutData(); 00206 return s_aboutData; 00207 } 00208 00209 } 00210 00211 #endif 00212
KDE 4.6 API Reference