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