]>
Commit | Line | Data |
---|---|---|
0c913637 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2015 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/>. |
0c913637 JH |
18 | */ |
19 | ||
20 | #include <cassert> | |
21 | #include <iostream> | |
22 | #include <utility> | |
23 | ||
24 | #include <boost/none_t.hpp> | |
25 | ||
26 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
27 | ||
5d2aa2c7 | 28 | #include <pv/prop/bool.hpp> |
0c913637 JH |
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 | #include "inputoutput.hpp" | |
35 | ||
36 | using boost::none; | |
37 | ||
0c913637 JH |
38 | using std::map; |
39 | using std::pair; | |
40 | using std::shared_ptr; | |
41 | using std::string; | |
42 | using std::vector; | |
43 | ||
44 | using Glib::VariantBase; | |
45 | using Glib::VariantType; | |
46 | ||
47 | using sigrok::Option; | |
48 | ||
5d2aa2c7 | 49 | using pv::prop::Bool; |
0c913637 JH |
50 | using pv::prop::Double; |
51 | using pv::prop::Enum; | |
52 | using pv::prop::Int; | |
53 | using pv::prop::Property; | |
54 | using pv::prop::String; | |
55 | ||
56 | namespace pv { | |
57 | namespace binding { | |
58 | ||
59 | InputOutput::InputOutput( | |
60 | const map<string, shared_ptr<Option>> &options) | |
61 | { | |
2ad82c2e | 62 | for (pair<string, shared_ptr<Option>> o : options) { |
0c913637 JH |
63 | const shared_ptr<Option> &opt = o.second; |
64 | assert(opt); | |
65 | ||
66 | const QString name = QString::fromStdString(opt->name()); | |
9a267f8d | 67 | const QString desc = QString::fromStdString(opt->description()); |
0c913637 JH |
68 | const VariantBase def_val = opt->default_value(); |
69 | const vector<VariantBase> values = opt->values(); | |
70 | ||
71 | options_[opt->id()] = def_val; | |
bb19aac4 | 72 | |
0c913637 JH |
73 | const Property::Getter get = [&, opt]() { |
74 | return options_[opt->id()]; }; | |
75 | const Property::Setter set = [&, opt](VariantBase value) { | |
76 | options_[opt->id()] = value; }; | |
77 | ||
78 | shared_ptr<Property> prop; | |
79 | ||
80 | if (!opt->values().empty()) | |
9a267f8d | 81 | prop = bind_enum(name, desc, values, get, set); |
5d2aa2c7 | 82 | else if (def_val.is_of_type(VariantType("b"))) |
9a267f8d | 83 | prop = shared_ptr<Property>(new Bool(name, desc, get, set)); |
0c913637 | 84 | else if (def_val.is_of_type(VariantType("d"))) |
9a267f8d | 85 | prop = shared_ptr<Property>(new Double(name, desc, 2, "", |
ec6cc07f | 86 | none, none, get, set)); |
5d2aa2c7 JH |
87 | else if (def_val.is_of_type(VariantType("i")) || |
88 | def_val.is_of_type(VariantType("t")) || | |
89 | def_val.is_of_type(VariantType("u"))) | |
0c913637 | 90 | prop = shared_ptr<Property>( |
9a267f8d | 91 | new Int(name, desc, "", none, get, set)); |
0c913637 JH |
92 | else if (def_val.is_of_type(VariantType("s"))) |
93 | prop = shared_ptr<Property>( | |
9a267f8d | 94 | new String(name, desc, get, set)); |
0c913637 JH |
95 | else |
96 | continue; | |
97 | ||
98 | properties_.push_back(prop); | |
99 | } | |
100 | } | |
101 | ||
102 | const map<string, VariantBase>& InputOutput::options() const | |
103 | { | |
104 | return options_; | |
105 | } | |
106 | ||
107 | shared_ptr<Property> InputOutput::bind_enum( | |
9a267f8d | 108 | const QString &name, const QString &desc, const vector<VariantBase> &values, |
0c913637 JH |
109 | Property::Getter getter, Property::Setter setter) |
110 | { | |
111 | vector< pair<VariantBase, QString> > enum_vals; | |
112 | for (VariantBase var : values) | |
20f59e95 | 113 | enum_vals.emplace_back(var, print_gvariant(var)); |
9a267f8d | 114 | return shared_ptr<Property>(new Enum(name, desc, enum_vals, getter, setter)); |
0c913637 JH |
115 | } |
116 | ||
117 | } // namespace binding | |
118 | } // namespace pv |