]> sigrok.org Git - pulseview.git/blob - pv/toolbars/mainbar.cpp
MainBar: Replaced device selector combo-box with a split button
[pulseview.git] / pv / toolbars / mainbar.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2012-2015 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 <algorithm>
24 #include <cassert>
25
26 #include <QAction>
27 #include <QDebug>
28 #include <QHelpEvent>
29 #include <QMenu>
30 #include <QToolTip>
31
32 #include "mainbar.hpp"
33
34 #include <pv/devicemanager.hpp>
35 #include <pv/mainwindow.hpp>
36 #include <pv/popups/deviceoptions.hpp>
37 #include <pv/popups/channels.hpp>
38 #include <pv/util.hpp>
39
40 #include <libsigrok/libsigrok.hpp>
41
42 using std::back_inserter;
43 using std::copy;
44 using std::list;
45 using std::map;
46 using std::max;
47 using std::min;
48 using std::shared_ptr;
49 using std::string;
50 using std::vector;
51
52 using sigrok::Capability;
53 using sigrok::ConfigKey;
54 using sigrok::Device;
55 using sigrok::Error;
56
57 namespace pv {
58 namespace toolbars {
59
60 const uint64_t MainBar::MinSampleCount = 100ULL;
61 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
62 const uint64_t MainBar::DefaultSampleCount = 1000000;
63
64 MainBar::MainBar(Session &session, MainWindow &main_window) :
65         QToolBar("Sampling Bar", &main_window),
66         session_(session),
67         main_window_(main_window),
68         device_selector_(this, session.device_manager(),
69                 main_window.action_connect()),
70         configure_button_(this),
71         configure_button_action_(NULL),
72         channels_button_(this),
73         sample_count_(" samples", this),
74         sample_rate_("Hz", this),
75         updating_sample_rate_(false),
76         updating_sample_count_(false),
77         sample_count_supported_(false),
78         icon_red_(":/icons/status-red.svg"),
79         icon_green_(":/icons/status-green.svg"),
80         icon_grey_(":/icons/status-grey.svg"),
81         run_stop_button_(this),
82         menu_button_(this)
83 {
84         setObjectName(QString::fromUtf8("MainBar"));
85
86         setMovable(false);
87         setFloatable(false);
88
89         // Device selector menu
90         connect(&device_selector_, SIGNAL(device_selected()),
91                 this, SLOT(on_device_selected()));
92
93         // Setup the decoder button
94 #ifdef ENABLE_DECODE
95         QToolButton *add_decoder_button = new QToolButton(this);
96         add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
97                 QIcon(":/icons/add-decoder.svg")));
98         add_decoder_button->setPopupMode(QToolButton::InstantPopup);
99         add_decoder_button->setMenu(main_window_.menu_decoder_add());
100 #endif
101
102         // Setup the menu
103         QMenu *const menu = new QMenu(this);
104
105         QMenu *const menu_help = new QMenu;
106         menu_help->setTitle(tr("&Help"));
107         menu_help->addAction(main_window.action_about());
108
109         menu->addAction(menu_help->menuAction());
110         menu->addSeparator();
111         menu->addAction(main_window.action_quit());
112
113         menu_button_.setMenu(menu);
114         menu_button_.setPopupMode(QToolButton::InstantPopup);
115         menu_button_.setIcon(QIcon::fromTheme("menu",
116                 QIcon(":/icons/menu.svg")));
117
118         // Setup the toolbar
119         addAction(main_window.action_open());
120         addAction(main_window.action_save_as());
121         addSeparator();
122         addAction(main_window.action_view_zoom_in());
123         addAction(main_window.action_view_zoom_out());
124         addAction(main_window.action_view_zoom_fit());
125         addAction(main_window.action_view_zoom_one_to_one());
126         addSeparator();
127         addAction(main_window.action_view_show_cursors());
128         addSeparator();
129
130         connect(&run_stop_button_, SIGNAL(clicked()),
131                 this, SLOT(on_run_stop()));
132         connect(&sample_count_, SIGNAL(value_changed()),
133                 this, SLOT(on_sample_count_changed()));
134         connect(&sample_rate_, SIGNAL(value_changed()),
135                 this, SLOT(on_sample_rate_changed()));
136
137         sample_count_.show_min_max_step(0, UINT64_MAX, 1);
138
139         set_capture_state(pv::Session::Stopped);
140
141         configure_button_.setIcon(QIcon::fromTheme("configure",
142                 QIcon(":/icons/configure.png")));
143
144         channels_button_.setIcon(QIcon::fromTheme("channels",
145                 QIcon(":/icons/channels.svg")));
146
147         run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
148
149         addWidget(&device_selector_);
150         configure_button_action_ = addWidget(&configure_button_);
151         addWidget(&channels_button_);
152         addWidget(&sample_count_);
153         addWidget(&sample_rate_);
154         addWidget(&run_stop_button_);
155 #ifdef ENABLE_DECODE
156         addSeparator();
157         addWidget(add_decoder_button);
158 #endif
159
160         QWidget *const spacer = new QWidget();
161         spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
162         addWidget(spacer);
163
164         addWidget(&menu_button_);
165
166         sample_count_.installEventFilter(this);
167         sample_rate_.installEventFilter(this);
168 }
169
170 void MainBar::update_device_list()
171 {
172         DeviceManager &mgr = session_.device_manager();
173         shared_ptr<Device> selected_device = session_.device();
174         list< shared_ptr<Device> > devs;
175
176         copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
177
178         if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
179                 devs.push_back(selected_device);
180         assert(selected_device);
181
182         device_selector_.set_device_list(devs, selected_device);
183         update_device_config_widgets();
184 }
185
186
187 void MainBar::set_capture_state(pv::Session::capture_state state)
188 {
189         const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
190         run_stop_button_.setIcon(*icons[state]);
191         run_stop_button_.setText((state == pv::Session::Stopped) ?
192                 tr("Run") : tr("Stop"));
193         run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
194 }
195
196 void MainBar::update_sample_rate_selector()
197 {
198         Glib::VariantContainerBase gvar_dict;
199         GVariant *gvar_list;
200         const uint64_t *elements = NULL;
201         gsize num_elements;
202
203         if (updating_sample_rate_)
204                 return;
205
206         const shared_ptr<Device> device = device_selector_.selected_device();
207         if (!device)
208                 return;
209
210         assert(!updating_sample_rate_);
211         updating_sample_rate_ = true;
212
213         const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
214         const auto iter = keys.find(ConfigKey::SAMPLERATE);
215         if (iter != keys.end() &&
216                 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
217                 const auto keys = device->config_keys(
218                         ConfigKey::DEVICE_OPTIONS);
219                 try {
220                         gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
221                 } catch(const sigrok::Error &e) {
222                         // Failed to enunmerate samplerate
223                         (void)e;
224                 }
225         }
226
227         if (!gvar_dict) {
228                 sample_rate_.show_none();
229                 updating_sample_rate_ = false;
230                 return;
231         }
232
233         if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
234                         "samplerate-steps", G_VARIANT_TYPE("at"))))
235         {
236                 elements = (const uint64_t *)g_variant_get_fixed_array(
237                                 gvar_list, &num_elements, sizeof(uint64_t));
238
239                 const uint64_t min = elements[0];
240                 const uint64_t max = elements[1];
241                 const uint64_t step = elements[2];
242
243                 g_variant_unref(gvar_list);
244
245                 assert(min > 0);
246                 assert(max > 0);
247                 assert(max > min);
248                 assert(step > 0);
249
250                 if (step == 1)
251                         sample_rate_.show_125_list(min, max);
252                 else
253                 {
254                         // When the step is not 1, we cam't make a 1-2-5-10
255                         // list of sample rates, because we may not be able to
256                         // make round numbers. Therefore in this case, show a
257                         // spin box.
258                         sample_rate_.show_min_max_step(min, max, step);
259                 }
260         }
261         else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
262                         "samplerates", G_VARIANT_TYPE("at"))))
263         {
264                 elements = (const uint64_t *)g_variant_get_fixed_array(
265                                 gvar_list, &num_elements, sizeof(uint64_t));
266                 sample_rate_.show_list(elements, num_elements);
267                 g_variant_unref(gvar_list);
268         }
269         updating_sample_rate_ = false;
270
271         update_sample_rate_selector_value();
272 }
273
274 void MainBar::update_sample_rate_selector_value()
275 {
276         if (updating_sample_rate_)
277                 return;
278
279         const shared_ptr<Device> device = device_selector_.selected_device();
280         if (!device)
281                 return;
282
283         try {
284                 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
285                 uint64_t samplerate =
286                         Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
287                 assert(!updating_sample_rate_);
288                 updating_sample_rate_ = true;
289                 sample_rate_.set_value(samplerate);
290                 updating_sample_rate_ = false;
291         } catch (Error error) {
292                 qDebug() << "WARNING: Failed to get value of sample rate";
293                 return;
294         }
295 }
296
297 void MainBar::update_sample_count_selector()
298 {
299         if (updating_sample_count_)
300                 return;
301
302         const shared_ptr<Device> device = device_selector_.selected_device();
303         if (!device)
304                 return;
305
306         assert(!updating_sample_count_);
307         updating_sample_count_ = true;
308
309         if (!sample_count_supported_)
310         {
311                 sample_count_.show_none();
312                 updating_sample_count_ = false;
313                 return;
314         }
315
316         uint64_t sample_count = sample_count_.value();
317         uint64_t min_sample_count = 0;
318         uint64_t max_sample_count = MaxSampleCount;
319
320         if (sample_count == 0)
321                 sample_count = DefaultSampleCount;
322
323         const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
324         const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
325         if (iter != keys.end() &&
326                 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
327                 try {
328                         auto gvar =
329                                 device->config_list(ConfigKey::LIMIT_SAMPLES);
330                         if (gvar)
331                                 g_variant_get(gvar.gobj(), "(tt)",
332                                         &min_sample_count, &max_sample_count);
333                 } catch(const sigrok::Error &e) {
334                         // Failed to query sample limit
335                         (void)e;
336                 }
337         }
338
339         min_sample_count = min(max(min_sample_count, MinSampleCount),
340                 max_sample_count);
341
342         sample_count_.show_125_list(
343                 min_sample_count, max_sample_count);
344
345         try {
346                 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
347                 sample_count = g_variant_get_uint64(gvar.gobj());
348                 if (sample_count == 0)
349                         sample_count = DefaultSampleCount;
350                 sample_count = min(max(sample_count, MinSampleCount),
351                         max_sample_count);
352         } catch (Error error) {}
353
354         sample_count_.set_value(sample_count);
355
356         updating_sample_count_ = false;
357 }
358
359 void MainBar::update_device_config_widgets()
360 {
361         using namespace pv::popups;
362
363         const shared_ptr<Device> device = device_selector_.selected_device();
364         if (!device)
365                 return;
366
367         // Update the configure popup
368         DeviceOptions *const opts = new DeviceOptions(device, this);
369         configure_button_action_->setVisible(
370                 !opts->binding().properties().empty());
371         configure_button_.set_popup(opts);
372
373         // Update the channels popup
374         Channels *const channels = new Channels(session_, this);
375         channels_button_.set_popup(channels);
376
377         // Update supported options.
378         sample_count_supported_ = false;
379
380         try {
381                 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
382                 {
383                         auto key = entry.first;
384                         auto capabilities = entry.second;
385                         switch (key->id()) {
386                         case SR_CONF_LIMIT_SAMPLES:
387                                 if (capabilities.count(Capability::SET))
388                                         sample_count_supported_ = true;
389                                 break;
390                         case SR_CONF_LIMIT_FRAMES:
391                                 if (capabilities.count(Capability::SET))
392                                 {
393                                         device->config_set(ConfigKey::LIMIT_FRAMES,
394                                                 Glib::Variant<guint64>::create(1));
395                                         on_config_changed();
396                                 }
397                                 break;
398                         default:
399                                 break;
400                         }
401                 }
402         } catch (Error error) {}
403
404         // Add notification of reconfigure events
405         disconnect(this, SLOT(on_config_changed()));
406         connect(&opts->binding(), SIGNAL(config_changed()),
407                 this, SLOT(on_config_changed()));
408
409         // Update sweep timing widgets.
410         update_sample_count_selector();
411         update_sample_rate_selector();
412 }
413
414 void MainBar::commit_sample_count()
415 {
416         uint64_t sample_count = 0;
417
418         if (updating_sample_count_)
419                 return;
420
421         const shared_ptr<Device> device = device_selector_.selected_device();
422         if (!device)
423                 return;
424
425         sample_count = sample_count_.value();
426
427         // Set the sample count
428         assert(!updating_sample_count_);
429         updating_sample_count_ = true;
430         if (sample_count_supported_)
431         {
432                 try {
433                         device->config_set(ConfigKey::LIMIT_SAMPLES,
434                                 Glib::Variant<guint64>::create(sample_count));
435                         on_config_changed();
436                 } catch (Error error) {
437                         qDebug() << "Failed to configure sample count.";
438                         return;
439                 }
440         }
441         updating_sample_count_ = false;
442 }
443
444 void MainBar::commit_sample_rate()
445 {
446         uint64_t sample_rate = 0;
447
448         if (updating_sample_rate_)
449                 return;
450
451         const shared_ptr<Device> device = device_selector_.selected_device();
452         if (!device)
453                 return;
454
455         sample_rate = sample_rate_.value();
456         if (sample_rate == 0)
457                 return;
458
459         // Set the samplerate
460         assert(!updating_sample_rate_);
461         updating_sample_rate_ = true;
462         try {
463                 device->config_set(ConfigKey::SAMPLERATE,
464                         Glib::Variant<guint64>::create(sample_rate));
465                 on_config_changed();
466         } catch (Error error) {
467                 qDebug() << "Failed to configure samplerate.";
468                 return;
469         }
470         updating_sample_rate_ = false;
471 }
472
473 void MainBar::on_device_selected()
474 {
475         shared_ptr<Device> device = device_selector_.selected_device();
476         if (!device)
477                 return;
478
479         main_window_.select_device(device);
480
481         update_device_config_widgets();
482 }
483
484 void MainBar::on_sample_count_changed()
485 {
486         commit_sample_count();
487 }
488
489 void MainBar::on_sample_rate_changed()
490 {
491         commit_sample_rate();
492 }
493
494 void MainBar::on_run_stop()
495 {
496         commit_sample_count();
497         commit_sample_rate();   
498         main_window_.run_stop();
499 }
500
501 void MainBar::on_config_changed()
502 {
503         commit_sample_count();
504         update_sample_count_selector(); 
505         commit_sample_rate();   
506         update_sample_rate_selector();
507 }
508
509 bool MainBar::eventFilter(QObject *watched, QEvent *event)
510 {
511         if ((watched == &sample_count_ || watched == &sample_rate_) &&
512                 (event->type() == QEvent::ToolTip)) {
513                 double sec = (double)sample_count_.value() / sample_rate_.value();
514                 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
515
516                 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
517                 QToolTip::showText(help_event->globalPos(), str);
518
519                 return true;
520         }
521
522         return false;
523 }
524
525 } // namespace toolbars
526 } // namespace pv