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