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