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