]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
Support specifying input formats on the command line
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 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 <cassert>
22
23#ifdef ENABLE_DECODE
24#include <libsigrokdecode/libsigrokdecode.h>
25#endif
26
27#include <algorithm>
28#include <iterator>
29
30#include <boost/algorithm/string/join.hpp>
31
32#include <QAction>
33#include <QApplication>
34#include <QButtonGroup>
35#include <QCloseEvent>
36#include <QFileDialog>
37#include <QMessageBox>
38#include <QMenu>
39#include <QMenuBar>
40#include <QSettings>
41#include <QStatusBar>
42#include <QVBoxLayout>
43#include <QWidget>
44
45#include "mainwindow.hpp"
46
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"
60#ifdef ENABLE_DECODE
61#include "widgets/decodermenu.hpp"
62#endif
63#include "widgets/hidingmenubar.hpp"
64
65#include <inttypes.h>
66#include <stdint.h>
67#include <stdarg.h>
68#include <glib.h>
69#include <libsigrokcxx/libsigrokcxx.hpp>
70
71using std::cerr;
72using std::endl;
73using std::list;
74using std::map;
75using std::pair;
76using std::shared_ptr;
77using std::string;
78using std::vector;
79
80using boost::algorithm::join;
81
82using sigrok::Error;
83using sigrok::OutputFormat;
84using sigrok::InputFormat;
85
86namespace pv {
87
88namespace view {
89class ViewItem;
90}
91
92const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
93const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
94
95MainWindow::MainWindow(DeviceManager &device_manager,
96 string open_file_name, string open_file_format,
97 QWidget *parent) :
98 QMainWindow(parent),
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_show_cursors_(new QAction(this)),
110 action_about_(new QAction(this))
111#ifdef ENABLE_DECODE
112 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
113#endif
114{
115 setup_ui();
116 restore_ui_settings();
117 if (open_file_name.empty())
118 select_init_device();
119 else
120 load_init_file(open_file_name, open_file_format);
121}
122
123QAction* MainWindow::action_open() const
124{
125 return action_open_;
126}
127
128QAction* MainWindow::action_save_as() const
129{
130 return action_save_as_;
131}
132
133QAction* MainWindow::action_connect() const
134{
135 return action_connect_;
136}
137
138QAction* MainWindow::action_quit() const
139{
140 return action_quit_;
141}
142
143QAction* MainWindow::action_view_zoom_in() const
144{
145 return action_view_zoom_in_;
146}
147
148QAction* MainWindow::action_view_zoom_out() const
149{
150 return action_view_zoom_out_;
151}
152
153QAction* MainWindow::action_view_zoom_fit() const
154{
155 return action_view_zoom_fit_;
156}
157
158QAction* MainWindow::action_view_zoom_one_to_one() const
159{
160 return action_view_zoom_one_to_one_;
161}
162
163QAction* MainWindow::action_view_show_cursors() const
164{
165 return action_view_show_cursors_;
166}
167
168QAction* MainWindow::action_about() const
169{
170 return action_about_;
171}
172
173#ifdef ENABLE_DECODE
174QMenu* MainWindow::menu_decoder_add() const
175{
176 return menu_decoders_add_;
177}
178#endif
179
180void MainWindow::run_stop()
181{
182 switch(session_.get_capture_state()) {
183 case Session::Stopped:
184 session_.start_capture([&](QString message) {
185 session_error("Capture failed", message); });
186 break;
187
188 case Session::AwaitingTrigger:
189 case Session::Running:
190 session_.stop_capture();
191 break;
192 }
193}
194
195void MainWindow::select_device(shared_ptr<devices::Device> device)
196{
197 try {
198 if (device)
199 session_.set_device(device);
200 else
201 session_.set_default_device();
202 } catch(const QString &e) {
203 QMessageBox msg(this);
204 msg.setText(e);
205 msg.setInformativeText(tr("Failed to Select Device"));
206 msg.setStandardButtons(QMessageBox::Ok);
207 msg.setIcon(QMessageBox::Warning);
208 msg.exec();
209 }
210}
211
212void MainWindow::export_file(shared_ptr<OutputFormat> format)
213{
214 using pv::dialogs::StoreProgress;
215
216 // Stop any currently running capture session
217 session_.stop_capture();
218
219 QSettings settings;
220 const QString dir = settings.value(SettingSaveDirectory).toString();
221
222 // Construct the filter
223 const vector<string> exts = format->extensions();
224 QString filter = tr("%1 files ").arg(
225 QString::fromStdString(format->description()));
226
227 if (exts.empty())
228 filter += "(*.*)";
229 else
230 filter += QString("(*.%1);;%2 (*.*)").arg(
231 QString::fromStdString(join(exts, ", *."))).arg(
232 tr("All Files"));
233
234 // Show the file dialog
235 const QString file_name = QFileDialog::getSaveFileName(
236 this, tr("Save File"), dir, filter);
237
238 if (file_name.isEmpty())
239 return;
240
241 const QString abs_path = QFileInfo(file_name).absolutePath();
242 settings.setValue(SettingSaveDirectory, abs_path);
243
244 // Show the options dialog
245 map<string, Glib::VariantBase> options;
246 if (!format->options().empty()) {
247 dialogs::InputOutputOptions dlg(
248 tr("Export %1").arg(QString::fromStdString(
249 format->description())),
250 format->options(), this);
251 if (!dlg.exec())
252 return;
253 options = dlg.options();
254 }
255
256 StoreProgress *dlg = new StoreProgress(file_name, format, options,
257 session_, this);
258 dlg->run();
259}
260
261void MainWindow::import_file(shared_ptr<InputFormat> format)
262{
263 assert(format);
264
265 QSettings settings;
266 const QString dir = settings.value(SettingOpenDirectory).toString();
267
268 // Construct the filter
269 const vector<string> exts = format->extensions();
270 const QString filter = exts.empty() ? "" :
271 tr("%1 files (*.%2)").arg(
272 QString::fromStdString(format->description())).arg(
273 QString::fromStdString(join(exts, ", *.")));
274
275 // Show the file dialog
276 const QString file_name = QFileDialog::getOpenFileName(
277 this, tr("Import File"), dir, tr(
278 "%1 files (*.*);;All Files (*.*)").arg(
279 QString::fromStdString(format->description())));
280
281 if (file_name.isEmpty())
282 return;
283
284 // Show the options dialog
285 map<string, Glib::VariantBase> options;
286 if (!format->options().empty()) {
287 dialogs::InputOutputOptions dlg(
288 tr("Import %1").arg(QString::fromStdString(
289 format->description())),
290 format->options(), this);
291 if (!dlg.exec())
292 return;
293 options = dlg.options();
294 }
295
296 load_file(file_name, format, options);
297
298 const QString abs_path = QFileInfo(file_name).absolutePath();
299 settings.setValue(SettingOpenDirectory, abs_path);
300}
301
302void MainWindow::setup_ui()
303{
304 setObjectName(QString::fromUtf8("MainWindow"));
305
306 // Set the window icon
307 QIcon icon;
308 icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
309 setWindowIcon(icon);
310
311 // Setup the central widget
312 central_widget_ = new QWidget(this);
313 vertical_layout_ = new QVBoxLayout(central_widget_);
314 vertical_layout_->setSpacing(6);
315 vertical_layout_->setContentsMargins(0, 0, 0, 0);
316 setCentralWidget(central_widget_);
317
318 view_ = new pv::view::View(session_, this);
319
320 vertical_layout_->addWidget(view_);
321
322 // Setup the menu bar
323 pv::widgets::HidingMenuBar *const menu_bar =
324 new pv::widgets::HidingMenuBar(this);
325
326 // File Menu
327 QMenu *const menu_file = new QMenu;
328 menu_file->setTitle(tr("&File"));
329
330 action_open_->setText(tr("&Open..."));
331 action_open_->setIcon(QIcon::fromTheme("document-open",
332 QIcon(":/icons/document-open.png")));
333 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
334 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
335 menu_file->addAction(action_open_);
336
337 action_save_as_->setText(tr("&Save As..."));
338 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
339 QIcon(":/icons/document-save-as.png")));
340 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
341 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
342 menu_file->addAction(action_save_as_);
343
344 menu_file->addSeparator();
345
346 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
347 device_manager_.context());
348 menu_file_export->setTitle(tr("&Export"));
349 connect(menu_file_export,
350 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
351 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
352 menu_file->addAction(menu_file_export->menuAction());
353
354 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
355 device_manager_.context());
356 menu_file_import->setTitle(tr("&Import"));
357 connect(menu_file_import,
358 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
359 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
360 menu_file->addAction(menu_file_import->menuAction());
361
362 menu_file->addSeparator();
363
364 action_connect_->setText(tr("&Connect to Device..."));
365 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
366 menu_file->addAction(action_connect_);
367
368 menu_file->addSeparator();
369
370 action_quit_->setText(tr("&Quit"));
371 action_quit_->setIcon(QIcon::fromTheme("application-exit",
372 QIcon(":/icons/application-exit.png")));
373 action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
374 action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
375 menu_file->addAction(action_quit_);
376
377 // View Menu
378 QMenu *menu_view = new QMenu;
379 menu_view->setTitle(tr("&View"));
380
381 action_view_zoom_in_->setText(tr("Zoom &In"));
382 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
383 QIcon(":/icons/zoom-in.png")));
384 // simply using Qt::Key_Plus shows no + in the menu
385 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
386 action_view_zoom_in_->setObjectName(
387 QString::fromUtf8("actionViewZoomIn"));
388 menu_view->addAction(action_view_zoom_in_);
389
390 action_view_zoom_out_->setText(tr("Zoom &Out"));
391 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
392 QIcon(":/icons/zoom-out.png")));
393 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
394 action_view_zoom_out_->setObjectName(
395 QString::fromUtf8("actionViewZoomOut"));
396 menu_view->addAction(action_view_zoom_out_);
397
398 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
399 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
400 QIcon(":/icons/zoom-fit.png")));
401 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
402 action_view_zoom_fit_->setObjectName(
403 QString::fromUtf8("actionViewZoomFit"));
404 menu_view->addAction(action_view_zoom_fit_);
405
406 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
407 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
408 QIcon(":/icons/zoom-original.png")));
409 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
410 action_view_zoom_one_to_one_->setObjectName(
411 QString::fromUtf8("actionViewZoomOneToOne"));
412 menu_view->addAction(action_view_zoom_one_to_one_);
413
414 menu_view->addSeparator();
415
416 action_view_show_cursors_->setCheckable(true);
417 action_view_show_cursors_->setChecked(view_->cursors_shown());
418 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
419 QIcon(":/icons/show-cursors.svg")));
420 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
421 action_view_show_cursors_->setObjectName(
422 QString::fromUtf8("actionViewShowCursors"));
423 action_view_show_cursors_->setText(tr("Show &Cursors"));
424 menu_view->addAction(action_view_show_cursors_);
425
426 // Decoders Menu
427#ifdef ENABLE_DECODE
428 QMenu *const menu_decoders = new QMenu;
429 menu_decoders->setTitle(tr("&Decoders"));
430
431 menu_decoders_add_->setTitle(tr("&Add"));
432 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
433 this, SLOT(add_decoder(srd_decoder*)));
434
435 menu_decoders->addMenu(menu_decoders_add_);
436#endif
437
438 // Help Menu
439 QMenu *const menu_help = new QMenu;
440 menu_help->setTitle(tr("&Help"));
441
442 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
443 action_about_->setText(tr("&About..."));
444 menu_help->addAction(action_about_);
445
446 menu_bar->addAction(menu_file->menuAction());
447 menu_bar->addAction(menu_view->menuAction());
448#ifdef ENABLE_DECODE
449 menu_bar->addAction(menu_decoders->menuAction());
450#endif
451 menu_bar->addAction(menu_help->menuAction());
452
453 setMenuBar(menu_bar);
454 QMetaObject::connectSlotsByName(this);
455
456 // Setup the toolbar
457 main_bar_ = new toolbars::MainBar(session_, *this);
458
459 // Populate the device list and select the initially selected device
460 update_device_list();
461
462 addToolBar(main_bar_);
463
464 // Set the title
465 setWindowTitle(tr("PulseView"));
466
467 // Setup session_ events
468 connect(&session_, SIGNAL(capture_state_changed(int)), this,
469 SLOT(capture_state_changed(int)));
470 connect(&session_, SIGNAL(device_selected()), this,
471 SLOT(device_selected()));
472}
473
474void MainWindow::select_init_device() {
475 QSettings settings;
476 map<string, string> dev_info;
477 list<string> key_list;
478
479 // Re-select last used device if possible.
480 settings.beginGroup("Device");
481 key_list.push_back("vendor");
482 key_list.push_back("model");
483 key_list.push_back("version");
484 key_list.push_back("serial_num");
485 key_list.push_back("connection_id");
486
487 for (string key : key_list) {
488 const QString k = QString::fromStdString(key);
489 if (!settings.contains(k))
490 continue;
491
492 const string value = settings.value(k).toString().toStdString();
493 if (!value.empty())
494 dev_info.insert(std::make_pair(key, value));
495 }
496
497 const shared_ptr<devices::HardwareDevice> device =
498 device_manager_.find_device_from_info(dev_info);
499 select_device(device);
500 update_device_list();
501
502 settings.endGroup();
503}
504
505void MainWindow::load_init_file(const std::string &file_name,
506 const std::string &format) {
507 shared_ptr<InputFormat> input_format;
508
509 if (!format.empty()) {
510 const map<string, shared_ptr<InputFormat> > formats =
511 device_manager_.context()->input_formats();
512 const auto iter = find_if(formats.begin(), formats.end(),
513 [&](const pair<string, shared_ptr<InputFormat> > f) {
514 return f.first == format; });
515 if (iter == formats.end()) {
516 cerr << "Unexpected input format: " << format << endl;
517 return;
518 }
519
520 input_format = (*iter).second;
521 }
522
523 load_file(QString::fromStdString(file_name), input_format);
524}
525
526
527void MainWindow::save_ui_settings()
528{
529 QSettings settings;
530
531 map<string, string> dev_info;
532 list<string> key_list;
533
534 settings.beginGroup("MainWindow");
535 settings.setValue("state", saveState());
536 settings.setValue("geometry", saveGeometry());
537 settings.endGroup();
538
539 if (session_.device()) {
540 settings.beginGroup("Device");
541 key_list.push_back("vendor");
542 key_list.push_back("model");
543 key_list.push_back("version");
544 key_list.push_back("serial_num");
545 key_list.push_back("connection_id");
546
547 dev_info = device_manager_.get_device_info(
548 session_.device());
549
550 for (string key : key_list) {
551
552 if (dev_info.count(key))
553 settings.setValue(QString::fromUtf8(key.c_str()),
554 QString::fromUtf8(dev_info.at(key).c_str()));
555 else
556 settings.remove(QString::fromUtf8(key.c_str()));
557 }
558
559 settings.endGroup();
560 }
561}
562
563void MainWindow::restore_ui_settings()
564{
565 QSettings settings;
566
567 settings.beginGroup("MainWindow");
568
569 if (settings.contains("geometry")) {
570 restoreGeometry(settings.value("geometry").toByteArray());
571 restoreState(settings.value("state").toByteArray());
572 } else
573 resize(1000, 720);
574
575 settings.endGroup();
576}
577
578void MainWindow::session_error(
579 const QString text, const QString info_text)
580{
581 QMetaObject::invokeMethod(this, "show_session_error",
582 Qt::QueuedConnection, Q_ARG(QString, text),
583 Q_ARG(QString, info_text));
584}
585
586void MainWindow::update_device_list()
587{
588 main_bar_->update_device_list();
589}
590
591void MainWindow::load_file(QString file_name,
592 std::shared_ptr<sigrok::InputFormat> format,
593 const std::map<std::string, Glib::VariantBase> &options)
594{
595 const QString errorMessage(
596 QString("Failed to load file %1").arg(file_name));
597 const QString infoMessage;
598
599 try {
600 if (format)
601 session_.set_device(shared_ptr<devices::Device>(
602 new devices::InputFile(
603 device_manager_.context(),
604 file_name.toStdString(),
605 format, options)));
606 else
607 session_.set_device(shared_ptr<devices::Device>(
608 new devices::SessionFile(
609 device_manager_.context(),
610 file_name.toStdString())));
611 } catch(Error e) {
612 show_session_error(tr("Failed to load ") + file_name, e.what());
613 session_.set_default_device();
614 update_device_list();
615 return;
616 }
617
618 update_device_list();
619
620 session_.start_capture([&, errorMessage, infoMessage](QString) {
621 session_error(errorMessage, infoMessage); });
622}
623
624void MainWindow::closeEvent(QCloseEvent *event)
625{
626 save_ui_settings();
627 event->accept();
628}
629
630void MainWindow::keyReleaseEvent(QKeyEvent *event)
631{
632 if (event->key() == Qt::Key_Alt) {
633 menuBar()->setHidden(!menuBar()->isHidden());
634 menuBar()->setFocus();
635 }
636 QMainWindow::keyReleaseEvent(event);
637}
638
639void MainWindow::show_session_error(
640 const QString text, const QString info_text)
641{
642 QMessageBox msg(this);
643 msg.setText(text);
644 msg.setInformativeText(info_text);
645 msg.setStandardButtons(QMessageBox::Ok);
646 msg.setIcon(QMessageBox::Warning);
647 msg.exec();
648}
649
650void MainWindow::on_actionOpen_triggered()
651{
652 QSettings settings;
653 const QString dir = settings.value(SettingOpenDirectory).toString();
654
655 // Show the dialog
656 const QString file_name = QFileDialog::getOpenFileName(
657 this, tr("Open File"), dir, tr(
658 "Sigrok Sessions (*.sr);;"
659 "All Files (*.*)"));
660
661 if (!file_name.isEmpty()) {
662 load_file(file_name);
663
664 const QString abs_path = QFileInfo(file_name).absolutePath();
665 settings.setValue(SettingOpenDirectory, abs_path);
666 }
667}
668
669void MainWindow::on_actionSaveAs_triggered()
670{
671 export_file(device_manager_.context()->output_formats()["srzip"]);
672}
673
674void MainWindow::on_actionConnect_triggered()
675{
676 // Stop any currently running capture session
677 session_.stop_capture();
678
679 dialogs::Connect dlg(this, device_manager_);
680
681 // If the user selected a device, select it in the device list. Select the
682 // current device otherwise.
683 if (dlg.exec())
684 select_device(dlg.get_selected_device());
685
686 update_device_list();
687}
688
689void MainWindow::on_actionQuit_triggered()
690{
691 close();
692}
693
694void MainWindow::on_actionViewZoomIn_triggered()
695{
696 view_->zoom(1);
697}
698
699void MainWindow::on_actionViewZoomOut_triggered()
700{
701 view_->zoom(-1);
702}
703
704void MainWindow::on_actionViewZoomFit_triggered()
705{
706 view_->zoom_fit();
707}
708
709void MainWindow::on_actionViewZoomOneToOne_triggered()
710{
711 view_->zoom_one_to_one();
712}
713
714void MainWindow::on_actionViewShowCursors_triggered()
715{
716 assert(view_);
717
718 const bool show = !view_->cursors_shown();
719 if(show)
720 view_->centre_cursors();
721
722 view_->show_cursors(show);
723}
724
725void MainWindow::on_actionAbout_triggered()
726{
727 dialogs::About dlg(device_manager_.context(), this);
728 dlg.exec();
729}
730
731void MainWindow::add_decoder(srd_decoder *decoder)
732{
733#ifdef ENABLE_DECODE
734 assert(decoder);
735 session_.add_decoder(decoder);
736#else
737 (void)decoder;
738#endif
739}
740
741void MainWindow::capture_state_changed(int state)
742{
743 main_bar_->set_capture_state((pv::Session::capture_state)state);
744}
745
746void MainWindow::device_selected()
747{
748 // Set the title to include the device/file name
749 const shared_ptr<devices::Device> device = session_.device();
750 if (!device)
751 return;
752
753 const string display_name = device->display_name(device_manager_);
754 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));
755}
756
757} // namespace pv