]> sigrok.org Git - pulseview.git/blame - pv/prop/int.cpp
TraceTreeItemOwner: Removed non-const item_list accessor
[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),
4c60462b 44 spin_box_(nullptr)
a2b92157
JH
45{
46}
47
f459c540
JH
48Int::~Int()
49{
50}
51
b1fe148e 52QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
a2b92157 53{
765e7d33
SA
54 int64_t int_val = 0, range_min = 0;
55 uint64_t range_max = 0;
ac223c1e 56
8dbbc7f0
JH
57 if (spin_box_)
58 return spin_box_;
a2b92157 59
8dbbc7f0 60 if (!getter_)
4c60462b 61 return nullptr;
e8d00928 62
8dbbc7f0 63 value_ = getter_();
ac223c1e 64
8dbbc7f0 65 GVariant *value = value_.gobj();
e8d00928 66 if (!value)
4c60462b 67 return nullptr;
ac223c1e 68
8dbbc7f0
JH
69 spin_box_ = new QSpinBox(parent);
70 spin_box_->setSuffix(suffix_);
ac223c1e 71
e8d00928 72 const GVariantType *const type = g_variant_get_type(value);
ac223c1e
JH
73 assert(type);
74
75 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
76 {
e8d00928 77 int_val = g_variant_get_byte(value);
ac223c1e
JH
78 range_min = 0, range_max = UINT8_MAX;
79 }
80 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
81 {
e8d00928 82 int_val = g_variant_get_int16(value);
ac223c1e
JH
83 range_min = INT16_MIN, range_max = INT16_MAX;
84 }
85 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
86 {
e8d00928 87 int_val = g_variant_get_uint16(value);
ac223c1e
JH
88 range_min = 0, range_max = UINT16_MAX;
89 }
90 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
91 {
e8d00928 92 int_val = g_variant_get_int32(value);
ac223c1e
JH
93 range_min = INT32_MIN, range_max = INT32_MAX;
94 }
95 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
96 {
e8d00928 97 int_val = g_variant_get_uint32(value);
ac223c1e
JH
98 range_min = 0, range_max = UINT32_MAX;
99 }
100 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
101 {
e8d00928 102 int_val = g_variant_get_int64(value);
ac223c1e
JH
103 range_min = INT64_MIN, range_max = INT64_MAX;
104 }
105 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
106 {
e8d00928 107 int_val = g_variant_get_uint64(value);
ac223c1e
JH
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);
765e7d33 122 range_max = min(range_max, (uint64_t)INT_MAX);
ac223c1e 123
8dbbc7f0
JH
124 if (range_)
125 spin_box_->setRange((int)range_->first, (int)range_->second);
a915c6a3 126 else
8dbbc7f0 127 spin_box_->setRange((int)range_min, (int)range_max);
a2b92157 128
8dbbc7f0 129 spin_box_->setValue((int)int_val);
a2b92157 130
b1fe148e 131 if (auto_commit)
8dbbc7f0 132 connect(spin_box_, SIGNAL(valueChanged(int)),
b1fe148e
JH
133 this, SLOT(on_value_changed(int)));
134
8dbbc7f0 135 return spin_box_;
a2b92157
JH
136}
137
138void Int::commit()
139{
8dbbc7f0 140 assert(setter_);
a2b92157 141
8dbbc7f0 142 if (!spin_box_)
a2b92157
JH
143 return;
144
4c60462b 145 GVariant *new_value = nullptr;
8dbbc7f0 146 const GVariantType *const type = g_variant_get_type(value_.gobj());
ac223c1e
JH
147 assert(type);
148
149 if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
8dbbc7f0 150 new_value = g_variant_new_byte(spin_box_->value());
ac223c1e 151 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
8dbbc7f0 152 new_value = g_variant_new_int16(spin_box_->value());
ac223c1e 153 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
8dbbc7f0 154 new_value = g_variant_new_uint16(spin_box_->value());
ac223c1e 155 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
8dbbc7f0 156 new_value = g_variant_new_int32(spin_box_->value());
ac223c1e 157 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
79118fe5 158 new_value = g_variant_new_uint32(spin_box_->value());
ac223c1e 159 else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
8dbbc7f0 160 new_value = g_variant_new_int64(spin_box_->value());
ac223c1e 161 else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
8dbbc7f0 162 new_value = g_variant_new_uint64(spin_box_->value());
ac223c1e
JH
163 else
164 {
165 // Unexpected value type.
166 assert(0);
167 }
168
169 assert(new_value);
170
8dbbc7f0 171 value_ = Glib::VariantBase(new_value);
ac223c1e 172
8dbbc7f0 173 setter_(value_);
a2b92157
JH
174}
175
b1fe148e
JH
176void Int::on_value_changed(int)
177{
178 commit();
179}
180
a2b92157
JH
181} // prop
182} // pv