]>
Commit | Line | Data |
---|---|---|
a2b92157 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/>. |
a2b92157 JH |
18 | */ |
19 | ||
eb8269e3 | 20 | #include <cassert> |
aca9aa83 | 21 | #include <cstdint> |
a2b92157 | 22 | |
46ebcd3f | 23 | #include <QDebug> |
a2b92157 JH |
24 | #include <QSpinBox> |
25 | ||
46ebcd3f SA |
26 | #include <libsigrokcxx/libsigrokcxx.hpp> |
27 | ||
2acdb232 | 28 | #include "int.hpp" |
a2b92157 | 29 | |
819f4c25 JH |
30 | using boost::optional; |
31 | using std::max; | |
32 | using std::min; | |
33 | using std::pair; | |
a2b92157 JH |
34 | |
35 | namespace pv { | |
36 | namespace prop { | |
37 | ||
38 | Int::Int(QString name, | |
9a267f8d | 39 | QString desc, |
a2b92157 JH |
40 | QString suffix, |
41 | optional< pair<int64_t, int64_t> > range, | |
42 | Getter getter, | |
43 | Setter setter) : | |
9a267f8d | 44 | Property(name, desc, getter, setter), |
8dbbc7f0 JH |
45 | suffix_(suffix), |
46 | range_(range), | |
4c60462b | 47 | spin_box_(nullptr) |
a2b92157 JH |
48 | { |
49 | } | |
50 | ||
b1fe148e | 51 | QWidget* Int::get_widget(QWidget *parent, bool auto_commit) |
a2b92157 | 52 | { |
dbed5609 | 53 | int64_t range_min = 0; |
765e7d33 | 54 | uint64_t range_max = 0; |
ac223c1e | 55 | |
8dbbc7f0 JH |
56 | if (spin_box_) |
57 | return spin_box_; | |
a2b92157 | 58 | |
8dbbc7f0 | 59 | if (!getter_) |
4c60462b | 60 | return nullptr; |
e8d00928 | 61 | |
46ebcd3f SA |
62 | try { |
63 | value_ = getter_(); | |
64 | } catch (const sigrok::Error &e) { | |
65 | qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); | |
66 | return nullptr; | |
67 | } | |
ac223c1e | 68 | |
8dbbc7f0 | 69 | GVariant *value = value_.gobj(); |
e8d00928 | 70 | if (!value) |
4c60462b | 71 | return nullptr; |
ac223c1e | 72 | |
8dbbc7f0 JH |
73 | spin_box_ = new QSpinBox(parent); |
74 | spin_box_->setSuffix(suffix_); | |
ac223c1e | 75 | |
e8d00928 | 76 | const GVariantType *const type = g_variant_get_type(value); |
ac223c1e JH |
77 | assert(type); |
78 | ||
2ad82c2e | 79 | if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) { |
ac223c1e | 80 | range_min = 0, range_max = UINT8_MAX; |
2ad82c2e | 81 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) { |
ac223c1e | 82 | range_min = INT16_MIN, range_max = INT16_MAX; |
2ad82c2e | 83 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) { |
ac223c1e | 84 | range_min = 0, range_max = UINT16_MAX; |
2ad82c2e | 85 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) { |
ac223c1e | 86 | range_min = INT32_MIN, range_max = INT32_MAX; |
2ad82c2e | 87 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) { |
ac223c1e | 88 | range_min = 0, range_max = UINT32_MAX; |
2ad82c2e | 89 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) { |
ac223c1e | 90 | range_min = INT64_MIN, range_max = INT64_MAX; |
2ad82c2e | 91 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) { |
ac223c1e | 92 | range_min = 0, range_max = UINT64_MAX; |
2ad82c2e | 93 | } else { |
ac223c1e | 94 | // Unexpected value type. |
0402d7a3 | 95 | assert(false); |
ac223c1e JH |
96 | } |
97 | ||
b0e15aa2 | 98 | // @todo sigrok supports 64-bit quantities, but Qt does not have a |
ac223c1e JH |
99 | // standard widget to allow the values to be modified over the full |
100 | // 64-bit range on 32-bit machines. To solve the issue we need a | |
101 | // custom widget. | |
102 | ||
103 | range_min = max(range_min, (int64_t)INT_MIN); | |
765e7d33 | 104 | range_max = min(range_max, (uint64_t)INT_MAX); |
ac223c1e | 105 | |
8dbbc7f0 JH |
106 | if (range_) |
107 | spin_box_->setRange((int)range_->first, (int)range_->second); | |
a915c6a3 | 108 | else |
8dbbc7f0 | 109 | spin_box_->setRange((int)range_min, (int)range_max); |
a2b92157 | 110 | |
dbed5609 | 111 | update_widget(); |
a2b92157 | 112 | |
b1fe148e | 113 | if (auto_commit) |
8dbbc7f0 | 114 | connect(spin_box_, SIGNAL(valueChanged(int)), |
b1fe148e JH |
115 | this, SLOT(on_value_changed(int))); |
116 | ||
8dbbc7f0 | 117 | return spin_box_; |
a2b92157 JH |
118 | } |
119 | ||
dbed5609 SA |
120 | void Int::update_widget() |
121 | { | |
122 | if (!spin_box_) | |
123 | return; | |
124 | ||
46ebcd3f SA |
125 | try { |
126 | value_ = getter_(); | |
127 | } catch (const sigrok::Error &e) { | |
128 | qWarning() << tr("Querying config key %1 resulted in %2").arg(name_, e.what()); | |
129 | return; | |
130 | } | |
131 | ||
dbed5609 SA |
132 | GVariant *value = value_.gobj(); |
133 | assert(value); | |
134 | ||
135 | const GVariantType *const type = g_variant_get_type(value); | |
136 | assert(type); | |
137 | ||
138 | int64_t int_val = 0; | |
139 | ||
140 | if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) { | |
141 | int_val = g_variant_get_byte(value); | |
142 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) { | |
143 | int_val = g_variant_get_int16(value); | |
144 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) { | |
145 | int_val = g_variant_get_uint16(value); | |
146 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) { | |
147 | int_val = g_variant_get_int32(value); | |
148 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) { | |
149 | int_val = g_variant_get_uint32(value); | |
150 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) { | |
151 | int_val = g_variant_get_int64(value); | |
152 | } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) { | |
153 | int_val = g_variant_get_uint64(value); | |
154 | } else { | |
155 | // Unexpected value type. | |
156 | assert(false); | |
157 | } | |
158 | ||
159 | spin_box_->setValue((int)int_val); | |
160 | } | |
161 | ||
a2b92157 JH |
162 | void Int::commit() |
163 | { | |
8dbbc7f0 | 164 | assert(setter_); |
a2b92157 | 165 | |
8dbbc7f0 | 166 | if (!spin_box_) |
a2b92157 JH |
167 | return; |
168 | ||
4c60462b | 169 | GVariant *new_value = nullptr; |
8dbbc7f0 | 170 | const GVariantType *const type = g_variant_get_type(value_.gobj()); |
ac223c1e JH |
171 | assert(type); |
172 | ||
173 | if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) | |
8dbbc7f0 | 174 | new_value = g_variant_new_byte(spin_box_->value()); |
ac223c1e | 175 | else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) |
8dbbc7f0 | 176 | new_value = g_variant_new_int16(spin_box_->value()); |
ac223c1e | 177 | else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) |
8dbbc7f0 | 178 | new_value = g_variant_new_uint16(spin_box_->value()); |
ac223c1e | 179 | else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) |
8dbbc7f0 | 180 | new_value = g_variant_new_int32(spin_box_->value()); |
ac223c1e | 181 | else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) |
79118fe5 | 182 | new_value = g_variant_new_uint32(spin_box_->value()); |
ac223c1e | 183 | else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) |
8dbbc7f0 | 184 | new_value = g_variant_new_int64(spin_box_->value()); |
ac223c1e | 185 | else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) |
8dbbc7f0 | 186 | new_value = g_variant_new_uint64(spin_box_->value()); |
c063290a | 187 | else { |
ac223c1e | 188 | // Unexpected value type. |
0402d7a3 | 189 | assert(false); |
ac223c1e JH |
190 | } |
191 | ||
192 | assert(new_value); | |
193 | ||
8dbbc7f0 | 194 | value_ = Glib::VariantBase(new_value); |
ac223c1e | 195 | |
8dbbc7f0 | 196 | setter_(value_); |
a2b92157 JH |
197 | } |
198 | ||
b1fe148e JH |
199 | void Int::on_value_changed(int) |
200 | { | |
201 | commit(); | |
202 | } | |
203 | ||
870ea3db UH |
204 | } // namespace prop |
205 | } // namespace pv |