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