]> sigrok.org Git - pulseview.git/blob - pv/prop/binding/decoderoptions.cpp
c5278c75fbb82583ba7082e4d4dc04ef4bf502f6
[pulseview.git] / pv / prop / binding / decoderoptions.cpp
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
21 #include <libsigrokdecode/libsigrokdecode.h>
22
23 #include "decoderoptions.h"
24
25 #include <boost/none_t.hpp>
26
27 #include <pv/data/decoderstack.h>
28 #include <pv/data/decode/decoder.h>
29 #include <pv/prop/double.h>
30 #include <pv/prop/enum.h>
31 #include <pv/prop/int.h>
32 #include <pv/prop/string.h>
33
34 using boost::none;
35 using std::make_pair;
36 using std::map;
37 using std::pair;
38 using std::shared_ptr;
39 using std::string;
40 using std::vector;
41
42 namespace pv {
43 namespace prop {
44 namespace binding {
45
46 DecoderOptions::DecoderOptions(
47         shared_ptr<pv::data::DecoderStack> decoder_stack,
48         shared_ptr<data::decode::Decoder> decoder) :
49         _decoder_stack(decoder_stack),
50         _decoder(decoder)
51 {
52         assert(_decoder);
53
54         const srd_decoder *const dec = _decoder->decoder();
55         assert(dec);
56
57         for (GSList *l = dec->options; l; l = l->next)
58         {
59                 const srd_decoder_option *const opt =
60                         (srd_decoder_option*)l->data;
61
62                 const QString name = QString::fromUtf8(opt->desc);
63
64                 const Property::Getter get = [&, opt]() {
65                         return getter(opt->id); };
66                 const Property::Setter set = [&, opt](GVariant *value) {
67                         setter(opt->id, value); };
68
69                 shared_ptr<Property> prop;
70
71                 if (opt->values)
72                         prop = bind_enum(name, opt, get, set);
73                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
74                         prop = shared_ptr<Property>(new Double(name, 2, "",
75                                 none, none, get, set));
76                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
77                         prop = shared_ptr<Property>(
78                                 new Int(name, "", none, get, set));
79                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
80                         prop = shared_ptr<Property>(
81                                 new String(name, get, set));
82                 else
83                         continue;
84
85                 _properties.push_back(prop);
86         }
87 }
88
89 shared_ptr<Property> DecoderOptions::bind_enum(
90         const QString &name, const srd_decoder_option *option,
91         Property::Getter getter, Property::Setter setter)
92 {
93         vector< pair<GVariant*, QString> > values;
94         for (GSList *l = option->values; l; l = l->next) {
95                 GVariant *const var = (GVariant*)l->data;
96                 assert(var);
97                 values.push_back(make_pair(var, print_gvariant(var)));
98         }
99
100         return shared_ptr<Property>(new Enum(name, values, getter, setter));
101 }
102
103 GVariant* DecoderOptions::getter(const char *id)
104 {
105         GVariant *val = NULL;
106
107         assert(_decoder);
108
109         // Get the value from the hash table if it is already present
110         const map<string, GVariant*>& options = _decoder->options();
111         const auto iter = options.find(id);
112
113         if (iter != options.end())
114                 val = (*iter).second;
115         else
116         {
117                 assert(_decoder->decoder());
118
119                 // Get the default value if not
120                 for (GSList *l = _decoder->decoder()->options; l; l = l->next)
121                 {
122                         const srd_decoder_option *const opt =
123                                 (srd_decoder_option*)l->data;
124                         if (strcmp(opt->id, id) == 0) {
125                                 val = opt->def;
126                                 break;
127                         }
128                 }
129         }
130
131         if (val)
132                 g_variant_ref(val);
133
134         return val;
135 }
136
137 void DecoderOptions::setter(const char *id, GVariant *value)
138 {
139         assert(_decoder);
140         _decoder->set_option(id, value);
141
142         assert(_decoder_stack);
143         _decoder_stack->begin_decode();
144 }
145
146 } // binding
147 } // prop
148 } // pv