2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
24 #include <boost/none_t.hpp>
26 #include <libsigrokcxx/libsigrokcxx.hpp>
28 #include <pv/prop/bool.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>
34 #include "inputoutput.hpp"
40 using std::shared_ptr
;
44 using Glib::VariantBase
;
45 using Glib::VariantType
;
50 using pv::prop::Double
;
53 using pv::prop::Property
;
54 using pv::prop::String
;
59 InputOutput::InputOutput(
60 const map
<string
, shared_ptr
<Option
>> &options
)
62 for (const pair
<const string
, shared_ptr
<Option
>>& o
: options
) {
63 const shared_ptr
<Option
> &opt
= o
.second
;
66 const QString name
= QString::fromStdString(opt
->name());
67 const QString desc
= QString::fromStdString(opt
->description());
69 const VariantBase def_val
= opt
->default_value();
70 const vector
<VariantBase
> values
= opt
->values();
72 options_
[opt
->id()] = def_val
;
74 const Property::Getter get
= [&, opt
]() {
75 return options_
[opt
->id()]; };
76 const Property::Setter set
= [&, opt
](VariantBase value
) {
77 options_
[opt
->id()] = value
; };
79 shared_ptr
<Property
> prop
;
81 if (!opt
->values().empty())
82 prop
= bind_enum(name
, desc
, values
, get
, set
);
83 else if (def_val
.is_of_type(VariantType("b")))
84 prop
= shared_ptr
<Property
>(new Bool(name
, desc
, get
, set
));
85 else if (def_val
.is_of_type(VariantType("d")))
86 prop
= shared_ptr
<Property
>(new Double(name
, desc
, 2, "",
87 none
, none
, get
, set
));
88 else if (def_val
.is_of_type(VariantType("i")) ||
89 def_val
.is_of_type(VariantType("t")) ||
90 def_val
.is_of_type(VariantType("u")))
91 prop
= shared_ptr
<Property
>(
92 new Int(name
, desc
, "", none
, get
, set
));
93 else if (def_val
.is_of_type(VariantType("s")))
94 prop
= shared_ptr
<Property
>(
95 new String(name
, desc
, get
, set
));
99 properties_
.push_back(prop
);
103 const map
<string
, VariantBase
>& InputOutput::options() const
108 shared_ptr
<Property
> InputOutput::bind_enum(
109 const QString
&name
, const QString
&desc
, const vector
<VariantBase
> &values
,
110 Property::Getter getter
, Property::Setter setter
)
112 vector
< pair
<VariantBase
, QString
> > enum_vals
;
113 for (VariantBase var
: values
)
114 enum_vals
.emplace_back(var
, print_gvariant(var
));
115 return shared_ptr
<Property
>(new Enum(name
, desc
, enum_vals
, getter
, setter
));
118 } // namespace binding