2 * This file is part of the PulseView project.
4 * Copyright (C) 2013 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 using boost::optional;
36 Int::Int(QString name,
38 optional< pair<int64_t, int64_t> > range,
41 Property(name, getter, setter),
52 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
54 int64_t int_val = 0, range_min = 0;
55 uint64_t range_max = 0;
65 GVariant *value = value_.gobj();
69 spin_box_ = new QSpinBox(parent);
70 spin_box_->setSuffix(suffix_);
72 const GVariantType *const type = g_variant_get_type(value);
75 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
77 int_val = g_variant_get_byte(value);
78 range_min = 0, range_max = UINT8_MAX;
80 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
82 int_val = g_variant_get_int16(value);
83 range_min = INT16_MIN, range_max = INT16_MAX;
85 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
87 int_val = g_variant_get_uint16(value);
88 range_min = 0, range_max = UINT16_MAX;
90 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
92 int_val = g_variant_get_int32(value);
93 range_min = INT32_MIN, range_max = INT32_MAX;
95 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
97 int_val = g_variant_get_uint32(value);
98 range_min = 0, range_max = UINT32_MAX;
100 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
102 int_val = g_variant_get_int64(value);
103 range_min = INT64_MIN, range_max = INT64_MAX;
105 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
107 int_val = g_variant_get_uint64(value);
108 range_min = 0, range_max = UINT64_MAX;
112 // Unexpected value type.
116 // @todo Sigrok supports 64-bit quantities, but Qt does not have a
117 // standard widget to allow the values to be modified over the full
118 // 64-bit range on 32-bit machines. To solve the issue we need a
121 range_min = max(range_min, (int64_t)INT_MIN);
122 range_max = min(range_max, (uint64_t)INT_MAX);
125 spin_box_->setRange((int)range_->first, (int)range_->second);
127 spin_box_->setRange((int)range_min, (int)range_max);
129 spin_box_->setValue((int)int_val);
132 connect(spin_box_, SIGNAL(valueChanged(int)),
133 this, SLOT(on_value_changed(int)));
145 GVariant *new_value = nullptr;
146 const GVariantType *const type = g_variant_get_type(value_.gobj());
149 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
150 new_value = g_variant_new_byte(spin_box_->value());
151 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
152 new_value = g_variant_new_int16(spin_box_->value());
153 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
154 new_value = g_variant_new_uint16(spin_box_->value());
155 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
156 new_value = g_variant_new_int32(spin_box_->value());
157 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
158 new_value = g_variant_new_uint32(spin_box_->value());
159 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
160 new_value = g_variant_new_int64(spin_box_->value());
161 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
162 new_value = g_variant_new_uint64(spin_box_->value());
165 // Unexpected value type.
171 value_ = Glib::VariantBase(new_value);
176 void Int::on_value_changed(int)