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