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