From: Joel Holdsworth Date: Mon, 1 Apr 2013 15:09:44 +0000 (+0100) Subject: Make pv::prop objects run on GVariants X-Git-Tag: pulseview-0.1.0~51 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=9b25892767cee1d871ac18beb025033e905b28ae;p=pulseview.git Make pv::prop objects run on GVariants --- diff --git a/pv/prop/double.cpp b/pv/prop/double.cpp index c1e776e1..6f6fe0c1 100644 --- a/pv/prop/double.cpp +++ b/pv/prop/double.cpp @@ -35,15 +35,13 @@ Double::Double(QString name, QString suffix, optional< pair > range, optional step, - function getter, - function setter) : - Property(name), + Getter getter, + Setter setter) : + Property(name, getter, setter), _decimals(decimals), _suffix(suffix), _range(range), _step(step), - _getter(getter), - _setter(setter), _spin_box(NULL) { } @@ -61,7 +59,12 @@ QWidget* Double::get_widget(QWidget *parent) if (_step) _spin_box->setSingleStep(*_step); - _spin_box->setValue(_getter ? _getter() : 0.0); + GVariant *const value = _getter ? _getter() : NULL; + + if (value) { + _spin_box->setValue(g_variant_get_double(value)); + g_variant_unref(value); + } return _spin_box; } @@ -73,7 +76,7 @@ void Double::commit() if (!_spin_box) return; - _setter(_spin_box->value()); + _setter(g_variant_new_double(_spin_box->value())); } } // prop diff --git a/pv/prop/double.h b/pv/prop/double.h index 19cbe2c1..6b6275c1 100644 --- a/pv/prop/double.h +++ b/pv/prop/double.h @@ -39,8 +39,8 @@ public: Double(QString name, int decimals, QString suffix, boost::optional< std::pair > range, boost::optional step, - boost::function getter, - boost::function setter); + Getter getter, + Setter setter); QWidget* get_widget(QWidget *parent); @@ -51,8 +51,6 @@ private: const QString _suffix; const boost::optional< std::pair > _range; const boost::optional _step; - boost::function _getter; - boost::function _setter; QDoubleSpinBox *_spin_box; }; diff --git a/pv/prop/enum.cpp b/pv/prop/enum.cpp index 894ba9f8..065fd548 100644 --- a/pv/prop/enum.cpp +++ b/pv/prop/enum.cpp @@ -20,7 +20,6 @@ #include -#include #include #include "enum.h" @@ -32,35 +31,38 @@ namespace pv { namespace prop { Enum::Enum(QString name, - vector > values, - function getter, - function setter) : - Property(name), + vector > values, + Getter getter, Setter setter) : + Property(name, getter, setter), _values(values), - _getter(getter), - _setter(setter), _selector(NULL) { } +Enum::~Enum() +{ + for (unsigned int i = 0; i < _values.size(); i++) + g_variant_unref(_values[i].first); +} + QWidget* Enum::get_widget(QWidget *parent) { if (_selector) return _selector; - const void *value = NULL; - if (_getter) - value = _getter(); + GVariant *const value = _getter ? _getter() : NULL; + assert(value); _selector = new QComboBox(parent); for (unsigned int i = 0; i < _values.size(); i++) { - const pair &v = _values[i]; - _selector->addItem(v.second, - qVariantFromValue((void*)v.first)); - if (v.first == value) + const pair &v = _values[i]; + _selector->addItem(v.second, qVariantFromValue((void*)v.first)); + if (g_variant_compare(v.first, value) == 0) _selector->setCurrentIndex(i); } + g_variant_unref(value); + return _selector; } @@ -75,7 +77,7 @@ void Enum::commit() if (index < 0) return; - _setter(_selector->itemData(index).value()); + _setter((GVariant*)_selector->itemData(index).value()); } } // prop diff --git a/pv/prop/enum.h b/pv/prop/enum.h index 02b60df5..402798c0 100644 --- a/pv/prop/enum.h +++ b/pv/prop/enum.h @@ -24,7 +24,6 @@ #include #include -#include #include "property.h" class QComboBox; @@ -35,19 +34,17 @@ namespace prop { class Enum : public Property { public: - Enum(QString name, - std::vector > values, - boost::function getter, - boost::function setter); + Enum(QString name, std::vector > values, + Getter getter, Setter setter); + + virtual ~Enum(); QWidget* get_widget(QWidget *parent); void commit(); private: - const std::vector< std::pair > _values; - boost::function _getter; - boost::function _setter; + const std::vector< std::pair > _values; QComboBox *_selector; }; diff --git a/pv/prop/property.cpp b/pv/prop/property.cpp index 0a14d241..8276a191 100644 --- a/pv/prop/property.cpp +++ b/pv/prop/property.cpp @@ -23,7 +23,9 @@ namespace pv { namespace prop { -Property::Property(QString name) : +Property::Property(QString name, Getter getter, Setter setter) : + _getter(getter), + _setter(setter), _name(name) { } diff --git a/pv/prop/property.h b/pv/prop/property.h index 002c9116..6ab83193 100644 --- a/pv/prop/property.h +++ b/pv/prop/property.h @@ -21,6 +21,8 @@ #ifndef PULSEVIEW_PV_PROP_PROPERTY_H #define PULSEVIEW_PV_PROP_PROPERTY_H +#include + #include #include @@ -33,8 +35,12 @@ namespace prop { class Property { +public: + typedef boost::function Getter; + typedef boost::function Setter; + protected: - Property(QString name); + Property(QString name, Getter getter, Setter setter); public: const QString& name() const; @@ -43,6 +49,10 @@ public: virtual void commit() = 0; +protected: + const Getter _getter; + const Setter _setter; + private: QString _name; };