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