]> sigrok.org Git - pulseview.git/commitdiff
Allow dock windows to be closed and handle this properly
authorSoeren Apel <redacted>
Fri, 2 Sep 2016 16:20:41 +0000 (18:20 +0200)
committerSoeren Apel <redacted>
Fri, 2 Sep 2016 16:20:41 +0000 (18:20 +0200)
pv/mainwindow.cpp
pv/mainwindow.hpp
pv/session.cpp
pv/session.hpp

index dab9c7866fab2b61ac85438dfe336bb05a96a9cd..8787073d5875e1d82c64d826eb845b26e09c9b3c 100644 (file)
@@ -186,7 +186,14 @@ 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(),
@@ -234,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"));
@@ -366,6 +386,40 @@ void MainWindow::on_new_view(Session *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();
index 2bd8dadbf815d8ebdc594c9a3afb92208319a3ad..0d2db8cb7662c911746a9054b5f8aa0f578d8226 100644 (file)
@@ -76,6 +76,8 @@ public:
 
        std::shared_ptr<Session> add_session();
 
+       void remove_session(std::shared_ptr<Session> session);
+
 private:
        void setup_ui();
 
@@ -96,6 +98,7 @@ private Q_SLOTS:
 
        void on_new_session();
        void on_new_view(Session *session);
+       void on_view_close_clicked();
 
        void on_actionViewStickyScrolling_triggered();
 
@@ -106,7 +109,7 @@ private Q_SLOTS:
 private:
        DeviceManager &device_manager_;
 
-       std::vector< std::shared_ptr<Session> > sessions_;
+       std::list< std::shared_ptr<Session> > sessions_;
 
        std::map< std::shared_ptr<QDockWidget>,
                std::shared_ptr<pv::view::View> > view_docks_;
index b53360fcd9508bbbc559978181a33cfe88ecad71..410a0b3d8be9b4235babbd0d4f0919154466ae9b 100644 (file)
@@ -148,6 +148,11 @@ void Session::set_name(QString name)
        name_changed();
 }
 
+const std::list< std::shared_ptr<pv::view::View> > Session::views() const
+{
+       return views_;
+}
+
 std::shared_ptr<pv::view::View> Session::main_view() const
 {
        return main_view_;
index 79ec42cd4956354be4c12d08df776c40cddba1b9..19860b6b5ad3c26ce04a2a2a52935d3aed535f65 100644 (file)
@@ -110,6 +110,8 @@ public:
 
        void set_name(QString name);
 
+       const std::list< std::shared_ptr<pv::view::View> > views() const;
+
        std::shared_ptr<pv::view::View> main_view() const;
 
        void set_main_bar(std::shared_ptr<pv::toolbars::MainBar> main_bar);