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