]> sigrok.org Git - pulseview.git/blame - pv/prop/int.cpp
Replaced using namespace with using class directives
[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
26#include "int.h"
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),
42 _suffix(suffix),
43 _range(range),
ac223c1e 44 _value(NULL),
a2b92157
JH
45 _spin_box(NULL)
46{
47}
48
f459c540
JH
49Int::~Int()
50{
ac223c1e
JH
51 if (_value)
52 g_variant_unref(_value);
f459c540
JH
53}
54
b1fe148e 55QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
a2b92157 56{
ac223c1e
JH
57 int64_t int_val = 0, range_min = 0, range_max = 0;
58
a2b92157
JH
59 if (_spin_box)
60 return _spin_box;
61
ac223c1e
JH
62 if (_value)
63 g_variant_unref(_value);
64
65 _value = _getter ? _getter() : NULL;
66 assert(_value);
67 if (!_value)
68 return NULL;
69
a2b92157
JH
70 _spin_box = new QSpinBox(parent);
71 _spin_box->setSuffix(_suffix);
ac223c1e
JH
72
73 const GVariantType *const type = g_variant_get_type(_value);
74 assert(type);
75
76 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
77 {
78 int_val = g_variant_get_byte(_value);
79 range_min = 0, range_max = UINT8_MAX;
80 }
81 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
82 {
83 int_val = g_variant_get_int16(_value);
84 range_min = INT16_MIN, range_max = INT16_MAX;
85 }
86 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
87 {
88 int_val = g_variant_get_uint16(_value);
89 range_min = 0, range_max = UINT16_MAX;
90 }
91 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
92 {
93 int_val = g_variant_get_int32(_value);
94 range_min = INT32_MIN, range_max = INT32_MAX;
95 }
96 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
97 {
98 int_val = g_variant_get_uint32(_value);
99 range_min = 0, range_max = UINT32_MAX;
100 }
101 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
102 {
103 int_val = g_variant_get_int64(_value);
104 range_min = INT64_MIN, range_max = INT64_MAX;
105 }
106 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
107 {
108 int_val = g_variant_get_uint64(_value);
109 range_min = 0, range_max = UINT64_MAX;
110 }
111 else
112 {
113 // Unexpected value type.
114 assert(0);
115 }
116
117 // @todo Sigrok supports 64-bit quantities, but Qt does not have a
118 // standard widget to allow the values to be modified over the full
119 // 64-bit range on 32-bit machines. To solve the issue we need a
120 // custom widget.
121
122 range_min = max(range_min, (int64_t)INT_MIN);
123 range_max = min(range_max, (int64_t)INT_MAX);
124
a2b92157
JH
125 if (_range)
126 _spin_box->setRange((int)_range->first, (int)_range->second);
a915c6a3 127 else
ac223c1e 128 _spin_box->setRange((int)range_min, (int)range_max);
a2b92157 129
ac223c1e 130 _spin_box->setValue((int)int_val);
a2b92157 131
b1fe148e
JH
132 if (auto_commit)
133 connect(_spin_box, SIGNAL(valueChanged(int)),
134 this, SLOT(on_value_changed(int)));
135
a2b92157
JH
136 return _spin_box;
137}
138
139void Int::commit()
140{
141 assert(_setter);
142
143 if (!_spin_box)
144 return;
145
ac223c1e
JH
146 assert(_value);
147
148 GVariant *new_value = NULL;
149 const GVariantType *const type = g_variant_get_type(_value);
150 assert(type);
151
152 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
153 new_value = g_variant_new_byte(_spin_box->value());
154 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
155 new_value = g_variant_new_int16(_spin_box->value());
156 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
157 new_value = g_variant_new_uint16(_spin_box->value());
158 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
159 new_value = g_variant_new_int32(_spin_box->value());
160 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
161 new_value = g_variant_new_int32(_spin_box->value());
162 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
163 new_value = g_variant_new_int64(_spin_box->value());
164 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
165 new_value = g_variant_new_uint64(_spin_box->value());
166 else
167 {
168 // Unexpected value type.
169 assert(0);
170 }
171
172 assert(new_value);
173
174 g_variant_unref(_value);
175 g_variant_ref(new_value);
176 _value = new_value;
177
178 _setter(new_value);
a2b92157
JH
179}
180
b1fe148e
JH
181void Int::on_value_changed(int)
182{
183 commit();
184}
185
a2b92157
JH
186} // prop
187} // pv