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