]> sigrok.org Git - pulseview.git/blobdiff - pv/prop/enum.cpp
Fix #1149 by handling "d" and "(dd)" types separately
[pulseview.git] / pv / prop / enum.cpp
index d5d571a9ec487396b921cdae5b45855bb7bbabde..1f542b74a73d6da49552a08236903ce9dad1b3fe 100644 (file)
  */
 
 #include <cassert>
+#include <cfloat>
+#include <cmath>
 
 #include <QComboBox>
 
 #include "enum.hpp"
 
+using std::abs;
 using std::pair;
 using std::vector;
 
@@ -54,10 +57,10 @@ QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
        for (unsigned int i = 0; i < values_.size(); i++) {
                const pair<Glib::VariantBase, QString> &v = values_[i];
                selector_->addItem(v.second, qVariantFromValue(v.first));
-               if (v.first.equal(variant))
-                       selector_->setCurrentIndex(i);
        }
 
+       update_widget();
+
        if (auto_commit)
                connect(selector_, SIGNAL(currentIndexChanged(int)),
                        this, SLOT(on_current_item_changed(int)));
@@ -65,6 +68,42 @@ QWidget* Enum::get_widget(QWidget *parent, bool auto_commit)
        return selector_;
 }
 
+void Enum::update_widget()
+{
+       if (!selector_)
+               return;
+
+       Glib::VariantBase variant = getter_();
+       assert(variant.gobj());
+
+       for (unsigned int i = 0; i < values_.size(); i++) {
+               const pair<Glib::VariantBase, QString> &v = values_[i];
+
+               // g_variant_equal() doesn't handle floating point properly
+               if (v.first.is_of_type(Glib::VariantType("d"))) {
+                       gdouble a, b;
+                       g_variant_get(variant.gobj(), "d", &a);
+                       g_variant_get((GVariant*)(v.first.gobj()), "d", &b);
+                       if (abs(a - b) <= 2 * DBL_EPSILON)
+                               selector_->setCurrentIndex(i);
+               } else {
+                       // Check for "(dd)" type and handle it if it's found
+                       if (v.first.is_of_type(Glib::VariantType("(dd)"))) {
+                               gdouble a1, a2, b1, b2;
+                               g_variant_get(variant.gobj(), "(dd)", &a1, &a2);
+                               g_variant_get((GVariant*)(v.first.gobj()), "(dd)", &b1, &b2);
+                               if ((abs(a1 - b1) <= 2 * DBL_EPSILON) && \
+                                       (abs(a2 - b2) <= 2 * DBL_EPSILON))
+                                       selector_->setCurrentIndex(i);
+
+                       } else
+                               // Handle all other types
+                               if (v.first.equal(variant))
+                                       selector_->setCurrentIndex(i);
+               }
+       }
+}
+
 void Enum::commit()
 {
        assert(setter_);