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>
31 #include <QApplication>
32 #include <QButtonGroup>
33 #include <QCloseEvent>
34 #include <QFileDialog>
35 #include <QMessageBox>
40 #include <QVBoxLayout>
43 #include "mainwindow.hpp"
45 #include "devicemanager.hpp"
46 #include "dialogs/about.hpp"
47 #include "dialogs/connect.hpp"
48 #include "dialogs/storeprogress.hpp"
49 #include "toolbars/mainbar.hpp"
50 #include "view/logicsignal.hpp"
51 #include "view/view.hpp"
52 #include "widgets/exportmenu.hpp"
54 #include "widgets/decodermenu.hpp"
56 #include "widgets/hidingmenubar.hpp"
62 #include <libsigrokcxx/libsigrokcxx.hpp>
66 using std::shared_ptr;
71 using sigrok::HardwareDevice;
72 using sigrok::OutputFormat;
80 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
81 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
83 MainWindow::MainWindow(DeviceManager &device_manager,
84 const char *open_file_name,
87 device_manager_(device_manager),
88 session_(device_manager),
89 action_open_(new QAction(this)),
90 action_save_as_(new QAction(this)),
91 action_connect_(new QAction(this)),
92 action_quit_(new QAction(this)),
93 action_view_zoom_in_(new QAction(this)),
94 action_view_zoom_out_(new QAction(this)),
95 action_view_zoom_fit_(new QAction(this)),
96 action_view_zoom_one_to_one_(new QAction(this)),
97 action_view_show_cursors_(new QAction(this)),
98 action_about_(new QAction(this)),
99 menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
102 restore_ui_settings();
103 if (open_file_name) {
104 const QString s(QString::fromUtf8(open_file_name));
105 QMetaObject::invokeMethod(this, "load_file",
106 Qt::QueuedConnection,
111 QAction* MainWindow::action_open() const
116 QAction* MainWindow::action_save_as() const
118 return action_save_as_;
121 QAction* MainWindow::action_connect() const
123 return action_connect_;
126 QAction* MainWindow::action_quit() const
131 QAction* MainWindow::action_view_zoom_in() const
133 return action_view_zoom_in_;
136 QAction* MainWindow::action_view_zoom_out() const
138 return action_view_zoom_out_;
141 QAction* MainWindow::action_view_zoom_fit() const
143 return action_view_zoom_fit_;
146 QAction* MainWindow::action_view_zoom_one_to_one() const
148 return action_view_zoom_one_to_one_;
151 QAction* MainWindow::action_view_show_cursors() const
153 return action_view_show_cursors_;
156 QAction* MainWindow::action_about() const
158 return action_about_;
161 QMenu* MainWindow::menu_decoder_add() const
163 return menu_decoders_add_;
166 void MainWindow::run_stop()
168 switch(session_.get_capture_state()) {
169 case Session::Stopped:
170 session_.start_capture([&](QString message) {
171 session_error("Capture failed", message); });
174 case Session::AwaitingTrigger:
175 case Session::Running:
176 session_.stop_capture();
181 void MainWindow::select_device(shared_ptr<Device> device)
184 session_.set_device(device);
185 } catch(const QString &e) {
186 QMessageBox msg(this);
188 msg.setInformativeText(tr("Failed to Select Device"));
189 msg.setStandardButtons(QMessageBox::Ok);
190 msg.setIcon(QMessageBox::Warning);
195 void MainWindow::export_file(shared_ptr<OutputFormat> format)
197 using pv::dialogs::StoreProgress;
199 // Stop any currently running capture session
200 session_.stop_capture();
203 const QString dir = settings.value(SettingSaveDirectory).toString();
206 const QString file_name = QFileDialog::getSaveFileName(
207 this, tr("Save File"), dir, tr("%1 files (*.*)").arg(
208 QString::fromStdString(format->description())));
210 if (file_name.isEmpty())
213 const QString abs_path = QFileInfo(file_name).absolutePath();
214 settings.setValue(SettingSaveDirectory, abs_path);
216 StoreProgress *dlg = new StoreProgress(file_name, format,
221 void MainWindow::setup_ui()
223 setObjectName(QString::fromUtf8("MainWindow"));
225 // Set the window icon
227 icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
230 // Setup the central widget
231 central_widget_ = new QWidget(this);
232 vertical_layout_ = new QVBoxLayout(central_widget_);
233 vertical_layout_->setSpacing(6);
234 vertical_layout_->setContentsMargins(0, 0, 0, 0);
235 setCentralWidget(central_widget_);
237 view_ = new pv::view::View(session_, this);
239 vertical_layout_->addWidget(view_);
241 // Setup the menu bar
242 pv::widgets::HidingMenuBar *const menu_bar =
243 new pv::widgets::HidingMenuBar(this);
246 QMenu *const menu_file = new QMenu;
247 menu_file->setTitle(tr("&File"));
249 action_open_->setText(tr("&Open..."));
250 action_open_->setIcon(QIcon::fromTheme("document-open",
251 QIcon(":/icons/document-open.png")));
252 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
253 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
254 menu_file->addAction(action_open_);
256 action_save_as_->setText(tr("&Save As..."));
257 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
258 QIcon(":/icons/document-save-as.png")));
259 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
260 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
261 menu_file->addAction(action_save_as_);
263 menu_file->addSeparator();
265 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
266 device_manager_.context());
267 menu_file_export->setTitle(tr("&Export"));
268 connect(menu_file_export,
269 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
270 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
271 menu_file->addAction(menu_file_export->menuAction());
273 menu_file->addSeparator();
275 action_connect_->setText(tr("&Connect to Device..."));
276 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
277 menu_file->addAction(action_connect_);
279 menu_file->addSeparator();
281 action_quit_->setText(tr("&Quit"));
282 action_quit_->setIcon(QIcon::fromTheme("application-exit",
283 QIcon(":/icons/application-exit.png")));
284 action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
285 action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
286 menu_file->addAction(action_quit_);
289 QMenu *menu_view = new QMenu;
290 menu_view->setTitle(tr("&View"));
292 action_view_zoom_in_->setText(tr("Zoom &In"));
293 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
294 QIcon(":/icons/zoom-in.png")));
295 // simply using Qt::Key_Plus shows no + in the menu
296 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
297 action_view_zoom_in_->setObjectName(
298 QString::fromUtf8("actionViewZoomIn"));
299 menu_view->addAction(action_view_zoom_in_);
301 action_view_zoom_out_->setText(tr("Zoom &Out"));
302 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
303 QIcon(":/icons/zoom-out.png")));
304 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
305 action_view_zoom_out_->setObjectName(
306 QString::fromUtf8("actionViewZoomOut"));
307 menu_view->addAction(action_view_zoom_out_);
309 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
310 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
311 QIcon(":/icons/zoom-fit.png")));
312 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
313 action_view_zoom_fit_->setObjectName(
314 QString::fromUtf8("actionViewZoomFit"));
315 menu_view->addAction(action_view_zoom_fit_);
317 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
318 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
319 QIcon(":/icons/zoom-original.png")));
320 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
321 action_view_zoom_one_to_one_->setObjectName(
322 QString::fromUtf8("actionViewZoomOneToOne"));
323 menu_view->addAction(action_view_zoom_one_to_one_);
325 menu_view->addSeparator();
327 action_view_show_cursors_->setCheckable(true);
328 action_view_show_cursors_->setChecked(view_->cursors_shown());
329 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
330 QIcon(":/icons/show-cursors.svg")));
331 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
332 action_view_show_cursors_->setObjectName(
333 QString::fromUtf8("actionViewShowCursors"));
334 action_view_show_cursors_->setText(tr("Show &Cursors"));
335 menu_view->addAction(action_view_show_cursors_);
339 QMenu *const menu_decoders = new QMenu;
340 menu_decoders->setTitle(tr("&Decoders"));
342 menu_decoders_add_->setTitle(tr("&Add"));
343 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
344 this, SLOT(add_decoder(srd_decoder*)));
346 menu_decoders->addMenu(menu_decoders_add_);
350 QMenu *const menu_help = new QMenu;
351 menu_help->setTitle(tr("&Help"));
353 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
354 action_about_->setText(tr("&About..."));
355 menu_help->addAction(action_about_);
357 menu_bar->addAction(menu_file->menuAction());
358 menu_bar->addAction(menu_view->menuAction());
360 menu_bar->addAction(menu_decoders->menuAction());
362 menu_bar->addAction(menu_help->menuAction());
364 setMenuBar(menu_bar);
365 QMetaObject::connectSlotsByName(this);
368 main_bar_ = new toolbars::MainBar(session_, *this);
370 // Populate the device list and select the initially selected device
371 update_device_list();
373 addToolBar(main_bar_);
376 setWindowTitle(tr("PulseView"));
378 // Setup session_ events
379 connect(&session_, SIGNAL(capture_state_changed(int)), this,
380 SLOT(capture_state_changed(int)));
381 connect(&session_, SIGNAL(device_selected()), this,
382 SLOT(device_selected()));
385 void MainWindow::save_ui_settings()
389 map<string, string> dev_info;
390 list<string> key_list;
392 settings.beginGroup("MainWindow");
393 settings.setValue("state", saveState());
394 settings.setValue("geometry", saveGeometry());
397 if (session_.device()) {
398 settings.beginGroup("Device");
399 key_list.push_back("vendor");
400 key_list.push_back("model");
401 key_list.push_back("version");
402 key_list.push_back("serial_num");
403 key_list.push_back("connection_id");
405 dev_info = device_manager_.get_device_info(
408 for (string key : key_list) {
410 if (dev_info.count(key))
411 settings.setValue(QString::fromUtf8(key.c_str()),
412 QString::fromUtf8(dev_info.at(key).c_str()));
414 settings.remove(QString::fromUtf8(key.c_str()));
421 void MainWindow::restore_ui_settings()
425 shared_ptr<HardwareDevice> device;
427 map<string, string> dev_info;
428 list<string> key_list;
431 settings.beginGroup("MainWindow");
433 if (settings.contains("geometry")) {
434 restoreGeometry(settings.value("geometry").toByteArray());
435 restoreState(settings.value("state").toByteArray());
441 // Re-select last used device if possible.
442 settings.beginGroup("Device");
443 key_list.push_back("vendor");
444 key_list.push_back("model");
445 key_list.push_back("version");
446 key_list.push_back("serial_num");
447 key_list.push_back("connection_id");
449 for (string key : key_list) {
450 if (!settings.contains(QString::fromUtf8(key.c_str())))
453 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
455 if (value.size() > 0)
456 dev_info.insert(std::make_pair(key, value));
459 device = device_manager_.find_device_from_info(dev_info);
462 select_device(device);
463 update_device_list();
469 void MainWindow::session_error(
470 const QString text, const QString info_text)
472 QMetaObject::invokeMethod(this, "show_session_error",
473 Qt::QueuedConnection, Q_ARG(QString, text),
474 Q_ARG(QString, info_text));
477 void MainWindow::update_device_list()
479 main_bar_->update_device_list();
482 void MainWindow::closeEvent(QCloseEvent *event)
488 void MainWindow::keyReleaseEvent(QKeyEvent *event)
490 if (event->key() == Qt::Key_Alt) {
491 menuBar()->setHidden(!menuBar()->isHidden());
492 menuBar()->setFocus();
494 QMainWindow::keyReleaseEvent(event);
497 void MainWindow::load_file(QString file_name)
499 const QString errorMessage(
500 QString("Failed to load file %1").arg(file_name));
501 const QString infoMessage;
504 session_.set_session_file(file_name.toStdString());
506 show_session_error(tr("Failed to load ") + file_name, e.what());
507 session_.set_default_device();
508 update_device_list();
512 update_device_list();
514 session_.start_capture([&, errorMessage, infoMessage](QString) {
515 session_error(errorMessage, infoMessage); });
518 void MainWindow::show_session_error(
519 const QString text, const QString info_text)
521 QMessageBox msg(this);
523 msg.setInformativeText(info_text);
524 msg.setStandardButtons(QMessageBox::Ok);
525 msg.setIcon(QMessageBox::Warning);
529 void MainWindow::on_actionOpen_triggered()
532 const QString dir = settings.value(SettingOpenDirectory).toString();
535 const QString file_name = QFileDialog::getOpenFileName(
536 this, tr("Open File"), dir, tr(
537 "Sigrok Sessions (*.sr);;"
540 if (!file_name.isEmpty()) {
541 load_file(file_name);
543 const QString abs_path = QFileInfo(file_name).absolutePath();
544 settings.setValue(SettingOpenDirectory, abs_path);
548 void MainWindow::on_actionSaveAs_triggered()
550 export_file(device_manager_.context()->output_formats()["srzip"]);
553 void MainWindow::on_actionConnect_triggered()
555 // Stop any currently running capture session
556 session_.stop_capture();
558 dialogs::Connect dlg(this, device_manager_);
560 // If the user selected a device, select it in the device list. Select the
561 // current device otherwise.
563 select_device(dlg.get_selected_device());
565 update_device_list();
568 void MainWindow::on_actionQuit_triggered()
573 void MainWindow::on_actionViewZoomIn_triggered()
578 void MainWindow::on_actionViewZoomOut_triggered()
583 void MainWindow::on_actionViewZoomFit_triggered()
588 void MainWindow::on_actionViewZoomOneToOne_triggered()
590 view_->zoom_one_to_one();
593 void MainWindow::on_actionViewShowCursors_triggered()
597 const bool show = !view_->cursors_shown();
599 view_->centre_cursors();
601 view_->show_cursors(show);
604 void MainWindow::on_actionAbout_triggered()
606 dialogs::About dlg(device_manager_.context(), this);
610 void MainWindow::add_decoder(srd_decoder *decoder)
614 session_.add_decoder(decoder);
620 void MainWindow::capture_state_changed(int state)
622 main_bar_->set_capture_state((pv::Session::capture_state)state);
625 void MainWindow::device_selected()
627 // Set the title to include the device/file name
628 const shared_ptr<sigrok::Device> device = session_.device();
632 const string display_name = device_manager_.get_display_name(device);
633 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));