KDELibs4Support

genericfactory.h
1 /* This file is part of the KDE project
2  Copyright (C) 2001 Simon Hausmann <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #ifndef KPARTS_GENERICFACTORY_H
20 #define KPARTS_GENERICFACTORY_H
21 
22 #include <kparts/factory.h>
23 #include <kparts/part.h>
24 #include <kgenericfactory.h>
25 #include <kaboutdata.h>
26 #include <kdebug.h>
27 
28 namespace KParts
29 {
30 
31 /**
32  * @internal
33  */
34 template <class T>
36 {
37 public:
39  {
40  if (s_self) {
41  kWarning() << "KParts::GenericFactory instantiated more than once!";
42  }
43  s_self = this;
44  }
45  ~GenericFactoryBase() override
46  {
47  delete s_aboutData;
48  delete s_componentData;
49  s_aboutData = nullptr;
50  s_componentData = nullptr;
51  s_self = 0;
52  }
53 
54  static const KComponentData &componentData();
55  static K4AboutData *aboutData();
57  {
58  return componentData();
59  }
60 
61 protected:
62  virtual KComponentData *createComponentData()
63  {
64  return new KComponentData(aboutData());
65  }
66 
67 private:
68  static GenericFactoryBase<T> *s_self;
69  static KComponentData *s_componentData;
70  static K4AboutData *s_aboutData;
71 };
72 
73 /**
74  * A template for a KParts::Factory implementation. It implements the pure virtual
75  * createPartObject method by instantiating the template argument when requested
76  * through the className field. In addition it is a container for a part's KComponentData
77  * object, by providing a static KComponentData componentData() method.
78  *
79  * The template argument has to inherit from KParts::Part and has to implement two methods:
80  * 1) There needs to be a public constructor with the following signature:
81  * MyPart( QWidget *parentWidget, QObject *parent, const QStringList& args )
82  *
83  * 2) It needs to provide one static method to create a K4AboutData object per
84  * request, holding information about the component's name, its authors, license, etc.
85  * The signature of that static method has to be
86  * K4AboutData *createAboutData()
87  *
88  * The template will take care of memory management of the KComponentData and the K4AboutData object,
89  * meaning ownership of what createAboutData returns is passed to the caller (this template) .
90  *
91  * For advanced use you can also inherit from the template and re-implement additionally the
92  * virtual KComponentData createComponentData() method, for example in case you want to extend the
93  * paths of your instance's KStandardDirs object.
94  *
95  * If a KParts::ReadOnlyPart is requested through this factory and the template argument
96  * implements a KParts::ReadWritePart then setReadWrite( false ) will automatically be
97  * called in createPartObject.
98  *
99  * Use the factory through the K_EXPORT_COMPONENT_FACTORY macro, like that:
100  * \code
101  * typedef KParts::GenericFactory&lt;YourKPart&gt; YourKPartFactory;
102  * K_EXPORT_COMPONENT_FACTORY( yourlibrary, YourKPartFactory )
103  * \endcode
104  * yourlibrary is the library name that you compiled your KPart into.
105  */
106 template <class T>
107 class KDELIBS4SUPPORT_DEPRECATED GenericFactory : public GenericFactoryBase<T>
108 {
109 public:
110  GenericFactory() { }
111 
112  virtual KParts::Part *createPartObject(QWidget *parentWidget,
113  QObject *parent,
114  const char *className,
115  const QStringList &args)
116  {
117  T *part = KDEPrivate::ConcreteFactory<T>::create(parentWidget,
118  parent,
119  className,
120  args);
121 
122  if (part && !qstrcmp(className, "KParts::ReadOnlyPart")) {
123  KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>(part);
124  if (rwp) {
125  rwp->setReadWrite(false);
126  }
127  }
128  return part;
129  }
130 };
131 
132 template <class T1, class T2>
133 class GenericFactory< KTypeList<T1, T2> > : public GenericFactoryBase<T1>
134 {
135 public:
136  GenericFactory() { }
137 
138  virtual KParts::Part *createPartObject(QWidget *parentWidget,
139  QObject *parent,
140  const char *className,
141  const QStringList &args)
142  {
143  QObject *object = KDEPrivate::MultiFactory< KTypeList<T1, T2> >::create(parentWidget,
144  parent,
145  className,
146  args);
147 
148  // (this cast is guaranteed to work...)
149  KParts::Part *part = dynamic_cast<KParts::Part *>(object);
150 
151  if (part && !qstrcmp(className, "KParts::ReadOnlyPart")) {
152  KParts::ReadWritePart *rwp = dynamic_cast<KParts::ReadWritePart *>(part);
153  if (rwp) {
154  rwp->setReadWrite(false);
155  }
156  }
157  return part;
158  }
159 };
160 
161 /**
162  * @internal
163  */
164 template <class T>
166 
167 /**
168  * @internal
169  */
170 template <class T>
172 
173 /**
174  * @internal
175  */
176 template <class T>
178 
179 /**
180  * @internal
181  */
182 template <class T>
184 {
185  if (!s_componentData) {
186  if (s_self) {
187  s_componentData = s_self->createComponentData();
188  } else {
189  s_componentData = new KComponentData(aboutData());
190  }
191  }
192  return *s_componentData;
193 }
194 
195 /**
196  * @internal
197  */
198 template <class T>
200 {
201  if (!s_aboutData) {
202  s_aboutData = T::createAboutData();
203  }
204  return s_aboutData;
205 }
206 
207 }
208 
209 #endif
210 
static const KComponentData & componentData()
static K4AboutData * aboutData()
T * create(QObject *parent=nullptr, const QVariantList &args=QVariantList())
virtual KParts::Part * createPartObject(QWidget *parentWidget, QObject *parent, const char *className, const QStringList &args)
Reimplement this method in your implementation to create the Part.
A generic factory object to create a Part.
Definition: factory.h:42
This class is used to store information about a program.
Definition: k4aboutdata.h:199
KComponentData partComponentData() override
If you have a part contained in a shared library you might want to query for meta-information like th...
virtual void setReadWrite(bool readwrite=true)
Part * createPartObject(QWidget *parentWidget=nullptr, QObject *parent=nullptr, const char *classname="KParts::Part", const QStringList &args=QStringList()) override=0
Reimplement this method in your implementation to create the Part.
Definition: factory.cpp:70
QObject * parent() const const
A template for a KParts::Factory implementation.
Per component data.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jun 17 2021 22:58:14 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.