diff --git a/include-gpl/mainwindow.h b/include-gpl/mainwindow.h index 0f8388707..e4dfb3736 100644 --- a/include-gpl/mainwindow.h +++ b/include-gpl/mainwindow.h @@ -103,7 +103,7 @@ private: PluginManager* m_pluginManager; void loadSettings(); - void loadPresetSettings(Preset* preset); + void loadPresetSettings(const Preset* preset); void savePresetSettings(Preset* preset); void saveSettings(); diff --git a/include-gpl/plugin/pluginmanager.h b/include-gpl/plugin/pluginmanager.h index fba9a0d58..6bd324ddd 100644 --- a/include-gpl/plugin/pluginmanager.h +++ b/include-gpl/plugin/pluginmanager.h @@ -47,8 +47,10 @@ public: void registerSampleSource(const QString& sourceName, PluginInterface* plugin); - void loadSettings(Preset* preset); + void loadSettings(const Preset* preset); + void loadSourceSettings(const Preset* preset); void saveSettings(Preset* preset); + void saveSourceSettings(Preset* preset); void freeAll(); diff --git a/include-gpl/settings/mainsettings.h b/include-gpl/settings/mainsettings.h new file mode 100644 index 000000000..f54d1b5c8 --- /dev/null +++ b/include-gpl/settings/mainsettings.h @@ -0,0 +1,34 @@ +#ifndef INCLUDE_SETTINGS_H +#define INCLUDE_SETTINGS_H + +#include +#include "preferences.h" +#include "preset.h" + +class MainSettings { +public: + MainSettings(); + ~MainSettings(); + + void load(); + void save() const; + + void resetToDefaults(); + + Preset* newPreset(const QString& group, const QString& description); + void deletePreset(const Preset* preset); + int getPresetCount() const { return m_presets.count(); } + const Preset* getPreset(int index) const { return m_presets[index]; } + + Preset* getWorkingPreset() { return &m_workingPreset; } + int getSourceIndex() const { return m_preferences.getSourceIndex(); } + void setSourceIndex(int value) { m_preferences.setSourceIndex(value); } + +protected: + Preferences m_preferences; + Preset m_workingPreset; + typedef QList Presets; + Presets m_presets; +}; + +#endif // INCLUDE_SETTINGS_H diff --git a/include-gpl/settings/preset.h b/include-gpl/settings/preset.h index 284411140..df85bb7e3 100644 --- a/include-gpl/settings/preset.h +++ b/include-gpl/settings/preset.h @@ -64,19 +64,8 @@ public: void setSourceConfig(const QString& sourceId, const QString& sourceSerial, int sourceSequence, const QByteArray& config) { addOrUpdateSourceConfig(sourceId, sourceSerial, sourceSequence, config); - /* - m_sourceId = sourceId; - m_sourceSerial = sourceSerial; - m_sourceSequence = sourceSequence; - m_sourceConfig = config;*/ } - /* - const QString& getSourceId() const { return m_sourceId; } - const QString& getSourceSerial() const { return m_sourceSerial; } - const int getSourceSequence() const { return m_sourceSequence; } - const QByteArray& getSourceConfig() const { return m_sourceConfig; }*/ - void addOrUpdateSourceConfig(const QString& sourceId, const QString& sourceSerial, int sourceSequence, @@ -84,7 +73,7 @@ public: const QByteArray* findBestSourceConfig(const QString& sourceId, const QString& sourceSerial, - int sourceSequence); + int sourceSequence) const; protected: // group and preset description diff --git a/sdrbase/mainwindow.cpp b/sdrbase/mainwindow.cpp index 11d75a542..b8e95fe64 100644 --- a/sdrbase/mainwindow.cpp +++ b/sdrbase/mainwindow.cpp @@ -124,9 +124,10 @@ MainWindow::MainWindow(QWidget* parent) : if (nbSources > 0) { - //bool sampleSourceSignalsBlocked = ui->sampleSource->blockSignals(true); + m_pluginManager->selectSampleSource(sampleSourceIndex); + bool sampleSourceSignalsBlocked = ui->sampleSource->blockSignals(true); ui->sampleSource->setCurrentIndex(sampleSourceIndex); - //ui->sampleSource->blockSignals(sampleSourceSignalsBlocked); + ui->sampleSource->blockSignals(sampleSourceSignalsBlocked); } qDebug() << "MainWindow::MainWindow: load current preset settings..."; @@ -211,12 +212,13 @@ void MainWindow::loadSettings() } } -void MainWindow::loadPresetSettings(Preset* preset) +void MainWindow::loadPresetSettings(const Preset* preset) { qDebug("MainWindow::loadPresetSettings: preset [%s | %s]", qPrintable(preset->getGroup()), qPrintable(preset->getDescription())); + ui->glSpectrumGUI->deserialize(preset->getSpectrumConfig()); m_pluginManager->loadSettings(preset); // has to be last step @@ -229,7 +231,6 @@ void MainWindow::saveSettings() savePresetSettings(m_settings.getWorkingPreset()); m_settings.save(); - } void MainWindow::savePresetSettings(Preset* preset) @@ -238,6 +239,8 @@ void MainWindow::savePresetSettings(Preset* preset) qPrintable(preset->getGroup()), qPrintable(preset->getDescription())); + preset->setSpectrumConfig(ui->glSpectrumGUI->serialize()); + preset->clearChannels(); m_pluginManager->saveSettings(preset); preset->setLayout(saveState()); @@ -498,14 +501,16 @@ void MainWindow::on_presetLoad_clicked() if(item == 0) { + qDebug("MainWindow::on_presetLoad_clicked: item null"); updatePresetControls(); return; } - Preset* preset = qvariant_cast(item->data(0, Qt::UserRole)); + const Preset* preset = qvariant_cast(item->data(0, Qt::UserRole)); if(preset == 0) { + qDebug("MainWindow::on_presetLoad_clicked: preset null"); return; } @@ -556,10 +561,10 @@ void MainWindow::on_action_Preferences_triggered() void MainWindow::on_sampleSource_currentIndexChanged(int index) { - savePresetSettings(m_settings.getWorkingPreset()); + m_pluginManager->saveSourceSettings(m_settings.getWorkingPreset()); m_pluginManager->selectSampleSource(ui->sampleSource->currentIndex()); m_settings.setSourceIndex(ui->sampleSource->currentIndex()); - m_pluginManager->loadSettings(m_settings.getWorkingPreset()); + m_pluginManager->loadSourceSettings(m_settings.getWorkingPreset()); } void MainWindow::on_action_About_triggered() diff --git a/sdrbase/plugin/pluginmanager.cpp b/sdrbase/plugin/pluginmanager.cpp index 7776fc8f7..21b406f81 100644 --- a/sdrbase/plugin/pluginmanager.cpp +++ b/sdrbase/plugin/pluginmanager.cpp @@ -83,7 +83,7 @@ void PluginManager::registerSampleSource(const QString& sourceName, PluginInterf m_sampleSourceRegistrations.append(SampleSourceRegistration(sourceName, plugin)); } -void PluginManager::loadSettings(Preset* preset) +void PluginManager::loadSettings(const Preset* preset) { fprintf(stderr, "PluginManager::loadSettings: Loading preset [%s | %s]\n", qPrintable(preset->getGroup()), qPrintable(preset->getDescription())); @@ -142,6 +142,13 @@ void PluginManager::loadSettings(Preset* preset) renameChannelInstances(); + loadSourceSettings(preset); +} + +void PluginManager::loadSourceSettings(const Preset* preset) +{ + fprintf(stderr, "PluginManager::loadSourceSettings: Loading preset [%s | %s]\n", qPrintable(preset->getGroup()), qPrintable(preset->getDescription())); + if(m_sampleSourcePluginGUI != 0) { const QByteArray* sourceConfig = preset->findBestSourceConfig(m_sampleSourceId, m_sampleSourceSerial, m_sampleSourceSequence); @@ -151,15 +158,6 @@ void PluginManager::loadSettings(Preset* preset) qDebug() << "PluginManager::loadSettings: deserializing source " << qPrintable(m_sampleSourceId); m_sampleSourcePluginGUI->deserialize(*sourceConfig); } - /* - qDebug("PluginManager::loadSettings: source compare [%s] vs [%s]", qPrintable(m_sampleSourceId), qPrintable(preset->getSourceId())); - - // TODO: have one set of source presets per identified source (preset -> find source with name) - if(m_sampleSourceId == preset->getSourceId()) - { - qDebug() << "PluginManager::loadSettings: deserializing source " << qPrintable(m_sampleSourceId); - m_sampleSourcePluginGUI->deserialize(preset->getSourceConfig()); - }*/ qint64 centerFrequency = preset->getCenterFrequency(); m_sampleSourcePluginGUI->setCenterFrequency(centerFrequency); @@ -181,17 +179,8 @@ bool PluginManager::ChannelInstanceRegistration::operator<(const ChannelInstance void PluginManager::saveSettings(Preset* preset) { - if(m_sampleSourcePluginGUI != NULL) - { - preset->addOrUpdateSourceConfig(m_sampleSourceId, m_sampleSourceSerial, m_sampleSourceSequence, m_sampleSourcePluginGUI->serialize()); - //preset->setSourceConfig(m_sampleSourceId, m_sampleSourceSerial, m_sampleSourceSequence, m_sampleSourcePluginGUI->serialize()); - preset->setCenterFrequency(m_sampleSourcePluginGUI->getCenterFrequency()); - } - /* - else - { - preset->setSourceConfig(QString::null, QString::null, 0, QByteArray()); - }*/ + qDebug("PluginManager::saveSettings"); + saveSourceSettings(preset); qSort(m_channelInstanceRegistrations.begin(), m_channelInstanceRegistrations.end()); // sort by increasing delta frequency and type @@ -201,6 +190,18 @@ void PluginManager::saveSettings(Preset* preset) } } +void PluginManager::saveSourceSettings(Preset* preset) +{ + qDebug("PluginManager::saveSourceSettings"); + + if(m_sampleSourcePluginGUI != NULL) + { + preset->addOrUpdateSourceConfig(m_sampleSourceId, m_sampleSourceSerial, m_sampleSourceSequence, m_sampleSourcePluginGUI->serialize()); + //preset->setSourceConfig(m_sampleSourceId, m_sampleSourceSerial, m_sampleSourceSequence, m_sampleSourcePluginGUI->serialize()); + preset->setCenterFrequency(m_sampleSourcePluginGUI->getCenterFrequency()); + } +} + void PluginManager::freeAll() { m_dspEngine->stopAcquistion(); diff --git a/sdrbase/settings/mainsettings.cpp b/sdrbase/settings/mainsettings.cpp new file mode 100644 index 000000000..5eecb9072 --- /dev/null +++ b/sdrbase/settings/mainsettings.cpp @@ -0,0 +1,94 @@ +#include +#include + +#include "settings/mainsettings.h" + +MainSettings::MainSettings() +{ + resetToDefaults(); +} + +MainSettings::~MainSettings() +{ + for(int i = 0; i < m_presets.count(); ++i) + { + delete m_presets[i]; + } +} + +void MainSettings::load() +{ + QSettings s; + + m_preferences.deserialize(qUncompress(QByteArray::fromBase64(s.value("preferences").toByteArray()))); + m_workingPreset.deserialize(qUncompress(QByteArray::fromBase64(s.value("current").toByteArray()))); + + QStringList groups = s.childGroups(); + + for(int i = 0; i < groups.size(); ++i) + { + if(groups[i].startsWith("preset")) + { + s.beginGroup(groups[i]); + Preset* preset = new Preset; + + if(preset->deserialize(qUncompress(QByteArray::fromBase64(s.value("data").toByteArray())))) + { + m_presets.append(preset); + } + else + { + delete preset; + } + + s.endGroup(); + } + } +} + +void MainSettings::save() const +{ + QSettings s; + + s.setValue("preferences", qCompress(m_preferences.serialize()).toBase64()); + s.setValue("current", qCompress(m_workingPreset.serialize()).toBase64()); + + QStringList groups = s.childGroups(); + + for(int i = 0; i < groups.size(); ++i) + { + if(groups[i].startsWith("preset")) + { + s.remove(groups[i]); + } + } + + for(int i = 0; i < m_presets.count(); ++i) + { + QString group = QString("preset-%1").arg(i + 1); + s.beginGroup(group); + s.setValue("data", qCompress(m_presets[i]->serialize()).toBase64()); + s.endGroup(); + } +} + +void MainSettings::resetToDefaults() +{ + m_preferences.resetToDefaults(); + m_workingPreset.resetToDefaults(); +} + +Preset* MainSettings::newPreset(const QString& group, const QString& description) +{ + Preset* preset = new Preset(); + preset->setGroup(group); + preset->setDescription(description); + m_presets.append(preset); + return preset; +} + +void MainSettings::deletePreset(const Preset* preset) +{ + m_presets.removeAll((Preset*)preset); + delete (Preset*)preset; +} diff --git a/sdrbase/settings/preset.cpp b/sdrbase/settings/preset.cpp index 7dc83f772..8c16f710b 100644 --- a/sdrbase/settings/preset.cpp +++ b/sdrbase/settings/preset.cpp @@ -34,6 +34,7 @@ QByteArray Preset::serialize() const s.writeString(2, m_description); s.writeU64(3, m_centerFrequency); s.writeBlob(4, m_layout); + s.writeBlob(5, m_spectrumConfig); s.writeS32(20, m_sourceConfigs.size()); @@ -44,7 +45,7 @@ QByteArray Preset::serialize() const s.writeS32(26 + i*4, m_sourceConfigs[i].m_sourceSequence); s.writeBlob(27 + i*4, m_sourceConfigs[i].m_config); - qDebug("Preset::serialize: source: id: %ss, ser: %s, seq: %d", + qDebug("Preset::serialize: source: id: %s, ser: %s, seq: %d", qPrintable(m_sourceConfigs[i].m_sourceId), qPrintable(m_sourceConfigs[i].m_sourceSerial), m_sourceConfigs[i].m_sourceSequence); @@ -55,6 +56,16 @@ QByteArray Preset::serialize() const } } + s.writeS32(200, m_channelConfigs.size()); + + for(int i = 0; i < m_channelConfigs.size(); i++) + { + qDebug("Preset::serialize: channel: id: %s", qPrintable(m_channelConfigs[i].m_channel)); + + s.writeString(201 + i * 2, m_channelConfigs[i].m_channel); + s.writeBlob(202 + i * 2, m_channelConfigs[i].m_config); + } + return s.final(); } @@ -74,6 +85,7 @@ bool Preset::deserialize(const QByteArray& data) d.readString(2, &m_description, "no name"); d.readU64(3, &m_centerFrequency, 0); d.readBlob(4, &m_layout); + d.readBlob(5, &m_spectrumConfig); qDebug("Preset::deserialize: m_group: %s m_description: %s m_centerFrequency: %llu", qPrintable(m_group), @@ -88,7 +100,9 @@ bool Preset::deserialize(const QByteArray& data) sourcesCount = ((200-23)/4) - 1; } - for(int i = 0; i < sourcesCount; i++) + m_sourceConfigs.clear(); + + for (int i = 0; i < sourcesCount; i++) { QString sourceId, sourceSerial; int sourceSequence; @@ -110,7 +124,22 @@ bool Preset::deserialize(const QByteArray& data) } } - return true; + qint32 channelCount; + d.readS32(200, &channelCount, 0); + + m_channelConfigs.clear(); + + for (int i = 0; i < channelCount; i++) + { + QString channel; + QByteArray config; + + d.readString(201 + i * 2, &channel, "unknown-channel"); + d.readBlob(202 + i * 2, &config); + + qDebug("Preset::deserialize: channel: id: %s", qPrintable(channel)); + m_channelConfigs.append(ChannelConfig(channel, config)); + } } else { @@ -159,7 +188,7 @@ void Preset::addOrUpdateSourceConfig(const QString& sourceId, const QByteArray* Preset::findBestSourceConfig(const QString& sourceId, const QString& sourceSerial, - int sourceSequence) + int sourceSequence) const { SourceConfigs::const_iterator it = m_sourceConfigs.begin(); SourceConfigs::const_iterator itFirstOfKind = m_sourceConfigs.end();