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 
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  virtual ~GenericFactoryBase()
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 
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 
164 template <class T>
166 
170 template <class T>
172 
176 template <class T>
178 
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 
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 KComponentData partComponentData()
If you have a part contained in a shared library you might want to query for meta-information like th...
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
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-2020 The KDE developers.
Generated on Wed Sep 30 2020 23:07:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.