+Enum::Enum(QString name, QString desc,
+ vector<pair<Glib::VariantBase, QString> > values,
+ Getter getter, Setter setter) :
+ Property(name, desc, getter, setter),
+ values_(values),
+ is_range_(false),
+ selector_(nullptr),
+ slider_layout_widget_(nullptr),
+ slider_(nullptr),
+ slider_label_(nullptr)
+{
+ // Try to determine whether the values make up a range, created by e.g.
+ // std_gvar_min_max_step_thresholds()
+
+ vector<double> deltas;
+ double prev_value = 0;
+
+ for (const pair<Glib::VariantBase, QString> &v : values_) {
+ gdouble value;
+ if (v.first.is_of_type(Glib::VariantType("d"))) {
+ g_variant_get((GVariant*)(v.first.gobj()), "d", &value);
+ } else if (v.first.is_of_type(Glib::VariantType("(dd)"))) {
+ gdouble dummy;
+ g_variant_get((GVariant*)(v.first.gobj()), "(dd)", &value, &dummy);
+ } else
+ break; // Type not d or (dd), so not a range that we can handle
+ deltas.push_back(value - prev_value);
+ prev_value = value;
+ }
+
+ if (deltas.size() > 0) {
+ bool constant_delta = true;
+ double prev_delta = numeric_limits<double>::quiet_NaN();
+
+ bool skip_first = true;
+ for (double delta : deltas) {
+ // First value is incorrect, it's the delta to 0 since no
+ // previous value existed yet
+ if (skip_first) {
+ skip_first = false;
+ continue;
+ }
+ if (std::isnan(prev_delta))
+ prev_delta = delta;
+
+ // 2*DBL_EPSILON doesn't work here, so use a workaround
+ if (abs(delta - prev_delta) > (delta/10))
+ constant_delta = false;
+
+ prev_delta = delta;
+ }
+
+ if (constant_delta)
+ is_range_ = true;
+ }
+}
+
+QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
+{
+ if (!getter_)
+ return nullptr;
+
+ Glib::VariantBase variant = getter_();
+ if (!variant.gobj())
+ return nullptr;
+
+ if (is_range_) {
+ // Use slider
+ if (slider_layout_widget_)
+ return slider_layout_widget_;
+
+ slider_ = new QSlider();
+ // Sliders can't handle float values, so we just use it to specify
+ // the number of steps that we're away from the range's beginning
+ slider_->setOrientation(Qt::Horizontal);
+ slider_->setMinimum(0);
+ slider_->setMaximum(values_.size() - 1);
+ slider_->setSingleStep(1);
+
+ slider_label_ = new QLabel();
+
+ slider_layout_widget_ = new QWidget(parent);
+ QHBoxLayout *layout = new QHBoxLayout(slider_layout_widget_);
+ layout->addWidget(slider_);
+ layout->addWidget(slider_label_);
+
+ update_widget();
+
+ if (auto_commit)
+ connect(slider_, SIGNAL(valueChanged(int)),
+ this, SLOT(on_value_changed(int)));
+
+ return slider_layout_widget_;
+
+ } else {
+ // Use combo box
+ if (selector_)
+ return selector_;
+
+ selector_ = new QComboBox(parent);
+ for (unsigned int i = 0; i < values_.size(); i++) {
+ const pair<Glib::VariantBase, QString> &v = values_[i];
+ selector_->addItem(v.second, qVariantFromValue(v.first));
+ }
+
+ update_widget();
+
+ if (auto_commit)
+ connect(selector_, SIGNAL(currentIndexChanged(int)),
+ this, SLOT(on_current_index_changed(int)));
+
+ return selector_;
+ }
+}
+
+void Enum::update_widget()