Created pv::widgets::SampleTimingWidget, and ported the SamplingBar to use it.
authorJoel Holdsworth <joel@airwebreathe.org.uk>
Mon, 30 Dec 2013 13:02:06 +0000 (14:02 +0100)
committerJoel Holdsworth <joel@airwebreathe.org.uk>
Sat, 18 Jan 2014 22:32:36 +0000 (22:32 +0000)
This widget allows the user to edit a sample timing value either from
a list of options, or as an value with a min/max/step, or as read
only, or hidden.

CMakeLists.txt
pv/toolbars/samplingbar.cpp
pv/toolbars/samplingbar.h
pv/widgets/sweeptimingwidget.cpp [new file with mode: 0644]
pv/widgets/sweeptimingwidget.h [new file with mode: 0644]

index 5542e2868b3dc96f2d194d19e8fba46063661999..2ba907af96be14167535429551873a751d639c04 100644 (file)
@@ -146,6 +146,7 @@ set(pulseview_SOURCES
        pv/widgets/colourpopup.cpp
        pv/widgets/popup.cpp
        pv/widgets/popuptoolbutton.cpp
+       pv/widgets/sweeptimingwidget.cpp
        pv/widgets/wellarray.cpp
 )
 
@@ -181,6 +182,7 @@ set(pulseview_HEADERS
        pv/widgets/colourpopup.h
        pv/widgets/popup.h
        pv/widgets/popuptoolbutton.h
+       pv/widgets/sweeptimingwidget.h
        pv/widgets/wellarray.h
 )
 
index 778f7aa1cdf747a74d00995da7d051e5a1ba7deb..06c47fd3efa5f40b4369b75cccfc673bd8cde431 100644 (file)
@@ -74,8 +74,8 @@ SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
        _configure_button_action(NULL),
        _probes_button(this),
        _record_length_selector(this),
-       _sample_rate_action(NULL),
-       _sample_rate_list(this),
+       _sample_rate("Hz", this),
+       _updating_sample_rate(false),
        _icon_red(":/icons/status-red.svg"),
        _icon_green(":/icons/status-green.svg"),
        _icon_grey(":/icons/status-grey.svg"),
@@ -85,9 +85,8 @@ SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
                this, SLOT(on_run_stop()));
        connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
                this, SLOT(on_device_selected()));
-
-       _sample_rate_value.setDecimals(0);
-       _sample_rate_value.setSuffix("Hz");
+       connect(&_sample_rate, SIGNAL(value_changed()),
+               this, SLOT(on_sample_rate_changed()));
 
        for (size_t i = 0; i < countof(RecordLengths); i++)
        {
@@ -115,14 +114,9 @@ SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
        _configure_button_action = addWidget(&_configure_button);
        addWidget(&_probes_button);
        addWidget(&_record_length_selector);
-       _sample_rate_list_action = addWidget(&_sample_rate_list);
-       _sample_rate_value_action = addWidget(&_sample_rate_value);
-       addWidget(&_run_stop_button);
+       addWidget(&_sample_rate);
 
-       connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
-               this, SLOT(on_sample_rate_changed()));
-       connect(&_sample_rate_value, SIGNAL(editingFinished()),
-               this, SLOT(on_sample_rate_changed()));
+       addWidget(&_run_stop_button);
 }
 
 void SamplingBar::set_device_list(
@@ -186,57 +180,40 @@ void SamplingBar::update_sample_rate_selector()
        const uint64_t *elements = NULL;
        gsize num_elements;
 
-       assert(_sample_rate_value_action);
-       assert(_sample_rate_list_action);
-
        if (!sdi)
                return;
 
+       _updating_sample_rate = true;
+
        if (sr_config_list(sdi->driver, sdi, NULL,
                        SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
+       {
+               _sample_rate.show_none();
+               _updating_sample_rate = false;
                return;
-
-       _sample_rate_list_action->setVisible(false);
-       _sample_rate_value_action->setVisible(false);
+       }
 
        if ((gvar_list = g_variant_lookup_value(gvar_dict,
-                       "samplerate-steps", G_VARIANT_TYPE("at")))) {
+                       "samplerate-steps", G_VARIANT_TYPE("at"))))
+       {
                elements = (const uint64_t *)g_variant_get_fixed_array(
                                gvar_list, &num_elements, sizeof(uint64_t));
-               _sample_rate_value.setRange(elements[0], elements[1]);
-               _sample_rate_value.setSingleStep(elements[2]);
+               _sample_rate.show_min_max_step(elements[0], elements[1],
+                       elements[2]);
                g_variant_unref(gvar_list);
-
-               _sample_rate_action = _sample_rate_value_action;
        }
        else if ((gvar_list = g_variant_lookup_value(gvar_dict,
                        "samplerates", G_VARIANT_TYPE("at"))))
        {
                elements = (const uint64_t *)g_variant_get_fixed_array(
                                gvar_list, &num_elements, sizeof(uint64_t));
-               _sample_rate_list.clear();
-
-               for (unsigned int i = 0; i < num_elements; i++)
-               {
-                       char *const s = sr_samplerate_string(elements[i]);
-                       _sample_rate_list.addItem(QString::fromUtf8(s),
-                               qVariantFromValue(elements[i]));
-                       g_free(s);
-               }
-
-               _sample_rate_list.show();
+               _sample_rate.show_list(elements, num_elements);
                g_variant_unref(gvar_list);
-
-               _sample_rate_action = _sample_rate_list_action;
        }
+       _updating_sample_rate = false;
 
        g_variant_unref(gvar_dict);
        update_sample_rate_selector_value();
-
-       // We delay showing the action, so that value change events
-       // are ignored.
-       if (_sample_rate_action)
-               _sample_rate_action->setVisible(true);
 }
 
 void SamplingBar::update_sample_rate_selector_value()
