]> sigrok.org Git - pulseview.git/commitdiff
Implemented decoder options binding
authorJoel Holdsworth <redacted>
Fri, 6 Sep 2013 23:54:30 +0000 (00:54 +0100)
committerJoel Holdsworth <redacted>
Sun, 29 Sep 2013 14:56:12 +0000 (23:56 +0900)
pv/data/decoder.cpp
pv/data/decoder.h
pv/dialogs/decoder.cpp
pv/dialogs/decoder.h
pv/mainwindow.cpp
pv/prop/binding/decoderoptions.cpp
pv/prop/binding/decoderoptions.h
pv/sigsession.cpp
pv/sigsession.h

index 2848d16ca4dffd166697b2517193acaec7030592..787d1600bb89f5695c44c90625c06aed44a5cf0a 100644 (file)
@@ -41,9 +41,11 @@ const int64_t Decoder::DecodeChunkLength = 4096;
 
 Decoder::Decoder(const srd_decoder *const dec,
        std::map<const srd_probe*,
-               boost::shared_ptr<pv::view::Signal> > probes) :
+               boost::shared_ptr<pv::view::Signal> > probes,
+       GHashTable *options) :
        _decoder(dec),
        _probes(probes),
+       _options(options),
        _decoder_inst(NULL)
 {
        init_decoder();
@@ -54,6 +56,8 @@ Decoder::~Decoder()
 {
        _decode_thread.interrupt();
        _decode_thread.join();
+
+       g_hash_table_destroy(_options);
 }
 
 const srd_decoder* Decoder::get_decoder() const
@@ -111,7 +115,7 @@ void Decoder::init_decoder()
                }
        }
 
-       _decoder_inst = srd_inst_new(_decoder->id, NULL);
+       _decoder_inst = srd_inst_new(_decoder->id, _options);
        assert(_decoder_inst);
 
        _decoder_inst->data_samplerate = _samplerate;
index 4cae427ea95813e842be6aaa25fb93641ee0de1d..8a0f00a42a5a731bb83b48d3a2c0bd6a3ce05e13 100644 (file)
@@ -61,7 +61,8 @@ private:
 public:
        Decoder(const srd_decoder *const decoder,
                std::map<const srd_probe*,
-                       boost::shared_ptr<pv::view::Signal> > probes);
+                       boost::shared_ptr<pv::view::Signal> > probes,
+               GHashTable *options);
 
        virtual ~Decoder();
 
@@ -86,6 +87,7 @@ private:
        const srd_decoder *const _decoder;
        std::map<const srd_probe*, boost::shared_ptr<view::Signal> >
                _probes;
+       GHashTable *_options;
 
        srd_decoder_inst *_decoder_inst;
 
