MainWindow: Enable dock nesting
[pulseview.git] / pv / binding / decoder.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 "decoder.hpp"
24
25 #include <boost/none_t.hpp>
26
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>
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 using pv::prop::Double;
43 using pv::prop::Enum;
44 using pv::prop::Int;
45 using pv::prop::Property;
46 using pv::prop::String;
47
48 namespace pv {
49 namespace binding {
50
51 Decoder::Decoder(
52         shared_ptr<pv::data::DecoderStack> decoder_stack,
53         shared_ptr<data::decode::Decoder> decoder) :
54         decoder_stack_(decoder_stack),
55         decoder_(decoder)
56 {
57         assert(decoder_);
58
59         const srd_decoder *const dec = decoder_->decoder();
60         assert(dec);
61
62         for (GSList *l = dec->options; l; l = l->next) {
63                 const srd_decoder_option *const opt =
64                         (srd_decoder_option*)l->data;
65
66                 const QString name = QString::fromUtf8(opt->desc);
67
68                 const Property::Getter get = [&, opt]() {
69                         return getter(opt->id); };
70                 const Property::Setter set = [&, opt](Glib::VariantBase value) {
71                         setter(opt->id, value); };
72
73                 shared_ptr<Property> prop;
74
75                 if (opt->values)
76                         prop = bind_enum(name, opt, get, set);
77                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
78                         prop = shared_ptr<Property>(new Double(name, 2, "",
79                                 none, none, get, set));
80                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
81                         prop = shared_ptr<Property>(
82                                 new Int(name, "", none, get, set));
83                 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
84                         prop = shared_ptr<Property>(
85                                 new String(name, get, set));
86                 else
87                         continue;
88
89                 properties_.push_back(prop);
90         }
91 }
92
93 shared_ptr<Property> Decoder::bind_enum(
94         const QString &name, const srd_decoder_option *option,
95         Property::Getter getter, Property::Setter setter)
96 {
97         vector< pair<Glib::VariantBase, QString> > values;
98         for (GSList *l = option->values; l; l = l->next) {
99                 Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
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 Glib::VariantBase Decoder::getter(const char *id)
107 {
108         GVariant *val = nullptr;
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         const auto iter = options.find(id);
115
116         if (iter != options.end())
117                 val = (*iter).second;
118         else {
119                 assert(decoder_->decoder());
120
121                 // Get the default value if not
122                 for (GSList *l = decoder_->decoder()->options; l; l = l->next) {
123                         const srd_decoder_option *const opt =
124                                 (srd_decoder_option*)l->data;
125                         if (strcmp(opt->id, id) == 0) {
126                                 val = opt->def;
127                                 break;
128                         }
129                 }
130         }
131
132         if (val)
133                 return Glib::VariantBase(val, true);
134         else
135                 return Glib::VariantBase();
136 }
137
138 void Decoder::setter(const char *id, Glib::VariantBase value)
139 {
140         assert(decoder_);
141         decoder_->set_option(id, value.gobj());
142
143         assert(decoder_stack_);
144         decoder_stack_->begin_decode();
145 }
146
147 } // binding
148 } // pv