From: Joel Holdsworth Date: Fri, 6 Sep 2013 23:54:30 +0000 (+0100) Subject: Implemented decoder options binding X-Git-Tag: pulseview-0.2.0~288 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=67fe5e9c02e4e9cfe94d465a0a5e5b598129e4e3;p=pulseview.git Implemented decoder options binding --- diff --git a/pv/data/decoder.cpp b/pv/data/decoder.cpp index 2848d16c..787d1600 100644 --- a/pv/data/decoder.cpp +++ b/pv/data/decoder.cpp @@ -41,9 +41,11 @@ const int64_t Decoder::DecodeChunkLength = 4096; Decoder::Decoder(const srd_decoder *const dec, std::map > probes) : + boost::shared_ptr > 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; diff --git a/pv/data/decoder.h b/pv/data/decoder.h index 4cae427e..8a0f00a4 100644 --- a/pv/data/decoder.h +++ b/pv/data/decoder.h @@ -61,7 +61,8 @@ private: public: Decoder(const srd_decoder *const decoder, std::map > probes); + boost::shared_ptr > probes, + GHashTable *options); virtual ~Decoder(); @@ -86,6 +87,7 @@ private: const srd_decoder *const _decoder; std::map > _probes; + GHashTable *_options; srd_decoder_inst *_decoder_inst; diff --git a/pv/dialogs/decoder.cpp b/pv/dialogs/decoder.cpp index 1611d9b3..b7ddb521 100644 --- a/pv/dialogs/decoder.cpp +++ b/pv/dialogs/decoder.cpp @@ -40,10 +40,12 @@ namespace pv { namespace dialogs { Decoder::Decoder(QWidget *parent, const srd_decoder *decoder, - const vector< shared_ptr > &sigs) : + const vector< shared_ptr > &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("* Required Probes"), &_form)); + + // Add the options + if (!_binding.properties().empty()) { + _form_layout.addRow(new QLabel(tr("

Options

"), + &_form)); + _binding.add_properties_to_form(&_form_layout); + } +} + +void Decoder::accept() +{ + QDialog::accept(); + _binding.commit(); } QComboBox* Decoder::create_probe_selector( diff --git a/pv/dialogs/decoder.h b/pv/dialogs/decoder.h index 08af38cb..2277f987 100644 --- a/pv/dialogs/decoder.h +++ b/pv/dialogs/decoder.h @@ -33,6 +33,8 @@ #include #include +#include + 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 > &sigs); + const std::vector< boost::shared_ptr > &sigs, + GHashTable *options); + + void accept(); std::map > get_probes(); @@ -62,6 +67,9 @@ private: std::map _probe_selector_map; + GHashTable *const _options; + pv::prop::binding::DecoderOptions _binding; + QVBoxLayout _layout; QWidget _form; diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 6f852ce6..dc5f981f 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -391,11 +391,16 @@ void MainWindow::add_decoder(QObject *action) const std::vector< boost::shared_ptr > &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() diff --git a/pv/prop/binding/decoderoptions.cpp b/pv/prop/binding/decoderoptions.cpp index 27ac4456..435d5f6d 100644 --- a/pv/prop/binding/decoderoptions.cpp +++ b/pv/prop/binding/decoderoptions.cpp @@ -20,16 +20,83 @@ #include "decoderoptions.h" +#include +#include + +#include +#include + +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 prop; + + if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x"))) + prop = shared_ptr( + new Int(name, "", none, getter, setter)); + else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s"))) + prop = shared_ptr( + 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 - diff --git a/pv/prop/binding/decoderoptions.h b/pv/prop/binding/decoderoptions.h index ac32d42c..8217b479 100644 --- a/pv/prop/binding/decoderoptions.h +++ b/pv/prop/binding/decoderoptions.h @@ -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 diff --git a/pv/sigsession.cpp b/pv/sigsession.cpp index 1ee5e003..d8976a14 100644 --- a/pv/sigsession.cpp +++ b/pv/sigsession.cpp @@ -195,7 +195,8 @@ boost::shared_ptr SigSession::get_data() void SigSession::add_decoder(srd_decoder *const dec, std::map > probes) + boost::shared_ptr > probes, + GHashTable *options) { { lock_guard lock(_signals_mutex); @@ -207,7 +208,7 @@ void SigSession::add_decoder(srd_decoder *const dec, } shared_ptr decoder( - new data::Decoder(dec, probes)); + new data::Decoder(dec, probes, options)); shared_ptr d( new view::DecodeSignal(*this, decoder, _decode_traces.size())); diff --git a/pv/sigsession.h b/pv/sigsession.h index 6068b03c..75ecff68 100644 --- a/pv/sigsession.h +++ b/pv/sigsession.h @@ -95,7 +95,8 @@ public: void add_decoder(srd_decoder *const dec, std::map > probes); + boost::shared_ptr > probes, + GHashTable *options); std::vector< boost::shared_ptr > get_decode_signals() const;