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