Seamly2D
Code documentation
vpropertyfactorymanager.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vpropertyfactorymanager.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
22 
23 #include <stddef.h>
24 #include <QList>
25 #include <QMap>
26 #include <QStringList>
27 
29 #include "vproperty.h"
32 
34 
36  : QObject(parent), d_ptr(new VPropertyFactoryManagerPrivate())
37 {
38 
39 }
40 
42 {
43  // Delete all factories
44  QList<VAbstractPropertyFactory*> tmpFactories = d_ptr->Factories.values();
45  while (!tmpFactories.isEmpty())
46  {
47  VAbstractPropertyFactory* tmpFactory = tmpFactories.takeLast();
48  tmpFactories.removeAll(tmpFactory);
49  delete tmpFactory;
50  }
51 
52 
53  delete d_ptr;
54  if (this == DefaultManager)
55  {
56  DefaultManager = nullptr;
57  }
58 }
59 
61 {
62  if (type.isEmpty())
63  {
64  return;
65  }
66 
67  // Remove old factory
68  unregisterFactory(getFactory(type), type, true);
69  // Register new one
70  d_ptr->Factories[type] = factory;
71 }
72 
74  bool delete_if_unused)
75 {
76  if (!factory)
77  {
78  return;
79  }
80 
81  if (!type.isEmpty())
82  {
83  // Remove all occurances
84  QString tmpKey;
85  do
86  {
87  tmpKey = d_ptr->Factories.key(factory, QString());
88  if (!tmpKey.isEmpty())
89  {
90  d_ptr->Factories.remove(tmpKey);
91  }
92  } while(!tmpKey.isEmpty());
93  }
94  else
95  {
96  // Only remove one type
97  if (d_ptr->Factories.value(type, nullptr) == factory)
98  {
99  d_ptr->Factories.remove(type);
100  }
101  }
102 
103  if (delete_if_unused && !isRegistered(factory))
104  {
105  delete factory;
106  }
107 }
108 
110 {
111  return (!d_ptr->Factories.key(factory, QString()).isEmpty());
112 }
113 
115 {
116  return d_ptr->Factories.value(type, nullptr);
117 }
118 
119 
120 VPE::VProperty* VPE::VPropertyFactoryManager::createProperty(const QString& type, const QString& name,
121  const QString& description, const QString &default_value)
122 {
123  VAbstractPropertyFactory* tmpFactory = getFactory(type);
124  VProperty* tmpResult = NULL;
125  if (tmpFactory)
126  {
127  tmpResult = tmpFactory->createProperty(type, name);
128 
129  if (tmpResult)
130  {
131  tmpResult->setDescription(description);
132 
133  if (!default_value.isEmpty())
134  {
135  tmpResult->deserialize(default_value);
136  }
137  }
138  }
139 
140  return tmpResult;
141 }
142 
143 // cppcheck-suppress unusedFunction
144 //VPE::VPropertyFactoryManager *VPE::VPropertyFactoryManager::getDefaultManager()
145 //{
146 // if (!DefaultManager)
147 // {
148 // DefaultManager = new VPropertyFactoryManager();
149 // /*VStandardPropertyFactory* tmpStandardProp = */new VStandardPropertyFactory(DefaultManager);
150 // }
151 
152 // return DefaultManager;
153 //}
154 
155 // cppcheck-suppress unusedFunction
157 {
158  return d_ptr->Factories.keys();
159 }
virtual VProperty * createProperty(const QString &type, const QString &name)=0
Creates a new property of a certain type and assigns a name and description (otionally)
static VPropertyFactoryManager * DefaultManager
The default manager.
virtual ~VPropertyFactoryManager() Q_DECL_OVERRIDE
Destructor.
void unregisterFactory(VAbstractPropertyFactory *factory, const QString &type=QString(), bool delete_if_unused=true)
Removes a factory from the manager.
bool isRegistered(VAbstractPropertyFactory *factory)
Returns whether a factory is registered (and thus owned) by this factory manager.
void registerFactory(const QString &type, VAbstractPropertyFactory *factory)
Register a factory to the factory manager Note that the manager takes ownership of the factory,...
VPropertyFactoryManager(QObject *parent=nullptr)
Constructor.
QStringList getSupportedTypes()
Returns the default manager.
VProperty * createProperty(const QString &type, const QString &name, const QString &description=QString(), const QString &default_value=QString())
Creates a new property of a certain type and assigns a name and description (otionally)
VAbstractPropertyFactory * getFactory(const QString &type)
Returns a pointer to a factory registered for a certain type.
virtual void setDescription(const QString &desc)
Sets the name of the property.
Definition: vproperty.cpp:210
virtual void deserialize(const QString &value)
Deserializes the value from a string.
Definition: vproperty.cpp:192