]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
DecodeTrace: Always show row if its class selector is showing
[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, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifdef ENABLE_DECODE
21#include <libsigrokdecode/libsigrokdecode.h>
22#endif
23
24#include <algorithm>
25#include <cassert>
26#include <cstdarg>
27#include <cstdint>
28#include <iterator>
29
30#include <QAction>
31#include <QApplication>
32#include <QCloseEvent>
33#include <QDebug>
34#include <QDockWidget>
35#include <QHBoxLayout>
36#include <QMessageBox>
37#include <QSettings>
38#include <QShortcut>
39#include <QWidget>
40
41#include "mainwindow.hpp"
42
43#include "application.hpp"
44#include "devicemanager.hpp"
45#include "devices/hardwaredevice.hpp"
46#include "dialogs/settings.hpp"
47#include "globalsettings.hpp"
48#include "toolbars/mainbar.hpp"
49#include "util.hpp"
50#include "views/trace/view.hpp"
51#include "views/trace/standardbar.hpp"
52
53#ifdef ENABLE_DECODE
54#include "subwindows/decoder_selector/subwindow.hpp"
55#include "views/decoder_output/view.hpp"
56#endif
57
58#include <libsigrokcxx/libsigrokcxx.hpp>
59
60using std::dynamic_pointer_cast;
61using std::make_shared;
62using std::shared_ptr;
63using std::string;
64
65namespace pv {
66
67using toolbars::MainBar;
68
69const QString MainWindow::WindowTitle = tr("PulseView");
70
71MainWindow::MainWindow(DeviceManager &device_manager, QWidget *parent) :
72 QMainWindow(parent),
73 device_manager_(device_manager),
74 session_selector_(this),
75 icon_red_(":/icons/status-red.svg"),
76 icon_green_(":/icons/status-green.svg"),
77 icon_grey_(":/icons/status-grey.svg")
78{
79 setup_ui();
80 restore_ui_settings();
81}
82
83MainWindow::~MainWindow()
84{
85 // Make sure we no longer hold any shared pointers to widgets after the
86 // destructor finishes (goes for sessions and sub windows alike)
87
88 while (!sessions_.empty())
89 remove_session(sessions_.front());
90
91 sub_windows_.clear();
92}
93
94void MainWindow::show_session_error(const QString text, const QString info_text)
95{
96 // TODO Emulate noquote()
97 qDebug() << "Notifying user of session error:" << info_text;
98
99 QMessageBox msg;
100 msg.setText(text + "\n\n" + info_text);
101 msg.setStandardButtons(QMessageBox::Ok);
102 msg.setIcon(QMessageBox::Warning);
103 msg.exec();
104}
105
106shared_ptr<views::ViewBase> MainWindow::get_active_view() const
107{
108 // If there's only one view, use it...
109 if (view_docks_.size() == 1)
110 return view_docks_.begin()->second;
111
112 // ...otherwise find the dock widget the widget with focus is contained in
113 QObject *w = QApplication::focusWidget();
114 QDockWidget *dock = nullptr;
115
116 while (w) {
117 dock = qobject_cast<QDockWidget*>(w);
118 if (dock)
119 break;
120 w = w->parent();
121 }
122
123 // Get the view contained in the dock widget
124 for (auto& entry : view_docks_)
125 if (entry.first == dock)
126 return entry.second;
127
128 return nullptr;
129}
130
131shared_ptr<views::ViewBase> MainWindow::add_view(views::ViewType type,
132 Session &session)
133{
134 GlobalSettings settings;
135 shared_ptr<views::ViewBase> v;
136
137 QMainWindow *main_window = nullptr;
138 for (auto& entry : session_windows_)
139 if (entry.first.get() == &session)
140 main_window = entry.second;
141
142 assert(main_window);
143
144 shared_ptr<MainBar> main_bar = session.main_bar();
145
146 // Only use the view type in the name if it's not the main view
147 QString title;
148 if (main_bar)
149 title = QString("%1 (%2)").arg(session.name(), views::ViewTypeNames[type]);
150 else
151 title = session.name();
152
153 QDockWidget* dock = new QDockWidget(title, main_window);
154 dock->setObjectName(title);
155 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
156
157 // Insert a QMainWindow into the dock widget to allow for a tool bar
158 QMainWindow *dock_main = new QMainWindow(dock);
159 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
160
161 if (type == views::ViewTypeTrace)
162 // This view will be the main view if there's no main bar yet
163 v = make_shared<views::trace::View>(session, (main_bar ? false : true), dock_main);
164#ifdef ENABLE_DECODE
165 if (type == views::ViewTypeDecoderOutput)
166 v = make_shared<views::decoder_output::View>(session, false, dock_main);
167#endif
168
169 if (!v)
170 return nullptr;
171
172 view_docks_[dock] = v;
173 session.register_view(v);
174
175 dock_main->setCentralWidget(v.get());
176 dock->setWidget(dock_main);
177
178 dock->setContextMenuPolicy(Qt::PreventContextMenu);
179 dock->setFeatures(QDockWidget::DockWidgetMovable |
180 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
181
182 QAbstractButton *close_btn =
183 dock->findChildren<QAbstractButton*>("qt_dockwidget_closebutton") // clazy:exclude=detaching-temporary
184 .front();
185
186 connect(close_btn, SIGNAL(clicked(bool)),
187 this, SLOT(on_view_close_clicked()));
188
189 connect(&session, SIGNAL(trigger_event(int, util::Timestamp)),
190 qobject_cast<views::ViewBase*>(v.get()),
191 SLOT(trigger_event(int, util::Timestamp)));
192
193 if (type == views::ViewTypeTrace) {
194 views::trace::View *tv =
195 qobject_cast<views::trace::View*>(v.get());
196
197 if (!main_bar) {
198 /* Initial view, create the main bar */
199 main_bar = make_shared<MainBar>(session, this, tv);
200 dock_main->addToolBar(main_bar.get());
201 session.set_main_bar(main_bar);
202
203 connect(main_bar.get(), SIGNAL(new_view(Session*, int)),
204 this, SLOT(on_new_view(Session*, int)));
205 connect(main_bar.get(), SIGNAL(show_decoder_selector(Session*)),
206 this, SLOT(on_show_decoder_selector(Session*)));
207
208 main_bar->action_view_show_cursors()->setChecked(tv->cursors_shown());
209
210 /* For the main view we need to prevent the dock widget from
211 * closing itself when its close button is clicked. This is
212 * so we can confirm with the user first. Regular views don't
213 * need this */
214 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
215 } else {
216 /* Additional view, create a standard bar */
217 pv::views::trace::StandardBar *standard_bar =
218 new pv::views::trace::StandardBar(session, this, tv);
219 dock_main->addToolBar(standard_bar);
220
221 standard_bar->action_view_show_cursors()->setChecked(tv->cursors_shown());
222 }
223 }
224
225 v->setFocus();
226
227 return v;
228}
229
230void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
231{
232 for (shared_ptr<Session> session : sessions_) {
233 if (!session->has_view(view))
234 continue;
235
236 // Find the dock the view is contained in and remove it
237 for (auto& entry : view_docks_)
238 if (entry.second == view) {
239 // Remove the view from the session
240 session->deregister_view(view);
241
242 // Remove the view from its parent; otherwise, Qt will
243 // call deleteLater() on it, which causes a double free
244 // since the shared_ptr in view_docks_ doesn't know
245 // that Qt keeps a pointer to the view around
246 view->setParent(nullptr);
247
248 // Delete the view's dock widget and all widgets inside it
249 entry.first->deleteLater();
250
251 // Remove the dock widget from the list and stop iterating
252 view_docks_.erase(entry.first);
253 break;
254 }
255 }
256}
257
258shared_ptr<subwindows::SubWindowBase> MainWindow::add_subwindow(
259 subwindows::SubWindowType type, Session &session)
260{
261 GlobalSettings settings;
262 shared_ptr<subwindows::SubWindowBase> w;
263
264 QMainWindow *main_window = nullptr;
265 for (auto& entry : session_windows_)
266 if (entry.first.get() == &session)
267 main_window = entry.second;
268
269 assert(main_window);
270
271 QString title = "";
272
273 switch (type) {
274#ifdef ENABLE_DECODE
275 case subwindows::SubWindowTypeDecoderSelector:
276 title = tr("Decoder Selector");
277 break;
278#endif
279 default:
280 break;
281 }
282
283 QDockWidget* dock = new QDockWidget(title, main_window);
284 dock->setObjectName(title);
285 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
286
287 // Insert a QMainWindow into the dock widget to allow for a tool bar
288 QMainWindow *dock_main = new QMainWindow(dock);
289 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
290
291#ifdef ENABLE_DECODE
292 if (type == subwindows::SubWindowTypeDecoderSelector)
293 w = make_shared<subwindows::decoder_selector::SubWindow>(session, dock_main);
294#endif
295
296 if (!w)
297 return nullptr;
298
299 sub_windows_[dock] = w;
300 dock_main->setCentralWidget(w.get());
301 dock->setWidget(dock_main);
302
303 dock->setContextMenuPolicy(Qt::PreventContextMenu);
304 dock->setFeatures(QDockWidget::DockWidgetMovable |
305 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
306
307 QAbstractButton *close_btn =
308 dock->findChildren<QAbstractButton*> // clazy:exclude=detaching-temporary
309 ("qt_dockwidget_closebutton").front();
310
311 // Allow all subwindows to be closed via ESC.
312 close_btn->setShortcut(QKeySequence(Qt::Key_Escape));
313
314 connect(close_btn, SIGNAL(clicked(bool)),
315 this, SLOT(on_sub_window_close_clicked()));
316
317 if (w->has_toolbar())
318 dock_main->addToolBar(w->create_toolbar(dock_main));
319
320 if (w->minimum_width() > 0)
321 dock->setMinimumSize(w->minimum_width(), 0);
322
323 return w;
324}
325
326shared_ptr<Session> MainWindow::add_session()
327{
328 static int last_session_id = 1;
329 QString name = tr("Session %1").arg(last_session_id++);
330
331 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
332
333 connect(session.get(), SIGNAL(add_view(views::ViewType, Session*)),
334 this, SLOT(on_add_view(views::ViewType, Session*)));
335 connect(session.get(), SIGNAL(name_changed()),
336 this, SLOT(on_session_name_changed()));
337 connect(session.get(), SIGNAL(device_changed()),
338 this, SLOT(on_session_device_changed()));
339 connect(session.get(), SIGNAL(capture_state_changed(int)),
340 this, SLOT(on_session_capture_state_changed(int)));
341
342 sessions_.push_back(session);
343
344 QMainWindow *window = new QMainWindow();
345 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
346 session_windows_[session] = window;
347
348 int index = session_selector_.addTab(window, name);
349 session_selector_.setCurrentIndex(index);
350 last_focused_session_ = session;
351
352 window->setDockNestingEnabled(true);
353
354 add_view(views::ViewTypeTrace, *session);
355
356 return session;
357}
358
359void MainWindow::remove_session(shared_ptr<Session> session)
360{
361 // Determine the height of the button before it collapses
362 int h = new_session_button_->height();
363
364 // Stop capture while the session still exists so that the UI can be
365 // updated in case we're currently running. If so, this will schedule a
366 // call to our on_capture_state_changed() slot for the next run of the
367 // event loop. We need to have this executed immediately or else it will
368 // be dismissed since the session object will be deleted by the time we
369 // leave this method and the event loop gets a chance to run again.
370 session->stop_capture();
371 QApplication::processEvents();
372
373 for (const shared_ptr<views::ViewBase>& view : session->views())
374 remove_view(view);
375
376 QMainWindow *window = session_windows_.at(session);
377 session_selector_.removeTab(session_selector_.indexOf(window));
378
379 session_windows_.erase(session);
380
381 if (last_focused_session_ == session)
382 last_focused_session_.reset();
383
384 // Remove the session from our list of sessions (which also destroys it)
385 sessions_.remove_if([&](shared_ptr<Session> s) {
386 return s == session; });
387
388 if (sessions_.empty()) {
389 // When there are no more tabs, the height of the QTabWidget
390 // drops to zero. We must prevent this to keep the static
391 // widgets visible
392 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>()) // clazy:exclude=range-loop
393 w->setMinimumHeight(h);
394
395 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
396 static_tab_widget_->setMinimumHeight(h + 2 * margin);
397 session_selector_.setMinimumHeight(h + 2 * margin);
398
399 // Update the window title if there is no view left to
400 // generate focus change events
401 setWindowTitle(WindowTitle);
402 }
403}
404
405void MainWindow::add_session_with_file(string open_file_name,
406 string open_file_format, string open_setup_file_name)
407{
408 shared_ptr<Session> session = add_session();
409 session->load_init_file(open_file_name, open_file_format, open_setup_file_name);
410}
411
412void MainWindow::add_default_session()
413{
414 // Only add the default session if there would be no session otherwise
415 if (sessions_.size() > 0)
416 return;
417
418 shared_ptr<Session> session = add_session();
419
420 // Check the list of available devices. Prefer the one that was
421 // found with user supplied scan specs (if applicable). Then try
422 // one of the auto detected devices that are not the demo device.
423 // Pick demo in the absence of "genuine" hardware devices.
424 shared_ptr<devices::HardwareDevice> user_device, other_device, demo_device;
425 for (const shared_ptr<devices::HardwareDevice>& dev : device_manager_.devices()) {
426 if (dev == device_manager_.user_spec_device()) {
427 user_device = dev;
428 } else if (dev->hardware_device()->driver()->name() == "demo") {
429 demo_device = dev;
430 } else {
431 other_device = dev;
432 }
433 }
434 if (user_device)
435 session->select_device(user_device);
436 else if (other_device)
437 session->select_device(other_device);
438 else
439 session->select_device(demo_device);
440}
441
442void MainWindow::save_sessions()
443{
444 QSettings settings;
445 int id = 0;
446
447 for (shared_ptr<Session>& session : sessions_) {
448 // Ignore sessions using the demo device or no device at all
449 if (session->device()) {
450 shared_ptr<devices::HardwareDevice> device =
451 dynamic_pointer_cast< devices::HardwareDevice >
452 (session->device());
453
454 if (device &&
455 device->hardware_device()->driver()->name() == "demo")
456 continue;
457
458 settings.beginGroup("Session" + QString::number(id++));
459 settings.remove(""); // Remove all keys in this group
460 session->save_settings(settings);
461 settings.endGroup();
462 }
463 }
464
465 settings.setValue("sessions", id);
466}
467
468void MainWindow::restore_sessions()
469{
470 QSettings settings;
471 int i, session_count;
472
473 session_count = settings.value("sessions", 0).toInt();
474
475 for (i = 0; i < session_count; i++) {
476 settings.beginGroup("Session" + QString::number(i));
477 shared_ptr<Session> session = add_session();
478 session->restore_settings(settings);
479 settings.endGroup();
480 }
481}
482
483void MainWindow::setup_ui()
484{
485 setObjectName(QString::fromUtf8("MainWindow"));
486
487 setCentralWidget(&session_selector_);
488
489 // Set the window icon
490 QIcon icon;
491 icon.addFile(QString(":/icons/pulseview.png"));
492 setWindowIcon(icon);
493
494 // Set up keyboard shortcuts that affect all views at once
495 view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut()));
496 view_sticky_scrolling_shortcut_->setAutoRepeat(false);
497
498 view_show_sampling_points_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Period), this, SLOT(on_view_show_sampling_points_shortcut()));
499 view_show_sampling_points_shortcut_->setAutoRepeat(false);
500
501 view_show_analog_minor_grid_shortcut_ = new QShortcut(QKeySequence(Qt::Key_G), this, SLOT(on_view_show_analog_minor_grid_shortcut()));
502 view_show_analog_minor_grid_shortcut_->setAutoRepeat(false);
503
504 view_colored_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_colored_bg_shortcut()));
505 view_colored_bg_shortcut_->setAutoRepeat(false);
506
507 // Set up the tab area
508 new_session_button_ = new QToolButton();
509 new_session_button_->setIcon(QIcon::fromTheme("document-new",
510 QIcon(":/icons/document-new.png")));
511 new_session_button_->setToolTip(tr("Create New Session"));
512 new_session_button_->setAutoRaise(true);
513
514 run_stop_button_ = new QToolButton();
515 run_stop_button_->setAutoRaise(true);
516 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
517 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
518
519 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
520 run_stop_shortcut_->setAutoRepeat(false);
521
522 settings_button_ = new QToolButton();
523 settings_button_->setIcon(QIcon::fromTheme("preferences-system",
524 QIcon(":/icons/preferences-system.png")));
525 settings_button_->setToolTip(tr("Settings"));
526 settings_button_->setAutoRaise(true);
527
528 QFrame *separator1 = new QFrame();
529 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
530 QFrame *separator2 = new QFrame();
531 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
532
533 QHBoxLayout* layout = new QHBoxLayout();
534 layout->setContentsMargins(2, 2, 2, 2);
535 layout->addWidget(new_session_button_);
536 layout->addWidget(separator1);
537 layout->addWidget(run_stop_button_);
538 layout->addWidget(separator2);
539 layout->addWidget(settings_button_);
540
541 static_tab_widget_ = new QWidget();
542 static_tab_widget_->setLayout(layout);
543
544 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
545 session_selector_.setTabsClosable(true);
546
547 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
548 close_application_shortcut_->setAutoRepeat(false);
549
550 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab()));
551
552 connect(new_session_button_, SIGNAL(clicked(bool)),
553 this, SLOT(on_new_session_clicked()));
554 connect(run_stop_button_, SIGNAL(clicked(bool)),
555 this, SLOT(on_run_stop_clicked()));
556 connect(settings_button_, SIGNAL(clicked(bool)),
557 this, SLOT(on_settings_clicked()));
558
559 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
560 this, SLOT(on_tab_close_requested(int)));
561 connect(&session_selector_, SIGNAL(currentChanged(int)),
562 this, SLOT(on_tab_changed(int)));
563
564
565 connect(static_cast<QApplication *>(QCoreApplication::instance()),
566 SIGNAL(focusChanged(QWidget*, QWidget*)),
567 this, SLOT(on_focus_changed()));
568}
569
570void MainWindow::update_acq_button(Session *session)
571{
572 int state = session->get_capture_state();
573
574 const QString run_caption =
575 session->using_file_device() ? tr("Reload") : tr("Run");
576
577 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
578 run_stop_button_->setIcon(*icons[state]);
579 run_stop_button_->setText((state == pv::Session::Stopped) ?
580 run_caption : tr("Stop"));
581}
582
583void MainWindow::save_ui_settings()
584{
585 QSettings settings;
586
587 settings.beginGroup("MainWindow");
588 settings.setValue("state", saveState());
589 settings.setValue("geometry", saveGeometry());
590 settings.endGroup();
591}
592
593void MainWindow::restore_ui_settings()
594{
595 QSettings settings;
596
597 settings.beginGroup("MainWindow");
598
599 if (settings.contains("geometry")) {
600 restoreGeometry(settings.value("geometry").toByteArray());
601 restoreState(settings.value("state").toByteArray());
602 } else
603 resize(1000, 720);
604
605 settings.endGroup();
606}
607
608shared_ptr<Session> MainWindow::get_tab_session(int index) const
609{
610 // Find the session that belongs to the tab's main window
611 for (auto& entry : session_windows_)
612 if (entry.second == session_selector_.widget(index))
613 return entry.first;
614
615 return nullptr;
616}
617
618void MainWindow::closeEvent(QCloseEvent *event)
619{
620 bool data_saved = true;
621
622 for (auto& entry : session_windows_)
623 if (!entry.first->data_saved())
624 data_saved = false;
625
626 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
627 tr("There is unsaved data. Close anyway?"),
628 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
629 event->ignore();
630 } else {
631 save_ui_settings();
632 save_sessions();
633 event->accept();
634 }
635}
636
637QMenu* MainWindow::createPopupMenu()
638{
639 return nullptr;
640}
641
642bool MainWindow::restoreState(const QByteArray &state, int version)
643{
644 (void)state;
645 (void)version;
646
647 // Do nothing. We don't want Qt to handle this, or else it
648 // will try to restore all the dock widgets and create havoc.
649
650 return false;
651}
652
653void MainWindow::on_add_view(views::ViewType type, Session *session)
654{
655 // We get a pointer and need a reference
656 for (shared_ptr<Session>& s : sessions_)
657 if (s.get() == session)
658 add_view(type, *s);
659}
660
661void MainWindow::on_focus_changed()
662{
663 shared_ptr<views::ViewBase> view = get_active_view();
664
665 if (view) {
666 for (shared_ptr<Session> session : sessions_) {
667 if (session->has_view(view)) {
668 if (session != last_focused_session_) {
669 // Activate correct tab if necessary
670 shared_ptr<Session> tab_session = get_tab_session(
671 session_selector_.currentIndex());
672 if (tab_session != session)
673 session_selector_.setCurrentWidget(
674 session_windows_.at(session));
675
676 on_focused_session_changed(session);
677 }
678
679 break;
680 }
681 }
682 }
683
684 if (sessions_.empty())
685 setWindowTitle(WindowTitle);
686}
687
688void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
689{
690 last_focused_session_ = session;
691
692 setWindowTitle(session->name() + " - " + WindowTitle);
693
694 // Update the state of the run/stop button, too
695 update_acq_button(session.get());
696}
697
698void MainWindow::on_new_session_clicked()
699{
700 add_session();
701}
702
703void MainWindow::on_run_stop_clicked()
704{
705 shared_ptr<Session> session = last_focused_session_;
706
707 if (!session)
708 return;
709
710 switch (session->get_capture_state()) {
711 case Session::Stopped:
712 session->start_capture([&](QString message) {
713 show_session_error("Capture failed", message); });
714 break;
715 case Session::AwaitingTrigger:
716 case Session::Running:
717 session->stop_capture();
718 break;
719 }
720}
721
722void MainWindow::on_settings_clicked()
723{
724 dialogs::Settings dlg(device_manager_);
725 dlg.exec();
726}
727
728void MainWindow::on_session_name_changed()
729{
730 // Update the corresponding dock widget's name(s)
731 Session *session = qobject_cast<Session*>(QObject::sender());
732 assert(session);
733
734 for (const shared_ptr<views::ViewBase>& view : session->views()) {
735 // Get the dock that contains the view
736 for (auto& entry : view_docks_)
737 if (entry.second == view) {
738 entry.first->setObjectName(session->name());
739 entry.first->setWindowTitle(session->name());
740 }
741 }
742
743 // Update the tab widget by finding the main window and the tab from that
744 for (auto& entry : session_windows_)
745 if (entry.first.get() == session) {
746 QMainWindow *window = entry.second;
747 const int index = session_selector_.indexOf(window);
748 session_selector_.setTabText(index, session->name());
749 }
750
751 // Refresh window title if the affected session has focus
752 if (session == last_focused_session_.get())
753 setWindowTitle(session->name() + " - " + WindowTitle);
754}
755
756void MainWindow::on_session_device_changed()
757{
758 Session *session = qobject_cast<Session*>(QObject::sender());
759 assert(session);
760
761 // Ignore if caller is not the currently focused session
762 // unless there is only one session
763 if ((sessions_.size() > 1) && (session != last_focused_session_.get()))
764 return;
765
766 update_acq_button(session);
767}
768
769void MainWindow::on_session_capture_state_changed(int state)
770{
771 (void)state;
772
773 Session *session = qobject_cast<Session*>(QObject::sender());
774 assert(session);
775
776 // Ignore if caller is not the currently focused session
777 // unless there is only one session
778 if ((sessions_.size() > 1) && (session != last_focused_session_.get()))
779 return;
780
781 update_acq_button(session);
782}
783
784void MainWindow::on_new_view(Session *session, int view_type)
785{
786 // We get a pointer and need a reference
787 for (shared_ptr<Session>& s : sessions_)
788 if (s.get() == session)
789 add_view((views::ViewType)view_type, *s);
790}
791
792void MainWindow::on_view_close_clicked()
793{
794 // Find the dock widget that contains the close button that was clicked
795 QObject *w = QObject::sender();
796 QDockWidget *dock = nullptr;
797
798 while (w) {
799 dock = qobject_cast<QDockWidget*>(w);
800 if (dock)
801 break;
802 w = w->parent();
803 }
804
805 // Get the view contained in the dock widget
806 shared_ptr<views::ViewBase> view;
807
808 for (auto& entry : view_docks_)
809 if (entry.first == dock)
810 view = entry.second;
811
812 // Deregister the view
813 for (shared_ptr<Session> session : sessions_) {
814 if (!session->has_view(view))
815 continue;
816
817 // Also destroy the entire session if its main view is closing...
818 if (view == session->main_view()) {
819 // ...but only if data is saved or the user confirms closing
820 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
821 tr("This session contains unsaved data. Close it anyway?"),
822 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
823 remove_session(session);
824 break;
825 } else
826 // All other views can be closed at any time as no data will be lost
827 remove_view(view);
828 }
829}
830
831void MainWindow::on_tab_changed(int index)
832{
833 shared_ptr<Session> session = get_tab_session(index);
834
835 if (session)
836 on_focused_session_changed(session);
837}
838
839void MainWindow::on_tab_close_requested(int index)
840{
841 shared_ptr<Session> session = get_tab_session(index);
842
843 if (!session)
844 return;
845
846 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
847 tr("This session contains unsaved data. Close it anyway?"),
848 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
849 remove_session(session);
850}
851
852void MainWindow::on_show_decoder_selector(Session *session)
853{
854#ifdef ENABLE_DECODE
855 // Close dock widget if it's already showing and return
856 for (auto& entry : sub_windows_) {
857 QDockWidget* dock = entry.first;
858 shared_ptr<subwindows::SubWindowBase> decoder_selector =
859 dynamic_pointer_cast<subwindows::decoder_selector::SubWindow>(entry.second);
860
861 if (decoder_selector && (&decoder_selector->session() == session)) {
862 sub_windows_.erase(dock);
863 dock->close();
864 return;
865 }
866 }
867
868 // We get a pointer and need a reference
869 for (shared_ptr<Session>& s : sessions_)
870 if (s.get() == session)
871 add_subwindow(subwindows::SubWindowTypeDecoderSelector, *s);
872#endif
873}
874
875void MainWindow::on_sub_window_close_clicked()
876{
877 // Find the dock widget that contains the close button that was clicked
878 QObject *w = QObject::sender();
879 QDockWidget *dock = nullptr;
880
881 while (w) {
882 dock = qobject_cast<QDockWidget*>(w);
883 if (dock)
884 break;
885 w = w->parent();
886 }
887
888 sub_windows_.erase(dock);
889 dock->close();
890
891 // Restore focus to the last used main view
892 if (last_focused_session_)
893 last_focused_session_->main_view()->setFocus();
894}
895
896void MainWindow::on_view_colored_bg_shortcut()
897{
898 GlobalSettings settings;
899
900 bool state = settings.value(GlobalSettings::Key_View_ColoredBG).toBool();
901 settings.setValue(GlobalSettings::Key_View_ColoredBG, !state);
902}
903
904void MainWindow::on_view_sticky_scrolling_shortcut()
905{
906 GlobalSettings settings;
907
908 bool state = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool();
909 settings.setValue(GlobalSettings::Key_View_StickyScrolling, !state);
910}
911
912void MainWindow::on_view_show_sampling_points_shortcut()
913{
914 GlobalSettings settings;
915
916 bool state = settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool();
917 settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, !state);
918}
919
920void MainWindow::on_view_show_analog_minor_grid_shortcut()
921{
922 GlobalSettings settings;
923
924 bool state = settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool();
925 settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, !state);
926}
927
928void MainWindow::on_close_current_tab()
929{
930 int tab = session_selector_.currentIndex();
931
932 on_tab_close_requested(tab);
933}
934
935} // namespace pv