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 "dialogs/about.hpp"
49 #include "dialogs/connect.hpp"
50 #include "dialogs/inputoutputoptions.hpp"
51 #include "dialogs/storeprogress.hpp"
52 #include "toolbars/mainbar.hpp"
53 #include "view/logicsignal.hpp"
54 #include "view/view.hpp"
55 #include "widgets/exportmenu.hpp"
57 #include "widgets/decodermenu.hpp"
59 #include "widgets/hidingmenubar.hpp"
65 #include <libsigrokcxx/libsigrokcxx.hpp>
69 using std::shared_ptr;
73 using boost::algorithm::join;
77 using sigrok::HardwareDevice;
78 using sigrok::OutputFormat;
86 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
87 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
89 MainWindow::MainWindow(DeviceManager &device_manager,
90 const char *open_file_name,
93 device_manager_(device_manager),
94 session_(device_manager),
95 action_open_(new QAction(this)),
96 action_save_as_(new QAction(this)),
97 action_connect_(new QAction(this)),
98 action_quit_(new QAction(this)),
99 action_view_zoom_in_(new QAction(this)),
100 action_view_zoom_out_(new QAction(this)),
101 action_view_zoom_fit_(new QAction(this)),
102 action_view_zoom_one_to_one_(new QAction(this)),
103 action_view_show_cursors_(new QAction(this)),
104 action_about_(new QAction(this))
106 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
110 restore_ui_settings();
111 if (open_file_name) {
112 const QString s(QString::fromUtf8(open_file_name));
113 QMetaObject::invokeMethod(this, "load_file",
114 Qt::QueuedConnection,
119 QAction* MainWindow::action_open() const
124 QAction* MainWindow::action_save_as() const
126 return action_save_as_;
129 QAction* MainWindow::action_connect() const
131 return action_connect_;
134 QAction* MainWindow::action_quit() const
139 QAction* MainWindow::action_view_zoom_in() const
141 return action_view_zoom_in_;
144 QAction* MainWindow::action_view_zoom_out() const
146 return action_view_zoom_out_;
149 QAction* MainWindow::action_view_zoom_fit() const
151 return action_view_zoom_fit_;
154 QAction* MainWindow::action_view_zoom_one_to_one() const
156 return action_view_zoom_one_to_one_;
159 QAction* MainWindow::action_view_show_cursors() const
161 return action_view_show_cursors_;
164 QAction* MainWindow::action_about() const
166 return action_about_;
170 QMenu* MainWindow::menu_decoder_add() const
172 return menu_decoders_add_;
176 void MainWindow::run_stop()
178 switch(session_.get_capture_state()) {
179 case Session::Stopped:
180 session_.start_capture([&](QString message) {
181 session_error("Capture failed", message); });
184 case Session::AwaitingTrigger:
185 case Session::Running:
186 session_.stop_capture();
191 void MainWindow::select_device(shared_ptr<Device> device)
194 session_.set_device(device);
195 } catch(const QString &e) {
196 QMessageBox msg(this);
198 msg.setInformativeText(tr("Failed to Select Device"));
199 msg.setStandardButtons(QMessageBox::Ok);
200 msg.setIcon(QMessageBox::Warning);
205 void MainWindow::export_file(shared_ptr<OutputFormat> format)
207 using pv::dialogs::StoreProgress;
209 // Stop any currently running capture session
210 session_.stop_capture();
213 const QString dir = settings.value(SettingSaveDirectory).toString();
215 // Construct the filter
216 const vector<string> exts = format->extensions();
217 QString filter = tr("%1 files ").arg(
218 QString::fromStdString(format->description()));
223 filter += QString("(*.%1);;%2 (*.*)").arg(
224 QString::fromStdString(join(exts, ", *."))).arg(
227 // Show the file dialog
228 const QString file_name = QFileDialog::getSaveFileName(
229 this, tr("Save File"), dir, filter);
231 if (file_name.isEmpty())
234 const QString abs_path = QFileInfo(file_name).absolutePath();
235 settings.setValue(SettingSaveDirectory, abs_path);
237 // Show the options dialog
238 map<string, Glib::VariantBase> options;
239 if (!format->options().empty()) {
240 dialogs::InputOutputOptions dlg(
241 tr("Export %1").arg(QString::fromStdString(
242 format->description())),
243 format->options(), this);
246 options = dlg.options();
249 StoreProgress *dlg = new StoreProgress(file_name, format, options,
254 void MainWindow::setup_ui()
256 setObjectName(QString::fromUtf8("MainWindow"));
258 // Set the window icon
260 icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
263 // Setup the central widget
264 central_widget_ = new QWidget(this);
265 vertical_layout_ = new QVBoxLayout(central_widget_);
266 vertical_layout_->setSpacing(6);
267 vertical_layout_->setContentsMargins(0, 0, 0, 0);
268 setCentralWidget(central_widget_);
270 view_ = new pv::view::View(session_, this);
272 vertical_layout_->addWidget(view_);
274 // Setup the menu bar
275 pv::widgets::HidingMenuBar *const menu_bar =
276 new pv::widgets::HidingMenuBar(this);
279 QMenu *const menu_file = new QMenu;
280 menu_file->setTitle(tr("&File"));
282 action_open_->setText(tr("&Open..."));
283 action_open_->setIcon(QIcon::fromTheme("document-open",
284 QIcon(":/icons/document-open.png")));
285 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
286 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
287 menu_file->addAction(action_open_);
289 action_save_as_->setText(tr("&Save As..."));
290 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
291 QIcon(":/icons/document-save-as.png")));
292 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
293 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
294 menu_file->addAction(action_save_as_);
296 menu_file->addSeparator();
298 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
299 device_manager_.context());
300 menu_file_export->setTitle(tr("&Export"));
301 connect(menu_file_export,
302 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
303 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
304 menu_file->addAction(menu_file_export->menuAction());
306 menu_file->addSeparator();
308 action_connect_->setText(tr("&Connect to Device..."));
309 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
310 menu_file->addAction(action_connect_);
312 menu_file->addSeparator();
314 action_quit_->setText(tr("&Quit"));
315 action_quit_->setIcon(QIcon::fromTheme("application-exit",
316 QIcon(":/icons/application-exit.png")));
317 action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
318 action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
319 menu_file->addAction(action_quit_);
322 QMenu *menu_view = new QMenu;
323 menu_view->setTitle(tr("&View"));
325 action_view_zoom_in_->setText(tr("Zoom &In"));
326 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
327 QIcon(":/icons/zoom-in.png")));
328 // simply using Qt::Key_Plus shows no + in the menu
329 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
330 action_view_zoom_in_->setObjectName(
331 QString::fromUtf8("actionViewZoomIn"));
332 menu_view->addAction(action_view_zoom_in_);
334 action_view_zoom_out_->setText(tr("Zoom &Out"));
335 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
336 QIcon(":/icons/zoom-out.png")));
337 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
338 action_view_zoom_out_->setObjectName(
339 QString::fromUtf8("actionViewZoomOut"));
340 menu_view->addAction(action_view_zoom_out_);
342 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
343 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
344 QIcon(":/icons/zoom-fit.png")));
345 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
346 action_view_zoom_fit_->setObjectName(
347 QString::fromUtf8("actionViewZoomFit"));
348 menu_view->addAction(action_view_zoom_fit_);
350 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
351 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
352 QIcon(":/icons/zoom-original.png")));
353 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
354 action_view_zoom_one_to_one_->setObjectName(
355 QString::fromUtf8("actionViewZoomOneToOne"));
356 menu_view->addAction(action_view_zoom_one_to_one_);
358 menu_view->addSeparator();
360 action_view_show_cursors_->setCheckable(true);
361 action_view_show_cursors_->setChecked(view_->cursors_shown());
362 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
363 QIcon(":/icons/show-cursors.svg")));
364 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
365 action_view_show_cursors_->setObjectName(
366 QString::fromUtf8("actionViewShowCursors"));
367 action_view_show_cursors_->setText(tr("Show &Cursors"));
368 menu_view->addAction(action_view_show_cursors_);
372 QMenu *const menu_decoders = new QMenu;
373 menu_decoders->setTitle(tr("&Decoders"));
375 menu_decoders_add_->setTitle(tr("&Add"));
376 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
377 this, SLOT(add_decoder(srd_decoder*)));
379 menu_decoders->addMenu(menu_decoders_add_);
383 QMenu *const menu_help = new QMenu;
384 menu_help->setTitle(tr("&Help"));
386 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
387 action_about_->setText(tr("&About..."));
388 menu_help->addAction(action_about_);
390 menu_bar->addAction(menu_file->menuAction());
391 menu_bar->addAction(menu_view->menuAction());
393 menu_bar->addAction(menu_decoders->menuAction());
395 menu_bar->addAction(menu_help->menuAction());
397 setMenuBar(menu_bar);
398 QMetaObject::connectSlotsByName(this);
401 main_bar_ = new toolbars::MainBar(session_, *this);
403 // Populate the device list and select the initially selected device
404 update_device_list();
406 addToolBar(main_bar_);
409 setWindowTitle(tr("PulseView"));
411 // Setup session_ events
412 connect(&session_, SIGNAL(capture_state_changed(int)), this,
413 SLOT(capture_state_changed(int)));
414 connect(&session_, SIGNAL(device_selected()), this,
415 SLOT(device_selected()));
418 void MainWindow::save_ui_settings()
422 map<string, string> dev_info;
423 list<string> key_list;
425 settings.beginGroup("MainWindow");
426 settings.setValue("state", saveState());
427 settings.setValue("geometry", saveGeometry());
430 if (session_.device()) {
431 settings.beginGroup("Device");
432 key_list.push_back("vendor");
433 key_list.push_back("model");
434 key_list.push_back("version");
435 key_list.push_back("serial_num");
436 key_list.push_back("connection_id");
438 dev_info = device_manager_.get_device_info(
441 for (string key : key_list) {
443 if (dev_info.count(key))
444 settings.setValue(QString::fromUtf8(key.c_str()),
445 QString::fromUtf8(dev_info.at(key).c_str()));
447 settings.remove(QString::fromUtf8(key.c_str()));
454 void MainWindow::restore_ui_settings()
458 shared_ptr<HardwareDevice> device;
460 map<string, string> dev_info;
461 list<string> key_list;
464 settings.beginGroup("MainWindow");
466 if (settings.contains("geometry")) {
467 restoreGeometry(settings.value("geometry").toByteArray());
468 restoreState(settings.value("state").toByteArray());
474 // Re-select last used device if possible.
475 settings.beginGroup("Device");
476 key_list.push_back("vendor");
477 key_list.push_back("model");
478 key_list.push_back("version");
479 key_list.push_back("serial_num");
480 key_list.push_back("connection_id");
482 for (string key : key_list) {
483 if (!settings.contains(QString::fromUtf8(key.c_str())))
486 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
488 if (value.size() > 0)
489 dev_info.insert(std::make_pair(key, value));
492 device = device_manager_.find_device_from_info(dev_info);
495 select_device(device);
496 update_device_list();
502 void MainWindow::session_error(
503 const QString text, const QString info_text)
505 QMetaObject::invokeMethod(this, "show_session_error",
506 Qt::QueuedConnection, Q_ARG(QString, text),
507 Q_ARG(QString, info_text));
510 void MainWindow::update_device_list()
512 main_bar_->update_device_list();
515 void MainWindow::closeEvent(QCloseEvent *event)
521 void MainWindow::keyReleaseEvent(QKeyEvent *event)
523 if (event->key() == Qt::Key_Alt) {
524 menuBar()->setHidden(!menuBar()->isHidden());
525 menuBar()->setFocus();
527 QMainWindow::keyReleaseEvent(event);
530 void MainWindow::load_file(QString file_name)
532 const QString errorMessage(
533 QString("Failed to load file %1").arg(file_name));
534 const QString infoMessage;
537 session_.set_session_file(file_name.toStdString());
539 show_session_error(tr("Failed to load ") + file_name, e.what());
540 session_.set_default_device();
541 update_device_list();
545 update_device_list();
547 session_.start_capture([&, errorMessage, infoMessage](QString) {
548 session_error(errorMessage, infoMessage); });
551 void MainWindow::show_session_error(
552 const QString text, const QString info_text)
554 QMessageBox msg(this);
556 msg.setInformativeText(info_text);
557 msg.setStandardButtons(QMessageBox::Ok);
558 msg.setIcon(QMessageBox::Warning);
562 void MainWindow::on_actionOpen_triggered()
565 const QString dir = settings.value(SettingOpenDirectory).toString();
568 const QString file_name = QFileDialog::getOpenFileName(
569 this, tr("Open File"), dir, tr(
570 "Sigrok Sessions (*.sr);;"
573 if (!file_name.isEmpty()) {
574 load_file(file_name);
576 const QString abs_path = QFileInfo(file_name).absolutePath();
577 settings.setValue(SettingOpenDirectory, abs_path);
581 void MainWindow::on_actionSaveAs_triggered()
583 export_file(device_manager_.context()->output_formats()["srzip"]);
586 void MainWindow::on_actionConnect_triggered()
588 // Stop any currently running capture session
589 session_.stop_capture();
591 dialogs::Connect dlg(this, device_manager_);
593 // If the user selected a device, select it in the device list. Select the
594 // current device otherwise.
596 select_device(dlg.get_selected_device());
598 update_device_list();
601 void MainWindow::on_actionQuit_triggered()
606 void MainWindow::on_actionViewZoomIn_triggered()
611 void MainWindow::on_actionViewZoomOut_triggered()
616 void MainWindow::on_actionViewZoomFit_triggered()
621 void MainWindow::on_actionViewZoomOneToOne_triggered()
623 view_->zoom_one_to_one();
626 void MainWindow::on_actionViewShowCursors_triggered()
630 const bool show = !view_->cursors_shown();
632 view_->centre_cursors();
634 view_->show_cursors(show);
637 void MainWindow::on_actionAbout_triggered()
639 dialogs::About dlg(device_manager_.context(), this);
643 void MainWindow::add_decoder(srd_decoder *decoder)
647 session_.add_decoder(decoder);
653 void MainWindow::capture_state_changed(int state)
655 main_bar_->set_capture_state((pv::Session::capture_state)state);
658 void MainWindow::device_selected()
660 // Set the title to include the device/file name
661 const shared_ptr<sigrok::Device> device = session_.device();
665 const string display_name = device_manager_.get_display_name(device);
666 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));