]> sigrok.org Git - pulseview.git/blob - pv/toolbars/mainbar.cpp
Use alphabetical order for #includes.
[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/channels.hpp>
47 #include <pv/popups/deviceoptions.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::copy;
60 using std::list;
61 using std::make_pair;
62 using std::map;
63 using std::max;
64 using std::min;
65 using std::pair;
66 using std::set;
67 using std::shared_ptr;
68 using std::string;
69 using std::vector;
70
71 using sigrok::Capability;
72 using sigrok::ConfigKey;
73 using sigrok::Error;
74 using sigrok::InputFormat;
75 using sigrok::OutputFormat;
76
77 using boost::algorithm::join;
78
79 namespace pv {
80 namespace toolbars {
81
82 const uint64_t MainBar::MinSampleCount = 100ULL;
83 const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
84 const uint64_t MainBar::DefaultSampleCount = 1000000;
85
86 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
87 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
88
89 MainBar::MainBar(Session &session, QWidget *parent,
90                 pv::views::TraceView::View *view) :
91         StandardBar(session, parent, view, false),
92         action_new_view_(new QAction(this)),
93         action_open_(new QAction(this)),
94         action_save_as_(new QAction(this)),
95         action_save_selection_as_(new QAction(this)),
96         action_connect_(new QAction(this)),
97         open_button_(new QToolButton()),
98         save_button_(new QToolButton()),
99         device_selector_(parent, session.device_manager(),
100                 action_connect_),
101         configure_button_(this),
102         configure_button_action_(nullptr),
103         channels_button_(this),
104         channels_button_action_(nullptr),
105         sample_count_(" samples", this),
106         sample_rate_("Hz", this),
107         updating_sample_rate_(false),
108         updating_sample_count_(false),
109         sample_count_supported_(false)
110 #ifdef ENABLE_DECODE
111         , add_decoder_button_(new QToolButton()),
112         menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
113 #endif
114 {
115         setObjectName(QString::fromUtf8("MainBar"));
116
117         // Actions
118         action_new_view_->setText(tr("New &View"));
119         action_new_view_->setIcon(QIcon::fromTheme("window-new",
120                 QIcon(":/icons/window-new.png")));
121         connect(action_new_view_, SIGNAL(triggered(bool)),
122                 this, SLOT(on_actionNewView_triggered()));
123
124         action_open_->setText(tr("&Open..."));
125         action_open_->setIcon(QIcon::fromTheme("document-open",
126                 QIcon(":/icons/document-open.png")));
127         action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
128         connect(action_open_, SIGNAL(triggered(bool)),
129                 this, SLOT(on_actionOpen_triggered()));
130
131         action_save_as_->setText(tr("&Save As..."));
132         action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
133                 QIcon(":/icons/document-save-as.png")));
134         action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
135         connect(action_save_as_, SIGNAL(triggered(bool)),
136                 this, SLOT(on_actionSaveAs_triggered()));
137
138         action_save_selection_as_->setText(tr("Save Selected &Range As..."));
139         action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
140                 QIcon(":/icons/document-save-as.png")));
141         action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
142         connect(action_save_selection_as_, SIGNAL(triggered(bool)),
143                 this, SLOT(on_actionSaveSelectionAs_triggered()));
144
145         widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
146                 session.device_manager().context());
147         menu_file_export->setTitle(tr("&Export"));
148         connect(menu_file_export,
149                 SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
150                 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
151
152         widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
153                 session.device_manager().context());
154         menu_file_import->setTitle(tr("&Import"));
155         connect(menu_file_import,
156                 SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
157                 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
158
159         action_connect_->setText(tr("&Connect to Device..."));
160         connect(action_connect_, SIGNAL(triggered(bool)),
161                 this, SLOT(on_actionConnect_triggered()));
162
163         // Open button
164         widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
165                 session.device_manager().context(), action_open_);
166         connect(import_menu,
167                 SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
168                 this,
169                 SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
170
171         open_button_->setMenu(import_menu);
172         open_button_->setDefaultAction(action_open_);
173         open_button_->setPopupMode(QToolButton::MenuButtonPopup);
174
175         // Save button
176         vector<QAction *> open_actions;
177         open_actions.push_back(action_save_as_);
178         open_actions.push_back(action_save_selection_as_);
179
180         widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
181                 session.device_manager().context(),
182                 open_actions);
183         connect(export_menu,
184                 SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
185                 this,
186                 SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
187
188         save_button_->setMenu(export_menu);
189         save_button_->setDefaultAction(action_save_as_);
190         save_button_->setPopupMode(QToolButton::MenuButtonPopup);
191
192         // Device selector menu
193         connect(&device_selector_, SIGNAL(device_selected()),
194                 this, SLOT(on_device_selected()));
195
196         // Setup the decoder button
197 #ifdef ENABLE_DECODE
198         menu_decoders_add_->setTitle(tr("&Add"));
199         connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
200                 this, SLOT(add_decoder(srd_decoder*)));
201
202         add_decoder_button_->setIcon(QIcon::fromTheme("add-decoder",
203                 QIcon(":/icons/add-decoder.svg")));
204         add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
205         add_decoder_button_->setMenu(menu_decoders_add_);
206 #endif
207
208         connect(&sample_count_, SIGNAL(value_changed()),
209                 this, SLOT(on_sample_count_changed()));
210         connect(&sample_rate_, SIGNAL(value_changed()),
211                 this, SLOT(on_sample_rate_changed()));
212
213         sample_count_.show_min_max_step(0, UINT64_MAX, 1);
214
215         set_capture_state(pv::Session::Stopped);
216
217         configure_button_.setToolTip(tr("Configure Device"));
218         configure_button_.setIcon(QIcon::fromTheme("configure",
219                 QIcon(":/icons/configure.png")));
220
221         channels_button_.setToolTip(tr("Configure Channels"));
222         channels_button_.setIcon(QIcon::fromTheme("channels",
223                 QIcon(":/icons/channels.svg")));
224
225         add_toolbar_widgets();
226
227         sample_count_.installEventFilter(this);
228         sample_rate_.installEventFilter(this);
229
230         // Setup session_ events
231         connect(&session_, SIGNAL(capture_state_changed(int)),
232                 this, SLOT(on_capture_state_changed(int)));
233         connect(&session, SIGNAL(device_changed()),
234                 this, SLOT(on_device_changed()));
235
236         update_device_list();
237 }
238
239 void MainBar::update_device_list()
240 {
241         DeviceManager &mgr = session_.device_manager();
242         shared_ptr<devices::Device> selected_device = session_.device();
243         list< shared_ptr<devices::Device> > devs;
244
245         copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
246
247         if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
248                 devs.push_back(selected_device);
249
250         device_selector_.set_device_list(devs, selected_device);
251         update_device_config_widgets();
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*, 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(min_sample_count, max_sample_count);
424
425         if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
426                 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
427                 sample_count = g_variant_get_uint64(gvar.gobj());
428                 if (sample_count == 0) {
429                         sample_count = DefaultSampleCount;
430                         default_count_set = true;
431                 }
432                 sample_count = min(max(sample_count, MinSampleCount),
433                         max_sample_count);
434         }
435
436         sample_count_.set_value(sample_count);
437
438         updating_sample_count_ = false;
439
440         // If we show the default rate then make sure the device uses the same
441         if (default_count_set)
442                 commit_sample_count();
443 }
444
445 void MainBar::update_device_config_widgets()
446 {
447         using namespace pv::popups;
448
449         const shared_ptr<devices::Device> device =
450                 device_selector_.selected_device();
451
452         // Hide the widgets if no device is selected
453         channels_button_action_->setVisible(!!device);
454         if (!device) {
455                 configure_button_action_->setVisible(false);
456                 sample_count_.show_none();
457                 sample_rate_.show_none();
458                 return;
459         }
460
461         const shared_ptr<sigrok::Device> sr_dev = device->device();
462         if (!sr_dev)
463                 return;
464
465         // Update the configure popup
466         DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
467         configure_button_action_->setVisible(
468                 !opts->binding().properties().empty());
469         configure_button_.set_popup(opts);
470
471         // Update the channels popup
472         Channels *const channels = new Channels(session_, this);
473         channels_button_.set_popup(channels);
474
475         // Update supported options.
476         sample_count_supported_ = false;
477
478         if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
479                 sample_count_supported_ = true;
480
481         if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
482                 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
483                         Glib::Variant<guint64>::create(1));
484                         on_config_changed();
485         }
486
487         // Add notification of reconfigure events
488         disconnect(this, SLOT(on_config_changed()));
489         connect(&opts->binding(), SIGNAL(config_changed()),
490                 this, SLOT(on_config_changed()));
491
492         // Update sweep timing widgets.
493         update_sample_count_selector();
494         update_sample_rate_selector();
495 }
496
497 void MainBar::commit_sample_rate()
498 {
499         uint64_t sample_rate = 0;
500
501         const shared_ptr<devices::Device> device =
502                 device_selector_.selected_device();
503         if (!device)
504                 return;
505
506         const shared_ptr<sigrok::Device> sr_dev = device->device();
507
508         sample_rate = sample_rate_.value();
509         if (sample_rate == 0)
510                 return;
511
512         try {
513                 sr_dev->config_set(ConfigKey::SAMPLERATE,
514                         Glib::Variant<guint64>::create(sample_rate));
515                 update_sample_rate_selector();
516         } catch (Error error) {
517                 qDebug() << "Failed to configure samplerate.";
518                 return;
519         }
520
521         // Devices with built-in memory might impose limits on certain
522         // configurations, so let's check what sample count the driver
523         // lets us use now.
524         update_sample_count_selector();
525 }
526
527 void MainBar::commit_sample_count()
528 {
529         uint64_t sample_count = 0;
530
531         const shared_ptr<devices::Device> device =
532                 device_selector_.selected_device();
533         if (!device)
534                 return;
535
536         const shared_ptr<sigrok::Device> sr_dev = device->device();
537
538         sample_count = sample_count_.value();
539         if (sample_count_supported_) {
540                 try {
541                         sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
542                                 Glib::Variant<guint64>::create(sample_count));
543                         update_sample_count_selector();
544                 } catch (Error error) {
545                         qDebug() << "Failed to configure sample count.";
546                         return;
547                 }
548         }
549
550         // Devices with built-in memory might impose limits on certain
551         // configurations, so let's check what sample rate the driver
552         // lets us use now.
553         update_sample_rate_selector();
554 }
555
556 void MainBar::session_error(const QString text, const QString info_text)
557 {
558         QMetaObject::invokeMethod(this, "show_session_error",
559                 Qt::QueuedConnection, Q_ARG(QString, text),
560                 Q_ARG(QString, info_text));
561 }
562
563 void MainBar::show_session_error(const QString text, const QString info_text)
564 {
565         QMessageBox msg(this);
566         msg.setText(text);
567         msg.setInformativeText(info_text);
568         msg.setStandardButtons(QMessageBox::Ok);
569         msg.setIcon(QMessageBox::Warning);
570         msg.exec();
571 }
572
573 void MainBar::add_decoder(srd_decoder *decoder)
574 {
575 #ifdef ENABLE_DECODE
576         assert(decoder);
577         session_.add_decoder(decoder);
578 #else
579         (void)decoder;
580 #endif
581 }
582
583 void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
584 {
585         using pv::dialogs::StoreProgress;
586
587         // Stop any currently running capture session
588         session_.stop_capture();
589
590         QSettings settings;
591         const QString dir = settings.value(SettingSaveDirectory).toString();
592
593         pair<uint64_t, uint64_t> sample_range;
594
595         // Selection only? Verify that the cursors are active and fetch their values
596         if (selection_only) {
597                 views::TraceView::View *trace_view =
598                         qobject_cast<views::TraceView::View*>(session_.main_view().get());
599
600                 if (!trace_view->cursors()->enabled()) {
601                         show_session_error(tr("Missing Cursors"), tr("You need to set the " \
602                                         "cursors before you can save the data enclosed by them " \
603                                         "to a session file (e.g. using ALT-V - Show Cursors)."));
604                         return;
605                 }
606
607                 const double samplerate = session_.get_samplerate();
608
609                 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
610                 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
611
612                 const uint64_t start_sample = (uint64_t)max(
613                         (double)0, start_time.convert_to<double>() * samplerate);
614                 const uint64_t end_sample = (uint64_t)max(
615                         (double)0, end_time.convert_to<double>() * samplerate);
616
617                 sample_range = make_pair(start_sample, end_sample);
618         } else {
619                 sample_range = make_pair(0, 0);
620         }
621
622         // Construct the filter
623         const vector<string> exts = format->extensions();
624         QString filter = tr("%1 files ").arg(
625                 QString::fromStdString(format->description()));
626
627         if (exts.empty())
628                 filter += "(*.*)";
629         else
630                 filter += QString("(*.%1);;%2 (*.*)").arg(
631                         QString::fromStdString(join(exts, ", *.")),
632                         tr("All Files"));
633
634         // Show the file dialog
635         const QString file_name = QFileDialog::getSaveFileName(
636                 this, tr("Save File"), dir, filter);
637
638         if (file_name.isEmpty())
639                 return;
640
641         const QString abs_path = QFileInfo(file_name).absolutePath();
642         settings.setValue(SettingSaveDirectory, abs_path);
643
644         // Show the options dialog
645         map<string, Glib::VariantBase> options;
646         if (!format->options().empty()) {
647                 dialogs::InputOutputOptions dlg(
648                         tr("Export %1").arg(QString::fromStdString(
649                                 format->description())),
650                         format->options(), this);
651                 if (!dlg.exec())
652                         return;
653                 options = dlg.options();
654         }
655
656         if (!selection_only)
657                 session_.set_name(QFileInfo(file_name).fileName());
658
659         StoreProgress *dlg = new StoreProgress(file_name, format, options,
660                 sample_range, session_, this);
661         dlg->run();
662 }
663
664 void MainBar::import_file(shared_ptr<InputFormat> format)
665 {
666         assert(format);
667
668         QSettings settings;
669         const QString dir = settings.value(SettingOpenDirectory).toString();
670
671         // Construct the filter
672         const vector<string> exts = format->extensions();
673         const QString filter = exts.empty() ? "" :
674                 tr("%1 files (*.%2)").arg(
675                         QString::fromStdString(format->description()),
676                         QString::fromStdString(join(exts, ", *.")));
677
678         // Show the file dialog
679         const QString file_name = QFileDialog::getOpenFileName(
680                 this, tr("Import File"), dir, tr(
681                         "%1 files (*.*);;All Files (*.*)").arg(
682                         QString::fromStdString(format->description())));
683
684         if (file_name.isEmpty())
685                 return;
686
687         // Show the options dialog
688         map<string, Glib::VariantBase> options;
689         if (!format->options().empty()) {
690                 dialogs::InputOutputOptions dlg(
691                         tr("Import %1").arg(QString::fromStdString(
692                                 format->description())),
693                         format->options(), this);
694                 if (!dlg.exec())
695                         return;
696                 options = dlg.options();
697         }
698
699         session_.load_file(file_name, format, options);
700
701         const QString abs_path = QFileInfo(file_name).absolutePath();
702         settings.setValue(SettingOpenDirectory, abs_path);
703 }
704
705 void MainBar::on_device_selected()
706 {
707         shared_ptr<devices::Device> device = device_selector_.selected_device();
708         if (!device) {
709                 reset_device_selector();
710                 return;
711         }
712
713         session_.select_device(device);
714 }
715
716 void MainBar::on_device_changed()
717 {
718         update_device_list();
719         update_device_config_widgets();
720 }
721
722 void MainBar::on_capture_state_changed(int state)
723 {
724         set_capture_state((pv::Session::capture_state)state);
725 }
726
727 void MainBar::on_sample_count_changed()
728 {
729         if (!updating_sample_count_)
730                 commit_sample_count();
731 }
732
733 void MainBar::on_sample_rate_changed()
734 {
735         if (!updating_sample_rate_)
736                 commit_sample_rate();
737 }
738
739 void MainBar::on_config_changed()
740 {
741         commit_sample_count();
742         commit_sample_rate();
743 }
744
745 void MainBar::on_actionNewView_triggered()
746 {
747         new_view(&session_);
748 }
749
750 void MainBar::on_actionOpen_triggered()
751 {
752         QSettings settings;
753         const QString dir = settings.value(SettingOpenDirectory).toString();
754
755         // Show the dialog
756         const QString file_name = QFileDialog::getOpenFileName(
757                 this, tr("Open File"), dir, tr(
758                         "Sigrok Sessions (*.sr);;"
759                         "All Files (*.*)"));
760
761         if (!file_name.isEmpty()) {
762                 session_.load_file(file_name);
763
764                 const QString abs_path = QFileInfo(file_name).absolutePath();
765                 settings.setValue(SettingOpenDirectory, abs_path);
766         }
767 }
768
769 void MainBar::on_actionSaveAs_triggered()
770 {
771         export_file(session_.device_manager().context()->output_formats()["srzip"]);
772 }
773
774 void MainBar::on_actionSaveSelectionAs_triggered()
775 {
776         export_file(session_.device_manager().context()->output_formats()["srzip"], true);
777 }
778
779 void MainBar::on_actionConnect_triggered()
780 {
781         // Stop any currently running capture session
782         session_.stop_capture();
783
784         dialogs::Connect dlg(this, session_.device_manager());
785
786         // If the user selected a device, select it in the device list. Select the
787         // current device otherwise.
788         if (dlg.exec())
789                 session_.select_device(dlg.get_selected_device());
790
791         update_device_list();
792 }
793
794 void MainBar::add_toolbar_widgets()
795 {
796         addAction(action_new_view_);
797         addSeparator();
798         addWidget(open_button_);
799         addWidget(save_button_);
800         addSeparator();
801
802         StandardBar::add_toolbar_widgets();
803
804         addWidget(&device_selector_);
805         configure_button_action_ = addWidget(&configure_button_);
806         channels_button_action_ = addWidget(&channels_button_);
807         addWidget(&sample_count_);
808         addWidget(&sample_rate_);
809 #ifdef ENABLE_DECODE
810         addSeparator();
811         addWidget(add_decoder_button_);
812 #endif
813 }
814
815 bool MainBar::eventFilter(QObject *watched, QEvent *event)
816 {
817         if (sample_count_supported_ && (watched == &sample_count_ ||
818                         watched == &sample_rate_) &&
819                         (event->type() == QEvent::ToolTip)) {
820                 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
821                 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
822
823                 QString str = tr("Total sampling time: %1").arg(
824                         pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
825                 QToolTip::showText(help_event->globalPos(), str);
826
827                 return true;
828         }
829
830         return false;
831 }
832
833 } // namespace toolbars
834 } // namespace pv