]> sigrok.org Git - pulseview.git/commitdiff
Added code to handle different integer types in the Int property.
authorJoel Holdsworth <redacted>
Sun, 29 Dec 2013 12:56:55 +0000 (13:56 +0100)
committerJoel Holdsworth <redacted>
Sun, 29 Dec 2013 17:53:20 +0000 (18:53 +0100)
This fixes bug #203

CMakeLists.txt
pv/prop/int.cpp
pv/prop/int.h

index 1fa6d5ad4f45809ba77163960533cb6d44c94f09..15a60d1e8722a617f28f78f35f69dcbea008a863 100644 (file)
@@ -226,6 +226,7 @@ include(${QT_USE_FILE})
 #-------------------------------------------------------------------------------
 
 add_definitions(${QT_DEFINITIONS})
+add_definitions(-D__STDC_LIMIT_MACROS)
 add_definitions(-Wall -Wextra)
 
 if(ENABLE_DECODE)
index 6a2cb7d0300b06345cbfa2422e07068812c5d13c..faa3171d85d41b13115ba37c624abfa65be6d4bf 100644 (file)
@@ -18,6 +18,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <stdint.h>
 #include <assert.h>
 
 #include <QSpinBox>
@@ -38,31 +39,93 @@ Int::Int(QString name,
        Property(name, getter, setter),
        _suffix(suffix),
        _range(range),
+       _value(NULL),
        _spin_box(NULL)
 {
 }
 
 Int::~Int()
 {
+       if (_value)
+               g_variant_unref(_value);
 }
 
 QWidget* Int::get_widget(QWidget *parent, bool auto_commit)
 {
+       int64_t int_val = 0, range_min = 0, range_max = 0;
+
        if (_spin_box)
                return _spin_box;
 
+       if (_value)
+               g_variant_unref(_value);
+
+       _value = _getter ? _getter() : NULL;
+       assert(_value);
+       if (!_value)
+               return NULL;
+
        _spin_box = new QSpinBox(parent);
        _spin_box->setSuffix(_suffix);
+
+       const GVariantType *const type = g_variant_get_type(_value);
+       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.
+               assert(0);
+       }
+
+       // @todo Sigrok supports 64-bit quantities, but Qt does not have a
+       // standard widget to allow the values to be modified over the full
+       // 64-bit range on 32-bit machines. To solve the issue we need a
+       // custom widget.
+
+       range_min = max(range_min, (int64_t)INT_MIN);
+       range_max = min(range_max, (int64_t)INT_MAX);
+
        if (_range)
                _spin_box->setRange((int)_range->first, (int)_range->second);
        else
-               _spin_box->setRange(INT_MIN, INT_MAX);
+               _spin_box->setRange((int)range_min, (int)range_max);
 
-       GVariant *const value = _getter ? _getter() : NULL;
-       if (value) {
-               _spin_box->setValue((int)g_variant_get_int64(value));
-               g_variant_unref(value);
-       }
+       _spin_box->setValue((int)int_val);
 
        if (auto_commit)
                connect(_spin_box, SIGNAL(valueChanged(int)),
@@ -78,7 +141,39 @@ void Int::commit()
        if (!_spin_box)
                return;
 
-       _setter(g_variant_new_int64(_spin_box->value()));
+       assert(_value);
+
+       GVariant *new_value = NULL;
+       const GVariantType *const type = g_variant_get_type(_value);
+       assert(type);
+
+       if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTE))
+               new_value = g_variant_new_byte(_spin_box->value());
+       else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT16))
+               new_value = g_variant_new_int16(_spin_box->value());
+       else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT16))
+               new_value = g_variant_new_uint16(_spin_box->value());
+       else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT32))
+               new_value = g_variant_new_int32(_spin_box->value());
+       else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT32))
+               new_value = g_variant_new_int32(_spin_box->value());
+       else if (g_variant_type_equal(type, G_VARIANT_TYPE_INT64))
+               new_value = g_variant_new_int64(_spin_box->value());
+       else if (g_variant_type_equal(type, G_VARIANT_TYPE_UINT64))
+               new_value = g_variant_new_uint64(_spin_box->value());
+       else
+       {
+               // Unexpected value type.
+               assert(0);
+       }
+
+       assert(new_value);
+
+       g_variant_unref(_value);
+       g_variant_ref(new_value);
+       _value = new_value;
+
+       _setter(new_value);
 }
 
 void Int::on_value_changed(int)
index f4c7387983409749354839538356b8bfc11e9c2d..6d910cdf4429b383fe5153bf71b66562d9e2c8c1 100644 (file)
@@ -54,6 +54,7 @@ private:
        const QString _suffix;
        const boost::optional< std::pair<int64_t, int64_t> > _range;
 
+       GVariant *_value;
        QSpinBox *_spin_box;
 };