]> sigrok.org Git - pulseview.git/commitdiff
Update property widgets before showing device config popup
authorSoeren Apel <redacted>
Sun, 6 May 2018 17:40:10 +0000 (19:40 +0200)
committerSoeren Apel <redacted>
Sun, 6 May 2018 17:40:10 +0000 (19:40 +0200)
16 files changed:
pv/binding/binding.cpp
pv/binding/binding.hpp
pv/popups/deviceoptions.cpp
pv/popups/deviceoptions.hpp
pv/prop/bool.cpp
pv/prop/bool.hpp
pv/prop/double.cpp
pv/prop/double.hpp
pv/prop/enum.cpp
pv/prop/enum.hpp
pv/prop/int.cpp
pv/prop/int.hpp
pv/prop/property.hpp
pv/prop/string.cpp
pv/prop/string.hpp
pv/widgets/popup.hpp

index ae4f2943bdfe988a9dc6e82af5337525c317c04f..ddf0c19a213580846b974ad36fb90a54aede7f94 100644 (file)
@@ -54,8 +54,7 @@ void Binding::add_properties_to_form(QFormLayout *layout,
        for (shared_ptr<pv::prop::Property> p : properties_) {
                assert(p);
 
-               QWidget *const widget = p->get_widget(layout->parentWidget(),
-                       auto_commit);
+               QWidget *const widget = p->get_widget(layout->parentWidget(), auto_commit);
                if (p->labeled_widget()) {
                        layout->addRow(widget);
                } else {
@@ -76,6 +75,14 @@ QWidget* Binding::get_property_form(QWidget *parent,
        return form;
 }
 
+void Binding::update_property_widgets()
+{
+       for (shared_ptr<pv::prop::Property> p : properties_) {
+               assert(p);
+               p->update_widget();
+       }
+}
+
 QString Binding::print_gvariant(Glib::VariantBase gvar)
 {
        QString s;
@@ -84,8 +91,7 @@ QString Binding::print_gvariant(Glib::VariantBase gvar)
                s = QString::fromStdString("(null)");
        else if (gvar.is_of_type(Glib::VariantType("s")))
                s = QString::fromStdString(
-                       Glib::VariantBase::cast_dynamic<Glib::Variant<string>>(
-                               gvar).get());
+                       Glib::VariantBase::cast_dynamic<Glib::Variant<string>>(gvar).get());
        else
                s = QString::fromStdString(gvar.print());
 
index e672ff176f24a5301026e519057b47b0db0b263f..6e92b08393694af4b593f859af7e449021527abb 100644 (file)
@@ -58,6 +58,8 @@ public:
        QWidget* get_property_form(QWidget *parent,
                bool auto_commit = false) const;
 
+       void update_property_widgets();
+
        static QString print_gvariant(Glib::VariantBase gvar);
 
 protected:
index df9113679d2d5ddddd67c169778ea4820166cddc..bfb702cf064ce062462b67e5c7f0d0eba3f822d0 100644 (file)
@@ -49,5 +49,14 @@ pv::binding::Device& DeviceOptions::binding()
        return binding_;
 }
 
+void DeviceOptions::show()
+{
+       // Update device config widgets with the current values supplied by the
+       // driver before actually showing the popup dialog
+       binding_.update_property_widgets();
+
+       Popup::show();
+}
+
 } // namespace popups
 } // namespace pv
index 21f88e90947f038a01f675a7483584ccc90ff776..52b2c5a585dae74e10041033076f54b0bd57bee7 100644 (file)
@@ -44,6 +44,8 @@ public:
 
        pv::binding::Device& binding();
 
+       virtual void show();
+
 private:
        shared_ptr<sigrok::Device> device_;
 
index 0dd1c50f10698702a2397ea604b27d6c285ab230..6c89741ff6f6657779d74f4a6e30d0c7675d72d4 100644 (file)
@@ -44,12 +44,10 @@ QWidget* Bool::get_widget(QWidget *parent, bool auto_commit)
        if (!variant.gobj())
                return nullptr;
 
-       bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
-               variant).get();
-
        check_box_ = new QCheckBox(name(), parent);
        check_box_->setToolTip(desc());
-       check_box_->setCheckState(value ? Qt::Checked : Qt::Unchecked);
+
+       update_widget();
 
        if (auto_commit)
                connect(check_box_, SIGNAL(stateChanged(int)),
@@ -63,6 +61,20 @@ bool Bool::labeled_widget() const
        return true;
 }
 
