Fix #685 by adding a special T marker when SR_DT_TRIGGER arrives
[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         qRegisterMetaType<util::Timestamp>("util::Timestamp");
120
121         setup_ui();
122         restore_ui_settings();
123         if (open_file_name.empty())
124                 select_init_device();
125         else
126                 load_init_file(open_file_name, open_file_format);
127 }
128
129 QAction* MainWindow::action_open() const
130 {
131         return action_open_;
132 }
133
134 QAction* MainWindow::action_save_as() const
135 {
136         return action_save_as_;
137 }
138
139 QAction* MainWindow::action_save_selection_as() const
140 {
141         return action_save_selection_as_;
142 }
143
144 QAction* MainWindow::action_connect() const
145 {
146         return action_connect_;
147 }
148
149 QAction* MainWindow::action_quit() const
150 {
151         return action_quit_;
152 }
153
154 QAction* MainWindow::action_view_zoom_in() const
155 {
156         return action_view_zoom_in_;
157 }
158
159 QAction* MainWindow::action_view_zoom_out() const
160 {
161         return action_view_zoom_out_;
162 }
163
164 QAction* MainWindow::action_view_zoom_fit() const
165 {
166         return action_view_zoom_fit_;
167 }
168
169 QAction* MainWindow::action_view_zoom_one_to_one() const
170 {
171         return action_view_zoom_one_to_one_;
172 }
173
174 QAction* MainWindow::action_view_sticky_scrolling() const
175 {
176         return action_view_sticky_scrolling_;
177 }
178
179 QAction* MainWindow::action_view_show_cursors() const
180 {
181         return action_view_show_cursors_;
182 }
183
184 QAction* MainWindow::action_about() const
185 {
186         return action_about_;
187 }
188
189 #ifdef ENABLE_DECODE
190 QMenu* MainWindow::menu_decoder_add() const
191 {
192         return menu_decoders_add_;
193 }
194 #endif
195
196 void MainWindow::run_stop()
197 {
198         switch(session_.get_capture_state()) {
199         case Session::Stopped:
200                 session_.start_capture([&](QString message) {
201                         session_error("Capture failed", message); });
202                 break;
203
204         case Session::AwaitingTrigger:
205         case Session::Running:
206                 session_.stop_capture();
207                 break;
208         }
209 }
210
211 void MainWindow::select_device(shared_ptr<devices::Device> device)
212 {
213         try {
214                 if (device)
215                         session_.set_device(device);
216                 else
217                         session_.set_default_device();
218         } catch(const QString &e) {
219                 QMessageBox msg(this);
220                 msg.setText(e);
221                 msg.setInformativeText(tr("Failed to Select Device"));
222                 msg.setStandardButtons(QMessageBox::Ok);
223                 msg.setIcon(QMessageBox::Warning);
224                 msg.exec();
225         }
226 }
227
228 void MainWindow::export_file(shared_ptr<OutputFormat> format,
229         bool selection_only)
230 {
231         using pv::dialogs::StoreProgress;
232
233         // Stop any currently running capture session
234         session_.stop_capture();
235
236         QSettings settings;
237         const QString dir = settings.value(SettingSaveDirectory).toString();
238
239         std::pair<uint64_t, uint64_t> sample_range;
240
241         // Selection only? Verify that the cursors are active and fetch their values
242         if (selection_only) {
243                 if (!view_->cursors()->enabled()) {
244                         show_session_error(tr("Missing Cursors"), tr("You need to set the " \
245                                         "cursors before you can save the data enclosed by them " \
246                                         "to a session file (e.g. using ALT-V - Show Cursors)."));
247                         return;
248                 }
249
250                 const double samplerate = session_.get_samplerate();
251
252                 const pv::util::Timestamp& start_time = view_->cursors()->first()->time();
253                 const pv::util::Timestamp& end_time = view_->cursors()->second()->time();
254
255                 const uint64_t start_sample = start_time.convert_to<double>() * samplerate;
256                 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
257
258                 sample_range = std::make_pair(start_sample, end_sample);
259         } else {
260                 sample_range = std::make_pair(0, 0);
261         }
262
263         // Construct the filter
264         const vector<string> exts = format->extensions();
265         QString filter = tr("%1 files ").arg(
266                 QString::fromStdString(format->description()));
267
268         if (exts.empty())
269                 filter += "(*.*)";
270         else
271                 filter += QString("(*.%1);;%2 (*.*)").arg(
272                         QString::fromStdString(join(exts, ", *."))).arg(
273                         tr("All Files"));
274
275         // Show the file dialog
276         const QString file_name = QFileDialog::getSaveFileName(
277                 this, tr("Save File"), dir, filter);
278
279         if (file_name.isEmpty())
280                 return;
281
282         const QString abs_path = QFileInfo(file_name).absolutePath();
283         settings.setValue(SettingSaveDirectory, abs_path);
284
285         // Show the options dialog
286         map<string, Glib::VariantBase> options;
287         if (!format->options().empty()) {
288                 dialogs::InputOutputOptions dlg(
289                         tr("Export %1").arg(QString::fromStdString(
290                                 format->description())),
291                         format->options(), this);
292                 if (!dlg.exec())
293                         return;
294                 options = dlg.options();
295         }
296
297         StoreProgress *dlg = new StoreProgress(file_name, format, options,
298                 sample_range, session_, this);
299         dlg->run();
300 }
301
302 void MainWindow::import_file(shared_ptr<InputFormat> format)
303 {
304         assert(format);
305
306         QSettings settings;
307         const QString dir = settings.value(SettingOpenDirectory).toString();
308
309         // Construct the filter
310         const vector<string> exts = format->extensions();
311         const QString filter = exts.empty() ? "" :
312                 tr("%1 files (*.%2)").arg(
313                         QString::fromStdString(format->description())).arg(
314                         QString::fromStdString(join(exts, ", *.")));
315
316         // Show the file dialog
317         const QString file_name = QFileDialog::getOpenFileName(
318                 this, tr("Import File"), dir, tr(
319                         "%1 files (*.*);;All Files (*.*)").arg(
320                         QString::fromStdString(format->description())));
321
322         if (file_name.isEmpty())
323                 return;
324
325         // Show the options dialog
326         map<string, Glib::VariantBase> options;
327         if (!format->options().empty()) {
328                 dialogs::InputOutputOptions dlg(
329                         tr("Import %1").arg(QString::fromStdString(
330                                 format->description())),
331                         format->options(), this);
332                 if (!dlg.exec())
333                         return;
334                 options = dlg.options();
335         }
336
337         load_file(file_name, format, options);
338
339         const QString abs_path = QFileInfo(file_name).absolutePath();
340         settings.setValue(SettingOpenDirectory, abs_path);
341 }
342
343 void MainWindow::setup_ui()
344 {
345         setObjectName(QString::fromUtf8("MainWindow"));
346
347         // Set the window icon
348         QIcon icon;
349         icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
350         setWindowIcon(icon);
351
352         // Setup the central widget
353         central_widget_ = new QWidget(this);
354         vertical_layout_ = new QVBoxLayout(central_widget_);
355         vertical_layout_->setSpacing(6);
356         vertical_layout_->setContentsMargins(0, 0, 0, 0);
357         setCentralWidget(central_widget_);
358
359         view_ = new pv::view::View(session_, this);
360
361         vertical_layout_->addWidget(view_);
362
363         // Setup the menu bar
364         pv::widgets::HidingMenuBar *const menu_bar =
365                 new pv::widgets::HidingMenuBar(this);
366
367         // File Menu
368         QMenu *const menu_file = new QMenu;
369         menu_file->setTitle(tr("&File"));
370
371         action_open_->setText(tr("&Open..."));
372         action_open_->setIcon(QIcon::fromTheme("document-open",
373                 QIcon(":/icons/document-open.png")));
374         action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
375         action_open_->setObjectName(QString::fromUtf8("actionOpen"));
376         menu_file->addAction(action_open_);
377
378         action_save_as_->setText(tr("&Save As..."));
379         action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
380                 QIcon(":/icons/document-save-as.png")));
381         action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
382         action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
383         menu_file->addAction(action_save_as_);
384
385         action_save_selection_as_->setText(tr("Save Selected &Range As..."));
386         action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
387                 QIcon(":/icons/document-save-as.png")));
388         action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
389         action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
390         menu_file->addAction(action_save_selection_as_);
391
392         menu_file->addSeparator();
393
394         widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
395                 device_manager_.context());
396         menu_file_export->setTitle(tr("&Export"));
397         connect(menu_file_export,
398                 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
399                 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
400         menu_file->addAction(menu_file_export->menuAction());
401
402         widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
403                 device_manager_.context());
404         menu_file_import->setTitle(tr("&Import"));
405         connect(menu_file_import,
406                 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
407                 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
408         menu_file->addAction(menu_file_import->menuAction());
409
410         menu_file->addSeparator();
411
412         action_connect_->setText(tr("&Connect to Device..."));
413         action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
414         menu_file->addAction(action_connect_);
415
416         menu_file->addSeparator();
417
418         action_quit_->setText(tr("&Quit"));
419         action_quit_->setIcon(QIcon::fromTheme("application-exit",
420                 QIcon(":/icons/application-exit.png")));
421         action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
422         action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
423         menu_file->addAction(action_quit_);
424
425         // View Menu
426         QMenu *menu_view = new QMenu;
427         menu_view->setTitle(tr("&View"));
428
429         action_view_zoom_in_->setText(tr("Zoom &In"));
430         action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
431                 QIcon(":/icons/zoom-in.png")));
432         // simply using Qt::Key_Plus shows no + in the menu
433         action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
434         action_view_zoom_in_->setObjectName(
435                 QString::fromUtf8("actionViewZoomIn"));
436         menu_view->addAction(action_view_zoom_in_);
437
438         action_view_zoom_out_->setText(tr("Zoom &Out"));
439         action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
440                 QIcon(":/icons/zoom-out.png")));
441         action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
442         action_view_zoom_out_->setObjectName(
443                 QString::fromUtf8("actionViewZoomOut"));
444         menu_view->addAction(action_view_zoom_out_);
445
446         action_view_zoom_fit_->setCheckable(true);
447         action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
448         action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
449                 QIcon(":/icons/zoom-fit.png")));
450         action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
451         action_view_zoom_fit_->setObjectName(
452                 QString::fromUtf8("actionViewZoomFit"));
453         menu_view->addAction(action_view_zoom_fit_);
454
455         action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
456         action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
457                 QIcon(":/icons/zoom-original.png")));
458         action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
459         action_view_zoom_one_to_one_->setObjectName(
460                 QString::fromUtf8("actionViewZoomOneToOne"));
461         menu_view->addAction(action_view_zoom_one_to_one_);
462
463         menu_file->addSeparator();
464
465         action_view_sticky_scrolling_->setCheckable(true);
466         action_view_sticky_scrolling_->setChecked(true);
467         action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
468         action_view_sticky_scrolling_->setObjectName(
469                 QString::fromUtf8("actionViewStickyScrolling"));
470         action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
471         menu_view->addAction(action_view_sticky_scrolling_);
472
473         view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
474
475         menu_view->addSeparator();
476
477         action_view_show_cursors_->setCheckable(true);
478         action_view_show_cursors_->setChecked(view_->cursors_shown());
479         action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
480                 QIcon(":/icons/show-cursors.svg")));
481         action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
482         action_view_show_cursors_->setObjectName(
483                 QString::fromUtf8("actionViewShowCursors"));
484         action_view_show_cursors_->setText(tr("Show &Cursors"));
485         menu_view->addAction(action_view_show_cursors_);
486
487         // Decoders Menu
488 #ifdef ENABLE_DECODE
489         QMenu *const menu_decoders = new QMenu;
490         menu_decoders->setTitle(tr("&Decoders"));
491
492         menu_decoders_add_->setTitle(tr("&Add"));
493         connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
494                 this, SLOT(add_decoder(srd_decoder*)));
495
496         menu_decoders->addMenu(menu_decoders_add_);
497 #endif
498
499         // Help Menu
500         QMenu *const menu_help = new QMenu;
501         menu_help->setTitle(tr("&Help"));
502
503         action_about_->setObjectName(QString::fromUtf8("actionAbout"));
504         action_about_->setText(tr("&About..."));
505         menu_help->addAction(action_about_);
506
507         menu_bar->addAction(menu_file->menuAction());
508         menu_bar->addAction(menu_view->menuAction());
509 #ifdef ENABLE_DECODE
510         menu_bar->addAction(menu_decoders->menuAction());
511 #endif
512         menu_bar->addAction(menu_help->menuAction());
513
514         setMenuBar(menu_bar);
515         QMetaObject::connectSlotsByName(this);
516
517         // Also add all actions to the main window for always-enabled hotkeys
518         for (QAction* action : menu_bar->actions())
519                 this->addAction(action);
520
521         // Setup the toolbar
522         main_bar_ = new toolbars::MainBar(session_, *this);
523
524         // Populate the device list and select the initially selected device
525         update_device_list();
526
527         addToolBar(main_bar_);
528
529         // Set the title
530         setWindowTitle(tr("PulseView"));
531
532         // Setup session_ events
533         connect(&session_, SIGNAL(capture_state_changed(int)), this,
534                 SLOT(capture_state_changed(int)));
535         connect(&session_, SIGNAL(device_selected()), this,
536                 SLOT(device_selected()));
537         connect(&session_, SIGNAL(trigger_event(util::Timestamp)), view_,
538                 SLOT(trigger_event(util::Timestamp)));
539
540         // Setup view_ events
541         connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this,
542                 SLOT(sticky_scrolling_changed(bool)));
543         connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this,
544                 SLOT(always_zoom_to_fit_changed(bool)));
545
546 }
547
548 void MainWindow::select_init_device() {
549         QSettings settings;
550         map<string, string> dev_info;
551         list<string> key_list;
552
553         // Re-select last used device if possible.
554         settings.beginGroup("Device");
555         key_list.push_back("vendor");
556         key_list.push_back("model");
557         key_list.push_back("version");
558         key_list.push_back("serial_num");
559         key_list.push_back("connection_id");
560
561         for (string key : key_list) {
562                 const QString k = QString::fromStdString(key);
563                 if (!settings.contains(k))
564                         continue;
565
566                 const string value = settings.value(k).toString().toStdString();
567                 if (!value.empty())
568                         dev_info.insert(std::make_pair(key, value));
569         }
570
571         const shared_ptr<devices::HardwareDevice> device =
572                 device_manager_.find_device_from_info(dev_info);
573         select_device(device);
574         update_device_list();
575
576         settings.endGroup();
577 }
578
579 void MainWindow::load_init_file(const std::string &file_name,
580         const std::string &format) {
581         shared_ptr<InputFormat> input_format;
582
583         if (!format.empty()) {
584                 const map<string, shared_ptr<InputFormat> > formats =
585                         device_manager_.context()->input_formats();
586                 const auto iter = find_if(formats.begin(), formats.end(),
587                         [&](const pair<string, shared_ptr<InputFormat> > f) {
588                                 return f.first == format; });
589                 if (iter == formats.end()) {
590                         cerr << "Unexpected input format: " << format << endl;
591                         return;
592                 }
593
594                 input_format = (*iter).second;
595         }
596
597         load_file(QString::fromStdString(file_name), input_format);
598 }
599
600
601 void MainWindow::save_ui_settings()
602 {
603         QSettings settings;
604
605         map<string, string> dev_info;
606         list<string> key_list;
607
608         settings.beginGroup("MainWindow");
609         settings.setValue("state", saveState());
610         settings.setValue("geometry", saveGeometry());
611         settings.endGroup();
612
613         if (session_.device()) {
614                 settings.beginGroup("Device");
615                 key_list.push_back("vendor");
616                 key_list.push_back("model");
617                 key_list.push_back("version");
618                 key_list.push_back("serial_num");
619                 key_list.push_back("connection_id");
620
621                 dev_info = device_manager_.get_device_info(
622                         session_.device());
623
624                 for (string key : key_list) {
625
626                         if (dev_info.count(key))
627                                 settings.setValue(QString::fromUtf8(key.c_str()),
628                                                 QString::fromUtf8(dev_info.at(key).c_str()));
629                         else
630                                 settings.remove(QString::fromUtf8(key.c_str()));
631                 }
632
633                 settings.endGroup();
634         }
635 }
636
637 void MainWindow::restore_ui_settings()
638 {
639         QSettings settings;
640
641         settings.beginGroup("MainWindow");
642
643         if (settings.contains("geometry")) {
644                 restoreGeometry(settings.value("geometry").toByteArray());
645                 restoreState(settings.value("state").toByteArray());
646         } else
647                 resize(1000, 720);
648
649         settings.endGroup();
650 }
651
652 void MainWindow::session_error(
653         const QString text, const QString info_text)
654 {
655         QMetaObject::invokeMethod(this, "show_session_error",
656                 Qt::QueuedConnection, Q_ARG(QString, text),
657                 Q_ARG(QString, info_text));
658 }
659
660 void MainWindow::update_device_list()
661 {
662         main_bar_->update_device_list();
663 }
664
665 void MainWindow::load_file(QString file_name,
666         std::shared_ptr<sigrok::InputFormat> format,
667         const std::map<std::string, Glib::VariantBase> &options)
668 {
669         const QString errorMessage(
670                 QString("Failed to load file %1").arg(file_name));
671
672         try {
673                 if (format)
674                         session_.set_device(shared_ptr<devices::Device>(
675                                 new devices::InputFile(
676                                         device_manager_.context(),
677                                         file_name.toStdString(),
678                                         format, options)));
679                 else
680                         session_.set_device(shared_ptr<devices::Device>(
681                                 new devices::SessionFile(
682                                         device_manager_.context(),
683                                         file_name.toStdString())));
684         } catch(Error e) {
685                 show_session_error(tr("Failed to load ") + file_name, e.what());
686                 session_.set_default_device();
687                 update_device_list();
688                 return;
689         }
690
691         update_device_list();
692
693         session_.start_capture([&, errorMessage](QString infoMessage) {
694                 session_error(errorMessage, infoMessage); });
695 }
696
697 void MainWindow::closeEvent(QCloseEvent *event)
698 {
699         save_ui_settings();
700         event->accept();
701 }
702
703 void MainWindow::keyReleaseEvent(QKeyEvent *event)
704 {
705         if (event->key() == Qt::Key_Alt) {
706                 menuBar()->setHidden(!menuBar()->isHidden());
707                 menuBar()->setFocus();
708         }
709         QMainWindow::keyReleaseEvent(event);
710 }
711
712 void MainWindow::show_session_error(
713         const QString text, const QString info_text)
714 {
715         QMessageBox msg(this);
716         msg.setText(text);
717         msg.setInformativeText(info_text);
718         msg.setStandardButtons(QMessageBox::Ok);
719         msg.setIcon(QMessageBox::Warning);
720         msg.exec();
721 }
722
723 void MainWindow::on_actionOpen_triggered()
724 {
725         QSettings settings;
726         const QString dir = settings.value(SettingOpenDirectory).toString();
727
728         // Show the dialog
729         const QString file_name = QFileDialog::getOpenFileName(
730                 this, tr("Open File"), dir, tr(
731                         "Sigrok Sessions (*.sr);;"
732                         "All Files (*.*)"));
733
734         if (!file_name.isEmpty()) {
735                 load_file(file_name);
736
737                 const QString abs_path = QFileInfo(file_name).absolutePath();
738                 settings.setValue(SettingOpenDirectory, abs_path);
739         }
740 }
741
742 void MainWindow::on_actionSaveAs_triggered()
743 {
744         export_file(device_manager_.context()->output_formats()["srzip"]);
745 }
746
747 void MainWindow::on_actionSaveSelectionAs_triggered()
748 {
749         export_file(device_manager_.context()->output_formats()["srzip"], true);
750 }
751
752 void MainWindow::on_actionConnect_triggered()
753 {
754         // Stop any currently running capture session
755         session_.stop_capture();
756
757         dialogs::Connect dlg(this, device_manager_);
758
759         // If the user selected a device, select it in the device list. Select the
760         // current device otherwise.
761         if (dlg.exec())
762                 select_device(dlg.get_selected_device());
763
764         update_device_list();
765 }
766
767 void MainWindow::on_actionQuit_triggered()
768 {
769         close();
770 }
771
772 void MainWindow::on_actionViewZoomIn_triggered()
773 {
774         view_->zoom(1);
775 }
776
777 void MainWindow::on_actionViewZoomOut_triggered()
778 {
779         view_->zoom(-1);
780 }
781
782 void MainWindow::on_actionViewZoomFit_triggered()
783 {
784         view_->zoom_fit(action_view_zoom_fit_->isChecked());
785 }
786
787 void MainWindow::on_actionViewZoomOneToOne_triggered()
788 {
789         view_->zoom_one_to_one();
790 }
791
792 void MainWindow::on_actionViewStickyScrolling_triggered()
793 {
794         view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
795 }
796
797 void MainWindow::on_actionViewShowCursors_triggered()
798 {
799         assert(view_);
800
801         const bool show = !view_->cursors_shown();
802         if (show)
803                 view_->centre_cursors();
804
805         view_->show_cursors(show);
806 }
807
808 void MainWindow::on_actionAbout_triggered()
809 {
810         dialogs::About dlg(device_manager_.context(), this);
811         dlg.exec();
812 }
813
814 void MainWindow::sticky_scrolling_changed(bool state)
815 {
816         action_view_sticky_scrolling_->setChecked(state);
817 }
818
819 void MainWindow::always_zoom_to_fit_changed(bool state)
820 {
821         action_view_zoom_fit_->setChecked(state);
822 }
823
824 void MainWindow::add_decoder(srd_decoder *decoder)
825 {
826 #ifdef ENABLE_DECODE
827         assert(decoder);
828         session_.add_decoder(decoder);
829 #else
830         (void)decoder;
831 #endif
832 }
833
834 void MainWindow::capture_state_changed(int state)
835 {
836         main_bar_->set_capture_state((pv::Session::capture_state)state);
837 }
838
839 void MainWindow::device_selected()
840 {
841         // Set the title to include the device/file name
842         const shared_ptr<devices::Device> device = session_.device();
843         if (!device)
844                 return;
845
846         const string display_name = device->display_name(device_manager_);
847         setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));
848 }
849
850 } // namespace pv