@@ -256,18 +233,9 @@ void SamplingBar::update_sample_rate_selector_value()
        samplerate = g_variant_get_uint64(gvar);
        g_variant_unref(gvar);
 
-       assert(_sample_rate_value_action);
-       assert(_sample_rate_list_action);
-
-       if (_sample_rate_action == _sample_rate_value_action)
-               _sample_rate_value.setValue(samplerate);
-       else if (_sample_rate_action == _sample_rate_list_action)
-       {
-               for (int i = 0; i < _sample_rate_list.count(); i++)
-                       if (samplerate == _sample_rate_list.itemData(
-                               i).value<uint64_t>())
-                               _sample_rate_list.setCurrentIndex(i);
-       }
+       _updating_sample_rate = true;
+       _sample_rate.set_value(samplerate);
+       _updating_sample_rate = false;
 }
 
 void SamplingBar::commit_sample_rate()
@@ -277,19 +245,7 @@ void SamplingBar::commit_sample_rate()
        sr_dev_inst *const sdi = get_selected_device();
        assert(sdi);
 
-       assert(_sample_rate_value_action);
-       assert(_sample_rate_list_action);
-
-       if (_sample_rate_action == _sample_rate_value_action)
-               sample_rate = (uint64_t)_sample_rate_value.value();
-       else if (_sample_rate_action == _sample_rate_list_action)
-       {
-               const int index = _sample_rate_list.currentIndex();
-               if (index >= 0)
-                       sample_rate = _sample_rate_list.itemData(
-                               index).value<uint64_t>();
-       }
-
+       sample_rate = _sample_rate.value();
        if (sample_rate == 0)
                return;
 
@@ -326,7 +282,8 @@ void SamplingBar::on_device_selected()
 
 void SamplingBar::on_sample_rate_changed()
 {
-       commit_sample_rate();
+       if (!_updating_sample_rate)
+               commit_sample_rate();
 }
 
 void SamplingBar::on_run_stop()
index 5821a01091a7eb59d6c7d1762a5ffef184b9c28d..aef26ef145c3882e73273ec9d52eb4aca3bf2646 100644 (file)
@@ -32,6 +32,7 @@
 
 #include <pv/sigsession.h>
 #include <pv/widgets/popuptoolbutton.h>
+#include <pv/widgets/sweeptimingwidget.h>
 
 struct st_dev_inst;
 class QAction;
@@ -88,11 +89,8 @@ private:
 
        QComboBox _record_length_selector;
 
-       QAction *_sample_rate_action;
-       QComboBox _sample_rate_list;
-       QAction *_sample_rate_list_action;
-       QDoubleSpinBox _sample_rate_value;
-       QAction *_sample_rate_value_action;
+       pv::widgets::SweepTimingWidget _sample_rate;
+       bool _updating_sample_rate;
 
        QIcon _icon_red;
        QIcon _icon_green;
