]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
Various minor whitespace and consistency fixes.
[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         case Session::AwaitingTrigger:
204         case Session::Running:
205                 session_.stop_capture();
206                 break;
207         }
208 }
209
210 void MainWindow::select_device(shared_ptr<devices::Device> device)
211 {
212         try {
213                 if (device)
214                         session_.set_device(device);
215                 else
216                         session_.set_default_device();
217         } catch (const QString &e) {
218                 QMessageBox msg(this);
219                 msg.setText(e);
220                 msg.setInformativeText(tr("Failed to Select Device"));
221                 msg.setStandardButtons(QMessageBox::Ok);
222                 msg.setIcon(QMessageBox::Warning);
223                 msg.exec();
224         }
225 }
226
227 void MainWindow::export_file(shared_ptr<OutputFormat> format,
228         bool selection_only)
229 {
230         using pv::dialogs::StoreProgress;
231
232         // Stop any currently running capture session
233         session_.stop_capture();
234
235         QSettings settings;
236         const QString dir = settings.value(SettingSaveDirectory).toString();
237
238         std::pair<uint64_t, uint64_t> sample_range;
239
240         // Selection only? Verify that the cursors are active and fetch their values
241         if (selection_only) {
242                 if (!view_->cursors()->enabled()) {
243                         show_session_error(tr("Missing Cursors"), tr("You need to set the " \
244                                         "cursors before you can save the data enclosed by them " \
245                                         "to a session file (e.g. using ALT-V - Show Cursors)."));
246                         return;
247                 }
248
249                 const double samplerate = session_.get_samplerate();
250
251                 const pv::util::Timestamp& start_time = view_->cursors()->first()->time();
252                 const pv::util::Timestamp& end_time = view_->cursors()->second()->time();
253
254                 const uint64_t start_sample = start_time.convert_to<double>() * samplerate;
255                 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
256
257                 sample_range = std::make_pair(start_sample, end_sample);
258         } else {
259                 sample_range = std::make_pair(0, 0);
260         }
261
262         // Construct the filter
263         const vector<string> exts = format->extensions();
264         QString filter = tr("%1 files ").arg(
265                 QString::fromStdString(format->description()));
266
267         if (exts.empty())
268                 filter += "(*.*)";
269         else
270                 filter += QString("(*.%1);;%2 (*.*)").arg(
271                         QString::fromStdString(join(exts, ", *."))).arg(
272                         tr("All Files"));
273
274         // Show the file dialog
275         const QString file_name = QFileDialog::getSaveFileName(
276                 this, tr("Save File"), dir, filter);
277
278         if (file_name.isEmpty())
279                 return;
280
281         const QString abs_path = QFileInfo(file_name).absolutePath();
282         settings.setValue(SettingSaveDirectory, abs_path);
283
284         // Show the options dialog
285         map<string, Glib::VariantBase> options;
286         if (!format->options().empty()) {
287                 dialogs::InputOutputOptions dlg(
288                         tr("Export %1").arg(QString::fromStdString(
289                                 format->description())),
290                         format->options(), this);
291                 if (!dlg.exec())
292                         return;
293                 options = dlg.options();
294         }
295
296         StoreProgress *dlg = new StoreProgress(file_name, format, options,
297                 sample_range, session_, this);
298         dlg->run();
299 }
300
301 void MainWindow::import_file(shared_ptr<InputFormat> format)
302 {
303         assert(format);
304
305         QSettings settings;
306         const QString dir = settings.value(SettingOpenDirectory).toString();
307
308         // Construct the filter
309         const vector<string> exts = format->extensions();
310         const QString filter = exts.empty() ? "" :
311                 tr("%1 files (*.%2)").arg(
312                         QString::fromStdString(format->description())).arg(
313                         QString::fromStdString(join(exts, ", *.")));
314
315         // Show the file dialog
316         const QString file_name = QFileDialog::getOpenFileName(
317                 this, tr("Import File"), dir, tr(
318                         "%1 files (*.*);;All Files (*.*)").arg(
319                         QString::fromStdString(format->description())));
320
321         if (file_name.isEmpty())
322                 return;
323
324         // Show the options dialog
325         map<string, Glib::VariantBase> options;
326         if (!format->options().empty()) {
327                 dialogs::InputOutputOptions dlg(
328                         tr("Import %1").arg(QString::fromStdString(
329                                 format->description())),
330                         format->options(), this);
331                 if (!dlg.exec())
332                         return;
333                 options = dlg.options();
334         }
335
336         load_file(file_name, format, options);
337
338         const QString abs_path = QFileInfo(file_name).absolutePath();
339         settings.setValue(SettingOpenDirectory, abs_path);
340 }
341
342 void MainWindow::setup_ui()
343 {
344         setObjectName(QString::fromUtf8("MainWindow"));
345
346         // Set the window icon
347         QIcon icon;
348         icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
349         setWindowIcon(icon);
350
351         // Setup the central widget
352         central_widget_ = new QWidget(this);
353         vertical_layout_ = new QVBoxLayout(central_widget_);
354         vertical_layout_->setSpacing(6);
355         vertical_layout_->setContentsMargins(0, 0, 0, 0);
356         setCentralWidget(central_widget_);
357
358         view_ = new pv::view::View(session_, this);
359
360         vertical_layout_->addWidget(view_);
361
362         // Setup the menu bar
363         pv::widgets::HidingMenuBar *const menu_bar =
364                 new pv::widgets::HidingMenuBar(this);
365
366         // File Menu
367         QMenu *const menu_file = new QMenu;
368         menu_file->setTitle(tr("&File"));
369
370         action_open_->setText(tr("&Open..."));
371         action_open_->setIcon(QIcon::fromTheme("document-open",
372                 QIcon(":/icons/document-open.png")));
373         action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
374         action_open_->setObjectName(QString::fromUtf8("actionOpen"));
375         menu_file->addAction(action_open_);
376
377         action_save_as_->setText(tr("&Save As..."));
378         action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
379                 QIcon(":/icons/document-save-as.png")));
380         action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
381         action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
382         menu_file->addAction(action_save_as_);
383
384         action_save_selection_as_->setText(tr("Save Selected &Range As..."));
385         action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
386                 QIcon(":/icons/document-save-as.png")));
387         action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
388         action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
389         menu_file->addAction(action_save_selection_as_);
390
391         menu_file->addSeparator();
392
393         widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
394                 device_manager_.context());
395         menu_file_export->setTitle(tr("&Export"));
396         connect(menu_file_export,
397                 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
398                 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
399         menu_file->addAction(menu_file_export->menuAction());
400
401         widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
402                 device_manager_.context());
403         menu_file_import->setTitle(tr("&Import"));
404         connect(menu_file_import,
405                 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
406                 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
407         menu_file->addAction(menu_file_import->menuAction());
408
409         menu_file->addSeparator();
410
411         action_connect_->setText(tr("&Connect to Device..."));
412         action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
413         menu_file->addAction(action_connect_);
414
415         menu_file->addSeparator();
416
417         action_quit_->setText(tr("&Quit"));
418         action_quit_->setIcon(QIcon::fromTheme("application-exit",
419                 QIcon(":/icons/application-exit.png")));
420         action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
421         action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
422         menu_file->addAction(action_quit_);
423
424         // View Menu
425         QMenu *menu_view = new QMenu;
426         menu_view->setTitle(tr("&View"));
427
428         action_view_zoom_in_->setText(tr("Zoom &In"));
429         action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
430                 QIcon(":/icons/zoom-in.png")));
431         // simply using Qt::Key_Plus shows no + in the menu
432         action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
433         action_view_zoom_in_->setObjectName(
434                 QString::fromUtf8("actionViewZoomIn"));
435         menu_view->addAction(action_view_zoom_in_);
436
437         action_view_zoom_out_->setText(tr("Zoom &Out"));
438         action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
439                 QIcon(":/icons/zoom-out.png")));
440         action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
441         action_view_zoom_out_->setObjectName(
442                 QString::fromUtf8("actionViewZoomOut"));
443         menu_view->addAction(action_view_zoom_out_);
444
445         action_view_zoom_fit_->setCheckable(true);
446         action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
447         action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
448                 QIcon(":/icons/zoom-fit.png")));
449         action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
450         action_view_zoom_fit_->setObjectName(
451                 QString::fromUtf8("actionViewZoomFit"));
452         menu_view->addAction(action_view_zoom_fit_);
453
454         action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
455         action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
456                 QIcon(":/icons/zoom-original.png")));
457         action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
458         action_view_zoom_one_to_one_->setObjectName(
459                 QString::fromUtf8("actionViewZoomOneToOne"));
460         menu_view->addAction(action_view_zoom_one_to_one_);
461
462         menu_file->addSeparator();
463
464         action_view_sticky_scrolling_->setCheckable(true);
465         action_view_sticky_scrolling_->setChecked(true);
466         action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
467         action_view_sticky_scrolling_->setObjectName(
468                 QString::fromUtf8("actionViewStickyScrolling"));
469         action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
470         menu_view->addAction(action_view_sticky_scrolling_);
471
472         view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
473
474         menu_view->addSeparator();
475
476         action_view_show_cursors_->setCheckable(true);
477         action_view_show_cursors_->setChecked(view_->cursors_shown());
478         action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
479                 QIcon(":/icons/show-cursors.svg")));
480         action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
481         action_view_show_cursors_->setObjectName(
482                 QString::fromUtf8("actionViewShowCursors"));
483         action_view_show_cursors_->setText(tr("Show &Cursors"));
484         menu_view->addAction(action_view_show_cursors_);
485
486         // Decoders Menu
487 #ifdef ENABLE_DECODE
488         QMenu *const menu_decoders = new QMenu;
489         menu_decoders->setTitle(tr("&Decoders"));
490
491         menu_decoders_add_->setTitle(tr("&Add"));
492         connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
493                 this, SLOT(add_decoder(srd_decoder*)));
494
495         menu_decoders->addMenu(menu_decoders_add_);
496 #endif
497
498         // Help Menu
499         QMenu *const menu_help = new QMenu;
500         menu_help->setTitle(tr("&Help"));
501
502         action_about_->setObjectName(QString::fromUtf8("actionAbout"));
503         action_about_->setText(tr("&About..."));
504         menu_help->addAction(action_about_);
505
506         menu_bar->addAction(menu_file->menuAction());
507         menu_bar->addAction(menu_view->menuAction());
508 #ifdef ENABLE_DECODE
509         menu_bar->addAction(menu_decoders->menuAction());
510 #endif
511         menu_bar->addAction(menu_help->menuAction());
512
513         setMenuBar(menu_bar);
514         QMetaObject::connectSlotsByName(this);
515
516         // Also add all actions to the main window for always-enabled hotkeys
517         for (QAction* action : menu_bar->actions())
518                 this->addAction(action);
519
520         // Setup the toolbar
521         main_bar_ = new toolbars::MainBar(session_, *this);
522
523         // Populate the device list and select the initially selected device
524         update_device_list();
525
526         addToolBar(main_bar_);
527
528         // Set the title
529         setWindowTitle(tr("PulseView"));
530
531         // Setup session_ events
532         connect(&session_, SIGNAL(capture_state_changed(int)), this,
533                 SLOT(capture_state_changed(int)));
534         connect(&session_, SIGNAL(device_selected()), this,
535                 SLOT(device_selected()));
536         connect(&session_, SIGNAL(trigger_event(util::Timestamp)), view_,
537                 SLOT(trigger_event(util::Timestamp)));
538
539         // Setup view_ events
540         connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this,
541                 SLOT(sticky_scrolling_changed(bool)));
542         connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this,
543                 SLOT(always_zoom_to_fit_changed(bool)));
544
545 }
546
547 void MainWindow::select_init_device()
548 {
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 {
582         shared_ptr<InputFormat> input_format;
583
584         if (!format.empty()) {
585                 const map<string, shared_ptr<InputFormat> > formats =
586                         device_manager_.context()->input_formats();
587                 const auto iter = find_if(formats.begin(), formats.end(),
588                         [&](const pair<string, shared_ptr<InputFormat> > f) {
589                                 return f.first == format; });
590                 if (iter == formats.end()) {
591                         cerr << "Unexpected input format: " << format << endl;
592                         return;
593                 }
594
595                 input_format = (*iter).second;
596         }
597
598         load_file(QString::fromStdString(file_name), input_format);
599 }
600
601
602 void MainWindow::save_ui_settings()
603 {
604         QSettings settings;
605
606         map<string, string> dev_info;
607         list<string> key_list;
608
609         settings.beginGroup("MainWindow");
610         settings.setValue("state", saveState());
611         settings.setValue("geometry", saveGeometry());
612         settings.endGroup();
613
614         if (session_.device()) {
615                 settings.beginGroup("Device");
616                 key_list.push_back("vendor");
617                 key_list.push_back("model");
618                 key_list.push_back("version");
619                 key_list.push_back("serial_num");
620                 key_list.push_back("connection_id");
621
622                 dev_info = device_manager_.get_device_info(
623                         session_.device());
624
625                 for (string key : key_list) {
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