]> sigrok.org Git - pulseview.git/blame - pv/binding/decoder.cpp
Fix #1222 by adding a tooltip for when there isn't enough space
[pulseview.git] / pv / binding / decoder.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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21ad818f
JH
18 */
19
4e5a4405
JH
20#include <libsigrokdecode/libsigrokdecode.h>
21
3cc9ad7b 22#include "decoder.hpp"
21ad818f 23
67fe5e9c
JH
24#include <boost/none_t.hpp>
25
2acdb232 26#include <pv/data/decode/decoder.hpp>
946b52e1 27#include <pv/data/decodesignal.hpp>
2acdb232
JH
28#include <pv/prop/double.hpp>
29#include <pv/prop/enum.hpp>
30#include <pv/prop/int.hpp>
31#include <pv/prop/string.hpp>
67fe5e9c 32
819f4c25 33using boost::none;
819f4c25 34using std::map;
182d2f5d 35using std::pair;
f9abf97e 36using std::shared_ptr;
819f4c25 37using std::string;
182d2f5d 38using std::vector;
67fe5e9c 39
61703a01
JH
40using pv::prop::Double;
41using pv::prop::Enum;
42using pv::prop::Int;
43using pv::prop::Property;
44using pv::prop::String;
45
21ad818f 46namespace pv {
21ad818f
JH
47namespace binding {
48
3cc9ad7b 49Decoder::Decoder(
946b52e1 50 shared_ptr<pv::data::DecodeSignal> decode_signal,
7491a29f 51 shared_ptr<data::decode::Decoder> decoder) :
946b52e1 52 decode_signal_(decode_signal),
8dbbc7f0 53 decoder_(decoder)
67fe5e9c 54{
8dbbc7f0 55 assert(decoder_);
67fe5e9c 56
8dbbc7f0 57 const srd_decoder *const dec = decoder_->decoder();
4e5a4405 58 assert(dec);
67fe5e9c 59
2ad82c2e 60 for (GSList *l = dec->options; l; l = l->next) {
67fe5e9c
JH
61 const srd_decoder_option *const opt =
62 (srd_decoder_option*)l->data;
63
636782c1 64 const QString name = QString::fromUtf8(opt->desc);
67fe5e9c 65
6db73158
JH
66 const Property::Getter get = [&, opt]() {
67 return getter(opt->id); };
e8d00928 68 const Property::Setter set = [&, opt](Glib::VariantBase value) {
6db73158 69 setter(opt->id, value); };
67fe5e9c
JH
70
71 shared_ptr<Property> prop;
72
182d2f5d 73 if (opt->values)
9a267f8d 74 prop = bind_enum(name, "", opt, get, set);
a28c3025 75 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
9a267f8d 76 prop = shared_ptr<Property>(new Double(name, "", 2, "",
6db73158 77 none, none, get, set));
182d2f5d 78 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
67fe5e9c 79 prop = shared_ptr<Property>(
9a267f8d 80 new Int(name, "", "", none, get, set));
67fe5e9c
JH
81 else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
82 prop = shared_ptr<Property>(
9a267f8d 83 new String(name, "", get, set));
67fe5e9c
JH
84 else
85 continue;
86
8dbbc7f0 87 properties_.push_back(prop);
67fe5e9c
JH
88 }
89}
90
3cc9ad7b 91shared_ptr<Property> Decoder::bind_enum(
9a267f8d
UH
92 const QString &name, const QString &desc,
93 const srd_decoder_option *option,
182d2f5d
JH
94 Property::Getter getter, Property::Setter setter)
95{
e8d00928 96 vector< pair<Glib::VariantBase, QString> > values;
182d2f5d 97 for (GSList *l = option->values; l; l = l->next) {
e8d00928 98 Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
20f59e95 99 values.emplace_back(var, print_gvariant(var));
182d2f5d
JH
100 }
101
9a267f8d 102 return shared_ptr<Property>(new Enum(name, desc, values, getter, setter));
182d2f5d
JH
103}
104
3cc9ad7b 105Glib::VariantBase Decoder::getter(const char *id)
67fe5e9c 106{
4c60462b 107 GVariant *val = nullptr;
615f6d25 108
8dbbc7f0 109 assert(decoder_);
4e5a4405 110
67fe5e9c 111 // Get the value from the hash table if it is already present
8dbbc7f0 112 const map<string, GVariant*>& options = decoder_->options();
f46e495e 113 const auto iter = options.find(id);
67fe5e9c 114
615f6d25
JH
115 if (iter != options.end())
116 val = (*iter).second;
2ad82c2e 117 else {
8dbbc7f0 118 assert(decoder_->decoder());
4e5a4405 119
67fe5e9c 120 // Get the default value if not
2ad82c2e 121 for (GSList *l = decoder_->decoder()->options; l; l = l->next) {
67fe5e9c
JH
122 const srd_decoder_option *const opt =
123 (srd_decoder_option*)l->data;
1429b21d 124 if (strcmp(opt->id, id) == 0) {
67fe5e9c 125 val = opt->def;
1429b21d
BV
126 break;
127 }
67fe5e9c
JH
128 }
129 }
130
c063290a 131 return (val) ? Glib::VariantBase(val, true) : Glib::VariantBase();
67fe5e9c
JH
132}
133
3cc9ad7b 134void Decoder::setter(const char *id, Glib::VariantBase value)
21ad818f 135{
8dbbc7f0
JH
136 assert(decoder_);
137 decoder_->set_option(id, value.gobj());
7491a29f 138
946b52e1
SA
139 assert(decode_signal_);
140 decode_signal_->begin_decode();
21ad818f
JH
141}
142
870ea3db
UH
143} // namespace binding
144} // namespace pv