PulseView  unreleased development snapshot
A Qt-based sigrok GUI
decoder.cpp
Go to the documentation of this file.
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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <libsigrokdecode/libsigrokdecode.h>
21 
22 #include "decoder.hpp"
23 
24 #include <boost/none_t.hpp>
25 
27 #include <pv/data/decodesignal.hpp>
28 #include <pv/prop/double.hpp>
29 #include <pv/prop/enum.hpp>
30 #include <pv/prop/int.hpp>
31 #include <pv/prop/string.hpp>
32 
33 using boost::none;
34 using std::map;
35 using std::pair;
36 using std::shared_ptr;
37 using std::string;
38 using std::vector;
39 
40 using pv::prop::Double;
41 using pv::prop::Enum;
42 using pv::prop::Int;
43 using pv::prop::Property;
44 using pv::prop::String;
45 
46 namespace pv {
47 namespace binding {
48 
50  shared_ptr<pv::data::DecodeSignal> decode_signal,
51  shared_ptr<data::decode::Decoder> decoder) :
52  decode_signal_(decode_signal),
53  decoder_(decoder)
54 {
55  assert(decoder_);
56 
57  const srd_decoder *const dec = decoder_->get_srd_decoder();
58  assert(dec);
59 
60  for (GSList *l = dec->options; l; l = l->next) {
61  const srd_decoder_option *const opt = (srd_decoder_option*)l->data;
62 
63  const QString name = QString::fromUtf8(opt->desc);
64 
65  const Property::Getter get = [&, opt]() {
66  return getter(opt->id); };
67  const Property::Setter set = [&, opt](Glib::VariantBase value) {
68  setter(opt->id, value); };
69 
70  shared_ptr<Property> prop;
71 
72  if (opt->values)
73  prop = bind_enum(name, "", opt, get, set);
74  else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("d")))
75  prop = shared_ptr<Property>(new Double(name, "", 2, "",
76  none, none, get, set));
77  else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x")))
78  prop = shared_ptr<Property>(
79  new Int(name, "", "", none, get, set));
80  else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s")))
81  prop = shared_ptr<Property>(
82  new String(name, "", get, set));
83  else
84  continue;
85 
86  properties_.push_back(prop);
87  }
88 }
89 
90 shared_ptr<Property> Decoder::bind_enum(
91  const QString &name, const QString &desc,
92  const srd_decoder_option *option,
93  Property::Getter getter, Property::Setter setter)
94 {
95  vector< pair<Glib::VariantBase, QString> > values;
96  for (GSList *l = option->values; l; l = l->next) {
97  Glib::VariantBase var = Glib::VariantBase((GVariant*)l->data, true);
98  values.emplace_back(var, print_gvariant(var));
99  }
100 
101  return shared_ptr<Property>(new Enum(name, desc, values, getter, setter));
102 }
103 
104 Glib::VariantBase Decoder::getter(const char *id)
105 {
106  GVariant *val = nullptr;
107 
108  assert(decoder_);
109 
110  // Get the value from the hash table if it is already present
111  const map<string, GVariant*>& options = decoder_->options();
112  const auto iter = options.find(id);
113 
114  if (iter != options.end())
115  val = (*iter).second;
116  else {
117  assert(decoder_->get_srd_decoder());
118 
119  // Get the default value if not
120  for (GSList *l = decoder_->get_srd_decoder()->options; l; l = l->next) {
121  const srd_decoder_option *const opt = (srd_decoder_option*)l->data;
122  if (strcmp(opt->id, id) == 0) {
123  val = opt->def;
124  break;
125  }
126  }
127  }
128 
129  return (val) ? Glib::VariantBase(val, true) : Glib::VariantBase();
130 }
131 
132 void Decoder::setter(const char *id, Glib::VariantBase value)
133 {
134  assert(decoder_);
135  decoder_->set_option(id, value.gobj());
136 
137  assert(decode_signal_);
138  decode_signal_->begin_decode();
139 }
140 
141 } // namespace binding
142 } // namespace pv
manual pdf if(NOT EXISTS"${CMAKE_CURRENT_BINARY_DIR}/images") message(STATUS"creating symlink for manual's images/ subdirectory") execute_process(COMMAND $
Definition: CMakeLists.txt:42
Glib::VariantBase getter(const char *id)
Definition: decoder.cpp:104
CMake option(DISABLE_WERROR"Build without -Werror"TRUE) option(ENABLE_SIGNALS"Build with UNIX signals"TRUE) option(ENABLE_STACKTRACE"Enable stack trace when crashing"FALSE) option(ENABLE_DECODE"Build with libsigrokdecode"TRUE) option(ENABLE_FLOW"Build with libsigrokflow"FALSE) option(ENABLE_TESTS"Enable unit tests"FALSE) option(STATIC_PKGDEPS_LIBS"Statically link to (pkg-config) libraries"FALSE) option(ENABLE_TS_UPDATE"Update .ts source files (Qt l10n)"FALSE) if(WIN32) set(STATIC_PKGDEPS_LIBS TRUE) set(ENABLE_SIGNALS FALSE) endif() if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING"Choose the type of build (None
T value(details::expression_node< T > *n)
Definition: exprtk.hpp:12358
static shared_ptr< prop::Property > bind_enum(const QString &name, const QString &desc, const srd_decoder_option *option, prop::Property::Getter getter, prop::Property::Setter setter)
Definition: decoder.cpp:90
Mac OS X or Android For some we provide binary for others we provide installers and for others we provide AppImage containers that you can run without the need to install anything Check the the usual way to install PulseView is to install the packages provided by your distro s package manager sometimes only outdated packages are made available to you In that you have two options
Definition: installation.txt:2
Decoder(shared_ptr< pv::data::DecodeSignal > decode_signal, shared_ptr< pv::data::decode::Decoder > decoder)
Definition: decoder.cpp:49
vector< shared_ptr< prop::Property > > properties_
Definition: binding.hpp:73
shared_ptr< pv::data::decode::Decoder > decoder_
Definition: decoder.hpp:59
void setter(const char *id, Glib::VariantBase value)
Definition: decoder.cpp:132
shared_ptr< pv::data::DecodeSignal > decode_signal_
Definition: decoder.hpp:58
static QString print_gvariant(Glib::VariantBase gvar)
Definition: binding.cpp:119