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