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;
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, (int64_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)));
147 GVariant *new_value = NULL;
148 const GVariantType *const type = g_variant_get_type(_value);
151 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
152 new_value = g_variant_new_byte(_spin_box->value());
153 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
154 new_value = g_variant_new_int16(_spin_box->value());
155 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
156 new_value = g_variant_new_uint16(_spin_box->value());
157 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
158 new_value = g_variant_new_int32(_spin_box->value());
159 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
160 new_value = g_variant_new_int32(_spin_box->value());
161 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
162 new_value = g_variant_new_int64(_spin_box->value());
163 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
164 new_value = g_variant_new_uint64(_spin_box->value());
167 // Unexpected value type.
173 g_variant_unref(_value);
174 g_variant_ref(new_value);
180 void Int::on_value_changed(int)