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