]> sigrok.org Git - pulseview.git/blob - samplingbar.cpp
c411f3c15260dfda8adcefc7e9322c6cfbba6318
[pulseview.git] / samplingbar.cpp
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <assert.h>
22
23 extern "C" {
24 #include <libsigrok/libsigrok.h>
25 }
26
27 #include <QAction>
28
29 #include "samplingbar.h"
30
31 SamplingBar::SamplingBar(QWidget *parent) :
32         QToolBar("Sampling Bar", parent),
33         _device_selector(this),
34         _sample_rate_list(this)
35 {
36         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
37                 this, SLOT(on_device_selected()));
38
39         _sample_rate_value.setDecimals(0);
40         _sample_rate_value.setSuffix("Hz");
41
42         addWidget(&_device_selector);
43         _sample_rate_list_action = addWidget(&_sample_rate_list);
44         _sample_rate_value_action = addWidget(&_sample_rate_value);
45
46         update_device_selector();
47         update_sample_rate_selector();
48 }
49
50 struct sr_dev_inst* SamplingBar::get_selected_device() const
51 {
52         const int index = _device_selector.currentIndex();
53         if(index < 0)
54                 return NULL;
55
56         return (sr_dev_inst*)_device_selector.itemData(
57                 index).value<void*>();
58 }
59
60 uint64_t SamplingBar::get_sample_rate() const
61 {
62         assert(_sample_rate_value_action);
63         assert(_sample_rate_list_action);
64
65         if(_sample_rate_value_action->isVisible())
66                 return (uint64_t)_sample_rate_value.value();
67         else if(_sample_rate_list_action->isVisible())
68         {
69                 const int index = _device_selector.currentIndex();
70                 if(index < 0)
71                         return 0;
72                 
73                 return _device_selector.itemData(index).value<uint64_t>();
74         }
75
76         return 0;
77 }
78
79 void SamplingBar::update_device_selector()
80 {
81         GSList *devices = NULL;
82
83         /* Scan all drivers for all devices. */
84         struct sr_dev_driver **const drivers = sr_driver_list();
85         for (struct sr_dev_driver **driver = drivers; *driver; driver++) {
86                 GSList *tmpdevs = sr_driver_scan(*driver, NULL);
87                 for (GSList *l = tmpdevs; l; l = l->next)
88                         devices = g_slist_append(devices, l->data);
89                 g_slist_free(tmpdevs);
90         }
91
92         for (GSList *l = devices; l; l = l->next) {
93                 sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
94
95                 QString title;
96                 if (sdi->vendor && sdi->vendor[0])
97                         title += sdi->vendor + QString(" ");
98                 if (sdi->model && sdi->model[0])
99                         title += sdi->model + QString(" ");
100                 if (sdi->version && sdi->version[0])
101                         title += sdi->version + QString(" ");
102
103                 _device_selector.addItem(title, qVariantFromValue(
104                         (void*)sdi));
105         }
106
107         g_slist_free(devices);
108 }
109
110 void SamplingBar::update_sample_rate_selector()
111 {
112         const sr_dev_inst *const sdi = get_selected_device();
113         const struct sr_samplerates *samplerates;
114
115         assert(_sample_rate_value_action);
116         assert(_sample_rate_list_action);
117
118         if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES,
119                 (const void **)&samplerates, sdi) != SR_OK)
120                 return;
121
122         _sample_rate_list_action->setVisible(false);
123         _sample_rate_value_action->setVisible(false);
124
125         if (samplerates->step)
126         {
127                 _sample_rate_value.setRange(
128                         samplerates->low, samplerates->high);
129                 _sample_rate_value.setSingleStep(samplerates->step);
130                 _sample_rate_value_action->setVisible(true);
131         }
132         else
133         {
134                 _sample_rate_list.clear();
135                 for (const uint64_t *rate = samplerates->list;
136                      *rate; rate++)
137                         _sample_rate_list.addItem(
138                                 sr_samplerate_string(*rate),
139                                 qVariantFromValue(*rate));
140                 _sample_rate_list.show();
141                 _sample_rate_list_action->setVisible(true);
142         }
143 }
144
145 void SamplingBar::on_device_selected()
146 {
147         update_sample_rate_selector();
148 }