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"
48 #include "devices/hardwaredevice.hpp"
49 #include "devices/inputfile.hpp"
50 #include "devices/sessionfile.hpp"
51 #include "dialogs/about.hpp"
52 #include "dialogs/connect.hpp"
53 #include "dialogs/inputoutputoptions.hpp"
54 #include "dialogs/storeprogress.hpp"
55 #include "toolbars/mainbar.hpp"
56 #include "view/logicsignal.hpp"
57 #include "view/view.hpp"
58 #include "widgets/exportmenu.hpp"
59 #include "widgets/importmenu.hpp"
61 #include "widgets/decodermenu.hpp"
63 #include "widgets/hidingmenubar.hpp"
69 #include <libsigrokcxx/libsigrokcxx.hpp>
76 using std::shared_ptr;
80 using boost::algorithm::join;
83 using sigrok::OutputFormat;
84 using sigrok::InputFormat;
92 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
93 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
95 MainWindow::MainWindow(DeviceManager &device_manager,
96 string open_file_name, string open_file_format,
99 device_manager_(device_manager),
100 session_(device_manager),
101 action_open_(new QAction(this)),
102 action_save_as_(new QAction(this)),
103 action_connect_(new QAction(this)),
104 action_quit_(new QAction(this)),
105 action_view_zoom_in_(new QAction(this)),
106 action_view_zoom_out_(new QAction(this)),
107 action_view_zoom_fit_(new QAction(this)),
108 action_view_zoom_one_to_one_(new QAction(this)),
109 action_view_sticky_scrolling_(new QAction(this)),
110 action_view_show_cursors_(new QAction(this)),
111 action_about_(new QAction(this))
113 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
117 restore_ui_settings();
118 if (open_file_name.empty())
119 select_init_device();
121 load_init_file(open_file_name, open_file_format);
124 QAction* MainWindow::action_open() const
129 QAction* MainWindow::action_save_as() const
131 return action_save_as_;
134 QAction* MainWindow::action_connect() const
136 return action_connect_;
139 QAction* MainWindow::action_quit() const
144 QAction* MainWindow::action_view_zoom_in() const
146 return action_view_zoom_in_;
149 QAction* MainWindow::action_view_zoom_out() const
151 return action_view_zoom_out_;
154 QAction* MainWindow::action_view_zoom_fit() const
156 return action_view_zoom_fit_;
159 QAction* MainWindow::action_view_zoom_one_to_one() const
161 return action_view_zoom_one_to_one_;
164 QAction* MainWindow::action_view_sticky_scrolling() const
166 return action_view_sticky_scrolling_;
169 QAction* MainWindow::action_view_show_cursors() const
171 return action_view_show_cursors_;
174 QAction* MainWindow::action_about() const
176 return action_about_;
180 QMenu* MainWindow::menu_decoder_add() const
182 return menu_decoders_add_;
186 void MainWindow::run_stop()
188 switch(session_.get_capture_state()) {
189 case Session::Stopped:
190 session_.start_capture([&](QString message) {
191 session_error("Capture failed", message); });
194 case Session::AwaitingTrigger:
195 case Session::Running:
196 session_.stop_capture();
201 void MainWindow::select_device(shared_ptr<devices::Device> device)
205 session_.set_device(device);
207 session_.set_default_device();
208 } catch(const QString &e) {
209 QMessageBox msg(this);
211 msg.setInformativeText(tr("Failed to Select Device"));
212 msg.setStandardButtons(QMessageBox::Ok);
213 msg.setIcon(QMessageBox::Warning);
218 void MainWindow::export_file(shared_ptr<OutputFormat> format)
220 using pv::dialogs::StoreProgress;
222 // Stop any currently running capture session
223 session_.stop_capture();
226 const QString dir = settings.value(SettingSaveDirectory).toString();
228 // Construct the filter
229 const vector<string> exts = format->extensions();
230 QString filter = tr("%1 files ").arg(
231 QString::fromStdString(format->description()));
236 filter += QString("(*.%1);;%2 (*.*)").arg(
237 QString::fromStdString(join(exts, ", *."))).arg(
240 // Show the file dialog
241 const QString file_name = QFileDialog::getSaveFileName(
242 this, tr("Save File"), dir, filter);
244 if (file_name.isEmpty())
247 const QString abs_path = QFileInfo(file_name).absolutePath();
248 settings.setValue(SettingSaveDirectory, abs_path);
250 // Show the options dialog
251 map<string, Glib::VariantBase> options;
252 if (!format->options().empty()) {
253 dialogs::InputOutputOptions dlg(
254 tr("Export %1").arg(QString::fromStdString(
255 format->description())),
256 format->options(), this);
259 options = dlg.options();
262 StoreProgress *dlg = new StoreProgress(file_name, format, options,
267 void MainWindow::import_file(shared_ptr<InputFormat> format)
272 const QString dir = settings.value(SettingOpenDirectory).toString();
274 // Construct the filter
275 const vector<string> exts = format->extensions();
276 const QString filter = exts.empty() ? "" :
277 tr("%1 files (*.%2)").arg(
278 QString::fromStdString(format->description())).arg(
279 QString::fromStdString(join(exts, ", *.")));
281 // Show the file dialog
282 const QString file_name = QFileDialog::getOpenFileName(
283 this, tr("Import File"), dir, tr(
284 "%1 files (*.*);;All Files (*.*)").arg(
285 QString::fromStdString(format->description())));
287 if (file_name.isEmpty())
290 // Show the options dialog
291 map<string, Glib::VariantBase> options;
292 if (!format->options().empty()) {
293 dialogs::InputOutputOptions dlg(
294 tr("Import %1").arg(QString::fromStdString(
295 format->description())),
296 format->options(), this);
299 options = dlg.options();
302 load_file(file_name, format, options);
304 const QString abs_path = QFileInfo(file_name).absolutePath();
305 settings.setValue(SettingOpenDirectory, abs_path);
308 void MainWindow::setup_ui()
310 setObjectName(QString::fromUtf8("MainWindow"));
312 // Set the window icon
314 icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
317 // Setup the central widget
318 central_widget_ = new QWidget(this);
319 vertical_layout_ = new QVBoxLayout(central_widget_);
320 vertical_layout_->setSpacing(6);
321 vertical_layout_->setContentsMargins(0, 0, 0, 0);
322 setCentralWidget(central_widget_);
324 view_ = new pv::view::View(session_, this);
326 vertical_layout_->addWidget(view_);
328 // Setup the menu bar
329 pv::widgets::HidingMenuBar *const menu_bar =
330 new pv::widgets::HidingMenuBar(this);
333 QMenu *const menu_file = new QMenu;
334 menu_file->setTitle(tr("&File"));
336 action_open_->setText(tr("&Open..."));
337 action_open_->setIcon(QIcon::fromTheme("document-open",
338 QIcon(":/icons/document-open.png")));
339 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
340 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
341 menu_file->addAction(action_open_);
343 action_save_as_->setText(tr("&Save As..."));
344 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
345 QIcon(":/icons/document-save-as.png")));
346 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
347 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
348 menu_file->addAction(action_save_as_);
350 menu_file->addSeparator();
352 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
353 device_manager_.context());
354 menu_file_export->setTitle(tr("&Export"));
355 connect(menu_file_export,
356 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
357 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
358 menu_file->addAction(menu_file_export->menuAction());
360 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
361 device_manager_.context());
362 menu_file_import->setTitle(tr("&Import"));
363 connect(menu_file_import,
364 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
365 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
366 menu_file->addAction(menu_file_import->menuAction());
368 menu_file->addSeparator();
370 action_connect_->setText(tr("&Connect to Device..."));
371 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
372 menu_file->addAction(action_connect_);
374 menu_file->addSeparator();
376 action_quit_->setText(tr("&Quit"));
377 action_quit_->setIcon(QIcon::fromTheme("application-exit",
378 QIcon(":/icons/application-exit.png")));
379 action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
380 action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
381 menu_file->addAction(action_quit_);
384 QMenu *menu_view = new QMenu;
385 menu_view->setTitle(tr("&View"));
387 action_view_zoom_in_->setText(tr("Zoom &In"));
388 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
389 QIcon(":/icons/zoom-in.png")));
390 // simply using Qt::Key_Plus shows no + in the menu
391 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
392 action_view_zoom_in_->setObjectName(
393 QString::fromUtf8("actionViewZoomIn"));
394 menu_view->addAction(action_view_zoom_in_);
396 action_view_zoom_out_->setText(tr("Zoom &Out"));
397 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
398 QIcon(":/icons/zoom-out.png")));
399 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
400 action_view_zoom_out_->setObjectName(
401 QString::fromUtf8("actionViewZoomOut"));
402 menu_view->addAction(action_view_zoom_out_);
404 action_view_zoom_fit_->setCheckable(true);
405 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
406 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
407 QIcon(":/icons/zoom-fit.png")));
408 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
409 action_view_zoom_fit_->setObjectName(
410 QString::fromUtf8("actionViewZoomFit"));
411 menu_view->addAction(action_view_zoom_fit_);
413 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
414 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
415 QIcon(":/icons/zoom-original.png")));
416 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
417 action_view_zoom_one_to_one_->setObjectName(
418 QString::fromUtf8("actionViewZoomOneToOne"));
419 menu_view->addAction(action_view_zoom_one_to_one_);
421 menu_file->addSeparator();
423 action_view_sticky_scrolling_->setCheckable(true);
424 action_view_sticky_scrolling_->setChecked(true);
425 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_R));
426 action_view_sticky_scrolling_->setObjectName(
427 QString::fromUtf8("actionViewStickyScrolling"));
428 action_view_sticky_scrolling_->setText(tr("Sticky Sc&rolling"));
429 menu_view->addAction(action_view_sticky_scrolling_);
431 view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
433 menu_view->addSeparator();
435 action_view_show_cursors_->setCheckable(true);
436 action_view_show_cursors_->setChecked(view_->cursors_shown());
437 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
438 QIcon(":/icons/show-cursors.svg")));
439 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
440 action_view_show_cursors_->setObjectName(
441 QString::fromUtf8("actionViewShowCursors"));
442 action_view_show_cursors_->setText(tr("Show &Cursors"));
443 menu_view->addAction(action_view_show_cursors_);
447 QMenu *const menu_decoders = new QMenu;
448 menu_decoders->setTitle(tr("&Decoders"));
450 menu_decoders_add_->setTitle(tr("&Add"));
451 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
452 this, SLOT(add_decoder(srd_decoder*)));
454 menu_decoders->addMenu(menu_decoders_add_);
458 QMenu *const menu_help = new QMenu;
459 menu_help->setTitle(tr("&Help"));
461 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
462 action_about_->setText(tr("&About..."));
463 menu_help->addAction(action_about_);
465 menu_bar->addAction(menu_file->menuAction());
466 menu_bar->addAction(menu_view->menuAction());
468 menu_bar->addAction(menu_decoders->menuAction());
470 menu_bar->addAction(menu_help->menuAction());
472 setMenuBar(menu_bar);
473 QMetaObject::connectSlotsByName(this);
475 // Also add all actions to the main window for always-enabled hotkeys
476 for (QAction* action : menu_bar->actions())
477 this->addAction(action);
480 main_bar_ = new toolbars::MainBar(session_, *this);
482 // Populate the device list and select the initially selected device
483 update_device_list();
485 addToolBar(main_bar_);
488 setWindowTitle(tr("PulseView"));
490 // Setup session_ events
491 connect(&session_, SIGNAL(capture_state_changed(int)), this,
492 SLOT(capture_state_changed(int)));
493 connect(&session_, SIGNAL(device_selected()), this,
494 SLOT(device_selected()));
496 // Setup view_ events
497 connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this,
498 SLOT(sticky_scrolling_changed(bool)));
499 connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this,
500 SLOT(always_zoom_to_fit_changed(bool)));
503 void MainWindow::select_init_device() {
505 map<string, string> dev_info;
506 list<string> key_list;
508 // Re-select last used device if possible.
509 settings.beginGroup("Device");
510 key_list.push_back("vendor");
511 key_list.push_back("model");
512 key_list.push_back("version");
513 key_list.push_back("serial_num");
514 key_list.push_back("connection_id");
516 for (string key : key_list) {
517 const QString k = QString::fromStdString(key);
518 if (!settings.contains(k))
521 const string value = settings.value(k).toString().toStdString();
523 dev_info.insert(std::make_pair(key, value));
526 const shared_ptr<devices::HardwareDevice> device =
527 device_manager_.find_device_from_info(dev_info);
528 select_device(device);
529 update_device_list();
534 void MainWindow::load_init_file(const std::string &file_name,
535 const std::string &format) {
536 shared_ptr<InputFormat> input_format;
538 if (!format.empty()) {
539 const map<string, shared_ptr<InputFormat> > formats =
540 device_manager_.context()->input_formats();
541 const auto iter = find_if(formats.begin(), formats.end(),
542 [&](const pair<string, shared_ptr<InputFormat> > f) {
543 return f.first == format; });
544 if (iter == formats.end()) {
545 cerr << "Unexpected input format: " << format << endl;
549 input_format = (*iter).second;
552 load_file(QString::fromStdString(file_name), input_format);
556 void MainWindow::save_ui_settings()
560 map<string, string> dev_info;
561 list<string> key_list;
563 settings.beginGroup("MainWindow");
564 settings.setValue("state", saveState());
565 settings.setValue("geometry", saveGeometry());
568 if (session_.device()) {
569 settings.beginGroup("Device");
570 key_list.push_back("vendor");
571 key_list.push_back("model");
572 key_list.push_back("version");
573 key_list.push_back("serial_num");
574 key_list.push_back("connection_id");
576 dev_info = device_manager_.get_device_info(
579 for (string key : key_list) {
581 if (dev_info.count(key))
582 settings.setValue(QString::fromUtf8(key.c_str()),
583 QString::fromUtf8(dev_info.at(key).c_str()));
585 settings.remove(QString::fromUtf8(key.c_str()));
592 void MainWindow::restore_ui_settings()
596 settings.beginGroup("MainWindow");
598 if (settings.contains("geometry")) {
599 restoreGeometry(settings.value("geometry").toByteArray());
600 restoreState(settings.value("state").toByteArray());
607 void MainWindow::session_error(
608 const QString text, const QString info_text)
610 QMetaObject::invokeMethod(this, "show_session_error",
611 Qt::QueuedConnection, Q_ARG(QString, text),
612 Q_ARG(QString, info_text));
615 void MainWindow::update_device_list()
617 main_bar_->update_device_list();
620 void MainWindow::load_file(QString file_name,
621 std::shared_ptr<sigrok::InputFormat> format,
622 const std::map<std::string, Glib::VariantBase> &options)
624 const QString errorMessage(
625 QString("Failed to load file %1").arg(file_name));
626 const QString infoMessage;
630 session_.set_device(shared_ptr<devices::Device>(
631 new devices::InputFile(
632 device_manager_.context(),
633 file_name.toStdString(),
636 session_.set_device(shared_ptr<devices::Device>(
637 new devices::SessionFile(
638 device_manager_.context(),
639 file_name.toStdString())));
641 show_session_error(tr("Failed to load ") + file_name, e.what());
642 session_.set_default_device();
643 update_device_list();
647 update_device_list();
649 session_.start_capture([&, errorMessage, infoMessage](QString) {
650 session_error(errorMessage, infoMessage); });
653 void MainWindow::closeEvent(QCloseEvent *event)
659 void MainWindow::keyReleaseEvent(QKeyEvent *event)
661 if (event->key() == Qt::Key_Alt) {
662 menuBar()->setHidden(!menuBar()->isHidden());
663 menuBar()->setFocus();
665 QMainWindow::keyReleaseEvent(event);
668 void MainWindow::show_session_error(
669 const QString text, const QString info_text)
671 QMessageBox msg(this);
673 msg.setInformativeText(info_text);
674 msg.setStandardButtons(QMessageBox::Ok);
675 msg.setIcon(QMessageBox::Warning);
679 void MainWindow::on_actionOpen_triggered()
682 const QString dir = settings.value(SettingOpenDirectory).toString();
685 const QString file_name = QFileDialog::getOpenFileName(
686 this, tr("Open File"), dir, tr(
687 "Sigrok Sessions (*.sr);;"
690 if (!file_name.isEmpty()) {
691 load_file(file_name);
693 const QString abs_path = QFileInfo(file_name).absolutePath();
694 settings.setValue(SettingOpenDirectory, abs_path);
698 void MainWindow::on_actionSaveAs_triggered()
700 export_file(device_manager_.context()->output_formats()["srzip"]);
703 void MainWindow::on_actionConnect_triggered()
705 // Stop any currently running capture session
706 session_.stop_capture();
708 dialogs::Connect dlg(this, device_manager_);
710 // If the user selected a device, select it in the device list. Select the
711 // current device otherwise.
713 select_device(dlg.get_selected_device());
715 update_device_list();
718 void MainWindow::on_actionQuit_triggered()
723 void MainWindow::on_actionViewZoomIn_triggered()
728 void MainWindow::on_actionViewZoomOut_triggered()
733 void MainWindow::on_actionViewZoomFit_triggered()
735 view_->zoom_fit(action_view_zoom_fit_->isChecked());
738 void MainWindow::on_actionViewZoomOneToOne_triggered()
740 view_->zoom_one_to_one();
743 void MainWindow::on_actionViewStickyScrolling_triggered()
745 view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
748 void MainWindow::on_actionViewShowCursors_triggered()
752 const bool show = !view_->cursors_shown();
754 view_->centre_cursors();
756 view_->show_cursors(show);
759 void MainWindow::on_actionAbout_triggered()
761 dialogs::About dlg(device_manager_.context(), this);
765 void MainWindow::sticky_scrolling_changed(bool state)
767 action_view_sticky_scrolling_->setChecked(state);
770 void MainWindow::always_zoom_to_fit_changed(bool state)
772 action_view_zoom_fit_->setChecked(state);
775 void MainWindow::add_decoder(srd_decoder *decoder)
779 session_.add_decoder(decoder);
785 void MainWindow::capture_state_changed(int state)
787 main_bar_->set_capture_state((pv::Session::capture_state)state);
790 void MainWindow::device_selected()
792 // Set the title to include the device/file name
793 const shared_ptr<devices::Device> device = session_.device();
797 const string display_name = device->display_name(device_manager_);
798 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));