From: Soeren Apel Date: Fri, 2 Sep 2016 16:20:41 +0000 (+0200) Subject: Allow dock windows to be closed and handle this properly X-Git-Tag: pulseview-0.4.0~252 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=36a8185e7e594990d475e6a043d5924605ca0f58;p=pulseview.git Allow dock windows to be closed and handle this properly --- diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index dab9c786..8787073d 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -186,7 +186,14 @@ shared_ptr 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 + ("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 MainWindow::add_session() return session; } +void MainWindow::remove_session(shared_ptr session) +{ + for (shared_ptr 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 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(w); + if (dock) + break; + w = w->parent(); + } + + // Get the view contained in the dock widget + shared_ptr view; + + for (auto entry : view_docks_) + if (entry.first.get() == dock) + view = entry.second; + + // Deregister the view + for (shared_ptr 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 view = get_active_view(); diff --git a/pv/mainwindow.hpp b/pv/mainwindow.hpp index 2bd8dadb..0d2db8cb 100644 --- a/pv/mainwindow.hpp +++ b/pv/mainwindow.hpp @@ -76,6 +76,8 @@ public: std::shared_ptr add_session(); + void remove_session(std::shared_ptr 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 > sessions_; + std::list< std::shared_ptr > sessions_; std::map< std::shared_ptr, std::shared_ptr > view_docks_; diff --git a/pv/session.cpp b/pv/session.cpp index b53360fc..410a0b3d 100644 --- a/pv/session.cpp +++ b/pv/session.cpp @@ -148,6 +148,11 @@ void Session::set_name(QString name) name_changed(); } +const std::list< std::shared_ptr > Session::views() const +{ + return views_; +} + std::shared_ptr Session::main_view() const { return main_view_; diff --git a/pv/session.hpp b/pv/session.hpp index 79ec42cd..19860b6b 100644 --- a/pv/session.hpp +++ b/pv/session.hpp @@ -110,6 +110,8 @@ public: void set_name(QString name); + const std::list< std::shared_ptr > views() const; + std::shared_ptr main_view() const; void set_main_bar(std::shared_ptr main_bar);