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, see <http://www.gnu.org/licenses/>.
27 using boost::optional;
35 Int::Int(QString name,
37 optional< pair<int64_t, int64_t> > range,
40 Property(name, getter, setter),
47 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
49 int64_t int_val = 0, range_min = 0;
50 uint64_t range_max = 0;
60 GVariant *value = value_.gobj();
64 spin_box_ = new QSpinBox(parent);
65 spin_box_->setSuffix(suffix_);
67 const GVariantType *const type = g_variant_get_type(value);
70 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) {
71 int_val = g_variant_get_byte(value);
72 range_min = 0, range_max = UINT8_MAX;
73 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) {
74 int_val = g_variant_get_int16(value);
75 range_min = INT16_MIN, range_max = INT16_MAX;
76 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) {
77 int_val = g_variant_get_uint16(value);
78 range_min = 0, range_max = UINT16_MAX;
79 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) {
80 int_val = g_variant_get_int32(value);
81 range_min = INT32_MIN, range_max = INT32_MAX;
82 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) {
83 int_val = g_variant_get_uint32(value);
84 range_min = 0, range_max = UINT32_MAX;
85 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) {
86 int_val = g_variant_get_int64(value);
87 range_min = INT64_MIN, range_max = INT64_MAX;
88 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) {
89 int_val = g_variant_get_uint64(value);
90 range_min = 0, range_max = UINT64_MAX;
92 // Unexpected value type.
96 // @todo Sigrok supports 64-bit quantities, but Qt does not have a
97 // standard widget to allow the values to be modified over the full
98 // 64-bit range on 32-bit machines. To solve the issue we need a
101 range_min = max(range_min, (int64_t)INT_MIN);
102 range_max = min(range_max, (uint64_t)INT_MAX);
105 spin_box_->setRange((int)range_->first, (int)range_->second);
107 spin_box_->setRange((int)range_min, (int)range_max);
109 spin_box_->setValue((int)int_val);
112 connect(spin_box_, SIGNAL(valueChanged(int)),
113 this, SLOT(on_value_changed(int)));
125 GVariant *new_value = nullptr;
126 const GVariantType *const type = g_variant_get_type(value_.gobj());
129 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
130 new_value = g_variant_new_byte(spin_box_->value());
131 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
132 new_value = g_variant_new_int16(spin_box_->value());
133 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
134 new_value = g_variant_new_uint16(spin_box_->value());
135 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
136 new_value = g_variant_new_int32(spin_box_->value());
137 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
138 new_value = g_variant_new_uint32(spin_box_->value());
139 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
140 new_value = g_variant_new_int64(spin_box_->value());
141 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
142 new_value = g_variant_new_uint64(spin_box_->value());
145 // Unexpected value type.
151 value_ = Glib::VariantBase(new_value);
156 void Int::on_value_changed(int)