2014-09-10 17:27:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vpropertyfactorymanager.cpp
|
|
|
|
** @author hedgeware <internal(at)hedgeware.net>
|
|
|
|
** @date
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** All rights reserved. This program and the accompanying materials
|
|
|
|
** are made available under the terms of the GNU Lesser General Public License
|
|
|
|
** (LGPL) version 2.1 which accompanies this distribution, and is available at
|
|
|
|
** http://www.gnu.org/licenses/lgpl-2.1.html
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vpropertyfactorymanager.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include "vabstractpropertyfactory.h"
|
2014-09-10 17:27:45 +02:00
|
|
|
#include "vproperty.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "vpropertyfactorymanager_p.h"
|
|
|
|
#include "vstandardpropertyfactory.h"
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VPropertyFactoryManager* VPE::VPropertyFactoryManager::DefaultManager = NULL;
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VPropertyFactoryManager::VPropertyFactoryManager(QObject *parent)
|
2014-09-10 17:27:45 +02:00
|
|
|
: QObject(parent), d_ptr(new VPropertyFactoryManagerPrivate())
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VPropertyFactoryManager::~VPropertyFactoryManager()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
// Delete all factories
|
|
|
|
QList<VAbstractPropertyFactory*> tmpFactories = d_ptr->Factories.values();
|
2014-09-10 19:57:08 +02:00
|
|
|
while (!tmpFactories.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
VAbstractPropertyFactory* tmpFactory = tmpFactories.takeLast();
|
|
|
|
tmpFactories.removeAll(tmpFactory);
|
|
|
|
delete tmpFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete d_ptr;
|
2014-09-10 19:57:08 +02:00
|
|
|
if (this == DefaultManager)
|
|
|
|
{
|
2017-07-05 18:35:34 +02:00
|
|
|
DefaultManager = nullptr;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertyFactoryManager::registerFactory(const QString& type, VAbstractPropertyFactory* factory)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (type.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
// Remove old factory
|
|
|
|
unregisterFactory(getFactory(type), type, true);
|
|
|
|
// Register new one
|
|
|
|
d_ptr->Factories[type] = factory;
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
void VPE::VPropertyFactoryManager::unregisterFactory(VAbstractPropertyFactory* factory, const QString& type,
|
2014-09-10 19:57:08 +02:00
|
|
|
bool delete_if_unused)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!factory)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!type.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
// Remove all occurances
|
|
|
|
QString tmpKey;
|
2014-09-10 19:57:08 +02:00
|
|
|
do
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpKey = d_ptr->Factories.key(factory, QString());
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!tmpKey.isEmpty())
|
|
|
|
{
|
|
|
|
d_ptr->Factories.remove(tmpKey);
|
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
} while(!tmpKey.isEmpty());
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
// Only remove one type
|
2017-07-05 18:35:34 +02:00
|
|
|
if (d_ptr->Factories.value(type, nullptr) == factory)
|
2014-09-10 19:57:08 +02:00
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
d_ptr->Factories.remove(type);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (delete_if_unused && !isRegistered(factory))
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
delete factory;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
bool VPE::VPropertyFactoryManager::isRegistered(VAbstractPropertyFactory* factory)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return (!d_ptr->Factories.key(factory, QString()).isEmpty());
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VAbstractPropertyFactory* VPE::VPropertyFactoryManager::getFactory(const QString& type)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2017-07-05 18:35:34 +02:00
|
|
|
return d_ptr->Factories.value(type, nullptr);
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-21 13:02:55 +01:00
|
|
|
VPE::VProperty* VPE::VPropertyFactoryManager::createProperty(const QString& type, const QString& name,
|
|
|
|
const QString& description, const QString &default_value)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
VAbstractPropertyFactory* tmpFactory = getFactory(type);
|
|
|
|
VProperty* tmpResult = NULL;
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpFactory)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpResult = tmpFactory->createProperty(type, name);
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpResult)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpResult->setDescription(description);
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!default_value.isEmpty())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpResult->deserialize(default_value);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpResult;
|
|
|
|
}
|
|
|
|
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2016-12-21 21:40:01 +01:00
|
|
|
//VPE::VPropertyFactoryManager *VPE::VPropertyFactoryManager::getDefaultManager()
|
|
|
|
//{
|
|
|
|
// if (!DefaultManager)
|
|
|
|
// {
|
|
|
|
// DefaultManager = new VPropertyFactoryManager();
|
|
|
|
// /*VStandardPropertyFactory* tmpStandardProp = */new VStandardPropertyFactory(DefaultManager);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return DefaultManager;
|
|
|
|
//}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2016-12-21 13:02:55 +01:00
|
|
|
QStringList VPE::VPropertyFactoryManager::getSupportedTypes()
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
return d_ptr->Factories.keys();
|
|
|
|
}
|