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