]> sigrok.org Git - pulseview.git/commitdiff
Fix #831 by saving/restoring PD options
authorSoeren Apel <redacted>
Mon, 26 Jun 2017 20:49:28 +0000 (22:49 +0200)
committerUwe Hermann <redacted>
Wed, 5 Jul 2017 22:37:08 +0000 (00:37 +0200)
pv/data/decodesignal.cpp
pv/globalsettings.cpp
pv/globalsettings.hpp

index 49b76f2aa9cedd87533031cadd512d50e1589ab2..b1f3cea75a9d8d40a629df8d2f67ddb824cf2c0d 100644 (file)
@@ -29,6 +29,7 @@
 #include <pv/binding/decoder.hpp>
 #include <pv/data/decode/decoder.hpp>
 #include <pv/data/decode/row.hpp>
+#include <pv/globalsettings.hpp>
 #include <pv/session.hpp>
 
 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<string, GVariant*>& 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<decode::Decoder>(dec));
+                               shared_ptr<decode::Decoder> decoder =
+                                       make_shared<decode::Decoder>(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()
index 915eaebcc9bee7df37981770f158117b60ffcd42..6b13df16e397fc44ab6dd2473b92bba1f316c58f 100644 (file)
@@ -19,6 +19,9 @@
 
 #include "globalsettings.hpp"
 
+#include <QByteArray>
+#include <QString>
+
 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
index eb964fa2c677b802fe077e2da34de722328d6328..05ff126dd092e9070264a8ee55afeb24bc8c2970 100644 (file)
@@ -23,6 +23,8 @@
 #include <functional>
 #include <map>
 
+#include <glib.h>
+
 #include <QSettings>
 #include <QString>
 #include <QVariant>
@@ -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<void(QVariant)> > callbacks_;