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