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