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