2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 #include <libsigrokdecode/libsigrokdecode.h>
30 #include <boost/algorithm/string/join.hpp>
33 #include <QApplication>
34 #include <QButtonGroup>
35 #include <QCloseEvent>
36 #include <QFileDialog>
37 #include <QMessageBox>
42 #include <QVBoxLayout>
45 #include "mainwindow.hpp"
47 #include "devicemanager.hpp"
49 #include "data/segment.hpp"
50 #include "devices/hardwaredevice.hpp"
51 #include "devices/inputfile.hpp"
52 #include "devices/sessionfile.hpp"
53 #include "dialogs/about.hpp"
54 #include "dialogs/connect.hpp"
55 #include "dialogs/inputoutputoptions.hpp"
56 #include "dialogs/storeprogress.hpp"
57 #include "toolbars/mainbar.hpp"
58 #include "view/logicsignal.hpp"
59 #include "view/view.hpp"
60 #include "widgets/exportmenu.hpp"
61 #include "widgets/importmenu.hpp"
63 #include "widgets/decodermenu.hpp"
65 #include "widgets/hidingmenubar.hpp"
71 #include <libsigrokcxx/libsigrokcxx.hpp>
78 using std::shared_ptr;
82 using boost::algorithm::join;
85 using sigrok::OutputFormat;
86 using sigrok::InputFormat;
94 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
95 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
97 MainWindow::MainWindow(DeviceManager &device_manager,
98 string open_file_name, string open_file_format,
101 device_manager_(device_manager),
102 session_(device_manager),
103 action_open_(new QAction(this)),
104 action_save_as_(new QAction(this)),
105 action_save_selection_as_(new QAction(this)),
106 action_connect_(new QAction(this)),
107 action_quit_(new QAction(this)),
108 action_view_zoom_in_(new QAction(this)),
109 action_view_zoom_out_(new QAction(this)),
110 action_view_zoom_fit_(new QAction(this)),
111 action_view_zoom_one_to_one_(new QAction(this)),
112 action_view_sticky_scrolling_(new QAction(this)),
113 action_view_show_cursors_(new QAction(this)),
114 action_about_(new QAction(this))
116 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
120 restore_ui_settings();
121 if (open_file_name.empty())
122 select_init_device();
124 load_init_file(open_file_name, open_file_format);
127 QAction* MainWindow::action_open() const
132 QAction* MainWindow::action_save_as() const
134 return action_save_as_;
137 QAction* MainWindow::action_save_selection_as() const
139 return action_save_selection_as_;
142 QAction* MainWindow::action_connect() const
144 return action_connect_;
147 QAction* MainWindow::action_quit() const
152 QAction* MainWindow::action_view_zoom_in() const
154 return action_view_zoom_in_;
157 QAction* MainWindow::action_view_zoom_out() const
159 return action_view_zoom_out_;
162 QAction* MainWindow::action_view_zoom_fit() const
164 return action_view_zoom_fit_;
167 QAction* MainWindow::action_view_zoom_one_to_one() const
169 return action_view_zoom_one_to_one_;
172 QAction* MainWindow::action_view_sticky_scrolling() const
174 return action_view_sticky_scrolling_;
177 QAction* MainWindow::action_view_show_cursors() const
179 return action_view_show_cursors_;
182 QAction* MainWindow::action_about() const
184 return action_about_;
188 QMenu* MainWindow::menu_decoder_add() const
190 return menu_decoders_add_;
194 void MainWindow::run_stop()
196 switch(session_.get_capture_state()) {
197 case Session::Stopped:
198 session_.start_capture([&](QString message) {
199 session_error("Capture failed", message); });
202 case Session::AwaitingTrigger:
203 case Session::Running:
204 session_.stop_capture();
209 void MainWindow::select_device(shared_ptr<devices::Device> device)
213 session_.set_device(device);
215 session_.set_default_device();
216 } catch(const QString &e) {
217 QMessageBox msg(this);
219 msg.setInformativeText(tr("Failed to Select Device"));
220 msg.setStandardButtons(QMessageBox::Ok);
221 msg.setIcon(QMessageBox::Warning);
226 void MainWindow::export_file(shared_ptr<OutputFormat> format,
229 using pv::dialogs::StoreProgress;
231 // Stop any currently running capture session
232 session_.stop_capture();
235 const QString dir = settings.value(SettingSaveDirectory).toString();
237 std::pair<uint64_t, uint64_t> sample_range;
239 // Selection only? Verify that the cursors are active and fetch their values
240 if (selection_only) {
241 if (!view_->cursors()->enabled()) {
242 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
243 "cursors before you can save the data enclosed by them " \
244 "to a session file (e.g. using ALT-V - Show Cursors)."));
248 const double samplerate = session_.get_samplerate();
250 const pv::util::Timestamp& start_time = view_->cursors()->first()->time();
251 const pv::util::Timestamp& end_time = view_->cursors()->second()->time();
253 const uint64_t start_sample = start_time.convert_to<double>() * samplerate;
254 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
256 sample_range = std::make_pair(start_sample, end_sample);
258 sample_range = std::make_pair(0, 0);
261 // Construct the filter
262 const vector<string> exts = format->extensions();
263 QString filter = tr("%1 files ").arg(
264 QString::fromStdString(format->description()));
269 filter += QString("(*.%1);;%2 (*.*)").arg(
270 QString::fromStdString(join(exts, ", *."))).arg(
273 // Show the file dialog
274 const QString file_name = QFileDialog::getSaveFileName(
275 this, tr("Save File"), dir, filter);
277 if (file_name.isEmpty())
280 const QString abs_path = QFileInfo(file_name).absolutePath();
281 settings.setValue(SettingSaveDirectory, abs_path);
283 // Show the options dialog
284 map<string, Glib::VariantBase> options;
285 if (!format->options().empty()) {
286 dialogs::InputOutputOptions dlg(
287 tr("Export %1").arg(QString::fromStdString(
288 format->description())),
289 format->options(), this);
292 options = dlg.options();
295 StoreProgress *dlg = new StoreProgress(file_name, format, options,
296 sample_range, session_, this);
300 void MainWindow::import_file(shared_ptr<InputFormat> format)
305 const QString dir = settings.value(SettingOpenDirectory).toString();
307 // Construct the filter
308 const vector<string> exts = format->extensions();
309 const QString filter = exts.empty() ? "" :
310 tr("%1 files (*.%2)").arg(
311 QString::fromStdString(format->description())).arg(
312 QString::fromStdString(join(exts, ", *.")));
314 // Show the file dialog
315 const QString file_name = QFileDialog::getOpenFileName(
316 this, tr("Import File"), dir, tr(
317 "%1 files (*.*);;All Files (*.*)").arg(
318 QString::fromStdString(format->description())));
320 if (file_name.isEmpty())
323 // Show the options dialog
324 map<string, Glib::VariantBase> options;
325 if (!format->options().empty()) {
326 dialogs::InputOutputOptions dlg(
327 tr("Import %1").arg(QString::fromStdString(
328 format->description())),
329 format->options(), this);
332 options = dlg.options();
335 load_file(file_name, format, options);
337 const QString abs_path = QFileInfo(file_name).absolutePath();
338 settings.setValue(SettingOpenDirectory, abs_path);
341 void MainWindow::setup_ui()
343 setObjectName(QString::fromUtf8("MainWindow"));
345 // Set the window icon
347 icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
350 // Setup the central widget
351 central_widget_ = new QWidget(this);
352 vertical_layout_ = new QVBoxLayout(central_widget_);
353 vertical_layout_->setSpacing(6);
354 vertical_layout_->setContentsMargins(0, 0, 0, 0);
355 setCentralWidget(central_widget_);
357 view_ = new pv::view::View(session_, this);
359 vertical_layout_->addWidget(view_);
361 // Setup the menu bar
362 pv::widgets::HidingMenuBar *const menu_bar =
363 new pv::widgets::HidingMenuBar(this);
366 QMenu *const menu_file = new QMenu;
367 menu_file->setTitle(tr("&File"));
369 action_open_->setText(tr("&Open..."));
370 action_open_->setIcon(QIcon::fromTheme("document-open",
371 QIcon(":/icons/document-open.png")));
372 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
373 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
374 menu_file->addAction(action_open_);
376 action_save_as_->setText(tr("&Save As..."));
377 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
378 QIcon(":/icons/document-save-as.png")));
379 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
380 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
381 menu_file->addAction(action_save_as_);
383 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
384 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
385 QIcon(":/icons/document-save-as.png")));
386 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
387 action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
388 menu_file->addAction(action_save_selection_as_);
390 menu_file->addSeparator();
392 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
393 device_manager_.context());
394 menu_file_export->setTitle(tr("&Export"));
395 connect(menu_file_export,
396 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
397 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
398 menu_file->addAction(menu_file_export->menuAction());
400 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
401 device_manager_.context());
402 menu_file_import->setTitle(tr("&Import"));
403 connect(menu_file_import,
404 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
405 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
406 menu_file->addAction(menu_file_import->menuAction());
408 menu_file->addSeparator();
410 action_connect_->setText(tr("&Connect to Device..."));
411 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
412 menu_file->addAction(action_connect_);
414 menu_file->addSeparator();
416 action_quit_->setText(tr("&Quit"));
417 action_quit_->setIcon(QIcon::fromTheme("application-exit",
418 QIcon(":/icons/application-exit.png")));
419 action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
420 action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
421 menu_file->addAction(action_quit_);
424 QMenu *menu_view = new QMenu;
425 menu_view->setTitle(tr("&View"));
427 action_view_zoom_in_->setText(tr("Zoom &In"));
428 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
429 QIcon(":/icons/zoom-in.png")));
430 // simply using Qt::Key_Plus shows no + in the menu
431 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
432 action_view_zoom_in_->setObjectName(
433 QString::fromUtf8("actionViewZoomIn"));
434 menu_view->addAction(action_view_zoom_in_);
436 action_view_zoom_out_->setText(tr("Zoom &Out"));
437 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
438 QIcon(":/icons/zoom-out.png")));
439 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
440 action_view_zoom_out_->setObjectName(
441 QString::fromUtf8("actionViewZoomOut"));
442 menu_view->addAction(action_view_zoom_out_);
444 action_view_zoom_fit_->setCheckable(true);
445 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
446 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
447 QIcon(":/icons/zoom-fit.png")));
448 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
449 action_view_zoom_fit_->setObjectName(
450 QString::fromUtf8("actionViewZoomFit"));
451 menu_view->addAction(action_view_zoom_fit_);
453 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
454 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
455 QIcon(":/icons/zoom-original.png")));
456 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
457 action_view_zoom_one_to_one_->setObjectName(
458 QString::fromUtf8("actionViewZoomOneToOne"));
459 menu_view->addAction(action_view_zoom_one_to_one_);
461 menu_file->addSeparator();
463 action_view_sticky_scrolling_->setCheckable(true);
464 action_view_sticky_scrolling_->setChecked(true);
465 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
466 action_view_sticky_scrolling_->setObjectName(
467 QString::fromUtf8("actionViewStickyScrolling"));
468 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
469 menu_view->addAction(action_view_sticky_scrolling_);
471 view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
473 menu_view->addSeparator();
475 action_view_show_cursors_->setCheckable(true);
476 action_view_show_cursors_->setChecked(view_->cursors_shown());
477 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
478 QIcon(":/icons/show-cursors.svg")));
479 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
480 action_view_show_cursors_->setObjectName(
481 QString::fromUtf8("actionViewShowCursors"));
482 action_view_show_cursors_->setText(tr("Show &Cursors"));
483 menu_view->addAction(action_view_show_cursors_);
487 QMenu *const menu_decoders = new QMenu;
488 menu_decoders->setTitle(tr("&Decoders"));
490 menu_decoders_add_->setTitle(tr("&Add"));
491 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
492 this, SLOT(add_decoder(srd_decoder*)));
494 menu_decoders->addMenu(menu_decoders_add_);
498 QMenu *const menu_help = new QMenu;
499 menu_help->setTitle(tr("&Help"));
501 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
502 action_about_->setText(tr("&About..."));
503 menu_help->addAction(action_about_);
505 menu_bar->addAction(menu_file->menuAction());
506 menu_bar->addAction(menu_view->menuAction());
508 menu_bar->addAction(menu_decoders->menuAction());
510 menu_bar->addAction(menu_help->menuAction());
512 setMenuBar(menu_bar);
513 QMetaObject::connectSlotsByName(this);
515 // Also add all actions to the main window for always-enabled hotkeys
516 for (QAction* action : menu_bar->actions())
517 this->addAction(action);
520 main_bar_ = new toolbars::MainBar(session_, *this);
522 // Populate the device list and select the initially selected device
523 update_device_list();
525 addToolBar(main_bar_);
528 setWindowTitle(tr("PulseView"));
530 // Setup session_ events
531 connect(&session_, SIGNAL(capture_state_changed(int)), this,
532 SLOT(capture_state_changed(int)));
533 connect(&session_, SIGNAL(device_selected()), this,
534 SLOT(device_selected()));
536 // Setup view_ events
537 connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this,
538 SLOT(sticky_scrolling_changed(bool)));
539 connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this,
540 SLOT(always_zoom_to_fit_changed(bool)));
543 void MainWindow::select_init_device() {
545 map<string, string> dev_info;
546 list<string> key_list;
548 // Re-select last used device if possible.
549 settings.beginGroup("Device");
550 key_list.push_back("vendor");
551 key_list.push_back("model");
552 key_list.push_back("version");
553 key_list.push_back("serial_num");
554 key_list.push_back("connection_id");
556 for (string key : key_list) {
557 const QString k = QString::fromStdString(key);
558 if (!settings.contains(k))
561 const string value = settings.value(k).toString().toStdString();
563 dev_info.insert(std::make_pair(key, value));
566 const shared_ptr<devices::HardwareDevice> device =
567 device_manager_.find_device_from_info(dev_info);
568 select_device(device);
569 update_device_list();
574 void MainWindow::load_init_file(const std::string &file_name,
575 const std::string &format) {
576 shared_ptr<InputFormat> input_format;
578 if (!format.empty()) {
579 const map<string, shared_ptr<InputFormat> > formats =
580 device_manager_.context()->input_formats();
581 const auto iter = find_if(formats.begin(), formats.end(),
582 [&](const pair<string, shared_ptr<InputFormat> > f) {
583 return f.first == format; });
584 if (iter == formats.end()) {
585 cerr << "Unexpected input format: " << format << endl;
589 input_format = (*iter).second;
592 load_file(QString::fromStdString(file_name), input_format);
596 void MainWindow::save_ui_settings()
600 map<string, string> dev_info;
601 list<string> key_list;
603 settings.beginGroup("MainWindow");
604 settings.setValue("state", saveState());
605 settings.setValue("geometry", saveGeometry());
608 if (session_.device()) {
609 settings.beginGroup("Device");
610 key_list.push_back("vendor");
611 key_list.push_back("model");
612 key_list.push_back("version");
613 key_list.push_back("serial_num");
614 key_list.push_back("connection_id");
616 dev_info = device_manager_.get_device_info(
619 for (string key : key_list) {
621 if (dev_info.count(key))
622 settings.setValue(QString::fromUtf8(key.c_str()),
623 QString::fromUtf8(dev_info.at(key).c_str()));
625 settings.remove(QString::fromUtf8(key.c_str()));
632 void MainWindow::restore_ui_settings()
636 settings.beginGroup("MainWindow");
638 if (settings.contains("geometry")) {
639 restoreGeometry(settings.value("geometry").toByteArray());
640 restoreState(settings.value("state").toByteArray());
647 void MainWindow::session_error(
648 const QString text, const QString info_text)
650 QMetaObject::invokeMethod(this, "show_session_error",
651 Qt::QueuedConnection, Q_ARG(QString, text),
652 Q_ARG(QString, info_text));
655 void MainWindow::update_device_list()
657 main_bar_->update_device_list();
660 void MainWindow::load_file(QString file_name,
661 std::shared_ptr<sigrok::InputFormat> format,
662 const std::map<std::string, Glib::VariantBase> &options)
664 const QString errorMessage(
665 QString("Failed to load file %1").arg(file_name));
669 session_.set_device(shared_ptr<devices::Device>(
670 new devices::InputFile(
671 device_manager_.context(),
672 file_name.toStdString(),
675 session_.set_device(shared_ptr<devices::Device>(
676 new devices::SessionFile(
677 device_manager_.context(),
678 file_name.toStdString())));
680 show_session_error(tr("Failed to load ") + file_name, e.what());
681 session_.set_default_device();
682 update_device_list();
686 update_device_list();
688 session_.start_capture([&, errorMessage](QString infoMessage) {
689 session_error(errorMessage, infoMessage); });
692 void MainWindow::closeEvent(QCloseEvent *event)
698 void MainWindow::keyReleaseEvent(QKeyEvent *event)
700 if (event->key() == Qt::Key_Alt) {
701 menuBar()->setHidden(!menuBar()->isHidden());
702 menuBar()->setFocus();
704 QMainWindow::keyReleaseEvent(event);
707 void MainWindow::show_session_error(
708 const QString text, const QString info_text)
710 QMessageBox msg(this);
712 msg.setInformativeText(info_text);
713 msg.setStandardButtons(QMessageBox::Ok);
714 msg.setIcon(QMessageBox::Warning);
718 void MainWindow::on_actionOpen_triggered()
721 const QString dir = settings.value(SettingOpenDirectory).toString();
724 const QString file_name = QFileDialog::getOpenFileName(
725 this, tr("Open File"), dir, tr(
726 "Sigrok Sessions (*.sr);;"
729 if (!file_name.isEmpty()) {
730 load_file(file_name);
732 const QString abs_path = QFileInfo(file_name).absolutePath();
733 settings.setValue(SettingOpenDirectory, abs_path);
737 void MainWindow::on_actionSaveAs_triggered()
739 export_file(device_manager_.context()->output_formats()["srzip"]);
742 void MainWindow::on_actionSaveSelectionAs_triggered()
744 export_file(device_manager_.context()->output_formats()["srzip"], true);
747 void MainWindow::on_actionConnect_triggered()
749 // Stop any currently running capture session
750 session_.stop_capture();
752 dialogs::Connect dlg(this, device_manager_);
754 // If the user selected a device, select it in the device list. Select the
755 // current device otherwise.
757 select_device(dlg.get_selected_device());
759 update_device_list();
762 void MainWindow::on_actionQuit_triggered()
767 void MainWindow::on_actionViewZoomIn_triggered()
772 void MainWindow::on_actionViewZoomOut_triggered()
777 void MainWindow::on_actionViewZoomFit_triggered()
779 view_->zoom_fit(action_view_zoom_fit_->isChecked());
782 void MainWindow::on_actionViewZoomOneToOne_triggered()
784 view_->zoom_one_to_one();
787 void MainWindow::on_actionViewStickyScrolling_triggered()
789 view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
792 void MainWindow::on_actionViewShowCursors_triggered()
796 const bool show = !view_->cursors_shown();
798 view_->centre_cursors();
800 view_->show_cursors(show);
803 void MainWindow::on_actionAbout_triggered()
805 dialogs::About dlg(device_manager_.context(), this);
809 void MainWindow::sticky_scrolling_changed(bool state)
811 action_view_sticky_scrolling_->setChecked(state);
814 void MainWindow::always_zoom_to_fit_changed(bool state)
816 action_view_zoom_fit_->setChecked(state);
819 void MainWindow::add_decoder(srd_decoder *decoder)
823 session_.add_decoder(decoder);
829 void MainWindow::capture_state_changed(int state)
831 main_bar_->set_capture_state((pv::Session::capture_state)state);
834 void MainWindow::device_selected()
836 // Set the title to include the device/file name
837 const shared_ptr<devices::Device> device = session_.device();
841 const string display_name = device->display_name(device_manager_);
842 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));