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