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, range_max = 0;
64 GVariant *value = value_.gobj();
68 spin_box_ = new QSpinBox(parent);
69 spin_box_->setSuffix(suffix_);
71 const GVariantType *const type = g_variant_get_type(value);
74 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
76 int_val = g_variant_get_byte(value);
77 range_min = 0, range_max = UINT8_MAX;
79 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
81 int_val = g_variant_get_int16(value);
82 range_min = INT16_MIN, range_max = INT16_MAX;
84 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
86 int_val = g_variant_get_uint16(value);
87 range_min = 0, range_max = UINT16_MAX;
89 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
91 int_val = g_variant_get_int32(value);
92 range_min = INT32_MIN, range_max = INT32_MAX;
94 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
96 int_val = g_variant_get_uint32(value);
97 range_min = 0, range_max = UINT32_MAX;
99 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
101 int_val = g_variant_get_int64(value);
102 range_min = INT64_MIN, range_max = INT64_MAX;
104 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
106 int_val = g_variant_get_uint64(value);
107 range_min = 0, range_max = UINT64_MAX;
111 // Unexpected value type.
115 // @todo Sigrok supports 64-bit quantities, but Qt does not have a
116 // standard widget to allow the values to be modified over the full
117 // 64-bit range on 32-bit machines. To solve the issue we need a
120 range_min = max(range_min, (int64_t)INT_MIN);
121 range_max = min(range_max, (int64_t)INT_MAX);
124 spin_box_->setRange((int)range_->first, (int)range_->second);
126 spin_box_->setRange((int)range_min, (int)range_max);
128 spin_box_->setValue((int)int_val);
131 connect(spin_box_, SIGNAL(valueChanged(int)),
132 this, SLOT(on_value_changed(int)));
144 GVariant *new_value = NULL;
145 const GVariantType *const type = g_variant_get_type(value_.gobj());
148 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
149 new_value = g_variant_new_byte(spin_box_->value());
150 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
151 new_value = g_variant_new_int16(spin_box_->value());
152 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
153 new_value = g_variant_new_uint16(spin_box_->value());
154 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
155 new_value = g_variant_new_int32(spin_box_->value());
156 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
157 new_value = g_variant_new_int32(spin_box_->value());
158 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
159 new_value = g_variant_new_int64(spin_box_->value());
160 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
161 new_value = g_variant_new_uint64(spin_box_->value());
164 // Unexpected value type.
170 value_ = Glib::VariantBase(new_value);
175 void Int::on_value_changed(int)