+void Bool::update_widget()
+{
+       if (!check_box_)
+               return;
+
+       Glib::VariantBase variant = getter_();
+       assert(variant.gobj());
+
+       bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
+               variant).get();
+
+       check_box_->setCheckState(value ? Qt::Checked : Qt::Unchecked);
+}
+
 void Bool::commit()
 {
        assert(setter_);
index ef0916f48523a533d228296549138cd786eb7c29..fd5744fe3830f0d56779b981afca4e85aae2e4ab 100644 (file)
@@ -38,6 +38,7 @@ public:
 
        QWidget* get_widget(QWidget *parent, bool auto_commit);
        bool labeled_widget() const;
+       void update_widget();
 
        void commit();
 
index e1539a127d63dccd9830673a21d66bd07e343f69..57512d723ff26b5cd3473432040e975cf914e8bb 100644 (file)
@@ -58,9 +58,6 @@ QWidget* Double::get_widget(QWidget *parent, bool auto_commit)
        if (!variant.gobj())
                return nullptr;
 
-       double value = Glib::VariantBase::cast_dynamic<Glib::Variant<double>>(
-               variant).get();
-
        spin_box_ = new QDoubleSpinBox(parent);
        spin_box_->setDecimals(decimals_);
        spin_box_->setSuffix(suffix_);
@@ -69,7 +66,7 @@ QWidget* Double::get_widget(QWidget *parent, bool auto_commit)
        if (step_)
                spin_box_->setSingleStep(*step_);
 
-       spin_box_->setValue(value);
+       update_widget();
 
        if (auto_commit)
                connect(spin_box_, SIGNAL(valueChanged(double)),
@@ -78,6 +75,19 @@ QWidget* Double::get_widget(QWidget *parent, bool auto_commit)
        return spin_box_;
 }
 
+void Double::update_widget()
+{
+       if (!spin_box_)
+               return;
+
+       Glib::VariantBase variant = getter_();
+       assert(variant.gobj());
+
+       double value = Glib::VariantBase::cast_dynamic<Glib::Variant<double>>(
+               variant).get();
+       spin_box_->setValue(value);
+}
+
 void Double::commit()
 {
        assert(setter_);
index 4e18e9563ff0adc9c05dc076fb5b57e8cf952409..44e9f7e7cb25b94b508ad1b96457454f5b46c7cc 100644 (file)
@@ -47,6 +47,7 @@ public:
        virtual ~Double() = default;
 
        QWidget* get_widget(QWidget *parent, bool auto_commit);
+       void update_widget();
 
        void commit();
 
index d5d571a9ec487396b921cdae5b45855bb7bbabde..a6a53764ca040b9366d5f589e1667607a6701ea1 100644 (file)
@@ -54,10 +54,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 +65,21 @@ 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];
+               if (v.first.equal(variant))
+                       selector_->setCurrentIndex(i);
+       }
+}
+
 void Enum::commit()
 {
        assert(setter_);
index 0193e238d562b224828bc98ac70b49c105b63dd0..617ff071b3784621aa3ecac67aad215f9f9c775a 100644 (file)
@@ -49,6 +49,7 @@ public:
        virtual ~Enum() = default;
 
        QWidget* get_widget(QWidget *parent, bool auto_commit);
+       void update_widget();
 
        void commit();
 
index 2930a3ea1e8c96b5d0091e6c2b640050ecb5d09f..ce6a040d633c00c1a2583ba6fe9fc85f443f136c 100644 (file)
@@ -47,7 +47,7 @@ Int::Int(QString name,
 
 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
 {
-       int64_t int_val = 0, range_min = 0;
+       int64_t range_min = 0;
        uint64_t range_max = 0;
 
        if (spin_box_)
@@ -69,25 +69,18 @@ QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
        assert(type);
 
        if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) {
-               int_val = g_variant_get_byte(value);
                range_min = 0, range_max = UINT8_MAX;
        } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) {
-               int_val = g_variant_get_int16(value);
                range_min = INT16_MIN, range_max = INT16_MAX;
        } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) {
-               int_val = g_variant_get_uint16(value);
                range_min = 0, range_max = UINT16_MAX;
        } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) {
-               int_val = g_variant_get_int32(value);
                range_min = INT32_MIN, range_max = INT32_MAX;
        } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) {
-               int_val = g_variant_get_uint32(value);
                range_min = 0, range_max = UINT32_MAX;
        } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) {
-               int_val = g_variant_get_int64(value);
                range_min = INT64_MIN, range_max = INT64_MAX;
        } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) {
-               int_val = g_variant_get_uint64(value);
                range_min = 0, range_max = UINT64_MAX;
        } else {
                // Unexpected value type.
@@ -107,7 +100,7 @@ QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
        else
                spin_box_->setRange((int)range_min, (int)range_max);
 
-       spin_box_->setValue((int)int_val);
+       update_widget();
 
        if (auto_commit)
                connect(spin_box_, SIGNAL(valueChanged(int)),
@@ -116,6 +109,42 @@ QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
        return spin_box_;
 }
 
+void Int::update_widget()
+{
+       if (!spin_box_)
+               return;
+
+       value_ = getter_();
+       GVariant *value = value_.gobj();
+       assert(value);
+
+       const GVariantType *const type = g_variant_get_type(value);
+       assert(type);
+
+       int64_t int_val = 0;
+
+       if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE)) {
+               int_val = g_variant_get_byte(value);
+       } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16)) {
+               int_val = g_variant_get_int16(value);
+       } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16)) {
+               int_val = g_variant_get_uint16(value);
+       } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32)) {
+               int_val = g_variant_get_int32(value);
+       } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32)) {
+               int_val = g_variant_get_uint32(value);
+       } else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64)) {
+               int_val = g_variant_get_int64(value);
+       } else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64)) {
+               int_val = g_variant_get_uint64(value);
+       } else {
+               // Unexpected value type.
+               assert(false);
+       }
+
+       spin_box_->setValue((int)int_val);
+}
+
 void Int::commit()
 {
        assert(setter_);
index 709e02e635eda56ee1f195cd4b9f76733c32b560..f6b7c2d1f1642d64a55a575fbcd7100618cd3c46 100644 (file)
@@ -45,6 +45,7 @@ public:
        virtual ~Int() = default;
 
        QWidget* get_widget(QWidget *parent, bool auto_commit);
+       void update_widget();
 
        void commit();
 
index 322c608490c775803eeebe00b189a1140c3ba3c1..ebc6412a0bf0f2746be61b00b63ff30289a34b34 100644 (file)
@@ -53,9 +53,9 @@ public:
        const QString& name() const;
        const QString& desc() const;
 
-       virtual QWidget* get_widget(QWidget *parent,
-               bool auto_commit = false) = 0;
+       virtual QWidget* get_widget(QWidget *parent, bool auto_commit = false) = 0;
        virtual bool labeled_widget() const;
+       virtual void update_widget() = 0;
 
        virtual void commit() = 0;
 
index 96a83422e0a3e2a026336845ac20c72c876e7d35..d93d78eddf72589190cee89a2f0a082c76a9204b 100644 (file)
@@ -52,11 +52,9 @@ QWidget* String::get_widget(QWidget *parent, bool auto_commit)
        if (!variant.gobj())
                return nullptr;
 
-       string value = Glib::VariantBase::cast_dynamic<Glib::Variant<ustring>>(
-               variant).get();
-
        line_edit_ = new QLineEdit(parent);
-       line_edit_->setText(QString::fromStdString(value));
+
+       update_widget();
 
        if (auto_commit)
                connect(line_edit_, SIGNAL(textEdited(const QString&)),
@@ -65,6 +63,20 @@ QWidget* String::get_widget(QWidget *parent, bool auto_commit)
        return line_edit_;
 }
 
+void String::update_widget()
+{
+       if (!line_edit_)
+               return;
+
+       Glib::VariantBase variant = getter_();
+       assert(variant.gobj());
+
+       string value = Glib::VariantBase::cast_dynamic<Glib::Variant<ustring>>(
+               variant).get();
+
+       line_edit_->setText(QString::fromStdString(value));
+}
+
 void String::commit()
 {
        assert(setter_);
index 8ce8be61ef6312f67842b9b4981d5e0d29a0895c..8282cb7f5928616fb2b64da1b5420c6439948458 100644 (file)
@@ -35,6 +35,7 @@ public:
        String(QString name, QString desc, Getter getter, Setter setter);
 
        QWidget* get_widget(QWidget *parent, bool auto_commit);
+       void update_widget();
 
        void commit();
 
index 116dad50aad72a98000f306937bb1df9863e46d3..66f7f06f5d87192a2ea77cbafee44af21cec294c 100644 (file)
@@ -53,7 +53,7 @@ public:
 
        bool eventFilter(QObject *obj, QEvent *event);
 
-       void show();
+       virtual void show();
 
 private:
        bool space_for_arrow() const;