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