]> sigrok.org Git - pulseview.git/blame - pv/prop/binding/decoderoptions.cpp
StoreSession: Support output formats other than srzip
[pulseview.git] / pv / prop / binding / decoderoptions.cpp
CommitLineData
21ad818f
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
4e5a4405
JH
21#include <libsigrokdecode/libsigrokdecode.h>
22
2acdb232 23#include "decoderoptions.hpp"
21ad818f 24
67fe5e9c
JH
25#include <boost/none_t.hpp>
26
2acdb232
JH
27#include <pv/data/decoderstack.hpp>
28#include <pv/data/decode/decoder.hpp>
29#include <pv/prop/double.hpp>
30#include <pv/prop/enum.hpp>
31#include <pv/prop/int.hpp>
32#include <pv/prop/string.hpp>
67fe5e9c 33
819f4c25 34using boost::none;
182d2f5d 35using std::make_pair;
819f4c25 36using std::map;
182d2f5d 37using std::pair;
f9abf97e 38using std::shared_ptr;
819f4c25 39using std::string;
182d2f5d 40using std::vector;
67fe5e9c 41
21ad818f
JH
42namespace pv {
43namespace prop {
44namespace binding {
45
7491a29f
JH
46DecoderOptions::DecoderOptions(
47 shared_ptr<pv::data::DecoderStack> decoder_stack,
48 shared_ptr<data::decode::Decoder> decoder) :
8dbbc7f0
JH
49 decoder_stack_(decoder_stack),
50 decoder_(decoder)
67fe5e9c 51{
8dbbc7f0 52 assert(decoder_);
67fe5e9c 53
8dbbc7f0 54 const srd_decoder *const dec = decoder_->decoder();
4e5a4405 55 assert(dec);
67fe5e9c 56
4e5a4405 57 for (GSList *l = dec->options; l; l = l->next)
67fe5e9c
JH
58 {
59 const srd_decoder_option *const opt =
60 (srd_decoder_option*)l->data;
61
636782c1 62 const QString name = QString::fromUtf8(opt->desc);
67fe5e9c 63
6db73158
JH
64 const Property::Getter get = [&, opt]() {
65 return getter(opt->id); };
e8d00928 66 const Property::Setter set = [&, opt](Glib::VariantBase value) {
6db73158 67 setter(opt->id, value); };
67fe5e9c
JH
68
69 shared_ptr<Property> prop;
70
182d2f5d 71 if (opt->values)
6db73158 72 prop = bind_enum(name, opt, get, set);
a28c3025
JH
73 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
74 prop = shared_ptr<Property>(new Double(name, 2, "",
6db73158 75 none, none, get, set));
182d2f5d 76 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
67fe5e9c 77 prop = shared_ptr<Property>(
6db73158 78 new Int(name, "", none, get, set));
67fe5e9c
JH
79 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
80 prop = shared_ptr<Property>(
6db73158 81 new String(name, get, set));
67fe5e9c
JH
82 else
83 continue;
84
8dbbc7f0 85 properties_.push_back(prop);
67fe5e9c
JH
86 }
87}
88
182d2f5d
JH
89shared_ptr<Property> DecoderOptions::bind_enum(
90 const QString &name, const srd_decoder_option *option,
91 Property::Getter getter, Property::Setter setter)
92{
e8d00928 93 vector< pair<Glib::VariantBase, QString> > values;
182d2f5d 94 for (GSList *l = option->values; l; l = l->next) {
e8d00928 95 Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
182d2f5d
JH
96 values.push_back(make_pair(var, print_gvariant(var)));
97 }
98
99 return shared_ptr<Property>(new Enum(name, values, getter, setter));
100}
101
e8d00928 102Glib::VariantBase DecoderOptions::getter(const char *id)
67fe5e9c 103{
615f6d25
JH
104 GVariant *val = NULL;
105
8dbbc7f0 106 assert(decoder_);
4e5a4405 107
67fe5e9c 108 // Get the value from the hash table if it is already present
8dbbc7f0 109 const map<string, GVariant*>& options = decoder_->options();
f46e495e 110 const auto iter = options.find(id);
67fe5e9c 111
615f6d25
JH
112 if (iter != options.end())
113 val = (*iter).second;
114 else
67fe5e9c 115 {
8dbbc7f0 116 assert(decoder_->decoder());
4e5a4405 117
67fe5e9c 118 // Get the default value if not
8dbbc7f0 119 for (GSList *l = decoder_->decoder()->options; l; l = l->next)
67fe5e9c
JH
120 {
121 const srd_decoder_option *const opt =
122 (srd_decoder_option*)l->data;
1429b21d 123 if (strcmp(opt->id, id) == 0) {
67fe5e9c 124 val = opt->def;
1429b21d
BV
125 break;
126 }
67fe5e9c
JH
127 }
128 }
129
130 if (val)
e8d00928
ML
131 return Glib::VariantBase(val, true);
132 else
133 return Glib::VariantBase();
67fe5e9c
JH
134}
135
e8d00928 136void DecoderOptions::setter(const char *id, Glib::VariantBase value)
21ad818f 137{
8dbbc7f0
JH
138 assert(decoder_);
139 decoder_->set_option(id, value.gobj());
7491a29f 140
8dbbc7f0
JH
141 assert(decoder_stack_);
142 decoder_stack_->begin_decode();
21ad818f
JH
143}
144
145} // binding
146} // prop
147} // pv