From: Joel Holdsworth Date: Sat, 12 Oct 2013 11:17:10 +0000 (+0100) Subject: Added auto-commit support to properties X-Git-Tag: pulseview-0.2.0~256 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b1fe148e0dce6e369c9d4770ca935a6a9ec94b59;p=pulseview.git Added auto-commit support to properties --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 43208aae..2ea44dba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -159,6 +159,12 @@ set(pulseview_HEADERS pv/dialogs/connect.h pv/popups/deviceoptions.h pv/popups/probes.h + pv/prop/bool.h + pv/prop/double.h + pv/prop/enum.h + pv/prop/int.h + pv/prop/property.h + pv/prop/string.h pv/toolbars/samplingbar.h pv/view/cursor.h pv/view/decodesignal.h diff --git a/pv/prop/bool.cpp b/pv/prop/bool.cpp index ad9a13e7..179208a5 100644 --- a/pv/prop/bool.cpp +++ b/pv/prop/bool.cpp @@ -40,7 +40,7 @@ Bool::~Bool() { } -QWidget* Bool::get_widget(QWidget *parent) +QWidget* Bool::get_widget(QWidget *parent, bool auto_commit) { if (_check_box) return _check_box; @@ -55,6 +55,10 @@ QWidget* Bool::get_widget(QWidget *parent) g_variant_unref(value); } + if (auto_commit) + connect(_check_box, SIGNAL(stateChanged(int)), + this, SLOT(on_state_changed(int))); + return _check_box; } @@ -74,5 +78,10 @@ void Bool::commit() _check_box->checkState() == Qt::Checked)); } +void Bool::on_state_changed(int) +{ + commit(); +} + } // prop } // pv diff --git a/pv/prop/bool.h b/pv/prop/bool.h index 22fc95cc..0d8e205e 100644 --- a/pv/prop/bool.h +++ b/pv/prop/bool.h @@ -30,16 +30,21 @@ namespace prop { class Bool : public Property { + Q_OBJECT; + public: Bool(QString name, Getter getter, Setter setter); virtual ~Bool(); - QWidget* get_widget(QWidget *parent); + QWidget* get_widget(QWidget *parent, bool auto_commit); bool labeled_widget() const; void commit(); +private slots: + void on_state_changed(int); + private: QCheckBox *_check_box; }; diff --git a/pv/prop/double.cpp b/pv/prop/double.cpp index 39e205ee..188286ae 100644 --- a/pv/prop/double.cpp +++ b/pv/prop/double.cpp @@ -50,7 +50,7 @@ Double::~Double() { } -QWidget* Double::get_widget(QWidget *parent) +QWidget* Double::get_widget(QWidget *parent, bool auto_commit) { if (_spin_box) return _spin_box; @@ -70,6 +70,10 @@ QWidget* Double::get_widget(QWidget *parent) g_variant_unref(value); } + if (auto_commit) + connect(_spin_box, SIGNAL(valueChanged(double)), + this, SLOT(on_value_changed(double))); + return _spin_box; } @@ -83,5 +87,10 @@ void Double::commit() _setter(g_variant_new_double(_spin_box->value())); } +void Double::on_value_changed(double) +{ + commit(); +} + } // prop } // pv diff --git a/pv/prop/double.h b/pv/prop/double.h index 5edb3924..ba4c5958 100644 --- a/pv/prop/double.h +++ b/pv/prop/double.h @@ -34,6 +34,8 @@ namespace prop { class Double : public Property { + Q_OBJECT + public: Double(QString name, int decimals, QString suffix, boost::optional< std::pair > range, @@ -43,10 +45,13 @@ public: virtual ~Double(); - QWidget* get_widget(QWidget *parent); + QWidget* get_widget(QWidget *parent, bool auto_commit); void commit(); +private slots: + void on_value_changed(double); + private: const int _decimals; const QString _suffix; diff --git a/pv/prop/enum.cpp b/pv/prop/enum.cpp index 2249b570..412a31b0 100644 --- a/pv/prop/enum.cpp +++ b/pv/prop/enum.cpp @@ -45,7 +45,7 @@ Enum::~Enum() g_variant_unref(_values[i].first); } -QWidget* Enum::get_widget(QWidget *parent) +QWidget* Enum::get_widget(QWidget *parent, bool auto_commit) { if (_selector) return _selector; @@ -62,6 +62,10 @@ QWidget* Enum::get_widget(QWidget *parent) g_variant_unref(value); + if (auto_commit) + connect(_selector, SIGNAL(currentIndexChanged(int)), + this, SLOT(on_current_item_changed(int))); + return _selector; } @@ -79,5 +83,10 @@ void Enum::commit() _setter((GVariant*)_selector->itemData(index).value()); } +void Enum::on_current_item_changed(int) +{ + commit(); +} + } // prop } // pv diff --git a/pv/prop/enum.h b/pv/prop/enum.h index 402798c0..a7e6ed8d 100644 --- a/pv/prop/enum.h +++ b/pv/prop/enum.h @@ -33,16 +33,21 @@ namespace prop { class Enum : public Property { + Q_OBJECT; + public: Enum(QString name, std::vector > values, Getter getter, Setter setter); virtual ~Enum(); - QWidget* get_widget(QWidget *parent); + QWidget* get_widget(QWidget *parent, bool auto_commit); void commit(); +private slots: + void on_current_item_changed(int); + private: const std::vector< std::pair > _values; diff --git a/pv/prop/int.cpp b/pv/prop/int.cpp index 98936292..6a2cb7d0 100644 --- a/pv/prop/int.cpp +++ b/pv/prop/int.cpp @@ -46,7 +46,7 @@ Int::~Int() { } -QWidget* Int::get_widget(QWidget *parent) +QWidget* Int::get_widget(QWidget *parent, bool auto_commit) { if (_spin_box) return _spin_box; @@ -64,6 +64,10 @@ QWidget* Int::get_widget(QWidget *parent) g_variant_unref(value); } + if (auto_commit) + connect(_spin_box, SIGNAL(valueChanged(int)), + this, SLOT(on_value_changed(int))); + return _spin_box; } @@ -77,5 +81,10 @@ void Int::commit() _setter(g_variant_new_int64(_spin_box->value())); } +void Int::on_value_changed(int) +{ + commit(); +} + } // prop } // pv diff --git a/pv/prop/int.h b/pv/prop/int.h index 279bedc1..f4c73879 100644 --- a/pv/prop/int.h +++ b/pv/prop/int.h @@ -34,6 +34,8 @@ namespace prop { class Int : public Property { + Q_OBJECT; + public: Int(QString name, QString suffix, boost::optional< std::pair > range, @@ -41,10 +43,13 @@ public: virtual ~Int(); - QWidget* get_widget(QWidget *parent); + QWidget* get_widget(QWidget *parent, bool auto_commit); void commit(); +private slots: + void on_value_changed(int); + private: const QString _suffix; const boost::optional< std::pair > _range; diff --git a/pv/prop/property.h b/pv/prop/property.h index 31b90330..22e211b1 100644 --- a/pv/prop/property.h +++ b/pv/prop/property.h @@ -33,8 +33,10 @@ class QWidget; namespace pv { namespace prop { -class Property +class Property : public QObject { + Q_OBJECT; + public: typedef boost::function Getter; typedef boost::function Setter; @@ -45,7 +47,8 @@ protected: public: const QString& name() const; - virtual QWidget* get_widget(QWidget *parent) = 0; + virtual QWidget* get_widget(QWidget *parent, + bool auto_commit = false) = 0; virtual bool labeled_widget() const; virtual void commit() = 0; diff --git a/pv/prop/string.cpp b/pv/prop/string.cpp index b9de789b..b8f3351c 100644 --- a/pv/prop/string.cpp +++ b/pv/prop/string.cpp @@ -38,7 +38,7 @@ String::String(QString name, { } -QWidget* String::get_widget(QWidget *parent) +QWidget* String::get_widget(QWidget *parent, bool auto_commit) { if (_line_edit) return _line_edit; @@ -52,6 +52,10 @@ QWidget* String::get_widget(QWidget *parent) g_variant_unref(value); } + if (auto_commit) + connect(_line_edit, SIGNAL(textEdited(const QString&)), + this, SLOT(on_text_edited(const QString&))); + return _line_edit; } @@ -66,5 +70,10 @@ void String::commit() _setter(g_variant_new_string(ba.data())); } +void String::on_text_edited(const QString&) +{ + commit(); +} + } // prop } // pv diff --git a/pv/prop/string.h b/pv/prop/string.h index bd95d9b9..d54635fe 100644 --- a/pv/prop/string.h +++ b/pv/prop/string.h @@ -30,13 +30,18 @@ namespace prop { class String : public Property { + Q_OBJECT; + public: String(QString name, Getter getter, Setter setter); - QWidget* get_widget(QWidget *parent); + QWidget* get_widget(QWidget *parent, bool auto_commit); void commit(); +private slots: + void on_text_edited(const QString&); + private: QLineEdit *_line_edit; };