KDECore
kpluginfactory.h
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2007 Matthias Kretz <kretz@kde.org> 00003 Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 00020 */ 00021 00022 #ifndef KDECORE_KPLUGINFACTORY_H 00023 #define KDECORE_KPLUGINFACTORY_H 00024 00025 #include "kdecore_export.h" 00026 00027 #include <QtCore/QObject> 00028 #include <QtCore/QVariant> 00029 #include <QtCore/QStringList> 00030 #include <kcomponentdata.h> 00031 #include <kexportplugin.h> 00032 #include <kglobal.h> 00033 00034 class KPluginFactoryPrivate; 00035 namespace KParts { class Part; } 00036 00037 #define K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \ 00038 class name : public baseFactory \ 00039 { \ 00040 public: \ 00041 explicit name(const char * = 0, const char * = 0, QObject * = 0); \ 00042 explicit name(const KAboutData &, QObject * = 0); \ 00043 ~name(); \ 00044 static KComponentData componentData(); \ 00045 private: \ 00046 void init(); \ 00047 }; 00048 00049 #define K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \ 00050 K_GLOBAL_STATIC(KComponentData, name##factorycomponentdata) \ 00051 name::name(const char *componentName, const char *catalogName, QObject *parent) \ 00052 : baseFactory(componentName, catalogName, parent) { init(); } \ 00053 name::name(const KAboutData &aboutData, QObject *parent) \ 00054 : baseFactory(aboutData, parent) { init(); } \ 00055 void name::init() \ 00056 { \ 00057 if (name##factorycomponentdata->isValid()) \ 00058 setComponentData(*name##factorycomponentdata); \ 00059 else \ 00060 *name##factorycomponentdata = KPluginFactory::componentData(); \ 00061 pluginRegistrations \ 00062 } \ 00063 name::~name() {} \ 00064 KComponentData name::componentData() \ 00065 { \ 00066 return *name##factorycomponentdata; \ 00067 } 00068 00069 #define K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) \ 00070 K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, baseFactory) \ 00071 K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, baseFactory, pluginRegistrations) 00072 00127 #define K_PLUGIN_FACTORY(name, pluginRegistrations) K_PLUGIN_FACTORY_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations) 00128 00140 #define K_PLUGIN_FACTORY_DECLARATION(name) K_PLUGIN_FACTORY_DECLARATION_WITH_BASEFACTORY(name, KPluginFactory) 00141 00156 #define K_PLUGIN_FACTORY_DEFINITION(name, pluginRegistrations) K_PLUGIN_FACTORY_DEFINITION_WITH_BASEFACTORY(name, KPluginFactory, pluginRegistrations) 00157 00232 class KDECORE_EXPORT KPluginFactory : public QObject 00233 { 00234 Q_OBJECT 00235 Q_DECLARE_PRIVATE(KPluginFactory) 00236 public: 00247 explicit KPluginFactory(const char *componentName = 0, const char *catalogName = 0, QObject *parent = 0); 00248 00258 explicit KPluginFactory(const KAboutData &aboutData, QObject *parent = 0); 00262 #ifndef KDE_NO_DEPRECATED 00263 KDE_CONSTRUCTOR_DEPRECATED explicit KPluginFactory(const KAboutData *aboutData, QObject *parent = 0); 00264 #endif 00265 00269 #ifndef KDE_NO_DEPRECATED 00270 explicit KDE_CONSTRUCTOR_DEPRECATED KPluginFactory(QObject *parent); 00271 #endif 00272 00277 virtual ~KPluginFactory(); 00278 00291 KComponentData componentData() const; 00292 00304 template<typename T> 00305 T *create(QObject *parent = 0, const QVariantList &args = QVariantList()); 00306 00318 template<typename T> 00319 T *create(const QString &keyword, QObject *parent = 0, const QVariantList &args = QVariantList()); 00320 00334 template<typename T> 00335 T *create(QWidget *parentWidget, QObject *parent, const QString &keyword = QString(), const QVariantList &args = QVariantList()); 00336 00340 #ifndef KDE_NO_DEPRECATED 00341 template<typename T> 00342 KDE_DEPRECATED 00343 T *create(QObject *parent, const QStringList &args) 00344 { 00345 return create<T>(parent, stringListToVariantList(args)); 00346 } 00347 #endif 00348 00352 #ifndef KDE_NO_DEPRECATED 00353 KDE_DEPRECATED QObject *create(QObject *parent = 0, const char *classname = "QObject", const QStringList &args = QStringList()) 00354 { 00355 return create(classname, 0, parent, stringListToVariantList(args), QString()); 00356 } 00357 #endif 00358 00359 Q_SIGNALS: 00360 void objectCreated(QObject *object); 00361 00362 protected: 00366 typedef QObject *(*CreateInstanceFunction)(QWidget *, QObject *, const QVariantList &); 00367 00368 explicit KPluginFactory(KPluginFactoryPrivate &dd, QObject *parent = 0); 00369 00401 template<class T> 00402 void registerPlugin(const QString &keyword = QString(), CreateInstanceFunction instanceFunction 00403 = InheritanceChecker<T>().createInstanceFunction(reinterpret_cast<T *>(0))) 00404 { 00405 registerPlugin(keyword, &T::staticMetaObject, instanceFunction); 00406 } 00407 00412 QVariantList stringListToVariantList(const QStringList &list); 00413 00418 QStringList variantListToStringList(const QVariantList &list); 00419 00420 virtual void setupTranslations(); 00421 00422 KPluginFactoryPrivate *const d_ptr; 00423 00427 #ifndef KDE_NO_DEPRECATED 00428 virtual KDE_DEPRECATED QObject *createObject(QObject *parent, const char *className, const QStringList &args); 00429 #endif 00430 00434 #ifndef KDE_NO_DEPRECATED 00435 virtual KDE_DEPRECATED KParts::Part *createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args); 00436 #endif 00437 00438 00450 void setComponentData(const KComponentData &componentData); 00451 00466 virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword); 00467 00468 template<class impl, class ParentType> 00469 static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args) 00470 { 00471 Q_UNUSED(parentWidget); 00472 ParentType *p = 0; 00473 if (parent) { 00474 p = qobject_cast<ParentType *>(parent); 00475 Q_ASSERT(p); 00476 } 00477 return new impl(p, args); 00478 } 00479 00480 template<class impl> 00481 static QObject *createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args) 00482 { 00483 return new impl(parentWidget, parent, args); 00484 } 00485 00490 template<class impl> 00491 struct InheritanceChecker 00492 { 00493 CreateInstanceFunction createInstanceFunction(KParts::Part *) { return &createPartInstance<impl>; } 00494 CreateInstanceFunction createInstanceFunction(QWidget *) { return &createInstance<impl, QWidget>; } 00495 CreateInstanceFunction createInstanceFunction(...) { return &createInstance<impl, QObject>; } 00496 }; 00497 00498 private: 00499 void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction); 00500 }; 00501 00502 typedef KPluginFactory KLibFactory; 00503 00504 template<typename T> 00505 inline T *KPluginFactory::create(QObject *parent, const QVariantList &args) 00506 { 00507 QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, QString()); 00508 00509 T *t = qobject_cast<T *>(o); 00510 if (!t) { 00511 delete o; 00512 } 00513 return t; 00514 } 00515 00516 template<typename T> 00517 inline T *KPluginFactory::create(const QString &keyword, QObject *parent, const QVariantList &args) 00518 { 00519 QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, keyword); 00520 00521 T *t = qobject_cast<T *>(o); 00522 if (!t) { 00523 delete o; 00524 } 00525 return t; 00526 } 00527 00528 template<typename T> 00529 inline T *KPluginFactory::create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args) 00530 { 00531 QObject *o = create(T::staticMetaObject.className(), parentWidget, parent, args, keyword); 00532 00533 T *t = qobject_cast<T *>(o); 00534 if (!t) { 00535 delete o; 00536 } 00537 return t; 00538 } 00539 00540 #endif // KDECORE_KPLUGINFACTORY_H
KDE 4.7 API Reference