X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fprop%2Fenum.cpp;h=1f542b74a73d6da49552a08236903ce9dad1b3fe;hp=29f95d8ebcf9d19417a0fe4f8806b512b9f7b615;hb=a2f96263f56e019f811a7fc2eba3129ede1d9a5f;hpb=820c3dea881d351f2bbf4e03c2302f5ca645df63 diff --git a/pv/prop/enum.cpp b/pv/prop/enum.cpp index 29f95d8e..1f542b74 100644 --- a/pv/prop/enum.cpp +++ b/pv/prop/enum.cpp @@ -14,39 +14,114 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * along with this program; if not, see . */ +#include +#include +#include + #include -#include "enum.h" +#include "enum.hpp" -using namespace std; +using std::abs; +using std::pair; +using std::vector; namespace pv { namespace prop { -Enum::Enum(QString name, vector< pair > values) : - Property(name), - _values(values), - _selector(NULL) +Enum::Enum(QString name, QString desc, + vector > values, + Getter getter, Setter setter) : + Property(name, desc, getter, setter), + values_(values), + selector_(nullptr) { } -QWidget* Enum::get_widget(QWidget *parent) +QWidget* Enum::get_widget(QWidget *parent, bool auto_commit) { - if(_selector) - return _selector; + if (selector_) + return selector_; + + if (!getter_) + return nullptr; + + Glib::VariantBase variant = getter_(); + if (!variant.gobj()) + return nullptr; + + selector_ = new QComboBox(parent); + for (unsigned int i = 0; i < values_.size(); i++) { + const pair &v = values_[i]; + selector_->addItem(v.second, qVariantFromValue(v.first)); + } + + update_widget(); + + if (auto_commit) + connect(selector_, SIGNAL(currentIndexChanged(int)), + this, SLOT(on_current_item_changed(int))); + + return selector_; +} + +void Enum::update_widget() +{ + if (!selector_) + return; + + Glib::VariantBase variant = getter_(); + assert(variant.gobj()); - _selector = new QComboBox(parent); - for(vector< pair >::const_iterator i = _values.begin(); - i != _values.end(); i++) - _selector->addItem((*i).second, - qVariantFromValue((*i).first)); + for (unsigned int i = 0; i < values_.size(); i++) { + const pair &v = values_[i]; - return _selector; + // 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_); + + if (!selector_) + return; + + const int index = selector_->currentIndex(); + if (index < 0) + return; + + setter_(selector_->itemData(index).value()); +} + +void Enum::on_current_item_changed(int) +{ + commit(); } -} // prop -} // pv +} // namespace prop +} // namespace pv