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