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 00287 KComponentData componentData() const; 00288 00300 template<typename T> 00301 T *create(QObject *parent = 0, const QVariantList &args = QVariantList()); 00302 00314 template<typename T> 00315 T *create(const QString &keyword, QObject *parent = 0, const QVariantList &args = QVariantList()); 00316 00330 template<typename T> 00331 T *create(QWidget *parentWidget, QObject *parent, const QString &keyword = QString(), const QVariantList &args = QVariantList()); 00332 00336 #ifndef KDE_NO_DEPRECATED 00337 template<typename T> 00338 KDE_DEPRECATED 00339 T *create(QObject *parent, const QStringList &args) 00340 { 00341 return create<T>(parent, stringListToVariantList(args)); 00342 } 00343 #endif 00344 00348 #ifndef KDE_NO_DEPRECATED 00349 KDE_DEPRECATED QObject *create(QObject *parent = 0, const char *classname = "QObject", const QStringList &args = QStringList()) 00350 { 00351 return create(classname, 0, parent, stringListToVariantList(args), QString()); 00352 } 00353 #endif 00354 00355 Q_SIGNALS: 00356 void objectCreated(QObject *object); 00357 00358 protected: 00362 typedef QObject *(*CreateInstanceFunction)(QWidget *, QObject *, const QVariantList &); 00363 00364 explicit KPluginFactory(KPluginFactoryPrivate &dd, QObject *parent = 0); 00365 00397 template<class T> 00398 void registerPlugin(const QString &keyword = QString(), CreateInstanceFunction instanceFunction 00399 = InheritanceChecker<T>().createInstanceFunction(reinterpret_cast<T *>(0))) 00400 { 00401 registerPlugin(keyword, &T::staticMetaObject, instanceFunction); 00402 } 00403 00408 QVariantList stringListToVariantList(const QStringList &list); 00409 00414 QStringList variantListToStringList(const QVariantList &list); 00415 00416 virtual void setupTranslations(); 00417 00418 KPluginFactoryPrivate *const d_ptr; 00419 00423 #ifndef KDE_NO_DEPRECATED 00424 virtual KDE_DEPRECATED QObject *createObject(QObject *parent, const char *className, const QStringList &args); 00425 #endif 00426 00430 #ifndef KDE_NO_DEPRECATED 00431 virtual KDE_DEPRECATED KParts::Part *createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args); 00432 #endif 00433 00434 00446 void setComponentData(const KComponentData &componentData); 00447 00462 virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword); 00463 00464 template<class impl, class ParentType> 00465 static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args) 00466 { 00467 Q_UNUSED(parentWidget); 00468 ParentType *p = 0; 00469 if (parent) { 00470 p = qobject_cast<ParentType *>(parent); 00471 Q_ASSERT(p); 00472 } 00473 return new impl(p, args); 00474 } 00475 00476 template<class impl> 00477 static QObject *createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args) 00478 { 00479 return new impl(parentWidget, parent, args); 00480 } 00481 00486 template<class impl> 00487 struct InheritanceChecker 00488 { 00489 CreateInstanceFunction createInstanceFunction(KParts::Part *) { return &createPartInstance<impl>; } 00490 CreateInstanceFunction createInstanceFunction(QWidget *) { return &createInstance<impl, QWidget>; } 00491 CreateInstanceFunction createInstanceFunction(...) { return &createInstance<impl, QObject>; } 00492 }; 00493 00494 private: 00495 void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction); 00496 }; 00497 00498 typedef KPluginFactory KLibFactory; 00499 00500 template<typename T> 00501 inline T *KPluginFactory::create(QObject *parent, const QVariantList &args) 00502 { 00503 QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, QString()); 00504 00505 T *t = qobject_cast<T *>(o); 00506 if (!t) { 00507 delete o; 00508 } 00509 return t; 00510 } 00511 00512 template<typename T> 00513 inline T *KPluginFactory::create(const QString &keyword, QObject *parent, const QVariantList &args) 00514 { 00515 QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, keyword); 00516 00517 T *t = qobject_cast<T *>(o); 00518 if (!t) { 00519 delete o; 00520 } 00521 return t; 00522 } 00523 00524 template<typename T> 00525 inline T *KPluginFactory::create(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args) 00526 { 00527 QObject *o = create(T::staticMetaObject.className(), parentWidget, parent, args, keyword); 00528 00529 T *t = qobject_cast<T *>(o); 00530 if (!t) { 00531 delete o; 00532 } 00533 return t; 00534 } 00535 00536 #endif // KDECORE_KPLUGINFACTORY_H
KDE 4.6 API Reference