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 g_variant_unref(_value);
55 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
57 int64_t int_val = 0, range_min = 0, range_max = 0;
63 g_variant_unref(_value);
65 _value = _getter ? _getter() : NULL;
70 _spin_box = new QSpinBox(parent);
71 _spin_box->setSuffix(_suffix);
73 const GVariantType *const type = g_variant_get_type(_value);
76 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
78 int_val = g_variant_get_byte(_value);
79 range_min = 0, range_max = UINT8_MAX;
81 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
83 int_val = g_variant_get_int16(_value);
84 range_min = INT16_MIN, range_max = INT16_MAX;
86 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
88 int_val = g_variant_get_uint16(_value);
89 range_min = 0, range_max = UINT16_MAX;
91 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
93 int_val = g_variant_get_int32(_value);
94 range_min = INT32_MIN, range_max = INT32_MAX;
96 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
98 int_val = g_variant_get_uint32(_value);
99 range_min = 0, range_max = UINT32_MAX;
101 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
103 int_val = g_variant_get_int64(_value);
104 range_min = INT64_MIN, range_max = INT64_MAX;
106 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
108 int_val = g_variant_get_uint64(_value);
109 range_min = 0, range_max = UINT64_MAX;
113 // Unexpected value type.
117 // @todo Sigrok supports 64-bit quantities, but Qt does not have a
118 // standard widget to allow the values to be modified over the full
119 // 64-bit range on 32-bit machines. To solve the issue we need a
122 range_min = max(range_min, (int64_t)INT_MIN);
123 range_max = min(range_max, (int64_t)INT_MAX);
126 _spin_box->setRange((int)_range->first, (int)_range->second);
128 _spin_box->setRange((int)range_min, (int)range_max);
130 _spin_box->setValue((int)int_val);
133 connect(_spin_box, SIGNAL(valueChanged(int)),
134 this, SLOT(on_value_changed(int)));
148 GVariant *new_value = NULL;
149 const GVariantType *const type = g_variant_get_type(_value);
152 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
153 new_value = g_variant_new_byte(_spin_box->value());
154 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
155 new_value = g_variant_new_int16(_spin_box->value());
156 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
157 new_value = g_variant_new_uint16(_spin_box->value());
158 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
159 new_value = g_variant_new_int32(_spin_box->value());
160 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
161 new_value = g_variant_new_int32(_spin_box->value());
162 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
163 new_value = g_variant_new_int64(_spin_box->value());
164 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
165 new_value = g_variant_new_uint64(_spin_box->value());
168 // Unexpected value type.
174 g_variant_unref(_value);
175 g_variant_ref(new_value);
181 void Int::on_value_changed(int)