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