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