diff --git a/sdrbase/device/deviceapi.cpp b/sdrbase/device/deviceapi.cpp index 398a3ea6d..54455230a 100644 --- a/sdrbase/device/deviceapi.cpp +++ b/sdrbase/device/deviceapi.cpp @@ -16,6 +16,8 @@ #include "device/deviceapi.h" #include "plugin/plugingui.h" +#include "plugin/pluginapi.h" +#include "plugin/plugininterface.h" #include "gui/glspectrum.h" #include "gui/channelwindow.h" #include "mainwindow.h" @@ -156,10 +158,44 @@ void DeviceAPI::setSampleSourcePluginGUI(PluginGUI *gui) m_sampleSourcePluginGUI = gui; } +void DeviceAPI::registerChannelInstance(const QString& channelName, PluginGUI* pluginGUI) +{ + m_channelInstanceRegistrations.append(ChannelInstanceRegistration(channelName, pluginGUI)); + renameChannelInstances(); +} + +void DeviceAPI::removeChannelInstance(PluginGUI* pluginGUI) +{ + for(ChannelInstanceRegistrations::iterator it = m_channelInstanceRegistrations.begin(); it != m_channelInstanceRegistrations.end(); ++it) + { + if(it->m_gui == pluginGUI) + { + m_channelInstanceRegistrations.erase(it); + break; + } + } + + renameChannelInstances(); +} + +void DeviceAPI::renameChannelInstances() +{ + for(int i = 0; i < m_channelInstanceRegistrations.count(); i++) + { + m_channelInstanceRegistrations[i].m_gui->setName(QString("%1:%2").arg(m_channelInstanceRegistrations[i].m_channelName).arg(i)); + } +} + void DeviceAPI::freeAll() { m_deviceEngine->stopAcquistion(); + while(!m_channelInstanceRegistrations.isEmpty()) + { + ChannelInstanceRegistration reg(m_channelInstanceRegistrations.takeLast()); + reg.m_gui->destroy(); + } + if(m_sampleSourcePluginGUI != 0) { m_deviceEngine->setSource(0); @@ -199,6 +235,69 @@ void DeviceAPI::saveSourceSettings(Preset* preset) } } +void DeviceAPI::loadChannelSettings(const Preset *preset, PluginAPI *pluginAPI) +{ + qDebug("DeviceAPI::loadChannelSettings: Loading preset [%s | %s]\n", qPrintable(preset->getGroup()), qPrintable(preset->getDescription())); + + // Available channel plugins + PluginAPI::ChannelRegistrations *channelRegistrations = pluginAPI->getChannelRegistrations(); + + // copy currently open channels and clear list + ChannelInstanceRegistrations openChannels = m_channelInstanceRegistrations; + m_channelInstanceRegistrations.clear(); + + for(int i = 0; i < preset->getChannelCount(); i++) + { + const Preset::ChannelConfig& channelConfig = preset->getChannelConfig(i); + ChannelInstanceRegistration reg; + + // if we have one instance available already, use it + + for(int i = 0; i < openChannels.count(); i++) + { + qDebug("PluginManager::loadSettings: channels compare [%s] vs [%s]", qPrintable(openChannels[i].m_channelName), qPrintable(channelConfig.m_channel)); + + if(openChannels[i].m_channelName == channelConfig.m_channel) + { + qDebug("PluginManager::loadSettings: channel [%s] found", qPrintable(openChannels[i].m_channelName)); + reg = openChannels.takeAt(i); + m_channelInstanceRegistrations.append(reg); + break; + } + } + + // if we haven't one already, create one + + if(reg.m_gui == NULL) + { + for(int i = 0; i < channelRegistrations->count(); i++) + { + if((*channelRegistrations)[i].m_channelName == channelConfig.m_channel) + { + qDebug("PluginManager::loadSettings: creating new channel [%s]", qPrintable(channelConfig.m_channel)); + reg = ChannelInstanceRegistration(channelConfig.m_channel, (*channelRegistrations)[i].m_plugin->createChannel(channelConfig.m_channel, this)); + break; + } + } + } + + if(reg.m_gui != NULL) + { + qDebug("PluginManager::loadSettings: deserializing channel [%s]", qPrintable(channelConfig.m_channel)); + reg.m_gui->deserialize(channelConfig.m_config); + } + } + + // everything, that is still "available" is not needed anymore + for(int i = 0; i < openChannels.count(); i++) + { + qDebug("PluginManager::loadSettings: destroying spare channel [%s]", qPrintable(openChannels[i].m_channelName)); + openChannels[i].m_gui->destroy(); + } + + renameChannelInstances(); +} + void DeviceAPI::saveChannelSettings(Preset *preset) { qDebug("DeviceAPI::saveChannelSettings"); diff --git a/sdrbase/device/deviceapi.h b/sdrbase/device/deviceapi.h index 83328e76f..64acc16bd 100644 --- a/sdrbase/device/deviceapi.h +++ b/sdrbase/device/deviceapi.h @@ -35,6 +35,7 @@ class MessageQueue; class ChannelMarker; class QWidget; class PluginGUI; +class PluginAPI; class Preset; class SDRANGEL_API DeviceAPI : public QObject { @@ -69,10 +70,14 @@ public: void setSampleSourceSequence(int sequence); void setSampleSourcePluginGUI(PluginGUI *gui); + void registerChannelInstance(const QString& channelName, PluginGUI* pluginGUI); + void removeChannelInstance(PluginGUI* pluginGUI); + void freeAll(); void loadSourceSettings(const Preset* preset); void saveSourceSettings(Preset* preset); + void loadChannelSettings(const Preset* preset, PluginAPI *pluginAPI); void saveChannelSettings(Preset* preset); protected: @@ -103,6 +108,8 @@ protected: ChannelWindow *channelWindow); ~DeviceAPI(); + void renameChannelInstances(); + MainWindow *m_mainWindow; int m_deviceTabIndex; DSPDeviceEngine *m_deviceEngine; diff --git a/sdrbase/plugin/pluginapi.cpp b/sdrbase/plugin/pluginapi.cpp index c0e13f730..22d399b43 100644 --- a/sdrbase/plugin/pluginapi.cpp +++ b/sdrbase/plugin/pluginapi.cpp @@ -28,6 +28,12 @@ void PluginAPI::registerSampleSource(const QString& sourceName, PluginInterface* m_pluginManager->registerSampleSource(sourceName, plugin); } +PluginAPI::ChannelRegistrations *PluginAPI::getChannelRegistrations() +{ + return m_pluginManager->getChannelRegistrations(); +} + + PluginAPI::PluginAPI(PluginManager* pluginManager, MainWindow* mainWindow) : QObject(mainWindow), m_pluginManager(pluginManager), diff --git a/sdrbase/plugin/pluginapi.h b/sdrbase/plugin/pluginapi.h index 338126fa8..dbf4890a9 100644 --- a/sdrbase/plugin/pluginapi.h +++ b/sdrbase/plugin/pluginapi.h @@ -37,6 +37,7 @@ public: void registerChannel(const QString& channelName, PluginInterface* plugin); void registerChannelInstance(const QString& channelName, PluginGUI* pluginGUI); void removeChannelInstance(PluginGUI* pluginGUI); + ChannelRegistrations *getChannelRegistrations(); // Sample Source stuff void registerSampleSource(const QString& sourceName, PluginInterface* plugin); diff --git a/sdrbase/plugin/pluginmanager.h b/sdrbase/plugin/pluginmanager.h index 4d6c4d36c..688465978 100644 --- a/sdrbase/plugin/pluginmanager.h +++ b/sdrbase/plugin/pluginmanager.h @@ -47,6 +47,8 @@ public: void removeChannelInstance(PluginGUI* pluginGUI); void registerSampleSource(const QString& sourceName, PluginInterface* plugin); + PluginAPI::ChannelRegistrations *getChannelRegistrations() { return &m_channelRegistrations; } + void loadChannelSettings(const Preset* preset, DeviceAPI *deviceAPI); // void loadSourceSettings(const Preset* preset); void saveSettings(Preset* preset);