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