]> sigrok.org Git - pulseview.git/blob - samplingbar.cpp
Initial work moving ruler into the pv::view::Ruler widget
[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 #include <boost/foreach.hpp>
24
25 extern "C" {
26 #include <libsigrok/libsigrok.h>
27 }
28
29 #include <QAction>
30
31 #include "samplingbar.h"
32
33 const uint64_t SamplingBar::RecordLengths[11] = {
34         1000000,
35         2000000,
36         5000000,
37         10000000,
38         25000000,
39         50000000,
40         100000000,
41         250000000,
42         500000000,
43         1000000000,
44         10000000000
45 };
46
47 SamplingBar::SamplingBar(QWidget *parent) :
48         QToolBar("Sampling Bar", parent),
49         _device_selector(this),
50         _record_length_selector(this),
51         _sample_rate_list(this),
52         _run_stop_button(this)
53 {
54         connect(&_run_stop_button, SIGNAL(clicked()), this, SIGNAL(run_stop()));
55         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
56                 this, SLOT(on_device_selected()));
57
58         _sample_rate_value.setDecimals(0);
59         _sample_rate_value.setSuffix("Hz");
60
61         BOOST_FOREACH(uint64_t l, RecordLengths)
62         {
63                 char *const text = sr_si_string_u64(l, " samples");
64                 _record_length_selector.addItem(QString(text),
65                         qVariantFromValue(l));
66                 g_free(text);
67         }
68
69         _run_stop_button.setText("Run");
70
71         addWidget(&_device_selector);
72         addWidget(&_record_length_selector);
73         _sample_rate_list_action = addWidget(&_sample_rate_list);
74         _sample_rate_value_action = addWidget(&_sample_rate_value);
75         addWidget(&_run_stop_button);
76
77         update_device_selector();
78         update_sample_rate_selector();
79 }
80
81 struct sr_dev_inst* SamplingBar::get_selected_device() const
82 {
83         const int index = _device_selector.currentIndex();
84         if(index < 0)
85                 return NULL;
86
87         return (sr_dev_inst*)_device_selector.itemData(
88                 index).value<void*>();
89 }
90
91 uint64_t SamplingBar::get_record_length() const
92 {
93         const int index = _record_length_selector.currentIndex();
94         if(index < 0)
95                 return 0;
96
97         return _record_length_selector.itemData(index).value<uint64_t>();
98 }
99
100 uint64_t SamplingBar::get_sample_rate() const
101 {
102         assert(_sample_rate_value_action);
103         assert(_sample_rate_list_action);
104
105         if(_sample_rate_value_action->isVisible())
106                 return (uint64_t)_sample_rate_value.value();
107         else if(_sample_rate_list_action->isVisible())
108         {
109                 const int index = _device_selector.currentIndex();
110                 if(index < 0)
111                         return 0;
112                 
113                 return _device_selector.itemData(index).value<uint64_t>();
114         }
115
116         return 0;
117 }
118
119 void SamplingBar::update_device_selector()
120 {
121         GSList *devices = NULL;
122
123         /* Scan all drivers for all devices. */
124         struct sr_dev_driver **const drivers = sr_driver_list();
125         for (struct sr_dev_driver **driver = drivers; *driver; driver++) {
126                 GSList *tmpdevs = sr_driver_scan(*driver, NULL);
127                 for (GSList *l = tmpdevs; l; l = l->next)
128                         devices = g_slist_append(devices, l->data);
129                 g_slist_free(tmpdevs);
130         }
131
132         for (GSList *l = devices; l; l = l->next) {
133                 sr_dev_inst *const sdi = (sr_dev_inst*)l->data;
134
135                 QString title;
136                 if (sdi->vendor && sdi->vendor[0])
137                         title += sdi->vendor + QString(" ");
138                 if (sdi->model && sdi->model[0])
139                         title += sdi->model + QString(" ");
140                 if (sdi->version && sdi->version[0])
141                         title += sdi->version + QString(" ");
142
143                 _device_selector.addItem(title, qVariantFromValue(
144                         (void*)sdi));
145         }
146
147         g_slist_free(devices);
148 }
149
150 void SamplingBar::update_sample_rate_selector()
151 {
152         const sr_dev_inst *const sdi = get_selected_device();
153         const struct sr_samplerates *samplerates;
154
155         assert(_sample_rate_value_action);
156         assert(_sample_rate_list_action);
157
158         if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES,
159                 (const void **)&samplerates, sdi) != SR_OK)
160                 return;
161
162         _sample_rate_list_action->setVisible(false);
163         _sample_rate_value_action->setVisible(false);
164
165         if (samplerates->step)
166         {
167                 _sample_rate_value.setRange(
168                         samplerates->low, samplerates->high);
169                 _sample_rate_value.setSingleStep(samplerates->step);
170                 _sample_rate_value_action->setVisible(true);
171         }
172         else
173         {
174                 _sample_rate_list.clear();
175                 for (const uint64_t *rate = samplerates->list;
176                      *rate; rate++)
177                         _sample_rate_list.addItem(
178                                 sr_samplerate_string(*rate),
179                                 qVariantFromValue(*rate));
180                 _sample_rate_list.show();
181                 _sample_rate_list_action->setVisible(true);
182         }
183 }
184
185 void SamplingBar::on_device_selected()
186 {
187         update_sample_rate_selector();
188 }