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