X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fprop%2Fdouble.cpp;h=57512d723ff26b5cd3473432040e975cf914e8bb;hp=c1e776e1f6c890c21d9d435cbc66316d8976e528;hb=dbed5609ae31cdfc3e9db10f3ab91b7607c08372;hpb=793f8096c486d0fba871227d9772a510f7496c08 diff --git a/pv/prop/double.cpp b/pv/prop/double.cpp index c1e776e1..57512d72 100644 --- a/pv/prop/double.cpp +++ b/pv/prop/double.cpp @@ -14,67 +14,94 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ -#include +#include #include -#include "double.h" +#include "double.hpp" -using namespace std; -using namespace boost; +using boost::optional; +using std::pair; namespace pv { namespace prop { Double::Double(QString name, + QString desc, int decimals, QString suffix, optional< pair > range, optional step, - function getter, - function setter) : - Property(name), - _decimals(decimals), - _suffix(suffix), - _range(range), - _step(step), - _getter(getter), - _setter(setter), - _spin_box(NULL) + Getter getter, + Setter setter) : + Property(name, desc, getter, setter), + decimals_(decimals), + suffix_(suffix), + range_(range), + step_(step), + spin_box_(nullptr) { } -QWidget* Double::get_widget(QWidget *parent) +QWidget* Double::get_widget(QWidget *parent, bool auto_commit) { - if (_spin_box) - return _spin_box; + if (spin_box_) + return spin_box_; - _spin_box = new QDoubleSpinBox(parent); - _spin_box->setDecimals(_decimals); - _spin_box->setSuffix(_suffix); - if (_range) - _spin_box->setRange(_range->first, _range->second); - if (_step) - _spin_box->setSingleStep(*_step); + if (!getter_) + return nullptr; - _spin_box->setValue(_getter ? _getter() : 0.0); + Glib::VariantBase variant = getter_(); + if (!variant.gobj()) + return nullptr; - return _spin_box; + spin_box_ = new QDoubleSpinBox(parent); + spin_box_->setDecimals(decimals_); + spin_box_->setSuffix(suffix_); + if (range_) + spin_box_->setRange(range_->first, range_->second); + if (step_) + spin_box_->setSingleStep(*step_); + + update_widget(); + + if (auto_commit) + connect(spin_box_, SIGNAL(valueChanged(double)), + this, SLOT(on_value_changed(double))); + + return spin_box_; +} + +void Double::update_widget() +{ + if (!spin_box_) + return; + + Glib::VariantBase variant = getter_(); + assert(variant.gobj()); + + double value = Glib::VariantBase::cast_dynamic>( + variant).get(); + spin_box_->setValue(value); } void Double::commit() { - assert(_setter); + assert(setter_); - if (!_spin_box) + if (!spin_box_) return; - _setter(_spin_box->value()); + setter_(Glib::Variant::create(spin_box_->value())); +} + +void Double::on_value_changed(double) +{ + commit(); } -} // prop -} // pv +} // namespace prop +} // namespace pv