]> sigrok.org Git - pulseview.git/blame - pv/prop/int.cpp
Added devices::File as a common base of file devices
[pulseview.git] / pv / prop / int.cpp
CommitLineData
a2b92157
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
ac223c1e 21#include <stdint.h>
a2b92157
JH
22#include <assert.h>
23
24#include <QSpinBox>
25
2acdb232 26#include "int.hpp"
a2b92157 27
819f4c25
JH
28using boost::optional;
29using std::max;
30using std::min;
31using std::pair;
a2b92157
JH
32
33namespace pv {
34namespace prop {
35
36Int::Int(QString name,
37 QString suffix,
38 optional< pair<int64_t, int64_t> > range,
39 Getter getter,
40 Setter setter) :
41 Property(name, getter, setter),
8dbbc7f0
JH
42 suffix_(suffix),
43 range_(range),
44 spin_box_(NULL)
a2b92157
JH
45{
46}
47
f459c540
JH
48Int::~Int()
49{
50}
51
b1fe148e 52QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
a2b92157 53{
ac223c1e
JH
54 int64_t int_val = 0, range_min = 0, range_max = 0;
55
8dbbc7f0
JH
56 if (spin_box_)
57 return spin_box_;
a2b92157 58
8dbbc7f0 59 if (!getter_)
e8d00928
ML
60 return NULL;
61
8dbbc7f0 62 value_ = getter_();
ac223c1e 63
8dbbc7f0 64 GVariant *value = value_.gobj();
e8d00928 65 if (!value)
ac223c1e
JH
66 return NULL;
67
8dbbc7f0
JH
68 spin_box_ = new QSpinBox(parent);
69 spin_box_->setSuffix(suffix_);
ac223c1e 70
e8d00928 71 const GVariantType *const type = g_variant_get_type(value);
ac223c1e
JH
72 assert(type);
73
74 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
75 {
e8d00928 76 int_val = g_variant_get_byte(value);
ac223c1e
JH
77 range_min = 0, range_max = UINT8_MAX;
78 }
79 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
80 {
e8d00928 81 int_val = g_variant_get_int16(value);
ac223c1e
JH
82 range_min = INT16_MIN, range_max = INT16_MAX;
83 }
84 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
85 {
e8d00928 86 int_val = g_variant_get_uint16(value);
ac223c1e
JH
87 range_min = 0, range_max = UINT16_MAX;
88 }
89 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
90 {
e8d00928 91 int_val = g_variant_get_int32(value);
ac223c1e
JH
92 range_min = INT32_MIN, range_max = INT32_MAX;
93 }
94 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
95 {
e8d00928 96 int_val = g_variant_get_uint32(value);
ac223c1e
JH
97 range_min = 0, range_max = UINT32_MAX;
98 }
99 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
100 {
e8d00928 101 int_val = g_variant_get_int64(value);
ac223c1e
JH
102 range_min = INT64_MIN, range_max = INT64_MAX;
103 }
104 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
105 {
e8d00928 106 int_val = g_variant_get_uint64(value);
ac223c1e
JH
107 range_min = 0, range_max = UINT64_MAX;
108 }
109 else
110 {
111 // Unexpected value type.
112 assert(0);
113 }
114
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
118 // custom widget.
119
120 range_min = max(range_min, (int64_t)INT_MIN);
121 range_max = min(range_max, (int64_t)INT_MAX);
122
8dbbc7f0
JH
123 if (range_)
124 spin_box_->setRange((int)range_->first, (int)range_->second);
a915c6a3 125 else
8dbbc7f0 126 spin_box_->setRange((int)range_min, (int)range_max);
a2b92157 127
8dbbc7f0 128 spin_box_->setValue((int)int_val);
a2b92157 129
b1fe148e 130 if (auto_commit)
8dbbc7f0 131 connect(spin_box_, SIGNAL(valueChanged(int)),
b1fe148e
JH
132 this, SLOT(on_value_changed(int)));
133
8dbbc7f0 134 return spin_box_;
a2b92157
JH
135}
136
137void Int::commit()
138{
8dbbc7f0 139 assert(setter_);
a2b92157 140
8dbbc7f0 141 if (!spin_box_)
a2b92157
JH
142 return;
143
ac223c1e 144 GVariant *new_value = NULL;
8dbbc7f0 145 const GVariantType *const type = g_variant_get_type(value_.gobj());
ac223c1e
JH
146 assert(type);
147
148 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
8dbbc7f0 149 new_value = g_variant_new_byte(spin_box_->value());
ac223c1e 150 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
8dbbc7f0 151 new_value = g_variant_new_int16(spin_box_->value());
ac223c1e 152 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
8dbbc7f0 153 new_value = g_variant_new_uint16(spin_box_->value());
ac223c1e 154 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
8dbbc7f0 155 new_value = g_variant_new_int32(spin_box_->value());
ac223c1e 156 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
79118fe5 157 new_value = g_variant_new_uint32(spin_box_->value());
ac223c1e 158 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
8dbbc7f0 159 new_value = g_variant_new_int64(spin_box_->value());
ac223c1e 160 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
8dbbc7f0 161 new_value = g_variant_new_uint64(spin_box_->value());
ac223c1e
JH
162 else
163 {
164 // Unexpected value type.
165 assert(0);
166 }
167
168 assert(new_value);
169
8dbbc7f0 170 value_ = Glib::VariantBase(new_value);
ac223c1e 171
8dbbc7f0 172 setter_(value_);
a2b92157
JH
173}
174
b1fe148e
JH
175void Int::on_value_changed(int)
176{
177 commit();
178}
179
a2b92157
JH
180} // prop
181} // pv