]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
MainWindow: Merge save_selection_to_file() back into export_file()
[pulseview.git] / pv / mainwindow.cpp
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 "util.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"
62 #ifdef ENABLE_DECODE
63 #include "widgets/decodermenu.hpp"
64 #endif
65 #include "widgets/hidingmenubar.hpp"
66
67 #include <inttypes.h>
68 #include <stdint.h>
69 #include <stdarg.h>
70 #include <glib.h>
71 #include <libsigrokcxx/libsigrokcxx.hpp>
72
73 using std::cerr;
74 using std::endl;
75 using std::list;
76 using std::map;
77 using std::pair;
78 using std::shared_ptr;
79 using std::string;
80 using std::vector;
81
82 using boost::algorithm::join;
83
84 using sigrok::Error;
85 using sigrok::OutputFormat;
86 using sigrok::InputFormat;
87
88 namespace pv {
89
90 namespace view {
91 class ViewItem;
92 }
93
94 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
95 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
96
97 MainWindow::MainWindow(DeviceManager &device_manager,
98         string open_file_name, string open_file_format,
99         QWidget *parent) :
100         QMainWindow(parent),
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))
115 #ifdef ENABLE_DECODE
116         , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
117 #endif
118 {
119         setup_ui();
120         restore_ui_settings();
121         if (open_file_name.empty())
122                 select_init_device();
123         else
124                 load_init_file(open_file_name, open_file_format);
125 }
126
127 QAction* MainWindow::action_open() const
128 {
129         return action_open_;
130 }
131
132 QAction* MainWindow::action_save_as() const
133 {
134         return action_save_as_;
135 }
136
137 QAction* MainWindow::action_save_selection_as() const
138 {
139         return action_save_selection_as_;
140 }
141
142 QAction* MainWindow::action_connect() const
143 {
144         return action_connect_;
145 }
146
147 QAction* MainWindow::action_quit() const
148 {
149         return action_quit_;
150 }
151
152 QAction* MainWindow::action_view_zoom_in() const
153 {
154         return action_view_zoom_in_;
155 }
156
157 QAction* MainWindow::action_view_zoom_out() const
158 {
159         return action_view_zoom_out_;
160 }
161
162 QAction* MainWindow::action_view_zoom_fit() const
163 {
164         return action_view_zoom_fit_;
165 }
166
167 QAction* MainWindow::action_view_zoom_one_to_one() const
168 {
169         return action_view_zoom_one_to_one_;
170 }
171
172 QAction* MainWindow::action_view_sticky_scrolling() const
173 {
174         return action_view_sticky_scrolling_;
175 }
176
177 QAction* MainWindow::action_view_show_cursors() const
178 {
179         return action_view_show_cursors_;
180 }
181
182 QAction* MainWindow::action_about() const
183 {
184         return action_about_;
185 }
186
187 #ifdef ENABLE_DECODE
188 QMenu* MainWindow::menu_decoder_add() const
189 {
190         return menu_decoders_add_;
191 }
192 #endif
193
194 void MainWindow::run_stop()
195 {
196         switch(session_.get_capture_state()) {
197         case Session::Stopped:
198                 session_.start_capture([&](QString message) {
199                         session_error("Capture failed", message); });
200                 break;
201
202         case Session::AwaitingTrigger:
203         case Session::Running:
204                 session_.stop_capture();
205                 break;
206         }
207 }
208
209 void MainWindow::select_device(shared_ptr<devices::Device> device)
210 {
211         try {
212                 if (device)
213                         session_.set_device(device);
214                 else
215                         session_.set_default_device();
216         } catch(const QString &e) {
217                 QMessageBox msg(this);
218                 msg.setText(e);
219                 msg.setInformativeText(tr("Failed to Select Device"));
220                 msg.setStandardButtons(QMessageBox::Ok);
221                 msg.setIcon(QMessageBox::Warning);
222                 msg.exec();
223         }
224 }
225
226 void MainWindow::export_file(shared_ptr<OutputFormat> format,
227         bool selection_only)
228 {
229         using pv::dialogs::StoreProgress;
230
231         // Stop any currently running capture session
232         session_.stop_capture();
233
234         QSettings settings;
235         const QString dir = settings.value(SettingSaveDirectory).toString();
236
237         std::pair<uint64_t, uint64_t> sample_range;
238
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)."));
245                         return;
246                 }
247
248                 const double samplerate = session_.get_samplerate();
249
250                 const pv::util::Timestamp& start_time = view_->cursors()->first()->time();
251                 const pv::util::Timestamp& end_time = view_->cursors()->second()->time();
252
253                 const uint64_t start_sample = start_time.convert_to<double>() * samplerate;
254             const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
255
256             sample_range = std::make_pair(start_sample, end_sample);
257         } else {
258                 sample_range = std::make_pair(0, 0);
259         }
260
261         // Construct the filter
262         const vector<string> exts = format->extensions();
263         QString filter = tr("%1 files ").arg(
264                 QString::fromStdString(format->description()));
265
266         if (exts.empty())
267                 filter += "(*.*)";
268         else
269                 filter += QString("(*.%1);;%2 (*.*)").arg(
270                         QString::fromStdString(join(exts, ", *."))).arg(
271                         tr("All Files"));
272
273         // Show the file dialog
274         const QString file_name = QFileDialog::getSaveFileName(
275                 this, tr("Save File"), dir, filter);
276
277         if (file_name.isEmpty())
278                 return;
279
280         const QString abs_path = QFileInfo(file_name).absolutePath();
281         settings.setValue(SettingSaveDirectory, abs_path);
282
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);
290                 if (!dlg.exec())
291                         return;
292                 options = dlg.options();
293         }
294
295         StoreProgress *dlg = new StoreProgress(file_name, format, options,
296                 sample_range, session_, this);
297         dlg->run();
298 }
299
300 void MainWindow::import_file(shared_ptr<InputFormat> format)
301 {
302         assert(format);
303
304         QSettings settings;
305         const QString dir = settings.value(SettingOpenDirectory).toString();
306
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, ", *.")));
313
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())));
319
320         if (file_name.isEmpty())
321                 return;
322
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);
330                 if (!dlg.exec())
331                         return;
332                 options = dlg.options();
333         }
334
335         load_file(file_name, format, options);
336
337         const QString abs_path = QFileInfo(file_name).absolutePath();
338         settings.setValue(SettingOpenDirectory, abs_path);
339 }
340
341 void MainWindow::setup_ui()
342 {
343         setObjectName(QString::fromUtf8("MainWindow"));
344
345         // Set the window icon
346         QIcon icon;
347         icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
348         setWindowIcon(icon);
349
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_);
356
357         view_ = new pv::view::View(session_, this);
358
359         vertical_layout_->addWidget(view_);
360
361         // Setup the menu bar
362         pv::widgets::HidingMenuBar *const menu_bar =
363                 new pv::widgets::HidingMenuBar(this);
364
365         // File Menu
366         QMenu *const menu_file = new QMenu;
367         menu_file->setTitle(tr("&File"));
368
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_);
375
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_);
382
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_);
389
390         menu_file->addSeparator();
391
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());
399
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());
407
408         menu_file->addSeparator();
409
410         action_connect_->setText(tr("&Connect to Device..."));
411         action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
412         menu_file->addAction(action_connect_);
413
414         menu_file->addSeparator();
415
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_);
422
423         // View Menu
424         QMenu *menu_view = new QMenu;
425         menu_view->setTitle(tr("&View"));
426
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_);
435
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_);
443
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_);
452
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_);
460
461         menu_file->addSeparator();
462
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_);
470
471         view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
472
473         menu_view->addSeparator();
474
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_);
484
485         // Decoders Menu
486 #ifdef ENABLE_DECODE
487         QMenu *const menu_decoders = new QMenu;
488         menu_decoders->setTitle(tr("&Decoders"));
489
490         menu_decoders_add_->setTitle(tr("&Add"));
491         connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
492                 this, SLOT(add_decoder(srd_decoder*)));
493
494         menu_decoders->addMenu(menu_decoders_add_);
495 #endif
496
497         // Help Menu
498         QMenu *const menu_help = new QMenu;
499         menu_help->setTitle(tr("&Help"));
500
501         action_about_->setObjectName(QString::fromUtf8("actionAbout"));
502         action_about_->setText(tr("&About..."));
503         menu_help->addAction(action_about_);
504
505         menu_bar->addAction(menu_file->menuAction());
506         menu_bar->addAction(menu_view->menuAction());
507 #ifdef ENABLE_DECODE
508         menu_bar->addAction(menu_decoders->menuAction());
509 #endif
510         menu_bar->addAction(menu_help->menuAction());
511
512         setMenuBar(menu_bar);
513         QMetaObject::connectSlotsByName(this);
514
515         // Also add all actions to the main window for always-enabled hotkeys
516         for (QAction* action : menu_bar->actions())
517                 this->addAction(action);
518
519         // Setup the toolbar
520         main_bar_ = new toolbars::MainBar(session_, *this);
521
522         // Populate the device list and select the initially selected device
523         update_device_list();
524
525         addToolBar(main_bar_);
526
527         // Set the title
528         setWindowTitle(tr("PulseView"));
529
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()));
535
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)));
541 }
542
543 void MainWindow::select_init_device() {
544         QSettings settings;
545         map<string, string> dev_info;
546         list<string> key_list;
547
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");
555
556         for (string key : key_list) {
557                 const QString k = QString::fromStdString(key);
558                 if (!settings.contains(k))
559                         continue;
560
561                 const string value = settings.value(k).toString().toStdString();
562                 if (!value.empty())
563                         dev_info.insert(std::make_pair(key, value));
564         }
565
566         const shared_ptr<devices::HardwareDevice> device =
567                 device_manager_.find_device_from_info(dev_info);
568         select_device(device);
569         update_device_list();
570
571         settings.endGroup();
572 }
573
574 void MainWindow::load_init_file(const std::string &file_name,
575         const std::string &format) {
576         shared_ptr<InputFormat> input_format;
577
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;
586                         return;
587                 }
588
589                 input_format = (*iter).second;
590         }
591
592         load_file(QString::fromStdString(file_name), input_format);
593 }
594
595
596 void MainWindow::save_ui_settings()
597 {
598         QSettings settings;
599
600         map<string, string> dev_info;
601         list<string> key_list;
602
603         settings.beginGroup("MainWindow");
604         settings.setValue("state", saveState());
605         settings.setValue("geometry", saveGeometry());
606         settings.endGroup();
607
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");
615
616                 dev_info = device_manager_.get_device_info(
617                         session_.device());
618
619                 for (string key : key_list) {
620
621                         if (dev_info.count(key))
622                                 settings.setValue(QString::fromUtf8(key.c_str()),
623                                                 QString::fromUtf8(dev_info.at(key).c_str()));
624                         else
625                                 settings.remove(QString::fromUtf8(key.c_str()));
626                 }
627
628                 settings.endGroup();
629         }
630 }
631
632 void MainWindow::restore_ui_settings()
633 {
634         QSettings settings;
635
636         settings.beginGroup("MainWindow");
637
638         if (settings.contains("geometry")) {
639                 restoreGeometry(settings.value("geometry").toByteArray());
640                 restoreState(settings.value("state").toByteArray());
641         } else
642                 resize(1000, 720);
643
644         settings.endGroup();
645 }
646
647 void MainWindow::session_error(
648         const QString text, const QString info_text)
649 {
650         QMetaObject::invokeMethod(this, "show_session_error",
651                 Qt::QueuedConnection, Q_ARG(QString, text),
652                 Q_ARG(QString, info_text));
653 }
654
655 void MainWindow::update_device_list()
656 {
657         main_bar_->update_device_list();
658 }
659
660 void MainWindow::load_file(QString file_name,
661         std::shared_ptr<sigrok::InputFormat> format,
662         const std::map<std::string, Glib::VariantBase> &options)
663 {
664         const QString errorMessage(
665                 QString("Failed to load file %1").arg(file_name));
666
667         try {
668                 if (format)
669                         session_.set_device(shared_ptr<devices::Device>(
670                                 new devices::InputFile(
671                                         device_manager_.context(),
672                                         file_name.toStdString(),
673                                         format, options)));
674                 else
675                         session_.set_device(shared_ptr<devices::Device>(
676                                 new devices::SessionFile(
677                                         device_manager_.context(),
678                                         file_name.toStdString())));
679         } catch(Error e) {
680                 show_session_error(tr("Failed to load ") + file_name, e.what());
681                 session_.set_default_device();
682                 update_device_list();
683                 return;
684         }
685
686         update_device_list();
687
688         session_.start_capture([&, errorMessage](QString infoMessage) {
689                 session_error(errorMessage, infoMessage); });
690 }
691
692 void MainWindow::closeEvent(QCloseEvent *event)
693 {
694         save_ui_settings();
695         event->accept();
696 }
697
698 void MainWindow::keyReleaseEvent(QKeyEvent *event)
699 {
700         if (event->key() == Qt::Key_Alt) {
701                 menuBar()->setHidden(!menuBar()->isHidden());
702                 menuBar()->setFocus();
703         }
704         QMainWindow::keyReleaseEvent(event);
705 }
706
707 void MainWindow::show_session_error(
708         const QString text, const QString info_text)
709 {
710         QMessageBox msg(this);
711         msg.setText(text);
712         msg.setInformativeText(info_text);
713         msg.setStandardButtons(QMessageBox::Ok);
714         msg.setIcon(QMessageBox::Warning);
715         msg.exec();
716 }
717
718 void MainWindow::on_actionOpen_triggered()
719 {
720         QSettings settings;
721         const QString dir = settings.value(SettingOpenDirectory).toString();
722
723         // Show the dialog
724         const QString file_name = QFileDialog::getOpenFileName(
725                 this, tr("Open File"), dir, tr(
726                         "Sigrok Sessions (*.sr);;"
727                         "All Files (*.*)"));
728
729         if (!file_name.isEmpty()) {
730                 load_file(file_name);
731
732                 const QString abs_path = QFileInfo(file_name).absolutePath();
733                 settings.setValue(SettingOpenDirectory, abs_path);
734         }
735 }
736
737 void MainWindow::on_actionSaveAs_triggered()
738 {
739         export_file(device_manager_.context()->output_formats()["srzip"]);
740 }
741
742 void MainWindow::on_actionSaveSelectionAs_triggered()
743 {
744         export_file(device_manager_.context()->output_formats()["srzip"], true);
745 }
746
747 void MainWindow::on_actionConnect_triggered()
748 {
749         // Stop any currently running capture session
750         session_.stop_capture();
751
752         dialogs::Connect dlg(this, device_manager_);
753
754         // If the user selected a device, select it in the device list. Select the
755         // current device otherwise.
756         if (dlg.exec())
757                 select_device(dlg.get_selected_device());
758
759         update_device_list();
760 }
761
762 void MainWindow::on_actionQuit_triggered()
763 {
764         close();
765 }
766
767 void MainWindow::on_actionViewZoomIn_triggered()
768 {
769         view_->zoom(1);
770 }
771
772 void MainWindow::on_actionViewZoomOut_triggered()
773 {
774         view_->zoom(-1);
775 }
776
777 void MainWindow::on_actionViewZoomFit_triggered()
778 {
779         view_->zoom_fit(action_view_zoom_fit_->isChecked());
780 }
781
782 void MainWindow::on_actionViewZoomOneToOne_triggered()
783 {
784         view_->zoom_one_to_one();
785 }
786
787 void MainWindow::on_actionViewStickyScrolling_triggered()
788 {
789         view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
790 }
791
792 void MainWindow::on_actionViewShowCursors_triggered()
793 {
794         assert(view_);
795
796         const bool show = !view_->cursors_shown();
797         if (show)
798                 view_->centre_cursors();
799
800         view_->show_cursors(show);
801 }
802
803 void MainWindow::on_actionAbout_triggered()
804 {
805         dialogs::About dlg(device_manager_.context(), this);
806         dlg.exec();
807 }
808
809 void MainWindow::sticky_scrolling_changed(bool state)
810 {
811         action_view_sticky_scrolling_->setChecked(state);
812 }
813
814 void MainWindow::always_zoom_to_fit_changed(bool state)
815 {
816         action_view_zoom_fit_->setChecked(state);
817 }
818
819 void MainWindow::add_decoder(srd_decoder *decoder)
820 {
821 #ifdef ENABLE_DECODE
822         assert(decoder);
823         session_.add_decoder(decoder);
824 #else
825         (void)decoder;
826 #endif
827 }
828
829 void MainWindow::capture_state_changed(int state)
830 {
831         main_bar_->set_capture_state((pv::Session::capture_state)state);
832 }
833
834 void MainWindow::device_selected()
835 {
836         // Set the title to include the device/file name
837         const shared_ptr<devices::Device> device = session_.device();
838         if (!device)
839                 return;
840
841         const string display_name = device->display_name(device_manager_);
842         setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));
843 }
844
845 } // namespace pv