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