From: Soeren Apel Date: Sun, 6 May 2018 17:40:10 +0000 (+0200) Subject: Update property widgets before showing device config popup X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=dbed5609ae31cdfc3e9db10f3ab91b7607c08372;p=pulseview.git Update property widgets before showing device config popup --- diff --git a/pv/binding/binding.cpp b/pv/binding/binding.cpp index ae4f2943..ddf0c19a 100644 --- a/pv/binding/binding.cpp +++ b/pv/binding/binding.cpp @@ -54,8 +54,7 @@ void Binding::add_properties_to_form(QFormLayout *layout, for (shared_ptr 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 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>( - gvar).get()); + Glib::VariantBase::cast_dynamic>(gvar).get()); else s = QString::fromStdString(gvar.print()); diff --git a/pv/binding/binding.hpp b/pv/binding/binding.hpp index e672ff17..6e92b083 100644 --- a/pv/binding/binding.hpp +++ b/pv/binding/binding.hpp @@ -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: diff --git a/pv/popups/deviceoptions.cpp b/pv/popups/deviceoptions.cpp index df911367..bfb702cf 100644 --- a/pv/popups/deviceoptions.cpp +++ b/pv/popups/deviceoptions.cpp @@ -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 diff --git a/pv/popups/deviceoptions.hpp b/pv/popups/deviceoptions.hpp index 21f88e90..52b2c5a5 100644 --- a/pv/popups/deviceoptions.hpp +++ b/pv/popups/deviceoptions.hpp @@ -44,6 +44,8 @@ public: pv::binding::Device& binding(); + virtual void show(); + private: shared_ptr device_; diff --git a/pv/prop/bool.cpp b/pv/prop/bool.cpp index 0dd1c50f..6c89741f 100644 --- a/pv/prop/bool.cpp +++ b/pv/prop/bool.cpp @@ -44,12 +44,10 @@ QWidget* Bool::get_widget(QWidget *parent, bool auto_commit) if (!variant.gobj()) return nullptr; - bool value = Glib::VariantBase::cast_dynamic>( - 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>( + variant).get(); + + check_box_->setCheckState(value ? Qt::Checked : Qt::Unchecked); +} + void Bool::commit() { assert(setter_); diff --git a/pv/prop/bool.hpp b/pv/prop/bool.hpp index ef0916f4..fd5744fe 100644 --- a/pv/prop/bool.hpp +++ b/pv/prop/bool.hpp @@ -38,6 +38,7 @@ public: QWidget* get_widget(QWidget *parent, bool auto_commit); bool labeled_widget() const; + void update_widget(); void commit(); diff --git a/pv/prop/double.cpp b/pv/prop/double.cpp index e1539a12..57512d72 100644 --- a/pv/prop/double.cpp +++ b/pv/prop/double.cpp @@ -58,9 +58,6 @@ QWidget* Double::get_widget(QWidget *parent, bool auto_commit) if (!variant.gobj()) return nullptr; - double value = Glib::VariantBase::cast_dynamic>( - 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>( + variant).get(); + spin_box_->setValue(value); +} + void Double::commit() { assert(setter_); diff --git a/pv/prop/double.hpp b/pv/prop/double.hpp index 4e18e956..44e9f7e7 100644 --- a/pv/prop/double.hpp +++ b/pv/prop/double.hpp @@ -47,6 +47,7 @@ public: virtual ~Double() = default; QWidget* get_widget(QWidget *parent, bool auto_commit); + void update_widget(); void commit(); diff --git a/pv/prop/enum.cpp b/pv/prop/enum.cpp index d5d571a9..a6a53764 100644 --- a/pv/prop/enum.cpp +++ b/pv/prop/enum.cpp @@ -54,10 +54,10 @@ QWidget* Enum::get_widget(QWidget *parent, bool auto_commit) for (unsigned int i = 0; i < values_.size(); i++) { const pair &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 &v = values_[i]; + if (v.first.equal(variant)) + selector_->setCurrentIndex(i); + } +} + void Enum::commit() { assert(setter_); diff --git a/pv/prop/enum.hpp b/pv/prop/enum.hpp index 0193e238..617ff071 100644 --- a/pv/prop/enum.hpp +++ b/pv/prop/enum.hpp @@ -49,6 +49,7 @@ public: virtual ~Enum() = default; QWidget* get_widget(QWidget *parent, bool auto_commit); + void update_widget(); void commit(); diff --git a/pv/prop/int.cpp b/pv/prop/int.cpp index 2930a3ea..ce6a040d 100644 --- a/pv/prop/int.cpp +++ b/pv/prop/int.cpp @@ -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_); diff --git a/pv/prop/int.hpp b/pv/prop/int.hpp index 709e02e6..f6b7c2d1 100644 --- a/pv/prop/int.hpp +++ b/pv/prop/int.hpp @@ -45,6 +45,7 @@ public: virtual ~Int() = default; QWidget* get_widget(QWidget *parent, bool auto_commit); + void update_widget(); void commit(); diff --git a/pv/prop/property.hpp b/pv/prop/property.hpp index 322c6084..ebc6412a 100644 --- a/pv/prop/property.hpp +++ b/pv/prop/property.hpp @@ -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; diff --git a/pv/prop/string.cpp b/pv/prop/string.cpp index 96a83422..d93d78ed 100644 --- a/pv/prop/string.cpp +++ b/pv/prop/string.cpp @@ -52,11 +52,9 @@ QWidget* String::get_widget(QWidget *parent, bool auto_commit) if (!variant.gobj()) return nullptr; - string value = Glib::VariantBase::cast_dynamic>( - 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>( + variant).get(); + + line_edit_->setText(QString::fromStdString(value)); +} + void String::commit() { assert(setter_); diff --git a/pv/prop/string.hpp b/pv/prop/string.hpp index 8ce8be61..8282cb7f 100644 --- a/pv/prop/string.hpp +++ b/pv/prop/string.hpp @@ -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(); diff --git a/pv/widgets/popup.hpp b/pv/widgets/popup.hpp index 116dad50..66f7f06f 100644 --- a/pv/widgets/popup.hpp +++ b/pv/widgets/popup.hpp @@ -53,7 +53,7 @@ public: bool eventFilter(QObject *obj, QEvent *event); - void show(); + virtual void show(); private: bool space_for_arrow() const;