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