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