From: Soeren Apel Date: Mon, 26 Jun 2017 20:49:28 +0000 (+0200) Subject: Fix #831 by saving/restoring PD options X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d3feec232211b3acec6a323ca3b8008ed8b2faa0;p=pulseview.git Fix #831 by saving/restoring PD options --- diff --git a/pv/data/decodesignal.cpp b/pv/data/decodesignal.cpp index 49b76f2a..b1f3cea7 100644 --- a/pv/data/decodesignal.cpp +++ b/pv/data/decodesignal.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include using std::lock_guard; @@ -422,6 +423,22 @@ void DecodeSignal::save_settings(QSettings &settings) const settings.setValue("id", decoder->decoder()->id); + // Save decoder options + const map& options = decoder->options(); + + settings.setValue("options", (int)options.size()); + + // Note: decode::Decoder::options() returns only the options + // that differ from the default. See binding::Decoder::getter() + int i = 0; + for (auto option : options) { + settings.beginGroup("option" + QString::number(i)); + settings.setValue("name", QString::fromStdString(option.first)); + GlobalSettings::store_gvariant(settings, option.second); + settings.endGroup(); + i++; + } + settings.endGroup(); } @@ -447,8 +464,6 @@ void DecodeSignal::save_settings(QSettings &settings) const settings.endGroup(); } - - // TODO Save decoder options } void DecodeSignal::restore_settings(QSettings &settings) @@ -471,7 +486,21 @@ void DecodeSignal::restore_settings(QSettings &settings) continue; if (QString::fromUtf8(dec->id) == id) { - stack_.push_back(make_shared(dec)); + shared_ptr decoder = + make_shared(dec); + + stack_.push_back(decoder); + + // Restore decoder options that differ from their default + int options = settings.value("options").toInt(); + + for (int i = 0; i < options; i++) { + settings.beginGroup("option" + QString::number(i)); + QString name = settings.value("name").toString(); + GVariant *value = GlobalSettings::restore_gvariant(settings); + decoder->set_option(name.toUtf8(), value); + settings.endGroup(); + } // Include the newly created decode channels in the channel lists update_channel_list(); @@ -511,8 +540,6 @@ void DecodeSignal::restore_settings(QSettings &settings) } begin_decode(); - - // TODO Restore decoder options } void DecodeSignal::update_channel_list() diff --git a/pv/globalsettings.cpp b/pv/globalsettings.cpp index 915eaebc..6b13df16 100644 --- a/pv/globalsettings.cpp +++ b/pv/globalsettings.cpp @@ -19,6 +19,9 @@ #include "globalsettings.hpp" +#include +#include + using std::function; using std::map; using std::multimap; @@ -102,4 +105,37 @@ void GlobalSettings::undo_tracked_changes() tracked_changes_.clear(); } +void GlobalSettings::store_gvariant(QSettings &settings, GVariant *v) +{ + const GVariantType *var_type = g_variant_get_type(v); + char *var_type_str = g_variant_type_dup_string(var_type); + + QByteArray var_data = QByteArray((const char*)g_variant_get_data(v), + g_variant_get_size(v)); + + settings.setValue("value", var_data); + settings.setValue("type", var_type_str); + + g_free(var_type_str); +} + +GVariant* GlobalSettings::restore_gvariant(QSettings &settings) +{ + QString raw_type = settings.value("type").toString(); + GVariantType *var_type = g_variant_type_new(raw_type.toUtf8()); + + QByteArray data = settings.value("value").toByteArray(); + + gpointer var_data = g_memdup((gconstpointer)data.constData(), + (guint)data.size()); + + GVariant *value = g_variant_new_from_data(var_type, var_data, + data.size(), false, g_free, var_data); + + g_variant_type_free(var_type); + + return value; +} + + } // namespace pv diff --git a/pv/globalsettings.hpp b/pv/globalsettings.hpp index eb964fa2..05ff126d 100644 --- a/pv/globalsettings.hpp +++ b/pv/globalsettings.hpp @@ -23,6 +23,8 @@ #include #include +#include + #include #include #include @@ -74,6 +76,10 @@ public: */ void undo_tracked_changes(); + static void store_gvariant(QSettings &settings, GVariant *v); + + static GVariant* restore_gvariant(QSettings &settings); + private: static multimap< QString, function > callbacks_;