QString suffix,
optional< pair<double, double> > range,
optional<double> step,
- function<double ()> getter,
- function<void (double)> 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)
{
}
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;
}
if (!_spin_box)
return;
- _setter(_spin_box->value());
+ _setter(g_variant_new_double(_spin_box->value()));
}
} // prop
Double(QString name, int decimals, QString suffix,
boost::optional< std::pair<double, double> > range,
boost::optional<double> step,
- boost::function<double ()> getter,
- boost::function<void (double)> setter);
+ Getter getter,
+ Setter setter);
QWidget* get_widget(QWidget *parent);
const QString _suffix;
const boost::optional< std::pair<double, double> > _range;
const boost::optional<double> _step;
- boost::function<double ()> _getter;
- boost::function<void (double)> _setter;
QDoubleSpinBox *_spin_box;
};
#include <assert.h>
-#include <glib-2.0/glib.h>
#include <QComboBox>
#include "enum.h"
namespace prop {
Enum::Enum(QString name,
- vector<pair<const void*, QString> > values,
- function<GVariant* ()> getter,
- function<void (GVariant*)> setter) :
- Property(name),
+ vector<pair<GVariant*, QString> > 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<const void*, QString> &v = _values[i];
- _selector->addItem(v.second,
- qVariantFromValue((void*)v.first));
- if (v.first == value)
+ const pair<GVariant*, QString> &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;
}
if (index < 0)
return;
- _setter(_selector->itemData(index).value<GVariant*>());
+ _setter((GVariant*)_selector->itemData(index).value<void*>());
}
} // prop
#include <utility>
#include <vector>
-#include <glib-2.0/glib.h>
#include "property.h"
class QComboBox;
class Enum : public Property
{
public:
- Enum(QString name,
- std::vector<std::pair<const void*, QString> > values,
- boost::function<GVariant* ()> getter,
- boost::function<void (GVariant*)> setter);
+ Enum(QString name, std::vector<std::pair<GVariant*, QString> > values,
+ Getter getter, Setter setter);
+
+ virtual ~Enum();
QWidget* get_widget(QWidget *parent);
void commit();
private:
- const std::vector< std::pair<const void*, QString> > _values;
- boost::function<GVariant* ()> _getter;
- boost::function<void (GVariant*)> _setter;
+ const std::vector< std::pair<GVariant*, QString> > _values;
QComboBox *_selector;
};
namespace pv {
namespace prop {
-Property::Property(QString name) :
+Property::Property(QString name, Getter getter, Setter setter) :
+ _getter(getter),
+ _setter(setter),
_name(name)
{
}
#ifndef PULSEVIEW_PV_PROP_PROPERTY_H
#define PULSEVIEW_PV_PROP_PROPERTY_H
+#include <glib-2.0/glib.h>
+
#include <boost/function.hpp>
#include <QString>
class Property
{
+public:
+ typedef boost::function<GVariant* ()> Getter;
+ typedef boost::function<void (GVariant*)> Setter;
+
protected:
- Property(QString name);
+ Property(QString name, Getter getter, Setter setter);
public:
const QString& name() const;
virtual void commit() = 0;
+protected:
+ const Getter _getter;
+ const Setter _setter;
+
private:
QString _name;
};