]> sigrok.org Git - pulseview.git/blob - pv/toolbars/mainbar.cpp
MainBar: Make sure device and GUI are in sync on startup
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <extdef.h>
21
22 #include <algorithm>
23 #include <cassert>
24
25 #include <QAction>
26 #include <QDebug>
27 #include <QFileDialog>
28 #include <QHelpEvent>
29 #include <QMenu>
30 #include <QMessageBox>
31 #include <QSettings>
32 #include <QToolTip>
33
34 #include "mainbar.hpp"
35
36 #include <boost/algorithm/string/join.hpp>
37
38 #include <pv/devicemanager.hpp>
39 #include <pv/devices/hardwaredevice.hpp>
40 #include <pv/devices/inputfile.hpp>
41 #include <pv/devices/sessionfile.hpp>
42 #include <pv/dialogs/connect.hpp>
43 #include <pv/dialogs/inputoutputoptions.hpp>
44 #include <pv/dialogs/storeprogress.hpp>
45 #include <pv/mainwindow.hpp>
46 #include <pv/popups/deviceoptions.hpp>
47 #include <pv/popups/channels.hpp>
48 #include <pv/util.hpp>
49 #include <pv/view/view.hpp>
50 #include <pv/widgets/exportmenu.hpp>
51 #include <pv/widgets/importmenu.hpp>
52 #ifdef ENABLE_DECODE
53 #include <pv/widgets/decodermenu.hpp>
54 #endif
55
56 #include <libsigrokcxx/libsigrokcxx.hpp>
57
58 using std::back_inserter;
59 using std::cerr;
60 using std::copy;
61 using std::endl;
62 using std::list;
63 using std::map;
64 using std::max;
65 using std::min;
66 using std::shared_ptr;
67 using std::string;
68 using std::vector;
69
70 using sigrok::Capability;
71 using sigrok::ConfigKey;
72 using sigrok::Error;
73 using sigrok::InputFormat;
74 using sigrok::OutputFormat;
75
76 using boost::algorithm::join;
77
78 namespace pv {
79 namespace toolbars {
80
81 const uint64_t MainBar::MinSampleCount = 100ULL;
82 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
83 const uint64_t MainBar::DefaultSampleCount = 1000000;
84
85 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
86 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
87
88 MainBar::MainBar(Session &session, QWidget *parent,
89                 pv::views::TraceView::View *view) :
90         StandardBar(session, parent, view, false),
91         action_new_view_(new QAction(this)),
92         action_open_(new QAction(this)),
93         action_save_as_(new QAction(this)),
94         action_save_selection_as_(new QAction(this)),
95         action_connect_(new QAction(this)),
96         open_button_(new QToolButton()),
97         save_button_(new QToolButton()),
98         device_selector_(parent, session.device_manager(),
99                 action_connect_),
100         configure_button_(this),
101         configure_button_action_(nullptr),
102         channels_button_(this),
103         channels_button_action_(nullptr),
104         sample_count_(" samples", this),
105         sample_rate_("Hz", this),
106         updating_sample_rate_(false),
107         updating_sample_count_(false),
108         sample_count_supported_(false)
109 #ifdef ENABLE_DECODE
110         , add_decoder_button_(new QToolButton()),
111         menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
112 #endif
113 {
114         setObjectName(QString::fromUtf8("MainBar"));
115
116         // Actions
117         action_new_view_->setText(tr("New &View"));
118         action_new_view_->setIcon(QIcon::fromTheme("window-new",
119                 QIcon(":/icons/window-new.png")));
120         connect(action_new_view_, SIGNAL(triggered(bool)),
121                 this, SLOT(on_actionNewView_triggered()));
122
123         action_open_->setText(tr("&Open..."));
124         action_open_->setIcon(QIcon::fromTheme("document-open",
125                 QIcon(":/icons/document-open.png")));
126         action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
127         connect(action_open_, SIGNAL(triggered(bool)),
128                 this, SLOT(on_actionOpen_triggered()));
129
130         action_save_as_->setText(tr("&Save As..."));
131         action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
132                 QIcon(":/icons/document-save-as.png")));
133         action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
134         connect(action_save_as_, SIGNAL(triggered(bool)),
135                 this, SLOT(on_actionSaveAs_triggered()));
136
137         action_save_selection_as_->setText(tr("Save Selected &Range As..."));
138         action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
139                 QIcon(":/icons/document-save-as.png")));
140         action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
141         connect(action_save_selection_as_, SIGNAL(triggered(bool)),
142                 this, SLOT(on_actionSaveSelectionAs_triggered()));
143
144         widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
145                 session.device_manager().context());
146         menu_file_export->setTitle(tr("&Export"));
147         connect(menu_file_export,
148                 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
149                 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
150
151         widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
152                 session.device_manager().context());
153         menu_file_import->setTitle(tr("&Import"));
154         connect(menu_file_import,
155                 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
156                 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
157
158         action_connect_->setText(tr("&Connect to Device..."));
159         connect(action_connect_, SIGNAL(triggered(bool)),
160                 this, SLOT(on_actionConnect_triggered()));
161
162         // Open button
163         widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
164                 session.device_manager().context(), action_open_);
165         connect(import_menu,
166                 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
167                 this,
168                 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
169
170         open_button_->setMenu(import_menu);
171         open_button_->setDefaultAction(action_open_);
172         open_button_->setPopupMode(QToolButton::MenuButtonPopup);
173
174         // Save button
175         vector<QAction *> open_actions;
176         open_actions.push_back(action_save_as_);
177         open_actions.push_back(action_save_selection_as_);
178
179         widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
180                 session.device_manager().context(),
181                 open_actions);
182         connect(export_menu,
183                 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
184                 this,
185                 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
186
187         save_button_->setMenu(export_menu);
188         save_button_->setDefaultAction(action_save_as_);
189         save_button_->setPopupMode(QToolButton::MenuButtonPopup);
190
191         // Device selector menu
192         connect(&device_selector_, SIGNAL(device_selected()),
193                 this, SLOT(on_device_selected()));
194
195         // Setup the decoder button
196 #ifdef ENABLE_DECODE
197         menu_decoders_add_->setTitle(tr("&Add"));
198         connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
199                 this, SLOT(add_decoder(srd_decoder*)));
200
201         add_decoder_button_->setIcon(QIcon::fromTheme("add-decoder",
202                 QIcon(":/icons/add-decoder.svg")));
203         add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
204         add_decoder_button_->setMenu(menu_decoders_add_);
205 #endif
206
207         connect(&sample_count_, SIGNAL(value_changed()),
208                 this, SLOT(on_sample_count_changed()));
209         connect(&sample_rate_, SIGNAL(value_changed()),
210                 this, SLOT(on_sample_rate_changed()));
211
212         sample_count_.show_min_max_step(0, UINT64_MAX, 1);
213
214         set_capture_state(pv::Session::Stopped);
215
216         configure_button_.setToolTip(tr("Configure Device"));
217         configure_button_.setIcon(QIcon::fromTheme("configure",
218                 QIcon(":/icons/configure.png")));
219
220         channels_button_.setToolTip(tr("Configure Channels"));
221         channels_button_.setIcon(QIcon::fromTheme("channels",
222                 QIcon(":/icons/channels.svg")));
223
224         add_toolbar_widgets();
225
226         sample_count_.installEventFilter(this);
227         sample_rate_.installEventFilter(this);
228
229         // Setup session_ events
230         connect(&session_, SIGNAL(capture_state_changed(int)),
231                 this, SLOT(on_capture_state_changed(int)));
232         connect(&session, SIGNAL(device_changed()),
233                 this, SLOT(on_device_changed()));
234
235         update_device_list();
236 }
237
238 void MainBar::update_device_list()
239 {
240         DeviceManager &mgr = session_.device_manager();
241         shared_ptr<devices::Device> selected_device = session_.device();
242         list< shared_ptr<devices::Device> > devs;
243
244         copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
245
246         if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
247                 devs.push_back(selected_device);
248
249         device_selector_.set_device_list(devs, selected_device);
250         update_device_config_widgets();
251 }
252
253
254 void MainBar::set_capture_state(pv::Session::capture_state state)
255 {
256         bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
257
258         device_selector_.setEnabled(ui_enabled);
259         configure_button_.setEnabled(ui_enabled);
260         channels_button_.setEnabled(ui_enabled);
261         sample_count_.setEnabled(ui_enabled);
262         sample_rate_.setEnabled(ui_enabled);
263 }
264
265 void MainBar::reset_device_selector()
266 {
267         device_selector_.reset();
268 }
269
270 QAction* MainBar::action_open() const
271 {
272         return action_open_;
273 }
274
275 QAction* MainBar::action_save_as() const
276 {
277         return action_save_as_;
278 }
279
280 QAction* MainBar::action_save_selection_as() const
281 {
282         return action_save_selection_as_;
283 }
284
285 QAction* MainBar::action_connect() const
286 {
287         return action_connect_;
288 }
289
290 void MainBar::update_sample_rate_selector()
291 {
292         Glib::VariantContainerBase gvar_dict;
293         GVariant *gvar_list;
294         const uint64_t *elements = nullptr;
295         gsize num_elements;
296         map< const ConfigKey*, std::set<Capability> > keys;
297
298         if (updating_sample_rate_) {
299                 sample_rate_.show_none();
300                 return;
301         }
302
303         const shared_ptr<devices::Device> device =
304                 device_selector_.selected_device();
305         if (!device)
306                 return;
307
308         assert(!updating_sample_rate_);
309         updating_sample_rate_ = true;
310
311         const shared_ptr<sigrok::Device> sr_dev = device->device();
312
313         if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
314                 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
315         } else {
316                 sample_rate_.show_none();
317                 updating_sample_rate_ = false;
318                 return;
319         }
320
321         if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
322                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
323                 elements = (const uint64_t *)g_variant_get_fixed_array(
324                                 gvar_list, &num_elements, sizeof(uint64_t));
325
326                 const uint64_t min = elements[0];
327                 const uint64_t max = elements[1];
328                 const uint64_t step = elements[2];
329
330                 g_variant_unref(gvar_list);
331
332                 assert(min > 0);
333                 assert(max > 0);
334                 assert(max > min);
335                 assert(step > 0);
336
337                 if (step == 1)
338                         sample_rate_.show_125_list(min, max);
339                 else {
340                         // When the step is not 1, we cam't make a 1-2-5-10
341                         // list of sample rates, because we may not be able to
342                         // make round numbers. Therefore in this case, show a
343                         // spin box.
344                         sample_rate_.show_min_max_step(min, max, step);
345                 }
346         } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
347                         "samplerates", G_VARIANT_TYPE("at")))) {
348                 elements = (const uint64_t *)g_variant_get_fixed_array(
349                                 gvar_list, &num_elements, sizeof(uint64_t));
350                 sample_rate_.show_list(elements, num_elements);
351                 g_variant_unref(gvar_list);
352         }
353         updating_sample_rate_ = false;
354
355         update_sample_rate_selector_value();
356 }
357
358 void MainBar::update_sample_rate_selector_value()
359 {
360         if (updating_sample_rate_)
361                 return;
362
363         const shared_ptr<devices::Device> device =
364                 device_selector_.selected_device();
365         if (!device)
366                 return;
367
368         try {
369                 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
370                 uint64_t samplerate =
371                         Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
372                 assert(!updating_sample_rate_);
373                 updating_sample_rate_ = true;
374                 sample_rate_.set_value(samplerate);
375                 updating_sample_rate_ = false;
376         } catch (Error error) {
377                 qDebug() << "WARNING: Failed to get value of sample rate";
378                 return;
379         }
380 }
381
382 void MainBar::update_sample_count_selector()
383 {
384         if (updating_sample_count_)
385                 return;
386
387         const shared_ptr<devices::Device> device =
388                 device_selector_.selected_device();
389         if (!device)
390                 return;
391
392         const shared_ptr<sigrok::Device> sr_dev = device->device();
393
394         assert(!updating_sample_count_);
395         updating_sample_count_ = true;
396
397         if (!sample_count_supported_) {
398                 sample_count_.show_none();
399                 updating_sample_count_ = false;
400                 return;
401         }
402
403         uint64_t sample_count = sample_count_.value();
404         uint64_t min_sample_count = 0;
405         uint64_t max_sample_count = MaxSampleCount;
406         bool default_count_set = false;
407
408         if (sample_count == 0) {
409                 sample_count = DefaultSampleCount;
410                 default_count_set = true;
411         }
412
413         if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
414                 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
415                 if (gvar.gobj())
416                         g_variant_get(gvar.gobj(), "(tt)",
417                                 &min_sample_count, &max_sample_count);
418         }
419
420         min_sample_count = min(max(min_sample_count, MinSampleCount),
421                 max_sample_count);
422
423         sample_count_.show_125_list(
424                 min_sample_count, max_sample_count);
425
426         if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
427                 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
428                 sample_count = g_variant_get_uint64(gvar.gobj());
429                 if (sample_count == 0) {
430                         sample_count = DefaultSampleCount;
431                         default_count_set = true;
432                 }
433                 sample_count = min(max(sample_count, MinSampleCount),
434                         max_sample_count);
435         }
436
437         sample_count_.set_value(sample_count);
438
439         updating_sample_count_ = false;
440
441         // If we show the default rate then make sure the device uses the same
442         if (default_count_set)
443                 commit_sample_count();
444 }
445
446 void MainBar::update_device_config_widgets()
447 {
448         using namespace pv::popups;
449
450         const shared_ptr<devices::Device> device =
451                 device_selector_.selected_device();
452
453         // Hide the widgets if no device is selected
454         channels_button_action_->setVisible(!!device);
455         if (!device) {
456                 configure_button_action_->setVisible(false);
457                 sample_count_.show_none();
458                 sample_rate_.show_none();
459                 return;
460         }
461
462         const shared_ptr<sigrok::Device> sr_dev = device->device();
463         if (!sr_dev)
464                 return;
465
466         // Update the configure popup
467         DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
468         configure_button_action_->setVisible(
469                 !opts->binding().properties().empty());
470         configure_button_.set_popup(opts);
471
472         // Update the channels popup
473         Channels *const channels = new Channels(session_, this);
474         channels_button_.set_popup(channels);
475
476         // Update supported options.
477         sample_count_supported_ = false;
478
479         if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
480                 sample_count_supported_ = true;
481
482         if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
483                 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
484                         Glib::Variant<guint64>::create(1));
485                         on_config_changed();
486         }
487
488         // Add notification of reconfigure events
489         disconnect(this, SLOT(on_config_changed()));
490         connect(&opts->binding(), SIGNAL(config_changed()),
491                 this, SLOT(on_config_changed()));
492
493         // Update sweep timing widgets.
494         update_sample_count_selector();
495         update_sample_rate_selector();
496 }
497
498 void MainBar::commit_sample_rate()
499 {
500         uint64_t sample_rate = 0;
501
502         const shared_ptr<devices::Device> device =
503                 device_selector_.selected_device();
504         if (!device)
505                 return;
506
507         const shared_ptr<sigrok::Device> sr_dev = device->device();
508
509         sample_rate = sample_rate_.value();
510         if (sample_rate == 0)
511                 return;
512
513         try {
514                 sr_dev->config_set(ConfigKey::SAMPLERATE,
515                         Glib::Variant<guint64>::create(sample_rate));
516                 update_sample_rate_selector();
517         } catch (Error error) {
518                 qDebug() << "Failed to configure samplerate.";
519                 return;
520         }
521
522         // Devices with built-in memory might impose limits on certain
523         // configurations, so let's check what sample count the driver
524         // lets us use now.
525         update_sample_count_selector();
526 }
527
528 void MainBar::commit_sample_count()
529 {
530         uint64_t sample_count = 0;
531
532         const shared_ptr<devices::Device> device =
533                 device_selector_.selected_device();
534         if (!device)
535                 return;
536
537         const shared_ptr<sigrok::Device> sr_dev = device->device();
538
539         sample_count = sample_count_.value();
540         if (sample_count_supported_) {
541                 try {
542                         sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
543                                 Glib::Variant<guint64>::create(sample_count));
544                         update_sample_count_selector();
545                 } catch (Error error) {
546                         qDebug() << "Failed to configure sample count.";
547                         return;
548                 }
549         }
550
551         // Devices with built-in memory might impose limits on certain
552         // configurations, so let's check what sample rate the driver
553         // lets us use now.
554         update_sample_rate_selector();
555 }
556
557 void MainBar::session_error(const QString text, const QString info_text)
558 {
559         QMetaObject::invokeMethod(this, "show_session_error",
560                 Qt::QueuedConnection, Q_ARG(QString, text),
561                 Q_ARG(QString, info_text));
562 }
563
564 void MainBar::show_session_error(const QString text, const QString info_text)
565 {
566         QMessageBox msg(this);
567         msg.setText(text);
568         msg.setInformativeText(info_text);
569         msg.setStandardButtons(QMessageBox::Ok);
570         msg.setIcon(QMessageBox::Warning);
571         msg.exec();
572 }
573
574 void MainBar::add_decoder(srd_decoder *decoder)
575 {
576 #ifdef ENABLE_DECODE
577         assert(decoder);
578         session_.add_decoder(decoder);
579 #else
580         (void)decoder;
581 #endif
582 }
583
584 void MainBar::export_file(shared_ptr<OutputFormat> format,
585         bool selection_only)
586 {
587         using pv::dialogs::StoreProgress;
588
589         // Stop any currently running capture session
590         session_.stop_capture();
591
592         QSettings settings;
593         const QString dir = settings.value(SettingSaveDirectory).toString();
594
595         std::pair<uint64_t, uint64_t> sample_range;
596
597         // Selection only? Verify that the cursors are active and fetch their values
598         if (selection_only) {
599                 views::TraceView::View *trace_view =
600                         qobject_cast<views::TraceView::View*>(session_.main_view().get());
601
602                 if (!trace_view->cursors()->enabled()) {
603                         show_session_error(tr("Missing Cursors"), tr("You need to set the " \
604                                         "cursors before you can save the data enclosed by them " \
605                                         "to a session file (e.g. using ALT-V - Show Cursors)."));
606                         return;
607                 }
608
609                 const double samplerate = session_.get_samplerate();
610
611                 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
612                 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
613
614                 const uint64_t start_sample =
615                         std::max((double)0, start_time.convert_to<double>() * samplerate);
616                 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
617
618                 sample_range = std::make_pair(start_sample, end_sample);
619         } else {
620                 sample_range = std::make_pair(0, 0);
621         }
622
623         // Construct the filter
624         const vector<string> exts = format->extensions();
625         QString filter = tr("%1 files ").arg(
626                 QString::fromStdString(format->description()));
627
628         if (exts.empty())
629                 filter += "(*.*)";
630         else
631                 filter += QString("(*.%1);;%2 (*.*)").arg(
632                         QString::fromStdString(join(exts, ", *.")),
633                         tr("All Files"));
634
635         // Show the file dialog
636         const QString file_name = QFileDialog::getSaveFileName(
637                 this, tr("Save File"), dir, filter);
638
639         if (file_name.isEmpty())
640                 return;
641
642         const QString abs_path = QFileInfo(file_name).absolutePath();
643         settings.setValue(SettingSaveDirectory, abs_path);
644
645         // Show the options dialog
646         map<string, Glib::VariantBase> options;
647         if (!format->options().empty()) {
648                 dialogs::InputOutputOptions dlg(
649                         tr("Export %1").arg(QString::fromStdString(
650                                 format->description())),
651                         format->options(), this);
652                 if (!dlg.exec())
653                         return;
654                 options = dlg.options();
655         }
656
657         if (!selection_only)
658                 session_.set_name(QFileInfo(file_name).fileName());
659
660         StoreProgress *dlg = new StoreProgress(file_name, format, options,
661                 sample_range, session_, this);
662         dlg->run();
663 }
664
665 void MainBar::import_file(shared_ptr<InputFormat> format)
666 {
667         assert(format);
668
669         QSettings settings;
670         const QString dir = settings.value(SettingOpenDirectory).toString();
671
672         // Construct the filter
673         const vector<string> exts = format->extensions();
674         const QString filter = exts.empty() ? "" :
675                 tr("%1 files (*.%2)").arg(
676                         QString::fromStdString(format->description()),
677                         QString::fromStdString(join(exts, ", *.")));
678
679         // Show the file dialog
680         const QString file_name = QFileDialog::getOpenFileName(
681                 this, tr("Import File"), dir, tr(
682                         "%1 files (*.*);;All Files (*.*)").arg(
683                         QString::fromStdString(format->description())));
684
685         if (file_name.isEmpty())
686                 return;
687
688         // Show the options dialog
689         map<string, Glib::VariantBase> options;
690         if (!format->options().empty()) {
691                 dialogs::InputOutputOptions dlg(
692                         tr("Import %1").arg(QString::fromStdString(
693                                 format->description())),
694                         format->options(), this);
695                 if (!dlg.exec())
696                         return;
697                 options = dlg.options();
698         }
699
700         session_.load_file(file_name, format, options);
701
702         const QString abs_path = QFileInfo(file_name).absolutePath();
703         settings.setValue(SettingOpenDirectory, abs_path);
704 }
705
706 void MainBar::on_device_selected()
707 {
708         shared_ptr<devices::Device> device = device_selector_.selected_device();
709         if (!device) {
710                 reset_device_selector();
711                 return;
712         }
713
714         session_.select_device(device);
715 }
716
717 void MainBar::on_device_changed()
718 {
719         update_device_list();
720         update_device_config_widgets();
721 }
722
723 void MainBar::on_capture_state_changed(int state)
724 {
725         set_capture_state((pv::Session::capture_state)state);
726 }
727
728 void MainBar::on_sample_count_changed()
729 {
730         if (!updating_sample_count_)
731                 commit_sample_count();
732 }
733
734 void MainBar::on_sample_rate_changed()
735 {
736         if (!updating_sample_rate_)
737                 commit_sample_rate();
738 }
739
740 void MainBar::on_config_changed()
741 {
742         commit_sample_count();
743         commit_sample_rate();   
744 }
745
746 void MainBar::on_actionNewView_triggered()
747 {
748         new_view(&session_);
749 }
750
751 void MainBar::on_actionOpen_triggered()
752 {
753         QSettings settings;
754         const QString dir = settings.value(SettingOpenDirectory).toString();
755
756         // Show the dialog
757         const QString file_name = QFileDialog::getOpenFileName(
758                 this, tr("Open File"), dir, tr(
759                         "Sigrok Sessions (*.sr);;"
760                         "All Files (*.*)"));
761
762         if (!file_name.isEmpty()) {
763                 session_.load_file(file_name);
764
765                 const QString abs_path = QFileInfo(file_name).absolutePath();
766                 settings.setValue(SettingOpenDirectory, abs_path);
767         }
768 }
769
770 void MainBar::on_actionSaveAs_triggered()
771 {
772         export_file(session_.device_manager().context()->output_formats()["srzip"]);
773 }
774
775 void MainBar::on_actionSaveSelectionAs_triggered()
776 {
777         export_file(session_.device_manager().context()->output_formats()["srzip"], true);
778 }
779
780 void MainBar::on_actionConnect_triggered()
781 {
782         // Stop any currently running capture session
783         session_.stop_capture();
784
785         dialogs::Connect dlg(this, session_.device_manager());
786
787         // If the user selected a device, select it in the device list. Select the
788         // current device otherwise.
789         if (dlg.exec())
790                 session_.select_device(dlg.get_selected_device());
791
792         update_device_list();
793 }
794
795 void MainBar::add_toolbar_widgets()
796 {
797         addAction(action_new_view_);
798         addSeparator();
799         addWidget(open_button_);
800         addWidget(save_button_);
801         addSeparator();
802
803         StandardBar::add_toolbar_widgets();
804
805         addWidget(&device_selector_);
806         configure_button_action_ = addWidget(&configure_button_);
807         channels_button_action_ = addWidget(&channels_button_);
808         addWidget(&sample_count_);
809         addWidget(&sample_rate_);
810 #ifdef ENABLE_DECODE
811         addSeparator();
812         addWidget(add_decoder_button_);
813 #endif
814 }
815
816 bool MainBar::eventFilter(QObject *watched, QEvent *event)
817 {
818         if (sample_count_supported_ && (watched == &sample_count_ ||
819                         watched == &sample_rate_) &&
820                         (event->type() == QEvent::ToolTip)) {
821                 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
822                 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
823
824                 QString str = tr("Total sampling time: %1").arg(
825                         pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
826                 QToolTip::showText(help_event->globalPos(), str);
827
828                 return true;
829         }
830
831         return false;
832 }
833
834 } // namespace toolbars
835 } // namespace pv