index 1611d9b3217c9b22fd75192fc0d956e3d736aca5..b7ddb52135a52824c0f335a67b6c47d0e8f708d6 100644 (file)
@@ -40,10 +40,12 @@ namespace pv {
 namespace dialogs {
 
 Decoder::Decoder(QWidget *parent, const srd_decoder *decoder,
-       const vector< shared_ptr<view::Signal> > &sigs) :
+       const vector< shared_ptr<view::Signal> > &sigs, GHashTable *options) :
        QDialog(parent),
        _decoder(decoder),
        _sigs(sigs),
+       _options(options),
+       _binding(decoder, options),
        _layout(this),
        _form(this),
        _form_layout(&_form),
@@ -97,6 +99,19 @@ Decoder::Decoder(QWidget *parent, const srd_decoder *decoder,
 
        _form_layout.addRow(new QLabel(
                tr("<i>* Required Probes</i>"), &_form));
+
+       // Add the options
+       if (!_binding.properties().empty()) {
+               _form_layout.addRow(new QLabel(tr("<h3>Options</h3>"),
+                       &_form));
+               _binding.add_properties_to_form(&_form_layout);
+       }
+}
+
+void Decoder::accept()
+{
+       QDialog::accept();
+       _binding.commit();
 }
 
 QComboBox* Decoder::create_probe_selector(
index 08af38cb71bf6a655218deed04b7fc62e551f020..2277f987c23e4b0250ec715b1882949d4f4a43fd 100644 (file)
@@ -33,6 +33,8 @@
 #include <QLabel>
 #include <QVBoxLayout>
 
+#include <pv/prop/binding/decoderoptions.h>
+
 struct srd_decoder;
 
 namespace pv {
@@ -47,7 +49,10 @@ class Decoder : public QDialog
 {
 public:
        Decoder(QWidget *parent, const srd_decoder *decoder,
-               const std::vector< boost::shared_ptr<view::Signal> > &sigs);
+               const std::vector< boost::shared_ptr<view::Signal> > &sigs,
+               GHashTable *options);
+
+       void accept();
 
        std::map<const srd_probe*, boost::shared_ptr<view::Signal> >
                get_probes();
@@ -62,6 +67,9 @@ private:
 
        std::map<const srd_probe*, QComboBox*> _probe_selector_map;
 
+       GHashTable *const _options;
+       pv::prop::binding::DecoderOptions _binding;
+
        QVBoxLayout _layout;
 
        QWidget _form;
index 6f852ce6044e16a1fe305c2334b21b36227bc6c0..dc5f981f7b3a5207ab0bc777e1656e292d12784b 100644 (file)
@@ -391,11 +391,16 @@ void MainWindow::add_decoder(QObject *action)
        const std::vector< boost::shared_ptr<view::Signal> > &sigs =
                _session.get_signals();
 
-       dialogs::Decoder dlg(this, dec, sigs);
-       if(dlg.exec() != QDialog::Accepted)
+       GHashTable *const options = g_hash_table_new_full(g_str_hash,
+               g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
+
+       dialogs::Decoder dlg(this, dec, sigs, options);
+       if(dlg.exec() != QDialog::Accepted) {
+               g_hash_table_destroy(options);
                return;
+       }
 
-       _session.add_decoder(dec, dlg.get_probes());
+       _session.add_decoder(dec, dlg.get_probes(), options);
 }
 
 void MainWindow::run_stop()
index 27ac44566e68323ee5ab0a453006a25d23f4b486..435d5f6d8bac998e03cac28deb26818cf5a4c3b0 100644 (file)
 
 #include "decoderoptions.h"
 
+#include <boost/foreach.hpp>
+#include <boost/none_t.hpp>
+
+#include <pv/prop/int.h>
+#include <pv/prop/string.h>
+
+using namespace boost;
+using namespace std;
+
 namespace pv {
 namespace prop {
 namespace binding {
 
-DecoderOptions::DecoderOptions(struct srd_decoder *decoder) :
-       _decoder(decoder)
+DecoderOptions::DecoderOptions(const srd_decoder *decoder,
+       GHashTable *options) :
+       _decoder(decoder),
+       _options(options)
+{
+       assert(decoder);
+
+
+       for (GSList *l = decoder->options; l; l = l->next)
+       {
+               const srd_decoder_option *const opt =
+                       (srd_decoder_option*)l->data;
+
+               const QString name(opt->desc);
+
+               const Property::Getter getter = bind(
+                       &DecoderOptions::getter, this, opt->id);
+               const Property::Setter setter = bind(
+                       &DecoderOptions::setter, this, opt->id, _1);
+
+               shared_ptr<Property> prop;
+
+               if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
+                       prop = shared_ptr<Property>(
+                               new Int(name, "", none, getter, setter));
+               else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
+                       prop = shared_ptr<Property>(
+                               new String(name, getter, setter));
+               else
+                       continue;
+
+               _properties.push_back(prop);
+       }
+}
+
+GVariant* DecoderOptions::getter(const char *id)
+{
+       // Get the value from the hash table if it is already present
+       GVariant *val = (GVariant*)g_hash_table_lookup(_options, id);
+
+       if (!val)
+       {
+               // Get the default value if not
+               for (GSList *l = _decoder->options; l; l = l->next)
+               {
+                       const srd_decoder_option *const opt =
+                               (srd_decoder_option*)l->data;
+                       if (strcmp(opt->id, id) == 0)
+                               val = opt->def;
+               }
+       }
+
+       if (val)
+               g_variant_ref(val);
+
+       return val;
+}
+
+void DecoderOptions::setter(const char *id, GVariant *value)
 {
+       g_variant_ref(value);
+       g_hash_table_insert(_options, (void*)id, value);
 }
 
 } // binding
 } // prop
 } // pv
-
index ac32d42cabf29c9260da357d5e22b6a7d4a620f2..8217b4791e19c6f9a3b98b4f71df8f5118634f5a 100644 (file)
@@ -32,10 +32,16 @@ namespace binding {
 class DecoderOptions : public Binding
 {
 public:
-       DecoderOptions(struct srd_decoder *decoder);
+       DecoderOptions(const srd_decoder *decoder, GHashTable *options);
 
-protected:
-       struct srd_decoder *const _decoder;
+private:
+       GVariant* getter(const char *id);
+
+       void setter(const char *id, GVariant *value);
+
+private:
+       const srd_decoder *const _decoder;
+       GHashTable *const _options;
 };
 
 } // binding
index 1ee5e0033939a1918102f9edd71e84c4e48b03bc..d8976a1483c1c7ab47708f7fa285d84789b21c80 100644 (file)
@@ -195,7 +195,8 @@ boost::shared_ptr<data::Logic> SigSession::get_data()
 
 void SigSession::add_decoder(srd_decoder *const dec,
        std::map<const srd_probe*,
-               boost::shared_ptr<view::Signal> > probes)
+               boost::shared_ptr<view::Signal> > probes,
+       GHashTable *options)
 {
        {
                lock_guard<mutex> lock(_signals_mutex);
@@ -207,7 +208,7 @@ void SigSession::add_decoder(srd_decoder *const dec,
                }
 
                shared_ptr<data::Decoder> decoder(
-                       new data::Decoder(dec, probes));
+                       new data::Decoder(dec, probes, options));
                shared_ptr<view::DecodeSignal> d(
                        new view::DecodeSignal(*this, decoder,
                                _decode_traces.size()));
index 6068b03c3058177261dbe9ff31a96d10f6405c7b..75ecff683fab00f1aad317fc5cbb178da17cce76 100644 (file)
@@ -95,7 +95,8 @@ public:
 
        void add_decoder(srd_decoder *const dec,
                std::map<const srd_probe*,
-                       boost::shared_ptr<view::Signal> > probes);
+                       boost::shared_ptr<view::Signal> > probes,
+               GHashTable *options);
 
        std::vector< boost::shared_ptr<view::DecodeSignal> >
                get_decode_signals() const;