]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/mainbar.cpp
mainbar: fix build without libsigrokdecode
[pulseview.git] / pv / toolbars / mainbar.cpp
... / ...
CommitLineData
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/views/trace/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#include <pv/data/decodesignal.hpp>
55#endif
56
57#include <libsigrokcxx/libsigrokcxx.hpp>
58
59using std::back_inserter;
60using std::copy;
61using std::list;
62using std::make_pair;
63using std::map;
64using std::max;
65using std::min;
66using std::pair;
67using std::set;
68using std::shared_ptr;
69using std::string;
70using std::vector;
71
72using sigrok::Capability;
73using sigrok::ConfigKey;
74using sigrok::Error;
75using sigrok::InputFormat;
76using sigrok::OutputFormat;
77
78using boost::algorithm::join;
79
80namespace pv {
81namespace toolbars {
82
83const uint64_t MainBar::MinSampleCount = 100ULL;
84const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
85const uint64_t MainBar::DefaultSampleCount = 1000000;
86
87const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
88const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
89
90MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::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(), 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 setContextMenuPolicy(Qt::PreventContextMenu);
117
118 // Actions
119 action_new_view_->setText(tr("New &View"));
120 action_new_view_->setIcon(QIcon::fromTheme("window-new",
121 QIcon(":/icons/window-new.png")));
122 connect(action_new_view_, SIGNAL(triggered(bool)),
123 this, SLOT(on_actionNewView_triggered()));
124
125 action_open_->setText(tr("&Open..."));
126 action_open_->setIcon(QIcon::fromTheme("document-open",
127 QIcon(":/icons/document-open.png")));
128 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
129 connect(action_open_, SIGNAL(triggered(bool)),
130 this, SLOT(on_actionOpen_triggered()));
131
132 action_save_as_->setText(tr("&Save As..."));
133 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
134 QIcon(":/icons/document-save-as.png")));
135 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
136 connect(action_save_as_, SIGNAL(triggered(bool)),
137 this, SLOT(on_actionSaveAs_triggered()));
138
139 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
140 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
141 QIcon(":/icons/document-save-as.png")));
142 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
143 connect(action_save_selection_as_, SIGNAL(triggered(bool)),
144 this, SLOT(on_actionSaveSelectionAs_triggered()));
145
146 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
147 session.device_manager().context());
148 menu_file_export->setTitle(tr("&Export"));
149 connect(menu_file_export, 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, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
156 this, SLOT(import_file(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, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
166 this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
167
168 open_button_->setMenu(import_menu);
169 open_button_->setDefaultAction(action_open_);
170 open_button_->setPopupMode(QToolButton::MenuButtonPopup);
171
172 // Save button
173 vector<QAction*> open_actions;
174 open_actions.push_back(action_save_as_);
175 open_actions.push_back(action_save_selection_as_);
176
177 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
178 session.device_manager().context(), open_actions);
179 connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
180 this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
181
182 save_button_->setMenu(export_menu);
183 save_button_->setDefaultAction(action_save_as_);
184 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
185
186 // Device selector menu
187 connect(&device_selector_, SIGNAL(device_selected()),
188 this, SLOT(on_device_selected()));
189
190 // Setup the decoder button
191#ifdef ENABLE_DECODE
192 menu_decoders_add_->setTitle(tr("&Add"));
193 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
194 this, SLOT(add_decoder(srd_decoder*)));
195
196 add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
197 add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
198 add_decoder_button_->setMenu(menu_decoders_add_);
199 add_decoder_button_->setToolTip(tr("Add low-level, non-stacked protocol decoder"));
200#endif
201
202 connect(&sample_count_, SIGNAL(value_changed()),
203 this, SLOT(on_sample_count_changed()));
204 connect(&sample_rate_, SIGNAL(value_changed()),
205 this, SLOT(on_sample_rate_changed()));
206
207 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
208
209 set_capture_state(pv::Session::Stopped);
210
211 configure_button_.setToolTip(tr("Configure Device"));
212 configure_button_.setIcon(QIcon::fromTheme("preferences-system",
213 QIcon(":/icons/preferences-system.png")));
214
215 channels_button_.setToolTip(tr("Configure Channels"));
216 channels_button_.setIcon(QIcon(":/icons/channels.svg"));
217
218 add_toolbar_widgets();
219
220 sample_count_.installEventFilter(this);
221 sample_rate_.installEventFilter(this);
222
223 // Setup session_ events
224 connect(&session_, SIGNAL(capture_state_changed(int)),
225 this, SLOT(on_capture_state_changed(int)));
226 connect(&session, SIGNAL(device_changed()),
227 this, SLOT(on_device_changed()));
228
229 update_device_list();
230}
231
232void MainBar::update_device_list()
233{
234 DeviceManager &mgr = session_.device_manager();
235 shared_ptr<devices::Device> selected_device = session_.device();
236 list< shared_ptr<devices::Device> > devs;
237
238 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
239
240 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
241 devs.push_back(selected_device);
242
243 device_selector_.set_device_list(devs, selected_device);
244 update_device_config_widgets();
245}
246
247void MainBar::set_capture_state(pv::Session::capture_state state)
248{
249 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
250
251 device_selector_.setEnabled(ui_enabled);
252 configure_button_.setEnabled(ui_enabled);
253 channels_button_.setEnabled(ui_enabled);
254 sample_count_.setEnabled(ui_enabled);
255 sample_rate_.setEnabled(ui_enabled);
256}
257
258void MainBar::reset_device_selector()
259{
260 device_selector_.reset();
261}
262
263QAction* MainBar::action_open() const
264{
265 return action_open_;
266}
267
268QAction* MainBar::action_save_as() const
269{
270 return action_save_as_;
271}
272
273QAction* MainBar::action_save_selection_as() const
274{
275 return action_save_selection_as_;
276}
277
278QAction* MainBar::action_connect() const
279{
280 return action_connect_;
281}
282
283void MainBar::update_sample_rate_selector()
284{
285 Glib::VariantContainerBase gvar_dict;
286 GVariant *gvar_list;
287 const uint64_t *elements = nullptr;
288 gsize num_elements;
289 map< const ConfigKey*, set<Capability> > keys;
290
291 if (updating_sample_rate_) {
292 sample_rate_.show_none();
293 return;
294 }
295
296 const shared_ptr<devices::Device> device = device_selector_.selected_device();
297 if (!device)
298 return;
299
300 assert(!updating_sample_rate_);
301 updating_sample_rate_ = true;
302
303 const shared_ptr<sigrok::Device> sr_dev = device->device();
304
305 sample_rate_.allow_user_entered_values(false);
306 if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
307 try {
308 auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
309 if (gvar.gobj()) {
310 bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
311 gvar).get();
312 sample_rate_.allow_user_entered_values(value);
313 }
314 } catch (Error& error) {
315 // Do nothing
316 }
317 }
318
319
320 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
321 try {
322 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
323 } catch (Error& error) {
324 qDebug() << tr("Failed to get sample rate list:") << error.what();
325 }
326 } else {
327 sample_rate_.show_none();
328 updating_sample_rate_ = false;
329 return;
330 }
331
332 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
333 "samplerate-steps", G_VARIANT_TYPE("at")))) {
334 elements = (const uint64_t *)g_variant_get_fixed_array(
335 gvar_list, &num_elements, sizeof(uint64_t));
336
337 const uint64_t min = elements[0];
338 const uint64_t max = elements[1];
339 const uint64_t step = elements[2];
340
341 g_variant_unref(gvar_list);
342
343 assert(min > 0);
344 assert(max > 0);
345 assert(max > min);
346 assert(step > 0);
347
348 if (step == 1)
349 sample_rate_.show_125_list(min, max);
350 else {
351 // When the step is not 1, we cam't make a 1-2-5-10
352 // list of sample rates, because we may not be able to
353 // make round numbers. Therefore in this case, show a
354 // spin box.
355 sample_rate_.show_min_max_step(min, max, step);
356 }
357 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
358 "samplerates", G_VARIANT_TYPE("at")))) {
359 elements = (const uint64_t *)g_variant_get_fixed_array(
360 gvar_list, &num_elements, sizeof(uint64_t));
361 sample_rate_.show_list(elements, num_elements);
362 g_variant_unref(gvar_list);
363 }
364 updating_sample_rate_ = false;
365
366 update_sample_rate_selector_value();
367}
368
369void MainBar::update_sample_rate_selector_value()
370{
371 if (updating_sample_rate_)
372 return;
373
374 const shared_ptr<devices::Device> device = device_selector_.selected_device();
375 if (!device)
376 return;
377
378 try {
379 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
380 uint64_t samplerate =
381 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
382 assert(!updating_sample_rate_);
383 updating_sample_rate_ = true;
384 sample_rate_.set_value(samplerate);
385 updating_sample_rate_ = false;
386 } catch (Error& error) {
387 qDebug() << tr("Failed to get sample rate:") << error.what();
388 }
389}
390
391void MainBar::update_sample_count_selector()
392{
393 if (updating_sample_count_)
394 return;
395
396 const shared_ptr<devices::Device> device = device_selector_.selected_device();
397 if (!device)
398 return;
399
400 const shared_ptr<sigrok::Device> sr_dev = device->device();
401
402 assert(!updating_sample_count_);
403 updating_sample_count_ = true;
404
405 if (!sample_count_supported_) {
406 sample_count_.show_none();
407 updating_sample_count_ = false;
408 return;
409 }
410
411 uint64_t sample_count = sample_count_.value();
412 uint64_t min_sample_count = 0;
413 uint64_t max_sample_count = MaxSampleCount;
414 bool default_count_set = false;
415
416 if (sample_count == 0) {
417 sample_count = DefaultSampleCount;
418 default_count_set = true;
419 }
420
421 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
422 try {
423 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
424 if (gvar.gobj())
425 g_variant_get(gvar.gobj(), "(tt)",
426 &min_sample_count, &max_sample_count);
427 } catch (Error& error) {
428 qDebug() << tr("Failed to get sample limit list:") << error.what();
429 }
430 }
431
432 min_sample_count = min(max(min_sample_count, MinSampleCount),
433 max_sample_count);
434
435 sample_count_.show_125_list(min_sample_count, max_sample_count);
436
437 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
438 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
439 sample_count = g_variant_get_uint64(gvar.gobj());
440 if (sample_count == 0) {
441 sample_count = DefaultSampleCount;
442 default_count_set = true;
443 }
444 sample_count = min(max(sample_count, MinSampleCount),
445 max_sample_count);
446 }
447
448 sample_count_.set_value(sample_count);
449
450 updating_sample_count_ = false;
451
452 // If we show the default rate then make sure the device uses the same
453 if (default_count_set)
454 commit_sample_count();
455}
456
457void MainBar::update_device_config_widgets()
458{
459 using namespace pv::popups;
460
461 const shared_ptr<devices::Device> device = device_selector_.selected_device();
462
463 // Hide the widgets if no device is selected
464 channels_button_action_->setVisible(!!device);
465 if (!device) {
466 configure_button_action_->setVisible(false);
467 sample_count_.show_none();
468 sample_rate_.show_none();
469 return;
470 }
471
472 const shared_ptr<sigrok::Device> sr_dev = device->device();
473 if (!sr_dev)
474 return;
475
476 // Update the configure popup
477 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
478 configure_button_action_->setVisible(!opts->binding().properties().empty());
479 configure_button_.set_popup(opts);
480
481 // Update the channels popup
482 Channels *const channels = new Channels(session_, this);
483 channels_button_.set_popup(channels);
484
485 // Update supported options.
486 sample_count_supported_ = false;
487
488 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
489 sample_count_supported_ = true;
490
491 // Add notification of reconfigure events
492 disconnect(this, SLOT(on_config_changed()));
493 connect(&opts->binding(), SIGNAL(config_changed()),
494 this, SLOT(on_config_changed()));
495
496 // Update sweep timing widgets.
497 update_sample_count_selector();
498 update_sample_rate_selector();
499}
500
501void MainBar::commit_sample_rate()
502{
503 uint64_t sample_rate = 0;
504
505 const shared_ptr<devices::Device> device = device_selector_.selected_device();
506 if (!device)
507 return;
508
509 const shared_ptr<sigrok::Device> sr_dev = device->device();
510
511 sample_rate = sample_rate_.value();
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() << tr("Failed to configure samplerate:") << error.what();
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
528void MainBar::commit_sample_count()
529{
530 uint64_t sample_count = 0;
531
532 const shared_ptr<devices::Device> device = 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() << tr("Failed to configure sample count:") << error.what();
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
556void MainBar::show_session_error(const QString text, const QString info_text)
557{
558 QMessageBox msg(this);
559 msg.setText(text + "\n\n" + info_text);
560 msg.setStandardButtons(QMessageBox::Ok);
561 msg.setIcon(QMessageBox::Warning);
562 msg.exec();
563}
564
565void MainBar::add_decoder(srd_decoder *decoder)
566{
567#ifdef ENABLE_DECODE
568 assert(decoder);
569 shared_ptr<data::DecodeSignal> signal = session_.add_decode_signal();
570 if (signal)
571 signal->stack_decoder(decoder);
572#else
573 (void)decoder;
574#endif
575}
576
577void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
578{
579 using pv::dialogs::StoreProgress;
580
581 // Stop any currently running capture session
582 session_.stop_capture();
583
584 QSettings settings;
585 const QString dir = settings.value(SettingSaveDirectory).toString();
586
587 pair<uint64_t, uint64_t> sample_range;
588
589 // Selection only? Verify that the cursors are active and fetch their values
590 if (selection_only) {
591 views::trace::View *trace_view =
592 qobject_cast<views::trace::View*>(session_.main_view().get());
593
594 if (!trace_view->cursors()->enabled()) {
595 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
596 "cursors before you can save the data enclosed by them " \
597 "to a session file (e.g. using the Show Cursors button)."));
598 return;
599 }
600
601 const double samplerate = session_.get_samplerate();
602
603 const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
604 const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
605
606 const uint64_t start_sample = (uint64_t)max(
607 0.0, start_time.convert_to<double>() * samplerate);
608 const uint64_t end_sample = (uint64_t)max(
609 0.0, end_time.convert_to<double>() * samplerate);
610
611 if ((start_sample == 0) && (end_sample == 0)) {
612 // Both cursors are negative and were clamped to 0
613 show_session_error(tr("Invalid Range"), tr("The cursors don't " \
614 "define a valid range of samples."));
615 return;
616 }
617
618 sample_range = make_pair(start_sample, end_sample);
619 } else {
620 sample_range = 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
665void 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 = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
675 tr("%1 files").arg(QString::fromStdString(format->description())),
676 QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
677 const QString filter_all = QString::fromStdString("%1 (%2)").arg(
678 tr("All Files"), QString::fromStdString("*"));
679 const QString filter = QString::fromStdString("%1%2%3").arg(
680 exts.empty() ? "" : filter_exts,
681 exts.empty() ? "" : ";;",
682 filter_all);
683
684 // Show the file dialog
685 const QString file_name = QFileDialog::getOpenFileName(
686 this, tr("Import File"), dir, filter);
687
688 if (file_name.isEmpty())
689 return;
690
691 // Show the options dialog
692 map<string, Glib::VariantBase> options;
693 if (!format->options().empty()) {
694 dialogs::InputOutputOptions dlg(
695 tr("Import %1").arg(QString::fromStdString(
696 format->description())),
697 format->options(), this);
698 if (!dlg.exec())
699 return;
700 options = dlg.options();
701 }
702
703 session_.load_file(file_name, format, options);
704
705 const QString abs_path = QFileInfo(file_name).absolutePath();
706 settings.setValue(SettingOpenDirectory, abs_path);
707}
708
709void MainBar::on_device_selected()
710{
711 shared_ptr<devices::Device> device = device_selector_.selected_device();
712
713 if (device)
714 session_.select_device(device);
715 else
716 reset_device_selector();
717}
718
719void MainBar::on_device_changed()
720{
721 update_device_list();
722 update_device_config_widgets();
723}
724
725void MainBar::on_capture_state_changed(int state)
726{
727 set_capture_state((pv::Session::capture_state)state);
728}
729
730void MainBar::on_sample_count_changed()
731{
732 if (!updating_sample_count_)
733 commit_sample_count();
734}
735
736void MainBar::on_sample_rate_changed()
737{
738 if (!updating_sample_rate_)
739 commit_sample_rate();
740}
741
742void MainBar::on_config_changed()
743{
744 // We want to also call update_sample_rate_selector() here in case
745 // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
746 // commit_sample_rate() does this already, so we don't call it here
747
748 commit_sample_count();
749 commit_sample_rate();
750}
751
752void MainBar::on_actionNewView_triggered()
753{
754 new_view(&session_);
755}
756
757void MainBar::on_actionOpen_triggered()
758{
759 QSettings settings;
760 const QString dir = settings.value(SettingOpenDirectory).toString();
761
762 // Show the dialog
763 const QString file_name = QFileDialog::getOpenFileName(
764 this, tr("Open File"), dir, tr(
765 "sigrok Sessions (*.sr);;"
766 "All Files (*)"));
767
768 if (!file_name.isEmpty()) {
769 session_.load_file(file_name);
770
771 const QString abs_path = QFileInfo(file_name).absolutePath();
772 settings.setValue(SettingOpenDirectory, abs_path);
773 }
774}
775
776void MainBar::on_actionSaveAs_triggered()
777{
778 export_file(session_.device_manager().context()->output_formats()["srzip"]);
779}
780
781void MainBar::on_actionSaveSelectionAs_triggered()
782{
783 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
784}
785
786void MainBar::on_actionConnect_triggered()
787{
788 // Stop any currently running capture session
789 session_.stop_capture();
790
791 dialogs::Connect dlg(this, session_.device_manager());
792
793 // If the user selected a device, select it in the device list. Select the
794 // current device otherwise.
795 if (dlg.exec())
796 session_.select_device(dlg.get_selected_device());
797
798 update_device_list();
799}
800
801void MainBar::add_toolbar_widgets()
802{
803 addAction(action_new_view_);
804 addSeparator();
805 addWidget(open_button_);
806 addWidget(save_button_);
807 addSeparator();
808
809 StandardBar::add_toolbar_widgets();
810
811 addWidget(&device_selector_);
812 configure_button_action_ = addWidget(&configure_button_);
813 channels_button_action_ = addWidget(&channels_button_);
814 addWidget(&sample_count_);
815 addWidget(&sample_rate_);
816#ifdef ENABLE_DECODE
817 addSeparator();
818 addWidget(add_decoder_button_);
819#endif
820}
821
822bool MainBar::eventFilter(QObject *watched, QEvent *event)
823{
824 if (sample_count_supported_ && (watched == &sample_count_ ||
825 watched == &sample_rate_) &&
826 (event->type() == QEvent::ToolTip)) {
827
828 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
829 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
830
831 QString str = tr("Total sampling time: %1").arg(
832 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
833 QToolTip::showText(help_event->globalPos(), str);
834
835 return true;
836 }
837
838 return false;
839}
840
841} // namespace toolbars
842} // namespace pv