]> sigrok.org Git - pulseview.git/commitdiff
Added sample rate selector
authorJoel Holdsworth <redacted>
Sat, 30 Jun 2012 08:41:08 +0000 (09:41 +0100)
committerJoel Holdsworth <redacted>
Mon, 3 Sep 2012 12:59:05 +0000 (13:59 +0100)
samplingbar.cpp
samplingbar.h

index 43b6e5e9eeb1b2f01101d99a1dcba399b298cecb..c411f3c15260dfda8adcefc7e9322c6cfbba6318 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <assert.h>
+
 extern "C" {
 #include <libsigrok/libsigrok.h>
 }
 
+#include <QAction>
+
 #include "samplingbar.h"
 
 SamplingBar::SamplingBar(QWidget *parent) :
        QToolBar("Sampling Bar", parent),
-       _device_selector(this)
+       _device_selector(this),
+       _sample_rate_list(this)
 {
+       connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
+               this, SLOT(on_device_selected()));
+
+       _sample_rate_value.setDecimals(0);
+       _sample_rate_value.setSuffix("Hz");
+
        addWidget(&_device_selector);
+       _sample_rate_list_action = addWidget(&_sample_rate_list);
+       _sample_rate_value_action = addWidget(&_sample_rate_value);
 
        update_device_selector();
+       update_sample_rate_selector();
 }
 
 struct sr_dev_inst* SamplingBar::get_selected_device() const
@@ -43,6 +57,25 @@ struct sr_dev_inst* SamplingBar::get_selected_device() const
                index).value<void*>();
 }
 
+uint64_t SamplingBar::get_sample_rate() const
+{
+       assert(_sample_rate_value_action);
+       assert(_sample_rate_list_action);
+
+       if(_sample_rate_value_action->isVisible())
+               return (uint64_t)_sample_rate_value.value();
+       else if(_sample_rate_list_action->isVisible())
+       {
+               const int index = _device_selector.currentIndex();
+               if(index < 0)
+                       return 0;
+               
+               return _device_selector.itemData(index).value<uint64_t>();
+       }
+
+       return 0;
+}
+
 void SamplingBar::update_device_selector()
 {
        GSList *devices = NULL;
@@ -73,3 +106,43 @@ void SamplingBar::update_device_selector()
 
        g_slist_free(devices);
 }
+
+void SamplingBar::update_sample_rate_selector()
+{
+       const sr_dev_inst *const sdi = get_selected_device();
+       const struct sr_samplerates *samplerates;
+
+       assert(_sample_rate_value_action);
+       assert(_sample_rate_list_action);
+
+       if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES,
+               (const void **)&samplerates, sdi) != SR_OK)
+               return;
+
+       _sample_rate_list_action->setVisible(false);
+       _sample_rate_value_action->setVisible(false);
+
+       if (samplerates->step)
+       {
+               _sample_rate_value.setRange(
+                       samplerates->low, samplerates->high);
+               _sample_rate_value.setSingleStep(samplerates->step);
+               _sample_rate_value_action->setVisible(true);
+       }
+       else
+       {
+               _sample_rate_list.clear();
+               for (const uint64_t *rate = samplerates->list;
+                    *rate; rate++)
+                       _sample_rate_list.addItem(
+                               sr_samplerate_string(*rate),
+                               qVariantFromValue(*rate));
+               _sample_rate_list.show();
+               _sample_rate_list_action->setVisible(true);
+       }
+}
+
+void SamplingBar::on_device_selected()
+{
+       update_sample_rate_selector();
+}
index 99957f977b4ca5062bde5201c8e3d394d1d1ed90..d183dabdebdf55060847128e0f922872dd23f69a 100644 (file)
 #ifndef SAMPLINGBAR_H
 #define SAMPLINGBAR_H
 
+#include <stdint.h>
+
 #include <QComboBox>
+#include <QDoubleSpinBox>
 #include <QToolBar>
+#include <QToolButton>
+
+class QAction;
 
 class SamplingBar : public QToolBar
 {
@@ -32,12 +38,22 @@ public:
        SamplingBar(QWidget *parent);
 
        struct sr_dev_inst* get_selected_device() const;
+       uint64_t get_sample_rate() const;
 
 private:
        void update_device_selector();
+       void update_sample_rate_selector();
+
+private slots:
+       void on_device_selected();
 
 private:
        QComboBox _device_selector;
+
+       QComboBox _sample_rate_list;
+       QAction *_sample_rate_list_action;
+       QDoubleSpinBox _sample_rate_value;
+       QAction *_sample_rate_value_action;
 };
 
 #endif // SAMPLINGBAR_H