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