]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Use a generic approach when adding the "close on enter" hook for popups
[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
dde1a563 25#include <QAction>
48888313 26#include <QDebug>
40065ab6
JS
27#include <QHelpEvent>
28#include <QToolTip>
dde1a563 29
d4984fe7
JH
30#include "samplingbar.h"
31
dd63af74 32#include <pv/devicemanager.h>
94574501 33#include <pv/device/devinst.h>
51d4a9ab 34#include <pv/popups/deviceoptions.h>
6ac6242b 35#include <pv/popups/channels.h>
62974f45 36#include <pv/util.h>
cdb50f67 37
19adbc2c 38using std::map;
b50ef520
JH
39using std::max;
40using std::min;
f9abf97e 41using std::shared_ptr;
819f4c25 42using std::string;
dd63af74 43
51e77110 44namespace pv {
f4c92e1c 45namespace toolbars {
51e77110 46
b50ef520
JH
47const uint64_t SamplingBar::MinSampleCount = 100ULL;
48const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
49const uint64_t SamplingBar::DefaultSampleCount = 1000000;
09f5d123 50
aca00b1e 51SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
6fb67b27 52 QToolBar("Sampling Bar", parent),
aca00b1e 53 _session(session),
dde1a563 54 _device_selector(this),
95237c18 55 _updating_device_selector(false),
cdb50f67 56 _configure_button(this),
b3e8a5d8 57 _configure_button_action(NULL),
6ac6242b 58 _channels_button(this),
124d97de 59 _sample_count(" samples", this),
1198b887
JH
60 _sample_rate("Hz", this),
61 _updating_sample_rate(false),
85756012 62 _updating_sample_count(false),
6546a6a7 63 _sample_count_supported(false),
2b49eeb0 64 _icon_red(":/icons/status-red.svg"),
f5798068
JH
65 _icon_green(":/icons/status-green.svg"),
66 _icon_grey(":/icons/status-grey.svg"),
274d4f13 67 _run_stop_button(this)
6fb67b27 68{
cdb50f67 69 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 70 this, SLOT(on_run_stop()));
dde1a563
JH
71 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
72 this, SLOT(on_device_selected()));
124d97de
ML
73 connect(&_sample_count, SIGNAL(value_changed()),
74 this, SLOT(on_sample_count_changed()));
1198b887
JH
75 connect(&_sample_rate, SIGNAL(value_changed()),
76 this, SLOT(on_sample_rate_changed()));
dde1a563 77
124d97de 78 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
215f9499 79
2b49eeb0 80 set_capture_state(pv::SigSession::Stopped);
274d4f13 81
688ef645
JH
82 _configure_button.setIcon(QIcon::fromTheme("configure",
83 QIcon(":/icons/configure.png")));
b7b659aa 84
6ac6242b
ML
85 _channels_button.setIcon(QIcon::fromTheme("channels",
86 QIcon(":/icons/channels.svg")));
cdb50f67 87
f5798068
JH
88 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
89
6fb67b27 90 addWidget(&_device_selector);
b3e8a5d8 91 _configure_button_action = addWidget(&_configure_button);
6ac6242b 92 addWidget(&_channels_button);
124d97de 93 addWidget(&_sample_count);
1198b887 94 addWidget(&_sample_rate);
6fb67b27 95
1198b887 96 addWidget(&_run_stop_button);
40065ab6
JS
97
98 _sample_count.installEventFilter(this);
99 _sample_rate.installEventFilter(this);
6fb67b27
JH
100}
101
18203d86 102void SamplingBar::set_device_list(
e95e8563
JH
103 const std::list< shared_ptr<pv::device::DevInst> > &devices,
104 shared_ptr<pv::device::DevInst> selected)
18203d86 105{
e95e8563
JH
106 int selected_index = -1;
107
108 assert(selected);
109
95237c18
JH
110 _updating_device_selector = true;
111
18203d86 112 _device_selector.clear();
19adbc2c 113 _device_selector_map.clear();
18203d86 114
d9aecf1f 115 for (shared_ptr<pv::device::DevInst> dev_inst : devices) {
19adbc2c
JH
116 assert(dev_inst);
117 const string title = dev_inst->format_device_title();
118 const sr_dev_inst *sdi = dev_inst->dev_inst();
119 assert(sdi);
120
e95e8563
JH
121 if (selected == dev_inst)
122 selected_index = _device_selector.count();
123
19adbc2c 124 _device_selector_map[sdi] = dev_inst;
dd63af74
JH
125 _device_selector.addItem(title.c_str(),
126 qVariantFromValue((void*)sdi));
18203d86
JH
127 }
128
e95e8563
JH
129 // The selected device should have been in the list
130 assert(selected_index != -1);
131 _device_selector.setCurrentIndex(selected_index);
132
133 update_device_config_widgets();
134
95237c18 135 _updating_device_selector = false;
18203d86
JH
136}
137
94574501 138shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
d4984fe7 139{
6fb67b27 140 const int index = _device_selector.currentIndex();
333d5bbc 141 if (index < 0)
94574501 142 return shared_ptr<pv::device::DevInst>();
19adbc2c
JH
143
144 const sr_dev_inst *const sdi =
145 (const sr_dev_inst*)_device_selector.itemData(
146 index).value<void*>();
147 assert(sdi);
6fb67b27 148
f46e495e 149 const auto iter = _device_selector_map.find(sdi);
19adbc2c 150 if (iter == _device_selector_map.end())
94574501 151 return shared_ptr<pv::device::DevInst>();
19adbc2c 152
94574501 153 return shared_ptr<pv::device::DevInst>((*iter).second);
6fb67b27
JH
154}
155
2b49eeb0 156void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 157{
2b49eeb0
JH
158 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
159 _run_stop_button.setIcon(*icons[state]);
160 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
161 tr("Run") : tr("Stop"));
6ac96c2e
JH
162}
163
dde1a563
JH
164void SamplingBar::update_sample_rate_selector()
165{
488f5d3f
BV
166 GVariant *gvar_dict, *gvar_list;
167 const uint64_t *elements = NULL;
168 gsize num_elements;
dde1a563 169
3d4f16af
JH
170 if (_updating_sample_rate)
171 return;
172
94574501 173 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c 174 if (!dev_inst)
ef4d0201
JH
175 return;
176
3d4f16af 177 assert(!_updating_sample_rate);
1198b887
JH
178 _updating_sample_rate = true;
179
8dd44190 180 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
1198b887
JH
181 {
182 _sample_rate.show_none();
183 _updating_sample_rate = false;
dde1a563 184 return;
1198b887 185 }
dde1a563 186
488f5d3f 187 if ((gvar_list = g_variant_lookup_value(gvar_dict,
1198b887
JH
188 "samplerate-steps", G_VARIANT_TYPE("at"))))
189 {
488f5d3f
BV
190 elements = (const uint64_t *)g_variant_get_fixed_array(
191 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
192
193 const uint64_t min = elements[0];
194 const uint64_t max = elements[1];
195 const uint64_t step = elements[2];
196
488f5d3f 197 g_variant_unref(gvar_list);
aafe53af
JH
198
199 assert(min > 0);
200 assert(max > 0);
201 assert(max > min);
202 assert(step > 0);
203
204 if (step == 1)
205 _sample_rate.show_125_list(min, max);
206 else
207 {
208 // When the step is not 1, we cam't make a 1-2-5-10
209 // list of sample rates, because we may not be able to
210 // make round numbers. Therefore in this case, show a
211 // spin box.
212 _sample_rate.show_min_max_step(min, max, step);
213 }
dde1a563 214 }
488f5d3f
BV
215 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
216 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 217 {
488f5d3f
BV
218 elements = (const uint64_t *)g_variant_get_fixed_array(
219 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 220 _sample_rate.show_list(elements, num_elements);
488f5d3f 221 g_variant_unref(gvar_list);
dde1a563 222 }
1198b887 223 _updating_sample_rate = false;
48888313 224
488f5d3f 225 g_variant_unref(gvar_dict);
48888313
JH
226 update_sample_rate_selector_value();
227}
228
229void SamplingBar::update_sample_rate_selector_value()
230{
488f5d3f
BV
231 GVariant *gvar;
232 uint64_t samplerate;
233
3d4f16af
JH
234 if (_updating_sample_rate)
235 return;
236
94574501 237 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
238 if (!dev_inst)
239 return;
240
8dd44190
JH
241 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
242 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
243 return;
244 }
488f5d3f
BV
245 samplerate = g_variant_get_uint64(gvar);
246 g_variant_unref(gvar);
48888313 247
3d4f16af 248 assert(!_updating_sample_rate);
1198b887
JH
249 _updating_sample_rate = true;
250 _sample_rate.set_value(samplerate);
251 _updating_sample_rate = false;
48888313
JH
252}
253
85756012
ML
254void SamplingBar::update_sample_count_selector()
255{
85756012 256 GVariant *gvar;
85756012 257
3d4f16af
JH
258 if (_updating_sample_count)
259 return;
260
94574501 261 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
262 if (!dev_inst)
263 return;
264
3d4f16af 265 assert(!_updating_sample_count);
b50ef520 266 _updating_sample_count = true;
85756012 267
b50ef520 268 if (_sample_count_supported)
df3c1aa3 269 {
3cbffdcf 270 uint64_t sample_count = _sample_count.value();
1d04852f 271 uint64_t min_sample_count = 0;
b50ef520
JH
272 uint64_t max_sample_count = MaxSampleCount;
273
3cbffdcf
JH
274 if (sample_count == 0)
275 sample_count = DefaultSampleCount;
276
8dd44190
JH
277 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
278 {
1d04852f
JH
279 g_variant_get(gvar, "(tt)",
280 &min_sample_count, &max_sample_count);
b50ef520
JH
281 g_variant_unref(gvar);
282 }
283
1d04852f
JH
284 min_sample_count = min(max(min_sample_count, MinSampleCount),
285 max_sample_count);
286
287 _sample_count.show_125_list(
288 min_sample_count, max_sample_count);
b50ef520 289
8dd44190 290 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
b50ef520
JH
291 {
292 sample_count = g_variant_get_uint64(gvar);
293 if (sample_count == 0)
294 sample_count = DefaultSampleCount;
295 sample_count = min(max(sample_count, MinSampleCount),
296 max_sample_count);
297
298 g_variant_unref(gvar);
299 }
300
301 _sample_count.set_value(sample_count);
85756012 302 }
b50ef520
JH
303 else
304 _sample_count.show_none();
df3c1aa3 305
df3c1aa3 306 _updating_sample_count = false;
85756012
ML
307}
308
e95e8563
JH
309void SamplingBar::update_device_config_widgets()
310{
311 GVariant *gvar;
312
313 using namespace pv::popups;
314
315 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
316 if (!dev_inst)
317 return;
318
319 // Update the configure popup
320 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
321 _configure_button_action->setVisible(
322 !opts->binding().properties().empty());
323 _configure_button.set_popup(opts);
324
6ac6242b
ML
325 // Update the channels popup
326 Channels *const channels = new Channels(_session, this);
327 _channels_button.set_popup(channels);
e95e8563
JH
328
329 // Update supported options.
330 _sample_count_supported = false;
331
332 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
333 {
334 gsize num_opts;
335 const int *const options =
336 (const int32_t *)g_variant_get_fixed_array(
337 gvar, &num_opts, sizeof(int32_t));
338 for (unsigned int i = 0; i < num_opts; i++)
339 {
340 switch (options[i]) {
341 case SR_CONF_LIMIT_SAMPLES:
342 _sample_count_supported = true;
343 break;
344 case SR_CONF_LIMIT_FRAMES:
345 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
346 g_variant_new_uint64(1));
347 break;
348 }
349 }
350 }
351
352 // Add notification of reconfigure events
353 disconnect(this, SLOT(on_config_changed()));
354 connect(dev_inst.get(), SIGNAL(config_changed()),
355 this, SLOT(on_config_changed()));
356
357 // Update sweep timing widgets.
358 update_sample_count_selector();
359 update_sample_rate_selector();
360}
361
124d97de
ML
362void SamplingBar::commit_sample_count()
363{
364 uint64_t sample_count = 0;
365
3d4f16af
JH
366 if (_updating_sample_count)
367 return;
368
94574501 369 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
370 if (!dev_inst)
371 return;
372
124d97de
ML
373 sample_count = _sample_count.value();
374
375 // Set the sample count
3d4f16af
JH
376 assert(!_updating_sample_count);
377 _updating_sample_count = true;
3668e2fe
JH
378 if (_sample_count_supported &&
379 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
8dd44190 380 g_variant_new_uint64(sample_count))) {
124d97de
ML
381 qDebug() << "Failed to configure sample count.";
382 return;
383 }
3d4f16af 384 _updating_sample_count = false;
124d97de
ML
385}
386
48888313
JH
387void SamplingBar::commit_sample_rate()
388{
389 uint64_t sample_rate = 0;
390
3d4f16af
JH
391 if (_updating_sample_rate)
392 return;
393
94574501 394 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
395 if (!dev_inst)
396 return;
397
1198b887 398 sample_rate = _sample_rate.value();
f9541bde
JH
399 if (sample_rate == 0)
400 return;
401
48888313 402 // Set the samplerate
3d4f16af
JH
403 assert(!_updating_sample_rate);
404 _updating_sample_rate = true;
8dd44190
JH
405 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
406 g_variant_new_uint64(sample_rate))) {
48888313
JH
407 qDebug() << "Failed to configure samplerate.";
408 return;
409 }
3d4f16af 410 _updating_sample_rate = false;
dde1a563
JH
411}
412
413void SamplingBar::on_device_selected()
414{
95237c18
JH
415 if (_updating_device_selector)
416 return;
417
94574501 418 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
419 if (!dev_inst)
420 return;
421
19adbc2c 422 _session.set_device(dev_inst);
51d4a9ab 423
e95e8563 424 update_device_config_widgets();
dde1a563 425}
51e77110 426
124d97de
ML
427void SamplingBar::on_sample_count_changed()
428{
3d4f16af 429 commit_sample_count();
124d97de
ML
430}
431
48888313
JH
432void SamplingBar::on_sample_rate_changed()
433{
3d4f16af 434 commit_sample_rate();
48888313
JH
435}
436
9f3d12f3
JH
437void SamplingBar::on_run_stop()
438{
b50ef520 439 commit_sample_count();
9f3d12f3
JH
440 commit_sample_rate();
441 run_stop();
442}
443
82afd5e3
JH
444void SamplingBar::on_config_changed()
445{
446 commit_sample_count();
447 update_sample_count_selector();
448 commit_sample_rate();
449 update_sample_rate_selector();
450}
451
40065ab6
JS
452bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
453{
454 if ((watched == &_sample_count || watched == &_sample_rate) &&
455 (event->type() == QEvent::ToolTip)) {
456 double sec = (double)_sample_count.value() / _sample_rate.value();
40065ab6 457 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45
JS
458
459 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
40065ab6
JS
460 QToolTip::showText(help_event->globalPos(), str);
461
462 return true;
463 }
464
465 return false;
466}
467
f4c92e1c 468} // namespace toolbars
51e77110 469} // namespace pv