Perform autoranging for analog traces also when new data comes in
[pulseview.git] / pv / prop / double.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <assert.h>
21
22 #include <QDoubleSpinBox>
23
24 #include "double.hpp"
25
26 using boost::optional;
27 using std::pair;
28
29 namespace pv {
30 namespace prop {
31
32 Double::Double(QString name,
33         int decimals,
34         QString suffix,
35         optional< pair<double, double> > range,
36         optional<double> step,
37         Getter getter,
38         Setter setter) :
39         Property(name, getter, setter),
40         decimals_(decimals),
41         suffix_(suffix),
42         range_(range),
43         step_(step),
44         spin_box_(nullptr)
45 {
46 }
47
48 QWidget* Double::get_widget(QWidget *parent, bool auto_commit)
49 {
50         if (spin_box_)
51                 return spin_box_;
52
53         if (!getter_)
54                 return nullptr;
55
56         Glib::VariantBase variant = getter_();
57         if (!variant.gobj())
58                 return nullptr;
59
60         double value = Glib::VariantBase::cast_dynamic<Glib::Variant<double>>(
61                 variant).get();
62
63         spin_box_ = new QDoubleSpinBox(parent);
64         spin_box_->setDecimals(decimals_);
65         spin_box_->setSuffix(suffix_);
66         if (range_)
67                 spin_box_->setRange(range_->first, range_->second);
68         if (step_)
69                 spin_box_->setSingleStep(*step_);
70
71         spin_box_->setValue(value);
72
73         if (auto_commit)
74                 connect(spin_box_, SIGNAL(valueChanged(double)),
75                         this, SLOT(on_value_changed(double)));
76
77         return spin_box_;
78 }
79
80 void Double::commit()
81 {
82         assert(setter_);
83
84         if (!spin_box_)
85                 return;
86
87         setter_(Glib::Variant<double>::create(spin_box_->value()));
88 }
89
90 void Double::on_value_changed(double)
91 {
92         commit();
93 }
94
95 } // prop
96 } // pv