2020-09-20 21:13:36 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2020 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program 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 General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <QList>
|
|
|
|
|
|
|
|
#include "plugin/pluginapi.h"
|
|
|
|
#include "feature/feature.h"
|
2020-09-23 23:38:05 -04:00
|
|
|
#include "feature/featureutils.h"
|
|
|
|
#include "settings/featuresetpreset.h"
|
2020-10-16 02:35:06 -04:00
|
|
|
#include "maincore.h"
|
2020-09-23 23:38:05 -04:00
|
|
|
|
2020-09-20 21:13:36 -04:00
|
|
|
#include "featureset.h"
|
|
|
|
|
|
|
|
FeatureSet::FeatureSet(int tabIndex)
|
|
|
|
{
|
|
|
|
m_featureTabIndex = tabIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
FeatureSet::~FeatureSet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:53:12 -04:00
|
|
|
Feature *FeatureSet::addFeature(int selectedFeatureIndex, PluginAPI *pluginAPI, WebAPIAdapterInterface *apiAdapter)
|
2020-09-20 21:13:36 -04:00
|
|
|
{
|
|
|
|
PluginAPI::FeatureRegistrations *featureRegistrations = pluginAPI->getFeatureRegistrations(); // Available feature plugins
|
|
|
|
Feature *feature = featureRegistrations->at(selectedFeatureIndex).m_plugin->createFeature(apiAdapter);
|
|
|
|
QString featureName;
|
|
|
|
feature->getIdentifier(featureName);
|
2020-10-16 02:35:06 -04:00
|
|
|
m_featureInstanceRegistrations.append(feature);
|
|
|
|
MainCore::instance()->addFeatureInstance(this, feature);
|
2020-09-20 21:13:36 -04:00
|
|
|
renameFeatureInstances();
|
2022-03-23 00:53:12 -04:00
|
|
|
return feature;
|
2020-09-20 21:13:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureSet::removeFeatureInstance(Feature* feature)
|
|
|
|
{
|
|
|
|
for (FeatureInstanceRegistrations::iterator it = m_featureInstanceRegistrations.begin(); it != m_featureInstanceRegistrations.end(); ++it)
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
if (*it == feature)
|
2020-09-20 21:13:36 -04:00
|
|
|
{
|
|
|
|
m_featureInstanceRegistrations.erase(it);
|
2020-10-16 02:35:06 -04:00
|
|
|
feature->setIndexInFeatureSet(-1);
|
|
|
|
MainCore::instance()->removeFeatureInstance(feature);
|
2020-09-20 21:13:36 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renameFeatureInstances();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureSet::renameFeatureInstances()
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
for (int i = 0; i < m_featureInstanceRegistrations.count(); i++)
|
|
|
|
{
|
|
|
|
m_featureInstanceRegistrations[i]->setName(QString("%1:%2").arg(m_featureInstanceRegistrations[i]->getURI()).arg(i));
|
|
|
|
m_featureInstanceRegistrations[i]->setIndexInFeatureSet(i);
|
2020-09-20 21:13:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 23:38:05 -04:00
|
|
|
// sort by name
|
2020-10-16 02:35:06 -04:00
|
|
|
bool FeatureSet::compareFeatures(Feature *featureA, Feature *featureB)
|
2020-09-23 23:38:05 -04:00
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
if (featureA && featureB) {
|
|
|
|
return featureA->getName() < featureB->getName();
|
2020-09-23 23:38:05 -04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 21:13:36 -04:00
|
|
|
void FeatureSet::freeFeatures()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < m_featureInstanceRegistrations.count(); i++)
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
qDebug("FeatureSet::freeFeatures: destroying feature [%s]", qPrintable(m_featureInstanceRegistrations[i]->getURI()));
|
|
|
|
m_featureInstanceRegistrations[i]->destroy();
|
2020-09-20 21:13:36 -04:00
|
|
|
}
|
2020-10-16 02:35:06 -04:00
|
|
|
|
|
|
|
MainCore::instance()->clearFeatures(this);
|
2020-09-20 21:13:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureSet::deleteFeature(int featureIndex)
|
|
|
|
{
|
|
|
|
if ((featureIndex >= 0) && (featureIndex < m_featureInstanceRegistrations.count()))
|
|
|
|
{
|
|
|
|
qDebug("FeatureSet::deleteFeature: delete feature [%s] at %d",
|
2020-10-16 02:35:06 -04:00
|
|
|
qPrintable(m_featureInstanceRegistrations[featureIndex]->getURI()),
|
2020-09-20 21:13:36 -04:00
|
|
|
featureIndex);
|
2020-10-16 02:35:06 -04:00
|
|
|
m_featureInstanceRegistrations[featureIndex]->destroy();
|
2020-09-20 21:13:36 -04:00
|
|
|
m_featureInstanceRegistrations.removeAt(featureIndex);
|
2020-10-16 02:35:06 -04:00
|
|
|
MainCore::instance()->removeFeatureInstanceAt(this, featureIndex);
|
2020-09-20 21:13:36 -04:00
|
|
|
renameFeatureInstances();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Feature *FeatureSet::getFeatureAt(int featureIndex) const
|
|
|
|
{
|
|
|
|
if ((featureIndex >= 0) && (featureIndex < m_featureInstanceRegistrations.count())) {
|
2020-10-16 02:35:06 -04:00
|
|
|
return m_featureInstanceRegistrations[featureIndex];
|
2020-09-20 21:13:36 -04:00
|
|
|
} else{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Feature *FeatureSet::getFeatureAt(int featureIndex)
|
|
|
|
{
|
|
|
|
if ((featureIndex >= 0) && (featureIndex < m_featureInstanceRegistrations.count())) {
|
2020-10-16 02:35:06 -04:00
|
|
|
return m_featureInstanceRegistrations[featureIndex];
|
2020-09-20 21:13:36 -04:00
|
|
|
} else{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 23:38:05 -04:00
|
|
|
|
|
|
|
void FeatureSet::loadFeatureSetSettings(const FeatureSetPreset *preset, PluginAPI *pluginAPI, WebAPIAdapterInterface *apiAdapter)
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
MainCore *mainCore = MainCore::instance();
|
2020-09-23 23:38:05 -04:00
|
|
|
qDebug("FeatureSet::loadFeatureSetSettings: Loading preset [%s | %s]", qPrintable(preset->getGroup()), qPrintable(preset->getDescription()));
|
|
|
|
|
|
|
|
// Available feature plugins
|
|
|
|
PluginAPI::FeatureRegistrations *featureRegistrations = pluginAPI->getFeatureRegistrations();
|
|
|
|
|
|
|
|
// copy currently open features and clear list
|
|
|
|
FeatureInstanceRegistrations openFeatures = m_featureInstanceRegistrations;
|
|
|
|
m_featureInstanceRegistrations.clear();
|
2020-10-16 02:35:06 -04:00
|
|
|
mainCore->clearFeatures(this);
|
2020-09-23 23:38:05 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < openFeatures.count(); i++)
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
qDebug("FeatureSet::loadFeatureSetSettings: destroying old feature [%s]", qPrintable(openFeatures[i]->getURI()));
|
|
|
|
openFeatures[i]->destroy();
|
2020-09-23 23:38:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
qDebug("FeatureSet::loadFeatureSetSettings: %d feature(s) in preset", preset->getFeatureCount());
|
|
|
|
|
|
|
|
for (int i = 0; i < preset->getFeatureCount(); i++)
|
|
|
|
{
|
|
|
|
const FeatureSetPreset::FeatureConfig& featureConfig = preset->getFeatureConfig(i);
|
2020-10-16 02:35:06 -04:00
|
|
|
Feature* reg = nullptr;
|
2020-09-23 23:38:05 -04:00
|
|
|
|
|
|
|
// create feature instance
|
|
|
|
|
|
|
|
for(int i = 0; i < featureRegistrations->count(); i++)
|
|
|
|
{
|
|
|
|
if (FeatureUtils::compareFeatureURIs((*featureRegistrations)[i].m_featureIdURI, featureConfig.m_featureIdURI))
|
|
|
|
{
|
|
|
|
qDebug("FeatureSet::loadFeatureSetSettings: creating new feature [%s] from config [%s]",
|
|
|
|
qPrintable((*featureRegistrations)[i].m_featureIdURI),
|
|
|
|
qPrintable(featureConfig.m_featureIdURI));
|
|
|
|
Feature *feature =
|
|
|
|
(*featureRegistrations)[i].m_plugin->createFeature(apiAdapter);
|
2020-10-16 02:35:06 -04:00
|
|
|
reg = feature;
|
|
|
|
m_featureInstanceRegistrations.append(feature);
|
|
|
|
mainCore->addFeatureInstance(this, feature);
|
2020-09-23 23:38:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:35:06 -04:00
|
|
|
if (reg)
|
2020-09-23 23:38:05 -04:00
|
|
|
{
|
|
|
|
qDebug("FeatureSet::loadFeatureSetSettings: deserializing feature [%s]", qPrintable(featureConfig.m_featureIdURI));
|
2020-10-16 02:35:06 -04:00
|
|
|
reg->deserialize(featureConfig.m_config);
|
2020-09-23 23:38:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renameFeatureInstances();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureSet::saveFeatureSetSettings(FeatureSetPreset *preset)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_featureInstanceRegistrations.count(); i++)
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
qDebug("FeatureSet::saveFeatureSetSettings: saving feature [%s]", qPrintable(m_featureInstanceRegistrations[i]->getURI()));
|
|
|
|
preset->addFeature(m_featureInstanceRegistrations[i]->getURI(), m_featureInstanceRegistrations[i]->serialize());
|
2020-09-23 23:38:05 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-10 18:22:42 -04:00
|
|
|
|
2020-10-16 02:35:06 -04:00
|
|
|
void FeatureSet::addFeatureInstance(Feature *feature)
|
2020-10-10 18:22:42 -04:00
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
m_featureInstanceRegistrations.push_back(feature);
|
|
|
|
renameFeatureInstances();
|
|
|
|
MainCore::instance()->addFeatureInstance(this, feature);
|
2020-10-10 18:22:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureSet::removeFeatureInstanceAt(int index)
|
|
|
|
{
|
2020-10-16 02:35:06 -04:00
|
|
|
if (index < m_featureInstanceRegistrations.size())
|
|
|
|
{
|
2020-10-10 18:22:42 -04:00
|
|
|
m_featureInstanceRegistrations.removeAt(index);
|
2020-10-16 02:35:06 -04:00
|
|
|
renameFeatureInstances();
|
|
|
|
MainCore::instance()->removeFeatureInstanceAt(this, index);
|
2020-10-10 18:22:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureSet::clearFeatures()
|
|
|
|
{
|
|
|
|
m_featureInstanceRegistrations.clear();
|
2020-10-16 02:35:06 -04:00
|
|
|
MainCore::instance()->clearFeatures(this);
|
2020-10-10 18:22:42 -04:00
|
|
|
}
|