factory.cpp
1 /***************************************************************************
2  * Copyright (C) 2008 by Tomassino Ferrauto *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include "factory.h"
22 #include <QStringList>
23 
24 namespace farsa {
25 
27 {
28  // The meyer singleton
29  static Factory factory;
30 
31  return factory;
32 }
33 
35  static ConfigurationParameters typeDescriptions(true);
36  return typeDescriptions;
37 }
38 
39 Factory::Factory() :
40  m_classMap(),
41  m_parentsMap(),
42  m_childrenMap()
43 {
44 }
45 
47 {
48  // Deleting all creators
49  for(QMap<QString, ParameterSettableCreator*>::iterator i = m_classMap.begin(); i != m_classMap.end(); i++) {
50  delete i.value();
51  i.value() = NULL;
52  }
53 }
54 
55 QStringList Factory::getAllSubclasses(QString className, int levelToStop, bool noAbstractClasses)
56 {
57  QStringList subclasses;
58  QStringList currents;
59  currents.append(className);
60  int level = 0;
61  while (((level < levelToStop) || (levelToStop == -1)) && (currents.count() > 0)) {
62  QStringList subs;
63  foreach (QString current, currents) {
64  subs.append(m_childrenMap[current]);
65  }
66  subclasses.append(subs);
67  currents = subs;
68  level++;
69  }
70  if (noAbstractClasses) {
71  // filter QStringList
72  QStringList filtered;
73  foreach (QString current, subclasses) {
74  if (isAbstract(current)) continue;
75  filtered << current;
76  }
77  return filtered;
78  }
79  return subclasses;
80 }
81 
82 bool Factory::isAbstract(QString className)
83 {
84  // if there is no creator, then it is abstract
85  return !(m_classMap.contains(className));
86 }
87 
88 ConfigurationWidget* Factory::getEditorForType(ConfigurationParameters& params, QString prefix, QWidget* parent, Qt::WindowFlags f)
89 {
90  const QString typeForGroup = params.getValue(prefix + ConfigurationParameters::GroupSeparator() + "type");
91 
92  // Creating the configuration widget. If we have a specific editor for the type creating it,
93  // otherwise returing the default widget
94  if ((!typeForGroup.isEmpty()) && (m_editorsMap.contains(typeForGroup))) {
95  return m_editorsMap[typeForGroup]->create(params, prefix, parent, f);
96  }
97  return NULL;
98 }
99 
100 void Factory::cleanupMapsForReRegistration(QString className)
101 {
102  // Need to check whether className was already registered to clean up things. We check m_parentsMap
103  // because in the previous registration the class could have been an abstract class
104  if (m_parentsMap.contains(className)) {
105  // Removing the old creator if it exists
106  if (m_classMap.contains(className)) {
107  delete m_classMap[className];
108  }
109 
110  // Also removing the class from the list of children of its parent class
111  if (m_childrenMap.contains(m_parentsMap[className])) {
112  const int nRemoved = m_childrenMap[m_parentsMap[className]].removeAll(className);
113  Q_ASSERT_X((nRemoved <= 1), "Registering a class", ("More that one instance of " + className + " were present").toAscii().data());
114  }
115 
116  // The class being re-registered could be a parent of other classes: as we don't know what
117  // happened to that relations, we leave them as they are
118  }
119 }
120 
121 } // end namespace farsa