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