• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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     KDE_CONSTRUCTOR_DEPRECATED explicit KPluginFactory(const KAboutData *aboutData, QObject *parent = 0);
00263 
00267     explicit KDE_CONSTRUCTOR_DEPRECATED KPluginFactory(QObject *parent);
00268 
00273     virtual ~KPluginFactory();
00274 
00283     KComponentData componentData() const;
00284 
00296     template<typename T>
00297     T *create(QObject *parent = 0, const QVariantList &args = QVariantList());
00298 
00310     template<typename T>
00311     T *create(const QString &keyword, QObject *parent = 0, const QVariantList &args = QVariantList());
00312 
00326     template<typename T>
00327     T *create(QWidget *parentWidget, QObject *parent, const QString &keyword = QString(), const QVariantList &args = QVariantList());
00328 
00332     template<typename T>
00333     KDE_DEPRECATED
00334     T *create(QObject *parent, const QStringList &args)
00335     {
00336         return create<T>(parent, stringListToVariantList(args));
00337     }
00338 
00342     KDE_DEPRECATED QObject *create(QObject *parent = 0, const char *classname = "QObject", const QStringList &args = QStringList())
00343     {
00344         return create(classname, 0, parent, stringListToVariantList(args), QString());
00345     }
00346 
00347 Q_SIGNALS:
00348     void objectCreated(QObject *object);
00349 
00350 protected:
00354     typedef QObject *(*CreateInstanceFunction)(QWidget *, QObject *, const QVariantList &);
00355 
00356     explicit KPluginFactory(KPluginFactoryPrivate &dd, QObject *parent = 0);
00357 
00389     template<class T>
00390     void registerPlugin(const QString &keyword = QString(), CreateInstanceFunction instanceFunction
00391             = InheritanceChecker<T>().createInstanceFunction(reinterpret_cast<T *>(0)))
00392     {
00393         registerPlugin(keyword, &T::staticMetaObject, instanceFunction);
00394     }
00395 
00400     QVariantList stringListToVariantList(const QStringList &list);
00401 
00406     QStringList variantListToStringList(const QVariantList &list);
00407 
00408     virtual void setupTranslations();
00409 
00410     KPluginFactoryPrivate *const d_ptr;
00411 
00415     virtual KDE_DEPRECATED QObject *createObject(QObject *parent, const char *className, const QStringList &args);
00416 
00420     virtual KDE_DEPRECATED KParts::Part *createPartObject(QWidget *parentWidget, QObject *parent, const char *classname, const QStringList &args);
00421 
00422 
00434     void setComponentData(const KComponentData &componentData);
00435 
00450     virtual QObject *create(const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword);
00451 
00452     template<class impl, class ParentType>
00453     static QObject *createInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
00454     {
00455         Q_UNUSED(parentWidget);
00456         ParentType *p = 0;
00457         if (parent) {
00458             p = qobject_cast<ParentType *>(parent);
00459             Q_ASSERT(p);
00460         }
00461         return new impl(p, args);
00462     }
00463 
00464     template<class impl>
00465     static QObject *createPartInstance(QWidget *parentWidget, QObject *parent, const QVariantList &args)
00466     {
00467         return new impl(parentWidget, parent, args);
00468     }
00469 
00474     template<class impl>
00475     struct InheritanceChecker
00476     {
00477         CreateInstanceFunction createInstanceFunction(KParts::Part *) { return &createPartInstance<impl>; }
00478         CreateInstanceFunction createInstanceFunction(QWidget *) { return &createInstance<impl, QWidget>; }
00479         CreateInstanceFunction createInstanceFunction(...) { return &createInstance<impl, QObject>; }
00480     };
00481 
00482 private:
00483     void registerPlugin(const QString &keyword, const QMetaObject *metaObject, CreateInstanceFunction instanceFunction);
00484 };
00485 
00486 typedef KPluginFactory KLibFactory;
00487 
00488 template<typename T>
00489 inline T *KPluginFactory::create(QObject *parent, const QVariantList &args)
00490 {
00491     QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, QString());
00492 
00493     T *t = qobject_cast<T *>(o);
00494     if (!t) {
00495         delete o;
00496     }
00497     return t;
00498 }
00499 
00500 template<typename T>
00501 inline T *KPluginFactory::create(const QString &keyword, QObject *parent, const QVariantList &args)
00502 {
00503     QObject *o = create(T::staticMetaObject.className(), parent && parent->isWidgetType() ? reinterpret_cast<QWidget *>(parent): 0, parent, args, keyword);
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(QWidget *parentWidget, QObject *parent, const QString &keyword, const QVariantList &args)
00514 {
00515     QObject *o = create(T::staticMetaObject.className(), parentWidget, parent, args, keyword);
00516 
00517     T *t = qobject_cast<T *>(o);
00518     if (!t) {
00519         delete o;
00520     }
00521     return t;
00522 }
00523 
00524 #endif // KDECORE_KPLUGINFACTORY_H

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal