]> sigrok.org Git - pulseview.git/blob - pv/prop/int.cpp
Replaced using namespace with using class directives
[pulseview.git] / pv / prop / int.cpp
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
21 #include <stdint.h>
22 #include <assert.h>
23
24 #include <QSpinBox>
25
26 #include "int.h"
27
28 using boost::optional;
29 using std::max;
30 using std::min;
31 using std::pair;
32
33 namespace pv {
34 namespace prop {
35
36 Int::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),
44         _value(NULL),
45         _spin_box(NULL)
46 {
47 }
48
49 Int::~Int()
50 {
51         if (_value)
52                 g_variant_unref(_value);
53 }
54
55 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
56 {
57         int64_t int_val = 0, range_min = 0, range_max = 0;
58
59         if (_spin_box)
60                 return _spin_box;
61
62         if (_value)
63                 g_variant_unref(_value);
64
65         _value = _getter ? _getter() : NULL;
66         assert(_value);
67         if (!_value)
68                 return NULL;
69
70         _spin_box = new QSpinBox(parent);
71         _spin_box->setSuffix(_suffix);
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
125         if (_range)
126                 _spin_box->setRange((int)_range->first, (int)_range->second);
127         else
128                 _spin_box->setRange((int)range_min, (int)range_max);
129
130         _spin_box->setValue((int)int_val);
131
132         if (auto_commit)
133                 connect(_spin_box, SIGNAL(valueChanged(int)),
134                         this, SLOT(on_value_changed(int)));
135
136         return _spin_box;
137 }
138
139 void Int::commit()
140 {
141         assert(_setter);
142
143         if (!_spin_box)
144                 return;
145
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);
179 }
180
181 void Int::on_value_changed(int)
182 {
183         commit();
184 }
185
186 } // prop
187 } // pv