]> sigrok.org Git - pulseview.git/blobdiff - pv/mainwindow.cpp
Allow dock windows to be closed and handle this properly
[pulseview.git] / pv / mainwindow.cpp
index 6e408583a52d0cb1e7e2c45870c950cbedc4094f..8787073d5875e1d82c64d826eb845b26e09c9b3c 100644 (file)
@@ -186,15 +186,18 @@ shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
                dock->setWidget(dock_main);
 
                dock->setFeatures(QDockWidget::DockWidgetMovable |
-                       QDockWidget::DockWidgetFloatable);
+                       QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
+
+               QAbstractButton *close_btn =
+                       dock->findChildren<QAbstractButton*>
+                               ("qt_dockwidget_closebutton").front();
+
+               connect(close_btn, SIGNAL(clicked(bool)),
+                       this, SLOT(on_view_close_clicked()));
 
                if (type == view::TraceView) {
                        connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(),
                                SLOT(trigger_event(util::Timestamp)));
-                       connect(v.get(), SIGNAL(sticky_scrolling_changed(bool)), this,
-                               SLOT(sticky_scrolling_changed(bool)));
-                       connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), this,
-                               SLOT(always_zoom_to_fit_changed(bool)));
 
                        v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
                        v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
@@ -204,8 +207,16 @@ shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
                                main_bar = make_shared<MainBar>(session, *this);
                                dock_main->addToolBar(main_bar.get());
                                session.set_main_bar(main_bar);
+
+                               connect(main_bar.get(), SIGNAL(new_session()),
+                                       this, SLOT(on_new_session()));
+                               connect(main_bar.get(), SIGNAL(new_view(Session*)),
+                                       this, SLOT(on_new_view(Session*)));
                        }
                        main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
+
+                       connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
+                               main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
                }
        }
 
@@ -219,6 +230,9 @@ shared_ptr<Session> MainWindow::add_session()
 
        shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
 
+       connect(session.get(), SIGNAL(add_view(const QString&, view::ViewType, Session*)),
+               this, SLOT(on_add_view(const QString&, view::ViewType, Session*)));
+
        sessions_.push_back(session);
 
        shared_ptr<view::View> main_view =
@@ -227,6 +241,19 @@ shared_ptr<Session> MainWindow::add_session()
        return session;
 }
 
+void MainWindow::remove_session(shared_ptr<Session> session)
+{
+       for (shared_ptr<view::View> view : session->views()) {
+               // Find the dock the view is contained in and close it
+               for (auto entry : view_docks_)
+                       if (entry.second == view)
+                               entry.first->close();
+       }
+
+       sessions_.remove_if([&](shared_ptr<Session> s) {
+               return s == session; });
+}
+
 void MainWindow::setup_ui()
 {
        setObjectName(QString::fromUtf8("MainWindow"));
@@ -253,6 +280,8 @@ void MainWindow::setup_ui()
        action_about_->setObjectName(QString::fromUtf8("actionAbout"));
        action_about_->setText(tr("&About..."));
 
+       setDockNestingEnabled(true);
+
        // Set the title
        setWindowTitle(tr("PulseView"));
 }
@@ -274,11 +303,13 @@ void MainWindow::save_ui_settings()
                                dynamic_pointer_cast< devices::HardwareDevice >
                                (session->device());
 
-                       if (device->hardware_device()->driver()->name() == "demo")
+                       if (device &&
+                               device->hardware_device()->driver()->name() == "demo")
                                continue;
                }
 
                settings.beginGroup("Session" + QString::number(id++));
+               settings.remove("");  // Remove all keys in this group
                session->save_settings(settings);
                settings.endGroup();
        }
@@ -333,6 +364,62 @@ bool MainWindow::restoreState(const QByteArray &state, int version)
        return false;
 }
 
+void MainWindow::on_add_view(const QString &title, view::ViewType type,
+       Session *session)
+{
+       // We get a pointer and need a reference
+       for (std::shared_ptr<Session> s : sessions_)
+               if (s.get() == session)
+                       add_view(title, type, *s);
+}
+
+void MainWindow::on_new_session()
+{
+       add_session();
+}
+
+void MainWindow::on_new_view(Session *session)
+{
+       // We get a pointer and need a reference
+       for (std::shared_ptr<Session> s : sessions_)
+               if (s.get() == session)
+                       add_view(session->name(), pv::view::TraceView, *s);
+}
+
+void MainWindow::on_view_close_clicked()
+{
+       // Find the dock widget that contains the close button that was clicked
+       QObject *w = QObject::sender();
+       QDockWidget *dock = 0;
+
+       while (w) {
+           dock = qobject_cast<QDockWidget*>(w);
+           if (dock)
+               break;
+           w = w->parent();
+       }
+
+       // Get the view contained in the dock widget
+       shared_ptr<view::View> view;
+
+       for (auto entry : view_docks_)
+               if (entry.first.get() == dock)
+                       view = entry.second;
+
+       // Deregister the view
+       for (shared_ptr<Session> session : sessions_) {
+               if (!session->has_view(view))
+                       continue;
+
+               // Also destroy the entire session if its main view is closing
+               if (view == session->main_view()) {
+                       remove_session(session);
+                       break;
+               } else
+                       session->deregister_view(view);
+       }
+}
+
 void MainWindow::on_actionViewStickyScrolling_triggered()
 {
        shared_ptr<pv::view::View> view = get_active_view();