diff --git a/pv/widgets/sweeptimingwidget.cpp b/pv/widgets/sweeptimingwidget.cpp
new file mode 100644 (file)
index 0000000..b116489
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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
+ */
+
+#include "sweeptimingwidget.h"
+
+#include <assert.h>
+
+namespace pv {
+namespace widgets {
+
+SweepTimingWidget::SweepTimingWidget(const char *suffix,
+       QWidget *parent) :
+       QWidget(parent),
+       _layout(this),
+       _read_only_value(this),
+       _value(this),
+       _list(this),
+       _value_type(None)
+{
+       setContentsMargins(0, 0, 0, 0);
+
+       _value.setDecimals(0);
+       _value.setSuffix(QString::fromUtf8(suffix));
+
+       connect(&_list, SIGNAL(currentIndexChanged(int)),
+               this, SIGNAL(value_changed()));
+       connect(&_value, SIGNAL(editingFinished()),
+               this, SIGNAL(value_changed()));
+
+       setLayout(&_layout);
+       _layout.setMargin(0);
+       _layout.addWidget(&_read_only_value);
+       _layout.addWidget(&_list);
+       _layout.addWidget(&_value);
+
+       show_none();
+}
+
+void SweepTimingWidget::show_none()
+{
+       _value_type = None;
+       _read_only_value.hide();
+       _value.hide();
+       _list.hide();
+}
+
+void SweepTimingWidget::show_read_only()
+{
+       _value_type = ReadOnly;
+       _read_only_value.show();
+       _value.hide();
+       _list.hide();
+}
+
+void SweepTimingWidget::show_min_max_step(uint64_t min, uint64_t max,
+       uint64_t step)
+{
+       _value_type = MinMaxStep;
+
+       _value.setRange(min, max);
+       _value.setSingleStep(step);
+
+       _read_only_value.hide();
+       _value.show();
+       _list.hide();
+}
+
+void SweepTimingWidget::show_list(const uint64_t *vals, size_t count)
+{
+       _value_type = List;
+
+       _list.clear();
+       for (size_t i = 0; i < count; i++)
+       {
+               char *const s = sr_samplerate_string(vals[i]);
+               _list.addItem(QString::fromUtf8(s),
+                       qVariantFromValue(vals[i]));
+               g_free(s);
+       }
+
+       _read_only_value.hide();
+       _value.hide();
+       _list.show();
+}
+
+uint64_t SweepTimingWidget::value() const
+{
+       switch(_value_type)
+       {
+       case None:
+       case ReadOnly:
+               return 0;
+
+       case MinMaxStep:
+               return (uint64_t)_value.value();
+
+       case List:
+       {
+               const int index = _list.currentIndex();
+               return (index >= 0) ? _list.itemData(
+                       index).value<uint64_t>() : 0;
+       }
+
+       default:
+               // Unexpected value type
+               assert(0);
+               return 0;
+       }
+}
+
+void SweepTimingWidget::set_value(uint64_t value)
+{
+       _read_only_value.setText(QString("%1").arg(value));
+
+       _value.setValue(value);
+
+       for (int i = 0; i < _list.count(); i++)
+               if (value == _list.itemData(i).value<uint64_t>())
+                       _list.setCurrentIndex(i);
+}
+
+} // widgets
+} // pv
diff --git a/pv/widgets/sweeptimingwidget.h b/pv/widgets/sweeptimingwidget.h
new file mode 100644 (file)
index 0000000..c4bfe37
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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
+ */
+
+#ifndef PULSEVIEW_PV_WIDGETS_SWEEPTIMINGWIDGET_H
+#define PULSEVIEW_PV_WIDGETS_SWEEPTIMINGWIDGET_H
+
+#include <libsigrok/libsigrok.h>
+
+#include <QComboBox>
+#include <QDoubleSpinBox>
+#include <QHBoxLayout>
+#include <QLineEdit>
+#include <QWidget>
+
+namespace pv {
+namespace widgets {
+
+class SweepTimingWidget : public QWidget
+{
+       Q_OBJECT
+
+private:
+       enum ValueType
+       {
+               None,
+               ReadOnly,
+               MinMaxStep,
+               List
+       };
+
+public:
+       SweepTimingWidget(const char *suffix, QWidget *parent = NULL);
+
+       void show_none();
+       void show_read_only();
+       void show_min_max_step(uint64_t min, uint64_t max, uint64_t step);
+       void show_list(const uint64_t *vals, size_t count);
+
+       uint64_t value() const;
+       void set_value(uint64_t value);
+
+signals:
+       void value_changed();
+
+private:
+       QHBoxLayout _layout;
+
+       QLineEdit _read_only_value;
+       QDoubleSpinBox _value;
+       QComboBox _list;
+
+       ValueType _value_type;
+};
+
+} // widgets
+} // pv
+
+#endif // PULSEVIEW_PV_WIDGETS_SWEEPTIMINGWIDGET_H