desktop file: Add additional Development category.
[pulseview.git] / pv / prop / int.cpp
blob98b23ba6386401adc172817e188178bb8d0d4f78
1 /*
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/>.
20 #include <cassert>
21 #include <cstdint>
23 #include <QSpinBox>
25 #include "int.hpp"
27 using boost::optional;
28 using std::max;
29 using std::min;
30 using std::pair;
32 namespace pv {
33 namespace prop {
35 Int::Int(QString name,
36 QString desc,
37 QString suffix,
38 optional< pair<int64_t, int64_t> > range,
39 Getter getter,
40 Setter setter) :
41 Property(name, desc, getter, setter),
42 suffix_(suffix),
43 range_(range),
44 spin_box_(nullptr)
48 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
50 int64_t int_val = 0, range_min = 0;
51 uint64_t range_max = 0;
53 if (spin_box_)
54 return spin_box_;
56 if (!getter_)
57 return nullptr;
59 value_ = getter_();
61 GVariant *value = value_.gobj();
62 if (!value)
63 return nullptr;
65 spin_box_ = new QSpinBox(parent);
66 spin_box_->setSuffix(suffix_);
68 const GVariantType *const type = g_variant_get_type(value);
69 assert(type);
71 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) {
72 int_val = g_variant_get_byte(value);
73 range_min = 0, range_max = UINT8_MAX;
74 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) {
75 int_val = g_variant_get_int16(value);
76 range_min = INT16_MIN, range_max = INT16_MAX;
77 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) {
78 int_val = g_variant_get_uint16(value);
79 range_min = 0, range_max = UINT16_MAX;
80 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) {
81 int_val = g_variant_get_int32(value);
82 range_min = INT32_MIN, range_max = INT32_MAX;
83 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) {
84 int_val = g_variant_get_uint32(value);
85 range_min = 0, range_max = UINT32_MAX;
86 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) {
87 int_val = g_variant_get_int64(value);
88 range_min = INT64_MIN, range_max = INT64_MAX;
89 } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) {
90 int_val = g_variant_get_uint64(value);
91 range_min = 0, range_max = UINT64_MAX;
92 } else {
93 // Unexpected value type.
94 assert(false);
97 // @todo Sigrok supports 64-bit quantities, but Qt does not have a
98 // standard widget to allow the values to be modified over the full
99 // 64-bit range on 32-bit machines. To solve the issue we need a
100 // custom widget.
102 range_min = max(range_min, (int64_t)INT_MIN);
103 range_max = min(range_max, (uint64_t)INT_MAX);
105 if (range_)
106 spin_box_->setRange((int)range_->first, (int)range_->second);
107 else
108 spin_box_->setRange((int)range_min, (int)range_max);
110 spin_box_->setValue((int)int_val);
112 if (auto_commit)
113 connect(spin_box_, SIGNAL(valueChanged(int)),
114 this, SLOT(on_value_changed(int)));
116 return spin_box_;
119 void Int::commit()
121 assert(setter_);
123 if (!spin_box_)
124 return;
126 GVariant *new_value = nullptr;
127 const GVariantType *const type = g_variant_get_type(value_.gobj());
128 assert(type);
130 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
131 new_value = g_variant_new_byte(spin_box_->value());
132 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
133 new_value = g_variant_new_int16(spin_box_->value());
134 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
135 new_value = g_variant_new_uint16(spin_box_->value());
136 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
137 new_value = g_variant_new_int32(spin_box_->value());
138 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
139 new_value = g_variant_new_uint32(spin_box_->value());
140 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
141 new_value = g_variant_new_int64(spin_box_->value());
142 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
143 new_value = g_variant_new_uint64(spin_box_->value());
144 else {
145 // Unexpected value type.
146 assert(false);
149 assert(new_value);
151 value_ = Glib::VariantBase(new_value);
153 setter_(value_);
156 void Int::on_value_changed(int)
158 commit();
161 } // namespace prop
162 } // namespace pv