]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Hide the configure device button when the popup would be empty
[pulseview.git] / pv / toolbars / samplingbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
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
09f5d123 21#include <extdef.h>
dde1a563 22
09f5d123 23#include <assert.h>
215f9499 24
18203d86
JH
25#include <boost/foreach.hpp>
26
6fb67b27 27#include <libsigrok/libsigrok.h>
6fb67b27 28
dde1a563 29#include <QAction>
48888313 30#include <QDebug>
dde1a563 31
d4984fe7
JH
32#include "samplingbar.h"
33
dd63af74 34#include <pv/devicemanager.h>
51d4a9ab 35#include <pv/popups/deviceoptions.h>
cdb50f67 36
819f4c25 37using std::string;
dd63af74 38
51e77110 39namespace pv {
f4c92e1c 40namespace toolbars {
51e77110 41
09f5d123
JH
42const uint64_t SamplingBar::RecordLengths[20] = {
43 1000,
44 2500,
45 5000,
46 10000,
47 25000,
48 50000,
49 100000,
50 250000,
51 500000,
215f9499
JH
52 1000000,
53 2000000,
54 5000000,
55 10000000,
56 25000000,
57 50000000,
58 100000000,
59 250000000,
60 500000000,
61 1000000000,
d2aad781 62 10000000000ULL,
215f9499
JH
63};
64
09f5d123
JH
65const uint64_t SamplingBar::DefaultRecordLength = 1000000;
66
aca00b1e 67SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
6fb67b27 68 QToolBar("Sampling Bar", parent),
aca00b1e 69 _session(session),
dde1a563 70 _device_selector(this),
95237c18 71 _updating_device_selector(false),
cdb50f67 72 _configure_button(this),
b3e8a5d8 73 _configure_button_action(NULL),
51d4a9ab 74 _probes_button(this),
b7b659aa 75 _probes_popup(_session, this),
215f9499 76 _record_length_selector(this),
32c81aef 77 _sample_rate_action(NULL),
274d4f13 78 _sample_rate_list(this),
2b49eeb0 79 _icon_red(":/icons/status-red.svg"),
f5798068
JH
80 _icon_green(":/icons/status-green.svg"),
81 _icon_grey(":/icons/status-grey.svg"),
274d4f13 82 _run_stop_button(this)
6fb67b27 83{
cdb50f67 84 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 85 this, SLOT(on_run_stop()));
dde1a563
JH
86 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
87 this, SLOT(on_device_selected()));
88
89 _sample_rate_value.setDecimals(0);
90 _sample_rate_value.setSuffix("Hz");
91
9ba4ca35 92 for (size_t i = 0; i < countof(RecordLengths); i++)
215f9499 93 {
09f5d123 94 const uint64_t &l = RecordLengths[i];
215f9499 95 char *const text = sr_si_string_u64(l, " samples");
27e8df22 96 _record_length_selector.addItem(QString::fromUtf8(text),
215f9499
JH
97 qVariantFromValue(l));
98 g_free(text);
09f5d123 99
9ba4ca35 100 if (l == DefaultRecordLength)
09f5d123 101 _record_length_selector.setCurrentIndex(i);
215f9499
JH
102 }
103
2b49eeb0 104 set_capture_state(pv::SigSession::Stopped);
274d4f13 105
688ef645
JH
106 _configure_button.setIcon(QIcon::fromTheme("configure",
107 QIcon(":/icons/configure.png")));
b7b659aa 108
51d4a9ab
JH
109 _probes_button.setIcon(QIcon::fromTheme("probes",
110 QIcon(":/icons/probes.svg")));
b7b659aa 111 _probes_button.set_popup(&_probes_popup);
cdb50f67 112
f5798068
JH
113 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
114
6fb67b27 115 addWidget(&_device_selector);
b3e8a5d8 116 _configure_button_action = addWidget(&_configure_button);
51d4a9ab 117 addWidget(&_probes_button);
215f9499 118 addWidget(&_record_length_selector);
dde1a563
JH
119 _sample_rate_list_action = addWidget(&_sample_rate_list);
120 _sample_rate_value_action = addWidget(&_sample_rate_value);
274d4f13 121 addWidget(&_run_stop_button);
6fb67b27 122
48888313
JH
123 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
124 this, SLOT(on_sample_rate_changed()));
18203d86 125 connect(&_sample_rate_value, SIGNAL(editingFinished()),
48888313 126 this, SLOT(on_sample_rate_changed()));
6fb67b27
JH
127}
128
18203d86
JH
129void SamplingBar::set_device_list(
130 const std::list<struct sr_dev_inst*> &devices)
131{
95237c18
JH
132 _updating_device_selector = true;
133
18203d86
JH
134 _device_selector.clear();
135
136 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
dd63af74
JH
137 const string title = DeviceManager::format_device_title(sdi);
138 _device_selector.addItem(title.c_str(),
139 qVariantFromValue((void*)sdi));
18203d86
JH
140 }
141
95237c18
JH
142 _updating_device_selector = false;
143
b7b659aa 144 on_device_selected();
18203d86
JH
145}
146
6fb67b27 147struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 148{
6fb67b27 149 const int index = _device_selector.currentIndex();
333d5bbc 150 if (index < 0)
6fb67b27
JH
151 return NULL;
152
153 return (sr_dev_inst*)_device_selector.itemData(
154 index).value<void*>();
155}
156
dba73e73
JH
157void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
158{
793f8096
UH
159 for (int i = 0; i < _device_selector.count(); i++)
160 if (sdi == _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
161 _device_selector.setCurrentIndex(i);
162 return;
163 }
164}
165
215f9499
JH
166uint64_t SamplingBar::get_record_length() const
167{
168 const int index = _record_length_selector.currentIndex();
333d5bbc 169 if (index < 0)
215f9499
JH
170 return 0;
171
172 return _record_length_selector.itemData(index).value<uint64_t>();
173}
174
2b49eeb0 175void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 176{
2b49eeb0
JH
177 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
178 _run_stop_button.setIcon(*icons[state]);
179 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
180 tr("Run") : tr("Stop"));
6ac96c2e
JH
181}
182
dde1a563
JH
183void SamplingBar::update_sample_rate_selector()
184{
185 const sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
186 GVariant *gvar_dict, *gvar_list;
187 const uint64_t *elements = NULL;
188 gsize num_elements;
dde1a563
JH
189
190 assert(_sample_rate_value_action);
191 assert(_sample_rate_list_action);
192
ef4d0201
JH
193 if (!sdi)
194 return;
195
68162c29
BV
196 if (sr_config_list(sdi->driver, sdi, NULL,
197 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
dde1a563
JH
198 return;
199
200 _sample_rate_list_action->setVisible(false);
201 _sample_rate_value_action->setVisible(false);
202
488f5d3f
BV
203 if ((gvar_list = g_variant_lookup_value(gvar_dict,
204 "samplerate-steps", G_VARIANT_TYPE("at")))) {
205 elements = (const uint64_t *)g_variant_get_fixed_array(
206 gvar_list, &num_elements, sizeof(uint64_t));
207 _sample_rate_value.setRange(elements[0], elements[1]);
208 _sample_rate_value.setSingleStep(elements[2]);
488f5d3f 209 g_variant_unref(gvar_list);
f9541bde 210
32c81aef 211 _sample_rate_action = _sample_rate_value_action;
dde1a563 212 }
488f5d3f
BV
213 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
214 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 215 {
488f5d3f
BV
216 elements = (const uint64_t *)g_variant_get_fixed_array(
217 gvar_list, &num_elements, sizeof(uint64_t));
dde1a563 218 _sample_rate_list.clear();
488f5d3f
BV
219
220 for (unsigned int i = 0; i < num_elements; i++)
221 {
222 char *const s = sr_samplerate_string(elements[i]);
27e8df22 223 _sample_rate_list.addItem(QString::fromUtf8(s),
488f5d3f
BV
224 qVariantFromValue(elements[i]));
225 g_free(s);
226 }
227
dde1a563 228 _sample_rate_list.show();
488f5d3f 229 g_variant_unref(gvar_list);
f9541bde 230
32c81aef 231 _sample_rate_action = _sample_rate_list_action;
dde1a563 232 }
48888313 233
488f5d3f 234 g_variant_unref(gvar_dict);
48888313 235 update_sample_rate_selector_value();
f9541bde
JH
236
237 // We delay showing the action, so that value change events
238 // are ignored.
32c81aef
JH
239 if (_sample_rate_action)
240 _sample_rate_action->setVisible(true);
48888313
JH
241}
242
243void SamplingBar::update_sample_rate_selector_value()
244{
245 sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
246 GVariant *gvar;
247 uint64_t samplerate;
248
48888313
JH
249 assert(sdi);
250
68162c29
BV
251 if (sr_config_get(sdi->driver, sdi, NULL,
252 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
eb4008a6
JH
253 qDebug() <<
254 "WARNING: Failed to get value of sample rate";
255 return;
256 }
488f5d3f
BV
257 samplerate = g_variant_get_uint64(gvar);
258 g_variant_unref(gvar);
48888313
JH
259
260 assert(_sample_rate_value_action);
261 assert(_sample_rate_list_action);
262
32c81aef 263 if (_sample_rate_action == _sample_rate_value_action)
488f5d3f 264 _sample_rate_value.setValue(samplerate);
32c81aef 265 else if (_sample_rate_action == _sample_rate_list_action)
48888313 266 {
793f8096 267 for (int i = 0; i < _sample_rate_list.count(); i++)
488f5d3f 268 if (samplerate == _sample_rate_list.itemData(
48888313
JH
269 i).value<uint64_t>())
270 _sample_rate_list.setCurrentIndex(i);
271 }
272}
273
274void SamplingBar::commit_sample_rate()
275{
276 uint64_t sample_rate = 0;
277
278 sr_dev_inst *const sdi = get_selected_device();
279 assert(sdi);
280
281 assert(_sample_rate_value_action);
282 assert(_sample_rate_list_action);
283
32c81aef 284 if (_sample_rate_action == _sample_rate_value_action)
48888313 285 sample_rate = (uint64_t)_sample_rate_value.value();
32c81aef 286 else if (_sample_rate_action == _sample_rate_list_action)
48888313
JH
287 {
288 const int index = _sample_rate_list.currentIndex();
289 if (index >= 0)
290 sample_rate = _sample_rate_list.itemData(
291 index).value<uint64_t>();
292 }
293
f9541bde
JH
294 if (sample_rate == 0)
295 return;
296
48888313 297 // Set the samplerate
68162c29 298 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
488f5d3f 299 g_variant_new_uint64(sample_rate)) != SR_OK) {
48888313
JH
300 qDebug() << "Failed to configure samplerate.";
301 return;
302 }
dde1a563
JH
303}
304
305void SamplingBar::on_device_selected()
306{
819f4c25 307 using pv::popups::DeviceOptions;
51d4a9ab 308
95237c18
JH
309 if (_updating_device_selector)
310 return;
311
dde1a563 312 update_sample_rate_selector();
51d4a9ab
JH
313
314 sr_dev_inst *const sdi = get_selected_device();
aca00b1e 315 _session.set_device(sdi);
51d4a9ab 316
b3e8a5d8
JH
317 // Update the configure popup
318 DeviceOptions *const opts = new DeviceOptions(sdi, this);
319 _configure_button_action->setVisible(
320 !opts->binding().properties().empty());
321 _configure_button.set_popup(opts);
dde1a563 322}
51e77110 323
48888313
JH
324void SamplingBar::on_sample_rate_changed()
325{
326 commit_sample_rate();
327}
328
9f3d12f3
JH
329void SamplingBar::on_run_stop()
330{
331 commit_sample_rate();
332 run_stop();
333}
334
f4c92e1c 335} // namespace toolbars
51e77110 336} // namespace pv