genericfactory.h
#ifndef __kparts__genericfactory_h__
#define __kparts__genericfactory_h__
#include <kparts/factory.h>
#include <kparts/part.h>
#include <kgenericfactory.h>
#include <kaboutdata.h>
#include <kdebug.h>
namespace KParts
{
template <class T>
class GenericFactoryBase : public KParts::Factory
{
public:
GenericFactoryBase()
{
if ( s_self )
kdWarning() << "KParts::GenericFactory instantiated more than once!" << endl;
s_self = this;
}
virtual ~GenericFactoryBase()
{
delete s_aboutData;
delete s_instance;
s_aboutData = 0;
s_instance = 0;
s_self = 0;
}
static KInstance *instance();
static KAboutData *aboutData();
protected:
virtual KInstance *createInstance()
{
return new KInstance( aboutData() );
}
virtual void virtual_hook( int id, void *data )
{
if ( id != VIRTUAL_QUERY_INSTANCE_PARAMS ) {
KParts::Factory::virtual_hook( id, data );
return;
}
QueryInstanceParams *params = reinterpret_cast<QueryInstanceParams *>( data );
params->instance = instance();
}
private:
static GenericFactoryBase<T> *s_self;
static KInstance *s_instance;
static KAboutData *s_aboutData;
};
template <class T>
class GenericFactory : public GenericFactoryBase<T>
{
public:
GenericFactory() { }
virtual KParts::Part *createPartObject( QWidget *parentWidget, const char *widgetName,
QObject *parent, const char *name,
const char *className,
const QStringList &args )
{
T *part = KDEPrivate::ConcreteFactory<T>::create( parentWidget,
widgetName,
parent,
name,
className,
args );
if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
{
KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
if ( rwp )
rwp->setReadWrite( false );
}
return part;
}
};
template <class T1, class T2>
class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
{
public:
GenericFactory() { }
virtual KParts::Part *createPartObject( QWidget *parentWidget, const char *widgetName,
QObject *parent, const char *name,
const char *className,
const QStringList &args )
{
QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create( parentWidget,
widgetName,
parent, name,
className,
args );
// (this cast is guaranteed to work...)
KParts::Part *part = dynamic_cast<KParts::Part *>( object );
if ( part && !qstrcmp( className, "KParts::ReadOnlyPart" ) )
{
KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>( part );
if ( rwp )
rwp->setReadWrite( false );
}
return part;
}
};
template <class T>
GenericFactoryBase<T> *GenericFactoryBase<T>::s_self = 0;
template <class T>
KInstance *GenericFactoryBase<T>::s_instance = 0;
template <class T>
KAboutData *GenericFactoryBase<T>::s_aboutData = 0;
template <class T>
KInstance *GenericFactoryBase<T>::instance()
{
if ( !s_instance )
{
if ( s_self )
s_instance = s_self->createInstance();
else
s_instance = new KInstance( aboutData() );
}
return s_instance;
}
template <class T>
KAboutData *GenericFactoryBase<T>::aboutData()
{
if ( !s_aboutData )
s_aboutData = T::createAboutData();
return s_aboutData;
}
}
#endif
KParts::Part
Base class for parts.
Definition: part.h:181
KParts::ReadWritePart
Base class for an "editor" part.
Definition: part.h:544
KTypeList
kdWarning
kdbgstream kdWarning(int area=0)
KInstance
KAboutData
KParts::Factory
A generic factory object to create a Part.
Definition: factory.h:40
KParts::ReadWritePart::setReadWrite
virtual void setReadWrite(bool readwrite=true)
Changes the behavior of this part to readonly or readwrite.
Definition: part.cpp:461
KNotifyClient::instance
KInstance * instance()
endl
kndbgstream & endl(kndbgstream &s)
KParts::GenericFactory
A template for a KParts::Factory implementation.
Definition: genericfactory.h:95
KParts::GenericFactory::createPartObject
virtual KParts::Part * createPartObject(QWidget *parentWidget, const char *widgetName, QObject *parent, const char *name, const char *className, const QStringList &args)
Reimplement this method in your implementation to create the Part.
Definition: genericfactory.h:100

KDE Home | KDE Accessibility Home | Description of Access Keys